Exercise Session 6 – Shortest Paths

62
Exercise Session 6 – Shortest Paths Computer Science II, D-ITET, ETH Zurich

Transcript of Exercise Session 6 – Shortest Paths

Page 1: Exercise Session 6 – Shortest Paths

Exercise Session 6 – Shortest PathsComputer Science II, D-ITET, ETH Zurich

Page 2: Exercise Session 6 – Shortest Paths

Program Today

Feedback of last exercises

Shortest pathsDijkstraHeaps, DecreaseKey and Lazy DeletionRunning Time of the AlgorithmsDijkstra and Negative Edge Weights?

In-Class-Exercises

1

Page 3: Exercise Session 6 – Shortest Paths

1. Feedback of last exercises

2

Page 4: Exercise Session 6 – Shortest Paths

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

Page 5: Exercise Session 6 – Shortest Paths

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

Page 6: Exercise Session 6 – Shortest Paths

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

Page 7: Exercise Session 6 – Shortest Paths

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

Page 8: Exercise Session 6 – Shortest Paths

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

Page 9: Exercise Session 6 – Shortest Paths

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

Page 10: Exercise Session 6 – Shortest Paths

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

Page 11: Exercise Session 6 – Shortest Paths

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

Page 12: Exercise Session 6 – Shortest Paths

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

Page 13: Exercise Session 6 – Shortest Paths

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

Page 14: Exercise Session 6 – Shortest Paths

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

Page 15: Exercise Session 6 – Shortest Paths

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

Page 16: Exercise Session 6 – Shortest Paths

2. Shortest paths

6

Page 17: Exercise Session 6 – Shortest Paths

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

Page 18: Exercise Session 6 – Shortest Paths

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

Page 19: Exercise Session 6 – Shortest Paths

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

Page 20: Exercise Session 6 – Shortest Paths

Example

s

a

b

c

d

e

2

3

2

6

1

3

1

1

10

Page 21: Exercise Session 6 – Shortest Paths

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

Page 22: Exercise Session 6 – Shortest Paths

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

Page 23: Exercise Session 6 – Shortest Paths

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

Page 24: Exercise Session 6 – Shortest Paths

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

Page 25: Exercise Session 6 – Shortest Paths

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

Page 26: Exercise Session 6 – Shortest Paths

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

Page 27: Exercise Session 6 – Shortest Paths

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

Page 28: Exercise Session 6 – Shortest Paths

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

Page 29: Exercise Session 6 – Shortest Paths

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

Page 30: Exercise Session 6 – Shortest Paths

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

Page 31: Exercise Session 6 – Shortest Paths

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

Page 32: Exercise Session 6 – Shortest Paths

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

Page 33: Exercise Session 6 – Shortest Paths

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

Page 34: Exercise Session 6 – Shortest Paths

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

Page 35: Exercise Session 6 – Shortest Paths

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

Page 36: Exercise Session 6 – Shortest Paths

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

Page 37: Exercise Session 6 – Shortest Paths

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

Page 38: Exercise Session 6 – Shortest Paths

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

Page 39: Exercise Session 6 – Shortest Paths

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

Page 40: Exercise Session 6 – Shortest Paths

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

Page 41: Exercise Session 6 – Shortest Paths

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

Page 42: Exercise Session 6 – Shortest Paths

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

Page 43: Exercise Session 6 – Shortest Paths

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

Page 44: Exercise Session 6 – Shortest Paths

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

Page 45: Exercise Session 6 – Shortest Paths

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

Page 46: Exercise Session 6 – Shortest Paths

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

Page 47: Exercise Session 6 – Shortest Paths

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

Page 48: Exercise Session 6 – Shortest Paths

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

Page 49: Exercise Session 6 – Shortest Paths

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

Page 50: Exercise Session 6 – Shortest Paths

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

Page 51: Exercise Session 6 – Shortest Paths

Shortest Path in a Maze

18

Page 52: Exercise Session 6 – Shortest Paths

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

Page 53: Exercise Session 6 – Shortest Paths

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

Page 54: Exercise Session 6 – Shortest Paths

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

Page 55: Exercise Session 6 – Shortest Paths

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

Page 56: Exercise Session 6 – Shortest Paths

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

Page 57: Exercise Session 6 – Shortest Paths

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

Page 58: Exercise Session 6 – Shortest Paths

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

Page 59: Exercise Session 6 – Shortest Paths

3. In-Class-Exercises

22

Page 60: Exercise Session 6 – Shortest Paths

In-Class Programming Exercise

Implement lazy deletion in a heap.−→ CodeExpert

23

Page 61: Exercise Session 6 – Shortest Paths

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

Page 62: Exercise Session 6 – Shortest Paths

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