1 / 12

Union-Find

Union-Find. Application in Kruskal’s Algorithm Optimizing Union and Find Methods. Minimum Spanning Trees. Tree that connects all vertices of a graph Sum of the edge weights is a minimum. Kruskal’s Algorithm. Sort edges in order of weights Start adding edges to sub-graph:

pules
Download Presentation

Union-Find

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 1 6 3 4 4 5 5 2 8 6

  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. x Union(x,y) Illustration y

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

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

  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)

More Related