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

how to find the shortest distance between two nodes that pass at least one of the compulsory nodes?

I have a bi-directed weighted graph with about 5000 nodes and i have a list of "important" nodes (100 or so). Given a start node and an end node, how do I find the shortest distance between this two nodes that pass at least 1 of the "important"…
abc
  • 59
  • 6
0
votes
1 answer

Traverse an array in least amount of hops, online coding challenge for tech company interview

I did an interview question/coding challenge where I needed to come up with the shortest amount of "hops" through an array "arr" where at each index i could jump 1->arr[i]. One twist is that I cannot land on any indexes with a 0 as its value. When…
ian turner
  • 61
  • 1
  • 4
0
votes
2 answers

Dijkstra and FileInput. Java

I have this Dijkstra algorithm java code below. I downloaded the code. I want to make changes to this program and store the data in file and read it in rather than put it in the source code. What would be the best ways to do this? import…
user560084
  • 57
  • 1
  • 3
  • 7
0
votes
0 answers

print path of shortestpath C++ STL

I am new to STL based c++ usage. Can anyone help me how to proceed with the following code in printing "path of vertices" that yielded the shortestpath score for each destination vertex along with the shortestpath value. Please find the working…
ksn
  • 105
  • 7
0
votes
1 answer

Can someone detect error in this code to implement dijkstra's algorithm using python?

I am trying to implement dijkstra's algorithm (on an undirected graph) to find the shortest path and my code is this. Note: I am not using heap/priority queue or anything but an adjacency list, a dictionary to store weights and a bool list to avoid…
noobita
  • 67
  • 11
0
votes
2 answers

Graph Data Structures using C++

I am making a console application that will print all the paths. But I am having a hard time thinking on how to display all paths from source to destination. Here is my code: #include using namespace std; int arr[8][8] =…
0
votes
0 answers

Matlab build graph from bwtraceboundary and find shortest path

I used bwtraceboundary where the image shows the resulting boundary path. Overall, it works great and actually does exactly what I expect, but I later run into problems with the 90 degree turn and protrusion that extends furthest right. I did find…
ThatsRightJack
  • 721
  • 6
  • 29
0
votes
1 answer

Trying to understand Dijkstra's Algorithm

I am trying to better understand Dijkstra's Algorithm. I have attached an image of the algorithm that is in my textbook. The pseudocode shows the input is an undirected graph, but is the algorithm any different for a directed graph? I have looked up…
K22
  • 147
  • 1
  • 5
  • 15
0
votes
1 answer

Find the hidden nodes between two nodes - graph - java

My English is not very well, BUT I will try my best to explain my issue here. I am working on an application in which I have to create graph. For now I am using GraphStream. Requirements for my graph is very complicated, which is : I have a table…
Noob Player
  • 279
  • 6
  • 25
0
votes
3 answers

Java - Find all possible paths based on array key pairs

I have an array of several key-pairs and i want to find all possible paths from one element and back to it e.g : array { a-b a-c a-d b-a b-c b-s d-c c-a c-d c-a d-a .... } I'm doing some foreach loops but i'm stuck given the data set . Is there…
Sam.tuver
  • 679
  • 2
  • 9
  • 19
0
votes
1 answer

Dijkstra algorithm: Are all shortest paths acyclic?

I know that the algorithm wont terminate if it reaches negativ cycles and a path wouldnt be a shortest path, if it would contain a cycle with distance greater than 0. My question is what happens if there exists a shortest path with a cycle of…
Rev
  • 1
  • 1
0
votes
1 answer

Neo4j - apoc.algo.dijkstra - shortest path from a to b, but via c, d, e etc

Hi I am experimenting with routing using neo4j but I am struggling with adapting apoc.algo.dijkstra. I don’t just want to go from A to B, but actually go from A to B via C, whilst taking into account a weighting on the link. The A-B query…
SAB
  • 175
  • 17
0
votes
2 answers

Building adjacency graph out of chessboard (for dijkstra)

I ran into a problem where I wanted to add a little feature to my homework and it turned out to be overwhelming for me (read bold sentences for question without context). My program has a list of about 35 items in it, containing information about…
0
votes
1 answer

Dijkstra algorithm under constraint

I have N vertices one being the source. I would like to find the shortest path that connects all the vertices together (so a N-steps path) with the constraint that all the vertices cannot be visited at whichever step. A network is defined by N the…
Jkev
  • 177
  • 1
  • 2
  • 8
0
votes
0 answers

How can I modify Dijkstra's algorithm to get the longest path most of the time?

I know that finding the longest path is an NP Hard problem. What was asked from us was to change Dijkstra's algorithm to find the longest path by adding another parameter to the algorithm. A parameter like the distance from the source to the given…
Firdaws
  • 93
  • 1
  • 10