Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8935175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:56:19+00:00 2026-06-15T09:56:19+00:00

Background I am trying to code Dijkstra’s algorithm in O(mlogn) time, where m is

  • 0

Background

I am trying to code Dijkstra’s algorithm in O(mlogn) time, where m is the number of edges and n is the number of nodes. I am using to find the shortest path between a given starting node and a given ending node. And I’m pretty new at this.

Here is the algorithm I have come up with:

Assume the graph is represented by an adjacency matrix and each node has a row index.

Initialize starting node distance to zero, and all other nodes to inifinity, in the heap.

Create a list of shortest paths, equal to the number of nodes in the graph, set to 0.

While the index of the node that corresponds to the minimum element in the heap 
has no value in the list of shortest paths and heap has node distances, do:
    Remove the minimum node distance from the heap, and bubble as necessary to fill the removed node.
    Put the minimum node distance into the list of shortest paths at its row index.

    For all nodes that were adjacent to the node with the minimum distance (that was just removed), do:
      Update the distances in the heap for the current node, using the following calculation:
        min((deleted node distance + adjacent edge weight), current node's distance)
    Reorganize the heap to be a minimum heap.

Return value in the list of shortest paths at the location of the end node.

This is O(mlogn) because you only update the distances once per edge.

“It takes linear time
to initialize the heap, and then we perform m updates at a cost of O(log n) each for a total time of O(mlog n).” – http://www.cs.cmu.edu/~avrim/451f07/lectures/lect1011.pdf

Problem

In order to update the distances from the starting vertex in the correct location in the heap, insertions to the heap must be key-value pairs – with the key being the node (row index) and the value being the distance.

There are lecture slides online that say each entry in a priority queue ADT is a key-value pair (otherwise, how could it prioritize?).

Question

The methods for PriorityQueue have at most one parameter, so how do you insert a key associated with a value?

This must be done in a single file with a specific name (i.e. It is my understanding that I can’t make a KeyValuePair class implementing Comparator).

I’d love to hear your thoughts.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:56:20+00:00Added an answer on June 15, 2026 at 9:56 am

    To use JDK’s implementation of priority queue for your application, you can maintain a Map<Key, Value> in addition to PriorityQueue<Value>. In your case, Key represents a node and Value is an object that holds the shortest distance to a node. To update the distance to a node, you first look up its corresponding distance object in the map. Then, you remove the distance object from the priority queue. Next, you update the distance object. Finally, you insert the distance object back in the priority queue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set an image as the background using this code: root.setStyle(-fx-background-image: url('splash.jpg');
Background : I'm trying to convert some JavaScript code which uses the the Crossfilter
I'm trying to execute a long-running piece of code in a background thread and
Greetings, coders, Background Info and Code I am trying to create a daemon-type program
for the past four days I am trying to understand the dijkstra's algorithm. But
I am trying to code a clean html5 website. I am using the html5
Background: I'm trying to get a total line count of all of the code
I am trying to code a chrome extension, and I have a background.html with
I am trying to speed up a piece of code by having background threads
Background: Trying to make a simple drop the ball game. The code is located

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.