1 / 35

CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans

CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans. Lecture 3: Levels of Abstraction. http://www.cs.virginia.edu/cs216. Sections. Make sure to go to the correct room.

percival
Download Presentation

CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS216: Program and Data Representation University of Virginia Computer Science Spring 2006David Evans Lecture 3: Levels of Abstraction http://www.cs.virginia.edu/cs216

  2. Sections • Make sure to go to the correct room. • You should go to your assigned section, but if you have a one-time scheduling conflict, it is okay to switch. • If you want to switch permanently, need permission from me • Meant to be useful. Give me (or ACs) feedback on how to make sections more useful.

  3. Menu • Orders of Growth: O, , , o • Levels of Abstraction • List Datatype

  4. Recap • Big-O: the set O(f) is the set of functions that growno faster thanf • There exist positive integers c, n0> 0 such thatf(n) cg(n)for all nn0. • Omega (): the set Ω(f) is the set of functions that growno slower thanf • There exist positive integers c, n0> 0 s.t. f(n) cg(n)for all nn0.

  5. Question from last class Given f O (h) and g  O (h) which of these are true: • For all positive integers m, f (m) < g (m). Proved false by counterexample Statement a is false, so opposite of a must be true. What is the opposite of statement a? afO (h) gO (h) m Z+, f (m) < g (m).

  6. Question from last class Given f O (h) and g  O (h) which of these are true: • For all positive integers m, f (m) < g (m). afO (h) gO (h) m Z+, f (m) < g (m). a is false  not a fO (h)gO (h) m Z+, f (m) g (m). (this is exactly our counterexample) Note this is very different from a claim like, fO (h)gO (h) m Z+, f (m) g (m).

  7. What else might be useful? f(n) = 12n2 + n O(n3) O(n2) f(n) = n2.5 (n2) Faster Growing f(n) = n3.1 – n2

  8. Theta (“Order of”) • Intuition: the set (f ) is the set of functions that growas fast asf • Definition: f (n) (g (n)) if and only if both: 1. f (n)O(g (n)) and 2. f (n)(g (n)) • Note: we do not have to pick the same c and n0 values for 1 and 2 • When we say, “f is order g” that means f (n) (g (n))

  9. Tight Bound Theta () f(n) = 12n2 + n O(n3) O(n2) f(n) = n2.5 (n2) (n2) Faster Growing f(n) = n3.1 – n2

  10. Little-oh (o) • Definition: fo (g): for allpositive constants c there is a value n0 such that f(n) cg(n)for all nn0. • Compare: f O (g): there are positive constants c and n0 such thatf(n) cg(n)for all nn0.

  11. Little-Oh (o) f(n) = 12n2 + n O(n3) O(n2) f(n) = n2.5 (n2) o(n2) (n2) Faster Growing f(n) = n3.1 – n2

  12. Summary • Big-O: there exist c, n0 > 0 such thatf(n) cg(n)for all nn0. • Omega (): there exist c, n0 > 0 s.t. f(n) cg(n)for all nn0. • Theta (): both O and  are true • Litte-o: there exists n0 > 0 such that for allc > 0, f(n) cg(n)for all nn0.

  13. (Trick) Question If wealth(n) is your net worth n days after today, would you prefer: a. wealth(n)  O(n) b. wealth(n)  O(n2) c. wealth(n)  o(n) d. wealth(n)   (n) Which of these are satisfied by wealth(n) = 0.0001n? Which is better: wealth(n) = 100000000 wealth(n) = 0.0001n

  14. Levels of Abstraction

  15. Course Goal 3 Understand how a program executes at levels of abstraction ranging from a high-level programming language to machine memory. From Lecture 1…

  16. Levels of Abstraction: Program Real World Problem High-Level Program Physical World Virtual World Machine Instructions Physical Processor

  17. http://www.rci.rutgers.edu/~cfs/472_html/Intro/TinkertoyComputer/TinkerToy.htmlhttp://www.rci.rutgers.edu/~cfs/472_html/Intro/TinkertoyComputer/TinkerToy.html Tic-Tac-Toe Play Tic-Tac-Toe Tic-Tac-Toe Strategy Low-level description Tinker Toy Computer

  18. Sequence Alignment: Program Genome Similarity High-Level Program Physical World Python Interpreter Align.py Virtual World Low-Level Program Electrons, etc.

  19. Levels of Abstraction: Data Real World Thing(s) Data Abstraction Physical World Virtual World Low-Level Data Structure Bits Electrons, etc.

  20. Levels of Abstraction: PS1 Genome Align.py Physical World Virtual World Python List Bits Electrons, etc.

  21. CS216 Levels of Abstraction • We go deeper into levels from the high level program to bits in the machine • We don’t deal with the processor and machine memory (see CS333) • Now: starting to look inside List data abstraction • Later: lower level programming language, virtual machines, assembly, data representation, etc.

  22. List Abstract Datatype • Ordered collection datatype: <x0, x1, ..., xn-1> • Operations for manipulating and observing elements of list

  23. List Operations (Ch 3) • Access (L, i): returns the ith element of L • Length (L): returns the number of elements in L • Concat (L, M): returns the result of concatenating L with M. (Elements <l0, l1, ..., l|L|-1,m0,m1 ..., m|M|-1,> ) • MakeEmptyList(): returns <> • IsEmptyList(L): returns true iff |L| = 0. Is this a sufficient list of List operations?

  24. Constructing Lists • The book’s list operations have no way of constructing any list other than the empty list! • We need at least: • Append (L, e): returns the result of appending e to L: <l0, l1, ..., l|L|-1,e>

  25. Can define using Append, Access, Length Revised List Operations • Access (L, i): returns L[i] • Length (L): returns |L| • Concat (L, M): returns the result of concatenating L with M. • MakeEmptyList(): returns <> • IsEmptyList(L): returns true iff |L| = 0. • Append (L, e): returns the result of appending e to L: <l0, l1, ..., l|L|-1,e> Easy to define using Length Are all of these operations necessary?

  26. Necessary List Operations • Access (L, i): returns L[i] • Length (L): returns |L| • MakeEmptyList(): returns <> • Append (L, e): returns the result of appending e to L: <l0, l1, ..., l|L|-1,e> Note that we have defined an immutable list. There are no operations for changing the value of a list, only making new lists.

  27. Continuous Representation L 1 3 Length: 2 Data: 3

  28. Linked Representation L 1 Info: 2 3 Info: Info: Next: Next: Next: Node Node Node We need a special value for Next when there is no Next node: Book: ΛC: 0 Python: None Scheme, Java: null

  29. Necessary List Operations • Access (L, i): returns L[i] • Length (L): returns |L| • MakeEmptyList(): returns <> • Append (L, e): returns the result of appending e to L: <l0, l1, ..., l|L|-1,e> Can we implement all of these with both representation choices?

  30. Length Linked Continuous L 3 L Info: 1 Next: 1 2 3 Length: 2 Data: def Length(L): if L == None: return 0 return 1 + \ Length(L.Next) 3 def Length(L): return L.Length

  31. Which Representation is Better? • Time of Length: (n is number of elements) • Continuous: O(1) • Linked: O(n) • Are these bounds tight? (Θ) • What about other operations? • Other factors to consider? Will explore this more next week

  32. Python Lists • Provide necessary operations: • Access (L, i): L[i] • Length (L): len(L) • MakeEmptyList(): L = [] • Append (L, e): L.append (e)

  33. Python List Operations • insert: L.insert (i, e) Returns <l0, l1, ..., li-1, e, li, ..., l|L|-1> • concatenation: L + M Returns <l0, l1, ..., l|L|-1, m0, m1, ..., m|M|-1 > • slicing: L[from:to] Returns <lfrom, lfrom + 1, ..., lto-1 > • Lots more

  34. How are they implemented? • PS1 • Try to “guess” by measuring performance of different operations • Unless you can do exhaustive experiments (hint: you can’t) you can’t be assured of a correct guess • Around PS4: • Look an lower abstraction level: C code for the Python List implementation

  35. Charge • Problem Set 1 due Monday • Point of PSs is to learn: • You can (and should) discuss your approaches and ideas with anyone • You should discuss and compare your answers to 1-6 with your assigned partner and produce a consensus best answer that you both understand and agree on • Take advantage of Small Hall On-Call Hours: • Wednesday 7-8:30pm • Thursday 4-5:30pm, 6:30-8:30pm • Friday 11am-12:30, 3:30-5pm • Saturdays 3-6pm • Sunday 3:30-9:30pm • Monday: Dynamic Programming

More Related