2.4 mst prim’s algorithm

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

Upload: krishver2

Post on 13-Feb-2017

211 views

Category:

Education


1 download

TRANSCRIPT

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.

Prim’s Algorithm

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

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:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                                                                             

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?

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.

GUIDED READING

ASSESSMENT