2.4 mst kruskal’s

21
Kruskal’s Algorithm Work with edges, rather than nodes Two steps: – Sort edges by increasing edge weight – Select the first |V| – 1 edges that do not generate a cycle

Transcript of 2.4 mst kruskal’s

Kruskal’s Algorithm

Work with edges, rather than nodesTwo steps:

– Sort edges by increasing edge weight– Select the first |V| – 1 edges that do not

generate a cycle

Kruskal's Algorithm

Initialization:

MST=NULL;

Select an edge of min weight and add it to MST

Iteration:

repeat till n-1 edges are added to MST

1.select a minimum edge

2.add it to MST if it does not form a cycle with existing selections else discard

Kruskals Algorithm

Input:

A connected weighted graph G = {V, E}

Initialization:

VMST = EMST = null

Iteration:

for i = 1 to |V|

select an edge v1,v2 with minimum weight such that it does not form a cycle with existing selected edges.

Add (v1,v2) to EMST

return EMST

Walk-ThroughConsider an undirected, weight graph

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10

Sort the edges by increasing edge weight

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10 edge dv

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

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10 edge dv

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

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10 edge dv

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

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10 edge dv

(B,E) 4

(B,F) 4

(B,H) 4

(A,H) 5

(D,F) 6

(A,B) 8

(A,F) 10

Accepting edge (E,G) would create a cycle

Select first |V|–1 edges which do not generate a cycle

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10 edge dv

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

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10 edge dv

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

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10 edge dv

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

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10 edge dv

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

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10 edge dv

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

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10 edge dv

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

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10 edge dv

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

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G 3

2

4

6

34

3

48

4

3

10 edge dv

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

edge dv

(D,E) 1

(D,G) 2

(E,G) 3

(C,D) 3

(G,H) 3

(C,F) 3

(B,C) 4

5

1

A

HB

F

E

D

C

G2

3

3

3

edge dv

(B,E) 4

(B,F) 4

(B,H) 4

(A,H) 5

(D,F) 6

(A,B) 8

(A,F) 10

Done

Total Cost = dv = 21

4

}not considered

                                                                                             

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