Recursion - PowerPoint PPT Presentation

About This Presentation
Title:

Recursion

Description:

The fact that 'expression' was defined recursively let us break expressions apart. ... Merge the result of the two recursive sorts ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 106
Provided by: thaddeusf
Category:
Tags: fact | recursion

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
  • CSC 172
  • SPRING 2002
  • LECTURE 8

2
Visual Proof
n
3
2
1
1
2
3
.
n
0
3
Visual Proof
n
3
2
1
1
..
(n1)/2
.
.
n
0
4
Visual Proof
n
3
2
1
1
..
(n1)/2
.
.
n
0
5
Visual Proof
n
3
2
1
1
..
(n1)/2
.
.
n
0
6
Visual Proof
n
3
2
1
1
..
(n1)/2
.
.
n
0
7
Visual Proof
n1
n
3
2
1
1
..
(n1)/2
.
.
n
0
8
Visual Proof
n/2
n1
n
3
2
1
1
..
(n1)/2
.
.
n
0
9
Visual Proof
n1
n
3
2
1
1
..
(n1)/2
.
.
n
0
10
Visual Proof
n1
n
3
2
1
1
..
(n1)/2
.
.
n
0
11
Visual Proof
n1
n
3
2
1
1
..
(n1)/2
.
.
n
0
12
Recursion Basis Induction
  • Usually thought of as a programming technique
  • Can also serve as a method of definition

13
Recursive Definition of Expressions
  • Example expressions with binary operators
  • Basis
  • An operand (variable or constant) is an
    expression
  • - x1, y, 3, 57, 21.3

14
Recursive 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

15
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

16
S(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

17
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 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

18
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 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 )

