Exercise Session 6 – Shortest Paths Computer Science II, D-ITET, ETH Zurich
Exercise Session 6 – Shortest PathsComputer Science II, D-ITET, ETH Zurich
Program Today
Feedback of last exercises
Shortest pathsDijkstraHeaps, DecreaseKey and Lazy DeletionRunning Time of the AlgorithmsDijkstra and Negative Edge Weights?
In-Class-Exercises
1
1. Feedback of last exercises
2
Depth-�rst-search and Breadth-�rst-search
A
B
C D
E
FG
H
Starting at ADFS: A,B,C,D,E, F,H,GBFS: A,B, F, C,H,D,G,E
There is no starting vertex where the DFS ordering equals the BFS ordering.
3
Depth-�rst-search and Breadth-�rst-search
A
B
C D
E
FG
H
Starting at ADFS: A,B,C,D,E, F,H,GBFS: A,B, F, C,H,D,G,EThere is no starting vertex where the DFS ordering equals the BFS ordering.
3
Depth-�rst-search and Breadth-�rst-search
Star: DFS ordering equals BFS ordering
A
B
C D
E
Starting at ADFS: A,B,C,D,EBFS: A,B,C,D,E
Starting at CDFS: C,A,B,D,EBFS: C,A,B,D,E
4
Depth-�rst-search and Breadth-�rst-search
Star: DFS ordering equals BFS ordering
A
B
C D
E
Starting at ADFS: A,B,C,D,EBFS: A,B,C,D,E
Starting at CDFS: C,A,B,D,EBFS: C,A,B,D,E
4
Topological Sorting
A B
C
D
E
Graph with cycles
Two minimal cycles sharing anedgeRemove edge =⇒ cycle-freeTopological Sorting by“removing” elements within-degree 0
5
Topological Sorting
A B
C
D
E
Graph with cyclesTwo minimal cycles sharing anedge
Remove edge =⇒ cycle-freeTopological Sorting by“removing” elements within-degree 0
5
Topological Sorting
A B
C
D
E
Graph with cyclesTwo minimal cycles sharing anedgeRemove edge =⇒ cycle-free
Topological Sorting by“removing” elements within-degree 0
5
Topological Sorting
A B
C
D
E
Graph with cyclesTwo minimal cycles sharing anedgeRemove edge =⇒ cycle-freeTopological Sorting by“removing” elements within-degree 0
5
Topological Sorting
A B
C
D
E
A Graph with cyclesTwo minimal cycles sharing anedgeRemove edge =⇒ cycle-freeTopological Sorting by“removing” elements within-degree 0
5
Topological Sorting
A B
C
D
E
A B Graph with cyclesTwo minimal cycles sharing anedgeRemove edge =⇒ cycle-freeTopological Sorting by“removing” elements within-degree 0
5
Topological Sorting
A B
C
D
E
A B
C
Graph with cyclesTwo minimal cycles sharing anedgeRemove edge =⇒ cycle-freeTopological Sorting by“removing” elements within-degree 0
5
Topological Sorting
A B
C
D
E
A B
C E
Graph with cyclesTwo minimal cycles sharing anedgeRemove edge =⇒ cycle-freeTopological Sorting by“removing” elements within-degree 0
5
2. Shortest paths
6
Shortest Paths
Given: G = (V,E, c), c : E → R, s, t ∈ V .
Path: s p t : 〈s = v0, v1, . . . , vk = t〉, (vi, vi+1) ∈ E (0 ≤ i < k)
Weight: c(p) := ∑k−1i=0 c((vi, vi+1)).
Weight of a shortest path from u to v:
δ(u, v) =
∞ no path from u to vmin{c(p) : u p
v} sonst
7
General Algorithm
1. Initialise ds and πs: ds[v] =∞, πs[v] = null for each v ∈ V2. Set ds[s]← 03. Choose an edge (u, v) ∈ E
Relaxiere (u, v):if ds[v] > d[u] + c(u, v) then
ds[v]← ds[u] + c(u, v)πs[v]← u
4. Repeat 3 until nothing can be relaxed any more.(until ds[v] ≤ ds[u] + c(u, v) ∀(u, v) ∈ E)
8
Algorithm Dijkstra(G, s)Input: Positively weighted Graph G = (V,E, c), starting point s ∈ V ,Output: Minimal weights d of the shortest paths and corresponding predecessor
node for each node.
foreach u ∈ V dods[u]←∞; πs[u]← ∅
ds[s]← 0; R← {s}while R 6= ∅ do
u← ExtractMin(R)foreach v ∈ N+(u) do
if ds[u] + c(u, v) < ds[v] thends[v]← ds[u] + c(u, v)πs[v]← uR← R ∪ {v}
9
Example
s
a
b
c
d
e
2
3
2
6
1
3
1
1
10
Example
s
a
b
c
d
e
2
3
2
6
1
3
1
1
0
∞
∞
∞
∞
∞s
M = {s}
R = {}
U = {a, b, c, d, e}
10
Example
s
a
b
c
d
e
2
3
2
6
1
3
1
1
0
∞
∞
∞
∞
∞ss
a
b
2
3
M = {s}
R = {a, b}
U = {c, d, e}
10
Example
s
a
b
c
d
e
2
3
2
6
1
3
1
1
0
∞
∞
∞
∞
∞ss
a
b
2
3
a c8
M = {s, a}
R = {b, c}
U = {d, e}
10
Example
s
a
b
c
d
e
2
3
2
6
1
3
1
1
0
∞
∞
∞
∞
∞ss
a
b
2
3
a c8
b d4
M = {s, a, b}
R = {c, d}
U = {e}
10
Example
s
a
b
c
d
e
2
3
2
6
1
3
1
1
0
∞
∞
∞
∞
∞ss
a
b
2
3
a c8
b d4
d
e5
7
M = {s, a, b, d}
R = {c, e}
U = {}
10
Example
s
a
b
c
d
e
2
3
2
6
1
3
1
1
0
∞
∞
∞
∞
∞ss
a
b
2
3
a c8
b d4
d
e5
7
e
6
M = {s, a, b, d, e}
R = {c}
U = {}
10
Example
s
a
b
c
d
e
2
3
2
6
1
3
1
1
0
∞
∞
∞
∞
∞ss
a
b
2
3
a c8
b d4
d
e5
7
e
6c
M = {s, a, b, d, e, c}
R = {}
U = {}
10
Implementation: Data Structure for R?
Relax for Dijkstra:if ds[u] + c(u, v) < ds[v] then
ds[v]← ds[u] + c(u, v)πs[v]← uif v 6∈ R then
Add(R, v) // Update of (v, d(v)) in the heap of Relse
DecreaseKey(R, v) // Update of a (v, d(v)) in the heap of R
11
DecreaseKey ?
Heap ( (a, 1), (b, 4), (c, 5), (d, 8) ) =(a,1)
(b,4)
(d,8)
(c,5)
after DecreaseKey(d, 3):
(a,1)
(d,3)
(b,4)
(c,5)
2 Probleme:Position of d unknown at �rst. Seach: Θ(n)Positions of the nodes can change during DecreaseKey
12
DecreaseKey ?
Heap ( (a, 1), (b, 4), (c, 5), (d, 8) ) =(a,1)
(b,4)
(d,8)
(c,5)
after DecreaseKey(d, 3):(a,1)
(d,3)
(b,4)
(c,5)
2 Probleme:
Position of d unknown at �rst. Seach: Θ(n)Positions of the nodes can change during DecreaseKey
12
DecreaseKey ?
Heap ( (a, 1), (b, 4), (c, 5), (d, 8) ) =(a,1)
(b,4)
(d,8)
(c,5)
after DecreaseKey(d, 3):(a,1)
(d,3)
(b,4)
(c,5)
2 Probleme:Position of d unknown at �rst. Seach: Θ(n)Positions of the nodes can change during DecreaseKey
12
Lazy Deletion !Heap ( (a, 1), (b, 4), (c, 5), (d, 8) ) =
(a,1)
(b,4)
(d,8)
(c,5)
Insert(d, 3):
(a,1)
(d,3)
(d,8) (b,4)
(c,5)
ExtractMin()→ (a, 1)(d,3)
(b,4)
(d,8)
(c,5)
ExtractMin()→ (d, 3)(b,4)
(d,8) (c,5)
Later ExtractMin()→ (d, 8) must be ignored
13
Lazy Deletion !Heap ( (a, 1), (b, 4), (c, 5), (d, 8) ) =
(a,1)
(b,4)
(d,8)
(c,5)
Insert(d, 3):(a,1)
(d,3)
(d,8) (b,4)
(c,5)
ExtractMin()
→ (a, 1)(d,3)
(b,4)
(d,8)
(c,5)
ExtractMin()→ (d, 3)(b,4)
(d,8) (c,5)
Later ExtractMin()→ (d, 8) must be ignored
13
Lazy Deletion !Heap ( (a, 1), (b, 4), (c, 5), (d, 8) ) =
(a,1)
(b,4)
(d,8)
(c,5)
Insert(d, 3):(a,1)
(d,3)
(d,8) (b,4)
(c,5)
ExtractMin()→ (a, 1)(d,3)
(b,4)
(d,8)
(c,5)
ExtractMin()
→ (d, 3)(b,4)
(d,8) (c,5)
Later ExtractMin()→ (d, 8) must be ignored
13
Lazy Deletion !Heap ( (a, 1), (b, 4), (c, 5), (d, 8) ) =
(a,1)
(b,4)
(d,8)
(c,5)
Insert(d, 3):(a,1)
(d,3)
(d,8) (b,4)
(c,5)
ExtractMin()→ (a, 1)(d,3)
(b,4)
(d,8)
(c,5)
ExtractMin()→ (d, 3)(b,4)
(d,8) (c,5)
Later ExtractMin()→ (d, 8) must be ignored13
Runtime Dijkstra
n := |V |, m := |E|n× ExtractMin: O(n log n)m× Insert or DecreaseKey: O(m log |V |)1× Init: O(n)Overal: O((n+m) log n). for connected graphs: O(m log n)
14
Quiz
n := |V |,m := |E|, c : E → R
problem method runtime dense sparsem ∈ O(n2) m ∈ O(n)
c ≡ 1 BFS
O(m+ n) O(n2) O(n)DAG Top-Sort O(m+ n) O(n2) O(n)c ≥ 0 Dijkstra O((m+ n) logn) O(n2 logn) O(n logn)general Bellman-Ford1 O(m · n) O(n3) O(n2)
1will be covered later in class (dynamic programming)15
Quiz
n := |V |,m := |E|, c : E → R
problem method runtime dense sparsem ∈ O(n2) m ∈ O(n)
c ≡ 1 BFS O(m+ n)
O(n2) O(n)DAG Top-Sort O(m+ n) O(n2) O(n)c ≥ 0 Dijkstra O((m+ n) logn) O(n2 logn) O(n logn)general Bellman-Ford1 O(m · n) O(n3) O(n2)
1will be covered later in class (dynamic programming)15
Quiz
n := |V |,m := |E|, c : E → R
problem method runtime dense sparsem ∈ O(n2) m ∈ O(n)
c ≡ 1 BFS O(m+ n) O(n2)
O(n)DAG Top-Sort O(m+ n) O(n2) O(n)c ≥ 0 Dijkstra O((m+ n) logn) O(n2 logn) O(n logn)general Bellman-Ford1 O(m · n) O(n3) O(n2)
1will be covered later in class (dynamic programming)15
Quiz
n := |V |,m := |E|, c : E → R
problem method runtime dense sparsem ∈ O(n2) m ∈ O(n)
c ≡ 1 BFS O(m+ n) O(n2) O(n)DAG Top-Sort
O(m+ n) O(n2) O(n)c ≥ 0 Dijkstra O((m+ n) logn) O(n2 logn) O(n logn)general Bellman-Ford1 O(m · n) O(n3) O(n2)
1will be covered later in class (dynamic programming)15
Quiz
n := |V |,m := |E|, c : E → R
problem method runtime dense sparsem ∈ O(n2) m ∈ O(n)
c ≡ 1 BFS O(m+ n) O(n2) O(n)DAG Top-Sort O(m+ n)
O(n2) O(n)c ≥ 0 Dijkstra O((m+ n) logn) O(n2 logn) O(n logn)general Bellman-Ford1 O(m · n) O(n3) O(n2)
1will be covered later in class (dynamic programming)15
Quiz
n := |V |,m := |E|, c : E → R
problem method runtime dense sparsem ∈ O(n2) m ∈ O(n)
c ≡ 1 BFS O(m+ n) O(n2) O(n)DAG Top-Sort O(m+ n) O(n2)
O(n)c ≥ 0 Dijkstra O((m+ n) logn) O(n2 logn) O(n logn)general Bellman-Ford1 O(m · n) O(n3) O(n2)
1will be covered later in class (dynamic programming)15
Quiz
n := |V |,m := |E|, c : E → R
problem method runtime dense sparsem ∈ O(n2) m ∈ O(n)
c ≡ 1 BFS O(m+ n) O(n2) O(n)DAG Top-Sort O(m+ n) O(n2) O(n)c ≥ 0 Dijkstra
O((m+ n) logn) O(n2 logn) O(n logn)general Bellman-Ford1 O(m · n) O(n3) O(n2)
1will be covered later in class (dynamic programming)15
Quiz
n := |V |,m := |E|, c : E → R
problem method runtime dense sparsem ∈ O(n2) m ∈ O(n)
c ≡ 1 BFS O(m+ n) O(n2) O(n)DAG Top-Sort O(m+ n) O(n2) O(n)c ≥ 0 Dijkstra O((m+ n) logn)
O(n2 logn) O(n logn)general Bellman-Ford1 O(m · n) O(n3) O(n2)
1will be covered later in class (dynamic programming)15
Quiz
n := |V |,m := |E|, c : E → R
problem method runtime dense sparsem ∈ O(n2) m ∈ O(n)
c ≡ 1 BFS O(m+ n) O(n2) O(n)DAG Top-Sort O(m+ n) O(n2) O(n)c ≥ 0 Dijkstra O((m+ n) logn) O(n2 logn)
O(n logn)general Bellman-Ford1 O(m · n) O(n3) O(n2)
1will be covered later in class (dynamic programming)15
Quiz
n := |V |,m := |E|, c : E → R
problem method runtime dense sparsem ∈ O(n2) m ∈ O(n)
c ≡ 1 BFS O(m+ n) O(n2) O(n)DAG Top-Sort O(m+ n) O(n2) O(n)c ≥ 0 Dijkstra O((m+ n) logn) O(n2 logn) O(n logn)
general Bellman-Ford1 O(m · n) O(n3) O(n2)
1will be covered later in class (dynamic programming)15
Quiz
n := |V |,m := |E|, c : E → R
problem method runtime dense sparsem ∈ O(n2) m ∈ O(n)
c ≡ 1 BFS O(m+ n) O(n2) O(n)DAG Top-Sort O(m+ n) O(n2) O(n)c ≥ 0 Dijkstra O((m+ n) logn) O(n2 logn) O(n logn)general Bellman-Ford1 O(m · n) O(n3) O(n2)
1will be covered later in class (dynamic programming)15
An Interesting Graph
s t32 16 8 4 2
5-5
10-10
19-19
36-36
69-69
Does Dijkstra work? (Try it out!)
16
Answer
Dijkstra (as we have presented it) works also for graphs with negative edgeweights, if no negative weight cycles are present. But Dijkstra may thenexhibit exponential running time!
17
Answer
Dijkstra (as we have presented it) works also for graphs with negative edgeweights, if no negative weight cycles are present. But Dijkstra may thenexhibit exponential running time!
17
Shortest Path in a Maze
18
A*-Algorithm(G, s, t, h)Input: Positively weighted Graph G = (V,E, c), starting point s ∈ V , end point
t ∈ V , estimate h(v) ≤ δ(v, t)Output: Existence and value of a shortest path from s to t
foreach u ∈ V do
d[u]←∞; f [u]←∞; π[u]← null
d[s]← 0; f [s]← h(s); R← {s}; M ← {}while R 6= ∅ do
u← ExtractMinf(R); M ←M ∪ {u}
if u = t then return successforeach v ∈ N+(u) with d[v] > d[u] + c(u, v) do
d[v]← d[u] + c(u, v); f [v]← d[v] + h(v); π[v]← uR← R ∪ {v}; M ←M − {v}
return failure19
Dijkstra and A*Edge data structure
Stores length and the destination nodeUsed for the edges in the graph, as well as for the back-edges of theshortest path
Graph data structureNodeP: Pointer to a nodestd::vector<Edge> get_adj(NodeP src): Returns a vector of Edgestarting from src
std::map<NodeP,Edge>
Maps a NodeP to an Edgem[u] Returns an edge
20
Dijkstra and A*Edge data structure
Stores length and the destination nodeUsed for the edges in the graph, as well as for the back-edges of theshortest path
Graph data structureNodeP: Pointer to a nodestd::vector<Edge> get_adj(NodeP src): Returns a vector of Edgestarting from src
std::map<NodeP,Edge>
Maps a NodeP to an Edgem[u] Returns an edge
20
Dijkstra and A*Edge data structure
Stores length and the destination nodeUsed for the edges in the graph, as well as for the back-edges of theshortest path
Graph data structureNodeP: Pointer to a nodestd::vector<Edge> get_adj(NodeP src): Returns a vector of Edgestarting from src
std::map<NodeP,Edge>
Maps a NodeP to an Edgem[u] Returns an edge
20
Dijkstra and A*
Node StructStores x and y coordinates
Manhattan Distance:d = |∆x|+ |∆y|
std::pair
Access with p.first und p.secondUsed to store nodes with their distances from s in the heap→ LazyDeletion
21
Dijkstra and A*
Node StructStores x and y coordinates
Manhattan Distance:d = |∆x|+ |∆y|
std::pair
Access with p.first und p.secondUsed to store nodes with their distances from s in the heap→ LazyDeletion
21
Dijkstra and A*
Node StructStores x and y coordinates
Manhattan Distance:d = |∆x|+ |∆y|
std::pair
Access with p.first und p.secondUsed to store nodes with their distances from s in the heap→ LazyDeletion
21
3. In-Class-Exercises
22
In-Class Programming Exercise
Implement lazy deletion in a heap.−→ CodeExpert
23
In-Class-Exercises (theory): Route planning
Exercise: You are givena directed, unweighted Graph G = (V,E),represented by an adjacency list,and a designated node t ∈ V (e.g., an emergency exit).
Design an algorithm,which computes for each node u ∈ V an outgoing edge in direction ofa shortest path to t.and has a running time of O(|V |+ |E|).
24
In-Class-Exercises: Route planning
Solution:1. Make a copy of the graph with edges having reverse direction:GT = (V,ET ), where ET = {(v, u) | (u, v) ∈ E}.Running time: O(|V |+ |E|).
2. Start a breadth-�rst search of GT , starting from t,and store all edges of the BFS-Tree.Running time: O(|V |+ |ET |) = O(|V |+ |E|).
3. Assign the stored edges (in reverse direction)to the discovered nodes. Running time: O(|V |).
25