rethinking recursion prof. chung-ta king department of computer science national tsing hua...
Post on 19-Dec-2015
215 views
TRANSCRIPT
![Page 1: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/1.jpg)
Rethinking Recursion
Prof. Chung-Ta KingDepartment of Computer
ScienceNational Tsing Hua University
CS1103 電機資訊工程實習
(Contents from Dr. Jürgen Eckerle, Dr. Sameh Elsharkawy, Dr. David Reed,www.cs.cornell.edu/courses/cs211/2004fa/Lectures/Induction/induction.pdf)
![Page 2: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/2.jpg)
2
What Is Special about the Tree?
http://id.mind.net/~zona/mmts/geometrySection/fractals/tree/treeFractal.html
![Page 3: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/3.jpg)
3
How about This?
![Page 4: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/4.jpg)
4
And These?
![Page 5: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/5.jpg)
They all defined/expressed in terms of themselves
Recursion
![Page 6: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/6.jpg)
6
How about This?
Robot factory
![Page 7: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/7.jpg)
7
Overview
What is recursion? Why recursion? Recursive programming Recursion and iteration Recursion and induction
![Page 8: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/8.jpg)
8
Recursion
In mathematics and computer science, recursion is a method of defining functions in which the function being defined is applied within its own definition
For example: n! = n(n-1)!
It is also used more generally to describe a process of repeating objects in a self-similar way
![Page 9: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/9.jpg)
9
Recursion
Recursion is a powerful technique for specifying functions, sets, and programs.
Recursively-defined functions factorial counting combinations (choose r out of n items) differentiation of polynomials
Recursively-defined sets language of expressions
Recursively-defined graphs, images, puzzles, concepts, …
![Page 10: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/10.jpg)
10
Recursive Definitions
Consider the following list of numbers:24, 88, 40, 37
Such a list can be defined as follows:A LIST is a: number
or a: number comma LIST That is, a LIST is defined to be a single number, or
a number followed by a comma followed by a LIST A more concise expression: (Grammar)
LIST numberLIST number , LIST
The concept of a LIST is used to define itself
![Page 11: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/11.jpg)
11
Recursive Definitions
The recursive part of the LIST definition is used several times, terminating with the non-recursive part:
number comma LIST 24 , 88, 40, 37
number comma LIST 88 , 40, 37
number comma LIST 40 , 37
number 37
![Page 12: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/12.jpg)
12
Infinite Recursion
All recursive definitions have to have a non-recursive part If they didn't, there would be no way to terminate
the recursive path Such a definition would cause infinite recursion
This problem is similar to an infinite loop, but the non-terminating "loop" is part of the definition itself
The non-recursive part is often called the base case
![Page 13: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/13.jpg)
13
Recursion
Every recursive definition has 2 parts: BASE CASE(S): case(s) so simple that they
can be solved directly RECURSIVE CASE(S): make use of
recursion to solve smaller subproblems and combine into a solution to the larger problem
To verify that a recursive definition works: Check if base case(s) are handled correctly ASSUME RECURSIVE CALLS WORK ON
SMALLER PROBLEMS, then check that results from the recursive calls are combined to solve the whole
![Page 14: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/14.jpg)
14
Recursion
N!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusive
This definition can be expressed recursively in a recurrence equation as:
1! = 1 base caseN! = N * (N-1)! recursive case
A factorial is defined in terms of another factorial
Eventually, the base case of 1! is reached
![Page 15: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/15.jpg)
15
Recursion
Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21, …Fibo(1) = 1Fibo(2) = 1Fibo(n) = Fibo(n–1) + Fibo(n–2)
The larger problem is a combination of two smaller problems
![Page 16: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/16.jpg)
16
Python Code
def factorial(n):if n == 1: return 1result = n * factorial(n-1)return result
for i in range(1,10): print factorial(i)
def fibonacci(N):if N <= 2: return 1return fibonacci(N-1)+fibonacci(N-2)
for i in range(1,10): print fibonacci(i)
![Page 17: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/17.jpg)
17
Overview
What is recursion? Why recursion? Recursive programming Recursion and iteration Recursion and induction
![Page 18: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/18.jpg)
18
Why Recursion?
Concise in representation and expression: For example: any algebraic expression such as
( x + y ) ( x + y ) * x( x + y ) * x – z * y / ( x + x )x * z / y + ( x ) – ( y * z ) + ( y – x * z – y / x )
Can be expressed using the recursive definition:S x | y | z | S + S | S – S | S * S | S/S | (S)
For example:( x + y ) ( S + S ) ( S ) S
Recursion for composition and decomposition
![Page 19: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/19.jpg)
19
Why Recursion?
Simplify solution divide and conquer Divide a given (complex) problem into a set of
smaller problems and solve these and merge them to a complete solution
If the smaller problems have the same structure as the originally problem, this problem solving process can be applied recursively.
This problem solving process stops as soon as trivial problems are reached which can be solved in one step.
Only need to focus on the smaller/simplified subproblems, instead of overwhelming by the (complex) original problem
![Page 20: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/20.jpg)
20
Divide and Conquer
Method: If the problem P is trivial, solve it. Otherwise [Divide] Divide P into a set of smaller
problems P[0], ..., P[n-1] [Conquer] Compute a solution S[i] of all the
subproblems P[i] [Merge] Merge all the subsolutions S[i] to a
solution S of P
![Page 21: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/21.jpg)
21
Overview
What is recursion? Why recursion? Recursive programming Recursion and iteration Recursion and induction
![Page 22: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/22.jpg)
22
Recursive Programming
A procedure can call itself, perhaps indirectly General structure:if stopping condition then solve base problemelse use recursion to solve smaller problem(s) combine solutions from smaller problem(s)
Each call to the procedure sets up a new execution environment (stack frame or activation record), with new parameters and local variables
When the procedure completes, control returns to the calling procedure, which may be an earlier invocation of itself
![Page 23: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/23.jpg)
23
Tower of Hanoi
Which is the base case?Which is the recursion case?
![Page 24: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/24.jpg)
24
Tower of Hanoi
![Page 25: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/25.jpg)
25
Tower of Hanoi
![Page 26: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/26.jpg)
26
Python Code
def hanoi(n, a='A', b='B', c='C'):# move n discs from a to c thru b
if n == 0: return hanoi(n-1, a, c, b) print 'disc', n, ':', a, '->', c hanoi(n-1, b, a, c)
hanoi(3)
![Page 27: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/27.jpg)
27
Thinking Recursively
1. Find a way of breaking the given problem into smaller/simpler subproblems Tower of Hanoi: largest disc & remaining n-1 discs
2. Relate the solution of the simpler subproblem with the solution of the larger problem Tower of Hanoi: move n-1 discs to the middle peg;
move the largest disc to the destination peg; move n-1 discs from the middle peg to the destination peg
3. Determine the smallest problem that cannot be decomposed any further and terminate there
![Page 28: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/28.jpg)
28
Generating Permutations
Numerous applications require systematically generating permutations (orderings)
Take some sequence of items (e.g. string of characters) and generate every possible arrangement without duplicates
"123" "123", "132", "213", "231", "312", "321"
![Page 29: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/29.jpg)
29
Recursive Generation
Recursive permutation generation for each letter in the word1. remove that letter2. get the permutation of the remaining letters3. add the letter back at the front
Example: "123" "1" + (1st permutation of "23") = "1" + "23" = "123" "1" + (2nd permutation of "23") = "1" + "32" = "132" "2" + (1st permutation of "13") = "2" + "13" = "213" "2" + (2nd permutation of "13") = "2" + "31" = "231" "3" + (1st permutation of "12") = "3" + "12" = "312" "3" + (2nd permutation of "12") = "3" + "21" = "321"
![Page 30: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/30.jpg)
30
Tiled Pictures
Consider the task of repeatedly displaying a set of images in a mosaic Three quadrants contain individual images Upper-left quadrant repeats pattern
The base case is reached when the area for the images shrinks to a certain size
![Page 31: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/31.jpg)
31
Tiled Pictures
![Page 32: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/32.jpg)
32
Fractals
A geometric shape made up of same pattern repeated in different sizes and orientations
Koch curve A curve of order 0 is a straight line A curve of order n consists of 4 curve of order n-1
![Page 33: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/33.jpg)
33
Fractals
Koch curve after five iteration steps (order 4 curve)
![Page 34: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/34.jpg)
34
Fractals
Koch snowflake From 3 Koch curves of order 4
![Page 35: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/35.jpg)
35
Sierpinski Triangle
A confined recursion of triangles to form a geometric lattice
![Page 36: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/36.jpg)
36
Shortest Path from s to v
How to solve it with a recursive procedure?
s
u v
yx
10
5
1
2 39
4 67
2
![Page 37: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/37.jpg)
37
Shortest Path from s to v
Decompose into smaller subproblems
Combine and find the minimum
s
u v
yx
101
2 394 6
2
s
u v
yx
5
1
2 394 6
2
1 49
![Page 38: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/38.jpg)
38
Overview
What is recursion? Why recursion? Recursive programming Recursion and iteration Recursion and induction
![Page 39: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/39.jpg)
39
Recursion vs. Iteration
Iteration can be used in place of recursion (Nearly) every recursively defined problem
can be solved iteratively Iterative optimization, e.g. by compiler, can be
implemented after recursive design Recursive solutions are often less efficient, in
terms of both time and space (next page) Recursion can simplify the solution of a
problem, often resulting in shorter, more easily understood, correct source code
What if we have multiple processors?
![Page 40: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/40.jpg)
40
Recursion and Redundancy
Consider the recursive fibonacci method:fib(5)
fib(4) + fib(3)
fib(3) + fib(2) fib(2) + fib(1)
fib(2) + fib(1)
SIGNIFICANT amount of redundancy in recursion # recursive calls > # loop iterations (by an exponential amount!)
Recursive version is often slower than iterative version
![Page 41: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/41.jpg)
41
Shortest Path from s to v
The recursive procedure builds a search tree of an exponential complexity
s
u v
yx
10
5
1
2 394 6
7
2
s
u
u y
x
10
xv
5
1 2
v
3 29
v6
There are more efficient algorithms:e.g. Dijkstra’s algorithm (O(n2))
![Page 42: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/42.jpg)
42
Divide-and-Conquer Again
What have we done with n! in terms of divide-and-conquer?
n! = n * (n – 1)! We only divide one number off in each recursion
the two subproblems are imbalanced
Can we do this?n! = (n * (n-1) * … * (n/2 +1)) * (n/2)! even nn! = n * ((n-1) * … * ((n-1)/2+1)) * ((n-1)/2)! odd
n Very much like a binary tree
Any difference between the two?
Tail recursion
![Page 43: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/43.jpg)
43
When Recursion? When it is the most natural way of thinking about and
implementing a solution can solve problem by breaking into smaller instances, solve,
combine solutions When it is roughly equivalent in efficiency to an
iterative solution, orWhen the problems to be solved are so small that efficiency doesn't matter
think only one level deep make sure the recursion handles the base case(s) correctly assume recursive calls work correctly on smaller problems make sure solutions to the recursive problems are combined
correctly avoid infinite recursion
make sure there is at least one base case & each recursive call gets closer
![Page 44: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/44.jpg)
44
Overview
What is recursion? Why recursion? Recursive programming Recursion and iteration Recursion and induction
![Page 45: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/45.jpg)
45
Recursion and Induction
Recursion: A solution strategy that solves a large problem by
breaking it up into smaller problems of same kind A concept that make something by itself, e.g. tools
that make tools, robots that make robots, Droste pictures
Induction: A mathematical strategy for proving statements
about integers (more generally, about sets that can be ordered in some fairly general ways)
Understanding induction is useful for figuring out how to write recursive code.
![Page 46: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/46.jpg)
46
Prove Inductively
Assume equally spaced dominoes, where spacing is less than domino length.
How would you argue that all dominoes would fall? Domino 0 falls because we push it over. Domino 1 falls because domino 0 falls, domino 0
is longer than inter-domino spacing, so it knocks over domino 1
Domino 2 falls because … Is there a more compact argument?
![Page 47: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/47.jpg)
47
Prove Inductively
Better argument Domino 0 falls because we push it over. Suppose domino k falls over. Because its length is
larger than inter-domino spacing, it will knock over domino k+1.
Therefore, all dominoes will fall over. This is an inductive argument. Not only is it more compact, but it works even
for an infinite number of dominoes!
![Page 48: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/48.jpg)
48
Induction over Integers
We want to prove that some property P holds for all integers.
Inductive argument: P(0): show that property P is true for integer 0 P(k) => P(k+1): if property P is true for integer k, it
is true for integer k+1 This means P(n) holds for all integers n
![Page 49: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/49.jpg)
49
Consider This
Can we show that these two definitions of SQ(n) are equal?
SQ1(0) = 0 for n = 0
SQ1(n) = SQ1(n-1) + n2 for n > 0
SQ2(n) = n(n+1)(2n+1)/6
where they all calculate SQ(n) = 02+12+…+n2
![Page 50: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/50.jpg)
50
Inductive Proof
Let proposition be P(j): SQ1(j) = SQ2(j)
Two parts of proof: Prove P(0). Prove P(k+1) assuming P(k).
P(0) P(1) P(2) P(k) P(k+1)
![Page 51: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/51.jpg)
51
Inductive Proof
P(0): show SQ1(0) = SQ2(0) (easy) SQ1(0) = 0 = SQ2(0)
P(k) P(k+1): Assume SQ1(k) = SQ2(k) SQ1(k+1) = SQ1(k) + (k+1)2 (definition of SQ1)
= SQ2(k) + (k+1)2 (inductive assumption)
= k(k+1)(2k+1)/6 + (k+1)2 (definition of SQ2)=(k+1)(k+2)(2k+3)/6 (algebra)
= SQ2(k+1) (definition of SQ2)
![Page 52: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/52.jpg)
52
Consider the Tiling Problem
Problem: A chess-board has one square cut out of it. Can the remaining board be tiled using tiles of the
shape shown in the picture? Not obvious that we can use induction to
solve this problem.
![Page 53: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/53.jpg)
53
Idea
Consider boards of size 2n x 2n for n = 1,2,….. Base case: show that tiling is possible for 2 x
2 board. Inductive case: assuming 2n x 2n board can
be tiled, show that 2n+1 x 2n+1 board can be tiled.
Chess-board (8x8) is a special case of this argument.
![Page 54: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/54.jpg)
54
Base Case
For a 2x2 board, it is trivial to tile the board regardless of which one of the four pieces has been cut.
![Page 55: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/55.jpg)
55
4x4 Case
Divide 4x4 board into four 2x2 sub-boards. One of the four sub-boards has the missing
piece. That sub-board can be tiled since it is a 2x2 board with a missing piece.
Tile the center squares of the three remaining sub-boards as shown.
This leaves 3 2x2 boards with a missing piece, which can be tiled.
![Page 56: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/56.jpg)
56
Inductive Proof
Claim: Any board of size 2n x 2n with one missing square can be tiled.
Base case: (n = 1) trivial Inductive case: assume inductive hypothesis for (n =
k) and consider board of size 2k+1x 2k+1
Divide board into four equal sub-boards of size 2k X 2k
One of the sub-boards has the missing piece; by inductive assumption, this can be tiled.
Tile the central squares of the remaining three sub-boards This leaves three sub-boards with a missing square each,
which can be tiled by inductive assumption.
![Page 57: Rethinking Recursion Prof. Chung-Ta King Department of Computer Science National Tsing Hua University CS1103 電機資訊工程實習 (Contents from Dr. Jürgen Eckerle,](https://reader036.vdocuments.pub/reader036/viewer/2022062516/56649d3b5503460f94a16247/html5/thumbnails/57.jpg)
57
When Induction Fails
Proposition: any n x n board with one missing square can be tiled
Problem: a 3 x 3 board with one missing square has 8 remaining squares, but our tile has 3 squares tiling is impossible
Therefore, any attempt to give an inductive proof of the proposition must fail
This does not say anything about the 2n x 2n cases