introduction to algorithms 6.046j/18.401j l ecture 16 greedy algorithms (and graphs) graph...

52
Introduction to Algorithms 6.046J/18.401J LECTURE 16 Greedy Algorithms (and Graphs) Graph representat ion Minimum spanning trees Optimal substruct ure Greedy choice Prim’s greedy MST Prof. Charles E. L eiserson November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.1

Upload: talia-corlew

Post on 15-Dec-2015

228 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

Introduction to Algorithms6.046J/18.401J

LECTURE 16Greedy Algorithms (and Graphs)• Graph representation• Minimum spanning trees• Optimal substructure• Greedy choice• Prim’s greedy MST algorithm

Prof. Charles E. LeisersonNovember 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.1

Page 2: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.2

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.)

Page 3: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.3

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, 0 if (i, j) ∉E.

Adjacency-matrix representation

A[i,j] =

Page 4: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.4

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, 0 if (i, j) ∉E.

Adjacency-matrix representation

A[i,j] =

1234

0 1 1 00 0 1 00 0 0 00 0 1 0

A 1 2 3 4

(V 2) storage⇒denserepresentation.

2 1

3 4

Page 5: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 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.

2 1

3 4

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

Page 6: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

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.

2 1

3 4

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

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

Page 7: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.7

Adjacency-list representation An adjacency list of a vertex v V is the list Adj[v] of vertices adjacent to v.

2 1

3 4

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

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)

Page 8: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.8

Minimum spanning treesInput: 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.)

Page 9: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.9

Minimum spanning treesInput: 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

Page 10: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.10

Example of MST

6 12

14 8

5

7

9

15

3 10

Page 11: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.11

Example of MST

6 12

14 8

5

7

9

15

3 10

Page 12: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.12

Optimal substructure MST T:

(Other edges of Gare not shown.)

Page 13: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.13

Optimal substructure MST T:

(Other edges of G are not shown.)Remove any edge (u, v) ∈T.

u

v

Page 14: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.14

Optimal substructure MST T:

(Other edges of Gare not shown.)

Remove any edge (u, v) T.

u

v

Page 15: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.15

Optimal substructure MST T:

(Other edges of Gare not shown.)

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

T1

u

v

T2

Page 16: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.16

Optimal substructure MST T:

(Other edges of Gare not shown.)

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 }.

T1

u

v

T2

Similarly for T2.

Page 17: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

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 forG1, then T = {(u, v)} ∪ T1 ∪ T2 would be a lower-weight spanning tree than T for G.

Page 18: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

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 forG1, then T = {(u, v)} ∪ T1 ∪ T2 would be a lower-weight spanning tree than T for G.

Do we also have overlapping subproblems?• Yes.

Page 19: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.19

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 forG1, 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.

Page 20: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

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.

Page 21: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.21

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.

Page 22: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

∈A∈V – A

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.22

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

T:

v

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

u

Page 23: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

AV – A

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.23

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

T:

v

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

u

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

Page 24: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

AV – A

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.24

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

T:

v

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

u

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.

Page 25: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.25

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

A V – A

v

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

u

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.

Page 26: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.26

Prim’s algorithmIDEA: Maintain V – A as a priority queue Q. Key each vertex in Q with the weight of the least- weight edge connecting it to a vertex in A.Q ←Vkey[v] ←∞for all v Vkey[s] ←0 for some arbitrary s Vwhile 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.

Page 27: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.27

Example of Prim’s algorithm

∞ 6 12

14

3

8

5

7

10

9

15

0

AV – A

Page 28: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.28

Example of Prim’s algorithm

∞ 6 12

14

3

8

5

7

10

9

15

AV – A

0

Page 29: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.29

Example of Prim’s algorithm

∞ 6 12

14

3

8

5

10

7

10

9

15

AV – A

0

Page 30: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.30

Example of Prim’s algorithm

∞ 6 12

14

3

8

5

10

7

10

9

15

AV – A

0

Page 31: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.31

Example of Prim’s algorithm

6 12

14

3

8

5

10

7

10

9

15

AV – A

0

Page 32: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.32

Example of Prim’s algorithm

6 12

14

3

8

5

10

7

10

9

15

AV – A

0

Page 33: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.33

Example of Prim’s algorithm

6 12

14

3

8

5

8

7

10

9

15

AV – A

0

Page 34: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.34

Example of Prim’s algorithm

6 12

14

3

8

5

8

7

10

9

15

AV – A

0

Page 35: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.35

Example of Prim’s algorithm

6 12

14

3

8

5

8

7

10

9

15

AV – A

0

Page 36: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.36

Example of Prim’s algorithm

6 12

14

3

8

5

8

7

10

9

15

AV – A

0

Page 37: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.37

Example of Prim’s algorithm

6 12

14

3

8

5

8

7

10

9

15

AV – A

0

Page 38: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.38

Example of Prim’s algorithm

6 12

14

3

8

5

8

7

10

9

15

AV – A

0

Page 39: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.39

Example of Prim’s algorithm

6 12

14

3

8

5

8

7

10

9

15

AV – A

0

Page 40: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.40

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

Page 41: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.41

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

(V)total

Page 42: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.42

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

(V)total

|V |times

Page 43: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.43

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

(V)total

|V |times

degree(u)times

Page 44: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.44

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

(V)total

|V |times

degree(u)times

Handshaking Lemma ⇒(E) implicit DECREASE-KEY’s.

Page 45: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.45

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

(V)total

|V |times

degree(u)times

Handshaking Lemma ⇒(E) implicit DECREASE-KEY’s.

Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY

Page 46: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

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

Page 47: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 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 Total

Page 48: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 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)

Page 49: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

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)

binaryheap

O(lg V) O(lg V) O(E lg V)

Page 50: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.50

Analysis of Prim (continued)

Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY

Q TEXTRACT-MIN TDECREASE-KEY Total

array O(V) O(1) O(V2)

binaryheap

O(lg V) O(lg V) O(E lg V)

Fibonacci heap

O(lg V)amortized

O(1)amortized

O(E + V lg V)

worst case

Page 51: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

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).

Page 52: Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy

November 9, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L16.52

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.