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

Can Dijkstra's Single Source Shortest Path Algorithm detect an infinite cycle in a graph?

So I came to this beautiful problem that asks you to write a program that finds whether a negative infinity shortest path exists in a directed graph. (Also can be thought of as finding whether a "negative cycle" exists in the graph). Here's a link…
Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83
9
votes
4 answers

Are there faster algorithms than Dijkstra?

Given a directed, connected graph with only positive edge weights, are there faster algorithms for finding the shortest path between two vertices, than Dijkstra using a fibonacci heap? Wikipedia says, that Dijkstra is in O(|E| + |V| * log(|V|))…
Dave O.
  • 2,231
  • 3
  • 21
  • 25
9
votes
3 answers

How can I emulate pointers in Haskell?

I am trying to implement Dijkstra's algorithm in Haskell. I have already implemented a binary heap using a tree. In the algorithm neighbours keys of the current vertex should be updated in the heap. How can I emulate pointer to value in the heap in…
9
votes
1 answer

Is there any implementation of bidirectional search for Dijkstra algorithm?

I am looking for an implementation of bidirectional search (a.k.a. "meet in the middle" algorithm) for Dijkstra (or any other source-to-destination shortest path algorithm) in Java. As bidirectional search processing is trickier than it looks like…
Fabien
  • 965
  • 3
  • 11
  • 23
8
votes
1 answer

Dijkstra's algorithm: What to do if there are two or more nodes with minimal weight?

In Dijkstra's algorithm what should I do if there are two or more nodes with minimal weights at a point in the algorithm? In wikipedia: http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm at step no. 6, it says "Set the unvisited node marked with…
coolscitist
  • 3,317
  • 8
  • 42
  • 59
8
votes
1 answer

How do I speed up all_pairs_dijkstra_path_length

I have a large osmnx (networkx) graph and nx.all_pairs_dijkstra_path_length is taking a long time to calculate. What possibilities are there to speed up the calculation?
Suuuehgi
  • 4,547
  • 3
  • 27
  • 32
8
votes
2 answers

Negative edge weight check in boost's dijkstra_shortest_paths

I am using the boost graph library to make a call to dijkstra_shortest_paths. However, I have something special setup in that the weight_map is actually a functor. Hence, everytime the boost library requires the weight of an edge, my functor is…
Frank
  • 10,461
  • 2
  • 31
  • 46
8
votes
3 answers

Shortest path using Dijkstra's algorithm

I'm currently reviving an old homework assignment, where I'm writing a program that among other functions, involves finding the shortest path in a graph using Dijkstra's algorithm. I think I've got it right for the most part, but I keep getting…
John Snow
  • 5,214
  • 4
  • 37
  • 44
8
votes
5 answers

Shortest path between raw geo coordinates and a node of a graph

I have implemented a simple Dijkstra's algorithm for finding the shortest path on an .osm map with Java. The pathfinding in a graph which is created from an .osm file works pretty well. But in case the user's current location and/or destination is…
Kirill
  • 867
  • 2
  • 11
  • 14
8
votes
5 answers

Why does Dijkstra's algorithm need a priority queue when this regular queue version is also correct?

I have read the following but please take a look at my code below. Why Dijkstra's Algorithm uses heap (priority queue)? I have two versions of dijkstra, one good version with PQueue, and one bad version with regular linked list queue. public…
8
votes
1 answer

JUnit Test Case via Private Method in Dijkstra Algorithm

I'm trying to figure out the best way to implement a test case for a class exercise. My class exercise provides the known bug and with this I should write a test case for it to fail, and thus find the bug. It involved using the Dijkstra Algorithm.…
8
votes
3 answers

Suggestions of the easiest algorithms for some Graph operations

The deadline for this project is closing in very quickly and I don't have much time to deal with what it's left. So, instead of looking for the best (and probably more complicated/time consuming) algorithms, I'm looking for the easiest algorithms to…
rfgamaral
  • 16,546
  • 57
  • 163
  • 275
8
votes
2 answers

Dijkstra’s shortest-path algorithm what if there are paths with same distance?

In this network Dijkstra’s shortest-path algorithm is used. The question is which path will A use to reach D because both are equal? is that table missing?
makkuzu
  • 485
  • 2
  • 4
  • 19
8
votes
1 answer

How to compute single source shortest paths with OSRM?

I've been playing around with the OSRM routing library recently. It seems to be highly efficient at solving the shortest path problem. However, I didn't see how to compute single source shortest paths with it. More precisely, given a fixed starting…
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
8
votes
3 answers

dijkstra's algorithm on iOS

I am tracking locations and their connections to other locations. I keep locations in an NSArray while each location is represented as a Dictionary. Each location has Dictionary has the attributes (locationName, Connections, latitude, longitude)…
user1278974
  • 215
  • 3
  • 11