Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph...

59
. . . . . . 1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming Chu University of Aizu Last Updated: 2020/12/1 Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu Algorithms and Data Structures

Transcript of Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph...

Page 1: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 1/1

Algorithms and Data Structures12th Lecture: Graph Algorithms II

Yutaka Watanobe, Jie Huang, Yan Pei,Wenxi Chen, Qiangfu Zhao, Wanming Chu

University of Aizu

Last Updated: 2020/12/1

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 2: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 2/1

Outline

Weighted GraphsMinimum Spanning TreeSingle-Source Shortest Paths

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 3: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 3/1

Adjacency Matrix Representation: UndirectedWeighted Graph

For a given connected, undirected graph G = (V ,E) where foreach edge (u, v) ∈ E , we have a weight w(u, v) specifying thecost to connect u and v .

0 1 2 3 4 5 6

0

1

2

3

4

5

6

∞ 8 12 3

8

12

3 7 23

7 -2

23 -2

1

0

2

3

6

5

4

j

i

8

12

3

7

23

-2

∞ ∞ ∞

∞ ∞ ∞

∞ ∞ ∞

∞ ∞ ∞

∞ ∞ ∞

∞ ∞ ∞ ∞

∞ ∞∞ ∞ ∞

∞ ∞ ∞

∞ ∞ ∞ ∞ ∞ ∞

∞ ∞

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 4: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 4/1

Adjacency List Representation: Directed WeightedGraph

For a given connected, directed graph G = (V ,E) where for eachedge (u, v) ∈ E , we have a weight w(u, v) specifying the cost toconnect u and v .

1

0

2

3

6

5

4

6

12

7

8

3

53

1

0

10

1

2

3

4

5

6

2

4

3

2

5

5

36 12 7

8

3 53

1 0

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 5: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 5/1

Spanning Tree

(a) (b) (c)

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 6: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 6/1

Minimum Spanning Tree (1)

4

5

13

23

2

1

2

10

1

3

100

12

356

7 4

2

1

21

3

7

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 7: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 7/1

Minimum Spanning Tree (2)

We wish to find an acyclic subset T ⊆ E that connects all of thevertices and whose total weight

w(T ) =∑

(u,v)∈T

w(u, v)

is minimized.Since T is acyclic and connects all of the vertices, it must form atree, which we call a spanning tree.We call the problem of determining the tree T the minimumspanning-tree (MST) problem.

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 8: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 8/1

Minimum Spanning Tree (3)

b

h

c

g

d

f

ia e11

4

8

8 7

1 2

2

6

4

7

14

10

9

A minimum spanning tree for a connected graph.The weights on edges are shown, and the edges in a minimumspanning tree are shaded.The total weight of the tree is 37.

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 9: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 9/1

Growing a Minimum Spanning Tree (1)

Assume that we have a connected, undirected graph G = (V ,E)with a weight function w : E → R, and we wish to find a MST forG.We use a greedy approach. It is captured by the ”generic”algorithm, which grows the minimum spanning tree one edge ata time.The algorithm manages a set of edges T , maintaining thefollowing loop invariant:

Prior to each iteration, T is a subset of some minimum spanningtree.

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 10: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 10/1

Growing a Minimum Spanning Tree (2)

At each step, we determine an edge (u, v) that can be added toT without violating this invariant, in the sense that T ∪ {(u, v)} isalso a subset of a MST.We call such an edge a safe edge for T , since it can be safelyadded to T while maintaining the invariant.

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 11: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 11/1

Generic Algorithm for MST

genericMST(G, w)

T = empty

while T does not form a spanning tree

find an edge (u, v) that is safe for T

T = T ∪ {(u, v)}

return T

This is a “generic” algorithm which can be a basis to implementdifferent algorithms.

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 12: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 12/1

Prim’s Algorithm for MST

Prim’s algorithm is a special case of the generic MST algorithms.Prim’s algorithm has the property that the edges in the set Talways form a single tree.The tree starts from an arbitrary root vertex r and grows until thetree spans all the vertices in V .At each step, a light edge is added to the tree T that connects Tto an isolated vertex of GT = (V ,T ). This rule adds only edgesthat are safe for T .This strategy is greedy since the tree is augmented at each stepwith an edge that contributes the minimum amount possible tothe tree’s weight.

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 13: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 13/1

Prim’s Algorithm based on the Generic Algorithm (1)

b

h

c

g

d

f

ia e11

4

8

8 7

1 2

2

6

4

7

14

10

9

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 14: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 14/1

Prim’s Algorithm based on the Generic Algorithm (2)

b

h

c

g

d

f

ia e11

4

8

8 7

1 2

2

6

4

7

14

10

9

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 15: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 15/1

Prim’s Algorithm based on the Generic Algorithm (3)

b

h

c

g

d

f

ia e11

4

8

8 7

1 2

2

6

4

7

14

10

9

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 16: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 16/1

