Instructor: Lilian de Greef Quarter: Summer 2017

Slides:



Advertisements
Similar presentations
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Advertisements

Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
CSE332: Data Abstractions Lecture 3: Asymptotic Analysis Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Dan Grossman Spring 2010.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
CSE332: Data Abstractions Lecture 3: Asymptotic Analysis Dan Grossman Spring 2012.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
CSE332: Data Abstractions Section 2 HyeIn Kim Spring 2013.
CSE373: Data Structures and Algorithms Lecture 2b: Proof by Induction and Powers of Two Nicki Dell Spring 2014.
CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction & Algorithm Analysis Lauren Milne Summer 2015.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Catie Baker Spring 2015.
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Mathematical Background and Linked Lists. 2 Iterative Algorithm for Sum Find the sum of the first n integers stored in an array v : sum (v[], n) temp_sum.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Lauren Milne Summer 2015.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Nicki Dell Spring 2014.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 3.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Lauren Milne Summer 2015.
October 3, 2001CSE 373, Autumn Mathematical Background Exponents X A X B = X A+B X A / X B = X A-B (X A ) B = X AB X N +X N = 2X N 2 N +2 N = 2 N+1.
CSE332: Data Abstractions Lecture 3: Asymptotic Analysis Tyler Robison Summer
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Linda Shapiro Winter 2015.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
Lecture 3COMPSCI.220.S1.T Running Time: Estimation Rules Running time is proportional to the most significant term in T(n) Once a problem size.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Algorithm Analysis 1.
Analysis of Algorithms and Inductive Proofs
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Kevin Quinn Fall 2015.
Analysis of Algorithms CS 477/677
Instructor: Lilian de Greef Quarter: Summer 2017
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Analysis of Algorithms
Towers of Hanoi Move n (4) disks from pole A to pole C
CSE373: Data Structures and Algorithms Lecture 3: Asymptotic Analysis
CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
CS 3343: Analysis of Algorithms
COMP 53 – Week Seven Big O Sorting.
Great Theoretical Ideas in Computer Science
David Kauchak CS52 – Spring 2015
Instructor: Lilian de Greef Quarter: Summer 2017
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis
Algorithm design and Analysis
CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction
What is CS 253 about? Contrary to the wide spread belief that the #1 job of computers is to perform calculations (which is why the are called “computers”),
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Dan Grossman Fall 2013.
CSE373: Data Structures and Algorithms Lecture 3: Asymptotic Analysis
Comparing Algorithms Unit 1.2.
CS 3343: Analysis of Algorithms
Analysis of Algorithms
CSE 373 Data Structures and Algorithms
CSE 2010: Algorithms and Data Structures Algorithms
CSE 373 Data Structures Lecture 5
Recursion.
Searching, Sorting, and Asymptotic Complexity
Searching, Sorting, and Asymptotic Complexity
CSE 373: Data Structures & Algorithms
Asymptotic complexity Searching/sorting
Mathematical Background 2
Introduction to Discrete Mathematics
CSE 373 Optional Section Led by Yuanwei, Luyi Apr
Asymptotic complexity
CSE 326: Data Structures Lecture #6 Asymptotic Analysis
Recursion Chapter 12.
Analysis of Algorithms
Algorithms and data structures: basic definitions
Presentation transcript:

Instructor: Lilian de Greef Quarter: Summer 2017 CSE 373: Data Structures and Algorithms Lecture 3: Asymptotic Analysis part 2 Math Review, Inductive Proofs, Recursive Functions Instructor: Lilian de Greef Quarter: Summer 2017 Laptops Question series Where are we? (classroom / school / university) Why are we here? (to learn!) One of best ways to learn: if you come up with answers. I'm going to make you think! Could spew info 3 hours/week, no way for you or me to check understanding. I will ask you lots of questions in class If you don't know or say the correct answer, that's one of the greatest opportunity for us! Give others chance to answer if you know it I strongly encourage you to ask questions! Interrupt me! Not telling you all the answers, asking you to come up with them Who's had classes done this way before? I know this is probably different from what you're used to Learning takes effort. I can't do it for you. Analogy to weight lifting. I'll do a lot of talking for the beginning of this lecture as we cover logistics.

