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

Dijkstra Algorithm multiple edges find minimum

i've been trying to find a way to get the minimum distance and path between vertexes in a Graph. I've found a solution in which i adapted to my necessities and it mostly works. Implementation i'm talking about:…
anthony
  • 5
  • 3
0
votes
1 answer

Shortest path not returning correct solution

Been trying to implement Dijkstras algorithm in a user defined graph. But it keeps giving incorrect solutions. Anyway you guys can take a look and help me out? I have been trying to use this graph as my test graph where A is the start Node and G is…
Tom
  • 23
  • 6
0
votes
1 answer

Dijkstra Algorithm Specific Case

I'm working on realization of Dijkstra Shortest Path algorithm in Python. Graph is oriented and weighed. Graph has 1070375 vertices. First task is to find shortest path between vertex #100562 and 1070345. I did it. I have not problem with it. But…
denys.p.
  • 1
  • 2
0
votes
2 answers

Scala - Shortest Path Between Two Nodes Recursive Algorithm

I am implementing Dijkstra's shortest path algorithm recursivingly in Scala, but I am having some trouble. I am getting the incorrect output for nodes 3 to 2, called like this, shortestPath(3, 2, x, BitSet.empty). This outputs 6, but the correct…
Teodorico Levoff
  • 1,641
  • 2
  • 26
  • 44
0
votes
2 answers

Dijkstra reconstructing graph

Ok, so I've implemented Djakstra's shortest path algorithm in Python and it is working properly (the distances are calculated correctly), but I want it to return the new optimized graph also. Here's my work: nodes = ('A', 'B', 'C', 'D', 'E', 'F',…
Damian Radinoiu
  • 97
  • 2
  • 10
0
votes
1 answer

Graphhopper Dijkstra One-To-Many Memory Error

No matter the size of the graph and the server I use, any time I attempt to route by the dijkstra_one_to_many algorithm, I overflow my heap. Test environment is a m3.2xlarge with 30gb of RAM and 2x80gb SSD drives. java.lang.OutOfMemoryError: Java…
Chadderall
  • 89
  • 9
0
votes
1 answer

Dijkstra Algorithm: Printing whole path in console works fine, can't get it into a proper String

I am currently working on a pathfinding project and everything expect the last bit works fine and I feel very stupid about it. So I have a class, which calculates me the shortest path between two nodes in a grid including obstacles. The whole…
Johannes Mols
  • 890
  • 1
  • 12
  • 35
0
votes
1 answer

How to calculate the shortest path between two vertices in Graph with given parameters?

We need to compute the minCost(), which has follwing parameters: gNodes - no of Nodes in graph g. an array of int's, gFrom, where each gfrom[i] denotes a node connected by ith edge in graph g. an array of int's, gTo, where each gTo[i] denotes a…
आनंद
  • 2,472
  • 4
  • 21
  • 25
0
votes
1 answer

Max-Probability Paths Using Dijkstra

I have made this program for shortest path in a bidirectional graph using dijkstra's algorithm. Can this algorithm be converted to a program to find the longest path in the graph. #include using namespace std; #define mp…
Vandan Bhardwaj
  • 53
  • 3
  • 11
0
votes
1 answer

Understanding Dijkstra Algorithm

I'm trying to understand the Dijkstra Algorithm for finding the shortest path. I've came up to this example, where the top table corresponds to the image in the bottom left corner. Now, my problem is that I don't understand the transition from step…
Steven
  • 1,123
  • 5
  • 14
  • 31
0
votes
1 answer

Dijkstra's Algorithm Java-- Distance not right

I am trying to code dijkstra's algorithm, starting at any vertex I need to show the distance and print the path of nodes. It works for vertex 2,4, and 5, but for 1 and 3 it gets messed up. It's probably something stupidly small, but I can't see…
Felicia
  • 33
  • 1
  • 1
  • 8
0
votes
2 answers

Research help: Path finding algorithm

Not a technical question, just asking for a point in the right direction for my research. Are there any models that address the following problem: Finding the route starting from X that passes through A, B, C in the most efficient order. In the…
0
votes
2 answers

Unpredictable results finding optimal path with Dijkstra's Algorithm

I am trying to implement Dirjkstra's Algorithm to give me the optimal path between 2 nodes in a weighted graph representing a 2d matrix. Summary: If I use equal edge weights for the graph, the optimal path has been (as far as I can tell) always…
RandomUser
  • 4,140
  • 17
  • 58
  • 94
0
votes
0 answers

What's the main difference between dijkstra's algorithm and Prim's algorithm?

I can't find too much difference between prim's and dijkstra's algorithm. Can anyone explain the main difference of them.
Ruhul
  • 59
  • 1
  • 5
0
votes
1 answer

Matrix reading from file Dijkstra's Algorithm

I have a shortest path algorithm in place and have fully understood its working, but instead of taking the matrix input inside the main function the code I want to read the matrix from a text file. I am a beginer and just trying to learn how this…
Vikram AJ
  • 1
  • 1