Transcript Document
Shortest Path and
Minimum Spanning Tree
HKOI Training 2009
7 Feb 2009
Graph
Graph
Vertex/Node
Edge
Number
Degree
Number
Direction
E ≤ V2
G = (V, E)
V
|V| or simply V
deg[v],in-deg[v],out-deg[v]
E
|E| or simply E
e = (u, v) or {u, v}
i.e.
deg[v] ≤ |V|
Graph Representations
Adjacency
Matrix
Adjacency List
Edge List
Memory
Q(V2)
Q(V + E)
Q(E)
Check if (u, v)
connected
Q(1)
O(out-degree[u])
O(E)
List Edges from u
Q(V)
O(out-degree[u])
Q(E)
List Edges to v
Q(V)
Q(E)
Q(E)
List All Edges
Q(V2)
Q(E)
Q(E)
Graph Modelling
S
S
E
Assuming you can only move between grey cells.
E
Why do we need to learn more
algorithm for shortest path?
BFS
is already doing a great job!
In what context?
Last graph =P
Any well model-ed graph?
Another Graph
S
Greedy?
BFS?
2 queues?
1 queue with
re-insert?
E
Now assume:
- Move to adjacent grey cell costs 1 unit
- Move to adjacent black cell costs 2 unit
Another Graph Again
S
E
From To
Cost
Grey
Grey
1000
Grey
Black
999
Black Grey
10
Black Black
888
So troublesome, let’s learn / find other algorithm(s)
S
3
2
1
3
3
1
T
3
S
T
BFS in Weighted Graph
Problems:
Queue does not promise smallest d[v] anymore
Expanding path caused unnecessary searching of
artificial vertices
Solution:
We can simply pick the shortest real vertex
We need “Sorted Queue” which “dequeues”
vertices in increasing order of d[v].
It is called a “Priority Queue” and negation of d[v]
is called the Priority of vertex v
S
3
2
1
3
3
1
T
3
S
3
2
1
3
3
1
T
3
S
3
2
1
3
3
1
We can see the end now!
T
3
S
3
2
1
3
3
1
T
3
S
3
2
1
3
3
1
T
3
S
3
2
1
3
3
1
T
3
Dijkstra’s Algorithm
for-each v, d[v] ← ∞
d[s] ← 0
Q.Insert(s,d[s])
while not Q.Empty() do
u = Q.ExtractMin()
for-each v where (u, v) in E
if d[v] > d[u] + wuv then
d[v] = d[u] + wuv
Q.DecreaseKey(v,d[v])
Dijkstra’s Algorithm
for-each v, d[v] ← ∞
d[s] ← 0
Q.Insert(s,d[s])
Lazy Deletion
while not Q.Empty() do
u = Q.ExtractMin()
for-each v where (u, v) in E
if d[v] > d[u] + wuv then
d[v] = d[u] + wuv
Q.Insert(v,d[v])
Implementations of Priority Queue
Insert
ExtractMin
DecreaseKey
Array
Q(1)
Q(n)
Q(1)
Sorted Array
O(n)
Q(1)
O(n)
Binary Heap
O(log n)
Q(log n)
Q(log n)
Q(1)
O(log n)
Q(1)
Fibonacci Heap
(amortized)
Complexity
Memory
Time
Array
O(V)
O(V2)
Array (Lazy)
O(E)
O(E2)
Sorted Array
O(V)
O(VE)
Binary Heap
O(V)
O(E log V)
Binary Heap
(Lazy)
O(E)
O(E log V)
Fibonacci Heap
(amortized)
O(V)
O(E + V log V)
Dijkstra’s Algorithm
Edsger
Dijkstra is doing a great job!
Can we now use it on all kinds of graph?
Another Graph
S
E
Now assume:
- Move to adjacent grey cell costs 1 unit
- Move to adjacent black cell gains 1 unit
Add a constant
to the weight?
Dijkstra’s Algorithm
Problems:
Cannot tackle graph with negative edges
• With negative edge, some unvisited nudes may
have smaller distance to source than some visited
nudes
Solutions:
Another idea
Consider the following code segment:
For each edge (u, v)
If d[v] > d[u] + wuv
d[v] ← d[u] + wuv
Assume one of the shortest paths is
(s, v1, v2, …, vk)
If d[vi] = its shortest path from s
After this loop, d[vi+1] = its shortest path from s
By MI, After k such loops, found shortest path from s
to vk
Bellman-Ford Algorithm
All v1, v2, …,vk distinct
for-each v, d[v] ← ∞
d[s] ← 0
Do V-1 times
for-each (u, v) in E
if d[v] > d[u] + wuv then
d[v] ← d[u] + wuv
O(VE)
Support Negative-weight Edges
Negative Cycle
A negative cycle is a cycle whose sum of edge
weights is negative
What happens of there are negative cycles in the
graph?
Doesn’t matter if the negative cycle is not reachable
from the source
If a negative cycle is reachable from the source, can
we detect it?
Answer: one more round of relaxations
All-pairs Shortest Path
Sometimes
we want to find the
distances between any pair of vertices
Use Dijkstra’s algorithm V times
O(VElogV)
Use
Bellman-Ford V times
O(V2E)
We want to be better
Intermediate vertices may shorten the
distance between two vertices
Label the vertices as v1, v2, … , vn
Let Vk-path be a path which uses only v1,
v2, … , vk as intermediate vertices
A s-t-V1-path must either be
A s-t-V2-path must either be
a s-t-V0-path, or
concatenation of a s-v1-V0-path and v1-t-V0-path
a s-t-V1-path, or
concatenation of a s-v2-V1-path and v2-t-V1-path
By MI …
Recurrence Relation
A s-t-Vk-path must either be
a s-t-Vk-1-path, or
concatenation of a s-vk-Vk-1-path and vk-t-Vk-1path
dij(k):=length of the shortest vi-vj-Vk-path
dij(k) = wij
if k=0
min(dij(k-1), dik(k-1) + dkj(k-1) )
if k>=1
Warshall’s Algorithm
d←∞
dij(k) =
wij
min(dij(k-1), dik(k-1) + dkj(k-1) )
for-each (u, v) in E
d[u][v] ← wuv
for-each k in V
e←d
for-each i in V
for-each j in V
if e[i][j] > d[i][k] + d[k][j]
e[i][j] ← d[i][k] + d[k][j]
d←e
Time Complexity: O(V3)
if k=0
if k>=1
Warshall’s Algorithm
d←∞
for-each (u, v) in E
d[u][v] ← wuv
for-each k in V
for-each i in V
for-each j in V
if e[i][j] > d[i][k] + d[k][j]
e[i][j] ← d[i][k] + d[k][j]
Time Complexity: O(V3)
1
3
2
2
1
3
3
3
4
3
1
5
from \ to
1
2
3
4
5
1
∞
3
∞
2
3
2
∞
∞
1
∞
∞
3
∞
∞
∞
∞
∞
4
∞
∞
3
∞
1
5
∞
∞
3
∞
∞