61A LECTURE 16 TREES, ORDERS OF GROWTH. Steven Tang and Eric Tzeng July 22, 2013

Similar documents
61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS

61A LECTURE 17 ORDERS OF GROWTH, EXCEPTIONS. Steven Tang and Eric Tzeng July 23, 2013

61A Lecture 21. Friday, March 13

Measuring Efficiency

TREES AND ORDERS OF GROWTH 7

MEMOIZATION, RECURSIVE DATA, AND SETS

61A LECTURE 7 DATA ABSTRACTION. Steven Tang and Eric Tzeng July 3, 2013

61A Lecture 3. Friday, September 5

61A LECTURE 6 RECURSION. Steven Tang and Eric Tzeng July 2, 2013

TREES AND ORDERS OF GROWTH 8

61A LECTURE 8 SEQUENCES, ITERABLES

61A LECTURE 8 SEQUENCES, ITERABLES. Steven Tang and Eric Tzeng July 8, 2013

TREES AND ORDERS OF GROWTH 7

SCHEME AND CALCULATOR 5b

TAIL RECURSION, SCOPE, AND PROJECT 4 11

61A Lecture 4. Monday, September 9

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

Recall from Last Time: Big-Oh Notation

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

UNIT 5B Binary Search

Structure and Interpretation of Computer Programs

Introduction to Computer Science and Programming for Astronomers

Algorithm Design and Recursion. Search and Sort Algorithms

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs Spring 2014 Final (with corrections)

ENVIRONMENT DIAGRAMS AND RECURSION 2

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

CSc 120. Introduc/on to Computer Programming II. 08: Efficiency and Complexity. Adapted from slides by Dr. Saumya Debray

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

n 1 i = n + i = n + f(n 1)

CSI33 Data Structures

CS116 - Module 5 - Accumulative Recursion

Emil Sekerinski, McMaster University, Winter Term 16/17 COMP SCI 1MD3 Introduction to Programming

6.00 Notes On Big-O Notation

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

Module 05: Types of recursion

What Would Python Print? Tuples, Lists, Dictionaries

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

Review implementation of Stable Matching Survey of common running times. Turn in completed problem sets. Jan 18, 2019 Sprenkle - CSCI211

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

RECURSION 3. 1 Recursion COMPUTER SCIENCE 61A. June 30, 2016

Data Structures CSCI C343, Fall 2014

CSI33 Data Structures

Last week. Last week. Last week. Today. Binary Trees. CSC148 Intro. to Computer Science. Lecture 8: Binary Trees, BST

ASYMPTOTIC COMPLEXITY

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 24 Infinite Sequences LAZY EVALUATION AGENDA 8/7/2012. Jom Magrotker UC Berkeley EECS July 30, 2012

CS61A Lecture 24 Infinite Sequences. Jom Magrotker UC Berkeley EECS July 30, 2012

CSE : Python Programming

Lecture 27: Streams and Lazy Evaluation

STREAMS, ITERATORS, AND BINARY TREES 10

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS1 Lecture 22 Mar. 6, 2019

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

Structure and Interpretation of Computer Programs

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Intro. Speed V Growth

RLISTS AND THE ENVIRONMENT MODEL 9

Lecture #5: Higher-Order Functions. A Simple Recursion. Avoiding Recalculation. Redundant Calculation. Tail Recursion and Repetition

Flow Control: Branches and loops

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012


SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

Outline. Simple Recursive Examples Analyzing Recursion Sorting The Tower of Hanoi Divide-and-conquer Approach In-Class Work. 1 Chapter 6: Recursion

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough.

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Binary search trees. We can define a node in a search tree using a Python class definition as follows: class SearchTree:

RECURSION 7. 1 Recursion COMPUTER SCIENCE 61A. October 15, 2012

Algorithmics. Some information. Programming details: Ruby desuka?

isinstance and While Loops

CS-141 Final Exam Review December 16, 2015 Presented by the RIT Computer Science Community

Introduction to Programming I

CSI33 Data Structures

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Introduction to the Analysis of Algorithms. Algorithm

RECURSION, RECURSION, (TREE) RECURSION! 3

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

TUPLES AND RECURSIVE LISTS 5

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CS 173, Running Time Analysis, Counting, and Dynamic Programming. Tandy Warnow

Structure and Interpretation of Computer Programs

CS-141 Final Exam Review December 8, 2017 Presented by the RIT Computer Science Community

CS Introduction to Data Structures Week 3, 2017

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

CS 61A Orders of Growth & Linked Lists Spring 2018 Discussion 6: March 7, Warmup

Last week. Another example. More recursive examples. How about these functions? Recursive programs. CSC148 Intro. to Computer Science

RECURSION, RECURSION, (TREE) RECURSION! 2

Python: Short Overview and Recap

CS-141 Final Exam Review May 16, 2015 Presented by the RIT Computer Science Community

CS 323 Lecture 1. Design and Analysis of Algorithms. Hoeteck Wee

Lecture #21: Search and Sets. Last modified: Wed Mar 9 15:44: CS61A: Lecture #21 1

