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

Find distance of route from get.shortest.paths()

I'm using the igraph package in R to do something rather simple: Calculate the shortest distance between two nodes in my network. Is there a straightforward way to extract the distance of a path calculated via get.shortest.paths()? Here is some…
baha-kev
  • 3,029
  • 9
  • 33
  • 31
5
votes
3 answers

Dijkstras Algorithm doesn't appear to work, my understanding must be flawed

Here's my interpretation of how Dijkstra's algorithm as described by wikipedia would deal with the below graph. First it marks the shortest distance to all neighbouring nodes, so A gets 1 and C gets 7. Then it picks the neighbour with the current…
TheIronKnuckle
  • 7,224
  • 4
  • 33
  • 56
5
votes
4 answers

Java Maze shortest path 2d int array

I am currently stuck on a project. My aim is to use Dijkstra's algorithm. I understand that I start at point (0,0) I look at the two nodes next to the start point and then I move to the smallest first and look at the surrounding nodes. My maze is…
Dennis Hayden
  • 184
  • 1
  • 5
  • 15
5
votes
3 answers

Dijkstra’s shortest path algorithm

The following is algorithm summary given to us by our professor. What is the parent of a node in a graph as referred to in step 3? I'm a little confused as I though that the nodes only had neighbors and doesn't have a parent? My second question…
5
votes
4 answers

Optimization problem in connected graphs with profits

I am trying to develop an algorithm to solve a problem that I am not able to classify, I expose the subject: You have a map divided into sections that have a certain area and where a certain number of people live. The problem consists of finding…
5
votes
3 answers

QuickGraph Dijkstra example

I have an AdjacencyGraph> which I would like to run AlgorithmExtensions.ShortestPathsDijkstra on, but the QuickGraph documentation isn't the best. Does anyone have an example I can follow? Everything I found on Google used an…
Ben S
  • 68,394
  • 30
  • 171
  • 212
5
votes
2 answers

Algorithm to find multiple short paths

Seeking an algorithm that will yield N short paths. Does anyone have experience with an algorithm to find multiple short paths in a directed graph? My application is for language (finding synonym chains) but logically, this could be for geography,…
5
votes
0 answers

Finding Shortest Path from starting node to each other nodes using Dijkstra's

This is the problem from hackerrank . I've been doing some basic graph problems. This is a undirected graph with equal weights on all edges. I have to print the weight to reach each node from the starting node. If it cannot reach, I had to return…
5
votes
3 answers

Does Dijkstra's algorithm work with negative edges if there is no "processed" check?

Typically, in Dijkstra's algorithm, for each encountered node, we check whether that node was processed before attempting to update the distances of its neighbors and adding them to the queue. This method is under the assumption that if a distance…
scdivad
  • 421
  • 3
  • 10
5
votes
5 answers

Is Dijkstra's algorithm deterministic?

I think that Dijkstra's algorithm is determined, so that if you choose the same starting vertex, you will get the same result (the same distances to every other vertex). But I don't think that it is deterministic (that it has defined the following…
Klarisa
  • 81
  • 4
5
votes
3 answers

Dijkstra's algorithm with an 2d-array

For the past few days I've tried to implement this algorithm. This far I've managed to make a dynamic 2d array and insert the distances between nodes, a function to remove a path between nodes and a function that tells me if there is a path between…
ogward
  • 1,183
  • 4
  • 13
  • 18
5
votes
1 answer

How to make a faster algorithm

Let = (, ) be a directed graph with edge weights and let be a vertex of . All of the edge weights are integers between 1 and 20. Design an algorithm for finding the shortest paths from . The running time of your algorithm should be asymptotically…
Talor
  • 81
  • 5
5
votes
1 answer

Confused about the proof of Dijkstra Algorithm

In the proof of the correctness of Dijkstra algorithm, there is a lemma stating as follow: Let u be v's predecessor on a shortest path P: s->...->u->v from s to v. Then, If d(u) = δ(s,u) and edge (u, v) is relaxed, we have d(v) = δ(s,v), where…
sundaycat
  • 162
  • 7
5
votes
4 answers

Adjusting Dijkstra for transit routing — minimize route changes

I am working on a small project that involves finding the best route on a bus system. I build a graph where the stops are vertices and the edges are weighted with the time between stops. I though I would try to use a variant of Dijkstra to find the…
Mark
  • 90,562
  • 7
  • 108
  • 148
5
votes
2 answers

Converting a graph into dictionary form

I am currently writing a program to model Dijkstra's algorithm, however I am having some trouble the graph in its current form below: G = [['a', 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h', 'i', 'j'], [({'a', 'b'}, 4), ({'a', 'c'}, 6), ({'a', 'd'},…
user9726150