Prim’s and Kruskal’s Algorithm

31
Prim’s and Kruskal’s Algorithm Presented By V.Muthumeena

Transcript of Prim’s and Kruskal’s Algorithm

Page 1: Prim’s and Kruskal’s Algorithm

Prim’s and Kruskal’s Algorithm

Presented ByV.Muthumeena

Page 2: Prim’s and Kruskal’s Algorithm

Minimum Spanning Tree

What is minimum spanning tree What is minimum cost spanning tree Applications of minimum cost spanning tree Prim’s Algorithm Kruskal’s Algorithm

Page 3: Prim’s and Kruskal’s Algorithm

What is Minimum Spanning Tree

simple, connected, undirected graph that is not edge-weighted.

Subset graph t=(V,E’) of G=(V,E).

Has | V | - 1 edges that connects all the vertices of the graph.

Thus a minimum spanning tree for G is a graph, T = (V’, E’) with the following properties: V’ = V T is connected T is acyclic.

Page 4: Prim’s and Kruskal’s Algorithm

Example

Undirected Graph G

G’s Spanning Trees

Page 5: Prim’s and Kruskal’s Algorithm

Minimum Cost Spanning Tree

simple, connected, undirected graph that is edge-weighted

The total cost of G is the sum of the weights on all its edges.

A minimum-cost spanning tree for G is a minimum spanning

tree of G that has the least total cost.

Page 6: Prim’s and Kruskal’s Algorithm

Example

Given Graph G

Some Spanning Trees of G are:

Cost: 20

Cost: 13 Cost: 12 Cost: 6Cost: 6Cost: 15

Page 7: Prim’s and Kruskal’s Algorithm

Minimum Cost Spanning Tree (contd..)

The Minimum spanning trees for the above graph:

Least Cost: 6

Page 8: Prim’s and Kruskal’s Algorithm

Applications

Minimum-cost spanning trees have many applications. Some are:

Building cable networks that join n locations with minimum cost.

Building a road network that joins n cities with minimum cost.

Obtaining an independent set of circuit equations for an electrical network.

In pattern recognition minimal spanning trees can be used to find noisy pixels.

Page 9: Prim’s and Kruskal’s Algorithm

1. All vertices are marked as not visited

2. Any vertex v you like is chosen as starting vertex and is marked as visited (define a cluster C)

3. The smallest- weighted edge e = (v,u), which connects one vertex v inside the cluster C with another vertex u outside of C, is chosen and is added to the MST.

4. The process is repeated until a spanning tree is formed

Prim’s Algorithm

Page 10: Prim’s and Kruskal’s Algorithm

Example

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Page 11: Prim’s and Kruskal’s Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Select A

Page 12: Prim’s and Kruskal’s Algorithm

E

A

2

E is of least cost

Page 13: Prim’s and Kruskal’s Algorithm

E

A

D2

1

From E, D is of Least Cost

Page 14: Prim’s and Kruskal’s Algorithm

FE

A

D2

1 2

From D, F is of Least Cost

Page 15: Prim’s and Kruskal’s Algorithm

D has equal weight 2. so B is also used

FE

A B

D2

1 2

2

Page 16: Prim’s and Kruskal’s Algorithm

From B to F, it forms a Cycle. (Discard)

FE

A B

D2

1 2

2

Cycle

Page 17: Prim’s and Kruskal’s Algorithm

The remaining vertex C is used

C

FE

A B

D

3

2

1 2

2

Page 18: Prim’s and Kruskal’s Algorithm

C

FE

A B

D

3

2

1 2

3

Minimum Spanning Tree

Page 19: Prim’s and Kruskal’s Algorithm

Running time: O ( m log n )

By implementing queue Q as a heap, Q could be initialized in O ( m ) time and a vertex could be extracted in each iteration in O ( log n ) time

Time Complexity

Page 20: Prim’s and Kruskal’s Algorithm

1. Each vertex is in its own cluster

2. Take the edge e with the smallest weight - if e connects two vertices in different clusters, then e is added to the MST and the two clusters, which are connected by e, are merged into a single cluster - if e connects two vertices, which are already in the same cluster, ignore it

3. Continue until n-1 edges were selected

Kruskal’s Algorithm

Page 21: Prim’s and Kruskal’s Algorithm

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Example

Page 22: Prim’s and Kruskal’s Algorithm

E

D

1

Least Cost of given Graph is 1

Page 23: Prim’s and Kruskal’s Algorithm

E

B

D

1

2

The next Least Cost is 2

Page 24: Prim’s and Kruskal’s Algorithm

E

A B

D2

1

2

Least Cost 2

Page 25: Prim’s and Kruskal’s Algorithm

FE

A B

D2

1 2

2

Least Cost 2

Page 26: Prim’s and Kruskal’s Algorithm

FE

A B

D2

1 2

3

2

Cycle

Next Least Cost is 3,from B to F

Page 27: Prim’s and Kruskal’s Algorithm

FE

A B

D2

1 2

2

Cycle is Discarded

Page 28: Prim’s and Kruskal’s Algorithm

CFE

A B

D

3

2

1 2

2

The Next Least Cost is 3 with vertex C

Page 29: Prim’s and Kruskal’s Algorithm

C

FE

A B

D

3

2

1 2

2

Minimum Spanning Tree

Page 30: Prim’s and Kruskal’s Algorithm

Running time: O ( m log n )

By implementing queue Q as a heap, Q could be initialized in O ( m ) time and a vertex could be extracted in each iteration in O ( log n ) time

Time Complexity

Page 31: Prim’s and Kruskal’s Algorithm

Thank you