19
S(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.

20
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

21
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?

22
Recursive 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

23
Simplified MergeSort
  • A linked list is a list of nodes with
  • Node getNext(), setNext() methods.
  • and getData(), setData methods.

24
Node 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

25
A linked List
6

1

3

2

5

9
?
26
A linked List in Split
Split head
6

1

3

2

5

9
?
27
A linked List in Split
secondNode head.getNext()
Split 0 head secondNode
6

1

3

2

5

9
?
28
A linked List in Split
head.setNext(secondNode.getNext())
Split 0 head secondNode
6

1

3

2

5

9
?
29
First recursive call in Split
secondNode.setNext(split(secondNode.getNext())
Split 0 head secondNode
6

1

3

2

5

9
?
30
New Split
split(secondNode.getNex
t())
Split 1 head secondNode
6

1

3

2

5

9
?
31
New Split
head.setNext(secondNode.getNext())
Split 1 head secondNode
6

1

3

2

5

9
?
32
Second recursive Split
secondNode.setNext(split(secondNode.getNext())
Split 1 head secondNode
6

1

3

2

5

9
?
33
Second recursive Split
secondNode.setNext(split(secondNode.getNext())
Split 2 head secondNode
6

1

3

2

5

9
?
34
Second 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?
35
Backing up
secondNode.setNext(split(secondNode.getNext())
Split 1 head secondNode
6

1

3

2

5
?
9
?
36
Backing up
secondNode.setNext(split(secondNode.getNext())
Split 1 head secondNode
6

1

3

2

5
?
9
?
return secondNode
What gets returned?
37
Backing up
secondNode.setNext(split(secondNode.getNext())
Split 1 head secondNode
6

1

3

2

5
?
9
?
return secondNode
What gets returned?
38
Backing up
secondNode.setNext(split(secondNode.getNext())
Split 1 head secondNode
6

1

3

2

5
?
9
?
return secondNode
What gets returned?
39
Node 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

40
Two linked Lists
1

3

7
?
2

8

9
?
41
Two linked Lists in merge
1

3

7
?
list1 merge 0 list2
2

8

9
?
42
Two linked Lists in merge
0th
1

3

7
?
list1 merge 1 list2
2

8

9
?
43
Two linked Lists in merge
0th
1

3

7
?
list1 merge 2 list2
2

8

9
?
1st
44
Two linked Lists in merge
0th
2nd
1

3

7
?
list1 merge 3 list2
2

8

9
?
1st
45
Two linked Lists in merge
0th
2nd
3rd
1

3

7
?
?
list1 merge 4 list2
2

8

9
?
1st
46
Two linked Lists in merge
0th
2nd
1

3

7
list1 merge 3 list2
2

8

9
?
1st
47
Two linked Lists in merge
0th
2nd
1

3

7
list1 merge 2 list2
2

8

9
?
1st
48
Two linked Lists in merge
0th
2nd
1

3

7
list1 merge 2 list2
2

8

9
?
1st
49
Two linked Lists in merge
0th
2nd
1

3

7
list1 merge 0 list2
2

8

9
?
1st
50
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(secondLis
    t))

51
Mergesort
3
1
4
1
5
9
2
6
52
Mergesort Split
6
3
1
4
1
5
9
2
53
Mergesort Split
6
3
1
4
1
5
9
2
54
Mergesort Split
9
6
3
1
4
1
5
2
55
Mergesort Split
9
6
3
1
4
1
5
2
56
Mergesort Split
1
9
6
3
1
4
5
2
57
Mergesort Split
1
9
6
3
1
4
5
2
58
Mergesort Split
1
1
9
6
3
4
5
2
59
Mergesort Split
1
1
9
6
3
4
5
2
60
Mergesort Split
6
1
1
9
3
4
5
2
61
Mergesort Split
6
1
1
9
3
4
5
2
62
Mergesort Split
1
6
1
9
3
4
5
2
63
Mergesort Split
1
6
1
9
3
4
5
2
64
Mergesort Split
6
1
1
9
3
4
5
2
65
Mergesort Split
6
1
1
9
3
4
5
2
66
Mergesort Merge
6
1
1
9
3
4
5
2
67
Mergesort Merge
6
1
1
9
3
4
5
2
68
Mergesort Merge
1
6
1
9
3
4
5
2
69
Mergesort Split
1
6
1
9
3
4
5
2
70
Mergesort Split
1
6
9
1
3
4
5
2
71
Mergesort Split
1
6
9
1
3
4
5
2
72
Mergesort Merge
1
6
9
1
3
4
5
2
73
Mergesort Merge
1
6
9
1
3
4
5
2
74
Mergesort Merge
1
6
1
9
3
4
5
2
75
Mergesort Merge
6
1
1
9
3
4
5
2
76
Mergesort Merge
6
1
1
9
3
4
5
2
77
Mergesort Merge
1
1
6
9
3
4
5
2
78
Mergesort Merge
1
1
9
6
3
4
5
2
79
Mergesort Split
1
1
9
6
3
4
5
2
80
Mergesort Split
1
1
9
6
2
3
4
5
81
Mergesort Split
1
1
9
6
2
3
4
5
82
Mergesort Split
1
1
9
6
4
2
3
5
83
Mergesort Split
1
1
9
6
4
2
3
5
84
Mergesort Split
1
1
9
6
2
4
3
5
85
Mergesort Split
1
1
9
6
2
4
3
5
86
Mergesort Merge
1
1
9
6
2
4
3
5
87
Mergesort Merge
1
1
9
6
2
4
3
5
88
Mergesort Merge
1
1
9
6
4
2
3
5
89
Mergesort Split
1
1
9
6
4
2
3
5
90
Mergesort Split
1
1
9
6
4
2
5
3
91
Mergesort Split
1
1
9
6
4
2
5
3
92
Mergesort Merge
1
1
9
6
4
2
5
3
93
Mergesort Merge
1
1
9
6
4
2
5
3
94
Mergesort Merge
1
1
9
6
4
2
3
5
95
Mergesort Merge
1
1
9
6
4
2
3
5
96
Mergesort Merge
1
1
9
6
4
3
2
5
97
Mergesort Merge
1
1
9
6
3
4
2
5
98
Mergesort Merge
1
1
9
6
3
4
5
2
99
Mergesort Merge
1
9
6
1
3
4
5
2
100
Mergesort Merge
9
6
1
1
3
4
5
2
101
Mergesort Merge
9
6
1
1
2
3
4
5
102
Mergesort Merge
9
6
3
1
1
2
4
5
103
Mergesort Merge
9
6
3
1
4
1
2
5
104
Mergesort Merge
9
6
3
1
4
1
5
2
105
Mergesort Merge
3
1
4
1
5
9
2
6
Write a Comment
User Comments (0)
About PowerShow.com