Disjoint Set Neil Tang 02/26/2008

Post on 04-Jan-2016

32 views 1 download

description

Disjoint Set Neil Tang 02/26/2008. Class Overview. Disjoint Set and An Application Basic Operations Linked-list Implementation Array Implementation Union-by-Size and Union-by-Height(Rank) Find with Path Compression Worst-Case Time Complexity. Disjoint Set. - PowerPoint PPT Presentation

Transcript of Disjoint Set Neil Tang 02/26/2008

CS223 Advanced Data Structures and Algorithms 1

Disjoint Set Disjoint Set

Neil TangNeil Tang02/26/200802/26/2008

CS223 Advanced Data Structures and Algorithms 2

Class OverviewClass Overview

Disjoint Set and An Application

Basic Operations

Linked-list Implementation

Array Implementation

Union-by-Size and Union-by-Height(Rank)

Find with Path Compression

Worst-Case Time Complexity

CS223 Advanced Data Structures and Algorithms 3

Disjoint SetDisjoint Set

Given a set of elements, we can have a collection S = {S1, S

2, ... Sk} of disjoint dynamic (sub) sets.

Representative of a set: We choose one element of a set to identify the set, e.g., we use the root of a tree to identify a tree, or the head element of a linked list to access the linked list.

Usually, we want to find out if two elements belong to the same set.

CS223 Advanced Data Structures and Algorithms 4

An ApplicationAn Application

Given an undirected graph G = (V, E)

We may want to find all connected components, whether the graph is connected or whether two given nodes belong to the same connected component.

a

b

c

d

ge

f h

i

CS223 Advanced Data Structures and Algorithms 5

Basic OperationsBasic Operations

find(x): find which disjoint set x belongs to

Union(x,y): Union set x and set y.

CS223 Advanced Data Structures and Algorithms 6

Linked-list ImplementationLinked-list Implementation

union(f, b)

fnil

head

taila b c

nilfind(b) tail

a b c fnil tail

CS223 Advanced Data Structures and Algorithms 7

Array ImplementationArray Implementation

Assume that all the elements are numbered sequentially from 0 to N-1.

CS223 Advanced Data Structures and Algorithms 8

Array ImplementationArray Implementation

CS223 Advanced Data Structures and Algorithms 9

Array ImplementationArray Implementation

CS223 Advanced Data Structures and Algorithms 10

Union OperationUnion Operation

Time complexity: O(1)

CS223 Advanced Data Structures and Algorithms 11

Find OperationFind Operation

Time complexity: O(N)

CS223 Advanced Data Structures and Algorithms 12

Union-by-SizeUnion-by-Size

Make the smaller tree a subtree of the larger and break ties arbitrarily.

CS223 Advanced Data Structures and Algorithms 13

Union-by-Height (Rank)Union-by-Height (Rank)

Make the shallow tree a subtree of the deeper and break ties arbitrarily.

CS223 Advanced Data Structures and Algorithms 14

Size and HeightSize and Height

-1 -1 -1 4 -5 4 4 6

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

0 1 2 3 4 5 6 7

CS223 Advanced Data Structures and Algorithms 15

Union-by-Height (Rank)Union-by-Height (Rank)

Time complexity: O(logN)

CS223 Advanced Data Structures and Algorithms 16

Worst-Case TreeWorst-Case Tree

CS223 Advanced Data Structures and Algorithms 17

Find with Path CompressionFind with Path Compression

CS223 Advanced Data Structures and Algorithms 18

Find with Path CompressionFind with Path Compression

CS223 Advanced Data Structures and Algorithms 19

Find with Path CompressionFind with Path Compression

Fully compatible with union-by-size.

Not compatible with union-by-height.

Union-by-size is usually as efficient as union-by-height.

CS223 Advanced Data Structures and Algorithms 20

Worst-Case Time ComplexityWorst-Case Time Complexity

If both union-by-rank and path compression heuristics are used, the worst-case running time for any sequence of M union/find operations is O(M * (M,N)), where (M, N) is the inverse Ackermann function which grows even slower than logN.

For any practical purposes, (M, N) < 4.