890 likes | 957 Views
Thy Shalt Come To The TGIW. From: shuchi@cs.cmu.edu Subject: TGIW When: Tonight, 6:00pm Where: WeH 7220
E N D
Thy Shalt Come To The TGIW From: shuchi@cs.cmu.edu Subject: TGIW When: Tonight, 6:00pm Where: WeH 7220 Nikhil will be talking about his work on Online Scheduling at this week's TGIW. This is joint work with Kedar, Jochen and Amitabh. The schedule and abstract are given below. First years are especially encouraged to attend. […]
Back From The Future Efficient Finger SearchUsing Eager Walk
Color Color Color Color Color Color Color Color Color Color Color List of Colors
Important Terms/Phrases • Catenable (Cannibal on Halloween…) • File • “n” (no “m”) • Nine (no “line”) • Node • Theory • Three (“free” lunch?)
Set Intersection BST: Balanced Search Tree • You have two setsA := {x1, x2, …, xa}, B := {y1, y2, …, yb} • You represent them as BSTs TA and TB • Give an algorithm to find A Å B
Easy Solution WLOG assume a ¸ b. For each yj2 TB, ask if yj2 TA. O(b log a) time Is this optimal?
8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 An Insight A := {1,2,3, …, 15}B := {1,3,10,11} Decisions at “top” mostly the same Show More
8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Enters Finger Search fx: finger on key x FSearch(fx, y): Given a finger fx, return finger fy in O(log d) time, where d is the distance between x and y in the sorted order. If y 2 T, return fy+ for smallesty+ 2 T
8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Why Bother Finger Searching? Another simple intersection algorithm: f = f0/* finger on conceptual “y0” */For i = 1 to b /* in-order walk TB */ f = FSearchA(f, yi) if f on yi then print yi Next i
Running Time Analysis Do b finger searches in TA. Suppose distances are d1, d2, …, db. Know åi di· a Total time = O(log d1 + log d2 + … + log db) Time maximized when all di’s are equal, so let di = a/b. Total time = O(b log(a/b))
Initialization Termination Condition Increment De-referencing InOrder(tree) := Iterator it; for(it.init(tree); it.hasMore(); it++) {// print (*it).node } Iterators
Special Case In-orderWalk
8 4 12 <6 >6 2 6 10 14 1 3 5 7 9 11 13 15 Complete Binary Search Tree
Node Size Node needs at least three machine words struct Node { int key; Node *left, *right; };
Running Time Analysis InOrder(node) := If(node != Nil) { InOrder(node.left); // print node.key InOrder(node.right); } How about time between two consecutive outputs?
Running Time Analysis InOrder(node) := If(node != Nil) { InOrder(node.left); // print node.key InOrder(node.right); } Amortized O(1) per node
8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Space Requirement O(log n) activation records on the program stack InOrder(node) := If(node != Nil) { InOrder(node.left); // print node.key InOrder(node.right); }
So Far So Good? #keys #pointers
8 4 4 12 12 2 2 6 6 10 10 14 14 1 1 3 3 5 5 7 7 9 9 11 11 13 13 15 15 I wonder... Worst case O(1)?
8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Why Bother? Fast sequentialaccess slow slow slow
8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Let's Trade Node needs 2 extra words Neighborpointers
8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 / Faster Insertion Doubled node)Doubled space Indexed Sequence (B+ Trees)
8 4 Right Parentof 6 12 2 6 10 14 1 3 5 7 9 11 13 15 Two Terms Right parent is first ancestorto the right
Two Terms h 12, 10, 9 i is “Left spine” of 12 “RL spine” of 8 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15
8 4 12 O(1) time? 2 6 10 14 1 3 5 7 9 11 13 15 Our Idea Pre-compute right child’s left spine How? Discover nodes step-by-step
2 4 8 Our "Hand" Iterator This Hand isnot valid. Justfor illustration. Stack of (Node *node, Stack *spine) Right Parent Stack (n,s) (n,s) (n,s)
ptr within stack ptr to spine current node parent node left spine node Stack of Stacks (n,s) 8 Right Parent Stack (RPS) 4 12 2 6 10 14 1 3 5 7 9 11 13 15
8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Initialization Left spine of root maintained during key insertions RPS
8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 De-reference Peek top of stack RPS
8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Termination Is stack empty? RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Increment Three easy steps 1. Pop top cell andkeep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 1 2 Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 1 2 Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 1 2 Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Hand on 2 RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 2 3 SS Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 2 3 SS Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 2 3 Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 3 4 Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 3 4 Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 3 4 Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 4 5 SS Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 4 5 SS Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 4 5 Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS RPS
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Hand on 5 RPS
Checkpoint Do you understand how to this algorithm manipulate the Hand?
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Pop Quiz This is the Handon 5, so what isthe Hand on 6?
ptr within stack ptr to spine current node parent node left spine node 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Pop Quiz Three easy steps 1. Pop top cell and keep its spine (SS) 2. Extend spine of new top cell 3. Prepend SS to RPS
ptr within stack ptr to spine current node parent node left spine node Answer 5 6 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15