89
1 Thy Shalt Come To The Thy Shalt Come To The TGIW TGIW From: [email protected] 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. […]

1 Thy Shalt Come To The TGIW From: [email protected] Subject:TGIW When:Tonight, 6:00pm Where:WeH 7220 Nikhil will be talking about his work on Online

  • View
    212

  • Download
    0

Embed Size (px)

Citation preview

1

Thy Shalt Come To The Thy Shalt Come To The TGIWTGIW

From: [email protected]: TGIWWhen: Tonight, 6:00pmWhere: 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. […]

October 2001

Guy BlellochGuy Blelloch

Maverick WooMaverick Woo

Back From The FutureBack From The Future

Efficient Finger SearchUsing Eager Walk

3

List of ColorsList of Colors

ColorColorColorColorColorColorColorColor

ColorColorColor

4

Important Terms/PhrasesImportant Terms/Phrases

Catenable (Cannibal on Halloween…)File“n” (no “m”)Nine (no “line”)NodeTheoryThree (“free” lunch?)

5

Set IntersectionSet Intersection

You have two sets

A := {x1, x2, …, xa},

B := {y1, y2, …, yb}

You represent them as BSTs

TA and TB

Give an algorithm to find A Å B

BST: Balanced Search Tree

BST: Balanced Search Tree

6

Easy SolutionEasy Solution

WLOG assume a ¸ b.

For each yj 2 TB, ask if yj 2 TA.

O(b log a) timeIs this optimal?Is this optimal?

7

An InsightAn Insight

A := {1,2,3, …, 15}B := {1,3,10,11}

Decisions at “top” mostly the same

75

6

31

2

4

8

1513

14

119

10

12

Show MoreShow More

9

Enters Finger SearchEnters Finger Search

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 smallest

y+ 2 T75

6

31

2

4

8

1513

14

119

10

12

fx: finger on key x

fx: finger on key x

10

Why Bother Finger Why Bother Finger Searching?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

75

6

31

2

4

8

1513

14

119

10

12

11

Running Time AnalysisRunning 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))

12

IteratorsIterators

InitializationTermination ConditionIncrementDe-referencing

InOrder(tree) :=

Iterator it;

for(it.init(tree);

it.hasMore();

it++)

{ // print (*it).node

}

13

Special CaseSpecial Case

In-orderWalk

14

Complete Binary Search Complete Binary Search TreeTree

75

6

31

2

4

8

1513

14

119

10

12

<6 >6

16

Node SizeNode Size

struct Node

{

int key;

Node *left, *right;

};

Node needs at least three machine words

Node needs at least three machine words

17

Running Time AnalysisRunning Time Analysis

InOrder(node) := If(node != Nil) { InOrder(node.left); // print node.key InOrder(node.right); }

How about time between two consecutive outputs?

18

Running Time AnalysisRunning Time Analysis

InOrder(node) := If(node != Nil) { InOrder(node.left); // print node.key InOrder(node.right); }

Amortized O(1) per node

19

Space RequirementSpace Requirement

O(log n) activation records on the program stack

InOrder(node) :=

If(node != Nil)

{

InOrder(node.left);

// print node.key

InOrder(node.right);

}

75

6

31

2

4

8

1513

14

119

10

12

20

So Far So Good?So Far So Good?

Version Tree Size Time/Node Space

Recursive n+2n = 3n Amortized O(1) O(log n)

#keys #pointers

21

I wonder...I wonder...

75

6

31

2

4

8

1513

14

119

10

12

75

6

31

2

4

1513

14

119

10

12

Version Tree Size Time/Node Space

Recursive n+2n = 3n Amortized O(1) O(log n)

Worst case O(1)?

Worst case O(1)?

22

Why Bother?Why Bother?

Fast sequentialaccess

slowslow

slowslow

slowslow

75

6

31

2

4

8

1513

14

119

10

12

23

Let's TradeLet's Trade

Neighborpointers

75

6

31

2

4

8

1513

14

119

10

12

Node needs 2 extra wordsNode needs 2 extra words

24

Faster InsertionFaster Insertion

Indexed Sequence(B+ Trees)

75

6

31

2

4

8

1513

14

119

10

12

1 2 3 4 5 6 7 8 9 10 1112 1314 15 /

Doubled node

)Doubled space

Doubled node

)Doubled space

