Dijkstra`s Algorithm - Data Structures Predecessor Sub

Dijkstra’s Algorithm - Data
Structures
Graphs - Shortest Paths
Application
n
n
n
For a graph,
G = ( V, E )
Dijkstra’s algorithm keeps two sets of vertices:
In a graph in which edges have costs ..
Find the shortest path from a source to a destination
Surprisingly ..
w While finding the shortest path from a source to one
destination,
w we can find the shortest paths to all over destinations as well!
n
Common algorithm for
single-source shortest paths
is due to Edsger Dijkstra
3
1
8
6
Adjacency
matrix
4
n
n
-
1
2
3
4
5
1
0
3
∞
∞
∞
5
2
∞
0
7
∞
∞
10
3
∞
∞
0
5
1
∞
4
∞
∞
∞
0
6
∞
5
∞
∞
∞
∞
0
7
6
∞
∞
8
2
∞
0
The edges in the predecessor sub-graph are
( π[j], j )
graphs
The Relaxation process
Relax the node v
attached to node u
Edge cost matrix
relax( Node u, Node v, double w[][] ) If the current best
estimate to v is
if (d[v] > d[u] + w[u][v]){
greater than the
d[v] = d[u] + w[u][v];
path through u ..
pi[v] = u;
}
Update the
Initial estimates are all ∞
No connections
While V-S is not empty
n Sort V-S based on d
n Add u , the closest vertex in V-S, to S
4
Operation
n Source distance, d s
=0
Set S to empty
n
π[j] contains the predecessor for node j
π[j]’ s predecessor is in π[π[j]], and so on ....
3
Dijkstra’s Algorithm Operation
w dj = ∞
• πj = nil
2
6
graphs
Initialise d and π
n For each vertex, j, in V
graphs
n
6
5
6
Best estimates of shortest path to each vertex
Predecessors for each vertex
Array of vertex indices, π[j], j = 1 .. |V|
1
2
5
Also
Predecessor Sub-graph
5
3
10
Vertices whose shortest paths have already been
determined
Remainder
1
The Shortest Path:
from vertex 1 to vertex 5
2
V-S
d
π
graphs
7
S
estimate to v
Make v’s predecessor
point to u
Add s first!
Relax all the vertices still in V-S connected to u
graphs
5
graphs
6
1
Dijkstra’s Algorithm - Full
Dijkstra’s Algorithm - Initialise
The Shortest Paths algorithm
The Shortest Paths algorithm
Given a graph, g, and a source, s
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 (! Empty(Q)){
u = removeMin( Q );
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w )
}
}
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 (! Empty(Q)){
u = removeMin( Q );
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w );
}
}
graphs
7
Initialize d, π, S,
vertex Q
graphs
8
Dijkstra’s Algorithm -
Dijkstra’s Algorithm - Loop
neighbours
The Shortest Paths algorithm
The Shortest Paths algorithm
Given a graph, g,
and a source, s
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 (! Empty(Q)){
u = removeMin( Q );
Greedy!
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w );
}
}
Update the
estimate of the
shortest_paths( Graph
g,paths
Nodetos )
shortest
initialise _single_source(
all nodes g, s )
S = { 0 }
/*
Make
attached to uS empty */
Q = Vertices( g ) /* Put the vertices in a PQ */
while (! Empty(Q)){
u = removeMin( Q );
Greedy!
AddNode( S, u ); /* Add u to S */
for each vertex v in Adjacent( u )
relax( u, v, w );
}
}
graphs
graphs
9
Dijkstra’s Algorithm - Operation
Initial Graph
10
Dijkstra’s Algorithm - Operation
Initial Graph
Source
Mark 0
Source
Distance to all
nodes marked ∞
Relax vertices adjacent to
source
graphs
11
graphs
12
2
Dijkstra’s Algorithm - Operation
Dijkstra’s Algorithm - Operation
Initial Graph
Source
Red arrows show
pre-decessors
graphs
Sort vertices and
choose closest
Source is now in S
13
Dijkstra’s Algorithm - Operation
graphs
14
Dijkstra’s Algorithm - Operation
Relax u because a
shorter path via x
exists
Change u’s
pre-decessor also
Relax y because a
shorter path via x
exists
Source is now in S
graphs
Relax y because a
shorter path via x
exists
Source is now in S
15
Dijkstra’s Algorithm - Operation
graphs
16
Dijkstra’s Algorithm - Operation
Relax v because a
shorter path via y
exists
Sort vertices and
choose closest
S is now { s, x }
graphs
S is now { s, x }
17
Sort vertices and
choose closest
graphs
18
3
Dijkstra’s Algorithm - Operation
S is now { s, x, y }
Sort vertices and
choose closest, u
graphs
Dijkstra’s Algorithm - Operation
S is now { s, x, y, u }
19
Dijkstra’s Algorithm - Operation
Finally add v
graphs
20
Dijkstra’s Algorithm - Time
Complexity
Dijkstra’s Algorithm
n Similar to MST algorithms
n Key step is sort on the edges
n Complexity is
w O( (|E|+|V|)log|V| ) or
Pre-decessors show
shortest paths sub-graph
S is now { s, x, y, u }
graphs
21
w O( n 2 log n )
for a dense graph with n = |V| and |E| ≈ |V|2
graphs
22
4