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
14
votes
10 answers

best algorithm for finding distance for all pairs where edges' weight is 1

As the title said, I'm trying to implement an algorithm that finds out the distances between all pairs of nodes in given graph. But there is more: (Things that might help you) The graph is unweighted. Meaning that all the edges can be considered as…
RanZilber
  • 1,840
  • 4
  • 31
  • 42
14
votes
1 answer

Correct formulation of the A* algorithm

I'm looking at definitions of the A* path-finding algorithm, and it seems to be defined somewhat differently in different places. The difference is in the action performed when going through the successors of a node, and finding that a successor is…
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
14
votes
3 answers

Does java have an indexed minimum priority queue?

I need it for an implementation of Dijkstra's algorithm, and I do have my own implementation but documenting my code would be easier with java's own classes.
Fatso
  • 1,278
  • 16
  • 46
13
votes
3 answers

Implementation of BFS, DFS and Dijkstra

Is it true that the implementation of BFS, DFS and Dijkstra are almost the same, except that BFS uses queue, DFS uses stack, while Dijkstra uses min priority queue? More precisely. Can we use the following code for all of BFS, DFS, and Dijkstra,…
13
votes
5 answers

Does dijkstras algorithm relax the edges of the shortest path in order?

In "Introduction to algorithms, 3rd edition" exercise 24.3-5 wants an example that this is wrong (not always true). Is that possible? In my mind this is impossible because every edge is relaxed at a time when the path to the current vertice is…
Steinbitglis
  • 2,482
  • 2
  • 27
  • 40
13
votes
6 answers

dijkstra's algorithm - in c++?

for the past four days I am trying to understand the dijkstra's algorithm. But I can't. I have a vector of points. From that I created a cost matrix. But I don't know how to make the dijkstra's algorithm. Sources are available in net, but I am not…
prabhakaran
  • 5,126
  • 17
  • 71
  • 107
13
votes
9 answers

Does Dijkstra's algorithm apply even if there is only one negative weight edge?

Will Dijkstra's Algorithm work if the digraph has only one negative weight edge and does not contain negative weight cycles?
jsh6303
  • 2,010
  • 3
  • 24
  • 50
12
votes
3 answers

How does Dijkstra's self-stabilizing algorithm work?

I have read his seminal paper, Self-stabilizing systems in spite of distributed control. However, I don't quite get how the self-stabilizing algorithm works. I am most interested in his, 'solution' of k-state machines. The density of the paper is…
user427390
12
votes
2 answers

Simple game algorithm checking if the move is valid

I'm programming my first game and I have one last problem to solve. I need an algorithm to check if I can move a chosen ball to a chosen place. Look at this picture: The rule is, if I picked up the blue ball on the white background (in the very…
Ania
  • 450
  • 4
  • 14
12
votes
3 answers

Interpreting Dijkstra's Algorithm

I understand how to find the shortest path from start to finish as explained by the Dijkstra's Algorithm, what I do not understand is the interpretation. Here, from the graph in the picture, order added to my known set from A to E is…
i_use_the_internet
  • 604
  • 1
  • 9
  • 28
12
votes
4 answers

how to save shortest path in dijkstra algorithm

So first let's define Dijkstra algorithm: Dijkstra's algorithm finds single-source shortest paths in a directed graph with non-negative edge weights. I want to know how can I save the shortest path form s to t with Dijkstra algorithm. I searched on…
Daniel.V
  • 2,322
  • 7
  • 28
  • 58
12
votes
4 answers

Dijkstra's Algorithm: Why is it needed to find minimum-distance element in the queue

I wrote this implementation of Dijksta's Algorithm, which at each iteration of the loop while Q is not empty instead of finding the minimum element of the queue it takes the head of the queue. Here is the code i wrote #include #include…
338327
  • 123
  • 4
12
votes
3 answers

graph - Dijkstra for The Single-Source Longest Path

Ok, I posted this question because of this exercise: Can we modify Dijkstra’s algorithm to solve the single-source longest path problem by changing minimum to maximum? If so, then prove your algorithm correct. If not, then provide a counterexample.…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
11
votes
4 answers

What's the simplest algorithm/solution for a single pair shortest path through a real-weighted undirected graph?

I need to find a shortest path through an undirected graph whose nodes are real (positive and negative) weighted. These weights are like resources which you can gain or loose by entering the node. The total cost (resource sum) of the path isn't very…
foobars
  • 200
  • 2
  • 11
11
votes
5 answers

Dijkstra with Parallel edges and self-loop

If I have a weighted undirected Graph with no negative weights, but can contain multiple edges between vertex and self-loops, Can I run Dijkstra algorithm without problem to find the minimum path between a source and a destination or exists a…
Manuel
  • 247
  • 2
  • 3
  • 14