25

Oh Well...Oh Well...

Version Tree Size Time/Node SpaceRecursive n+2n = 3n Amortized O(1) O(log n)

Nbr. Pointers n+4n = 5n Worst Case O(1) O(1)

Indexed Seq. 2n+4n = 6n Worst Case O(1) O(1)

26

Two TermsTwo Terms

75

6

31

2

4

8

1513

14

119

10

12Right Parentof 6

Right parent is first ancestorto the right

Right parent is first ancestorto the right

27

Two TermsTwo Terms

75

6

31

2

4

8

1513

14

119

10

12

h 12, 10, 9 i is “Left spine” of 12 “RL spine” of 8

28

Our IdeaOur Idea

Pre-compute right child’s left spineHow? Discover nodes step-by-step

75

6

31

2

4

8

1513

14

119

10

12

O(1) time?

29

Our "Hand" IteratorOur "Hand" Iterator

Stack of (Node *node, Stack *spine)

8

4

2

5

6

3

(n,s)Right Parent Stack

This Hand isnot valid. Justfor illustration.

This Hand isnot valid. Justfor illustration.

(n,s)

(n,s)

30

Stack of StacksStack of Stacks

2

4

8

5

6

75

6

31

2

4

8

1513

14

119

10

12Right Parent Stack (RPS)

ptr within stack

ptr to spinecurrent nodeparent nodeleft spine node

(n,s)

3

31

InitializationInitialization

Left spine of rootmaintained duringkey insertions

75

6

31

2

4

8

1513

14

119

10

12

1

2

4

8RPS

32

De-referenceDe-reference

Peek top of stack

1

2

4

8

75

6

31

2

4

8

1513

14

119

10

12

RPS

33

TerminationTermination

Is stack empty?

1

2

4

8

75

6

31

2

4

8

1513

14

119

10

12

RPS

34

IncrementIncrement

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

1

2

4

8

75

6

31

2

4

8

1513

14

119

10

12

RPS

ptr within stack

ptr to spinecurrent nodeparent nodeleft spine node

35

1 1 2 2

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

2

4

8

75

6

31

2

4

8

1513

14

119

10

12

RPS

ptr within stack

ptr to spinecurrent nodeparent nodeleft spine node

36

1 1 2 2

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

2

4

8

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

3

RPS

37

1 1 2 2

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

2

4

8

3

RPS

38

Hand on 2Hand on 2

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

2

4

8

3

RPS

39

2 2 3 3

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

4

8

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

3

RPS

SS

40

2 2 3 3

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

4

8

3

SS

RPS

6

41

2 2 3 3

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

3

4

8

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

RPS

6

42

3 3 4 4

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

4

8

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

RPS

6

43

3 3 4 4

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

4

8RPS

5

6

44

3 3 4 4

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

4

8RPS

5

6

45

4 4 5 5

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

8

5

6

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

SS

RPS

46

4 4 5 5

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

8

5

6

SS

RPS

12

47

4 4 5 5

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

5

6

8

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

RPS

12

48

Hand on 5Hand on 5

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

5

6

8RPS

12

49

CheckpointCheckpoint

Do you understand how to this algorithm manipulate the Hand?

50

Pop QuizPop Quiz

This is the Handon 5, so what isthe Hand on 6?

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

5

6

8 12

51

Pop QuizPop Quiz

75

6

31

2

4

8

1513

14

119

10

12ptr within

stackptr to spinecurrent nodeparent nodeleft spine node

5

6

8 12

Three easy steps1. Pop top cell and

keep its spine (SS)2. Extend spine of

new top cell3. Prepend SS to RPS

