recursion csc 172 spring 2002 lecture 8 visual proof 123.n0 1 2 3 n

105
Recursion CSC 172 SPRING 2002 LECTURE 8

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Recursion

CSC 172

SPRING 2002

LECTURE 8

Visual Proof

1 2 3 . n0

123

n

nin

i

...3211

Visual Proof

1 ..(n+1)/2. .n0

123

n

Visual Proof

1 ..(n+1)/2. .n0

123

n

Visual Proof

1 ..(n+1)/2. .n0

123

n

Visual Proof

1 ..(n+1)/2. .n0

123

n

Visual Proof

1 ..(n+1)/2. .n0

123

n+1

n

Visual Proof

1 ..(n+1)/2. .n0

123

n+1

n

n/2

Visual Proof

1 ..(n+1)/2. .n0

123

n+1

n

Visual Proof

1 ..(n+1)/2. .n0

123

n+1

n

Visual Proof

1 ..(n+1)/2. .n0

123

n+1

n

2

)1(

1

nni

n

i

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;

}}

A linked List

6 1 3 2 5 9

A linked List in Split

6 1 3 2 5 9

Split head

A linked List in Split

6 1 3 2 5 9

Split 0head secondNode

secondNode = head.getNext();

Split 0head secondNode

A linked List in Split

6 1 3 2 5 9

head.setNext(secondNode.getNext());

Split 0head secondNode

First recursive call in Split

6 1 3 2 5 9

secondNode.setNext(split(secondNode.getNext());

Split 1head secondNode

New Split

6 1 3 2 5 9

split(secondNode.getNext());

Split 1head secondNode

New Split

6 1 3 2 5 9

head.setNext(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());

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;

}}

Two linked Lists

1 3 7

2 8 9

Two linked Lists in merge

1 3 7

2 8 9

list1merge 0

list2

Two linked Lists in merge

1 3 7

2 8 9

list1merge 1

list2

0th

Two linked Lists in merge

1 3 7

2 8 9

list1merge 2

list2

0th

1st

Two linked Lists in merge

1 3 7

2 8 9

list1merge 3

list2

0th

1st

2nd

Two linked Lists in merge

1 3 7

2 8 9

list1merge 4

list2

0th

1st

2nd

3rd

Two linked Lists in merge

1 3 7

2 8 9

list1merge 3

list2

0th

1st

2nd

Two linked Lists in merge

1 3 7

2 8 9

list1merge 2

list2

0th

1st

2nd

Two linked Lists in merge

1 3 7

2 8 9

list1merge 2

list2

0th

1st

2nd

Two linked Lists in merge

1 3 7

2 8 9

list1merge 0

list2

0th

1st

2nd

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

}

}

Mergesort

3 1 4 1 5 9 2 6

Mergesort: Split

3 1 4 1 5 9 2

6

Mergesort: Split

3 1 4 1 5 9

2

6

Mergesort: Split

3 1 4 1 5

9

2

6

Mergesort: Split

3 1 4 1

5

9

2

6

Mergesort: Split

3 1 4

1

5

9

2

6

Mergesort: Split

3 1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Split

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

3

1

4

1

5

9

2

6

Mergesort: Merge

31

4

1

5

9

2

6

Mergesort: Merge

31 41

5

9

2

6

Mergesort: Merge

31 41 5

9

2

6

Mergesort: Merge

31 41 5 92 6