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
7
votes
5 answers

Find the shortest path with the least number of edges

I need to modify Dijkstra's algorithm so that if there are several shortest paths I need to find the one with minimum number of edges on the path. I've been stuck on how to use Dijkstra's method to find multiple shortest paths, how do you do that?…
Jacek Trociński
  • 882
  • 1
  • 8
  • 23
7
votes
2 answers

Fastest algorithm to detect if there is negative cycle in a graph

I use a matrix d to present a graph. d.(i).(j) means the distance between i and j; v denotes the number of nodes in the graph. It is possible that there is negative cycle in this graph. I would like to check if a negative cycle exists. I have…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
7
votes
3 answers

Dijkstra's algorithm: memory consumption

I have an implementation of Dijkstra's Algorithm, based on the code on this website. Basically, I have a number of nodes (say 10000), and each node can have 1 to 3 connections to other nodes. The nodes are generated randomly within a 3d space. The…
John
  • 536
  • 5
  • 16
7
votes
3 answers

Modify Dijkstra's Algorithm to get the Shortest Path Between Two Nodes

So I've seen similar questions to this but not quite exactly what I'm looking for. I need to modify Dijkstra's Algorithm to return the shortest path between a vertex S (source) and a vertex X (destination). I think I've figured out what to do, but…
csnate
  • 1,601
  • 4
  • 19
  • 31
7
votes
1 answer

Boost's Dijkstra's Algorithm Tutorial

I am having difficulty figuring out how to use Boost's Dijkstra's algorithm. I have gone over their example and documentation, but I still cannot understand how to use it. [Boost's documentation:…
Qman
  • 377
  • 1
  • 4
  • 14
7
votes
6 answers

Can we change Dijkstra's Algorithm to work with negative weights?

The pseudocode as taken from Wikipedia: function Dijkstra(Graph, source): 2 for each vertex v in Graph: // Initializations 3 dist[v] := infinity ; // Unknown distance…
Ori Popowski
  • 10,432
  • 15
  • 57
  • 79
6
votes
1 answer

Time complexity for Dijkstra's algorithm with min heap and optimizations

What is the time complexity of this particular implementation of Dijkstra's algorithm? I know several answers to this question say O(E log V) when you use a min heap, and so does this article and this article. However, the article here says…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
6
votes
2 answers

How do I select the node that minimizes the maximum shortest distance to the other nodes in a graph?

I have an undirected, connected, positively-weighted graph G = . I need to find one node v_min in V such that the maximum distance between v_min and the other nodes are minimized. I've learned that this is a special problem of the k-center…
6
votes
1 answer

What data structures to use for Dijkstra's algorithm in Erlang?

Disclaimer: The author is a newbie in Erlang. Imagine, we have a graph consisting of 1M nodes, and each node has 0-4 neighbours (the edges are emanating from each node to those neighbours, so the graph is directed and connected). Here is my choice…
skanatek
  • 5,133
  • 3
  • 47
  • 75
6
votes
2 answers

Performance of Dijkstra's algorithm implementation

Below is an implementation of Dijkstra's algorithm I wrote from the pseudocode in the Wikipedia article. For a graph with about 40 000 nodes and 80 000 edges, it takes 3 or 4 minutes to run. Is that anything like the right order of magnitude? If…
zoo
  • 1,901
  • 1
  • 17
  • 25
6
votes
1 answer

Shortest Path with Dijkstra

I using this exact code for this. I modified it a little. So far I added a start and end node index to the calculateShortestDistances() method. Also the path ArrayList for collecting the path node indexes. Also: new to Java... How do I collect the…
Ákos Nikházy
  • 1,276
  • 17
  • 33
6
votes
1 answer

Postgres Query Based on Previous and Next Rows

I'm trying to solve the bus routing problem in postgresql which requires visibility of previous and next rows. Here is my solution. Step 1) Have one edges table which represents all the edges (the source and target represent vertices (bus…
Royal Pinto
  • 2,869
  • 8
  • 27
  • 39
6
votes
3 answers

What is the space complexity of Dijkstra Algorithm?

The time complexity for Dijkstra Algorithm using an array is O(V^2) and if priority queue is implemented, we can further improve the complexity to O(E log V). But what about its space complexity? Is it O(V) in both cases?
troopers97
  • 91
  • 1
  • 1
  • 5
6
votes
2 answers

In pathfinding, what is the difference between DFS and Dijkstra?

I'm studying about DFS and Dijkstra. In my simple test cases, most of them shows that DFS is faster. Passing every nodes costs the same in my test cases. But most people prefer Dijkstra to DFS in pathfinding because Dijkstra is so accurate. So,…
Gengaozo
  • 51
  • 1
  • 3
6
votes
2 answers

Dijkstra Algorithm with Chebyshev Distance

I have been using Dijkstra Algorithm to find the shortest path in the Graph API which is given by the Princeton University Algorithm Part 2, and I have figured out how to find the path with Chebyshev Distance. Even though Chebyshev can move to any…