induction and recursion

28
1 Induction and Recursion Lecture 12-13 Ref. Handout p 40 - 46

Upload: klaus

Post on 20-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Induction and Recursion. Lecture 12-13 Ref. Handout p 40 - 46. A Question. Eve was human Human mothers produce human children Noah was Eve’s great ∗ 8 grandson Can you prove the Noah was human?. If you can reach the first rung of the ladder. You can go anywhere!. Induction. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Induction and Recursion

1

Induction and Recursion

Lecture 12-13

Ref. Handout p 40 - 46

Page 2: Induction and Recursion

2

A Question

Eve was human Human mothers produce human children

Noah was Eve’s great ∗ 8 grandson Can you prove the Noah was human?

Page 3: Induction and Recursion

3

Induction

If you can

reach the

first rung

of the

ladder

You can go anywhere!

... and you can get from any step tothe next step

Page 4: Induction and Recursion

4

Induction (the picture is from wikipedia.org)

A formal description of mathematical induction can be illustrated by reference to the sequential effect of falling dominoes

Page 5: Induction and Recursion

5

Induction has three parts

Base Case(s)o Simplest examples

Inductive Stepo From simpler to more complex

‘Exclusivity’o Only things defined from the base case and

inductive steps are legal o This part can be omitted in definition as we

assume it must be true

Page 6: Induction and Recursion

6

Induction – an example

What is a natural number?

{0,1,2,3,4,...... }

Use induction to define natural numbers:

1. Base: 0 is a natural number

2. Induction: if x is a natural number so is x+1

3. Exclusivity: only numbers generated using the above rules are natural numbers

Page 7: Induction and Recursion

7

Regular Languages defined by Induction

Base: { a } is a regular language for any ‘a’

Inductive steps:

1. If A is a regular language so is A*

2. If A and B are regular languages so is A U B

3. If A and B are regular languages so is A.B

4. ... etc

Page 8: Induction and Recursion

8

Checking Membership

Is this a regular language?

{ a, b, ab, ba, aab, bba, aaab, bbba, aaaab,... }

top down

A = { a } and B = { b } are regular

so: A* and B* are regular

so: A*.B and B*.A are regular

so: (A*.B U B*.A) is regular

bottom up

Page 9: Induction and Recursion

9

Induction – more examples palindromes

Palindrome = a string which read the same left to right as right to left.

E.g. Λ, x, aaa, hannah, did, radar,

Use induction to define palindromes:

1. Base: an empty string, Λ, is a palindrome

2. Base: a single character is a palindrome

3. Induction: if P is a palindrome, then so is xPx where x is any character

Page 10: Induction and Recursion

10

Induction – more exampleshow to define a path on a graph

A path is a sequence of connected arcs joining two node. (this is not an inductive definition)

Use induction to define the path:There is a path from node A to node B if either1. there is an arc from A to B, (base) or2. There is an arc from A to some node C and there is

a path from C to B (induction).

ab

c

e

g

f

d h

Page 11: Induction and Recursion

11

Why is Induction Important? - 1

How can we prove whether this program stops? s=0;

v = getPosInteger();

while(v != 0) {s = s+v;v = v-1;

}

It stops if v is 0 on the call of getPosInteger()

If it stops for v = n it stops for v = n+1

Assume that getPosInteger() returns a random non-negative integer

Page 12: Induction and Recursion

12

Why is Induction Important? - 2

static int factorial (int n) {if(n <= 1) // base 1!=1

return 1;else // induction: n!=n∗(n-1)!

return n ∗ factotial(n-1);}

Induction in programming is called recursion

n! = n (n-1) (n-2) ... 1∗ ∗ ∗ ∗ for n >= 1

Page 13: Induction and Recursion

13

Why is Induction Important? - 3

Base: if the list only has one number, it is ordered

Induction: let head(L)=the 1st number in L,tail(L)=rest of L after removing the 1st number.

If tail(L) is ordered and head(L) >=head(tail(L))then we say L is ordered

