Questions tagged [dijkstra]

Dijkstra's algorithm, conceived by Dutch computer scientist Edsger Dijkstra is a graph search algorithm that solves the single-source shortest path problem for a connected graph with nonnegative edge path costs, producing a shortest path tree. This algorithm is often used in routing and as a subroutine in other graph algorithms.

For a given source vertex (node) in the connected graph, the algorithm finds the path with lowest cost (i.e. the shortest path) between that vertex and every other vertex. It can also be used for finding costs of shortest paths from a single vertex to a single destination vertex by stopping the algorithm once the shortest path to the destination vertex has been determined. For example, if the vertices of the graph represent cities and edge path costs represent driving distances between pairs of cities connected by a direct road, Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. As a result, the shortest path first is widely used in network routing protocols, most notably IS-IS and OSPF (Open Shortest Path First).

Pseudocode :

function Dijkstra(Graph, source):
   for each vertex v in Graph:            // Initializations
       dist[v] := infinity ;              // Unknown distance function from source to v
       previous[v] := undefined ;         // Previous node in optimal path from source
   end for ;
   dist[source] := 0 ;                    // Distance from source to source
   Q := the set of all nodes in Graph ;   // All nodes in the graph are unoptimized - thus are in Q
   while Q is not empty:                  // The main loop
       u := vertex in Q with smallest distance in dist[] ;
      if dist[u] = infinity:
          break ;                        // all remaining vertices are inaccessible from source
      end if ;
      remove u from Q ;
      for each neighbor v of u:          // where v has not yet been removed from Q.
          alt := dist[u] + dist_between(u, v) ;
          if alt < dist[v]:              // Relax (u,v,a)
              dist[v] := alt ;
              previous[v] := u ;
              decrease-key v in Q;       // Reorder v in the Queue
          end if ;
      end for ;
  end while ;
  return dist[] ;
end Dijkstra.

Reference :

1895 questions
32
votes
3 answers

Dijkstra's Algorithm and Cycles

It's stated in a book that "Dijkstra's algorithm only works with Directed Acyclic Graphs". It appears the algorithm works for graphs with cycles too as long as there are no negative cycles. Is that correct? Edit 1: The book "Grokking Algorithms"…
sam
  • 777
  • 2
  • 6
  • 19
30
votes
1 answer

AStar - explanation of name

I am looking for an explanation why the AStar / A* algorithm is called AStar. All similar (shortest path problem) algorithms are often named like its developer(s), so what is AStar standing for?
29
votes
3 answers

Dijkstra on "Software Engineering"

Edsger Dijkstra, who could be somewhat abrasive at times (he called "Carl Friedrich Gauss, the Prince of Mathematicians but also somewhat of a coward") said in his essay "On the cruelty of really teaching computing science" (EWD1036): A number of…
cjs
  • 25,752
  • 9
  • 89
  • 101
28
votes
5 answers

Use Dijkstra's to find a Minimum Spanning Tree?

Dijkstra's is typically used to find the shortest distance between two nodes in a graph. Can it be used to find a minimum spanning tree? If so, how? Edit: This isn't homework, but I am trying to understand a question on an old practice exam.
25
votes
5 answers

Which datatype to use as queue in Dijkstra's algorithm?

I'm trying to implement Dijkstra's algorithm in Java (self-study). I use the pseudo-code provided by Wikipedia (link). Now near the end of the algorithm, I should decrease-key v in Q;. I guess i should have implemented Q with a BinaryHeap or…
Dänu
  • 5,791
  • 9
  • 43
  • 56
25
votes
13 answers

Dijkstra's algorithm in python

I am trying to implement Dijkstra's algorithm in python using arrays. This is my implementation. def extract(Q, w): m=0 minimum=w[0] for i in range(len(w)): if w[i]
user3504080
  • 259
  • 1
  • 3
  • 3
24
votes
2 answers

Dijkstra's algorithm a greedy or dynamic programming algorithm?

In this post it is described Dijkstras as a greedy algorithm, while here and here it is shown to have connections with dynamic programming algorithms. Which one is it then?
vkaul11
  • 4,098
  • 12
  • 47
  • 79
23
votes
2 answers

Dijkstra algorithm. Min heap as a min-priority queue

I'm reading about Dijkstra's algorithm in CLRS, Third Edition (p. 662). Here is a part from the book I don't understand: If the graph is sufficiently sparse — in particular, E = o(V^2/lg V) — we can improve the algorithm by implementing the…
Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138
23
votes
5 answers

Find cycle of shortest length in a directed graph with positive weights

I was asked this question in an interview, but I couldn't come up with any decent solution. So, I told them the naive approach of finding all the cycles then picking the cycle with the least length. I'm curious to know what is an efficient solution…
Chander Shivdasani
  • 9,878
  • 20
  • 76
  • 107
23
votes
4 answers

What is the purpose of the visited set in Dijkstra?

On the Wikipedia page for Dijkstra's algorithm, they mark visited nodes so they wouldn't be added to the queue again. However, if a node is visited then there can be no distance to that node that is shorter, so doesn't the check alt < dist[v]…
Duncan
  • 984
  • 10
  • 20
23
votes
4 answers

Why does Dijkstra's Algorithm use a heap (priority queue)?

I have tried using Djikstra's Algorithm on a cyclic weighted graph without using a priority queue (heap) and it worked. Wikipedia states that the original implementation of this algorithm does not use a priority queue and runs in O(V2) time. Now if…
Anshu Kandhari
  • 369
  • 1
  • 3
  • 9
22
votes
7 answers

Teaching programming and formal methods

Here's a sort of odd question. I'm in the process of writing a book on learning to program using formal methods, and I'm going to target it toward people with some programming experience. The idea is to teach them to be high-quality…
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
22
votes
7 answers

Bus public transport algorithm

I am working on an offline C# application that can find bus routes. I can extract the timetable/bus/route data. I am searching for the most simple solution that will work with basic data. What algorithm can be used to find a route from bus stop…
Daniel Novak
  • 2,746
  • 3
  • 28
  • 37
22
votes
2 answers

Am I right about the differences between Floyd-Warshall, Dijkstra's and Bellman-Ford algorithms?

I've been studying the three and I'm stating my inferences from them below. Could someone tell me if I have understood them accurately enough or not? Thank you. Dijkstra's algorithm is used only when you have a single source and you want to know…
GrowinMan
  • 4,891
  • 12
  • 41
  • 58
21
votes
4 answers

Is Dijkstra's algorithm, dynamic programming

All implementation of Dijkstra's algorithms I have seen do not have a recursive function, but I have also read that by definition dynamic programming is an algorithm with a recursive function and "memory" of things already calculated. So is…
user3725938
  • 227
  • 1
  • 2
  • 8