Dijkstra ShortPath
Dijkstra ShortPath
Graphs
(Dijkstra’s Algorithm)
Introduction
• Dijkstra's algorithm allows us to find the
shortest path between any two vertices of a
graph.
• It differs from the minimum spanning tree
because the shortest distance between two
vertices might not include all the vertices of
the graph.
Graphs - Shortest Paths
• Application
• In a graph in which edges have costs ..
• Find the shortest path from a source to a destination
• Surprisingly ..
• While finding the shortest path from a source to one
destination,
• we can find the shortest paths to all over
destinations as well!
• Common algorithm for
single-source shortest paths
is due to Edsger Dijkstra
Dijkstra’s Algorithm - Data Structures
• For a graph,
G = ( V, E )
• Dijkstra’s algorithm keeps two sets of vertices:
S Vertices whose shortest paths have already been
determined
V-S Remainder
• Also
d Best estimates of shortest path to each vertex
Predecessors for each vertex
Predecessor Sub-graph
• Array of vertex indices, [j], j = 1 .. |V|
• [j] contains the pre-decessor for node j
• j’s predecessor is in [[j]], and so on ....
• The edges in the pre-decessor sub-graph are
( [j], j )
Dijkstra’s Algorithm - Operation
• Initialise d and
• For each vertex, j, in V
• dj = Initial estimates are all
j = nil No connections
• Source distance, ds = 0
• Set S to empty
• While V-S is not empty
• Sort V-S based on d
Add s first!
• Add u, the closest vertex in V-S, to S
• Relax all the vertices still in V-S connected to u
Dijkstra’s Algorithm - Operation
• Initialise d and
• For each vertex, j, in V
• dj =
j = nil Initial estimates are all
• Source distance, ds = 0No connections
• Set S to empty
• While V-S is not empty
• Sort V-S based on d
• Add u, the closest vertex in V-S, to S
Add s first!
• Relax all the vertices still in V-S connected to u
Dijkstra’s Algorithm - Operation
• The Relaxation process
Source
Mark 0 Distance to all
nodes marked
Dijkstra’s Algorithm - Operation
• Initial Graph
Source
Source
Change u’s
pre-decessor also
S is now { s, x }
Sort vertices and
choose closest
Dijkstra’s Algorithm - Operation