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

Slides:



Advertisements
Similar presentations
Algorithms Algorithm: what is it ?. Algorithms Algorithm: what is it ? Some representative problems : - Interval Scheduling.
Advertisements

Practicum 2: - Asymptotics - List and Tree Structures Fundamental Data Structures and Algorithms Klaus Sutner Feb. 5, 2004.
Intro to Analysis of Algorithms. Algorithm “A sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any.
HST 952 Computing for Biomedical Scientists Lecture 10.
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Cmpt-225 Algorithm Efficiency.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Asymptotic Analysis Motivation Definitions Common complexity functions
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Computer Science 2 Data Structures and Algorithms V section 2 Intro to “big o” Lists Professor: Evan Korth New York University 1.
Elementary Data Structures and Algorithms
Fundamental Data Structures and Algorithms Klaus Sutner February 12, 2004 More LZW / Practicum.
Computer Science 2 Data Structures and Algorithms V Intro to “big o” Lists Professor: Evan Korth New York University 1.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
COMP s1 Computing 2 Complexity
MA/CSSE 473 Day 03 Asymptotics A Closer Look at Arithmetic With another student, try to write a precise, formal definition of “t(n) is in O(g(n))”
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Asymptotic Analysis-Ch. 3
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Analysis of Algorithms [ Section 4.1 ] Examples of functions important in CS: the constant function:f(n) =
MS 101: Algorithms Instructor Neelima Gupta
MS 101: Algorithms Instructor Neelima Gupta
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Copyright © 2014 Curt Hill Growth of Functions Analysis of Algorithms and its Notation.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Hashing Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 January 2005.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Asymptotic Behavior Algorithm : Design & Analysis [2]
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
1 COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
Introduction to Algorithm Analysis Concepts Fundamental Data Structures and Algorithms Aleks Nanevski and Margaret Reid-Miller January 15, 2003.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Introduction to Algorithms
Introduction Algorithms Order Analysis of Algorithm
CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Computation.
CS 3343: Analysis of Algorithms
Algorithm design and Analysis
CS 201 Fundamental Structures of Computer Science
Chapter 2.
PAC Intro to “big o” Lists Professor: Evan Korth New York University
At the end of this session, learner will be able to:
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Presentation transcript:

1 Practicum 2: More Car Dodging Trees & Lists 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

Car Dodging

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 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 Fifth car is fatal

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 Dodging with merging

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 Interrupted merging

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 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 )

Asymptotics

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 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 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 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 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 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 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 Approximations Used mostly to assert the quality of an approximation. H n ~ log n +  Here  is the Euler-Mascheroni constant  ≈

Lists and Trees

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 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 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 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 Basic Structure There are three cases to consider: nilEmptyList ( 12345, (...) )IntList ( (...), (...) ) NestList Use a small hierarchy.

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 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 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 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 Pretty Pictures nil

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 Lists as Trees There is a natural way to represent flat lists as trees.

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

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 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 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 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 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 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 One More Picture A 3 by 3 matrix as a tree.