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

The shortest path in graph with increasing edges

I have oriented graph and I have to find the shortest path between Q pairs of nodes (A,B). But the path must go over max. N edges and length of these edges must be increasing (from A to B (1,3,5,9)). Output is length of this path. (If it doesn't…
user7558351
0
votes
1 answer

Setting vertex weight with Dijkstra's shortest path algorithm

New to java. What I am trying to make is a directed graph that represents a city. Most of the vertices are just streets but some are tourist sites which have weights (since one has to stay there for some time). I am working with Dijkstra's shortest…
Yu Shi
  • 17
  • 6
0
votes
1 answer

Algorithm to determine if a vertex is in any of the shortest paths

Given an undirected graph G = {E,V}, with positive edge costs. For all node combinations, is there a way do determine if a certain node v is not on any of the shortest paths that its not an endpoint? My thought is that it could be done by performing…
Dez
  • 21
  • 6
0
votes
1 answer

Avoiding ConcurrentModificationException in dijkstra's algorithm

For this program, I read in a excel file that lays out a map of towns, towns adjacent to them, and the distance between them, which looks like: Bourke Nyngan 200 Brewarrina Walgett 134 Broken Hill Mildura 266 Broken Hill Wilcannia …
Lazarus
  • 125
  • 2
  • 15
0
votes
2 answers

How do you run a three dimensional list through dijkstra's algorithm in python?

I’m a student who is (trying) to write a python implementation of Dykstra’s Algorithm. I know this question has been asked 100 times before, but there are some specifics of my situation I’m not fully understanding. I have a weighted, non-directed…
JoeDoe
  • 3
  • 2
0
votes
0 answers

Can I complete this in O(M log N) time?

So I am implementing Dijkstra's algorithm and I am having trouble trying to reduce the runtime of it. Ideally, an O(M log N) runtime would be great. Here is my code: import static jdk.nashorn.internal.runtime.regexp.joni.Syntax.Java; import…
0
votes
0 answers

Shortest Path Algorithm C++ Dijkstra's With Layover Included

Good morning everyone, I have a problem where I have to calculate the shortest path (including layovers) from one train station to another. I usually don't ask for help but I am very stuck on this one. I started out by writing a standard dijkstra's…
0
votes
1 answer

dijkstra's algorithm running time with array and priority queue

I'm having difficulty understanding the time of the dijkstra algorithm. Below I put the pseudo code I was analyzing for array. Considering that V are the vertices and E the edges. The Q as an array has its initialization with time O(V), the minimum…
panchester
  • 325
  • 1
  • 4
  • 13
0
votes
1 answer

dijkstra algorithm incorrect conditional

I am working on a dijkstra algorithm using priority queues. I have been doing a lot of research, and I thought my code was following the algorithm, but I cannot get into the conditional when comparing for the shortest paths void dijkstra( int…
0
votes
2 answers

Find the lowest-weight cycle in a weighted, directed graph using Dijkstra's

Hi I am struggling with this question. It is the following: Devise an algorithm to find the lowest-weight cycle(i.e. of all cycles in the graph, the one with the smallest sum of edge weights), in a weighted, directed graph G = (V,E). Briefly…
aa1
  • 783
  • 1
  • 15
  • 31
0
votes
1 answer

Dijkstra's algorithm- why every time extract the vertex with smallest priority?

I am learning Dijkstra's algorithm to find shortest path. And I noticed that there is a priority queue to help extract the vertex with lowest priority in the vertex set. Will the algorithm still work if I pick a vertex regardless of priority,…
0
votes
1 answer

Modifying Dijkstra to save paths with equal values - StackOverflow error

I'm trying to modify Dijkstra's algorithm to show all the paths that have the minimum value. So I decided to use a list of previous vertices. And I added an if clause that checks if the path is igual to the previous with minimum value I add the…
Marcos Guimaraes
  • 1,243
  • 4
  • 27
  • 50
0
votes
3 answers

If the indirect path between 2 graph nodes is shorter than the direct path, can Dijkstras algorithm detect it?

Dijkstras algorithm assumes the closest neighbor based on edge weight between the start and intermediate node. This is repeated until the destination node is reached. What if the shortest path between start node and an intermediate node is an…
saladguy
  • 75
  • 1
  • 1
  • 7
0
votes
0 answers

Priority queue performance

I wrote a slightly modified Dijkstra algorithm using a priority queue that stores pairs of numbers. When i declare it as priority_queue < pair > Q; it is painfully slow- 268 ms. On the other hand, if i write it as priority_queue <…
0
votes
1 answer

Can anybody please describe this portion of code of the Dijkstra algorithm

I can't understand this given portion of the Dijkstra algorithm. I want to understand this portion of code line by line. The code: bool operator < (const DATA &p) const { return p.dist > dist; } I have the basic knowledge of c/c++ code.
Mukit
  • 1
  • 3