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 6610703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:52:27+00:00 2026-05-25T19:52:27+00:00

Update : I really botched the original question. My original title was Why do

  • 0

Update: I really botched the original question. My original title was “Why do we first do a topological sort for acyclic weighted digraph shortest path problems?” but my question content was about Dijkstra’s algorithm. Hopefully since I’ve changed the title, the question is updated in such a way that it is useful to someone else. The answer to the updated question title is “yes“.

Original question:

Why do I need to do a topological sort first? (see code here) Can’t I just use Dijkstra’s algorithm shown below and avoid the topological sort altogether (little messy syntax-wise but you get the idea)

MinIndexedPriorityQueue waitingEdges = new MinIndexedPriorityQueue
Graph g //some weighted directed graph
double[] distTo = new double[g.vertexCount]
Edge[] edgeTo = new Edge[g.vertexCount]
int source = //init to some value

void minPathInit()
    init distTo to double.MAX_VALUE
    //init first node
    distTo [source] = 0
    visit(source)
    while waitingEdges.count>0
        int vertex = waitingEdges.dequeue()
        relax(vertex )

void relax(v) //note that visit has been renamed to relax
    for each edge in graph.getEdgesFrom(v)
        int to= edge.to
        if edge.weight + distTo [edge.from]<  distTo [to]
            distTo[to] = edge.weight + distTo [edge.from]
            edgeTo[to] = edge
            if waitingEdges.contains(to)
                waitingEdges.change(to,  distTo[to] )
            else
                waitingEdges.enqueue(to,  distTo[to] )


//after everything is initialized
getPathTo(v)
    if not hasBeenVisited[v]
        return null
    Stack path = new Stack
    while edgeTo[v] != source
        path.push(edgeTo[v])
        v = edgeTo[v].from
    return path

I can understand why Dijkstra’s algorithm can’t handle negative cycles (because it would get stuck in an infinite loop) but if there are no negative cycles, why does it fail as shown (and require the topological sort first)

Update: Ok, I can see that I’ve botched up this question so I will try to fix it up a bit with an update. Thanks for taking the time to point the hole’s out for me. I mistakenly thought AcyclicSP becomes Dijkstra’s algorithm when removing the topological sort which is not the case.

However, my question about Dijkstra’s algorithm (using the version shown above) remains. Why can’t it be used even if there is a negative weight so long as there are no cycles? There is a java version of Dijkstra’s algorithm here. Mine is very similar to this (since this guy’s book is where I learned about it) but his example is probably easier to read for some of you.

  • 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-05-25T19:52:28+00:00Added an answer on May 25, 2026 at 7:52 pm

    You don’t make any topological sort in the original algorithm. But in the case of an a-cyclic graph, then you can decrees the running time to O(V) (while the original running time is O(|V|*log(|V|)).
    The reason is that you sort in O(|V|) time, and then you can use that order, and don’t need any heap (or priority queue). So the over all time decreases to O(|V|).

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

Sidebar

Related Questions

Update: What I really wanted all along were greenlets . Note: This question mutated
UPDATE So totally pulled a tool moment. I really meant by reference versus Out/Ref.
Update: Now that it's 2016 I'd use PowerShell for this unless there's a really
UPDATED QUESTION: Ok, I am going to simplify my question since I don't really
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update: giving a much more thorough example. The first two solutions offered were right
I have a rather basic and general question about atomicity of UPDATE ... SET
UPDATE: After some additional reading, what I really wanted was guaranteed early binding (which
Update: based on Lee's comment I decided to condense my code to a really
I need to update (well, rewrite really) a SMALL VB6 application which uses ADO

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.