Today: Brief Math Review (review mostly on your own) Continue asymptotic analysis with Big-O Proof by Induction Recursive Functions

Common Big-O Names O(1) constant (same as O(k) for constant k) O(log n) logarithmic O(n) linear O(n log n) “n log n” O(n2) quadratic O(n3) cubic O(nk) polynomial (where is k is any constant) O(kn) exponential (where k is any constant > 1) Note: “exponential” does not mean “grows really fast”, it means “grows at rate proportional to kn for some k>1” A savings account accrues interest exponentially (k=1.01?) If you don’t know k, you probably don’t know it’s exponential

Powers of 2: Fun Facts A bit is 0 or 1 (just two different “letters” or “symbols”) A sequence of n bits can represent 2n distinct things (For example, the numbers 1 through 2n) 210 is 1024 (“about a thousand”, kilo in CSE speak) 220 is “about a million”, mega in CSE speak 230 is “about a billion”, giga in CSE speak Java: an int is 32 bits and signed, so “max int” is “about 2 billion” a long is 64 bits and signed, so “max long” is 263-1

Which means… You could give a unique id to… Every person in the U.S. with 29 bits Every person in the world with 33 bits Every person to have ever lived with 38 bits (estimate) Every atom in the universe with 250-300 bits So if a password is 128 bits long and randomly generated, do you think you could guess it?

