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
21
votes
2 answers

Why do we call it "Relaxing" an edge?

In Dijkstra's shortest path algorithm and others, to examine an edge to see if it offers a better path to a node is referred to as relaxing the edge. Why is it called relaxing?
Eric G
  • 4,018
  • 4
  • 20
  • 23
21
votes
2 answers

graph - Shortest path with Vertex Weight

Here is an excise: In certain graph problems, vertices have can have weights instead of or in addi- tion to the weights of edges. Let Cv be the cost of vertex v, and C(x,y) the cost of the edge (x, y). This problem is concerned with finding…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
20
votes
10 answers

Understanding Dijkstra's Mozart programming style

I came across this article about programming styles, seen by Edsger Dijsktra. To quickly paraphrase, the main difference is Mozart, when the analogy is made to programming, fully understood (debatable) the problem before writing anything, while…
hlfcoding
  • 2,532
  • 1
  • 21
  • 25
20
votes
1 answer

Dijkstra algorithm with min-priority queue

I'm trying to implement the dijkstra algorithm with priority queue, but I can't understand how it works. I read many guide on the web but I can't understand this algorithm at all. My questions are: What is the priority for each node? I think that…
giacomotb
  • 607
  • 4
  • 10
  • 24
20
votes
5 answers

Is A* really better than Dijkstra in real-world path finding?

I'm developing a path finding program. It is said theoretically that A* is better than Dijkstra. In fact, the latter is a special case of the former. However, when testing in the real world, I begin to doubt that is A* really better? I used data of…
HanXu
  • 5,507
  • 6
  • 52
  • 76
20
votes
3 answers

What are some must-read EWDs?

Dijkstra was one of the most prolific computer scientists. He wrote the famous EWDs. It is not feasible to read them all. But I think there are some we all must read. Which of them are a must-read?
Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146
17
votes
7 answers

Dijkstra for longest path in a DAG

I am trying to find out if it is possible to use Dijkstra's algorithm to find the longest path in a directed acyclic path. I know that it is not possible to find the longest path with Dijkstra in a general graph, because of negative cost cycles. But…
punkyduck
  • 669
  • 2
  • 9
  • 17
17
votes
3 answers

Finding a shortest path that passes through some arbitrary sequence of nodes?

In this earlier question the OP asked how to find a shortest path in a graph that goes from u to v and also passes through some node w. The accepted answer, which is quite good, was to run Dijkstra's algorithm twice - once to get from u to w and…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
17
votes
8 answers

Dijkstra's algorithm to find all the shortest paths possible

I'm working on Dijkstra's algorithm, and I really need to find all the possible shortest paths, not just one. I'm using an adjacency matrix and I applied Dijkstra's algorithm, and I can find the shortest path. But I need to find all the paths with…
Darksody
  • 319
  • 1
  • 3
  • 8
17
votes
3 answers

Why use Dijkstra's algorithm instead of Best (Cheapest) First Search?

From what I have read so far. The Best First Search seems faster in terms of finding the shortest path to the goal because Dijkstra's algorithm has to relax all the nodes as it traverses the graph. What makes Dijkstra's algorithm better than Best…
Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90
16
votes
9 answers

How do you solve the 15-puzzle with A-Star or Dijkstra's Algorithm?

I've read in one of my AI books that popular algorithms (A-Star, Dijkstra) for path-finding in simulation or games is also used to solve the well-known "15-puzzle". Can anyone give me some pointers on how I would reduce the 15-puzzle to a graph of…
Sean
16
votes
7 answers

Efficiently finding the shortest path in large graphs

I'm looking to find a way to in real-time find the shortest path between nodes in a huge graph. It has hundreds of thousands of vertices and millions of edges. I know this question has been asked before and I guess the answer is to use a…
Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122
16
votes
1 answer

What algorithms did Dijkstra develop?

I recently asked a question about one of the Dijkstra’s algorithms (shunting-yard). But almost everyone thought "Dijkstra's algorithm" meant his shortest path algorithm. What other algorithms has Dijkstra developed?
baris.aydinoz
  • 1,902
  • 2
  • 18
  • 28
16
votes
2 answers

How to implement Dijkstra Algorithm in Haskell

For my studies I have to write the following function which gets the shortest route between two countries. I already have already written a function isRoute which checks if there is a connection between two countries, and a function yieldRoute which…
Jakob Abfalter
  • 4,980
  • 17
  • 54
  • 94
15
votes
8 answers

Finding fastest path at additional condition

I'm wondering, if Dijkstra's algorithm will work properly when there is more than one direct connection in an undirected graph. E.g.: I want to use Dijkstra to find the fastest path but, there's an additional condition. Sum of all additional_data…
Patryk
  • 3,042
  • 11
  • 41
  • 83