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

How to find the shortest diagonal path of in a squared grid using Dijkstra's algorithm in Java?

I am developing a system using Dijkstra's algorithm to display the shortest path in a squared grid using Java. When the path goes to a diagonal, vertical or horizontal cell near by, the path cost increases by 1. But the path's priority should be…
Sami Udakara
  • 145
  • 1
  • 1
  • 8
5
votes
1 answer

Dijkstra variant with k nodes?

I have to find a min path from a source and destination where source and destination are the same node and I want a minimum fixed number of nodes in the path. I thought to implement a Dijkstra algorithm (in Java) with the variant that k nodes are…
Denise
  • 131
  • 10
5
votes
1 answer

Minheap and removing item if data changes in between removals

I asked a question yesterday but it wasn't very clear so this is a more specific question. I'm representing my minheap as an array. I have a good understanding of minheaps I think, but I'm fuzzy on one certain concept within. Minheaps are always…
LismUK
  • 119
  • 1
  • 8
5
votes
3 answers

Shortest path algorithm using dictionaries [Python]

This is my first question and actually my first time trying this but I read the rules of the questions and I hope my question comply with all of them. I have a project for my algorithm subject, and it is to design a gui for dijkstra shortest path…
Ahmed Al-haddad
  • 805
  • 2
  • 16
  • 41
5
votes
3 answers

Dijkstra's Algorithm Issue

I'm working on a program where I have to find the shortest path between 12 cities, starting in Seattle and ending in Miami. I'm using Dijkstra's Algorithm because the paths are weighted. Here is my code so far, it all works except the answer I get…
Azul_Echo
  • 151
  • 1
  • 1
  • 9
5
votes
1 answer

Finding shortest path for multi-weighted edges between any two nodes

How can we use use Dijkstra's algorithm to find shortest path when there are multiple edges having different weights to go from one node to another and also the availability of edges to go from one mode to another depends on the edge you have taken…
5
votes
6 answers

Implementing Dijkstra's Algorithm

I've been tasked (coursework @ university) to implement a form of path-finding. Now, in-spec, I could just implement a brute force, since there's a limit on the number of nodes to search (begin, two in the middle, end), but I want to re-use this…
Puppy
  • 144,682
  • 38
  • 256
  • 465
5
votes
2 answers

Difference between branch & bound (+ extended list) and Dijkstra's Algorithm on Graphs

I was working through http://youtu.be/gGQ-vAmdAOI?t=23m14s when at 23:14 I felt that branch & bound with an "extended list" was very similar to Dijkstra's algorithm. Later on in the lecture when the algorithm is again extended with an admissable…
Sebastian Graf
  • 3,602
  • 3
  • 27
  • 38
5
votes
4 answers

dijkstra/prim's algorithm...a little help?

I was wondering for dijkstra's and prim's algorithm, what happens when they are choosing between more than one vertex to go to ,and there are more than one vertex with the same weight. For example Example Image…
bfpri
  • 51
  • 2
  • 3
5
votes
3 answers

How to optimize Dijkstra algorithm for a single shortest path between 2 nodes?

I was trying to understand this implementation in C of the Dijkstra algorithm and at the same time modify it so that only the shortest path between 2 specific nodes (source and destination) is found. However, I don't know exactly what do to. The way…
rfgamaral
  • 16,546
  • 57
  • 163
  • 275
5
votes
2 answers

Optimisation of a Dijkstra Shortest Path Search in Delphi

I'm looking for advices to speed up my implementation of Dijkstra Shortest Path Search on a weighted graph which is a square matrix N x N. The weight on horizontal vertice is called H (resp. V on vertical ones). A picture is worth a thousand…
5
votes
2 answers

Dijkstra path reconstruction

Ok so I have been trying for the past few weeks to create a rogue like game, what I am stuck on right now, is connecting rooms in a dungeon with hallways. Keep in mind this is all in C, and I am using ncurses. So what I have been trying to do so…
ultrainstinct
  • 255
  • 4
  • 15
5
votes
3 answers

A Shortest path with fuel constraints

I found this problem somewhere in a contest and haven't been able to come up with a solution yet. I am thinking on the lines of applying Dijkstra's or something but it is not very clear : ''You are given a weighted connected graph of cities with…
therealone
  • 51
  • 1
  • 2
5
votes
2 answers

Parallel Dijkstra Deadlocking

I'm using pthreads to try and parallelize Dijkstra's pathfinding algorithm, but I'm running into a deadlock scenario I can't seem to figure out. The gist of it is that every thread has its own priority queue where it gets work (a std::multiset) and…
nate
  • 53
  • 4
5
votes
2 answers

Go, Dijkstra : print out the path, not just calculate the shortest distance

Go, Dijkstra : print out the path, not just calculate the shortest distance. http://play.golang.org/p/A2jnzKcbWD I was able to find the shortest distance using Dijkstra algorithm, maybe not. The code can be found here. But it would be useless if I…
user2671513