Cormen Algo-lec16

  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Cormen Algo-lec16 as PDF for free.

More details

  • Words: 2,888
  • Pages: 52
Introduction to Algorithms 6.046J/18.401J

LECTURE 16 Greedy Algorithms (and Graphs) • Graph representation • Minimum spanning trees • Optimal substructure • Greedy choice • Prim’s greedy MST algorithm Prof. Charles E. Leiserson November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.1

Graphs (review) Definition. A directed graph (digraph) G = (V, E) is an ordered pair consisting of • a set V of vertices (singular: vertex), • a set E ⊆ V × V of edges. In an undirected graph G = (V, E), the edge set E consists of unordered pairs of vertices. In either case, we have | E | = O(V 2). Moreover, if G is connected, then | E | ≥ | V | – 1, which implies that lg | E | = Θ(lg V). (Review CLRS, Appendix B.) November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.2

Adjacency-matrix representation The adjacency matrix of a graph G = (V, E), where V = {1, 2, …, n}, is the matrix A[1 . . n, 1 . . n] given by 1 if (i, j) ∈ E, A[i, j] = 0 if (i, j) ∉ E.

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.3

Adjacency-matrix representation The adjacency matrix of a graph G = (V, E), where V = {1, 2, …, n}, is the matrix A[1 . . n, 1 . . n] given by 1 if (i, j) ∈ E, A[i, j] = 0 if (i, j) ∉ E. 22

11

33

44

November 9, 2005

A 1 2 3 4 1 0 1 1 0 2 0 0 1 0 3 0 0 0 0 4 0 0 1 0

Θ(V 2) storage ⇒ dense representation.

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.4

Adjacency-list representation An adjacency list of a vertex v ∈ V is the list Adj[v] of vertices adjacent to v. Adj[1] = {2, 3} 22 11 33

November 9, 2005

44

Adj[2] = {3} Adj[3] = {} Adj[4] = {3}

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.5

Adjacency-list representation An adjacency list of a vertex v ∈ V is the list Adj[v] of vertices adjacent to v. Adj[1] = {2, 3} 22 11 Adj[2] = {3} Adj[3] = {} Adj[4] = {3}

33 44 For undirected graphs, | Adj[v] | = degree(v). For digraphs, | Adj[v] | = out-degree(v).

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.6

Adjacency-list representation An adjacency list of a vertex v ∈ V is the list Adj[v] of vertices adjacent to v. Adj[1] = {2, 3} 22 11 Adj[2] = {3} Adj[3] = {} Adj[4] = {3}

33 44 For undirected graphs, | Adj[v] | = degree(v). For digraphs, | Adj[v] | = out-degree(v). Handshaking Lemma: ∑v∈V = 2 |E| for undirected graphs ⇒ adjacency lists use Θ(V + E) storage — a sparse representation (for either type of graph). November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.7

Minimum spanning trees Input: A connected, undirected graph G = (V, E) with weight function w : E → R. • For simplicity, assume that all edge weights are distinct. (CLRS covers the general case.)

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.8

Minimum spanning trees Input: A connected, undirected graph G = (V, E) with weight function w : E → R. • For simplicity, assume that all edge weights are distinct. (CLRS covers the general case.) Output: A spanning tree T — a tree that connects all vertices — of minimum weight: w(T ) = ∑ w(u , v) . (u ,v )∈T

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.9

Example of MST 6

12 9

5 14

8 3

November 9, 2005

7

15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.10

Example of MST 6

12 9

5 14

8 3

November 9, 2005

7

15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.11

Optimal substructure MST T: (Other edges of G are not shown.)

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.12

Optimal substructure MST T:

u

(Other edges of G are not shown.)

v

Remove any edge (u, v) ∈ T.

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.13

Optimal substructure MST T:

u

(Other edges of G are not shown.)

v

Remove any edge (u, v) ∈ T.

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.14

Optimal substructure MST T: (Other edges of G are not shown.)

u T1

T2 v

Remove any edge (u, v) ∈ T. Then, T is partitioned into two subtrees T1 and T2.

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.15

Optimal substructure MST T: (Other edges of G are not shown.)

u T1

T2 v

Remove any edge (u, v) ∈ T. Then, T is partitioned into two subtrees T1 and T2. Theorem. The subtree T1 is an MST of G1 = (V1, E1), the subgraph of G induced by the vertices of T1: V1 = vertices of T1, E1 = { (x, y) ∈ E : x, y ∈ V1 }. Similarly for T2. November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.16

Proof of optimal substructure Proof. Cut and paste: w(T) = w(u, v) + w(T1) + w(T2). If T1′ were a lower-weight spanning tree than T1 for G1, then T ′ = {(u, v)} ∪ T1′ ∪ T2 would be a lower-weight spanning tree than T for G.

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.17

