Top Banner
Spanning Trees and Minimum Spaning Trees spanning tree: (for connected, undirected graph) minimal set of edges that connect all vertices (no cycles) Minimum spanning tree: (for connected, undirected and weighted graph) minimal set of edges that connect all vertices such that the sum of weights is minimum.
29

2.4 mst prim’s algorithm

Feb 13, 2017

Download

Education

Krish_ver2
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 2.4 mst  prim’s algorithm

Spanning Trees and Minimum Spaning Treesspanning tree: (for connected, undirected graph)

minimal set of edges that connect all vertices (no cycles)

Minimum spanning tree: (for connected, undirected and weighted graph)

minimal set of edges that connect all vertices such that the sum of weights is minimum.

Page 2: 2.4 mst  prim’s algorithm

Prim’s Algorithm

Similar to Dijkstra’s Algorithm except that dv records edge weights, not path lengths

Page 3: 2.4 mst  prim’s algorithm

Prim's Algorithm

MST=NULL;Select an edge of min weight and add it to MST

Iteration:repeat till n-1 edges are added to MST1.select an edge (v1,v2) such that v1 is in MST and v2 is not in MST2.add it to MST

Initialization:

Page 4: 2.4 mst  prim’s algorithm

Prims AlgorithmInput:

A connected weighted graph G = {V, E}Initialization:

VMST = EMST = nullSelect an aribitrary vertex, x, from Vadd x to VMST

Iteration:for i = 1 to |V|-1

select an edge v1,v2 with minimum weight such that v1 V∈ MST and v2 V \ V∈ MST

Add v1 to VMST

Add (v1,v2) to EMST

return EMST

Page 5: 2.4 mst  prim’s algorithm

Walk-ThroughInitialize array

K dv pvA F

B F

C F

D F

E F

F F

G F

H F

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

2

Page 6: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Start with any node, say D

K dv pvA

B

C

D T 0

E

F

G

H

2

Page 7: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Update distances of adjacent, unselected nodes

K dv pvA

B

C 3 D

D T 0

E 25 D

F 18 D

G 2 D

H

2

Page 8: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Select node with minimum distance

K dv pvA

B

C 3 D

D T 0

E 25 D

F 18 D

G T 2 D

H

2

Page 9: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Update distances of adjacent, unselected nodes

K dv pvA

B

C 3 D

D T 0

E 7 G

F 18 D

G T 2 D

H 3 G

2

Page 10: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Select node with minimum distance

K dv pvA

B

C T 3 D

D T 0

E 7 G

F 18 D

G T 2 D

H 3 G

2

Page 11: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Update distances of adjacent, unselected nodes

K dv pvA

B 4 C

C T 3 D

D T 0

E 7 G

F 3 C

G T 2 D

H 3 G

2

Page 12: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Select node with minimum distance

K dv pvA

B 4 C

C T 3 D

D T 0

E 7 G

F T 3 C

G T 2 D

H 3 G

2

Page 13: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Update distances of adjacent, unselected nodes

K dv pvA 10 F

B 4 C

C T 3 D

D T 0

E 2 F

F T 3 C

G T 2 D

H 3 G

2

Page 14: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Select node with minimum distance

K dv pvA 10 F

B 4 C

C T 3 D

D T 0

E T 2 F

F T 3 C

G T 2 D

H 3 G

2

Page 15: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Update distances of adjacent, unselected nodes

K dv pvA 10 F

B 4 C

C T 3 D

D T 0

E T 2 F

F T 3 C

G T 2 D

H 3 G

2

Table entries unchanged

Page 16: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Select node with minimum distance

K dv pvA 10 F

B 4 C

C T 3 D

D T 0

E T 2 F

F T 3 C

G T 2 D

H T 3 G

2

Page 17: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Update distances of adjacent, unselected nodes

K dv pvA 4 H

B 4 C

C T 3 D

D T 0

E T 2 F

F T 3 C

G T 2 D

H T 3 G

2

Page 18: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Select node with minimum distance

K dv pvA T 4 H

B 4 C

C T 3 D

D T 0

E T 2 F

F T 3 C

G T 2 D

H T 3 G

2

Page 19: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Update distances of adjacent, unselected nodes

K dv pvA T 4 H

B 4 C

C T 3 D

D T 0

E T 2 F

F T 3 C

G T 2 D

H T 3 G

2

Table entries unchanged

Page 20: 2.4 mst  prim’s algorithm

4

25

A

HB

F

E

D

C

G 7

2

10

18

34

3

78

9

3

10

Select node with minimum distance

K dv pvA T 4 H

B T 4 C

C T 3 D

D T 0

E T 2 F

F T 3 C

G T 2 D

H T 3 G

2

Page 21: 2.4 mst  prim’s algorithm

4

A

HB

F

E

D

C

G2

34

3

3

Cost of Minimum Spanning Tree = dv = 21

K dv pvA T 4 H

B T 4 C

C T 3 D

D T 0

E T 2 F

F T 3 C

G T 2 D

H T 3 G

2

Done

Page 22: 2.4 mst  prim’s algorithm

                                                                                             

How many squares can you create in this figure by connecting any 4 dots (the corners of a square must lie upon a grid dot?

TRIANGLES: 

How many triangles are located in the image below?

Page 23: 2.4 mst  prim’s algorithm

There are 11 squares total; 5 small, 4 medium, and 2 large.

27 triangles.  There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell triangles, and 1 sixteen-cell triangle.

Page 24: 2.4 mst  prim’s algorithm

GUIDED READING

Page 25: 2.4 mst  prim’s algorithm
Page 26: 2.4 mst  prim’s algorithm

ASSESSMENT

Page 27: 2.4 mst  prim’s algorithm
Page 28: 2.4 mst  prim’s algorithm
Page 29: 2.4 mst  prim’s algorithm