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
11
votes
4 answers

How to reverse a graph in linear time?

I know there are two ways to represent my graph: one is using a matrix, and the other one is using a list. If I use a matrix, I have to flip all the bits in the matrix. Doesn't that take O(V^2) time? If I use a list, wouldn't I have to traverse each…
Timothy Leung
  • 1,407
  • 7
  • 22
  • 39
11
votes
5 answers

How to modify dijkstra algorithm to find all possible paths?

I know that could be asked before already but I cannot find it. I need to modify below dijkstra algorithm which works good for finding shortest path between 2 nodes but I need to find all possible paths also. I know it should be relatively easy to…
Zulu Z
  • 1,103
  • 5
  • 14
  • 27
11
votes
2 answers

Find shortest path between two articles in english Wikipedia in Python

The question: Find shortest path between two articles in english Wikipedia. Path between article A and B exist if there are articles C(i) and there is a link in article A that leads to article C(1), in article C(1) link that leads to article C(2),…
Jefferson X Masonic
  • 583
  • 2
  • 12
  • 27
11
votes
3 answers

Python Dijkstra k shortest paths

I'm trying to make a small public transport routing application. My data is represented in a following structure: graph = {'A': {'B':3, 'C':5}, 'B': {'C':2, 'D':2}, 'C': {'D':1}, 'D': {'C':3}, 'E': {'F':8}, 'F':…
Volodymyr Smirnov
  • 305
  • 1
  • 2
  • 9
11
votes
2 answers

Finding the shortest route using Dijkstra algorithm

I need to find the shortest route between 2 vertices of a graph. I have a matrix, which contains all the weights. How can I do it? Currently, I have the following code: private int[] Dijkstra(int start, int end) { bool[] done = new…
SirSergio
  • 159
  • 1
  • 3
  • 8
10
votes
2 answers

Dijkstra Shortest Path with VertexList = ListS in boost graph

I am quite new to Boost graph. I am trying to adapt an example for finding Dijkstra Shortest Path algorithm which used VertexList = vecS. I changed the vertex container to ListS. I learned that we have to provide our own vertex_index for the…
srkrish
  • 273
  • 4
  • 12
10
votes
2 answers

What type of heap is used and time complexity of std::priority_queue in c++?

What do I want to know I want to ask about following two question. What type of heap is used in std::priority_queue in C++? What is the time complexity of top(), pop(), push() operation for std::priority_queue in C++? I checked on the Internet,…
square1001
  • 1,402
  • 11
  • 26
10
votes
1 answer

Best Dijkstra papers to explain this quote?

I was enjoying "The Humble Programmer" earlier today and ran across this choice quote: Therefore, for the time being and perhaps forever, the rules of the second kind present themselves as elements of discipline required from the programmer. Some…
jemfinch
  • 2,851
  • 19
  • 24
10
votes
5 answers

Query regarding dijkstra algorithm

I am trying to find shortest path between two nodes in a dataset. I implemented dijkstra algorithm and am using it to prove given two nodes (like: Andrew_Card and Dick_Cheney) there does not exist a path between the source and destination. However,…
Jannat Arora
  • 2,759
  • 8
  • 44
  • 70
10
votes
1 answer

how to Update a key in Priority Queue in O(log n ) time in dijkstra's algorithm?

I have been working on dijkstra's algorithm for the past one week one I have the correct running code for it in java. It is using array for the computation of standard findMin function which gives you the vertex with smallest distance.Obviously it…
Max
  • 9,100
  • 25
  • 72
  • 109
9
votes
1 answer

What is a bucket or double-bucket data structure?

I'm doing some reading about shortest path algorithm implementations and have been running into over and over that implementing Dijkstra's Algorithm with a Double-Bucket data structure is a good implementation. However I cannot seem to find what a…
Carson
  • 1,147
  • 5
  • 19
  • 41
9
votes
5 answers

How to find nearest vector in {0,1,2}^12, over and over again

I'm searching a space of vectors of length 12, with entries 0, 1, 2. For example, one such vector is 001122001122. I have about a thousand good vectors, and about a thousand bad vectors. For each bad vector I need to locate the closest good…
Josephine
  • 1,409
  • 1
  • 9
  • 11
9
votes
2 answers

Dijkstra's algorithm in C++

I'm required to implement the Dijkstra's algorithm via ADT graph using the adjacency matrix representation for finding a shortest path by enhancing the pseudo code below using either C/C++ language. procedure Dijkstra(G, w, r, Parent[0:n-1], Dist) …
Abby
  • 101
  • 7
9
votes
2 answers

Dijkstra shortest path algorithm with edge cost

I have a directed, positive weighted graph. Each edge have a cost of use. I have only A money, i want to calculate shortest paths with dijkstra algorithm, but sum of edges costs on route must be less or equal to A. I want to do this with most…
Svisstack
  • 16,203
  • 6
  • 66
  • 100
9
votes
4 answers

Complete graph with only two possible costs. What's the shortest path's cost from 0 to N - 1

You are given a complete undirected graph with N vertices. All but K edges have a cost of A. Those K edges have a cost of B and you know them (as a list of pairs). What's the minimum cost from node 0 to node N - 1. 2 <= N <= 500k 0 <= K <= 500k 1 <=…