a presentation on prim's and kruskal's algorithm

41

Click here to load reader

Upload: gaurav-kolekar

Post on 21-Feb-2017

372 views

Category:

Engineering


12 download

TRANSCRIPT

Page 1: A presentation on prim's and kruskal's algorithm

A PRESENTATION ON PRIM’S AND KRUSKAL’S ALGORITHM

By:Gaurav Vivek Kolekar andLionel Sequeira

Page 2: A presentation on prim's and kruskal's algorithm

GRAPHS AND IT’S EXAMPLES

Page 3: A presentation on prim's and kruskal's algorithm

MINIMUM COST SPANNING TREE

A 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

Page 4: A presentation on prim's and kruskal's algorithm

ALGORITHM CHARACTERISTICS• Both Prim’s and Kruskal’s Algorithms work with

undirected graphs• Both work with weighted and unweighted graphs• Both are greedy algorithms that produce optimal

solutions

Page 5: A presentation on prim's and kruskal's algorithm

Kruskal’s algorithm

1. Select the shortest edge in a network

2. Select the next shortest edge which does not create a cycle

3. Repeat step 2 until all vertices have been connected

Prim’s algorithm

1. Select any vertex

2. Select the shortest edge connected to that vertex

3. Select the shortest edge connected to any vertex already connected

4. Repeat step 3 until all vertices have been connected

Page 6: A presentation on prim's and kruskal's algorithm

Work with edges, instead of nodes of a graphTwo steps:

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

generate a cycle

KRUSKAL’S ALOGORITHM

Page 7: A presentation on prim's and kruskal's algorithm

Consider an undirected, weight graph

5

1

A

HB

F

E

D

C

G 3

24

63

4

3

48

4

3

10

WALK-THROUGH

Page 8: A presentation on prim's and kruskal's algorithm

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

24

63

4

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 9: A presentation on prim's and kruskal's algorithm

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

24

63

4

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: A presentation on prim's and kruskal's algorithm

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

24

63

4

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: A presentation on prim's and kruskal's algorithm

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

24

63

4

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 12: A presentation on prim's and kruskal's algorithm

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

24

63

4

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: A presentation on prim's and kruskal's algorithm

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

24

63

4

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: A presentation on prim's and kruskal's algorithm

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

24

63

4

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: A presentation on prim's and kruskal's algorithm

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

24

63

4

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: A presentation on prim's and kruskal's algorithm

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

24

63

4

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: A presentation on prim's and kruskal's algorithm

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

24

63

4

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 18: A presentation on prim's and kruskal's algorithm

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

24

63

4

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 19: A presentation on prim's and kruskal's algorithm

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

24

63

4

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 20: A presentation on prim's and kruskal's algorithm

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

DoneTotal Cost = dv = 21

4

}not considered

Page 21: A presentation on prim's and kruskal's algorithm

PRIM’S ALGORITHM

Page 22: A presentation on prim's and kruskal'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

210

183

4

3

78

9

3

10

2

Page 23: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 24: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 25: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 26: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 27: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 28: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 29: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 30: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 31: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 32: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 33: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 34: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 35: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 36: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 37: A presentation on prim's and kruskal's algorithm

4

25

A

HB

F

E

D

C

G 7

210

183

4

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 38: A presentation on prim's and kruskal'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 39: A presentation on prim's and kruskal's algorithm

DIFFERENCE BETWEEN PRIM’S AND KRUSKAL’S ALGORITHM• The difference between Prim’s algorithm and Kruskal’s

algorithm is that the set of selected edges forms a tree at all times when using Prim’s algorithm while a forest is formed when using Kruskal’s algorithm.

• In Prim’s algorithm, a least-cost edge (u, v) is added to T such that T∪ {(u, v)} is also a tree. This repeats until T contains n-1 edges.

Page 40: A presentation on prim's and kruskal's algorithm

•Both algorithms will always give solutions with the same length.

•They will usually select edges in a different order.

•Occasionally they will use different edges – this may happen when you have to choose between edges with the same length.

SOME POINTS TO NOTE

Page 41: A presentation on prim's and kruskal's algorithm

READINGS