Overview. A mathema5cal proof technique Proves statements about natural numbers 0,1,2,... (or more generally, induc+vely defined objects) Merge Sort

Similar documents
Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

Thinking Induc,vely. COS 326 David Walker Princeton University

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

CSE 20 DISCRETE MATH WINTER

ques4ons? Midterm Projects, etc. Path- Based Sta4c Analysis Sta4c analysis we know Example 11/20/12

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

Do you remember any iterative sorting methods? Can we produce a good sorting method by. We try to divide and conquer: break into subproblems

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

CS126 Final Exam Review

Local defini1ons. Func1on mul1ples- of

Fall Recursion and induction. Stephen Brookes. Lecture 4

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Unit #3: Recursion, Induction, and Loop Invariants

Lecture Notes on Quicksort

Prelim One Solution. CS211 Fall Name. NetID

Announcements. CS243: Discrete Structures. Strong Induction and Recursively Defined Structures. Review. Example (review) Example (review), cont.

Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009

Unit #2: Recursion, Induction, and Loop Invariants

Recall our recursive multiply algorithm:

Lecture Notes on Quicksort

Recursive Definitions Structural Induction Recursive Algorithms

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Recursion Chapter 3.5

COT 3100 Spring 2010 Midterm 2

Computer Programming

The divide and conquer strategy has three basic parts. For a given problem of size n,

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 7. Aykut Erdem, Erkut Erdem, Fuat Akal

Thinking Induc,vely. COS 326 David Walker Princeton University

ECE368 Exam 2 Spring 2016

Sorting. Task Description. Selection Sort. Should we worry about speed?

Lecture 15 : Review DRAFT

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Arguing for program correctness and writing correct programs

Lecture 6,

Mathematical Induction

Solutions to the Second Midterm Exam

Recursion and Structural Induction

Lecture 6: Sequential Sorting

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

CS 5614: (Big) Data Management Systems. B. Aditya Prakash Lecture #13: Mining Streams 1

15 150: Principles of Functional Programming Sorting Integer Lists

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

CSCI 136 Data Structures & Advanced Programming. Lecture 9 Fall 2018 Instructors: Bills

Array Basics: Outline

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

CS 206 Introduction to Computer Science II

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

We cover recursion in 150. Why do it again in 151?

To illustrate what is intended the following are three write ups by students. Diagonalization

CS473 - Algorithms I

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

CS 3512, Spring Instructor: Doug Dunham. Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010

Does this program exist? Limits of computability. Does this program exist? A helper function. For example, if the string program is

(Refer Slide Time: 01.26)

Analyzing Complexity of Lists

ESc101 : Fundamental of Computing

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Lecture 6: Divide-and-Conquer

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

Solutions to Problem Set 1

Cisc320 Homework 3 Solutions. Question 1: Original. Insert 170. Insert 34. TA: Matt Saponaro

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

COMP250: Loop invariants

Problem Set 4 Solutions

Introduction to Data Mining

UNIT 5C Merge Sort. Course Announcements

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8

Problem solving paradigms

CS171 Midterm Exam. October 29, Name:

CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

RECURSION. Many Slides from Ken Birman, Cornell University

Array Basics: Outline

Programming Languages 3. Definition and Proof by Induction

Section 1.4 Proving Conjectures: Deductive Reasoning

Sorting L7.2 Recall linear search for an element in an array, which is O(n). The divide-and-conquer technique of binary search divides the array in ha

CSCI 2132 Software Development Lecture 18: Implementation of Recursive Algorithms

ESc101 : Fundamental of Computing

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

CSE 373: Data Structures & Algorithms More Sor9ng and Beyond Comparison Sor9ng

Recursion: The Beginning

Lecture 1. 1 Notation

CS201 Discussion 7 MARKOV AND RECURSION

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Array Basics: Outline

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

CSE 373: Data Structures and Algorithms

Data Structures and Algorithms Running time, Divide and Conquer January 25, 2018

Algorithmic Analysis and Sorting, Part Two

Announcements. Problem Set 3 is due this Tuesday! Midterm graded and will be returned on Friday during tutorial (average 60%)

Lecture 8: Mergesort / Quicksort Steven Skiena

Transcription:

