Vous êtes sur la page 1sur 33

Proof Techniques

Recursion
C++ Review

Proof by induction
Proof by Contradiction

Data Structures
Preliminaries
Hikmat Farhat

October 1, 2013

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

Proof by induction
Proof by Contradiction

Proofs

Two of the most common methods for proving statements in


data structure are
I
I

Proof by induction
Proof by contradiction

To prove that is statement is incorrect it is sufficient to


provide a counterexample

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

Proof by induction
Proof by Contradiction

Proof by Induction

A proof by induction has two parts


I

base case where the statement is shown to be true for some


specific case.
inductive step using an inductive hypothesis: the statement is
assume to be true for all values up to some limit k then shown
to be true for k + 1.

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

Proof by induction
Proof by Contradiction

Example of proof by induction


I

I
I
I
I

Prove that the Fibonacci numbers F0 = 1,


F1 = 1,F2 = 2 . . . Fi = Fi1 + Fi2 satisfy that Fi < (5/3)i for
all i.
Proof
Base case F1 = 1 < (5/3)
Induction hypothesis: Assume that Fi < (5/3)i for i k.
Induction step:
Fk+1 < (5/3)k + (5/3)k1
k+1

< (3/5)(5/3)

by hypothesis
2

k+1

+ (3/5) (5/3)

< (24/25)(5/3)k+1
< (5/3)k+1
Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

Proof by induction
Proof by Contradiction

Example 1

n is even if and only if n2 is even.

Proof. n is even n2 is even.


n is even n = 2m
n2 = 4m2
n2 = 2(2m2 )
n2 is even

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

Proof by induction
Proof by Contradiction

n2 is even n is even .
I the above means that for all n if n2 is even then n is even.
I We can prove that it is true by assuming that it is false and
reach a contradiction.
I The negation of the above is: n odd such that n2 is even.
n odd n = 2k + 1
n2 = 4k 2 + 4k + 1
n2 = 2(2k 2 + 2k) + 1
n2 is odd
I

But we assumed that n2 is even, a contradiction thus there


does NOT exist an n odd with n2 even.
Combining the above with previous slide we get
n even n2 even
Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

Proof by induction
Proof by Contradiction

Example 2: 2 is irrational.
Proof. Assume that

2 is rational. This means that

2=

m
gcd(m, n) = 1
n
m, n cannot be BOTH even

2 rational

2=

On the other hand, squaring both sides we get


n2 2 = m2
Thus by previous result m is even, let m = 2k. Then
n2 2 = 4k 2 n2 = 2k 2 n is even. Contradiction

Hikmat Farhat

Data Structures

m
n.

Proof Techniques
Recursion
C++ Review

Recursive definition of functions

Most mathematical functions are given by a simple formula


for example
C = 5(F 32)/9

Sometimes it is easier to define a function recursively


f (0) = 0
f (x) = 2f (x 1) + x 2

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

Recursion cont.
I

What is f (4) ? according to the definition


f (4) = 2f (3) + 42

= 2 2f (2) + 32 + 42


= 2 2 2f (1) + 22 + 32 + 42



= 2 2 2 2f (0) + 12 + 22 + 32 + 42



= 2 2 2 0 + 1 2 + 22 + 3 2 + 4 2
because f (0) = 0


= 2 2 2 + 2 2 + 32 + 42

= 2 12 + 32 + 42
= 42 + 16
= 58
Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ implementation

1
2
3
4
5
6

i n t f ( i n t x ){
i f ( x==0)
// b a s e c a s e
return 0;
else
r e t u r n 2 f ( x1)+x x ;
}

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

Example of bad recursion

1
2
3
4
5