52

AnswerAnswer

75

6

31

2

4

8

1513

14

119

10

12

6

8

5 5 6 65 5 6 6

ptr within stack

ptr to spinecurrent nodeparent nodeleft spine node

5

6

8 12

12

7

53

CorrectnessCorrectness

A node is either on left spine or not.

If on left spine, init finds it.

If not one left spine, another node must be on its left.

75

6

31

2

4

8

1513

14

119

10

12

54

Running Time AnalysisRunning Time Analysis

Increment has three easy steps1. Pop top cell and

O(1) keep its spine (SS)

2. Extend spine ofO(1)

new top cell3. Prepend SS to RPS O(1)

Total Time Worst case O(1)

55

Space RequirementSpace Requirement

Starts with log(n) cellsEach increment

1. Pop top cell and -1keep its spine (SS)

2. Extend spine of +1new top cell

3. Prepend SS to RPS +0Total change 0

56

How are we doing?How are we doing?

Version Tree Size Time/Node SpaceRecursive n+2n = 3n Amortized O(1) O(log n)

Nbr. Pointers n+4n = 5n Worst Case O(1) O(1)

Indexed Seq. 2n+4n = 6n Worst Case O(1) O(1)

Eager Walk n+2n = 3n Worst Case O(1) O(log n)

57

General CaseGeneral Case

FingerSearch

58

1980 Brown, Tarjan1980 Brown, Tarjan

Level-linked2-3 trees

75

6

31

2

4

8

1513

14

119

10

12

1 2 3 4 5 6 7 8 9 10 1112 1314 15 /

5 pointersper node

5 pointersper node

59

Since 1980...Since 1980...

1988 – Tarjan, Van Wyk Heterogeneous Red-Black

1989 – Pugh Skip List

1995 – Seidel, Aragon Treaps

1996 – Kaplan, Tarjan Purely Functional

Catenable Sorted List

2001 – Blandford, Blelloch HeterogeneousTreaps

60

Since 1980...Since 1980...

1988 – Tarjan, Van Wyk Heterogeneous Red-Black

1989 – Pugh Skip List

1993 – Cole (conjectured Splay treesby Sleator, Tarjan 1985)

1995 – Seidel, Aragon Treaps

1996 – Kaplan, Tarjan Purely Functional

Catenable Sorted List

2001 – Blandford, Blelloch HeterogeneousTreaps

1993 – Cole (conjectured Splay treesby Sleator, Tarjan 1985)

61

Space-Time Price ChartSpace-Time Price Chart

Version Tree Size Time SpaceLevel-link 2,3 n+5n = 6n Worst case O(log d) O(1)

Hetero. RB n+2n = 3n Amortized O(log d) O(1)

Skip List n+2n = 3n Expected O(log d) O(1)

Splay Tree n+2n = 3n Amortized O(log d) O(1)

Treaps n+3n = 4n Expected O(log d) O(1)

Functional Listn+~3n =

~4nWorst case O(log d) O(1)

Hetero. Treaps

n+2n = 3n Expected O(log d) O(1)

62

Our Forward SolutionOur Forward Solution

Same data structure

as in-order walk!

ptr within stack

ptr to spinecurrent nodeparent nodeleft spine node

3

4

8

75

6

31

2

4

8

1513

14

119

10

12

6

63

Pictorial HandPictorial Handptr within

stackptr to spinecurrent nodeparent nodeleft spine node

curr

RP

peer

64

Global LevelGlobal Level

TODO: show how the invariant is. In particular, create a picture saying how long the spines are (for all nodes, it’s all up to the point where the key on top of it on the stack is)

65

An ExampleAn Example

curr

RP

peer

75

6

31

2

4

8

1513

14

119

10

12

ptr within stack

ptr to spinecurrent nodeparent nodeleft spine node

66

Hand InvariantsHand Invariants

Let stack be labeled upwards

1. What nodes should be on RPS?

2.How long are their spines?

84

3

6

x3

x2

x1