Proof of optimal substructure Proof. Cut and paste: w(T) = w(u, v) + w(T1) + w(T2). If T1′ were a lower-weight spanning tree than T1 for G1, then T ′ = {(u, v)} ∪ T1′ ∪ T2 would be a lower-weight spanning tree than T for G. Do we also have overlapping subproblems? •Yes.

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.18

Proof of optimal substructure Proof. Cut and paste: w(T) = w(u, v) + w(T1) + w(T2). If T1′ were a lower-weight spanning tree than T1 for G1, then T ′ = {(u, v)} ∪ T1′ ∪ T2 would be a lower-weight spanning tree than T for G. Do we also have overlapping subproblems? •Yes. Great, then dynamic programming may work! •Yes, but MST exhibits another powerful property which leads to an even more efficient algorithm. November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.19

Hallmark for “greedy” algorithms Greedy-choice property A locally optimal choice is globally optimal.

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.20

Hallmark for “greedy” algorithms Greedy-choice property A locally optimal choice is globally optimal. Theorem. Let T be the MST of G = (V, E), and let A ⊆ V. Suppose that (u, v) ∈ E is the least-weight edge connecting A to V – A. Then, (u, v) ∈ T. November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.21

Proof of theorem Proof. Suppose (u, v) ∉ T. Cut and paste. T:

v ∈A ∈V–A

November 9, 2005

u (u, v) = least-weight edge connecting A to V – A

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.22

Proof of theorem Proof. Suppose (u, v) ∉ T. Cut and paste. T:

v ∈A ∈V–A

u (u, v) = least-weight edge connecting A to V – A

Consider the unique simple path from u to v in T.

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.23

Proof of theorem Proof. Suppose (u, v) ∉ T. Cut and paste. T:

v ∈A ∈V–A

u (u, v) = least-weight edge connecting A to V – A

Consider the unique simple path from u to v in T. Swap (u, v) with the first edge on this path that connects a vertex in A to a vertex in V – A. November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.24

Proof of theorem Proof. Suppose (u, v) ∉ T. Cut and paste. T ′: ∈A ∈V–A

v u (u, v) = least-weight edge connecting A to V – A

Consider the unique simple path from u to v in T. Swap (u, v) with the first edge on this path that connects a vertex in A to a vertex in V – A. A lighter-weight spanning tree than T results. November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.25

Prim’s algorithm IDEA: Maintain V – A as a priority queue Q. Key each vertex in Q with the weight of the leastweight edge connecting it to a vertex in A. Q←V key[v] ← ∞ for all v ∈ V key[s] ← 0 for some arbitrary s ∈ V while Q ≠ ∅ do u ← EXTRACT-MIN(Q) for each v ∈ Adj[u] do if v ∈ Q and w(u, v) < key[v] then key[v] ← w(u, v) ⊳ DECREASE-KEY π[v] ← u

At the end, {(v, π[v])} forms the MST. November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.26

Example of Prim’s algorithm ∈A ∈V–A

6

5

∞ ∞ 14 ∞ ∞

8 3

November 9, 2005

∞ ∞

∞ ∞

12 9

∞ ∞ ∞ ∞ 7 15 00 ∞ ∞ 10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.27

Example of Prim’s algorithm ∈A ∈V–A

6

5

∞ ∞ 14 ∞ ∞

8 3

November 9, 2005

∞ ∞

∞ ∞

12 9

∞ ∞ ∞ ∞ 7 15 00 ∞ ∞ 10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.28

Example of Prim’s algorithm ∈A ∈V–A

6

12

5

∞ ∞ 14 ∞ ∞

77 7

8 3

November 9, 2005

∞ ∞

00 10 10

9 15

∞ ∞ 15 15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.29

Example of Prim’s algorithm ∈A ∈V–A

6

12

5

∞ ∞ 14 ∞ ∞

77 7

8 3

November 9, 2005

∞ ∞

00 10 10

9 15

∞ ∞ 15 15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.30

Example of Prim’s algorithm ∈A ∈V–A

12 12

6

5

55 14 ∞ ∞

77 7

8 3

November 9, 2005

12

00 10 10

9 15

99 15 15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.31

Example of Prim’s algorithm ∈A ∈V–A

12 12

6

5

55 14 ∞ ∞

77 7

8 3

November 9, 2005

12

00 10 10

9 15

99 15 15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.32

Example of Prim’s algorithm ∈A ∈V–A

66

6

5

55 14 14 14

77 7

8 3

November 9, 2005

12

00 88

9 15

99 15 15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.33

Example of Prim’s algorithm ∈A ∈V–A

66

6

5

55 14 14 14

77 7

8 3

November 9, 2005

12

00 88

9 15

99 15 15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.34

Example of Prim’s algorithm ∈A ∈V–A

66

6

5

55 14 14 14

77 7

8 3

November 9, 2005

12

00 88

9 15

99 15 15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.35

Example of Prim’s algorithm ∈A ∈V–A

66

6

5

55 14 3

77 7

8

33