Prim’s Algorithm based on the Generic Algorithm (4)

b

h

c

g

d

f

ia e11

4

8

8 7

1 2

2

6

4

7

14

10

9

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 17: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 17/1

Prim’s Algorithm based on the Generic Algorithm (5)

b

h

c

g

d

f

ia e11

4

8

8 7

1 2

2

6

4

7

14

10

9

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 18: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 18/1

Prim’s Algorithm based on the Generic Algorithm (6)

b

h

c

g

d

f

ia e11

4

8

8 7

1 2

2

6

4

7

14

10

9

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 19: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 19/1

Prim’s Algorithm based on the Generic Algorithm (7)

b

h

c

g

d

f

ia e11

4

8

8 7

1 2

2

6

4

7

14

10

9

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 20: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 20/1

Prim’s Algorithm based on the Generic Algorithm (8)

b

h

c

g

d

f

ia e11

4

8

8 7

1 2

2

6

4

7

14

10

9

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 21: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 21/1

Prim’s Algorithm based on the Generic Algorithm (9)

b

h

c

g

d

f

ia e11

4

8

8 7

1 2

2

6

4

7

14

10

9

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 22: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 22/1

Prim’s Algorithm: Implementation

T T’

V - T V - T

u

v

v

v

v

v

Candidates

r r

Update

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 23: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 23/1

Prim’s Algorithm1

0

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

20

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

10

3

11

18

30

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

5

3

7

18

2

5

40

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

1

3

7

18

2

2

50

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

1

3

7

18

2

2

60

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

1

3

2

18

2

2

70

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

1

3

2

1

2

2

80

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

1

3

2

1

2

2

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 24: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 24/1

Prim’s Algorithm (1)

10

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 25: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 25/1

Prim’s Algorithm (2)

20

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

10

3

11

18

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 26: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 26/1

Prim’s Algorithm (3)

30

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

5

3

7

18

2

5

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 27: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 27/1

Prim’s Algorithm (4)

40

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

1

3

7

18

2

2

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 28: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 28/1

Prim’s Algorithm (5)

50

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

1

3

7

18

2

2

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 29: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 29/1

Prim’s Algorithm (6)

60

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

1

3

2

18

2

2

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 30: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 30/1

Prim’s Algorithm (7)

70

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

1

3

2

1

2

2

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 31: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 31/1

Prim’s Algorithm (8)

80

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

1

3

2

1

2

2

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 32: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 32/1

Prim’s Algorithm: O(|V |2) Implementation (1)

prim(G, w, r)

for each u in G

d[u] = inf

pi[u] = NIL

color[u] = WHITE

d[r] = 0

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 33: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 33/1

Prim’s Algorithm: O(|V |2) Implementation (2)

while true

mincost = inf

for each i in G

if color[i] != BLACK and d[i] < mincost

mincost = d[i]

u = i

if mincost == inf

break

color[u] = BLACK

for each v in Adj[u]

if color[v] != BLACK and w(u, v) < d[v]

pi[v] = u

d[v] = w(u, v)

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 34: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 34/1

Prim’s algorithm: ((|V |+ |E |) log |V |) Implementation(1)

prim(G, w, r)

for each u in G

d[u] = inf

pi[u] = NIL

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 35: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 35/1

Prim’s algorithm: ((|V |+ |E |) log |V |) Implementation(2)

For an efficient implementation, we use a min-heap H of vertices,keyed by their d values.

d[r] = 0

H = buildMinHeap(V)

while H is not empty

u = H.extractMin()

color[u] = BLACK

for each v in Adj[u]

if color[v] != BLACK

if w(u, v) < d[v]

pi[v] = u

d[v] = w(u, v)

heapDecreaseKey(H, v, w(u, v))

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 36: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 36/1

Single-Source Shortest Paths (1)

We are given a weighted, directed graph G = (V ,E), with weightfunction w : E → R mapping edges to real-valued weights.The weight of path p = {v0, v1, ..., vk} is the sum of the weights ofits constituent edges:

w(p) =k∑

i=1

w(vi−1, vi)

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 37: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 37/1

Single-Source shortest Paths (2)

We define the shortest-path weight from u to v by

