Lecture 19: Swinging From Up-Trees To Graphs: Today's Agenda
Lecture 19: Swinging From Up-Trees To Graphs: Today's Agenda
! Todays Agenda: " Smart Union and Find # Union-by-size/height and Path Compression " Run Time Analysis as tough as it gets! " Introduction to Graphs
Some of the material on these slides are courtesy of: S. Wolfman, CSE 326, 2000
Example: Initial Classes = {1,4,8}, {2,3}, {6}, {7}, {5,9,10} Name of equiv. class underlined
R. Rao, CSE 326
a b e {a,d,g,b,e}
c f
{c,f} {h}
Example of Find
Find: Just follow parent pointers to the root! find(f) = c find(e) = a Runtime depends on tree depth -(Tree size) Array up:
R. Rao, CSE 326
a d b e
c f
h i
-4
-2
-1
-2
7
4
Example of Union
Union: Just hang one root from the other! union(c,a) a d b e
0 (a) 1 (b) 2 (c) 3 (d) 4 (e) 5 (f) 6 (g) 7 (h) 8 (i)
c f
h i
Runtime = O(1)
Array up:
R. Rao, CSE 326
-6
-1
-2
7
5
Smart Union?
! For M Finds and N-1 Unions, worst case time is O(MN+N)
" Can we speed things up by being clever about growing our up-trees?
a b e d
c f
g h i b e a
c f d
g h i
Union(c,a)
R. Rao, CSE 326
a b e d
c f
g h i b e a
c f d
g h i b e
a d
g h c f i
Union(c,a)
R. Rao, CSE 326
Union-by-Size!
7
Smart Find?
Find(e) = a Runtime depends on tree depth d a b e If we do M Finds on the same element " O(M log N) time Can we make Find have side-effects so that next Find will be faster?
R. Rao, CSE 326 9
c f
h i
IntroducingPath Compression
! Path Compression: Point everything along path of a Find to root ! Reduces height of entire access path to 1: Finds get faster!
" Dj vu? " Idea similar to the one behind your old buddy the splay tree
a b e d
g
Find(e)
a b e d
Path compression
R. Rao, CSE 326 10
11
Find still takes O(max up-tree height) worst case But what happens to the tree heights over time? What is the amortized run time of Find if we do M Finds?
R. Rao, CSE 326 13
What is the amortized run time per operation if we do a sequence of M Unions or Finds using Union-by-Size & P.C.?
14
What is the amortized run time per operation if we do a sequence of M Unions or Finds using Union-by-Size & P.C.?
If you succeeded in solving thisyou shouldnt be in this class! One of the toughest run-time analysis problems ever!
(no, a similar problem wont be in the final)
Fine print: The final has not been written up yet, so we are not responsible for any statements about
the final made in this lecture or indeed any future lectures, but not including the final review lecture
15
" When both P.C. and Union-by-Size are used, the worst case run time for a sequence of M operations (Unions or Finds) is $(M %(M,N))
! What is %(M,N)?
16
2 2
(k logs)
2 2
log*
log*
N = 4 for N = 65536 = 22
17
A(i, j) whose inverse %(M, N) grows very, very slowly (slower than log* N)
A(1, j ) ' 2 j for j ! 1 A(i ,1) ' A(i & 1,2) for i ! 2
Go aheadtry out some values for i, j
How slowly does %(M, N) grow? %(M, N) # 4 for all practical choices of M and N (%(M, N) = 4 for M far larger than the number of atoms in
the universe (2300)!! (assuming M ! N))
Mighty profoundbut what in my dog Skips name does all this have to do with Unions and Finds?
19
" When both P.C. and Union-by-Size are used, the worst case total run time for any sequence of M Unions and Finds is $(M%(M,N))
! Textbook proves weaker result of O(M log* N) time
= total time/no. of operations = $(M%(M,N))/M = $(%(M,N)) - $(1) for all practical purposes (%(M, N) # 4 for all practical M, N) - constant time!
20
" Unions take O(1) worst case time, Finds can take O(N) " Union-by-Size (or by-Height) reduces worst case time for Find to O(log N) " If we use both Union-by-Size/Height & Path Compression:
# Any sequence of M Union/Find operations results in O(1)
Cities on a map (disjoint sets of connected cities) Electrical components on chip Computers connected in a network Groups of people related to each other by blood Textbook example: Maze generation using Unions/Finds: # Start with walls everywhere and each cell in a set by itself # Knock down walls randomly and Union cells that become connected # Use Find to find out if two cells are already connected # Terminate when starting and ending cell are in same set i.e. connected (or when all cells are in same set)
22
23
94
10 a g b
97
96
99
25
problems
26
142
378 421
401
27
B
R. Rao, CSE 326 28
Resistor
29
30
Tokyo
UW
32
Cheech Marin
Tom Hanks
F or es
3 Apollo 1
sp De a er te ly ek Se
Wallace Shawn
in g n sa Su
t Gum
Cary Elwes
Toy Story
Laurie Metcalf
33
34
Graphs: Definition
! A graph is simply a collection of nodes plus edges
" Linked lists, trees, and heaps are all special cases of graphs
! The nodes are known as vertices (node = vertex) ! Formal Definition: A graph G is a pair (V, E) where
" V is a set of vertices or nodes " E is a set of edges that connect vertices
35
Graphs: An Example
! Here is a graph G = (V, E)
" Each edge is a pair (v1, v2), where v1, v2 are vertices in V V = {A, B, C, D, E, F} E = {(A,B), (A,D), (B,C), (C,D), (C,E), (D,E)} B C A F D
R. Rao, CSE 326
E
36
! If the order of edge pairs (v1, v2) does not matter, the graph is
v2
37
Graph Representations
Space and time are measured in terms of both: Number of vertices = |V| and Number of edges = |E| There are two ways of representing graphs: The adjacency matrix representation The adjacency list representation
38
Space = ? C 0 1 0 1 1 0 D 1 0 1 0 1 0 E 0 0 1 1 0 0 F 0 0 0 0 0 0
39
if (v, w) is in E otherwise A B C C D E E F
A 0 1 0 1 0 0
B 1 0 1 0 0 0
Space = |V|2 A 0 0 0 0 0 0 B 1 0 0 0 0 0 C 0 1 0 0 0 0 D 1 0 1 0 0 0 E 0 0 1 1 0 0 F 0 0 0 0 0 0
40
B A B A C
D C D C D
41
B C D
E E
E F
b B A B A C D C D C D
42
B C D
E E
E F
b B C D E Adjacency List
43
A B C D E F
b B C D E Adjacency List
44
A B C D E F
Problem: Find an order in which all these courses can be taken. Example: 142, 143, 378, 370, 321, 341, 322, 326, 421, 401
R. Rao, CSE 326
How would you Topo-Sort this digraph given an adjacency list representation of G = (V, E)?
Next Class: Getting intimate with Topo-sorts Finding shortest ways to get to your classrooms To Do: Homework #4 (to be posted on class web Monday 2/24) Read and enjoy chapter 9
R. Rao, CSE 326 47