s3

s2

s1

75

6

31

2

4

8

1513

14

119

10

12

Skip InvariantsSkip Invariants

67

Hand InvariantsHand Invariants

Let stack be h(xn,sn),(xn-1,sn-1),…,(x1,s1)i

1.x1 is on right spine of tree Æ

8 j > 1 : xj-1 is RP of xj

2.8 j : sj is a prefix of left spine of

xj’s right child Æ

8 j : |sj| =

h(xj) – 1 – h(xj+1)

75

6

31

2

4

8

1513

14

119

10

12

84

3

6

x3

x2

x1

s3

s2

s1

68

Hand InvariantsHand Invariants

Let stack be h(xn,sn),(xn-1,sn-1),…,(x1,s1)i

1.x1 is on right spine of tree Æ

8 j > 1 : xj-1 is RP of xj

2.8 j : sj is a prefix of left spine of

xj’s right child Æ

8 j : |sj| =

h(xj) – 1 – h(xj+1)

75

6

31

2

4

8

1513

14

119

10

12

84

3

6

x3

x2

x1

s3

s2

s1

69

Hand InvariantsHand Invariants

Let stack be h(xn,sn),(xn-1,sn-1),…,(x1,s1)i

1.x1 is on right spine of tree Æ

8 j > 1 : xj-1 is RP of xj

2.8 j : sj is a prefix of left spine of

xj’s right child Æ

8 j : |sj| =

h(xj) – 1 – h(xj+1)

75

6

31

2

4

8

1513

14

119

10

12

84

3

6

x3

x2

x1

s3

s2

s1

70

Hand InvariantsHand Invariants

Let stack be h(xn,sn),(xn-1,sn-1),…,(x1,s1)i

1.x1 is on right spine of tree Æ

8 j > 1 : xj-1 is RP of xj

2.8 j : sj is a prefix of left spine of

xj’s right child Æ

8 j : |sj| =

h(xj) – 1 – h(xj+1)

75

6

31

2

4

8

1513

14

119

10

12

84

3

6

x3

x2

x1

s3

s2

s1

71

Hand InvariantsHand Invariants

Let stack be h(xn,sn),(xn-1,sn-1),…,(x1,s1)i

1.x1 is on right spine of tree Æ

8 j > 1 : xj-1 is RP of xj

2.8 j : sj is a prefix of left spine of

xj’s right child Æ

8 j : |sj| =

h(xj) – 1 – h(xj+1)

75

6

31

2

4

8

1513

14

119

10

12

84

3

6

x3

x2

x1

s3

s2

s1

72

Hand InvariantsHand Invariants

Denote height of xj by h(xj).

Define h(xj) = 0 for any j > n.

(In reality, that xj does not exist.)

75

6

31

2

4

8

1513

14

119

10

12

h=1

h=2

h=3

h=4

84

3

6

x3

x2

x1

s3

s2

s1

73

Sub-tree Search LemmaSub-tree Search Lemma

Given fx to minimum key x of a sub-tree,

we can obtain fy in O(log d) time if y is in

the same sub-tree as x, or if y is the right

parent of the sub-tree’s root. Restoring

invariants of hand also

takes O(log d) time.

1

2

4

8

75

6

31

2

4

8

1513

14

119

10

12

Prove LemmaProve Lemma

77

CheckpointCheckpoint

Do you understand what the lemma is?

(Realize that this sub-tree search does not always happen because the target can be somewhere else.)

75

6

31

2

4

8

1513

14

119

10

12

v=3

78

Where can our destination Where can our destination be?be?FSearch(fcurr,y)

1. curr < y < RP

2. y = RP

3. RP < y < peer

4. y = peer

5. peer < y

curr

RP

peer

Can distinguish cases in O(1) time

Can distinguish cases in O(1) time

79

Case 1Case 1

1. Do an increment(now at curr++)

2. Sub-tree search

Increment is O(1), Search is O(log d)

Invariants restored automagically

curr

RP

peer

80

