Presentation is loading. Please wait.

Presentation is loading. Please wait.

Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.

Similar presentations


Presentation on theme: "Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods."— Presentation transcript:

1 Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods

2 Minimum Spanning Trees  Tree that connects all vertices of a graph  Sum of the edge weights is a minimum

3 Kruskal’s Algorithm  Sort edges in order of weights  Start adding edges to sub-graph:  Start from lowest weight  Skip edge if it makes the sub-graph cyclic

4 Kruskal’s Algorithm 3 1 6 6 55 8 44 2

5 Union-Find & Kruskal’s Algorithm  Vertices grouped in sets  Can only add edges linking vertices not in same set

6 Non-Optimal Solution  Array of labels  Change labels for a union  O (n) for each union  O (n^2)

7 Union-Find Methods  makeSet ( x )  union ( x, y )  find ( x )

8 Optimizing Union(x,y)  Sets of vertices stored in trees  Root of tree is label of set  union(x,y) by joining two trees  Root of smaller tree points to root of larger tree

9 Union(x,y) Illustration x y

10 Path Compression  Nodes from ‘x’ to root have same label  Change these parent-pointers to the root

11 Path Compression Illustration g b a d e f c g b a d e f c

12 Time Efficiency  Sorting is O(e log e)  Find maximum is O(log n)  Path compression makes future finds O(1)  Calling find many times gives O(1) average  Union is 2 finds and a pointer change: O(1)  Kruskal becomes O(e log e)


Download ppt "Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods."

Similar presentations


Ads by Google