0% found this document useful (0 votes)
33 views29 pages

Dijkstra ShortPath

Dijkstra's algorithm finds the shortest paths from a source node to all other nodes in a graph. It maintains two sets - S of nodes whose shortest paths are determined, and V-S of remaining nodes. Starting from the source, it iteratively relaxes and updates the distance estimates of nodes adjacent to those in S until all nodes are added to S. This guarantees the shortest path distances computed will not increase, proving the algorithm's correctness. The time complexity is O((|E|+|V|)log|V|) or O(n^2 log n) for a dense graph.

Uploaded by

I - Coder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views29 pages

Dijkstra ShortPath

Dijkstra's algorithm finds the shortest paths from a source node to all other nodes in a graph. It maintains two sets - S of nodes whose shortest paths are determined, and V-S of remaining nodes. Starting from the source, it iteratively relaxes and updates the distance estimates of nodes adjacent to those in S until all nodes are added to S. This guarantees the shortest path distances computed will not increase, proving the algorithm's correctness. The time complexity is O((|E|+|V|)log|V|) or O(n^2 log n) for a dense graph.

Uploaded by

I - Coder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Data Structures and Algorithms

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

Relax the node v


attached to node u Edge cost matrix

If the current best


relax( Node u, Node v, double w[][] )
estimate to v is
if d[v] > d[u] + w[u,v] then
greater than the
d[v] := d[u] + w[u,v]
path through u ..
pi[v] := u
Update the
estimate to v
Make v’s predecessor
point to u
Dijkstra’s Algorithm - Full
• The Shortest Paths algorithm

Given a graph, g, and a source, s

shortest_paths( Graph g, Node s )


initialise_single_source( g, s )
S := { 0 } /* Make S empty */
Q := Vertices( g ) /* Put the vertices in a PQ */
while not Empty(Q)
u := ExtractCheapest( Q );
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w )
Dijkstra’s Algorithm - Initialise
• The Shortest Paths algorithm
Given a graph, g,
Initialise d, , S,
and a source, s vertex Q
shortest_paths( Graph g, Node s )
initialise_single_source( g, s )
S := { 0 } /* Make S empty */
Q := Vertices( g ) /* Put the vertices in a PQ */
while not Empty(Q)
u := ExtractCheapest( Q );
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w )
Dijkstra’s Algorithm - Loop
• The Shortest Paths algorithm
Given a graph, g,
and a source, s

shortest_paths( Graph g, Node


While there are s )
initialise_single_source(
still nodes in Q g, s )
S := { 0 } /* Make S empty */
Q := Vertices( g ) /* Put the vertices in a PQ */
while not Empty(Q)
u := ExtractCheapest( Q ); Greedy!
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w )
Dijkstra’s Algorithm - Relax neighbours
• The Shortest Paths algorithm
Given a graph, g,
and a source, s Update the
estimate of the
shortest_paths( Graph g, paths
shortest Node tos )
initialise_single_source(
all nodes g, s )
S := { 0 } /* Make
attached to u S empty */
Q := Vertices( g ) /* Put the vertices in a PQ */
while not Empty(Q)
u := ExtractCheapest( Q ); Greedy!
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w )
Dijkstra’s Algorithm - Operation
• Initial Graph

Source
Mark 0 Distance to all
nodes marked 
Dijkstra’s Algorithm - Operation
• Initial Graph

Source

Relax vertices adjacent to


source
Dijkstra’s Algorithm - Operation
• Initial Graph

Source

Red arrows show


pre-decessors
Dijkstra’s Algorithm - Operation

Source is now in S Sort vertices and


choose closest
Dijkstra’s Algorithm - Operation
Relax u because a
shorter path via x
exists

Source is now in S Relax y because a


shorter path via x
exists
Dijkstra’s Algorithm - Operation

Change u’s
pre-decessor also

Source is now in S Relax y because a


shorter path via x
exists
Dijkstra’s Algorithm - Operation

S is now { s, x } Sort vertices and


choose closest
Dijkstra’s Algorithm - Operation
Relax v because a
shorter path via y
exists

S is now { s, x }
Sort vertices and
choose closest
Dijkstra’s Algorithm - Operation

S is now { s, x, y } Sort vertices and


choose closest, u
Dijkstra’s Algorithm - Operation

S is now { s, x, y, u } Finally add v


Dijkstra’s Algorithm - Operation

S is now { s, x, y, u } Pre-decessors show


shortest paths sub-graph
Dijkstra’s Algorithm - Proof
• Greedy Algorithm
• Proof by contradiction best
• Lemma 1
• Shortest paths are composed of shortest paths
• Proof
• If there was a shorter path than any sub-path, then
substitution of that path would make the whole path
shorter
Dijkstra’s Algorithm - Proof
• Denote
 (s,v) - the cost of the shortest path from s to v
• Lemma 2
• If s...uv is a shortest path from s to v,
then after u has been added to S and relax(u,v,w) called,
d[v] = (s,v) and d[v] is not changed thereafter.
• Proof
• Follows from the fact that at all times d[v]  (s,v)
• See Cormen (or any other text) for the details
Dijkstra’s Algorithm - Proof
• Using Lemma 2
• After running Dijkstra’s algorithm, we assert
d[v] = (s,v) for all v
• Proof (by contradiction)
• Suppose that u is the first vertex added to S for which
d[u](s,u)
• Note
• v is not s because d[s] = 0
• There must be a path s...u,
otherwise d[u] would be 
• Since there’s a path, there must be a shortest path
Dijkstra’s Algorithm - Proof
• Proof (by contradiction)
• Suppose that u is the first vertex added to S for which
d[u](s,u)
• Let sxyu be the shortest path
su,
where x is in S and y is the
first outside S
• When x was added to S, d[x](s,x)
• Edge xy was relaxed at that time,
so d[y](s,y)
Dijkstra’s Algorithm - Proof
• Proof (by contradiction)
• Edge xy was relaxed at that time,
so d[y](s,y)
(s,u) d[u]
• But, when we chose u,
both u and y where in V-S,
so d[u] d[y]
(otherwise we would have chosen y)
• Thus the inequalities must be equalities
 d[y](s,y) (s,u) d[u]
• And our hypothesis (d[u](s,u)) is contradicted!
Dijkstra’s Algorithm - Time Complexity
• Dijkstra’s Algorithm
• Similar to MST algorithms
• Key step is sort on the edges
• Complexity is
• O( (|E|+|V|)log|V| ) or
• O( n2 log n )
for a dense graph with n = |V|

You might also like