ORDERS OF GROWTH, DATA ABSTRACTION 4

Transcription:

61A LECTURE 16 TREES, ORDERS OF GROWTH Steven Tang and Eric Tzeng July 22, 2013

Announcements Project 3 pushed back one day to August 2 Regrades for project 1 composition scores, due by next Monday Potluck Friday, July 26 6-8pm, in the Woz Lounge (same place as last time)

Data Structure Applications The data structures we cover in 61A are used everywhere in CS More about data structures in 61B Example: recursive lists (also called linked lists) Operating systems Interpreters and compilers Anything that uses a queue The Scheme programming language, which we will learn soon, uses recursive lists as its primary data structure

Example: Environments Rest Rest is Empty First Rest First Rest First First Recursive List Example: h+p://goo.gl/8dny1

Tree Structured Data Nested Sequences are Hierarchical Structures. ((1, 2), (3, 4), 5) 5 1 2 3 4 5 1 2 3 4 In every tree, a vast forest

Recursive Tree Processing Tree operajons typically make recursive calls on branches def count_leaves(tree): if type(tree)!= tuple: return 1 return sum(map(count_leaves, tree)) def map_tree(tree, fn): if type(tree)!= tuple: return fn(tree) return tuple(map_tree(branch, fn) for branch in tree)

Trees with Internal Node Values Trees can have values at internal nodes as well as their leaves. class Tree(object): def init (self, entry, left=none, right=none): self.entry = entry self.left = left self.right = right def fib_tree(n): if n == 0: return Tree(0) if n == 1: return Tree(1) left = fib_tree(n - 2) right = fib_tree(n - 1) return Tree(left.entry + right.entry, left, right)

Trees with Internal Node Values Trees can have values at internal nodes as well as their leaves. fib(5) fib(3) fib(4) fib(1) fib(2) 1 fib(0) fib(1) fib(2) fib(3) 0 1 fib(0) fib(1) fib(1) fib(2) 0 1 1 fib(0) fib(1) 0 1

Sets A built- in Python container type Set literals are enclosed in braces Duplicate elements are removed on construcjon Sets are unordered, just like dicjonary entries >>> s = {3, 2, 1, 4, 4} >>> s {1, 2, 3, 4} >>> 3 in s True >>> len(s) 4 >>> s.union({1, 5}) {1, 2, 3, 4, 5} >>> s.intersection({6, 5, 4, 3}) {3, 4}

Implementing Sets What we should be able to do with a set: Membership tesjng: Is a value an element of a set? Union: Return a set with all elements in set1 or set2 IntersecJon: Return a set with any elements in set1 and set2 AdjuncJon: Return a set with all elements in s and a value v Union Intersec.on Adjunc.on 1 4 3 5 2 3 1 4 3 5 2 3 1 4 3 2 1 2 1 2 4 3 5 3 4 3

Implementation considerations Many ways to accomplish this Not all solutions are made equal! Need a formal way to discuss how efficient implementations are Enter: orders of growth! Side note: we don t care about how efficient your implementations are in this course but you do need to know how to identify the characteristics of a program s performance

The Consumption of Time ImplementaJons of the same funcjonal abstracjon can require different amounts of Jme to compute their result. Time (remainders) def count_factors(n): factors = 0 for k in range(1, n + 1): if n % k == 0: factors += 1 return factors sqrt_n = sqrt(n) k, factors = 1, 0 while k < sqrt_n: if n % k == 0: factors += 2 k += 1 if k * k == n: factors += 1 return factors

Order of Growth A method for bounding the resources used by a funcjon as the "size" of a problem increases n: size of the problem R(n): Measurement of some resource used (Jme or space) means that there are posijve constants k1 and k2 such that for sufficiently large values of n.

A graphical explanation means that there are posijve constants k1 and k2 such that for sufficiently large values of n. sufficiently large value of n

Some useful properties Constant factors make no difference (why is this?) When summing terms, only the highest order term matters We often say the n 2 term dominates the other two

Constant Time: Time does not depend on input size. def g(n): return 42 def foo(n): baz = 7 if n > 5: baz += 5 return baz def is_even(n): return n % 2 == 0

Iteration vs. Tree Recursion (Time) IteraJve and recursive implementajons are not the same. def fib_iter(n): prev, curr = 1, 0 for _ in range(n - 1): prev, curr = curr, prev + curr return curr Time def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n - 2) + fib(n - 1) You guys have seen how to make the recursive one faster (memoizajon)

The Consumption of Time ImplementaJons of the same funcjonal abstracjon can require different amounts of Jme to compute their result. def count_factors(n): Time factors = 0 for k in range(1, n + 1): if n % k == 0: factors += 1 return factors sqrt_n = sqrt(n) k, factors = 1, 0 while k < sqrt_n: if n % k == 0: factors += 2 k += 1 if k * k == n: factors += 1 return factors

