• date post

13-Sep-2015
• Category

## Documents

• view

225

0

TAGS:

Embed Size (px)

description

DS and Algo

### Transcript of 19 Prim Kru Skal

• Minimum Spanning TreesText Read Weiss, 9.5Prims Algorithm Weiss 9.5.1 Similar to Dijkstras Algorithm Kruskals AlgorithmWeiss 9.5.2 Focuses on edges, rather than nodes

• DefinitionA Minimum Spanning Tree (MST) is a subgraph of an undirected graph such that the subgraph spans (includes) all nodes, is connected, is acyclic, and has minimum total edge weight

• Algorithm CharacteristicsBoth Prims and Kruskals Algorithms work with undirected graphsBoth work with weighted and unweighted graphs but are more interesting when edges are weightedBoth are greedy algorithms that produce optimal solutions

• Prims Algorithm

Similar to Dijkstras Algorithm except that dv records edge weights, not path lengths

• Walk-ThroughInitialize array425AHBFEDCG7210183437893102

KdvpvAFBFCFDFEFFFGFHF

KdvpvABC DT0EFGH

• 425AHBFEDCG721018343789310Update distances of adjacent, unselected nodes2

KdvpvABC 3DDT0E25DF18DG2DH

• 425AHBFEDCG721018343789310Select node with minimum distance2

KdvpvABC 3DDT0E25DF18DGT2DH

• 425AHBFEDCG721018343789310Update distances of adjacent, unselected nodes2

KdvpvABC 3DDT0E7GF18DGT2DH3G

• 425AHBFEDCG721018343789310Select node with minimum distance2

KdvpvABCT 3DDT0E7GF18DGT2DH3G

• 425AHBFEDCG721018343789310Update distances of adjacent, unselected nodes2

KdvpvAB4CCT 3DDT0E7GF3CGT2DH3G

• 425AHBFEDCG721018343789310Select node with minimum distance2

KdvpvAB4CCT 3DDT0E7GFT3CGT2DH3G

• 425AHBFEDCG721018343789310Update distances of adjacent, unselected nodes2

KdvpvA10FB4CCT 3DDT0E2FFT3CGT2DH3G

• 425AHBFEDCG721018343789310Select node with minimum distance2

KdvpvA10FB4CCT 3DDT0ET2FFT3CGT2DH3G

• 425AHBFEDCG721018343789310Update distances of adjacent, unselected nodes2Table entries unchanged

KdvpvA10FB4CCT 3DDT0ET2FFT3CGT2DH3G

• 425AHBFEDCG721018343789310Select node with minimum distance2

KdvpvA10FB4CCT 3DDT0ET2FFT3CGT2DHT3G

• 425AHBFEDCG721018343789310Update distances of adjacent, unselected nodes2

KdvpvA4HB4CCT 3DDT0ET2FFT3CGT2DHT3G

• 425AHBFEDCG721018343789310Select node with minimum distance2

KdvpvAT4HB4CCT 3DDT0ET2FFT3CGT2DHT3G

• 425AHBFEDCG721018343789310Update distances of adjacent, unselected nodes2Table entries unchanged

KdvpvAT4HB4CCT 3DDT0ET2FFT3CGT2DHT3G

• 425AHBFEDCG721018343789310Select node with minimum distance2

KdvpvAT4HBT4CCT 3DDT0ET2FFT3CGT2DHT3G

• 4AHBFEDCG23433Cost of Minimum Spanning Tree = dv = 212Done

KdvpvAT4HBT4CCT 3DDT0ET2FFT3CGT2DHT3G

• Kruskals Algorithm

Work with edges, rather than nodesTwo steps:Sort edges by increasing edge weightSelect the first |V| 1 edges that do not generate a cycle

• Walk-ThroughConsider an undirected, weight graph51AHBFEDCG3246343484310

• Sort the edges by increasing edge weight51AHBFEDCG3246343484310

edgedv(D,E)1 (D,G)2 (E,G)3 (C,D)3 (G,H)3 (C,F)3 (B,C)4

edgedv(B,E)4 (B,F)4 (B,H)4 (A,H)5 (D,F)6 (A,B)8 (A,F)10

