Disjoint Set Neil Tang 02232010 CS 223 Advanced

  • Slides: 20
Download presentation
Disjoint Set Neil Tang 02/23/2010 CS 223 Advanced Data Structures and Algorithms 1

Disjoint Set Neil Tang 02/23/2010 CS 223 Advanced Data Structures and Algorithms 1

Class Overview Ø Disjoint Set and An Application Ø Basic Operations Ø Linked-list Implementation

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 CS 223 Advanced Data Structures and Algorithms 2

Disjoint Set Ø Given a set of elements, we can have a collection S

Disjoint Set Ø Given a set of elements, we can have a collection S = {S 1, 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. CS 223 Advanced Data Structures and Algorithms 3

An Application Ø Given an undirected graph G = (V, E) Ø We may

An 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 d f h e g i c b CS 223 Advanced Data Structures and Algorithms 4

Basic Operations Ø find(x): find which disjoint set x belongs to Ø Union(x, y):

Basic Operations Ø find(x): find which disjoint set x belongs to Ø Union(x, y): Union set x and set y. CS 223 Advanced Data Structures and Algorithms 5

Linked-list Implementation head f nil tail a b find(b) union(f, b) a b CS

Linked-list Implementation head f nil tail a b find(b) union(f, b) a b CS 223 Advanced Data Structures and Algorithms c nil c tail f nil tail 6

Array Implementation Ø Assume that all the elements are numbered sequentially from 0 to

Array Implementation Ø Assume that all the elements are numbered sequentially from 0 to N-1. CS 223 Advanced Data Structures and Algorithms 7

Array Implementation CS 223 Advanced Data Structures and Algorithms 8

Array Implementation CS 223 Advanced Data Structures and Algorithms 8

Array Implementation CS 223 Advanced Data Structures and Algorithms 9

Array Implementation CS 223 Advanced Data Structures and Algorithms 9

Union Operation Time complexity: O(1) CS 223 Advanced Data Structures and Algorithms 10

Union Operation Time complexity: O(1) CS 223 Advanced Data Structures and Algorithms 10

Find Operation Time complexity: O(N) CS 223 Advanced Data Structures and Algorithms 11

Find Operation Time complexity: O(N) CS 223 Advanced Data Structures and Algorithms 11

Union-by-Size Ø Make the smaller tree a subtree of the larger and break ties

Union-by-Size Ø Make the smaller tree a subtree of the larger and break ties arbitrarily. CS 223 Advanced Data Structures and Algorithms 12

Union-by-Height (Rank) Ø Make the shallow tree a subtree of the deeper and break

Union-by-Height (Rank) Ø Make the shallow tree a subtree of the deeper and break ties arbitrarily. CS 223 Advanced Data Structures and Algorithms 13

Size and Height -1 -1 -1 4 -5 4 4 6 0 1 2

Size and Height -1 -1 -1 4 -5 4 4 6 0 1 2 3 4 5 6 7 -1 -1 -1 4 -3 4 4 6 CS 223 Advanced Data Structures and Algorithms 14

Union-by-Height (Rank) Time complexity: O(1) CS 223 Advanced Data Structures and Algorithms 15

Union-by-Height (Rank) Time complexity: O(1) CS 223 Advanced Data Structures and Algorithms 15

Worst-Case Tree CS 223 Advanced Data Structures and Algorithms 16

Worst-Case Tree CS 223 Advanced Data Structures and Algorithms 16

Find(14) with Path Compression CS 223 Advanced Data Structures and Algorithms 17

Find(14) with Path Compression CS 223 Advanced Data Structures and Algorithms 17

Find with Path Compression CS 223 Advanced Data Structures and Algorithms 18

Find with Path Compression CS 223 Advanced Data Structures and Algorithms 18

Find with Path Compression Ø Fully compatible with union-by-size. Ø Not compatible with union-by-height.

Find 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. CS 223 Advanced Data Structures and Algorithms 19

Worst-Case Time Complexity Ø If both union-by-size and path compression heuristics are used, the

Worst-Case Time Complexity Ø If both union-by-size and path compression heuristics are used, the worst-case running time for any sequence of M union/find operations is O(M * (M, N)) Ø (M, N) is the inverse Ackermann function which grows even slower than log. N. CS 223 Advanced Data Structures and Algorithms 20