+ All Categories
Home > Documents > The Disjoint Set Class s

The Disjoint Set Class s

Date post: 07-Apr-2018
Category:
Upload: prabha-krishnan
View: 227 times
Download: 0 times
Share this document with a friend

of 21

Transcript
  • 8/6/2019 The Disjoint Set Class s

    1/21

    The Dis oint Set Class

    SAMREEN DHILLONCS W3137

    SPRING 08

  • 8/6/2019 The Disjoint Set Class s

    2/21

    Overview

    E uivalence Relations

    The Dynamic Equivalence Problem Simple algorithms for solving

    Find

    Union

    Path Compression

    Example

  • 8/6/2019 The Disjoint Set Class s

    3/21

    Equivalence Relations (~)

    Relation which satisfies the three ro erties:

    Reflexive - a R a, for all a S Symmetric - a R b if and only if b R a

    Trans t ve a R an R c mp es t at a R c

    xamp es: Electrical Connectivity

    country

  • 8/6/2019 The Disjoint Set Class s

    4/21

    The dynamic Equivalence Problem

    Given an e uivalence relation ~, decide ifa~b for

    anya andb Solving via a 2-d array of booleans

    What if relation is implicit?

    Equivalence class of an element a S is a subset ofSwhich contains all the elements that are related to a

    . Disjoint sets

  • 8/6/2019 The Disjoint Set Class s

    5/21

    disjoint set union/find algorithm

    find: returns the e uivalence class for a iven

    element. union: adds relations Merges two equivalence classes

    Dynamic : the sets change during the course of thea gor m

    serva ons: Values doesnt matter ,location matters

    ==

  • 8/6/2019 The Disjoint Set Class s

    6/21

    Data structure for Solving the problem

    find in constant worst-time?

    union in constant worst-time? Consider Each set as a tree

    Initially all elements are disjoint

    - - - - - - - -

  • 8/6/2019 The Disjoint Set Class s

    7/21

    Union operation

    union : make arent link of root of one tree to the

    root of the other tree

    Union(4,5)

  • 8/6/2019 The Disjoint Set Class s

    8/21

    Union(6,7)

    Union(4,6)

  • 8/6/2019 The Disjoint Set Class s

    9/21

    DisjSets, find and union operations

  • 8/6/2019 The Disjoint Set Class s

    10/21

    Smart Union Algorithms

    union-b -size

    Always make smaller tree subtree of the larger No extra space : instead of -1 set as -(size of tree) for root

    Union(3,4)

    - - - -

    0 1 2 3 4 5 6 7

  • 8/6/2019 The Disjoint Set Class s

    11/21

    Smart Union Algorithms

    union-b -hei ht: Make a shallow tree a subtree of a

    deep tree Update height only if two equally deep trees are joined

    Instea o -1 , - e g t or root

    -1 -1 -1 4 -3 4 4 6

    0 1 2 3 4 5 6 7

  • 8/6/2019 The Disjoint Set Class s

    12/21

    Smart find algorithm Path compression

    Inde endent of union al orithm

    Make every node on the path from x to root have itsparent changed to root.

    Note that: union-by-height => union-by-rank

  • 8/6/2019 The Disjoint Set Class s

    13/21

    Path Compression

    A ter in (14)

  • 8/6/2019 The Disjoint Set Class s

    14/21

    Path Compression

    O(MlogN) in Worst case

  • 8/6/2019 The Disjoint Set Class s

    15/21

    Example : Maze generator

    http://www.math.com/students/puzzles/mazegen/mazegen.html

  • 8/6/2019 The Disjoint Set Class s

    16/21

    Maze Generator: Al orithm

    1. Start with walls everywhere, except for the entrance

    and exit)

    2. Continually, choose a wall randomly, and knock it

    down if the cells that the wall separates are not.

    . cells are connected

    4. We have a maze.

  • 8/6/2019 The Disjoint Set Class s

    17/21

    Maze Generator: Al orithm

    1. Start with walls everywhere, except for the entrance

    and exit).

    Use the union /find data structur e

    n t a y, eac ce s n ts ow n equ va enceclass

    Initial State: All wa lls up, all cells in their ow nset.

  • 8/6/2019 The Disjoint Set Class s

    18/21

    Maze Generator: Al orithm

    2. Continually, choose a wall randomly, and knock it

    down if the cells that the wall separates are notalready connected to each other.

    At som e point in the algor ithm : several w alldown sets have mer ed. If at this oint thewall between squa res 8 and 13 is rando m lyselected, this w all is not knocked do wn ,because 8 an d 13 are a lready connected.

  • 8/6/2019 The Disjoint Set Class s

    19/21

    Maze Generator: Al orithm

    3. Repeat this process until the starting and ending

    cells are connected

    Per orm ng tw o f indoper at on s 18 an13 are in different sets. Therefore, performing u n i o n to combinethem.

    W all betwe en squar es 18 and 13 is ran dom lyselected. The wa ll is kno cked dow n, because 18

    an 13 are not a rea y connecte . T eir setsare m erged.

  • 8/6/2019 The Disjoint Set Class s

    20/21

    Maze Generator: Al orithm

    4. We have a maze

    Run ning Time: O (N log N)

    Eventually, 24 walls are knocked dow n. Allelemen ts are in the sam e set.

  • 8/6/2019 The Disjoint Set Class s

    21/21

    Questions?


Recommended