• Select first |V|1 edges which do not generate a cycle51AHBFEDCG3246343484310

edgedv(D,E)1(D,G)2(E,G)3(C,D)3(G,H)3 (C,F)3(B,C)4

edgedv(B,E)4(B,F)4(B,H)4(A,H)5(D,F)6(A,B)8(A,F)10

• Select first |V|1 edges which do not generate a cycle51AHBFEDCG3246343484310

edgedv(D,E)1(D,G)2(E,G)3(C,D)3(G,H)3 (C,F)3(B,C)4

edgedv(B,E)4(B,F)4(B,H)4(A,H)5(D,F)6(A,B)8(A,F)10

• Select first |V|1 edges which do not generate a cycle51AHBFEDCG3246343484310Accepting edge (E,G) would create a cycle

edgedv(D,E)1(D,G)2(E,G)3(C,D)3(G,H)3 (C,F)3(B,C)4

edgedv(B,E)4(B,F)4(B,H)4(A,H)5(D,F)6(A,B)8(A,F)10

• Select first |V|1 edges which do not generate a cycle51AHBFEDCG3246343484310

edgedv(D,E)1(D,G)2(E,G)3(C,D)3(G,H)3 (C,F)3(B,C)4

edgedv(B,E)4(B,F)4(B,H)4(A,H)5(D,F)6(A,B)8(A,F)10

• Select first |V|1 edges which do not generate a cycle51AHBFEDCG3246343484310

edgedv(D,E)1(D,G)2(E,G)3(C,D)3(G,H)3 (C,F)3(B,C)4

edgedv(B,E)4(B,F)4(B,H)4(A,H)5(D,F)6(A,B)8(A,F)10

• Select first |V|1 edges which do not generate a cycle51AHBFEDCG3246343484310

edgedv(D,E)1(D,G)2(E,G)3(C,D)3(G,H)3 (C,F)3(B,C)4

edgedv(B,E)4(B,F)4(B,H)4(A,H)5(D,F)6(A,B)8(A,F)10

• Select first |V|1 edges which do not generate a cycle51AHBFEDCG3246343484310

edgedv(D,E)1(D,G)2(E,G)3(C,D)3(G,H)3 (C,F)3(B,C)4

edgedv(B,E)4(B,F)4(B,H)4(A,H)5(D,F)6(A,B)8(A,F)10

• Select first |V|1 edges which do not generate a cycle51AHBFEDCG3246343484310

edgedv(D,E)1(D,G)2(E,G)3(C,D)3(G,H)3 (C,F)3(B,C)4

edgedv(B,E)4(B,F)4(B,H)4(A,H)5(D,F)6(A,B)8(A,F)10

• Select first |V|1 edges which do not generate a cycle51AHBFEDCG3246343484310

edgedv(D,E)1(D,G)2(E,G)3(C,D)3(G,H)3 (C,F)3(B,C)4

edgedv(B,E)4(B,F)4(B,H)4(A,H)5(D,F)6(A,B)8(A,F)10

• Select first |V|1 edges which do not generate a cycle51AHBFEDCG3246343484310

edgedv(D,E)1(D,G)2(E,G)3(C,D)3(G,H)3 (C,F)3(B,C)4

edgedv(B,E)4(B,F)4(B,H)4(A,H)5(D,F)6(A,B)8(A,F)10

• Select first |V|1 edges which do not generate a cycle51AHBFEDCG3246343484310

edgedv(D,E)1(D,G)2(E,G)3(C,D)3(G,H)3 (C,F)3(B,C)4

edgedv(B,E)4(B,F)4(B,H)4(A,H)5(D,F)6(A,B)8(A,F)10

• Select first |V|1 edges which do not generate a cycle51AHBFEDCG2333DoneTotal Cost = dv = 21

4}not considered

edgedv(D,E)1(D,G)2(E,G)3(C,D)3(G,H)3 (C,F)3(B,C)4

edgedv(B,E)4(B,F)4(B,H)4(A,H)5(D,F)6(A,B)8(A,F)10

• Detecting CyclesUse Disjoint Sets (Chapter 8)