δ(u, v) ={

min{w(p) : u → v} if there is a path from u to v∞ otherwise

A shortest path from vertex u to vertex v is then defined as anypath p with weight w(p) = δ(u, v).Given a graph G = (V ,E), we want to find a shortest path from agiven source vertex s ∈ V to each vertex v ∈ V .

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 38: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 38/1

Initialize

For each vertex v ∈ V , we maintain an attribute d [v ], which is anupper bound on the weight of a shortest path from source s to v .We call d [v ] a shortest-path estimate.We initialize the shortest-path estimates and predecessors bythe following procedure.

initializeSingleSource(G, s)

for each v in G

d[v] = inf

pi[v] = NIL

d[s] = 0

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 39: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 39/1

Relaxation

The process of relaxing an edge (u, v) consists of testingwhether we can improve the shortest path to v found so far bygoing through u and, if so, updating d [v ] and pi[v ].A relaxation step may decrease the value of the shortest-pathestimate d [v ] and update v ’s predecessor field pi[v ].

relax(u, v, w)

if d[v] > d[u] + w(u, v)

d[v] = d[u] + w(u, v)

pi[v] = u

u

s

v

w(u, v)

d[v]

d[u]

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 40: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 40/1

Dijkstra’s Algorithm for SSSP (1)

Dijkstra’s algorithm solves the single-source shortest-pathsproblem on a weighted, directed graph G = (V ,E) for the case inwhich all edge weights are nonnegative.Dijkstra’s algorithm maintains a set S of vertices whose finalshortest path weights from the source s have already beendetermined.

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 41: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 41/1

Dijkstra’s Algorithm for SSSP (2)

The algorithm repeatedly selects the vertex u ∈ V − S with theminimum shortest-path estimate, adds u to S, and relaxes alledges leaving u.

S S’

V - S V - S

u

v

v

v

v

v

Candidates

s s

Update

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 42: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 42/1

Dijkstra’s Algorithm: O(|V |2) Implementation

dijkstra(G, w, s)

initializeSingleSource(G, s)

while true

mincost = inf

for each i in G

if color[i] != BLACK and d[i] < mincost

mincost = d[i]

u = i

if mincost == inf

break

color[u] = BLACK

for each v in Adj[u]

if color[v] != BLACK and d[u] + w(u, v) < d[v]

pi[v] = u

d[v] = d[u] + w(u, v)

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 43: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 43/1

Dijkstra’s Algorithm: O((|V |+ |E |) log |V |)Implementation

For an efficient implementation, we use a min-heap H of vertices,keyed by their d values.

dijkstra(G, w, s)

initializeSingleSource(G, s)

H = buildMinHeap(V)

while H is not empty

u = H.extractMin()

color[u] = BLACK

for each v in Adj[u]

if color[v] != BLACK

if d[u] + w(u, v) < d[v]

d[v] = d[u] + w(u, v)

color[v] = GRAY

hepaDecreaseKey(H, v, d[u] + w(u, v))

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 44: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 44/1

Dijkstra’s Algorithm (1)

5

10

2

9

3

2

7

1∞

0 4 6

u v

x y

s

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 45: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 45/1

Dijkstra’s Algorithm (2)

5

10

2

9

3

2

7

110

5

0 4 6

u v

x y

s

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 46: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 46/1

Dijkstra’s Algorithm (3)

5

10

2

9

3

2

7

18

5

14

7

0 4 6

u v

x y

s

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 47: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 47/1

Dijkstra’s Algorithm (4)

5

10

2

9

3

2

7

18

5

13

7

0 4 6

u v

x y

s

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 48: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 48/1

Dijkstra’s Algorithm (5)

5

10

2

9

3

2

7

18

5

9

7

0 4 6

u v

x y

s

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 49: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 49/1

Dijkstra’s Algorithm (6)

5

10

2

9

3

2

7

18

5

9

7

0 4 6

u v

x y

s

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 50: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 50/1

Dijkstra’s Algorithm: Another Example1

0

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

20

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

10

3

11

18

30

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

8

3

10

18

5

8

40

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

6

3

10

18

5

7

50

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

6

3

10

18

5

7

60

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

6

3

9

18

5

7

70

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

6

3

9

10

5

7

80

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

6

3

9

10

5

7

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 51: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 51/1

Dijkstra’s Algorithm: Another Example (1)

10

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 52: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 52/1

Dijkstra’s Algorithm: Another Example (2)

20

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

10

3

11

18

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 53: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 53/1

Dijkstra’s Algorithm: Another Example (3)

30

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

8

3

10

18

5

8

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 54: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 54/1

Dijkstra’s Algorithm: Another Example (4)

40

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

6

3

10

18

5

7

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 55: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 55/1

Dijkstra’s Algorithm: Another Example (5)

50

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

6

3

10

18

5

7

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 56: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 56/1

Dijkstra’s Algorithm: Another Example (6)

60

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

6

3

9

18

5

7

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 57: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 57/1

Dijkstra’s Algorithm: Another Example (7)

70

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

6

3

9

10

5

7

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 58: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 58/1

Dijkstra’s Algorithm: Another Example (8)

80

1

2 3

56

4

10

3

11

18

1

7

2

5

2

15

2

0

6

3

9

10

5

7

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures

Page 59: Algorithms and Data Structures. . . . . .1/1 Algorithms and Data Structures 12th Lecture: Graph Algorithms II Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming

. . . . . . 59/1

Reference

1 Introduction to Algorithms (third edition), Thomas H.Cormen,Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. TheMIT Press, 2012.

Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

Algorithms and Data Structures