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
6
votes
1 answer

SortedSet.Remove() does not remove anything

I am currently implementing Dijkstra's algorithm and I am using the C# SortedSet as a priority queue. However, in order to keep track of what vertices I have already visited, I want to remove the first item from the priority queue. Here is my code:…
van Leemhuyzen
  • 133
  • 1
  • 10
6
votes
3 answers

Why can't we apply Dijkstra's algorithm for a graph with negative weights?

Why can't we apply Dijkstra's algorithm for a graph with negative weights?
prasanga
  • 87
  • 1
  • 2
6
votes
1 answer

MySQL Recursive query to find the shortest path

I have an issue I just can't get my head around... I have a table called country_neighbour looking like…
Davhoj
  • 71
  • 3
6
votes
0 answers

Find shortest path on SVG Object

I have an SVG object with a floor map which is made from a creator user and I want to find dynamically the shortest path between 2 points on this object. Dijkstra algorithm is the better solution for finding shortest path into a graph but this…
user3016999
6
votes
3 answers

Dijkstra's algorithm on directed acyclic graph with negative edges

Will Dijkstra's algorithm work on a graph with negative edges if it is acyclic (DAG)? I think it would because since there are no cycles there cannot be a negative loop. Is there any other reason why this algorithm would fail? Thanks [midterm…
Marcus
  • 9,032
  • 11
  • 45
  • 84
6
votes
1 answer

Boost::graph Dijkstra and custom objects and properties

I want to use boost's dijkstra algorithm (since I'm using boost in other parts of my program). The problem I'm having is adding custom objects (I believe they are referred to as property) to the adjacency_list. Essentially I have a custom edge…
lilott8
  • 1,116
  • 2
  • 17
  • 41
6
votes
1 answer

Dijkstra's end condition

With Dijkstra you use the end-condition while(!q.isEmpty()){ //Some code } But if you know the end node, is it not possible to change the end-condition to while(!q.peek().equals(endNode){ //Some code } Every…
SBylemans
  • 1,764
  • 13
  • 28
6
votes
1 answer

Multi-start and Multi-end shortest path set

I am having problem with shortest path in directed weighted graph. I know Dijkstra, BFS, DFS. However, I have a set of vertices S for starting points and a set of vertices E to end. S and E doesn't overlap. So how can I find the set of edges with…
6
votes
2 answers

All pairs shortest path - warm restart?

Is it possible to warm start any of the well known algorithms (Dijkstra/Floyd-Warshall etc) for the APSP problem so as to be able to reduce the time complexity, and potentially the computation time? Let's say the graph is represented by a NxN…
6
votes
3 answers

Shortest Path distance between points given as X-Y coordinates

I am currently working on a project that has a vector containing X and Y coordinates for approximately 800 points. These points represent an electric network of lines. My goal is to compute the shortest distance Path between a Point A and Point B…
6
votes
2 answers

How do I define a custom distance in Boost Dijkstra?

I'm currently looking at the documentation of Boost Dijkstra - http://www.boost.org/doc/libs/1_52_0/libs/graph/doc/dijkstra_shortest_paths.html; my objective is to modify the distance combining to get a "max" instead of a "plus" when computing my…
Balise
  • 168
  • 5
6
votes
1 answer

Dijkstra's algorithm Vs Uniform Cost Search (Time comlexity)

My question is as follows: According to different sources, Dijkstra's algorithm is nothing but a variant of Uniform Cost Search. We know that Dijkstra's algorithm finds the shortest path between a source and all destinations ( single-source ).…
John
  • 627
  • 10
  • 18
6
votes
0 answers

to find the fractal dimension of a real-world network, using the box-covering algorithm

here, in the following code, I get the colors assigned to the vertices, but there is no significance for the length of the box 'l' I want the colors to be assigned w.r.t. the length of the boxes and the function should output the number of different…
Sai Nikhil
  • 1,237
  • 2
  • 15
  • 39
6
votes
2 answers

Implementing Dijkstra's algorithm using min-heap but failed

I am trying to implement Dijkstra's Algorithm using min-heap in java but getting wrong output every time. Here i fount the same topic in C++. Below is my graph. Node A, which is green colored, is source and Node F, which is red colored, is…
ravi
  • 6,140
  • 18
  • 77
  • 154
5
votes
1 answer

Dijkstra-based algorithm optimization for caching

I need to find the optimal path connecting two planar points. I'm given a function that determines the maximal advance velocity, which depends on both the location and the time. My solution is based on Dijkstra algorithm. At first I cover the plane…
valdo
  • 12,632
  • 2
  • 37
  • 67