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
0 answers

I don't know why this code for MICEMAZE in SPOJ is giving WA?

This is my code for MICEMAZE. Write a program that, given a description of the maze and the time limit, predicts the number of mice that will exit the maze. Assume that there are no bottlenecks is the maze, i.e. that all cells have room for an…
0
votes
2 answers

Graph minimum weight path

I have a weighted graph. I want to find the best path from node S to node E, so that the maximum single edge weight that was inside that path is the smallest possible. For example: S -> E (w=40) S -> A (w=30) A -> E (w=20) For this graph, djikstra…
Lake
  • 4,072
  • 26
  • 36
0
votes
2 answers

How to add a key/ priority to std::priority_queue in c++?

I'm trying to implement Dijkstra's Pathfinding Algorithm using the std::priority_queue. My Queue is of type Node* and I need it to prioritize based on a float gScore stored inside Node from smallest gScore to largest. I've read the documentation but…
Bojangles
  • 3
  • 1
0
votes
1 answer

Why is my Dijkstra code failing?

I am trying to solve a hackerrank question on Dijkstra's Algorithm-https://www.hackerrank.com/challenges/dijkstrashortreach. I am using kind of my own logic of Dijkstra's code. Although my code solves the easier test cases, it fails at higher ones.…
0
votes
1 answer

Calculating safest route in MATSim

I am interested in calculating routes based on the weights of links for traffic simulation using MATSim API. Every link has an attribute of safety index and the calculated route should represent the safest path. The Dijkstra routing algorithm…
DanaKazi
  • 3
  • 2
0
votes
1 answer

how to use Dijkstra c++ code using array based version

I need to use (not implement) an array based version of Dijkstras algo .The task is that given a set of line segments(obstacles) and start/end points I have to find and draw the shortest path from start/end point.I have done the calculating part…
abbas
  • 143
  • 1
  • 2
  • 8
0
votes
1 answer

Modification of Dijkstra's Algorithm

I am currently playing around with the very famous Dijkstra's Algorithm, and am a little stuck. I am trying to modify the pseudo code to take an input of BOTH the source and destination, in efforts to find the SHORTEST path between the two vertices…
user4309460
0
votes
1 answer

Dijkstra Algorithm in Adjacency Matrix Between Spesific Edges in Android

Hello i have a problem to find shortest path in adjacency matrix. I want to find path from startPoint to finishPoint(ex 2 - 9). My codes just below and i have StackOverflowError in "printPath(parent, parent[j]);" line. I m so thank you .. public…
0
votes
1 answer

Outputs 2147483647 as a distance in final answer in Dijkstra

The user will be asked what path and what distance. Then it'll compute the shortest distance using Dijkstra's algorithm. The problem is some distance and paths are wrong. There are distances that has 2147483647 and its path is null. The algo works…
Temmie
  • 127
  • 2
  • 14
0
votes
2 answers

Dijkstra on adjacency matrix in C

I need some help with Dijkstra's algorithm in C. I've generated my adjacency matrix, that looks something like: int mat[NB][NB] = {{0, 171, MAX, 132, [...]}, {171, 0, 30, 39, [...]}, , [...]}; I've found this implementation:…
Tim
  • 2,887
  • 10
  • 33
  • 33
0
votes
0 answers

Modify Dijkstra algorithm to find the shortest path

Here is Dijkstra pseudocode: Dijkstra(G,s) { foreach vεV do { d[v] = infinity } Queue priority initialization Q with n elements Q = V d[s] = 0 while(Q is not empty) { v = ExtractMin(Q) foreach e = (v,w) ε E do if d(w) > d(v) + l { …
Lee Yaan
  • 547
  • 7
  • 26
0
votes
0 answers

Unable to find Infinite loop in Dijkstras algorithma

Im attempting to implement Dijkstras algorithm however somewhere in it it seems that I have created an infinite loop and I cant for the life of me find where. I first sort a "City" object into an array of lists, then attempt to run dijkstras…
Hunter Tipton
  • 313
  • 1
  • 4
  • 11
0
votes
0 answers

Dijkstra Calculate Path

if I try to calculate the shortest Distance from Node A to Z in my Graph the Dijkstra Algorithm ist not working very well. I'm not sure why, i think it is a problem in the calculation of the Distance form Node to Node. PriorityQueue
Reptain
  • 9
  • 2
0
votes
2 answers

How to Represent a Graph using Dijkstra's Algorithm, Given a .txt file as Input

I'm having trouble using Dijkstra's algorithm when given a text file in this format. The first line represents the number of vertexes. Should I store this value as a 2-Dimensional Array? I was thinking that I could have the second part of the 2D…
SweetJD14
  • 73
  • 1
  • 2
  • 11
0
votes
2 answers

get the time taken for Dijkstra function

I just want to calculate the time that Dijkstra function will take to calculate shortest path for source node as the network nodes are in Q Do While True Dim dist As Integer = Integer.MaxValue For i = 1 To Q.Count If…
AlKobtan
  • 65
  • 8
1 2 3
99
100