Goals for Today Induc+on Lecture 22 Spring 2011 Be able to state the principle of induc+on Iden+fy its rela+onship to recursion State how it is different from recursion Be able to understand induc+ve proofs Iden+fy the reason why induc+on is necessary Follow most important steps of the proof Be able to construct simple induc+ve proofs More of this to come next lecture, discussion 1 2 Recursion A programming/algorithm strategy Overview Solves a problem by reducing it to simpler or smaller instance(s) of the same problem Induc5on A mathema5cal proof technique Proves statements about natural numbers 0,1,2,... (or more generally, induc+vely defined objects) Closely related, but different How do we know this is true? Or that this is true? Merge Sort private void merge(t[] x, int lo, int mid, int hi, T[] y) { // one of the subarrays is empty 3 4 Is this s+ll true? How about this? int mid = (lo lo+1; hi)/2; Merge Sort private void merge(t[] x, int lo, int mid, int hi, T[] y) { // one of the subarrays is empty Merge Sort Recursion: The strategy you used to perform the sor+ng Result is algorithm/program Induc5on: How you show that the the program actually sorts Also, how you show it has O(n log n) performance Result is a proof/argument Guides the Process 5 6 1

Simpler Example: Sum of Integers We can describe a func+on in different ways S(n) = the sum of the integers from 0 to n S(0) = 0,, S(3) = 0+1+2+3 = 6, Itera5ve Defini5on n S(n) = 0+1+... + n = Σ i=0 i Closed form characteriza5on S C (n) = n(n+1)/2 Are S(n) and S C (n) the same func+on? Our claim must be a property of the natural numbers is a statement with variable n Write as P(n) allows (numeric) values to be subs+tuted for n P(0), P(1), P(2), For each number n, P(n) is either true or false What are We Proving? Examples P(n): The number n is even P(n): Number n is even or odd P(n): S(n) = S C (n) P(n): MergeSort sorts any given array P(n): MergeSort sorts any given array of length n P(n): On any given array of length n, MergeSort finishes in less than c (n log n) steps 7 8 Are These Func+ons the Same? Are the same if same inputs give same outputs Property P(n): S(n) = S C (n) Test some values and see if work S(0) = 0, S C (0) = 0(1/2) = 0 S(1) = 0+1 = 1, S C (1) = 1(2/2) = 1 S(2) = 0+1+2 = 3, S C (2) = 2(3/2) = 3 S(3) = 0+1+2+3 = 6, S C (3) = 3(4/2) = 6 This approach will never be complete, as there are infinitely many n to check 9 Recursive Defini+on Let s formulate S(n) in yet another way: S(n) = 0 + 1 + 2 + + n- 1 + n this is S(n- 1) This gives us a recursive defini+on: S R (0) = 0 Base Case S R (n) = S R (n- 1) + n, n > 0 Recursive Case Example: S R (4) = S R (3) + 4 = S R (2) + 3 + 4 = S R (1) + 2+ 3 + 4= S R (0) + 1 + 2 + 3 + 4 = 0 + 1 + 2 + 3 + 4 10 An Intermediate Problem Are these func+ons the same? Recursive defini5on: S R (0) = 0 S R (n) = S R (n- 1) + n, n > 0 Closed form characteriza5on: S C (n) = n(n+1)/2 Property P(n): S R (n) = S C (n) Induc+on over Natural Numbers Goal: Prove property P(n) holds for n 0 1. Base Step: Show that P(0) is true Induc+ve Hypothesis 2. Induc+ve Step: Assume P(k) true for an unspecified integer k Use assump+on to show that P(k+1) is true Conclusion: Because we could have picked any k, we conclude P(n) holds for all integers n 0 11 12 2

0 1 2 3 4 Dominoes Assume equally spaced dominos, where spacing between dominos is less than domino length. Want to argue that all dominoes fall: Domino 0 falls because we push it over Domino 0 hits domino 1, therefore domino 1 falls Domino 1 hits domino 2, therefore domino 2 falls Domino 2 hits domino 3, therefore domino 3 falls... What is a beser way to make this argument? Repe++ve argument. Requires one sentence per domino. 13 A Beser Argument Argument: (Base Step) Domino 0 falls because we push it over (Induc+ve Hypothesis) Assume domino k falls over (Induc+ve Step) Because domino k s length is larger than the spacing, it will knock over domino k+1 (Conclusion) Because we could have picked any domino to be the kth one, the dominoes will fall over This is an induc+ve argument Much more compact than example from last slide Works for an arbitrary number of dominoes! 14 P(0) S R (n) = S C (n) for all n? P(1) P(2) P(k) P(k+1) Property P(n): S R (n) = S C (n) Base Step: Prove P(0) using the defini+on Induc+ve Hypothesis (IH): Assume that P(k) holds for unspecified k Induc+ve Step: Prove that P(k+1) is true using IH and the defini+on 15 Recall: S R (0) = 0, S R (n) = S R (n- 1) + n, n > 0 S C (n) = n(n+1)/2 Proof (by Induc+on) Property P(n): S R (n) = S C (n) Base Step: S R (0) = 0 and S C (0) = 0, both by defini+on Induc+ve Hypothesis: Assume S R (k) = S C (k) Induc+ve Step: S R (k+1) = S R (k) + (k+1) Defini+on of S R (k+1) = S C (k) + (k+1) Induc+ve Hypothesis = k(k+1)/2 + (k+1) Defini+on of S C (k) = [k(k+1)+2(k+1)]/2 = (k+1)(k+2)/2 Algebra = S C (k+1) Defini+on of S C (k+1) Conclusion: S R (n) = S C (n) for all n 0 16 Our Original Problem S(n) = the sum of the integers from 0 to n S(0) = 0,, S(3) = 0+1+2+3 = 6, Itera5ve Defini5on n S(n) = 0+1+... + n = Σ i=0 i Closed form characteriza5on S C (n) = n(n+1)/2 Property P(n): S(n) = S C (n) Did we show this? Finishing the Proof Can just show that S(n) = S R (n) For some, this is a convincing argument: S(n) = 0 + 1 + 2 + + n- 1 + n this is S(n- 1) Can also do another induc+ve proof Or could have worked it into our original proof Old P(n): S(n) = S C (n) Implies New P(n): S(n) = S R (n) = S C (n) Recursive Go- Between 17 18 3

Recall: A Complete Argument S(n) = 0 + 1 + + n S R (0) = 0, S R (n) = S R (n- 1) + n, n > 0 S C (0) = n(n+1)/2 Property P(n): S(n) = S R (n) = S C (n) Base Step: S(0) = 0 and S R (0) = 0 and S R (0) = 0, all by defini+on Induc+ve Hypothesis: Assume S(k) = S R (k) = S C (k) Induc+ve Step: First prove S(k+1) = S R (k+1) S(k+1) = 0 + 1 + + k + (k+1) Defini+on of S(k+1) = S(k) + (k+1) Defini+on of S(k) = S R (k) + (k+1) Induc+ve Hypothesis = S R (k+1) Defini+on of S R (k+1) Recall: A Complete Argument S(n) = 0 + 1 + + n S R (0) = 0, S R (n) = S R (n- 1) + n, n > 0 S C (0) = n(n+1)/2 Property P(n): S(n) = S R (n) = S C (n) Induc+ve Step (Con+nued): Now prove S R (k+1) = S C (k+1) S R (k+1) = S R (k) + (k+1) Defini+on of S R (k+1) = S C (k) + (k+1) Induc+ve Hypothesis = k(k+1)/2 + (k+1) Defini+on of S C (k) = [k(k+1)+2(k+1)]/2 = (k+1)(k+2)/2 Algebra = S C (k+1) Defini+on of S C (k+1) Conclusion: S(n) = S R (n) = S C (n) for all n 0 19 20 Induc+on Requires Recursion Either a recursive algorithm is provided Induc+on used to prove property of algorithm Example: Correctness of MergeSort Or you must construct a recursive algorithm May not be an actual program; could be a recursive func+on, or abstract process Example: Our recursive go- between for S(n), S C (n) Ouen call this the induc+ve strategy Remember Algorithm or strategy: recursion Proof argument: induc+on Recursion to be used in a proof only Example With No (Ini+al) Recursion Claim: Can make any amount of postage above 8 with some combina+on of 3 and 5 stamps 5 Property P(n): You can make n of postage from some combina+on of 3 and 5 stamps Induc+on: Prove that it can be done Recursion: A strategy that computes the number of 3, 5 stamps needed 3 21 22 Induc+ve Strategy Recursive A Note on Base Step P(0) P(1) P(2) P(3) P(4) P(k) P(k+1) return one 3, one 5 else { Compute answer for (n- 1) if (q > 0) { // If there is a 5 stamp, replace with two 3 ones return p+2 3 stamps, q- 1 5 stamps else { // If no 5 stamp, must be at least three 3 ones return p- 3 3 stamps, q+2 5 stamps Some+mes want to show a property is true for integers b Intui5on: Knock over domino b, and dominoes in front get knocked over Not interested in 0, 1,..., (b- 1) In general, the base step in induc+on does not have to be 0 If base step is some integer b Induc+on proves the proposi+on for n = b, b+1, b+2,... Does not say anything about n = 0, 1,..., b- 1 23 24 4

Induc+on: Base Step n = Returns: 8 amount of 3 and amount of 5 stamps Base Step: 3 +5 = 8 return one 3, one 5 else { Compute answer for (n- 1) if (q > 0) { // If there is a 5 stamp, replace with two 3 ones return p+2 3 stamps, q- 1 5 stamps else { // If no 5 stamp, must be at least three 3 ones return p- 3 3 stamps, q+2 5 stamps 25 Induc+on: Induc+ve Step n = (k+1) return one 3, one 5 IH: (3p) +(5q) = k else { Compute answer for (n- 1) Induc+ve Step: (3p+6) +(5q- 5) = (3p) +(5q) +1 if (q > 0) { // If there is a 5 stamp, replace with two = 3 (k+1) ones return p+2 3 stamps, q- 1 5 stamps else { // If no 5 stamp, must be at least three 3 ones return p- 3 3 stamps, q+2 5 stamps Induc+ve Step: (3p- 9) +(5q+10) = (3p) +(5q) +1 = (k+1) 26 Cleaning it Up: Induc+ve Proof Claim: You can make any amount of postage above 8 with some combina+on of 3 and 5 stamps Base Step: It is true for 8, because 8 = 3 + 5 Induc+ve Hypothesis: Suppose true for some k 8 Induc+ve Step: If we used a 5 stamp to make k, we replace it by two 3 stamps. This gives k+1 If did not use a 5 stamp to make k, we must have used at least three 3 stamps. Replace three 3 stamps by two 5 stamps. This gives k+1. Conclusion: Any amount of postage above 8 can be made with some combina+on of 3 and 5 stamps 27 Alternate Induc+ve Strategy return one 3, one 5 stamp else if (n == 9) { return three 3 stamps else if (n == 10) { return two 5 stamps else { Compute answer for (n- 3) return p+1 3 stamps, q 5 stamps Recursive 28 Strong Induc+on Weak induc5on P(0): Show that property P is true for 0 P(k) => P(k+1): Show that if property P is true for k, it is true for k+1 Conclude that P(n) holds for all n Strong induc5on P(0),, P(m): Show property P is true for 0 to m P(0) and P(1) and... and P(k) => P(k+1): Show that if P is true for numbers less than or equal to k, then it is true for k+1 Conclude that P(n) holds for all n Both proof techniques are equally powerful 29 Strong Induc+on: Base Step n = 8, 9, 10 Base Step (part 1): 3 +5 = 8 return one 3, one 5 stamp else if (n == 9) { Base Step (part 2): 3 +3 +3 = 9 return three 3 stamps else if (n == 10) { return two 5 stamps Base Step (part 3): 5 +5 = 10 else { Compute answer for (n- 3) return p+1 3 stamps, q 5 stamps 30 5

Strong Induc+on: Induc+ve Step n = Returns: k+1 amount of 3 and amount of 5 stamps return one 3, one 5 stamp else if (n == 9) { return three 3 stamps else if (n == 10) { return two 5 stamps else { Compute answer for (n- 3) return p+1 3 stamps, q 5 stamps Strong Induc+on Hypothesis: Strategy works for any amount of postage m, where 8 m k SIH: (3p) +(5q) = (k- 2) Induc+ve Step: (3p+3) +(5q) = (k+1) 31 Clean Up: Strong Induc+ve Proof Claim: You can make any amount of postage above 8 with some combina+on of 3 and 5 stamps Base Step: We consider three base cases: 8, 9, and 10 It is true for 8, since 3+5 = 8 It is true for 9, since 3+3+3 = 9 It is true for 10, since 5+5 = 10 (Strong) Induc+ve Hypothesis: Suppose there is some k such that claim is true for all numbers m, where 8 m k Induc+ve Step: As 8 k- 2 k, make postage for (k- 2) and add a 3 stamp. This gives answer for (k+1). Conclusion: Any amount of postage above 8 can be made with some combina+on of 3 and 5 stamps 32 Merge Sort: Correctness Idea Property P(n): * For (inclusive) any array and of hi (exclusive), length n as recursively input, and private Any array void merge(t[] of x, int lo, int mid, int hi, * mergesort() in O(n log n) +me sorts the contents in place. length 0 or 1 T[] y) { Base Step: Array of 1 or 0 elements is already sorted // one of the subarrays is empty 33 Merge Sort: Correctness Idea * Property Sorts the Comparable P(n): array x between lo * For (inclusive) any array and of hi (exclusive), length n as recursively input, and * mergesort() in O(n log n) +me sorts the contents in place. private Any array void merge(t[] of x, int lo, int mid, int hi, length k+1 T[] y) { Strong Induc+ve Hypothesis: For any array // of one length of the m subarrays k, is empty mergesort() sorts the array Induc+ve Step: Show that merge() takes two sorted halves and produces a single sorted array of length k+1 34 Induc+on and Recursion Recursion: A strategy used to solve a problem or compute result Result is algorithm/program Summary of Today Induc+on is a technique to prove statements Recursion is a strategy to construct algorithms Useful for program correctness and complexity Provides the Induc+ve Strategy Induc5on: Prove statements about the natural numbers Prove proper+es of recursive algorithms Result is a proof/argument Guides the Algorithm Development But all induc+on requires a recursive strategy Hard part is finding the strategy Auerwards, induc+on is ouen straighzorward Different varia+ons of induc+on exist to tailor to your recursive strategy 35 36 6

To Think About for Next Time int mid = (lo lo+1; hi)/2; private void merge(t[] x, int lo, int mid, int hi, T[] y) { What does this do // one of the subarrays is empty to complexity? 37 7