Title: Recursion
1Recursion
- CSC 172
- SPRING 2002
- LECTURE 8
2Visual Proof
n
3
2
1
1
2
3
.
n
0
3Visual Proof
n
3
2
1
1
..
(n1)/2
.
.
n
0
4Visual Proof
n
3
2
1
1
..
(n1)/2
.
.
n
0
5Visual Proof
n
3
2
1
1
..
(n1)/2
.
.
n
0
6Visual Proof
n
3
2
1
1
..
(n1)/2
.
.
n
0
7Visual Proof
n1
n
3
2
1
1
..
(n1)/2
.
.
n
0
8Visual Proof
n/2
n1
n
3
2
1
1
..
(n1)/2
.
.
n
0
9Visual Proof
n1
n
3
2
1
1
..
(n1)/2
.
.
n
0
10Visual Proof
n1
n
3
2
1
1
..
(n1)/2
.
.
n
0
11Visual Proof
n1
n
3
2
1
1
..
(n1)/2
.
.
n
0
12Recursion Basis Induction
- Usually thought of as a programming technique
- Can also serve as a method of definition
13Recursive Definition of Expressions
- Example expressions with binary operators
-
- Basis
- An operand (variable or constant) is an
expression - - x1, y, 3, 57, 21.3
-
14Recursive Definition of Expressions
- Induction
- If E is an expression, then (E) is an expression
- 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, xy, (xy), (xy)3
15An 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
16S(n) An expression E with binary operators of
length n has one more operand than operators.
- Basis n1
- E must be a single operand
- Zero operators
17S(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 n1 gt 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(n1) will hold under construction by rule 1
18S(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 lt n,
- because
- is one symbol
- length(E1 ) length(E2 ) n
- Let E1 and E2 have a and b operators
- BTIH E1 and E2 have a1 and b1 operands
- Thus, E has (a1)(b1) ab2 operands
- E has ab1 operators (the 1 is for the )
19S(n) An expression E with binary operators of
length n has one more operand than operators.
- E has (a1)(b1) ab2 operands
- E has ab1 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 dont know how it broke up, but we have all
the cases covered.
20Recursion 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
21Sorting 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?
22Recursive Mergesort
- Basis A list of length 1 is sorted
- Induction for lists of gt 1 element
- Split the list into two equal (as possible) parts
- Recursively sort each part
- 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
23Simplified MergeSort
- A linked list is a list of nodes with
- Node getNext(), setNext() methods.
- and getData(), setData methods.
-
24Node split
- public 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
-
25A linked List
6
1
3
2
5
9
?
26A linked List in Split
Split head
6
1
3
2
5
9
?
27A linked List in Split
secondNode head.getNext()
Split 0 head secondNode
6
1
3
2
5
9
?
28A linked List in Split
head.setNext(secondNode.getNext())
Split 0 head secondNode
6
1
3
2
5
9
?
29First recursive call in Split
secondNode.setNext(split(secondNode.getNext())
Split 0 head secondNode
6
1
3
2
5
9
?
30New Split
split(secondNode.getNex
t())
Split 1 head secondNode
6
1
3
2
5
9
?
31New Split
head.setNext(secondNode.getNext())
Split 1 head secondNode
6
1
3
2
5
9
?
32Second recursive Split
secondNode.setNext(split(secondNode.getNext())
Split 1 head secondNode
6
1
3
2
5
9
?
33Second recursive Split
secondNode.setNext(split(secondNode.getNext())
Split 2 head secondNode
6
1
3
2
5
9
?
34Second recursive Split
head.setNext(secondNode.getNext())
Split 2 head secondNode
6
1
3
2
5
?
9
?
secondNode.setNext(split(secondNode.getNext()) ?
return secondNode
What gets returned?
35Backing up
secondNode.setNext(split(secondNode.getNext())
Split 1 head secondNode
6
1
3
2
5
?
9
?
36Backing up
secondNode.setNext(split(secondNode.getNext())
Split 1 head secondNode
6
1
3
2
5
?
9
?
return secondNode
What gets returned?
37Backing up
secondNode.setNext(split(secondNode.getNext())
Split 1 head secondNode
6
1
3
2
5
?
9
?
return secondNode
What gets returned?
38Backing up
secondNode.setNext(split(secondNode.getNext())
Split 1 head secondNode
6
1
3
2
5
?
9
?
return secondNode
What gets returned?
39Node Merge
- public 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())
lt 1) - list1.setNext(merge(list1.getNext(),list2)
- return list1
- else
- list2.setNext(merge(list1,list2.getNext())
- return list2
-
40Two linked Lists
1
3
7
?
2
8
9
?
41Two linked Lists in merge
1
3
7
?
list1 merge 0 list2
2
8
9
?
42Two linked Lists in merge
0th
1
3
7
?
list1 merge 1 list2
2
8
9
?
43Two linked Lists in merge
0th
1
3
7
?
list1 merge 2 list2
2
8
9
?
1st
44Two linked Lists in merge
0th
2nd
1
3
7
?
list1 merge 3 list2
2
8
9
?
1st
45Two linked Lists in merge
0th
2nd
3rd
1
3
7
?
?
list1 merge 4 list2
2
8
9
?
1st
46Two linked Lists in merge
0th
2nd
1
3
7
list1 merge 3 list2
2
8
9
?
1st
47Two linked Lists in merge
0th
2nd
1
3
7
list1 merge 2 list2
2
8
9
?
1st
48Two linked Lists in merge
0th
2nd
1
3
7
list1 merge 2 list2
2
8
9
?
1st
49Two linked Lists in merge
0th
2nd
1
3
7
list1 merge 0 list2
2
8
9
?
1st
50Node 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(secondLis
t)) -
51Mergesort
3
1
4
1
5
9
2
6
52Mergesort Split
6
3
1
4
1
5
9
2
53Mergesort Split
6
3
1
4
1
5
9
2
54Mergesort Split
9
6
3
1
4
1
5
2
55Mergesort Split
9
6
3
1
4
1
5
2
56Mergesort Split
1
9
6
3
1
4
5
2
57Mergesort Split
1
9
6
3
1
4
5
2
58Mergesort Split
1
1
9
6
3
4
5
2
59Mergesort Split
1
1
9
6
3
4
5
2
60Mergesort Split
6
1
1
9
3
4
5
2
61Mergesort Split
6
1
1
9
3
4
5
2
62Mergesort Split
1
6
1
9
3
4
5
2
63Mergesort Split
1
6
1
9
3
4
5
2
64Mergesort Split
6
1
1
9
3
4
5
2
65Mergesort Split
6
1
1
9
3
4
5
2
66Mergesort Merge
6
1
1
9
3
4
5
2
67Mergesort Merge
6
1
1
9
3
4
5
2
68Mergesort Merge
1
6
1
9
3
4
5
2
69Mergesort Split
1
6
1
9
3
4
5
2
70Mergesort Split
1
6
9
1
3
4
5
2
71Mergesort Split
1
6
9
1
3
4
5
2
72Mergesort Merge
1
6
9
1
3
4
5
2
73Mergesort Merge
1
6
9
1
3
4
5
2
74Mergesort Merge
1
6
1
9
3
4
5
2
75Mergesort Merge
6
1
1
9
3
4
5
2
76Mergesort Merge
6
1
1
9
3
4
5
2
77Mergesort Merge
1
1
6
9
3
4
5
2
78Mergesort Merge
1
1
9
6
3
4
5
2
79Mergesort Split
1
1
9
6
3
4
5
2
80Mergesort Split
1
1
9
6
2
3
4
5
81Mergesort Split
1
1
9
6
2
3
4
5
82Mergesort Split
1
1
9
6
4
2
3
5
83Mergesort Split
1
1
9
6
4
2
3
5
84Mergesort Split
1
1
9
6
2
4
3
5
85Mergesort Split
1
1
9
6
2
4
3
5
86Mergesort Merge
1
1
9
6
2
4
3
5
87Mergesort Merge
1
1
9
6
2
4
3
5
88Mergesort Merge
1
1
9
6
4
2
3
5
89Mergesort Split
1
1
9
6
4
2
3
5
90Mergesort Split
1
1
9
6
4
2
5
3
91Mergesort Split
1
1
9
6
4
2
5
3
92Mergesort Merge
1
1
9
6
4
2
5
3
93Mergesort Merge
1
1
9
6
4
2
5
3
94Mergesort Merge
1
1
9
6
4
2
3
5
95Mergesort Merge
1
1
9
6
4
2
3
5
96Mergesort Merge
1
1
9
6
4
3
2
5
97Mergesort Merge
1
1
9
6
3
4
2
5
98Mergesort Merge
1
1
9
6
3
4
5
2
99Mergesort Merge
1
9
6
1
3
4
5
2
100Mergesort Merge
9
6
1
1
3
4
5
2
101Mergesort Merge
9
6
1
1
2
3
4
5
102Mergesort Merge
9
6
3
1
1
2
4
5
103Mergesort Merge
9
6
3
1
4
1
2
5
104Mergesort Merge
9
6
3
1
4
1
5
2
105Mergesort Merge
3
1
4
1
5
9
2
6