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

How to convert a coordinates to indice array (vice versa) in a NetworkX graph?

I want to use a raster for a A* and bidirectional Dijkstra path analysis in NetworkX. I am using Python for this project. Raster example (it's a png file converted when uploaded, but the real problem is TIFF): First I read in the raster with…
0
votes
0 answers

Dijkstra - Shortest Path Print

I have a little problem with dijkstra print path, it's working on small graph size like 10-20 etc. On 100 there is problem, because printing loop going to infinity, when I've tried recurrence method, VS is showing me exepction throw, stack overflow…
Patryk Panek
  • 405
  • 4
  • 20
0
votes
2 answers

PriorityQueue not working

I'm trying to implement Dijkstra algorithm on my own in java. I have a min-priority queue that stores nodes ordered by their current shortest path. The first step goes smoothly, I set starting node with distance 0 and others with Integer.MAX_VALUE.…
Linrong
  • 3
  • 3
0
votes
0 answers

How to use Dijkstra's Shortest Path in a Weighted graph to compute the Average value of the Weights? [Python]

I want to compute the Dijkstra's shortest path in a weighted graph to compute the average value of the weights. I didn't found anything useful in the web, so please help me because I think this could be useful not just for me. I have a list of…
piezzoritro
  • 141
  • 2
  • 14
0
votes
0 answers

Number of steps condition to dijkstra's algorithm

I'm currently a student and am working on a small project. My aim is to be able to determine the shortest path between two given cities, but with the condition that one needs to go through a specified number of other cities (or as you might have…
0
votes
0 answers

Finding minimum weight and number of edges traversed in a graph

I have to find the minimum distance between two vertices and no. of edges traversed in finding that minimum distance in a adirected weighted graph. I did write a code on using Dijkstras algo. But my test cases are failing. What am i missing here.?…
0
votes
1 answer

cytoscape.js: have dijkstra ignore hidden edges

Calculating dijkstra shortest path after hiding some edges, the algorithm still includes those edges. cy.remove(someEdges) gets the right result: the shortest path is rerouted around those missing edges. Would it be possible, and make sense, to get…
paul shannon
  • 355
  • 5
  • 15
0
votes
2 answers

Will Dijkstra ever a path with cycle?

Note: There is no negative cost. I am considering to implement U-turn in routing, which uses Dijkstra. Will Dijkstra ever recommend route A-B-C-B-D over A-B-D? When encountering B for the first time, B is marked as visited after visiting its…
na9090
  • 125
  • 2
  • 13
0
votes
1 answer

Hackerrank: Jack goes to Rapture Intuition, Modified Dijsktras

Problem Statement: https://www.hackerrank.com/challenges/jack-goes-to-rapture One of the solutions is use modified Dijkstra's Algorithm. Original: For a vertex u, Forall vertices v, instead of updating the distance by, alt = distance(u) + weight(u,…
Abhishek
  • 432
  • 5
  • 19
0
votes
0 answers

Dijkstra's algorithm: How to improve memory performance of my implementation

I have been struggling to improve the memory performance of this implementation of Dijkstra's algorithm. Do you have any tips on how to improve this while still using the same data structures and loops? I basically used an implementation from a…
0
votes
0 answers

Getting SIGABRT error on Codechef ? Its running on a testcase but giving Sigabrt error on remaining?

SIGABRT Error on Codechef Just wanna know where it is lacking. I have tried many things but not getting the solution. Please help, Thanks in advance !! Basic implementation of finding shortest path to every node using…
Sourabh Zalke
  • 39
  • 1
  • 4
0
votes
1 answer

I get NaN for every output from dijkstra algorithm

I have some nodes distributed somewhere. I want to use dijkstra algorithm to find distances and the paths between them. Here is the code: N=16; %number of nodes, There is one sink node in addition to these N…
sn at
  • 61
  • 7
0
votes
2 answers

Most efficient implementation of Dijkstra's shortest path using vectors and pairs in C++ stl

If std::vector > > v(n) represents the adjacency list of the graph with pair is the {vertex,weight} pair, I tried to implement the algorithm the following way: while (true) { long long yo = LLONG_MAX; int ind…
yobro97
  • 1,125
  • 8
  • 23
0
votes
1 answer

Routing indoor knowing steps

I need to create a path in an indoor map having all the connections (steps, hops) like AB, BC, BA, CB, .... Suppose I have to go from A to I how will be the algorithm? P.S. I'm developing in C# but any pseudo code or link to other resources is…
adrianoBP
  • 187
  • 6
  • 19
0
votes
1 answer

Modifying Dijkstra to find path with max coloured node

I just saw a solution of a question that modifying Dijkstra to get the shortest path with a max of K coloured edge. I am wondering what if we want find the shortest path with coloured node instead of edge, how are we gonna modify Dijkstra to do the…
swordgit
  • 115
  • 10