Define an ordered list (i.e. go through the list from left to right the numbers get bigger)

Page 14: Induction and Recursion

14

Why is Induction Important? - 3

ordered_list (L) { if(tail(L)==‘empty’) // base

return true; else // induction

return ( head(L)>=head(tail(L)) && ordered_list(tail(L) );

}

‘translate’ the definition to a program:

Page 15: Induction and Recursion

15

Bad Induction

X gets the same exam mark as herself ( or himself)

If n people always all get the same mark then so do (n+1) people

But for n=1 this is true so it’s ture for n=2 But if it’s true for n=2 it is true for n=3 So everyone gets the same mark

Page 16: Induction and Recursion

16

Memo for In-class test 12 [ /5]

questions my answers

correct answers

comments

1

2

3

4

5

Page 17: Induction and Recursion

17

Making a Long String(by duplicating a given string)

We want to have a program:public string duplicate (String s, int n)

which returns a string consisting of ‘s’ repeated ‘n’ times.

e.g. duplicate(“abc”, 4)

returns

“abcabcabcabc”

Page 18: Induction and Recursion

18

Making a Long String - cont

Easy Case 1 – when n=0 or n=1

public string duplicate(String s, int n){

if(n <= 0) return “”; // “”= Λ

if(n == 1) return s;

Page 19: Induction and Recursion

19

Making a Long String - cont

Easy Case 2 – when n is even

if(n%2 == 0)

return duplicate(s+s, n/2)

remainder ondividing by 2

‘+’ here meansappending twostrings into one

Page 20: Induction and Recursion

20

Making a Long String - cont

Easy Case 3 – when n is odd

if(n%2 == 0) // even

return duplicate(s+s, n/2);

else // odd

return s + duplicate(s+s, (n-1)/2);

Page 21: Induction and Recursion

21

Making a Long String - cont

Put them all together

public string duplicate(String s, int n){

if(n <= 0) return “”;

if(n%2 == 0) // even

return duplicate(s+s, n/2);

else // odd

return s + duplicate(s+s, (n-1)/2);

}

Page 22: Induction and Recursion

22

Making a Long String - cont

The Short Version

public string duplicate(String s, int n){

if(n <= 0) return “”;

String s2 = duplicate(s+s, n/2);

if(n%2 != 0)// odd, != means not equal

s2 = s2+s;

return s2;

}

Page 23: Induction and Recursion

23

It Works!

a) It works for n=0

b) If it works for all n up to N say

a) and so it works for all n up to N+1

Page 24: Induction and Recursion

24

General Recursion

If the problem is very easy – solve it

solve small problems

else break into small problems

Page 25: Induction and Recursion

25

Recursion and Stacks

Recursion

= using stacks

(but prettier)

fac(int n) {

if(n <= 1) return 1;

else

return (n ∗ fac(n-1));

}

fac(5)

5 ∗ fac(4)

4∗ fac(3)

3 ∗ fac(2)

2∗ fac(1)

fac(1)=1

Page 26: Induction and Recursion

26

Recursion and Iteration

A basic component of an algorithm is loop mechanism

The loops perform a repeated action of a set of instances

The loops can be classified as either iterative or recursive

An iterative loop – using explicit loop control provided by computer languages. e.g ‘while-loop’, ‘for-loop’

A recursive loops – a program calls itself

Page 27: Induction and Recursion

27

Recursion and Iteration – an examplemembership checking (x is in the list?)// using iteration

is_in_list(x, list) {if(list==[]) return false;h = Head(list); t = Tail(list);while(t != []) { if(h==x) return true; else {

t = Tail(list);h = Head(t);

}}

return false;}

// using recursion

is_in_list(x, list) { if(list==[]) return false

h = Head(list);t = Tail(list);

if(h==x) return true; else

return is_in_list(x,t);}

// note that [] means an// empty list

Page 28: Induction and Recursion

28

Memo for In-class test 13 [ /5]

questions my answers

correct answers

comments

1

2

3

4

5