Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.

Similar presentations


Presentation on theme: "EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern."— Presentation transcript:

1 EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern

2 2  Unless otherwise noted, all tables, graphs and code from Mark Allen Weiss' Data Structures and Algorithm Analysis in C++, 3 rd ed, copyright © 2006 by Pearson Education, Inc.

3 3 Maze Maker Problem

4 4 Maze Maker Algorithm  Represent maze with 2D array of cells  Randomly knock down walls until all cells connected by some path.

5 5 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

6 6 Relations  Given a set of objects a, b, c, …  A relation R is a binary predicate between objects electricallyConnected(component 1, component 2 ) semanticallyRelated(document 1, document 2 )  i.e., shares many terms ancestorOf(person 1, person 2 )  In mathematics, a relation is a set of pairs  In computation, representing a relation as all pairs is often infeasible

7 7 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? electricallyConnected semanticallyRelated ancestorOf Yes Not transitive Not symmetric

8 8 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

9 9 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(set 1, set 2 ): 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

10 10 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

11 11 Unioning Sets union(4, 5) union(6, 7)

12 12 Unioning Sets union(4, 6)

13 13 Representing Equivalence Classes  A[i] = j if element i points to j  A[i] = -1 for a "root" element

14 14 union(int x, int y) // x,y are roots a[y] = x find(int x) if a[x] < 0 return x else return find(a[x]) Implementing union/find What are the complexities of union() and find()? What is an iterative version of find()?

15 15 A Union Problem union(3, 4) Assuming find() more frequent than union(), how avoid this?

16 16 Union by Size (or Height) union(3, 4) Join smaller set to the larger

17 17 Representing Size/Height 4-5446 4-3446 size N = -N height N = -N-1 01234567

18 18 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] )

19 19 Path Compression find(14) worst case using union by size


Download ppt "EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern."

Similar presentations


Ads by Google