Supplement - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Supplement

Description:

union1(a, b) i = min(a, b) j = max(a, b) for (k = 0; k N; k ) if (set[k] == j) set[k] = i ... union3(a, b) // same union function in Approach 3. Chi-Cheng ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 19
Provided by: chichen
Category:
Tags: supplement

less

Transcript and Presenter's Notes

Title: Supplement


1
Supplement
CS 440 Theory of Algorithms
  • Disjoint Set
  • Structures

2
Disjoint Set Structures
  • N objects numbered 0, 1, , N 1, grouped into
    disjoint sets
  • One member of a set chosen to be the label of the
    set
  • Example If we choose the smallest number as the
    label, set 3, 5, 8, 10 is referred to as set 3
  • Initially, N objects are in N different sets
  • Each set contains one member

3
Disjoint Set Structures
  • Two operations find() and union()
  • find(x)
  • Returns the label of the set that contains object
    x
  • union(a, b)
  • Unions the two sets labeled a and b
  • Problem
  • Find efficient data structure and algorithms for
    disjoint set structures

4
Disjoint Set Structures Approach 1
  • Use an integer array set of size N
  • The label of a set is the smallest number in the
    set
  • setx is the label of x, i.e., x is in set
    setx
  • Initially, setx x
  • find1(x)
  • return setx
  • union1(a, b)
  • i min(a, b)
  • j max(a, b)
  • for (k 0 k lt N k)
  • if (setk j)
  • setk i

5
Disjoint Set Structures Approach 1
  • Example
  • 3 sets 0, 4, 1, 3, 6, 9, and 2, 5, 7, 8
  • set 0 1 2 1 0 2 1 2 2 1
  • After union1(2, 0)
  • 2 sets 0, 2, 4, 5, 7, 8 and 1, 3, 6, 9
  • set 0 1 0 1 0 0 1 0 0 1

6
Analysis of Approach 1
  • Suppose a sequence of n find and (N 1) union
    operations are executed
  • find1() O(1)
  • union1() O(N)
  • n find1() O(n)
  • (N 1) union1 O(N2)
  • Total O(n N2)
  • If N and n are of the same order, the time the
    sequence of operations takes is O(n2)

7
Disjoint Set Structures Approach 2
  • Use an integer array set of size N
  • Each set is a rooted tree, the label of a set is
    the smallest number in the set
  • Each non-root node in a tree points to its
    parent, and the root points to itself
  • If setx x, x is the label and the root of a
    tree
  • If setx y and x ? y, then y is xs parent in
    a tree
  • Initially, setx x
  • find2(x)
  • r x
  • while (setr ? r)
  • r setr
  • return r
  • union2(a, b)
  • if (a lt b)
  • setb a
  • else
  • seta b

8
Disjoint Set Structures Approach 2
  • Example
  • 3 sets
  • set 0 1 2 1 0 2 3 2 2 3
  • After union2(0, 1)
  • 2 sets 0, 2, 4, 5, 7, 8 and 1, 3, 6, 9
  • set 0 0 2 1 0 2 3 2 2 3

0
1
2
4
3
7
5
8
6
9
9
Analysis of Approach 2
  • Suppose a sequence of n find and (N 1) union
    operations are executed
  • find2() O(N), worst case
  • union2() O(1)
  • n find2() O(nN)
  • (N 1) union2 O(N)
  • Total O(nN)
  • If N and n are of the same order, the time the
    sequence of operations takes is O(n2)
  • Problem a union operation might increase the
    height of a tree

10
Disjoint Set Structures Approach 3
  • Solution to problem of Approach 2
  • Make the shorter tree a subtree of the other when
    two trees merge
  • Additional array height, where
  • heightx is the height of node x in the tree
  • If x is the root (i.e., label) of a tree,
    heightx is the height of the tree
  • Initially, heightx 1 for all x
  • find2(x) // same find function in Approach 2
  • union3(a, b)
  • if (heighta heightb)
  • heighta 1
  • setb a
  • else if (heighta gt heightb)
  • setb a
  • else
  • seta b

11
Disjoint Set Structures Approach 3
  • Example
  • 3 sets
  • set 0 1 2 1 0 2 3 2 2 3
  • After union3(0, 1)
  • 2 sets 0, 2, 4, 5, 7, 8 and 1, 3, 6, 9
  • set 1 1 2 1 0 2 3 2 2 3

0
1
2
4
3
7
5
8
6
9
12
Analysis of Approach 3
  • Suppose a sequence of n find and (N 1) union
    operations are executed
  • We can show that the height of a tree of k nodes
    is O(log k)
  • find2() O(log N), worst case
  • union3() O(1)
  • n find2() O(n log N)
  • (N 1) union3 O(N)
  • Total O(N n log N)
  • If N and n are of the same order, the time the
    sequence of operations takes is O(n log n)
  • Improvement?

13
Disjoint Set Structures Approach 4
  • Improvement to Approach 3
  • Path compression
  • When finding the root for x, make root become the
    parent of all the nodes on the path from the root
    to x
  • find3(x)
  • r x
  • while (setr ? r)
  • r setr
  • i x
  • while (i ! r)
  • j seti // j is is parent
  • seti r // make root r the parent of i
  • i j // continue all the way up
  • return r

14
Disjoint Set Structures Approach 4
  • find3rec(x)
  • // recursive version of find3, its really
    neat if (setx ? x)
  • setx find3rec(setx)
  • return setx
  • union3(a, b)
  • // same union function in Approach 3

15
Disjoint Set Structures Approach 4
  • Example

4
4
? Before find3(8) After ?
9
2
9
2
8
3
0
6
3
0
10
7
5
6
7
5
8
10
16
Analysis of Approach 4
  • Suppose a sequence of n find and (N 1) union
    operations are executed
  • Let c n (N 1)
  • The time the sequence of operations takes is O(c
    log N), which is very close to O(c)
  • The function log N the number of 2s in the
    equation 22
  • N
  • 22

. . .
17
Application Kruskals MST Algorithm
  • Repeatedly select the edge w/ min weight that is
    not yet processed and wont form a cycle until
    MST found
  • Sort the edges
  • Initialize disjoint sets
  • Select the edge (u, v) w/ min weight not
    processed
  • While edges in the solution ? E - 1
  • a find(u) b find(v)
  • If (a ! b) union(a, b) add (u, v) to the
    solution
  • Analysis Given a graph (V, E) let e E and n
    V
  • O(e) to make a MIN heap of the edges
  • O(n) to initialize the n disjoint sets
  • O(log e) to process each of the edges (i.e.,
    perform delete from heap and union/find) which is
    equivalent to O(log n)
  • ? O(e log n) to process all edges in the worst
    case
  • ? Kruskals algorithm takes a time in O(e log n)

18
Kruskals Minimum Spanning Tree Algorithm -
Example
4
G
2
5
3
1
3
6
6
2
T, initially
4
Write a Comment
User Comments (0)
About PowerShow.com