0 Up votes0 Down votes

13 views52 pagesOct 13, 2013

lec16

© Attribution Non-Commercial (BY-NC)

PDF, TXT or read online from Scribd

Attribution Non-Commercial (BY-NC)

13 views

lec16

Attribution Non-Commercial (BY-NC)

- Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
- Hidden Figures Young Readers' Edition
- The Law of Explosive Growth: Lesson 20 from The 21 Irrefutable Laws of Leadership
- The Art of Thinking Clearly
- The E-Myth Revisited: Why Most Small Businesses Don't Work and
- The Wright Brothers
- The Other Einstein: A Novel
- State of Fear
- State of Fear
- The Power of Discipline: 7 Ways it Can Change Your Life
- The Kiss Quotient: A Novel
- The 10X Rule: The Only Difference Between Success and Failure
- Being Wrong: Adventures in the Margin of Error
- Algorithms to Live By: The Computer Science of Human Decisions

You are on page 1of 52

6.046J/18.401J LECTURE 16

Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy choice Prims 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

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. 2 2 3 3 1 1 4 4 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.

L16.4

November 9, 2005

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} 2 1 1 2 3 3 4 4

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

November 9, 2005

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} 2 1 1 2 3 4 4 3 For undirected graphs, | Adj[v] | = degree(v). For digraphs, | Adj[v] | = out-degree(v).

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

November 9, 2005

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} 2 1 1 2 3 4 4 3 For undirected graphs, | Adj[v] | = degree(v). For digraphs, | Adj[v] | = out-degree(v). Handshaking Lemma: vV = 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

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

L16.8

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

L16.9

Example of MST

6 5 14 3 8 7 10 12 9 15

November 9, 2005

L16.10

Example of MST

6 5 14 3 8 7 10 12 9 15

November 9, 2005

L16.11

Optimal substructure

MST T:

(Other edges of G are not shown.)

November 9, 2005

L16.12

Optimal substructure

MST T:

(Other edges of G are not shown.)

u v

November 9, 2005

L16.13

Optimal substructure

MST T:

(Other edges of G are not shown.)

u v

November 9, 2005

L16.14

Optimal substructure

MST T:

(Other edges of G are not shown.)

u T1 v

T2

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

November 9, 2005

L16.15

Optimal substructure

MST T:

(Other edges of G are not shown.)

u T1 v

T2

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

L16.17

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

L16.18

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

Greedy-choice property A locally optimal choice is globally optimal.

November 9, 2005

L16.20

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: A VA u

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

November 9, 2005

L16.22

Proof of theorem

Proof. Suppose (u, v) T. Cut and paste. T: A VA u

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

November 9, 2005

L16.23

Proof of theorem

Proof. Suppose (u, v) T. Cut and paste. T: A VA 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 VA 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

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

QV 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

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

A VA 14 3 6 5 8 12 7 15 0 0 10 9

November 9, 2005

L16.27

A VA 14 3 6 5 8 12 7 15 0 0 10 9

November 9, 2005

L16.28

A VA 14 3 6 5 8 10 10 7 0 0 10 12 7 7 9 15 15 15

November 9, 2005

L16.29

A VA 14 3 6 5 8 10 10 7 0 0 10 12 7 7 9 15 15 15

November 9, 2005

L16.30

A VA 5 5 14 3 8 10 10 6 12 12 5 7 0 0 10 12 7 7 9 15 9 9 15 15

November 9, 2005

L16.31

A VA 5 5 14 3 8 10 10 6 12 12 5 7 0 0 10 12 7 7 9 15 9 9 15 15

November 9, 2005

L16.32

A VA 5 5 14 14 14 3 8 8 8 6 6 6 5 7 0 0 10 12 7 7 9 15 9 9 15 15

November 9, 2005

L16.33

A VA 5 5 14 14 14 3 8 8 8 6 6 6 5 7 0 0 10 12 7 7 9 15 9 9 15 15

November 9, 2005

L16.34

A VA 5 5 14 14 14 3 8 8 8 6 6 6 5 7 0 0 10 12 7 7 9 15 9 9 15 15

November 9, 2005

L16.35

A VA 5 5 14 3 3 3 8 8 8 6 6 6 5 7 0 0 10 12 7 7 9 15 9 9 15 15

November 9, 2005

L16.36

A VA 5 5 14 3 3 3 8 8 8 6 6 6 5 7 0 0 10 12 7 7 9 15 9 9 15 15

November 9, 2005

L16.37

A VA 5 5 14 3 3 3 8 8 8 6 6 6 5 7 0 0 10 12 7 7 9 15 9 9 15 15

November 9, 2005

L16.38

A VA 5 5 14 3 3 3 8 8 8 6 6 6 5 7 0 0 10 12 7 7 9 15 9 9 15 15

November 9, 2005

L16.39

Analysis of Prim

QV 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

L16.40

Analysis of Prim

(V) total QV 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

L16.41

Analysis of Prim

(V) total QV 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 | times

November 9, 2005

L16.42

Analysis of Prim

