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

How is Dijkstra algorithm better tham A* algorithm for finding shortest path?

How is Dijkstra algorithm better tham A* algorithm for finding shortest path?
Manish Basdeo
  • 6,139
  • 22
  • 68
  • 102
4
votes
1 answer

Networkx - Get edge properties in Dijkstra path

I have the following graph import networkx as nx g = nx.MultiGraph() #link 0 g.add_edge("A","B",cost=20,index=0) #link 1 g.add_edge("A","C",cost=20,index=1) #link 2 g.add_edge("B","C",cost=10,index=2) #link…
Cmarv
  • 141
  • 9
4
votes
1 answer

Shortest path in a maze

I'm developing a game similar to Pacman: consider this maze: Each white square is a node from the maze where an object located at P, say X, is moving towards node A in the right-to-left direction. X cannot switch to its opposite direction unless it…
Peanut
  • 175
  • 11
4
votes
2 answers

Python breadth first shortest path for Google foo bar (prepare the bunnies escape)

I am working on the following problem, and I think that I have it mostly solved, however some of the test cases are failing: You have maps of parts of the space station, each starting at a prison exit and ending at the door to an escape pod. The…
Mike
  • 537
  • 2
  • 6
  • 20
4
votes
3 answers

Shortest path with a twist

I have n vertices and m undirected weighted edges between them (weights are representing minutes). Each vertex contains a number of minutes required to drink a coffee on that vertex. I want to determine the shortest amount of time (minutes)…
PlsWork
  • 1,958
  • 1
  • 19
  • 31
4
votes
1 answer

Why is it mandatory that Dijkstra's algorithm extracts min in each round?

Consider the graph is valid for applying Dijkstra's algorithm i.e. there are no negative edge weights. I'm having a difficult time convincing myself that Dijkstra's algorithm only works if the minimum distance node in each round is chosen to be…
4
votes
1 answer

Stopping criteria for a bidirectional Dijkstra's (or uniform cost search) algorithm

The stopping criteria, as I understand, for a bidirectional Dijkstra's search (with a single start and terminal state) is as follows Start with a variable mu = infinity Start search from start (s) and terminal states (t) When the two intersect…
max_max_mir
  • 1,494
  • 3
  • 20
  • 36
4
votes
1 answer

Dijkstra's algorithm vs relaxing edges in topologically sorted graph for DAG

I was reading Introduction To Algorithms 3rd Edition. There are 3 methods given to solve the problem. My inquiry is about two of them. The one with no name The algorithm starts by topologically sorting the dag (see Section 22.4) to impose a linear…
user4952610
4
votes
1 answer

Difference between the runtime of Dijkstra's Algorithm: Priority Queue vs. Doubly Linked List

What is the difference, regarding runtime complexity, between the following and why?: (1) DIJKSTRA's algorithm using regular Priority Queue (Heap) (2) DIJKSTRA's algorithm using a doubly linked list (Unless there isn't a difference)
4
votes
2 answers

A* implemented in C

Where can I find an A* implementation in C? I was looking around but it seems my google-fu is not strong enough. I've started writing my own implementation, but then I remembered Stack Overflow and I thought I should ask here first. It seems a bit…
Vasiliy Sharapov
  • 997
  • 1
  • 8
  • 27
4
votes
1 answer

unorderable types: Vertex() < Vertex()

I'm working on a dijkstra and I get this error: TypeError: unorderable types: Vertex() < Vertex() The whole error log is: Traceback (most recent call last): File "C:/Users/Dimitar/PycharmProjects/Dijkstra/Dijkstra.py", line 165, in
4
votes
1 answer

Floyd-Warshall algorithm with loops?

I'm making an implementation of the Floyd-Warshall algorithm, and I have one question: If I have a loop in my graph (I mean, the cost of going from A to A is 1), what should the algorithm output, 0 (because the cost of going from any node to the…
4
votes
1 answer

Incremental Dijkstra or shortest path algorithm?

I have an initial directed graph G, from which I remove edges from time to time (never add new ones). I don't remove nodes (although some might end up disconnected). Is there a way to efficiently recompute shortest paths without running Dijkstra…
excalibur1491
  • 1,201
  • 2
  • 14
  • 29
4
votes
1 answer

Weight map as function in Boost Graph Dijkstra algorithm

I'm using Boost Graph Libraries and need to use a weightmap which is not constant, but which is a function of a parameter K (i.e. the edge costs depend on K). In practice, given the following code: #include #include…
user1403546
  • 1,680
  • 4
  • 22
  • 43
4
votes
1 answer

How to generate graph with maximum relaxation?

There is a Dijkstra's algorithm. I want to generate graph with given |E| and |V|, in which the algorithm perform maximum number of relaxations: alt ← dist[u] + length(u, v). This is a ACM problem. But I have no idea how to solve it. I look forward…
Max
  • 1,803
  • 3
  • 25
  • 39