Notes on Weiss, Chapter 8

EECS 311: Chapter 8 Notes
Chris Riesbeck
EECS Northwestern
 Unless otherwise noted, all tables,
graphs and code from Mark Allen
Weiss' Data Structures and Algorithm
Analysis in C++, 3rd ed, copyright ©
2006 by Pearson Education, Inc.
2
Maze Maker Problem
3
Maze Maker Algorithm
 Represent maze with 2D array of cells
 Randomly knock down walls until all cells
connected by some path.
4
Maze Maker Algorithm
 To avoid short
paths, don't knock
down wall if 2 cells
already connected.
 E.g., leave wall
between 8 and 13
 Problem: how to
quickly tell if two
cells already
connected
5
Relations
 Given a set of objects a, b, c, …
 A relation R is a binary predicate between
objects
 electricallyConnected(component1, component2)
 semanticallyRelated(document1, document2)
 i.e., shares many terms
 ancestorOf(person1, person2)
 In mathematics, a relation is a set of pairs
 In computation, representing a relation as
all pairs is often infeasible
6
Equivalence Relations
 R is an equivalence relation if R is
 Reflexive: R(a, a) is always true
 Symmetric: R(a, b)  R(b, a)
 Transitive: R(a, b) and R(b, c)  R(a, c)
 Which of these are equivalence
relations?
Yes
 electricallyConnected
 semanticallyRelated
 ancestorOf
Not transitive
Not symmetric
7
Equivalence Class
 The equivalence class for an object a
and relationship R is the set { x |
R(a, x) }
 Every object belongs to one and only
one equivalence class
 Equivalence classes are disjoint
 E.g.,
 the sets of all connected components
8
Union/Find
 find(x): return the equivalence class for x
for relation R
 find(x) = find(y) if and only if R(x, y)
 union(set1, set2): destructively merge two
equivalence classes
 What represents an equivalence class can
be anything as long as the above works.
 Often, one object is used to represent each
class
9
Implementing Equivalence Classes
 Since we know a, b, c, … in advance
 Make O(1) mapping from a, b, c, … to
0, 1, 2, …, N-1
 Make array A[N]
 Let A[0] represent { a }
 Let A[1] represent { b }
 …
 Initially, every object in its own class
10
Unioning Sets
union(4, 5)
union(6, 7)
11
Unioning Sets
union(4, 6)
12
Representing Equivalence Classes
 A[i] = j if element i points to j
 A[i] = -1 for a "root" element
13
Implementing union/find
union(int x, int y) // x,y are roots
a[y] = x
What are the
find(int x)
if a[x] < 0
return x
else
return find(a[x])
complexities of
union() and
find()?
What is an
iterative
version of
find()?
14
A Union Problem
union(3, 4)
Assuming find()
more frequent
than union(),
how avoid this?
15
Union by Size (or Height)
union(3, 4)
Join smaller
set to the
larger
16
Representing Size/Height
size N = -N
height N = -N-1
-1
-1
-1
4
-5
4
4
6
0
1
2
3
4
5
6
7
-1
-1
-1
4
-3
4
4
6
17
Path Compression
 Shorten connections during find(x)
 Reset every node between x and root
to root
find(x)
if a[x] < 0
return x;
else
return a[x] = find( a[x] )
18
Path Compression
worst case
using union by
size
find(14)
19