300 likes | 446 Views
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.
E N D
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 ... and you can get from any step to the next step
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
Induction has three parts • Base Case(s) • Simplest examples • Inductive Step • From simpler to more complex • ‘Exclusivity’ • Only things defined from the base case and inductive steps are legal • This part can be omitted in definition as we assume it must be true
Induction – an example • What is a natural number? {0,1,2,3,4,...... } • Use induction to define natural numbers: • Base: 0 is a natural number • Induction: if x is a natural number so is x+1 • Exclusivity: only numbers generated using the above rules are natural numbers
Regular Languages defined by Induction Base: • { a } is a regular language for any ‘a’ Inductive steps: • If A is a regular language so is A* • If A and B are regular languages so is A U B • If A and B are regular languages so is A.B • ... etc
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
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: • Base: an empty string, Λ, is a palindrome • Base: a single character is a palindrome • Induction: if P is a palindrome, then so is xPx where x is any character
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 either • there is an arc from A to B, (base) or • There is an arc from A to some node C and there is a path from C to B (induction). b a f e c d g h
How can we prove whether this program stops? s=0; v = getPosInteger(); while(v != 0) { s = s+v; v = v-1; } Why is Induction Important? - 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
Why is Induction Important? - 2 n! = n ∗(n-1) ∗(n-2) ∗ ... ∗1 for n >= 1 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
Why is Induction Important? - 3 Define an ordered list (i.e. go through the list from left to right the numbers get bigger) 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
Why is Induction Important? - 3 ‘translate’ the definition to a program: ordered_list (L) { if(tail(L)==‘empty’) // base return true; else // induction return ( head(L)>=head(tail(L)) && ordered_list(tail(L) ); }
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
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”
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;
Making a Long String - cont Easy Case 2 – when n is even if(n%2 == 0) return duplicate(s+s, n/2) remainder on dividing by 2 ‘+’ here means appending two strings into one
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);
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); }
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; }
It Works! • It works for n=0 • If it works for all n up to N say • and so it works for all n up to N+1
General Recursion If the problem is very easy – solve it solve small problems else break into small problems
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
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
// 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 Recursion and Iteration – an examplemembership checking (x is in the list?)