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

Dijkstra openmp code in c

Hi i work on openmp and want to test a code of dijkstra algorithm.The code is heredijkstra_openmp .This code is written by John Burkardt and i try to run it.But run time for 1 thread is same by four thread.I have a intel core i3 system that can…
emi777
  • 1
0
votes
2 answers

Djikstra's Shortest path algorithm

I am learning Djikstra's and i have prepared the code below based on slightly different idea than Djikstra. Now, In many of the websites, i have seen the use of Extract min and boolean array of visited edges. I have used none and my answer is also…
waterbyte
  • 241
  • 1
  • 16
0
votes
0 answers

Swift Dijkstra Algorithm Error (EXC_BAD_INSTRUCTION)

So I've built a swift playground that uses Dijkstra's algorithm to find the shortest route. However, I can't seem to manipulate my txt file so that my function will work in every case. It only works for a select few pathways. Whenever I map out a…
caleb
  • 391
  • 1
  • 3
  • 11
0
votes
1 answer

Dijkstra algorithm implementation - Code not working for bigger inputs

#include #include using namespace std; class edge { public: int dest; int dist; edge(int a,int b) { dest=a; dist=b; } }; class Graph { public: int…
0
votes
2 answers

Shortest Path Finder from CSV in C#

Let's say I have the following CSV Sydney,Dubai,1 Dubai,Venice,2 Venice,Rio,3 Venice,Sydney,1 Sydney,Rio,7 First field is From second is To and third is Duration. I need a method which can take a From input and spit out the shortest path to all…
envyM6
  • 1,099
  • 3
  • 14
  • 35
0
votes
1 answer

Neo4j: Shortest Path with Cost Function depending on two Consecutive Relations in Path

Say I have a graph with nodes representing cities and and relations representing possible connections between cities. A connection has a departure and an arrival time. I want to find the shortest path between cities A and B and the cost function is…
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
0
votes
1 answer

An example of Dijkstra's algorithm to fail with one negative edge

I'm trying to think of a graph which all edges have positive weights, except of one edge such that Dijkstra's algorithm fails to produce the correct output. I'd be glad for an idea. EDIT: I've seen this graph as a counter-example but I don't…
Elimination
  • 2,619
  • 4
  • 22
  • 38
0
votes
1 answer

Dijkstra's Algorithm length

from this website's pseudocode: Given a graph, G, with edges E of the form (v1, v2) and vertices V, and a source vertex, s dist : array of distances from the source to each vertex prev : array of pointers to preceding vertices i : loop…
kmlkz
  • 63
  • 5
0
votes
1 answer

Dijkstra with negative weights

I'm trying to find an example of a directed graph with negative weights (no negative cycles) such that running dijksra on it will produce wrong results for all the vertices in the graph (except to the source node). It isn't very hard to find an…
0
votes
1 answer

Shortest-Path - URI Online Judge 1640

i was trying to pass this code in uri online judge but i don't know where is my error, all the tests i made work. the link problem is this: https://www.urionlinejudge.com.br/repository/UOJ_1640_en.html the description of the problem is: A transport…
0
votes
1 answer

Access nodes from query result in Orient DB

I'm trying to obtain the shortest path between two nodes in a graph using the dijkstra algorithm in Orientdb, using javascript. My output looks like below. I would like to know the length of the result (no of nodes) and print the properties of the…
Dileep
  • 71
  • 8
0
votes
1 answer

Dijkstra's algorithm: is my implementation flawed?

In order to train myself both in Python and graph theory, I tried to implement the Dijkstra algo using Python 3, and submitted it against several online judges, to see if it was correct. It works well in many cases, but not always. For example, I am…
javatutorial
  • 1,916
  • 15
  • 20
0
votes
4 answers

Dijkstra Algorithm = SSSP

What i have learnt , that dijkstra cannot work with negative edge weights . For that we have to use bellman ford . Bellman fords works well with negative edge weights and negative cycles , which are not reachable from source otherwise, it will…
Garrick
  • 677
  • 4
  • 15
  • 34
0
votes
1 answer

graphviz' dijkstra tool not considering edge weights

I want to use the dijkstra-tool coming with the graphviz package to calculate the shortest path in a directed graph with positive (including 0) edge weights. But it seams that it does not consider the edge weights. I call it like this: dijkstra -dp…
nvrandow
  • 722
  • 10
  • 13
0
votes
0 answers

Weight decoration object in Dijkstra's algorithm code?

Can somebody explain to me what is "Weight decoration object" in the code below? Whats kind of parameters should I input? For example, if I want to find shortest path from two airport, I have String Vertices and weighted edges (int). Thanks! …
Codez
  • 1
  • 1
1 2 3
99
100