1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms...

42
1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004

Transcript of 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms...

Page 1: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

1

Practicum 2:More Car DodgingTrees & Lists

15-211 Fundamental Data Structures and Algorithms

Margaret Reid-Miller

5 February 2004

Page 2: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

2

Today

• Homework 4 More details on car dodging problem

• Asymptotics

• Nested Lists and Trees Hint: think inductively

Page 3: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

Car Dodging

Page 4: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

4

Simplified Car Dodging Problem

• Robot can move in one-dimension only (in the crosswalk), at speeds +1, 0, -1.

• Cars have width (x0, x1)… but move through crosswalk instantaneously at times t0, t1, t2, …, tn.

• If the robot starts at position 0, can it stay alive or does it die when “hit” by a car?

Page 5: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

5

Car Dodging Model

• Model as 2-dimensional space-time.

• Position is pair (x, t), x is location, t 0 is time. E.g., Robot’s initial position is (0,0).

• A position is reachable if robot can move from (0,0) to (x, t) without colliding with a car. The car boundary is safe.

Page 6: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

6

Fifth car is fatal

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 7: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

7

Car Dodging Algorithm

• Compute region R of all reachable points (a polygon with boundaries that are either car line-segments or lines of slope +1 or -1).

• Sweep a line through time. The intersection of the sweep-line with R at time t is a disjoint set of intervals I1, I2, I3, …, Ik.

• When do these intervals change?

Page 8: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

8

Dodging with merging

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 9: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

9

Reachable interval changes

• Growing: Upper endpoint moving up at speed +1. Lower endpoint moving down at speed -1.

• Truncated at one end (car event).

• Totally erased (car event).

• Split in two (car event) -> new merge event

Page 10: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

10

Interrupted merging

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Page 11: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

11

Car Dodger Classes

• Event Queue Priority queue, with time as priority. isEmpty(), delMin(), insert(ev), print(str)

• Interval Structure Any suitable structure print(), processEvent(ev), isAlive()

• Event

• Others?

Page 12: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

12

Car Dodger Issues

• Merge only between adjacent intervals.

• But car event may affect many intervals.

• How do you detect that a merge event is obsolete?

• Efficiency O(n log n) instead of O(n2)

Page 13: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

Asymptotics

Page 14: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

14

Fudging It

Running time analysis very often leads to more or less intractableproblems: counting steps even in very simple programs is just hopelessly complicated.

Trying to get precise answers is also really quite useless in most cases.

It's better to ignore details and focus on the “large picture”.

Page 15: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

15

Upper And Lower Bounds

f(n) = O( g(n) ) Big-Oh

f(n) ≤ c g(n) for some constant c and all n > n0

f(n) = ( g(n) ) Big-Omega

f(n) ≥ c g(n) for some constant c and all n > n0

f(n) = ( g(n) ) Theta

f(n) = O( g(n) ) and f(n) = ( g(n) )

Page 16: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

16

Upper And Lower Bounds

f(n) = O( g(n) ) Big-Oh

can only be used for upper bounds

f(n) = ( g(n) ) Big-Omega

can only be used for lower bounds

f(n) = ( g(n) ) Theta

pins down the running time exactly (up to a multiplicative constant).

Page 17: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

17

Important ClassesO( 1 ) constant

O( n ) linear

O( n log n ) n-log-n

O( n2 ) quadratic

O( n3 ) cubic

O( 2c n ) exponential (some authors differ)

O( n! ) factorial

Page 18: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

18

Holy Grail

O( nk ) polynomial

versus

O( 2c n ) exponential (some authors differ)

Anything that can be done in polynomial time is tractable, feasible, doable. But note that there are constants lurking in the dark.

Empirical Fact: They don't seem to matter much.

Page 19: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

19

Comparison

f(n) = o( g(n) )

lim f(n)/g(n) = 0

Interpretation: g is significantly worse that f.

Example:f(n) = o( n2 ) sub-quadratic

Often a big challenge to find an algorithm that is sub-xxxx.

Page 20: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

20

Similarity

f(n) ~ g(n)

lim f(n)/g(n) = 1

Interpretation: f and g are essentially indistinguishable.

This is much stronger than .

Example: n ~ n + 1/n

Page 21: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

21

Approximations

Used mostly to assert the quality of an approximation.

Hn ~ log n +

Here is the Euler-Mascheroni constant

≈ 0.5772156649015328

Page 22: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

Lists and Trees

Page 23: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

