Top Banner
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
21
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  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

Page 2: 2.4 mst  kruskal’s

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

Page 3: 2.4 mst  kruskal’s

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

Page 4: 2.4 mst  kruskal’s

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

Page 5: 2.4 mst  kruskal’s

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

Page 6: 2.4 mst  kruskal’s

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

Page 7: 2.4 mst  kruskal’s

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

Page 8: 2.4 mst  kruskal’s

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

Page 9: 2.4 mst  kruskal’s

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

Page 10: 2.4 mst  kruskal’s

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

Page 11: 2.4 mst  kruskal’s

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

Page 12: 2.4 mst  kruskal’s

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

Page 13: 2.4 mst  kruskal’s

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

Page 14: 2.4 mst  kruskal’s

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

Page 15: 2.4 mst  kruskal’s

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

Page 16: 2.4 mst  kruskal’s

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

Page 17: 2.4 mst  kruskal’s

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

Page 18: 2.4 mst  kruskal’s

                                                                                             

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 19: 2.4 mst  kruskal’s

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 20: 2.4 mst  kruskal’s

GUIDED READING

Page 21: 2.4 mst  kruskal’s