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
193
votes
13 answers

Why doesn't Dijkstra's algorithm work for negative weight edges?

Can somebody tell me why Dijkstra's algorithm for single source shortest path assumes that the edges must be non-negative. I am talking about only edges not the negative weight cycles.
Madu
  • 4,849
  • 9
  • 44
  • 78
175
votes
12 answers

How does Dijkstra's Algorithm and A-Star compare?

I was looking at what the guys in the Mario AI Competition have been doing and some of them have built some pretty neat Mario bots utilizing the A* (A-Star) Pathing Algorithm. (Video of Mario A* Bot In Action) My question is, how does A-Star…
KingNestor
  • 65,976
  • 51
  • 121
  • 152
154
votes
7 answers

Why use Dijkstra's Algorithm if Breadth First Search (BFS) can do the same thing faster?

Both can be used to find the shortest path from single source. BFS runs in O(E+V), while Dijkstra's runs in O((V+E)*log(V)). Also, I've seen Dijkstra used a lot like in routing protocols. Thus, why use Dijkstra's algorithm if BFS can do the same…
gingercat
  • 1,543
  • 2
  • 10
  • 4
131
votes
9 answers

Negative weights using Dijkstra's Algorithm

I am trying to understand why Dijkstra's algorithm will not work with negative weights. Reading an example on Shortest Paths, I am trying to figure out the following scenario: 2 A-------B \ / 3 \ / -2 \ / C From the…
Meir
  • 1,691
  • 2
  • 14
  • 15
129
votes
18 answers

Difference between Prim's and Dijkstra's algorithms?

What is the exact difference between Dijkstra's and Prim's algorithms? I know Prim's will give a MST but the tree generated by Dijkstra will also be a MST. Then what is the exact difference?
122
votes
8 answers

Understanding Time complexity calculation for Dijkstra Algorithm

As per my understanding, I have calculated time complexity of Dijkstra Algorithm as big-O notation using adjacency list given below. It didn't come out as it was supposed to and that led me to understand it step by step. Each vertex can be…
Meena Chaudhary
  • 9,909
  • 16
  • 60
  • 94
108
votes
3 answers

Why does Dijkstra's algorithm use decrease-key?

Dijkstra's algorithm was taught to me was as follows while pqueue is not empty: distance, node = pqueue.delete_min() if node has been visited: continue else: mark node as visited if node == target: break …
weeb
  • 1,939
  • 4
  • 18
  • 29
98
votes
11 answers

Find the shortest path in a graph which visits certain nodes

I have a undirected graph with about 100 nodes and about 200 edges. One node is labelled 'start', one is 'end', and there's about a dozen labelled 'mustpass'. I need to find the shortest path through this graph that starts at 'start', ends at…
Daniel
  • 2,032
  • 5
  • 21
  • 27
73
votes
8 answers

Bellman-Ford vs Dijkstra: Under what circumstances is Bellman-Ford better?

After a lot of Googling, I've found that most sources say that the Dijkstra algorithm is "more efficient" than the Bellman-Ford algorithm. But under what circumstances is the Bellman-Ford algorithm better than the Dijkstra algorithm? I know…
crazyCoder
  • 1,552
  • 3
  • 20
  • 25
67
votes
5 answers

What is difference between BFS and Dijkstra's algorithms when looking for shortest path?

I was reading about Graph algorithms and I came across these two algorithms: Dijkstra's algorithm Breadth-first search What is the difference between Dijkstra's algorithm and BFS while looking for the shortest-path between nodes? I searched a lot…
harrythomas
  • 1,407
  • 1
  • 13
  • 17
49
votes
3 answers

MongoDB + Neo4J vs OrientDB vs ArangoDB

I am currently on design phase of a MMO browser game, game will include tilemaps for some real time locations (so tile data for each cell) and a general world map. Game engine I prefer uses MongoDB for persistent data world. I will also implement a…
projectUnduli
  • 501
  • 1
  • 5
  • 5
41
votes
7 answers

Dijkstra's algorithm with negative weights

Can we use Dijkstra's algorithm with negative weights? STOP! Before you think "lol nub you can just endlessly hop between two points and get an infinitely cheap path", I'm more thinking of one-way paths. An application for this would be a…
orlp
  • 112,504
  • 36
  • 218
  • 315
35
votes
6 answers

Is Dijkstra's algorithm for directed or undirected graphs?

I keep trying to google this, but the results I'm finding are just adding to my confusion. It seems that it can be possibly used for both? If so, which is it designed for by default and what needs to change to make it work the non-default way…
Austin
  • 6,921
  • 12
  • 73
  • 138
35
votes
7 answers

How can I use binary heap in the Dijkstra algorithm?

I am writing code of dijkstra algorithm, for the part where we are supposed to find the node with minimum distance from the currently being used node, I am using a array over there and traversing it fully to figure out the node. This part can be…
Max
  • 9,100
  • 25
  • 72
  • 109
34
votes
4 answers

Dijkstra vs. Floyd-Warshall: Finding optimal route on all node pairs

I am reading up on Dijkstra's algorithm and the Floyd-Warshall algorithm. I understand that Dijkstra's finds the optimal route from one node to all other nodes and Floyd-Warshall finds the optimal route for all node pairings. My question is would…
pyt
  • 341
  • 1
  • 3
  • 3
1
2 3
99 100