23

More Recursive Data Structures

Inductive thinking is often the best way to tackle complicated data structures.

Plain linked lists are a cheap example, but not convincing: everybody knows how to hack linked lists, induction be damned.

Let's try something more ambitious:

Nested Lists and Binary Trees.

Page 24: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

24

Nested Lists

In an ordinary lists, only atomic elements such as integers can be stored.

How about allowing lists of lists of lists ... of integers?

( 1, 2, ( 3, 4 ), ((5)), 6 )

How hard would it be to design this type of data structure?

What basic operations should we use?

How does it compare to other structures such as trees?

Page 25: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

25

Basic Operations

1. How do we construct such a nested list?

What is the inductive structure here?

2. Suppose we already have built such a nested list.

What are the access operations we need to get at the pieces?

3. How do we deal with operations such as flattening?

Page 26: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

26

Basic Operations

How do we implement this using Java's language features?

MAW: null is not such a great idea.

In the OO framework everything should be a class, even an empty list.

The root concept List appears in several incarnations:

empty, with leading int, with leading list.

Page 27: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

27

Basic Structure

There are three cases to consider:

nil EmptyList

( 12345, (...) ) IntList

( (...), (...) ) NestList

Use a small hierarchy.

Page 28: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

28

Access

We only need the standard first/rest machinery.

Every position in a nested list can be accessed by a sequence of first/rest operations.

Note that simple iterators don't quite work here: we need to be able to go forward or down:

( (5,6), 2, 3, 4 )

Page 29: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

29

Flattening

Intuitively, the flatten operation is easy:

( (5,6), 2, ((3)), 4 ) --> (5,6,2,3,4)

Domain: nested lists, codomain: simple lists.

We may assume we have the usual operations on simple lists available.

So how does flatten act on a nested list?

Page 30: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

30

Flattening

( (5,6), 2, ((3)), 4 ) --> (5,6,2,3,4)

flatten(nil) = nil

flatten( (x,R) ) = prepend( flatten(R), x )

flatten( (L,R) ) = join( flatten(L), flatten(R) )

Page 31: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

31

Binary Trees

Really ordered binary trees: every child is either left or right (even when the other child is missing).

Information can be stored only at leaf nodes (for simplicity, let's just say an integer can be stored).

Intuitively, it should be clear that this DS is more “powerful” than just linked lists. Right?

Page 32: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

32

Pretty Pictures

nil

Page 33: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

33

Basic Choices

Note that this is really quite similar to the nested list construction.

Again we build a small class hierarchy:

Root concept Tree appears in incarnations:

nil empty

T(a,L,R) interior node

Page 34: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

34

Lists as Trees

There is a natural way to represent flat lists as trees.

Page 35: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

35

Lists as Trees

There is a natural way to represent nested lists as trees.

Page 36: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

36

How To Convert?

How would a conversion function work?

l2t( nil ) = nil

l2t( (x,R) ) = T( - , T(x,nil,nil), l2t(R) )

l2t( (L,R) ) = T( - , l2t(L), l2t(R) )

Page 37: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

37

How To Convert?

Looks a bit inelegant. Better would be

l2t( nil ) = nil

l2t( (x,R) ) = T( x, l2t(R) )

l2t( (L,R) ) = T( l2t(L), l2t(R) )

What does this require in terms of the trees?

Page 38: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

38

Proper Trees

Note that not every tree can be the result of converting a nested list.

How do we check whether a tree is obtained by converting a list?

Let's call these trees proper.

We want a function

proper : trees --> {true,false}

Page 39: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

39

Who'se More Powerful?

It looks like trees are more powerful than nested lists.

Are they really?

Suppose Fritz Hackmann has this absolutely fantastic implementation of nested lists. Francoise Haquer needs a tree implementation, and fast.

Could she somehow use Fritz's code?

Page 40: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

40

What Does This Mean?

At the very least, we need to be able to convert trees into nested lists. There has to be an injective map

t2l : trees --> lists

Let's not worry about efficiency at this point.

--> ( (a,b),c,d,(e))

Page 41: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

41

Converting Back

t2l( nil ) = nil

t2l( T( -, T(x,nil,nil), R ) ) = ( x, t2l(R) )

t2l( T( -, L, R ) ) = ( ( t2l(L) ), t2l(R) )

Take this with a grain of salt, there should be list operations on the right.

Page 42: 1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004.

42

One More Picture

A 3 by 3 matrix as a tree.