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

Why does A* run faster than Dijkstra's algorithm?

Wikipedia says A* runs in O(|E|) where |E| is the number of edges in the graph. But my friend says A* is just a general case of Dijkstra's algorithm, and Dijkstra's algorithm runs in O(|E| + |V| log |V|). So I am confused about why A* runs faster…
Jessica
  • 2,335
  • 2
  • 23
  • 36
5
votes
2 answers

When will Dijkstra's algorithm and Prim's algorithm produce different outputs?

I know the difference between Prim's and Dijkstra's algorithm. The former produces a MST while latter gives shortest path from source to all node. Mathematically, these aren't the same, so we wouldn't always expect the two algorithms to produce the…
java_doctor_101
  • 3,287
  • 4
  • 46
  • 78
5
votes
2 answers

The most vital edge on the shortest path

I have this question form the Sedgewick's course on algorithms: "Critical edge. Given an edge-weighted digraph, design an E*log(V) algorithm to find an edge whose removal causes the maximal increase (possibly infinite) in the length of the shortest…
mosceo
  • 1,222
  • 11
  • 26
5
votes
3 answers

Is BFS best search algorithm?

I know about Dijkstra's agorithm, Floyd-Warshall algorithm and Bellman-Ford algorithm for finding the cheepest paths between 2 vertices in graphs. But when all the edges have the same cost, the cheapest path is the path with minimal number of edges?…
Ivan Kuckir
  • 2,327
  • 3
  • 27
  • 46
5
votes
2 answers

Dijkstra on Java: Getting Interesting Results Using a Fibonacci Heap vs. PriorityQueue

I recently made an initial comparison of the running time of Dijkstra's algorithm using two data structures, the Java-based PriorityQueue (based on a binary heap, if I'm not mistaken), and a Fibonacci heap. I used Java's currentTimeMillis() to make…
Fiery Phoenix
  • 1,156
  • 2
  • 16
  • 30
5
votes
2 answers

Dijkstra's Algorithm implementation giving incorrect results

I need some help implementing Dijkstra's Algorithm and was hoping someone would be able to assist me. I have it so that it is printing some of routes but it isn't capturing the correct costs for the path. Here is my node structure: class Node …
Yecats
  • 1,715
  • 5
  • 26
  • 40
5
votes
2 answers

Dijkstra's algorithm with priority queue

In my implementation of Dijkstra's algorithm I have 1 array with all nodes and 1 priority queue with all nodes. Whenever a node is dequeued I update all adjacent nodes with new distance and where it came from, so I can backtrack the path. The node…
Carlj901
  • 1,329
  • 4
  • 24
  • 39
5
votes
1 answer

Finding the cheapest path with ignoring one cost

I want to find the cheapest path between 2 vertices and I can choose one path which I can go for free., e.g: Cheapest path between vertices 1 and 6 is 1-3-4-5-6 - I go on edge 1-3 (cost 30) for free and it gives me total cost 21. Is there any other…
Paulina
  • 483
  • 3
  • 13
5
votes
3 answers

Deleting a node from the middle of a heap

Deleting a node from the middle of the heap can be done in O(lg n) provided we can find the element in the heap in constant time. Suppose the node of a heap contains id as its field. Now if we provide the id, how can we delete the node in O(lg n)…
Jonh
  • 199
  • 4
  • 9
5
votes
1 answer

best structure Graph to implement Dijkstra in prolog

The question is simple. How can I struct my Graph in SWI prolog to implement the Dijkstra's algorithm? I have found this but it's too slow for my job.
Chris P
  • 2,059
  • 4
  • 34
  • 68
5
votes
2 answers

Faster alternatives to Dijkstra's algorithm for GPS system

I'm a real speed freak if it gets to algorithms, and in the plugins I made for a game. The speed is.. a bit.. not satisfying. Especially while driving around with a car and you do not follow your path, the path has to be recalculated.. and it takes…
user1182183
5
votes
1 answer

Update STL Priority Queue when modifying references to internal data

Lets say I am writing Dijkstra's Algorithm, and I have a priority queue that is keeping the shortest distance node on the top. However as I traverse the graph I will be updating the distance to that vertex. I have placed references to all vertices…
Matthew Hoggan
  • 7,402
  • 16
  • 75
  • 140
4
votes
1 answer

Strategy to build test graphs for Dijkstra's algorithm?

I recently implemented Dijkstra's algorithm to practice Java. I'm now considering how to build random test graphs (with unidirectional edges). Currently, I use a naive method. Nodes are created at random locations in 2d space (where x and y are…
theazureshadow
  • 9,499
  • 5
  • 33
  • 48
4
votes
2 answers

Dijkstra linear running time on dense graph with Binary Heap

First: The general running time of Dijkstras Shortest Path algorithm is where m is the number of edges and n the number of vertices Second: the number of expected decreasekey operations is the following Third: The expected running time of dijkstra…
niklas
  • 2,887
  • 3
  • 38
  • 70
4
votes
6 answers

Build a list using specific keys in a dict (python)?

I'm implementing the Dijkstra search algorithm in Python. At the end of the search, I reconstruct the shortest path using a predecessor map, starting with the destination node's predecessor. For example: path = [] path.append(destination) previous =…
XåpplI'-I0llwlg'I -
  • 21,649
  • 28
  • 102
  • 151