CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction & Algorithm Analysis Lauren Milne Summer 2015.

Slides:



Advertisements
Similar presentations
Recursive Definitions and Structural Induction
Advertisements

Analysis of Algorithms
Proofs, Recursion and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Induction and recursion
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Dan Grossman Spring 2010.
Discrete Structures Chapter 5: Sequences, Mathematical Induction, and Recursion 5.2 Mathematical Induction I [Mathematical induction is] the standard proof.
Chapter 10 Sequences, Induction, and Probability Copyright © 2014, 2010, 2007 Pearson Education, Inc Mathematical Induction.
1 Section 3.3 Mathematical Induction. 2 Technique used extensively to prove results about large variety of discrete objects Can only be used to prove.
CSE115/ENGR160 Discrete Mathematics 03/29/11 Ming-Hsuan Yang UC Merced 1.
analysis, plug ‘n’ chug, & induction
Induction and recursion
MATHEMATICS INDUCTION AND BINOM THEOREM By : IRA KURNIAWATI, S.Si, M.Pd.
Algorithm Design and Analysis (ADA)
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Recursive Definitions and Structural Induction
Induction and recursion
Chapter 6 Mathematical Induction
Mathematics Review Exponents Logarithms Series Modular arithmetic Proofs.
College Algebra Fifth Edition James Stewart Lothar Redlin Saleem Watson.
CSE373: Data Structures and Algorithms Lecture 2b: Proof by Induction and Powers of Two Nicki Dell Spring 2014.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!
CS 3343: Analysis of Algorithms
Chapter 4: Induction and Recursion
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Math Review Data Structures & File Management Computer Science Dept Va Tech July 2000 ©2000 McQuain WD 1 Summation Formulas Let N > 0, let A, B, and C.
9.4 Mathematical Induction
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.
Proofs, Induction and Recursion Basic Proof Techniques Mathematical Induction Recursive Functions and Recurrence Relations.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Nicki Dell Spring 2014.
Section 3.3: Mathematical Induction Mathematical induction is a proof technique that can be used to prove theorems of the form:  n  Z +,P(n) We have.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
CompSci 102 Discrete Math for Computer Science March 1, 2012 Prof. Rodger Slides modified from Rosen.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield.
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.
8.4 Mathematical Induction Copyright © by Houghton Mifflin Company, Inc. All rights reserved. 2 Recursively defined sequences Write the first 5.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction Linda Shapiro Winter 2015.
Mathematical Induction
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Chapter 5. Section 5.1 Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If we can reach.
Mathematical Induction Thinking Skill: Develop Confidence in Reason Warm Up: Find the k+1 term (P k+1 ) 1) 2)
Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If we can reach a particular.
1 Discrete Mathematical Mathematical Induction ( الاستقراء الرياضي )
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
1 Recursive Definitions and Structural Induction CS/APMA 202 Rosen section 3.4 Aaron Bloomfield.
Mathematical Induction EECS 203: Discrete Mathematics Lecture 11 Spring
Mathematical Induction I Lecture 19 Section 4.2 Mon, Feb 14, 2005.
Chapter 5 1. Chapter Summary  Mathematical Induction  Strong Induction  Recursive Definitions  Structural Induction  Recursive Algorithms.
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Kevin Quinn Fall 2015.
Instructor: Lilian de Greef Quarter: Summer 2017
CSE15 Discrete Mathematics 03/22/17
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
CSE 311 Foundations of Computing I
Induction and recursion
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction
CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction
CS201: Data Structures and Discrete Mathematics I
CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis Dan Grossman Fall 2013.
Induction and recursion
CSE 311: Foundations of Computing
CSE 373: Data Structures & Algorithms
Mathematical Induction
Mathematical Induction
Mathematical Induction
Mathematical Induction
Presentation transcript:

CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction & Algorithm Analysis Lauren Milne Summer 2015

Today Did everyone get email sent on Monday about TA Sections starting on Thursday? Homework 1 due 10:59pm next Wednesday, July 1st. Review math essential to algorithm analysis Proof by induction Exponents and logarithms Floor and ceiling functions Begin algorithm analysis

Homework 1 Download Eclipse and Java Implement Stack ADT for Java double Array ArrayStack: push(double n), pop(), peek(), constructor Starts small (approx. 10 elements) and doubles in size when full, copy elements over Throw exception if stack is empty for pop() and peek() List ListStack: push(double n), pop(), peek(), constructor Inner ListStackNode class Test Reverse.java and Dstack.java Using .dat sound files (created using .wav files through sox), and .dat files you create manually (edge cases) Write up: README.txt QueueStack – push() and pop()

Background on Induction Type of mathematical proof Typically used to establish a given statement for all natural numbers (integers > 0) Proof is a sequence of deductive steps Show the statement is true for the first number. Show that if the statement is true for any one number, this implies the statement is true for the next number. If so, we can infer that the statement is true for all numbers.

Think about climbing a ladder 1. Show you can get to the first rung (base case) 2. Show you can get between rungs (inductive step) dominos? if you can knock down the first one, and prove that each domino will knock down another domino 3. Now you can climb forever.

