# chapter 7: greedy algorithms

Post on 20-Jan-2016

24 views

Category:

## Documents

Embed Size (px)

DESCRIPTION

Chapter 7: Greedy Algorithms. Kruskal’s, Prim’s, and Dijkstra’s Algorithms. Kruskal’s Algorithm. Solves the Minimum Spanning Tree Problem Input: List of edges in a graph n – the number of vertices Output: Prints the list of edges in the Minimum Spanning Tree. 4. 5. A. B. C. 3. 6. 4. - PowerPoint PPT Presentation

TRANSCRIPT

• Chapter 7: Greedy AlgorithmsKruskals, Prims, and Dijkstras Algorithms

• Kruskals AlgorithmSolves the Minimum Spanning Tree ProblemInput:List of edges in a graphn the number of verticesOutput:Prints the list of edges in the Minimum Spanning Tree

• ABCDEFGHIJ45734553674643565

• Kruskalskruskal(e, n) {sort(e);ABCDEFGHIJ45734553674643565

• Kruskalskruskal(e, n) {sort(e);

• kruskal(e, n) {sort(e);for (i = A to J) makeset(i)ABCDEFGHIJ

• kruskal(e, n) {...count = 0;i = 1ABCDEFGHIJCount0i1

• kruskal(e, n) {while (count < n-1) {if (findset(e[i].v) != findset(e[i].w)) {print(e[i].v + + e[i].w);count++;union(e[i].v, e[i].w);}i++;}ABCDEFGHIJcount0i1n10

• kruskal(e, n) {while (count < n-1) {if (findset(e[i].v) != findset(e[i].w)) {print(e[i].v + + e[i].w);count++;union(e[i].v, e[i].w);}i++;}ABCDHEFGIJCount1i2n10

• kruskal(e, n) {while (count < n-1) {if (findset(e[i].v) != findset(e[i].w)) {print(e[i].v + + e[i].w);count++;union(e[i].v, e[i].w);}i++;}ABCDHEFGIJCount2i3n10

• kruskal(e, n) {while (count < n-1) {if (findset(e[i].v) != findset(e[i].w)) {print(e[i].v + + e[i].w);count++;union(e[i].v, e[i].w);}i++;}ADHBCEFGIJCount3i4n10

• kruskal(e, n) {while (count < n-1) {if (findset(e[i].v) != findset(e[i].w)) {print(e[i].v + + e[i].w);count++;union(e[i].v, e[i].w);}i++;}ADHBCEFGIJCount4i5n10

• kruskal(e, n) {while (count < n-1) {if (findset(e[i].v) != findset(e[i].w)) {print(e[i].v + + e[i].w);count++;union(e[i].v, e[i].w);}i++;}ADHBCEFGIJCount5i6n10

• kruskal(e, n) {while (count < n-1) {if (findset(e[i].v) != findset(e[i].w)) {print(e[i].v + + e[i].w);count++;union(e[i].v, e[i].w);}i++;}ADHBCEFGIJCount6i7n10

• kruskal(e, n) {while (count < n-1) {if (findset(e[i].v) != findset(e[i].w)) {print(e[i].v + + e[i].w);count++;union(e[i].v, e[i].w);}i++;}ADHBCEFGJICount7i8n10

• kruskal(e, n) {while (count < n-1) {if (findset(e[i].v) != findset(e[i].w)) {print(e[i].v + + e[i].w);count++;union(e[i].v, e[i].w);}i++;}ADHBCEFGJICount8i9n10

• kruskal(e, n) {while (count < n-1) {if (findset(e[i].v) != findset(e[i].w)) {print(e[i].v + + e[i].w);count++;union(e[i].v, e[i].w);}i++;}ADHBCEFGJICount8i10n10

• kruskal(e, n) {while (count < n-1) {if (findset(e[i].v) != findset(e[i].w)) {print(e[i].v + + e[i].w);count++;union(e[i].v, e[i].w);}i++;}ADHBCEFGJICount9i11n10

• ABCDEFGHIJ45734553674643565

• ABCDEFGHIJ45734553674643565

• ABCDEFGHIJ45734553674643565ABCDEFGHIJ

• Theorem 7.2.5 pp. 280Let G be a connected, weighted graph, and let G be a sub-graph of a minimal spanning tree of G. Let C be a component of G, and let S be the set of all Edges with one vertex in C and the other not in C. If we add a minimum weight edge in S to G, the resulting graph is also contained in a minimal spanning tree of G

• Theorem 7.2.5 pp. 280Let G be a connected, weighted graph, and let G be a sub-graph of a minimal spanning tree of G. Let C be a component of G, and let S be the set of all Edges with one vertex in C and the other not in C. If we add a minimum weight edge in S to G, the resulting graph is also contained in a minimal spanning tree of GABCDEFGHIJ45734553674643565GABCDEFGHIJMinimal Spanning Tree of G

• Theorem 7.2.5 pp. 280G be a sub-graph of a minimal spanning tree of G. Let C be a component of G, and let S be the set of all Edges with one vertex in C and the other not in C. ABCDEFGHIJ45734553674643565GG Subset of Minimal Spanning Tree of G S C ADE

• Theorem 7.2.5 pp. 280If we add a minimum weight edge in S to G, the resulting graph is also contained in a minimal spanning tree of GABCDEFGHIJ45734553674643565GG Subset of Minimal Spanning Tree of G S C ADE

• Theorem 7.2.6: Kruskals Algorithm finds minimum spanning treeProof by inductionG is a sub-graph constructed by Kruskals AlgorithmG is initially empty but each step of the Algorithm increases the size of GInductive Assumption: G is contained in the MST.

• Theorem 7.2.6: Kruskals Algorithm finds minimum spanning treeProof by inductionLet (v,w) be the next edge selected by Kruskals AlgorithmKruskals algorithm finds the minimum weight edge (v,w) such that v and w are not already in GC can be any subset of the MST, so you can always construct a C such that v is in C and w is not.Therefore, by Theorem 7.2.5, when (v,w) is added to G, the resulting graph is also contained in the MST.

Recommended