Docsity

Mst kruskal , Slides of Algorithms and Programming

Partial preview of the text

Download Mst kruskal and more Slides Algorithms and Programming in PDF only on Docsity!

Minimum Spanning Tree

(Kruskal’s Algorithm)

Presented By

Kawsar Ahmed

Lecturer

Department of Information and Communication Technology

Mawlana Bhashani Science and Technology University

Santosh, Tangail-1902, Bangladesh

Last Class’s Topic

  • (^) Minimum Spanning Tree (Prim’s Algorithm)
    • (^) What is spanning tree & Minimum spanning tree?
    • (^) What will happen if we add an edge in a minimum spanning tree? Thursday, November 3, Kawsar Ahmed, Lecturer, ICT, 2

Kruskal – Step 2

Thursday, November 3, Kawsar Ahmed, Lecturer, ICT, 5

Kruskal – Step 4

Thursday, November 3, Kawsar Ahmed, Lecturer, ICT, 7

  • Kruskal – Step
  • Thursday, November 3, 2016 Kawsar Ahmed, Lecturer, ICT,
    • Kruskal – Step
  • Thursday, November 3, 2016 Kawsar Ahmed, Lecturer, ICT,
    • Kruskal – Step
  • Thursday, November 3, 2016 Kawsar Ahmed, Lecturer, ICT,

Why Avoiding Cycles Matters

Up to this point, we have simply taken the edges in order of their weight. But now we will have to reject an edge since it forms a cycle when added to those already chosen. Thursday, November 3, 2016 Kawsar Ahmed, Lecturer, ICT, 10

Forms a Cycle

So we cannot take the blue edge having weight 55. Thursday, November 3, Kawsar Ahmed, Lecturer, ICT, 11

Disjoint-Set

• Keep a collection of sets S 1 , S 2 , .., Sk,

– Each Si is a set, e,g, S 1 ={v 1 , v 2 , v 8 }.

• Three operations

– Make-Set(x)-creates a new set whose only member is

x.

– Union(x, y) – unites the sets that contain x and y, say,

Sx and Sy, into a new set that is the union of the two

sets.

– Find-Set(x)-returns a pointer to the representative of

the set containing x.

Thursday, November 3, Kawsar Ahmed, Lecturer, ICT, 13

O(V)

O(E)

O(ElogE)

O(V)

Kruskal's Algorithm

  • (^) The algorithm adds the cheapest edge that

connects two trees of the forest

MST-Kruskal (G,w)

01 A  

02 for each vertex v  V[G] do

03 Make-Set(v)

04 sort the edges of E by non-decreasing weight w

05 for each edge (u,v)  E, in order by non-

decreasing weight do

06 if Find-Set(u)  Find-Set(v) then

07 A  A  {(u,v)}

08 Union(u,v)

09 return A

Overall Complexity: O(VE) Thursday, November 3, Kawsar Ahmed, Lecturer, ICT, 14