November 9, 2005

12

00 88

9 15

99 15 15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.36

Example of Prim’s algorithm ∈A ∈V–A

66

6

5

55 14 3

77 7

8

33

November 9, 2005

12

00 88

9 15

99 15 15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.37

Example of Prim’s algorithm ∈A ∈V–A

66

6

5

55 14 3

77 7

8

33

November 9, 2005

12

00 88

9 15

99 15 15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.38

Example of Prim’s algorithm ∈A ∈V–A

66

6

5

55 14 3

77 7

8

33

November 9, 2005

12

00 88

9 15

99 15 15

10

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.39

Analysis of Prim Q←V key[v] ← ∞ for all v ∈ V key[s] ← 0 for some arbitrary s ∈ V while Q ≠ ∅ do u ← EXTRACT-MIN(Q) for each v ∈ Adj[u] do if v ∈ Q and w(u, v) < key[v] then key[v] ← w(u, v) π[v] ← u

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.40

Analysis of Prim Θ(V) total

November 9, 2005

Q←V key[v] ← ∞ for all v ∈ V key[s] ← 0 for some arbitrary s ∈ V while Q ≠ ∅ do u ← EXTRACT-MIN(Q) for each v ∈ Adj[u] do if v ∈ Q and w(u, v) < key[v] then key[v] ← w(u, v) π[v] ← u

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.41

Analysis of Prim Θ(V) total

|V | times

November 9, 2005

Q←V key[v] ← ∞ for all v ∈ V key[s] ← 0 for some arbitrary s ∈ V while Q ≠ ∅ do u ← EXTRACT-MIN(Q) for each v ∈ Adj[u] do if v ∈ Q and w(u, v) < key[v] then key[v] ← w(u, v) π[v] ← u

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.42

Analysis of Prim Q←V Θ(V) key[v] ← ∞ for all v ∈ V total key[s] ← 0 for some arbitrary s ∈ V while Q ≠ ∅ do u ← EXTRACT-MIN(Q) for each v ∈ Adj[u] |V | do if v ∈ Q and w(u, v) < key[v] times degree(u) times then key[v] ← w(u, v) π[v] ← u

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.43

Analysis of Prim Q←V Θ(V) key[v] ← ∞ for all v ∈ V total key[s] ← 0 for some arbitrary s ∈ V while Q ≠ ∅ do u ← EXTRACT-MIN(Q) for each v ∈ Adj[u] |V | do if v ∈ Q and w(u, v) < key[v] times degree(u) times then key[v] ← w(u, v) π[v] ← u Handshaking Lemma ⇒ Θ(E) implicit DECREASE-KEY’s. November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.44

Analysis of Prim Q←V Θ(V) key[v] ← ∞ for all v ∈ V total key[s] ← 0 for some arbitrary s ∈ V while Q ≠ ∅ do u ← EXTRACT-MIN(Q) for each v ∈ Adj[u] |V | do if v ∈ Q and w(u, v) < key[v] times degree(u) times then key[v] ← w(u, v) π[v] ← u Handshaking Lemma ⇒ Θ(E) implicit DECREASE-KEY’s.

Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.45

Analysis of Prim (continued) Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.46

Analysis of Prim (continued) Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY Q

TEXTRACT-MIN TDECREASE-KEY

November 9, 2005

Total

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.47

Analysis of Prim (continued) Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY Q

TEXTRACT-MIN TDECREASE-KEY

array

November 9, 2005

O(V)

O(1)

Total O(V2)

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.48

Analysis of Prim (continued) Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY Q

TEXTRACT-MIN TDECREASE-KEY

Total

array

O(V)

O(1)

O(V2)

binary heap

O(lg V)

O(lg V)

O(E lg V)

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.49

Analysis of Prim (continued) Time = Θ(V)·TEXTRACT-MIN + Θ(E)·TDECREASE-KEY Q

TEXTRACT-MIN TDECREASE-KEY

Total

array

O(V)

O(1)

O(V2)

binary heap

O(lg V)

O(lg V)

O(E lg V)

Fibonacci O(lg V) heap amortized November 9, 2005

O(1) O(E + V lg V) amortized worst case

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.50

MST algorithms Kruskal’s algorithm (see CLRS): • Uses the disjoint-set data structure (Lecture 10). • Running time = O(E lg V).

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.51

MST algorithms Kruskal’s algorithm (see CLRS): • Uses the disjoint-set data structure (Lecture 10). • Running time = O(E lg V). Best to date: • Karger, Klein, and Tarjan [1993]. • Randomized algorithm. • O(V + E) expected time.

November 9, 2005

Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson

L16.52

Related Documents

Cormen Algo-lec15
December 2019 12
Cormen Algo-lec12
December 2019 12
Cormen Algo-lec16
December 2019 11
Cormen Algo-lec13
December 2019 11
Cormen Algo-lec10
December 2019 1
Cormen Algo-lec19
December 2019 1