Exponentiation Goal: one more muljplicajon lets us double the problem size. def exp(b, n): if n == 0: return 1 return b * exp(b, n - 1) def square(x): return x * x def fast_exp(b, n): if n == 0: return 1 elif n % 2 == 0: return square(fast_exp(b, n // 2)) else: return b * fast_exp(b, n - 1)

Exponentiation Goal: one more muljplicajon lets us double the problem size. def exp(b, n): if n == 0: return 1 return b * exp(b, n - 1) Time Space def square(x): return x * x def fast_exp(b, n): if n == 0: return 1 elif n % 2 == 0: return square(fast_exp(b, n // 2)) else: return b * fast_exp(b, n - 1)

The Consumption of Space Which environment frames do we need to keep during evaluajon? Each step of evaluajon has a set of ac.ve environments. Values and frames in acjve environments consume memory. Memory used for other values and frames can be reclaimed. Ac.ve environments: Environments for any statements currently being executed Parent environments of funcjons named in acjve environments

The Consumption of Space ImplementaJons of the same funcjonal abstracjon can require different amounts of Jme to compute their result. def count_factors(n): factors = 0 for k in range(1, n + 1): if n % k == 0: factors += 1 return factors sqrt_n = sqrt(n) k, factors = 1, 0 while k < sqrt_n: if n % k == 0: factors += 2 k += 1 if k * k == n: factors += 1 return factors Time Space

Fibonacci Memory Consumption fib(5) fib(3) fib(4) fib(1) fib(2) 1 fib(0) fib(1) fib(2) fib(3) 0 1 fib(0) fib(1) fib(1) fib(2) 0 1 1 fib(0) fib(1) Assume we have reached this step 0 1

Fibonacci Memory Consumption fib(5) Has an acjve environment Can be reclaimed Hasn't yet been created fib(3) fib(4) fib(1) fib(2) 1 fib(0) fib(1) fib(2) fib(3) 0 1 fib(0) fib(1) fib(1) fib(2) 0 1 1 fib(0) fib(1) Assume we have reached this step 0 1

Iteration vs. Tree Recursion IteraJve and recursive implementajons are not the same. Time Space def fib_iter(n): prev, curr = 1, 0 for _ in range(n - 1): prev, curr = curr, prev + curr return curr def fib(n): if n == 1: return 0 if n == 2: return 1 return fib(n - 2) + fib(n - 1) You guys have seen how to make the recursive one faster (memoizajon)

Comparing Orders of Growth (n is problem size) ExponenJal growth! Recursive fib takes steps, where IncremenJng the problem scales R(n) by a factor. QuadraJc growth. E.g., operajons on all pairs. IncremenJng n increases R(n) by the problem size n. Linear growth. Resources scale with the problem. Logarithmic growth. These processes scale well. Doubling the problem only increments R(n). Constant. The problem size doesn't ma+er.

Break! After the break, we ll take what we just learned and use it to compare three different implementations of sets

Sets as Unordered Sequences Proposal 1: A set is represented by a recursive list that contains no duplicate items def empty(s): return s is Rlist.empty def set_contains(s, v): if empty(s): return False elif s.first == v: return True return set_contains(s.rest, v)

Sets as Unordered Sequences def adjoin_set(s, v): if set_contains(s, v): return s return Rlist(v, s) def intersect_set(set1, set2): f = lambda v: set_contains(set2, v) return filter_rlist(set1, f) def union_set(set1, set2): f = lambda v: not set_contains(set2, v) set1_not_set2 = filter_rlist(set1, f) return extend_rlist(set1_not_set2, set2) Time order of growth The size of the set Assume sets are the same size

Sets as Ordered Sequences Proposal 2: A set is represented by a recursive list with unique elements ordered from least to greatest def set_contains2(s, v): if empty(s) or s.first > v: return False elif s.first == v: return True return set_contains(s.rest, v) Order of growth?

Set Intersection Using Ordered Sequences This algorithm assumes that elements are in order. def intersect_set2(set1, set2): if empty(set1) or empty(set2): return Rlist.empty e1, e2 = set1.first, set2.first if e1 == e2: rest = intersect_set2(set1.rest, set2.rest) return Rlist(e1, rest) elif e1 < e2: return intersect_set2(set1.rest, set2) elif e2 < e1: return intersect_set2(set1, set2.rest) Order of growth?

Tree Sets Proposal 3: A set is represented as a Tree. Each entry is: Larger than all entries in its lef branch and Smaller than all entries in its right branch 7 3 5 3 9 1 7 3 9 1 5 11 5 9 1 7 11 11

Membership in Tree Sets Set membership tests traverse the tree The element is either in the lef or right sub- branch By focusing on one branch, we reduce the set by about half def set_contains3(s, v): if s is None: return False elif s.entry == v: return True elif s.entry < v: return set_contains3(s.right, v) elif s.entry > v: return set_contains3(s.left, v) Order of growth? 3 9 5 1 7 9 11 If 9 is in the set, it is in this branch

Adjoining to a Tree Set 8 8 8 8 5 9 7 None 3 9 7 11 None None 1 7 11 Right! Lef! Right! Stop! 5 9 7 8 3 9 7 11 8 1 7 11 8 8

What Did I Leave Out? Sets as ordered sequences: Adjoining an element to a set Union of two sets Sets as binary trees: IntersecJon of two sets Union of two sets That's homework!