View
214
Download
0
Category
Preview:
Citation preview
Recursion: Basis & Induction
Usually thought of as a programming technique
Can also serve as a method of definition
Recursive Definition of Expressions
Example: expressions with binary operators
Basis:
An operand (variable or constant) is an expression
- x1, y, 3, 57, 21.3
Recursive Definition of Expressions
Induction:
1. If E is an expression, then (E) is an expression
2. If E1 and E2 are expressions, and ° is a binary operator (e.g. +, *), then E1 ° E2 is an expression
Thus we can build,
x, y, 3, x+y, (x+y), (x+y)*3
An Interesting Proof
S(n): An expression E with binary operators of length n has one more operand than operators.
Proof is by complete induction on the length (number of operators, operands, and parentheses) of the expression
S(n): An expression E with binary operators of length n has one more operand than operators.
Basis: n=1
E must be a single operand
Zero operators
S(n): An expression E with binary operators of length n has one more operand than operators.
Induction (complete):
Assume S(1), S(2), . . . S(n).
Let E have length n+1 > 1
How was E constructed? Rule 1, or Rule 2? If by rule 1, then E = (E1)
E1 has length n-1 BTIH: S(n-1) has one more operand than operators E and E1 have the same number of operands & operators So, S(n+1) will hold under construction by rule 1
S(n): An expression E with binary operators of length n has one more operand than operators.
If by rule 2, then E = E1 ° E2
Both E1 and E2 have length <= n, because
° is one symbol
length(E1 ) + length(E2 ) = n
Let E1 and E2 have a and b operators
BTIH E1 and E2 have a+1 and b+1 operandsThus, E has (a+1)+(b+1) = a+b+2 operandsE has a+b+1 operators (the “+1” is for the “°”)
S(n): An expression E with binary operators of length n has one more operand than operators.
E has (a+1)+(b+1) = a+b+2 operandsE has a+b+1 operators
Note: we used all of S(1),S(2),. . .,S(n) in the inductive step
The fact that “expression” was defined recursively let us break expressions apart.
We don’t know how it broke up, but we have all the cases covered.
Recursion: an organization of process
A style of programming and problem-solving where we express a solution in terms of smaller instances of itself.
Uses basis/induction just like inductive proofs
Basis: needs no smaller instances
Induction: solution in terms of smaller instances
Sorting Lists with MergeSort
Note: a list of length 1 is sorted
If two lists are sorted, how can you combine them into one sorted list?
How can you divide one list into two?
Recursive Mergesort
Basis: A list of length 1 is sortedInduction: for lists of >= 1 element1. Split the list into two equal (as possible) parts2. Recursively sort each part3. Merge the result of the two recursive sorts
One at a time, select the smaller element from the two fronts of each list
Physical demo
Simplified MergeSort
A linked list is a list of nodes with
Node getNext(), setNext() methods.
and getData(), setData methods.
Node splitpublic static Node split(Node head){
Node secondNode;if (head == null) return null;else if (head.getNext() == null) return null;else {
secondNode = head.getNext();head.setNext(secondNode.getNext());
secondNode.setNext(split(secondNode.getNext());return secondNode;
}}
Split 0head secondNode
First recursive call in Split
6 1 3 2 5 9
secondNode.setNext(split(secondNode.getNext());
Split 1head secondNode
Second recursive Split
6 1 3 2 5 9
secondNode.setNext(split(secondNode.getNext());
Split 2head secondNode
Second recursive Split
6 1 3 2 5 9
secondNode.setNext(split(secondNode.getNext());
Split 2head secondNode
Second recursive Split
6 1 3 2 5 9
head.setNext(secondNode.getNext());
secondNode.setNext(split(secondNode.getNext()); ?
What gets returned? return secondNode;
Split 1head secondNode
Backing up
6 1 3 2 5 9
secondNode.setNext(split(secondNode.getNext());
What gets returned? return secondNode;
Split 1head secondNode
Backing up
6 1 3 2 5 9
secondNode.setNext(split(secondNode.getNext());
What gets returned? return secondNode;
Split 1head secondNode
Backing up
6 1 3 2 5 9
secondNode.setNext(split(secondNode.getNext());
What gets returned? return secondNode;
Node Mergepublic static Node merge(Node list1, Node list2){
if (list1 == null) return list2;else if (list2 == null) return list1;else if (list1.getData.compareTo(list2.getData()) < 1) {
list1.setNext(merge(list1.getNext(),list2);return list1;
} else {list2.setNext(merge(list1,list2.getNext());return list2;
}}
Node MergeSort
public static Node mergeSort(Node list){
Node secondList;
if (list == null) return null;
else if (list.getNext() == null) return list;
else {
secondList = split(list);
return merge(mergeSort(list),mergeSort(secondList));
}
}
Recommended