Case 2Case 2

Proceed as in case 1

(Sub-tree search fory in will walk right spine as RP 2 , invariants restored automagically)

Analysis is the same as case 1

curr

RP

peer

81

Case 3Case 3

1. Proceed as in case 2

to obtain a hand on RP

(search in gives fRP)

2. Do an increment (now on RP++)

3. Sub-tree search

curr

RP

peer

82

d is at least size of

O(log d) to arrive at RP

O(1) to arrive at RP++

O(log d) for a sub-tree search

Case 3 AnalysisCase 3 Analysis

curr

RP

peer

83

Case 4Case 4

Proceed as in case 3

to obtain fpeer

Analysis is the same as case 3

curr

RP

peer

84

Case 5Case 5

1. Find the largest

right parent p · y by

popping RPS (and discard spines)

2. Get fp by fixing hand invariants

3. FSearch(fp, y)

(this time guaranteed to be case 1)

curr

RP

peer

85

GraphicallyGraphically

We know it’s case 5

curr

RP

peer

86

GraphicallyGraphically

curr

RP

peer

pFind largest parentp · y by popping RPS

p’s RP

p’s peer

87

GraphicallyGraphically

p

p’s RP

p’s peerFix invariants toobtain fp

curr

RP

peer

88

GraphicallyGraphically

p

p’s RP

p’s peerNow it must becase 1 (or y=p)

curr

RP

peer

89

Case 5 AnalysisCase 5 Analysis

Imagine writing down all keys

… curr, ……, p, ……, y …

d

·O(log d)·O(log d)

distance

time

75

6

31

2

4

8

1513

14

119

10

12

8

4

3

6

Skip DetailsSkip Details

90

Case 5 AnalysisCase 5 Analysis

Consider RPS before FSearch(fcurr, y).

Let the p be xj.

By invariant 2, |sj| = h(xj) – 1 – h(xj+1). This is the amount of work we have already done previously.

To fix invariant 2, we need to extend sj down to leaf so that |sj| = h(xj) – 1.

) Time = h(xj+1) = v in figure

8

4

3

6

75

6

31

2

4

8

1513

14

119

10

12

v

x3

x2

x1

s3

s2

s1

x1

x2

x3

91

Case 5 AnalysisCase 5 Analysis

How many keys between xj+1 and xj?

Answer: 2v-1 – 1

) d > 2v-1 – 1) v = O(log d)

75

6

31

2

4

8

1513

14

119

10

12

v

x1

x2

x3

92

Space-Time Price ChartSpace-Time Price Chart

Version Tree Size Time SpaceLevel-link 2,3 n+5n = 6n Worst case O(log d) O(1)

Hetero. RB n+2n = 3n Amortized O(log d) O(1)

Skip List n+2n = 3n Expected O(log d) O(1)

Splay Tree n+2n = 3n Amortized O(log d) O(1)

Treaps n+3n = 4n Expected O(log d) O(1)

Functional Listn+~3n =

~4nWorst case O(log d) O(1)

Hetero. Treaps

n+2n = 3n Expected O(log d) O(1)

Eager Walk n+2n = 3n Worst case O(log d) O(log n)

101

ExtensionExtension

Slight change will work for any height-balanced BST (e.g. Red-Black, B-Tree)

Treaps? AVL? Splay trees?

Can also support forward and backward simultaneously (messy…)

102

It's Halloween!It's Halloween!You are cordially invited to

the first HALLOWEEN PARTY of the Millennium on Halloween at the Church after Dusk

DRESS CODE AND DINNER MENU Humans will be eaten.

DIRECTIONS Follow the smell of blood up Forbes towards Squirrel Hill, turn right onto Wightman, turn second right onto Bartlett. Your liquid or solid of choice will be found at 5516 Bartlett St. Spouses, dates, siblings, housemates, familiars and friends are welcome.

John, Mukesh, and Urs

I said “Catenable”;not “Cannibal”

I said “Catenable”;not “Cannibal”