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

Boost::graph Dijkstra : initially populating the queue

I'm using boost::graph and its Dijkstra implementation. I want to compute THE shortest path from a set of vertices to another set of vertices. I do not want to compute all the possible paths between those sets. The idea is the following : I'm in a…
Tristram Gräbener
  • 9,601
  • 3
  • 34
  • 50
4
votes
2 answers

leetcode problem #787 "cheapest flights within k stops"

I am trying to solve LeetCode problem #787, "Cheapest Flights Within K Stops." "There are n cities connected by some number of flights. You are given an array flights where flights[i] = [fromi, toi, pricei] indicates that there is a flight from…
4
votes
0 answers

Is there a name/algorithm for this graph problem? Expand a grid outward to collect maximum value on edges

I'm facing an undirected weighted graph problem that I'm sure has been tackled many times, and I'm wondering if it has a name and existing algorithm. Edges represent value I want to capture and maximize, starting from a subgrid and expanding outward…
4
votes
1 answer

Can we use Dijkstra's to find the shortest path even in a graph having negative edge weights?

Suppose I have a graph where the minimum edge weight is −100. Can I add 100 as an offset to all the edges and use Dijkstra's algorithm? Please help me understand why such a method gives wrong solution.
Pradhan
  • 421
  • 5
  • 17
4
votes
2 answers

Why does my implementation of Dijkstra's algorithm fail under this undefined case?

I am trying to write an implementation of Dijkstra's Algorithm in Prolog (specifically, GNU Prolog). To begin, I made a fact connection that describes the connected vertices of the graph below. It now seems like my implementation works for around…
4
votes
2 answers

shortest path to surround a target in a weighted 2d array

I'm having some trouble finding the right approach to coding this. Take a random-generated 2d array, about 50x50 with each cell having a value 1~99. Starting at a random position "Green", and the goal is to surround the target "Red" with the lowest…
4
votes
1 answer

who knows Sedgewick-Vitter algorithm?

I have to realize Dijkstra and Sedgewick-Vitter algorithms without using Fibonacci heap. Information about Dijkstra full internet, but I couldn't find pseudo code or examples of Sedgewick-Vitter algorithm. I only found that there are some info in…
Mario
  • 335
  • 7
  • 20
4
votes
2 answers

Finding the shortest distance between two nodes

I found and implemented Dijkstra's algorithm into a graph that I had created - which shows a map of my local area The code works fine but I want it to display all the nodes that it visited in order to get to the location Node from the source node.…
4
votes
2 answers

What's the time complexity of Dijkstra's Algorithm

Dijkstra((V, E)): S = {} //O(1) for each vertex v ∈ V: //O(V) d[v] = ∞ //O(1) d[source] = 0 //O(1) while S != V: //O(V) v = non visited vertex with the smallest d[v] //O(V) for each edge (v, u): //O(E) …
user8314628
  • 1,952
  • 2
  • 22
  • 46
4
votes
1 answer

Can I use Prim's algorithm instead of Dijkstra's to find shortest path?

I have been fighting all day in understanding Dijkstra's algorithm and implementing with no significant results. I have a matrix of cities and their distances. What I want to do is to given an origin point and a destination point, to find the…
Pithikos
  • 18,827
  • 15
  • 113
  • 136
4
votes
2 answers

Is my understanding of Dijkstra algorithm correct?

Consider the following weighted directed graph: Let's consider node 1 as a starting node, according to the Dijkstra algorithm we have the following steps: node 1 marked visited. the shortest path to node 2 has weight 1. Mark node 2 visited. the…
voipp
  • 1,243
  • 3
  • 18
  • 31
4
votes
1 answer

Constrained shortest distance in a graph

I came across this problem at a coding site and I have no idea on how to solve it. The editorial is not available nor was I able to find any related article online. So I am asking this here. Problem: You have a graph G that contains N vertices and…
Andrew Scott
  • 465
  • 3
  • 11
4
votes
2 answers

Using more than once property in Dijkstra algorithm in Neo4j

Is there any way in Cypher I can use the Dijkstra algorithm to calculate minimal weight with more than one property instead of: CALL apoc.algo.dijkstra(start, end, 'RELATED_TO>', '1_property') yield path, weight to do something like: CALL…
Itay Regev
  • 191
  • 8
4
votes
2 answers

The shortest path between nodes in graph

I don't know if I should be asking this here or not, the question is about algorithm. Imagine you have an undirected graph. Edges have different values. Imagine that some vertice are "good" and some are "bad". Now I want to determine two good nodes,…
Giorgi Cercvadze
  • 403
  • 1
  • 7
  • 23
4
votes
2 answers

Python Dijkstra Algorithm

I am trying to write Dijkstra's Algorithm, however I am struggling on how to 'say' certain things in code. To visualize, here are the columns I want represented using arrays: max_nodes A B C Length Predecessor …
user612041
  • 217
  • 3
  • 9
  • 19