Why you should care Induction turns out to be a useful technique AVL trees Heaps Graph algorithms Can also prove things like 3n > n3 for n ≥ 4 Exposure to rigorous thinking

Example problem Find the sum of the integers from 1 to n 1 + 2 + 3 + 4 + … + (n-1) + n For any n ≥ 1 Could use brute force, but would be slow There’s probably a clever shortcut

Finding the formula Shortcut will be some formula involving n Compare examples and look for patterns Not something I will ask you to do! Start with n = 10: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Large enough to be a pain to add up Worthwhile to find shortcut

Finding the formula = 5×11 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 how many pairs? what’s the sum? = 5×11

Finding the formula 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 4×10 + 5

Finding the formula 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 4×9

Finding the formula 1 + 2 + 3 + 4 + 5 + 6 + 7 = 3×8 + 4

Finding the formula n=7 3×8 + 4 n=8 4×9 n=9 4×10 + 5 n=10 5×11

n=7 3×8 + 4 n is odd n=8 4×9 n is even n=9 4×10 + 5 n=10 5×11 Finding the formula n=7 3×8 + 4 n is odd n=8 4×9 n is even n=9 4×10 + 5 n=10 5×11

Finding the formula = (n/2)×(n+1) When n is even 1 + 2 + 3 + 4 + … + (n-3) + (n-2) + (n-1) + n = (n/2)×(n+1)

Finding the formula 3×8 + 4 4×9 n(n+1)/2 4×10 + 5 5×11

Finding the formula = ((n-1)/2)×(n+1) + (n+1)/2 When n is odd 1 + 2 + 3 + 4 + … + (n-3) + (n-2) + (n-1) + n = ((n-1)/2)×(n+1) + (n+1)/2

Finding the formula = ((n-1)/2)×(n+1) + (n+1)/2 When n is odd 1 + 2 + 3 + 4 + … + (n-3) + (n-2) + (n-1) + n = ((n-1)/2)×(n+1) + (n+1)/2

Finding the formula = ((n-1)×(n+1) + (n+1))/2 When n is odd 1 + 2 + 3 + 4 + … + (n-3) + (n-2) + (n-1) + n = ((n-1)×(n+1) + (n+1))/2

Finding the formula = ((n-1)×(n+1) + (n+1))/2 When n is odd 1 + 2 + 3 + 4 + … + (n-3) + (n-2) + (n-1) + n = ((n-1)×(n+1) + (n+1))/2

Finding the formula = ((n-1 + 1)×(n+1))/2 When n is odd 1 + 2 + 3 + 4 + … + (n-3) + (n-2) + (n-1) + n = ((n-1 + 1)×(n+1))/2

Finding the formula = ((n-1 + 1)×(n+1))/2 When n is odd 1 + 2 + 3 + 4 + … + (n-3) + (n-2) + (n-1) + n = ((n-1 + 1)×(n+1))/2

Finding the formula = (n (n+1))/2 When n is odd 1 + 2 + 3 + 4 + … + (n-3) + (n-2) + (n-1) + n = (n (n+1))/2

Finding the formula 3×8 + 4 n(n+1)/2 4×9 4×10 + 5 5×11

Are we done? The pattern seems pretty clear Is there any reason to think it changes? But we want something for any n ≥ 1 A mathematical approach is skeptical

Are we done? The pattern seems pretty clear Is there any reason to think it changes? But we want something for any n ≥ 1 A mathematical approach is skeptical All we know is n(n+1)/2 works for 7 to 10 We must prove the formula works in all cases

Proof by induction Prove the formula works for all cases. Induction proofs have four components: Relationship that you want to prove, e.g., sum of integers from 1 to n = n(n+1)/2 The base case (usually "let n = 1"), The assumption step (“assume true for n = k") The induction step (“now let n = k + 1"). n and k are just variables! In our ladder metaphor, steps 3 and 4 mean, assume that we can get to step k on the ladder, can you show we can get to step k+1?

Proof by induction P(n) = sum of integers from 1 to n We need to do Base case Assumption Induction step n and k are just variables! prove for P(1) assume for P(k) show for P(k+1)

Proof by induction P(n) = sum of integers from 1 to n We need to do Base case Assumption Induction step prove for P(1) assume for P(k) show for P(k+1) 1 2 3 4 5 6…

✓ Proof by induction What we are trying to prove: P(n) = n(n+1)/2 Base case P(1) = 1 1(1+1)/2 = 1(2)/2 = 1(1) = 1 ✓

Proof by induction What we are trying to prove: P(n) = n(n+1)/2 Assume true for k: P(k) = k(k+1)/2 Induction step: Now consider P(k+1) = 1 + 2 + … + k + (k+1)

Proof by induction What we are trying to prove: P(n) = n(n+1)/2 Assume true for k: P(k) = k(k+1)/2 Induction step: Now consider P(k+1) = 1 + 2 + … + k + (k+1) = k(k+1)/2 + (k+1)