Math Review: Logs & Exponents (Interlude #2 from Big-O)

…but, does it matter? Logs & Exponents Definition: 𝑙𝑜𝑔 𝑎 𝑥=𝑦 if 𝑎 𝑦 =𝑥 𝑙𝑜𝑔 2 32= 𝑙𝑜𝑔 10 10,000= Outside of CSE, 𝑙𝑜𝑔(𝑥) is often short-hand for In CSE, 𝑙𝑜𝑔(𝑥) is often short-hand for …but, does it matter?

Can Make a log2 Out of Any log! 𝑙𝑜𝑔 𝐴 𝑥= 𝑙𝑜𝑔 𝐵 (𝑥) 𝑙𝑜𝑔 𝐵 (𝐴) so 𝑙𝑜𝑔 2 𝑥= 𝑙𝑜𝑔 𝑤ℎ𝑎𝑡𝑒𝑣𝑒𝑟 (𝑥) 𝑙𝑜𝑔 𝑤ℎ𝑎𝑡𝑒𝑣𝑒𝑟 (2)

Other Properties of Logarithms (to review on your own time) 𝑙𝑜𝑔 𝐴∗𝐵 =𝑙𝑜𝑔𝐴+𝑙𝑜𝑔𝐵 So 𝑙𝑜𝑔 𝑁 𝑘 =𝑘∗𝑙𝑜𝑔𝑁 𝑙𝑜𝑔 𝐴/𝐵 =𝑙𝑜𝑔𝐴−𝑙𝑜𝑔𝐵 𝑙𝑜𝑔 𝑙𝑜𝑔𝑥 =𝑙𝑜𝑔 𝑙𝑜𝑔 𝑥 Grows as slowly as 22 grows quickly 𝑙𝑜𝑔 𝑥 𝑙𝑜𝑔 𝑥 is written 𝑙𝑜𝑔 2 𝑥 It is greater than 𝑙𝑜𝑔 𝑥 for all 𝑥>2 It is not the same as 𝑙𝑜𝑔 𝑙𝑜𝑔 𝑥

Floor and Ceiling Floor function: the largest integer < X (to review on your own time) Floor function: the largest integer < X Ceiling function: the smallest integer > X

Floor and Ceiling Properties (to review on your own time)

Back to Big-O

O(n) O(n2) O(n + n/2) None of the above What’s the asymptotic runtime of this (semi-)pseudocode? x := 0; for i=1 to N do for j=1 to i do x := x + 3; return x; O(n) O(n2) O(n + n/2) None of the above (Some textbooks format algorithms in this style of semi-pseudocode)

How do we prove the right answer? Proof by Induction! What’s the asymptotic runtime of this (semi-)pseudocode? x := 0; for i=1 to N do for j=1 to i do x := x + 3; return x; O(n) O(n2) O(n + n/2) None of the above How do we prove the right answer? Proof by Induction!

Inductive Proofs (Interlude from Asymptotic Analysis)

Steps to Inductive Proof If not given, define n (or “x” or “t” or whatever letter you use) Base Case Inductive Hypothesis (IHOP): Assume what you want to prove is true for some arbitrary value k (or “p” or “d” or whatever letter you choose) Inductive Step: Use the IHOP (and maybe base case) to prove it's true for n = k+1

Example #0: Proof that I can climb any length ladder Let n = number of rungs on a ladder. Base Case: for n = 1 Inductive Hypothesis (IHOP): Assume true for some arbitrary integer n = k. Inductive Step: (aiming to prove it's true for n = k+1) By IHOP, I can climb k steps of the ladder. If I’ve climbed that far, I can always climb one more. So I can climb k+1 steps. I can climb forever!

Example #1 Prove that the number of loop iterations is 𝑛∗(𝑛+1 2 for i=1 to N do for j=1 to i do x := x + 3; return x; 𝑛∗(𝑛+1 2

(Extra room for notes)

Example #2: Prove that 1 + 2 + 4 + 8 + … + 2n = 2n+1 - 1

(Extra room for notes)

Useful Mathematical Property! 𝑖=0 𝑛 2 𝑖 = 2 𝑛+1 −1 You’ll use it or see it again before the end of CSE 373.

Example #3: (Parody) Reverse Induction Example #3: (Parody) Reverse Induction! Proof by Reverse Induction That You Can Always Cage a Lion: Let n = number of lions Base Case: There exists some countable, arbitrarily large value of M such that when n = M, the lions are so packed together that it's trivial to cage one. IHOP: Assume this is also true for n = k for some arbitrary value k. Inductive Step: Then for n = k-1, release a lion to reduce the problem to the case of n = k, which by the IHOP is true. QED :) Fun fact: Reverse induction is a thing! The math part of the above is actually correct.

Big-O: Recursive Functions How do we asymptotically analyze recursive functions?

Example #1: Towers of Hanoi

Example #1: Towers of Hanoi // Prints instructions for moving disks from one // pole to another, where the three poles are // labeled with integers "from", "to", and "other". // Code from rosettacode.org public void move(int n, int from, int to, int other) { if (n == 1) { System.out.println("Move disk from pole " + from + " to pole " + to);} else { move(n - 1, from, other, to); move(1, from, to, other); move(n - 1, other, to, from); }

Example #1: Towers of Hanoi if (n == 1) { System.out.println("Move disk from pole " + from + " to pole " + to);} else { move(n - 1, from, other, to); move(1, from, to, other); move(n - 1, other, to, from); }

Example #1: Solving the Recurrence Relation

(continued)

Example #2: Binary Search 3 5 16 37 50 73 75 126 Find an integer in a sorted array (Can also be done non-recursively) // Requires the array to be sorted. // Returns whether k is in array. public boolean find(int[]arr, int k){ return help(arr,k,0,arr.length); } private boolean help(int[]arr, int k, int lo, int hi) { int mid = (hi+lo)/2; // i.e., lo+(hi-lo)/2 if(lo==hi) return false; if(arr[mid]==k) return true; if(arr[mid]< k) return help(arr,k,mid+1,hi); else return help(arr,k,lo,mid);

What is the recurrence relation? // Requires the array to be sorted. // Returns whether k is in array. public boolean find(int[]arr, int k){ return help(arr,k,0,arr.length); } private boolean help(int[]arr, int k, int lo, int hi) { int mid = (hi+lo)/2; // i.e., lo+(hi-lo)/2 if(lo==hi) return false; if(arr[mid]==k) return true; if(arr[mid]< k) return help(arr,k,mid+1,hi); else return help(arr,k,lo,mid); 2T(n-1) + 3 T(n-1)*T(n-1) + 3 T(n/2) + 3 T(n/2) * T(n/2) + 3