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

Dijkstra algorithm for 2D array in Java

This is for a school project; I'm running into a huge amount of trouble, and I can't seem to find a understandable solution. a b c d e z a - 2 3 - - - b 2 - - 5 2 - c 3 - - - 5 - d - 5 - - 1 2 e - 2 5 1 - 4 z - - - 2 4 - That's the two…
Stan
  • 79
  • 1
  • 1
  • 4
7
votes
4 answers

Using Dijkstra's algorithm to find a path that can carry the most weight

I have a graph, with X nodes and Y edges. Weighted edges. The point is to start at one node, and stop at another node which is the last location. Now here comes the problem: Visualize the problem. The edges are roads, and the edge weights are the…
noor asiah
  • 71
  • 1
  • 2
7
votes
5 answers

Find shortest path from Vertex u to v passing through a vertex w?

In a directed graph with non-negative edge weights I can easily find shortest path from u to v using dijkstra's. But is there any simple tweak to Dijkstra's so that I can find shortest path from u to v through a given vertex w. Or any other…
Pradhan
  • 421
  • 5
  • 17
7
votes
5 answers

Fastest implementation for All-pairs shortest paths problem?

I have a weighted graph 30k nodes 160k edges, no negative weights. I would like to compute all the shortest paths from all the nodes to the others. I think I cannot assume any particular heuristics to simplify the problem. I tried to use this…
Eugenio
  • 3,195
  • 5
  • 33
  • 49
7
votes
1 answer

Can Dijkstra's Algorithm work on a graph with weights of 0?

If there exists a weighted graph G, and all weights are 0, does Dijkstra's algorithm still find the shortest path? If so, why? As per my understanding of the algorithm, Dijsktra's algorithm will run like a normal BFS if there are no edge weights,…
danielschnoll
  • 3,045
  • 5
  • 23
  • 34
7
votes
2 answers

Dijkstra's Algorithm modification

Let G (V, E) be a weighted, directed graph with non negative weight function W : E -> {0, 1, 2... W } for some non negative integer W . How to modify Dijkstra’s algorithm to compute the shortest paths from a given source vertex s in O(V W + E) time.
Dhruv Singh
  • 2,185
  • 2
  • 11
  • 17
7
votes
3 answers

Shortest path in graph where cost depends on the history of traversing

My goal is to find the shortest path (lowest cost) between given cities (vertices) connected with roads (edges). Each road and each city has charge (cost) that has to be paid before entering that road/city. If that would be whole task, I would use…
3voC
  • 647
  • 7
  • 19
7
votes
2 answers

Complexity Of Dijkstra's algorithm

I read from many sources that Dijkstra's Shortest Path also will run in O(V^2) complexity if using a naive way to get the min element (linear search). However, it can be optimised to O(VLogV) if priority queue is used as this data structure will…
Donald
  • 1,300
  • 2
  • 13
  • 29
7
votes
2 answers

About Dijkstra's paper

I am reading Coders at Work. I came across this paragraph in Donald Knuth's interview. Seibel: It seems a lot of the people I’ve talked to had direct access to a machine when they were starting out. Yet Dijkstra has a paper I’m sure you’re familiar…
Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146
7
votes
2 answers

Make Boost Dijkstra algorithm stop when it reaches a destination node

I'm using boost::graph and its Dijkstra implementation. When someone is using the Dijkstra algorithm, it may be to know the shortest path between 2 nodes in a graph. But as you need to check all nodes in the graph to find the shortest path, usually…
Emmanuel Jay
  • 484
  • 3
  • 10
7
votes
4 answers

Dijkstra's Algorithm for Negative Weights

Okay, first of all I know Dijkstra does not work for negative weights and we can use Bellman-ford instead of it. But in a problem I was given it states that all the edges have weights from 0 to 1 (0 and 1 are not included). And the cost of the path…
S. Salman
  • 590
  • 1
  • 6
  • 22
7
votes
1 answer

Dijkstra graph with a table of weights on each edge

I have a boost graph with multiples weights for each edges (imagine one set of weights per hour of the day). Every one of those weights values is stored in a propretyEdge class : class propretyEdge { std::map weights; //…
Emmanuel Jay
  • 484
  • 3
  • 10
7
votes
2 answers

How to implement Dijkstra's algorithm in Neo4j using Cypher

My question is: is it possible to implement Dijkstra's algorithm using Cypher? the explanation on the neo4j website only talks about REST API and it is very difficult to understand for a beginner like me Please note that I want to find the shortest…
Shazu
  • 587
  • 3
  • 10
7
votes
2 answers

The Big O on the Dijkstra Fibonacci-heap solution

From Wikipedia: O(|E| + |V| log|V|) From Big O Cheat List: O((|V| + |E|) log |V|) I consider there is a difference between E + V log V and (E+V) log V, isn't there? Because, if Wikipedia's one is correct, shouldn't it be shown as O(|V| log |V|) only…
Nikola
  • 2,093
  • 3
  • 22
  • 43
7
votes
3 answers

Why the tree resulting from Kruskal is different from Dijkstra?

Can anyone explain why the tree resulting from Kruskal is different from Dijkstra ? I know for the fact that kruskal works on nondescending order of edges but Dijkstra takes advantage of priority queue but still cannot understand why the resulted…
HMdeveloper
  • 2,772
  • 9
  • 45
  • 74