QV (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

L16.43

Analysis of Prim

QV (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-KEYs.

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

Analysis of Prim

QV (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-KEYs.

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

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

November 9, 2005

L16.46

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

November 9, 2005

L16.47

Time = (V)TEXTRACT-MIN + (E)TDECREASE-KEY Q array TEXTRACT-MIN TDECREASE-KEY O(V) O(1) Total O(V2)

November 9, 2005

L16.48

Time = (V)TEXTRACT-MIN + (E)TDECREASE-KEY Q array binary heap TEXTRACT-MIN TDECREASE-KEY O(V) O(lg V) O(1) O(lg V) Total O(V2) O(E lg V)

November 9, 2005

L16.49

Time = (V)TEXTRACT-MIN + (E)TDECREASE-KEY Q array binary heap TEXTRACT-MIN TDECREASE-KEY O(V) O(lg V) O(1) O(lg V) Total O(V2) O(E lg V)

November 9, 2005

L16.50

MST algorithms

Kruskals algorithm (see CLRS): Uses the disjoint-set data structure (Lecture 10). Running time = O(E lg V).

November 9, 2005

L16.51

MST algorithms

Kruskals 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

L16.52

- CSCI203 Spring 2010 Workshops Lab 7[1] With Answers!Uploaded bytechnofreak9
- Group 4 Research ReportUploaded byindula123
- Restrepo 2007 ApproximatingUploaded bypastafarianboy
- Asa 2011 GraphsUploaded byjhdmss
- rationalpolynomialstechnologyUploaded byapi-328593738
- Thematic TutorialsUploaded bymohsindalvi87
- Daniel J Gross, John T Saccoman, Charles L Suffel-Spanning Tree Results for Graphs and Multigraphs_ a Matrix-Theoretic Approach-World Scientific Publishing Company (2014)Uploaded byasakji
- Getting Started in Sport of ProgrammingUploaded bypratiknagelia
- Page Number Problem: An Evaluation of Heuristics and its Solution Using a Hybrid Evolutionary AlgorithmUploaded byijcseitjournal
- A Scatter Plot is a Type of Graph That Relates Two Paired Sets of DataUploaded bydariosteck1010
- hw3-14Uploaded byJiyang Xie
- On Playing Golf With Two BallsUploaded bySagnik Das
- 131 - CS8451, CS6402 Design and Analysis of Algorithms - Important QuestionsUploaded byk
- Hinged dissections.pdfUploaded bylucaspunky
- Syllabus Seventh SemesterUploaded bySingh Ak
- TreesUploaded byjanurag1993
- Lionel Timothy Levine- Limit Theorems for Internal Aggregation ModelsUploaded byHemAO1
- Segmentation of Ancient Arabic DocumentsUploaded byTazerwalt Lukasesane
- Tree MatchUploaded byPrima Nugraha
- BFS_pptUploaded byRohith Thurlapati
- Major ClustUploaded byWii Nguyen
- Connectivity, Coverage and Placement in Wireless Sensor11Uploaded byTan Hung Pham
- DigraphsUploaded byHarshit Mittal
- Unit 4 ds.pdfUploaded byNivitha

- Practice and Trend of DSAS in ChinaUploaded byraghavendran raghu
- Jay Brome 2018 LawsuitUploaded byKristin Lam
- McDonald v. OneWest Federal Case - McDonald Response to Motion to DismissUploaded byBMcDonald
- FM Report Ratio analysisUploaded bymaaz.bilgrami
- Range Rover P38 Owners manualUploaded bydonaldsdaks
- Controlling Over Voltages Produced by Shunt Capacitor Bank SwitchingUploaded bySanj Pandey
- Times of IndiaUploaded bymetrotor
- The Indian RailwaysUploaded byRoshani Joshi
- Ivonne Porras Ocampo Viva Voce FeedbackUploaded byMiguel Veloza
- Acpi Guideline for VendorsUploaded byNaymen Andro
- mktefficiency (1)-1Uploaded byHaseeb Waheed
- Willie E. Garrett v. E. I. Du Pont De Nemours & Company, Inc., a Corporation of the State of Delaware, 257 F.2d 687, 3rd Cir. (1958)Uploaded byScribd Government Docs
- Missile Autopilot Design and AnalysisUploaded byYonas Ghiwot
- Samsung ElectronicsUploaded byAnonymous 01LLHnZTS
- Guide to the HB Iso and PowerUploaded bycoachmark285
- HIAC-8000A LiquidParticleCounter Manual USUploaded by이치우
- Beer industryUploaded byadityabhargava05
- P1a.9 test sq 09_10Uploaded byPaul Burgess
- 1000 v- 1960mm x 992 x 35-Adani Solar - IEC Installation Manual.pdfUploaded bySunil Singh
- Rao KhanUploaded byGelica Cruz
- sg248851.pdfUploaded byAnonymous CCS5PmOUfY
- lec48Uploaded byzaid
- 43023794-TM-9-2320-209-20-2-2Uploaded byJaccup Eubank
- Eccentric Axial LoadUploaded byCong Tho Hoang
- bpj lesson 18Uploaded byapi-307093352
- Thermal Theory and Modeling of Solar CollectorsUploaded byhailemebrahtu
- MN_2017-09-19Uploaded bymoorabool
- Active Directory Tips and Tricks - SpiceworksUploaded byHasan Basri
- Type Conversion FunctionsUploaded byMohammad Ali
- List of Power Stations in PakistanUploaded byEngr Shair Hassan Barech

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.