Upload
alan-york
View
218
Download
0
Embed Size (px)
DESCRIPTION
What Order? Define d[v] be the length of shortest path from s to v At any time, vertices are classified into: BlackWith known d[v] GreyWith some known path WhiteNot yet touched The Grey vertex with shortest known path has potential to become Black The Head of Queue is always the case in an unweighted graph Why?
Citation preview
Shortest Paths
08-07-2006PowerPoint adapted from Alan Tam’s Shortest Path, 2004with slight modifications
08-07-2006 2
Breadth First Search (BFS)
Techniques to Enumerate all Vertices for Goal
BFS needs O(V) queue and O(V) set for duplicate elimination and runs in O(V + E) time
BFS can find Shortest Path if Graph is Not Weighted
BFS works because a Queue ensures a specific Order
08-07-2006 3
What Order?
Define d[v] be the length of shortest path from s to v
At any time, vertices are classified into: Black With known d[v] Grey With some known path White Not yet touched
The Grey vertex with shortest known path has potential to become Black
The Head of Queue is always the case in an unweighted graph
Why?
08-07-2006 4
Weighted Graph
Queue does not promise smallest d[v] anymore
Expanding weighted edge causeds unnecessary searching of artificial vertices
We can simply pick the real vertex nearest to the starting 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
08-07-2006 5
0 3
1
31
32
3
08-07-2006 6
0
3
2
3
1
31
32
3
3
08-07-2006 7
0
3
3
2
5
3
1
31
32
3
Done?
08-07-2006 8
0
3
3
2
4
3
1
31
32
3
Done?
08-07-2006 9
0
3
3
2
4
3
1
31
32
3
08-07-2006 10
0
3
3
2
4
3
1
31
32
3
Done?
08-07-2006 11
Dijkstra’s Algorithm
d ← ∞d[s] ← 0enqueue(queue, s)while not empty(queue) do
u = dequeue(queue)for-each v where (u, v) in E
if d[v] > d[u] + wuv thend[v] = d[u] + wuv
enqueue(queue, v)
Note that d[v] represents the known shortest distance of v from s during the process and may not be the real shortest distance until v is marked black.
08-07-2006 12
Requirements for Priority Queue Map<V, int>
Store temporary values of d[v] PriorityQueue<V, function: V→int >
Extract vertices in increasing order of d[v] Initially all d[v] are infinity When we visit a White vertex
Set d[v] Insert v into Queue
When we visit a Gray vertex Update d[v] What to do to the Priority Queue?
08-07-2006 13
Implementing Priority Queue? Usage:
Insert: V Extract: V Decrease: E - V
Array Insert: O(1) Extract: O(n) Decrease: O(1)
Sorted Array Insert: O(n) Extract: O(1) Decrease: O(n)
Heap Insert: O(lg n) Extract: O(lg n) Decrease: O(lg n)
Fibonacci Heap Insert: O(1) Extract: Amortized O(lg
n) Decrease: O(1)
08-07-2006 14
Time Complexity
Worst Case Big-Oh Memory Time
Array V
Array (Lazy)
Sorted Array
Heap
Heap (Lazy)
Fibonacci Heap V E + V lg V
08-07-2006 15
Time Complexity
Worst Case Big-Oh Memory Time
Array V V2
Array (Lazy) E VE
Sorted Array V VE
Heap V E lg V
Heap (Lazy) E E lg V
Fibonacci Heap V E + V lg V
08-07-2006 16
Another Attempt
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
08-07-2006 17
Bellman-Ford Algorithm
All v1, v2, …,vk distinct?
Do V-1 timesfor-each (u, v) in Eif d[v] > d[u] + wuv then
d[v] ← d[u] + wuv
Order = O(VE) Support Negative-weight Edges
08-07-2006 18
Another Attempt
Number vertices as v1, v2, …, vV Define no≤k-path as a path which does not pass
through vertices v1, v2, …, vk A path s-t must either be
a no≤1-path, or concatenation of a no≤1-path s-v1 and a no≤1-path v1-t
A no≤1-path s-t must either be a no≤2-path, or concatenation of a no≤2-path s-v2 and a no≤2-path v2-t
By MI …
08-07-2006 19
Trivial Paths
S TFor all
SFor all
TDirect Pathfrom S to T
08-07-2006 20
R SFor all
RFor all
SDirect Pathfrom R to SS T
For all S
For all TDirect Path
from S to T
For S = vV
Direct Pathfrom R to T
08-07-2006 21
no≤(V-1)-Paths
S TFor all
SFor all
TShortest Pathfrom S to T
via vV
08-07-2006 22
R SFor all
RFor all
SShortest Pathfrom R to S
via vV
S TFor all
SFor all
TShortest Pathfrom S to T
via vV
For S = vV-1
Shortest Pathfrom R to T
via vV
08-07-2006 23
no≤(V-2)-Paths
S TFor all
SFor all
TShortest Pathfrom S to Tvia vV-1,vV
08-07-2006 24
no≤2-Paths
S TFor all
SFor all
TShortest Pathfrom S to T
via v3,v4,...,vV
08-07-2006 25
R SFor all
RFor all
SShortest Pathfrom R to S
via v3,v4,...,vV
S TFor all
SFor all
TShortest Pathfrom S to Tvia v3,v4,...,vV
For S = V2
Shortest Pathfrom R to Tvia v3,v4,...vV
08-07-2006 26
no≤1-Paths
S TFor all
SFor all
TShortest Pathfrom S to T
via v2,v3,...,vV
08-07-2006 27
R SFor all
RFor all
SShortest Pathfrom R to S
via v2,v3,...,vV
S TFor all
SFor all
TShortest Pathfrom S to Tvia v2,v3,...,vV
For S = V1
Shortest Pathfrom R to Tvia v2,v3,...vV
08-07-2006 28
Shortest Paths
S TFor all
SFor all
TShortest Pathfrom S to T
via v1,v2,...,vV
08-07-2006 29
Warshall-Floyd Algorithm
for-each (u, v) in Ed[u][v] ← wuv
for-each i in Vfor-each j in V
for-each k in Vif d[j][k] > d[j][i] + d[i][k]
d[j][k] ← d[j][i] + d[i][k]
Time Complexity: O(V3)
08-07-2006 30
Graph Modeling
Conversion of a problem into a graph problem
Sometimes a problem can be easily solved once its underlying graph model is recognized
Graph modeling appears almost every year in NOI or IOI
(cx, 2004)
08-07-2006 31
Basics of Graph Modeling
A few steps: identify the vertices and the edges identify the objective of the problem state the objective in graph terms implementation:
• construct the graph from the input instance• run the suitable graph algorithms on the graph• convert the output to the required format
(cx, 2004)