Proof by induction What we are trying to prove: P(n) = n(n+1)/2 Assume true for k: P(k) = k(k+1)/2 Induction step: Now consider P(k+1) = 1 + 2 + … + k + (k+1) = k(k+1)/2 + (k+1) = k(k+1)/2 + 2(k+1)/2

Proof by induction What we are trying to prove: P(n) = n(n+1)/2 Assume true for k: P(k) = k(k+1)/2 Induction step: Now consider P(k+1) = 1 + 2 + … + k + (k+1) = k(k+1)/2 + (k+1) = k(k+1)/2 + 2(k+1)/2 = (k(k+1) + 2(k+1))/2

Proof by induction What we are trying to prove: P(n) = n(n+1)/2 Assume true for k: P(k) = k(k+1)/2 Induction step: Now consider P(k+1) = 1 + 2 + … + k + (k+1) = k(k+1)/2 + (k+1) = k(k+1)/2 + 2(k+1)/2 = (k+1)(k+2)/2 = (k(k+1) + 2(k+1))/2

Proof by induction What we are trying to prove: P(n) = n(n+1)/2 Assume true for k: P(k) = k(k+1)/2 Induction step: Now consider P(k+1) = 1 + 2 + … + k + (k+1) = k(k+1)/2 + (k+1) = k(k+1)/2 + 2(k+1)/2 = (k+1)(k+2)/2 = (k(k+1) + 2(k+1))/2

✓ Proof by induction What we are trying to prove: P(n) = n(n+1)/2 Assume true for k: P(k) = k(k+1)/2 Induction step: Now consider P(k+1) = 1 + 2 + … + k + (k+1) = k(k+1)/2 + (k+1) = k(k+1)/2 + 2(k+1)/2 = (k+1)(k+2)/2 ✓ = (k(k+1) + 2(k+1))/2 = (k+1)((k+1)+1)/2

✓ Proof by induction What we are trying to prove: P(n) = n(n+1)/2 Assume true for k: P(k) = k(k+1)/2 Induction step: Now consider P(k+1) = 1 + 2 + … + k + (k+1) = k(k+1)/2 + (k+1) = k(k+1)/2 + 2(k+1)/2 = (k+1)(k+2)/2 ✓ = (k(k+1) + 2(k+1))/2 = (k+1)((k+1)+1)/2

We’re done! P(n) = sum of integers from 1 to n We have shown Base case Assumption Induction step Success: we have proved that P(n) is true for any n ≥ 1  proved for P(1) assumed for P(k) proved for P(k+1)

Another one to try What is the sum of the first n powers of 2? For general n, the sum is 2n+1 - 1 have them work on this on their own: identify pattern first, then figure out induction

How to prove it P(n) = “the sum of the first n powers of 2 is 2n+1-1” Theorem: P(n) holds for all n ≥ 0 Proof: By induction on n Base case: n=0. Sum of first power of 2 is 20 , which equals 1 = 21 - 1. Inductive case: Assume the sum of the first k powers of 2 is 2k+1-1 Show the sum of the first (k+1) powers of 2 is 2k+2-1

How to prove it The sum of the first k+1 powers of 2 is 20 + 21 + 22 + ... + 2(k-1) + 2k + 2k+1 = 2k+1 -1 + 2k+1 = 2(2k+1) -1 = 2k+2 - 1

Conclusion Mathematical induction is a technique for proving something is true for all integers starting from a small one, usually 0 or 1. A proof consists of three parts: 1. Prove it for the base case. 2. Assume it for some integer k. 3. With that assumption, show it holds for k+1 It can be used for complexity and correctness analyses.

End of Inductive Proofs!

Powers of 2 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 0 through 2n-1 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 what is “max int”? 2^32 / 2 = 2^31 = 2 *2^30 = 2 billion

Powers of 2 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 0 through 2n-1 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: a long is 64 bits and signed, so what is max long? 263-1

Therefore… 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? US population = 320 million World pop = 7.2 billion

Logarithms and Exponents Definition: x = 2y if log2 x = y 8 = 23, so log2 8 = 3 65536= 216, so log2 65536 = 16 The exponent of a number says how many times to use the number in a multiplication. e.g. 23 = 2 × 2 × 2 = 8 (2 is used 3 times in a multiplication to get 8) A logarithm says how many of one number to multiply to get another number. It asks "what exponent produced this?” e.g. log28 = 3 (2 makes 8 when used 3 times in a multiplication)

Logarithms and Exponents Definition: x = 2y if log2 x = y 8 = 23, so log2 8 = 3 65536= 216, so log2 65536 = 16 Since so much is binary in CS, log almost always means log2 log2 n tells you how many bits needed to represent n combinations. So, log2 1,000,000 = “a little under 20” Logarithms and exponents are inverse functions. Just as exponents grow very quickly, logarithms grow very slowly.

Logarithms and Exponents See Excel file for plot data – play with it! what is this 2^n n

Logarithms and Exponents See Excel file for plot data – play with it! which are crossing? n^2 and 2^n n

Logarithms and Exponents See Excel file for plot data – play with it! n, log(n) n