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

Shortest path between every pair of nodes in And/Or graph?

I have an AND/OR dependency graph where nodes are web services and there is an arc between two services S1-->S2 if some output of S1 are similar to some input of S2 The weight of the arc is a function of many parameters (example: execution time) I…
0
votes
1 answer

why negative edge allowed in bellman ford algorithms?

why negative edges cycles are allowed in bellman ford algorithms while no negative edge allowed in dijkstra algorithms?
user507401
  • 2,809
  • 9
  • 25
  • 20
0
votes
0 answers

android - google places API with dijkstra algorithm

O'm working on an app for bus transit and route. I'm currently using dijkstra algorithm to find the shortest way from bus stop A to bus stop B, but it's still limited between bus stops. Is it possible to use google places API? for example, I put…
0
votes
1 answer

Dijkstra shortest path algorithm infinite loop

Following is the full code of my program. There is something wrong in the dijkstra function whereas when I use an vertex id greater than 18 it stays in infinite loop. I tried different combinations and didn't see any problem so far other than using…
Vsky
  • 59
  • 1
  • 9
0
votes
0 answers

Djikstra's Algorithm for neighbor checking and setting pointers

while(!adjacents.isEmpty()) { if(! adjacents.front()->visited ) //visited is a bool to see if it's been visited or not { if( (tentdist + abs(adjacents.front()->val - val)) < adjacents.front()->tentdist) { …
0
votes
0 answers

How can I optimize this python program just to make it 0.2-0.1 secs faster? Dijkstra algorithm

import time class DJ: def __init__(self, roads, n): self.roads, self.n=roads,n #Dijkstra with mod for a pathway from vertex a to c #returns dist of a to all points if arg c is 0 def dis(self,a,c): …
0
votes
1 answer

Find safest route in graph but prevent doubling the travel time of the fastest route?

my university project is to design a route planner of the city i live in, calculating the shortest routes between streets. (traveling salesman) In C#, im using a graph to store all the streets. Currently i can find the shortest route between two…
squish
  • 846
  • 12
  • 24
0
votes
1 answer

How to generate a map of weighted graph for Dijkstra’s Shortest Path Algorithm from a text file in Java?

I have a text file "NYRoadNetwork.txt" which contains the following information of a weighted graph: The first row represents the number of nodes in the graph, i.e. 30 The second row represents the number of edges connecting any two nodes…
larry
  • 1
0
votes
1 answer

Dijkstra vs iteration. How to model the graph and determine the time complexity in both cases?

I am wondering about an issue that I found here: Finding cheapest combination of items with conditions on the selection I wanted to continue discussing this because I find this very interesting and the solution really cool, but I have issues…
pandaadb
  • 6,306
  • 2
  • 22
  • 41
0
votes
1 answer

can we run Dijkstra's algorithm on the modified graph and subtract the added weights to get the original distances?

Consider the following strategy to convert a graph with negative edge weights to one that does not have negative edge weights. Let the maximum magnitude negative edge weight in the graph be -k. Then, for each edge in the graph with weight w, update…
0
votes
1 answer

Shortest Path with Edge Weight Doubled on Every Other Hop

How would I optimally solve a graph theory problem, where the edge weight changes on every other, or even third hop? Could I still use some kind of modified Dijkstra's algorithm?
Sauhaarda
  • 3
  • 1
0
votes
1 answer

Dijkstra's algorithm in Java source and target

I'm trying to use Dijkstra's algorithm to find the shortest path between two nodes in the graph. what should I do to the following code to stop calculating when the shortest path between the source and the target is found? public void…
raghad Alamri
  • 13
  • 1
  • 1
  • 8
0
votes
0 answers

DijkstraShortestPath returns no path on a connected graph jgrpaht

I am building a weighted graph of roads, in java jgrapht. I am trying to find the shortest path using Dijkstra algorithm, but the function returns that there is no path between the vertexes. But as for roads concern the graph is connected. it seems…
shay C
  • 51
  • 2
  • 7
0
votes
1 answer

Dijkstra's algorithm running time with Array

in wikipedia they state under the Heading: "running time" for Dijstras algorithm is O(|V|^2 + |E|*decreaseKey) = O(|V|) Dijkstra's_algorithm This is my first time analyzing time complexity for an algoritm, but in my opinion it should…
J.doe
  • 373
  • 2
  • 14
0
votes
0 answers

Trying to write Dijkstra's algorithm in C and the program crashes

So I have just come across Dijkstra's algorithm for path finding and decided to try it out in C. I wrote the code to take the data of a Dijkstra diagram and find the shortest path. The data I used was: Number of nodes: 5 Cost matrix 0 4 0 8 0 4 0 3…