Download presentation
Presentation is loading. Please wait.
Published byAshley Robertson Modified over 9 years ago
1
1 Practicum 2: More Car Dodging Trees & Lists 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 5 February 2004
2
2 Today Homework 4 More details on car dodging problem Asymptotics Nested Lists and Trees Hint: think inductively
3
Car Dodging
4
4 Simplified Car Dodging Problem Robot can move in one-dimension only (in the crosswalk), at speeds +1, 0, -1. Cars have width (x 0, x 1 )… but move through crosswalk instantaneously at times t 0, t 1, t 2, …, t n. If the robot starts at position 0, can it stay alive or does it die when “hit” by a car?
5
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.
6
6 Fifth car is fatal
7
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 I 1, I 2, I 3, …, I k. When do these intervals change?
8
8 Dodging with merging
9
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
10
10 Interrupted merging
11
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?
12
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(n 2 )
13
Asymptotics
14
14 Fudging It Running time analysis very often leads to more or less intractable problems: 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”.
15
15 Upper And Lower Bounds f(n) = O( g(n) )Big-Oh f(n) ≤ c g(n) for some constant c and all n > n 0 f(n) = ( g(n) ) Big-Omega f(n) ≥ c g(n) for some constant c and all n > n 0 f(n) = ( g(n) ) Theta f(n) = O( g(n) ) and f(n) = ( g(n) )
16
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).
17
17 Important Classes O( 1 )constant O( n )linear O( n log n )n-log-n O( n 2 )quadratic O( n 3 )cubic O( 2 c n )exponential (some authors differ) O( n! )factorial
18
18 Holy Grail O( n k )polynomial versus O( 2 c 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.
19
19 Comparison f(n) = o( g(n) ) lim f(n)/g(n) = 0 Interpretation: g is significantly worse that f. Example: f(n) = o( n 2 )sub-quadratic Often a big challenge to find an algorithm that is sub-xxxx.
20
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
21
21 Approximations Used mostly to assert the quality of an approximation. H n ~ log n + Here is the Euler-Mascheroni constant ≈ 0.5772156649015328
22
Lists and Trees
23
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.
24
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?
25
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?
26
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.
27
27 Basic Structure There are three cases to consider: nilEmptyList ( 12345, (...) )IntList ( (...), (...) ) NestList Use a small hierarchy.
28
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 )
29
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?
30
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) )
31
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?
32
32 Pretty Pictures nil
33
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: nilempty T(a,L,R)interior node
34
34 Lists as Trees There is a natural way to represent flat lists as trees.
35
35 Lists as Trees There is a natural way to represent nested lists as trees.
36
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) )
37
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?
38
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}
39
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?
40
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))
41
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.
42
42 One More Picture A 3 by 3 matrix as a tree.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.