i n t bad ( i n t n ) {
i f ( n==0)
return 0;
else
r e t u r n bad ( n/3+1)+n 1;
I

What is bad(1)? Integer arithmetic: 1/3=0

What is bad(2),bad(3)...

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

Rules of Recursion

Base cases: you must always have some base cases which can
be solved without recursion.

Progress: recursive calls must always make progress toward


the base case

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

Another Example

1
2
3
4

Suppose we need to print an arbitrary decimal number and we


have access to function that prints single digits: printDigit(int
d)

Can we use printDigit() to print an arbitrary decimal number?

Observation: to print 243 we need to print 24 then print 3.

to print 24 we need to print 2 then 4.

This is basically a recursive algorithm.

i n t p ri nt Ou t ( i n t x ){
i f ( x >=10)
printOut ( x /10);
p r i n t D i g i t ( x %10);

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

Correctness Proof
I

The recursive printing algorithm is correct. Proof by induction

Base case: if the input is a single digit (i.e. <10) there is a


single call to printDigit which is correct.

Hypothesis.
with up to k
we can write
number with

calling printOut on M will execute the sequence of two


statements

Assume that printOut works for any number


digits. Given a number with M with k + 1 digits
is as M = d1 . . . dk dk+1 = Ndk+1 where N is a
k digits.

printOut(bM/10c) this by hypothesis prints N correctly


printOut(M%10) this prints dk+1
Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Class definition
I

A C++ class is a collection of members


I
I

data
functions (or methods)

class IntCell {
public :
I n t C e l l (){
s t o r e d V a l u e =0;
}
I n t C e l l ( i n t i n i t V a l u e ){
s t o r e d V a l u e=i n i t V a l u e ;
}
i n t getValue (){
return storedValue ;
}
v o i d s e t V a l u e ( i n t v a l ){
s t o r e d V a l u e=v a l ;
}
private :
int storedValue ;
};

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Variations
I

It is important to recall that the body of the constructor is


executed, after all member elements are initialized. In
particular if another class is a member function.

For this reason sometimes it is helpful to initialize the


constructor as follows

class IntCell{
public :
I n t C e l l ( i n t i n i t V a l u e =0 , i n t h v a l =0): s t o r e d V a l u e ( i n i t V a l u e ) , h e i g h t ( h v a l ){
}
private :
int storedValue ;
const i nt height ;
/ i n n e w e r v e r s i o n d a t a members can be i n i t i a l i z e d , f o r e x a m p l e :
c o n s t i n t h e i g h t =10;
/
};

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Mutators and Accessors


I

A member function that does not change the state of its


object is called an accessor.

A member function that changes the state of its object is


called an mutator.

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Separarting Interface and Implementation:Interface

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Separarting Interface and Implementation:Implementation

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Pointers

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

vector and string

the C++ standard defines the vector class that replaces


arrays.

vector behaves as a first class object: we can copy using =,


determine the number of elements and check the out of
bounds condition.

Similarly the old string is just an array of characters so we


cannot use == to determine if two strings are equal.

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Using vector class

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Parameter Passing

Three types of parameter passing


1. Call by value: a copy of the variable is passed to the function
2. Call by reference: a reference to the variable is passed.
3. Call by constant reference: a reference to an immutable
variable is passed
d o u b l e avg ( c o n s t Type & a r r , i n t n , b o o l & e r r o r F l a g )

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Big Three

All C++ classes have three member functions that are written
for us
I
I
I

Destructor
Copy constructor
operator=

On many occasions these implicit defaults are enough

Usually when pointers are involved the defaults do not work


because the defaults perform a shallow copy

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Big three implicitly defined

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Problems

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

What is the output?

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Need for generic operations

Suppose we need to find the largest item in a list of items.

a simple solution is to read, sequentially, all the items and


keep track of the largest by comparing items to the current
largest

This method is independent of the type of items.

It is the same for integers, floats or any type in which two


items can be compared

In this case we can write a generic function that can find the
max for any list of items as long as they are comparable

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

C++ templates

I
I

In C++ generic operations are done using templates


there are two types of templates
1. Function templates
2. Class templates

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Function Template

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Using Function Template

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Class template

Hikmat Farhat

Data Structures

Proof Techniques
Recursion
C++ Review

C++ Classes
Templates

Using Class template

Hikmat Farhat

Data Structures

Vous aimerez peut-être aussi