20
CS223 Advanced Data Structures and Algorithms 1 Disjoint Set Disjoint Set Neil Tang Neil Tang 02/26/2008 02/26/2008

Disjoint Set Neil Tang 02/26/2008

  • Upload
    tim

  • View
    33

  • Download
    1

Embed Size (px)

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

Citation preview

Page 1: 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

Page 2: Disjoint Set   Neil Tang 02/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

Page 3: Disjoint Set   Neil Tang 02/26/2008

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.

Page 4: Disjoint Set   Neil Tang 02/26/2008

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

Page 5: Disjoint Set   Neil Tang 02/26/2008

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.

Page 6: Disjoint Set   Neil Tang 02/26/2008

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

Page 7: Disjoint Set   Neil Tang 02/26/2008

CS223 Advanced Data Structures and Algorithms 7

Array ImplementationArray Implementation

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

Page 8: Disjoint Set   Neil Tang 02/26/2008

CS223 Advanced Data Structures and Algorithms 8

Array ImplementationArray Implementation

Page 9: Disjoint Set   Neil Tang 02/26/2008

CS223 Advanced Data Structures and Algorithms 9

Array ImplementationArray Implementation

Page 10: Disjoint Set   Neil Tang 02/26/2008

CS223 Advanced Data Structures and Algorithms 10

Union OperationUnion Operation

Time complexity: O(1)

Page 11: Disjoint Set   Neil Tang 02/26/2008

CS223 Advanced Data Structures and Algorithms 11

Find OperationFind Operation

Time complexity: O(N)

Page 12: Disjoint Set   Neil Tang 02/26/2008

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.

Page 13: Disjoint Set   Neil Tang 02/26/2008

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.

Page 14: Disjoint Set   Neil Tang 02/26/2008

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

Page 15: Disjoint Set   Neil Tang 02/26/2008

CS223 Advanced Data Structures and Algorithms 15

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

Time complexity: O(logN)

Page 16: Disjoint Set   Neil Tang 02/26/2008

CS223 Advanced Data Structures and Algorithms 16

Worst-Case TreeWorst-Case Tree

Page 17: Disjoint Set   Neil Tang 02/26/2008

CS223 Advanced Data Structures and Algorithms 17

Find with Path CompressionFind with Path Compression

Page 18: Disjoint Set   Neil Tang 02/26/2008

CS223 Advanced Data Structures and Algorithms 18

Find with Path CompressionFind with Path Compression

Page 19: Disjoint Set   Neil Tang 02/26/2008

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.

Page 20: Disjoint Set   Neil Tang 02/26/2008

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.