4 recursion

226
Recursion Jeff Edmonds York University COSC 3101 Lecture Lecture 4 4 Code Stack of Stack Frames Tree of Stack Frames Friends and Strong Inductio Towers of Hanoi Merge & Quick Sort Simple Recursion on Trees Generalizing the Problem Heap Sort & Priority Queues Trees Representing Equation Pretty Print Parsing Recursive Images

Upload: bansal-ashish

Post on 14-May-2015

83 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 4 recursion

Recursion

Jeff Edmonds

York UniversityCOSC 3101LectureLecture

44

CodeStack of Stack FramesTree of Stack FramesFriends and Strong InductionTowers of HanoiMerge & Quick SortSimple Recursion on TreesGeneralizing the ProblemHeap Sort & Priority QueuesTrees Representing Equations Pretty PrintParsingRecursive ImagesFeedback

Page 2: 4 recursion

The Goal is to UNDERSTAND

Recursive Algorithms

Fundamentally to their core

Page 3: 4 recursion

Representation:Understand the relationship between different representations of the same

information or idea

1

2

3 Rudich www.discretemath.com

Page 4: 4 recursion

Different Representationsof Recursive Algorithms

Code - Implement on Computer

Stack of Stack Frames - Run on Computer

Tree of Stack Frames - View entire computation

Friends & Strong Induction

- Worry about one step at a time.

ProsViews

Page 5: 4 recursion

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2

+ f

Code Representation of an Algorithm

Pros and Cons?

Page 6: 4 recursion

Code Representation of an Algorithm

• Runs on computers• Precise and succinct• Perception that being

able to code is the only thing needed to get a job. (false)

• I am not a computer• I need a higher level of

intuition.• Prone to bugs• Language dependent

Pros: Cons:

Page 7: 4 recursion

Different Representationsof Recursive Algorithms

Code - Implement on Computer

Stack of Stack Frames - Run on Computer

Tree of Stack Frames - View entire computation

Friends & Strong Induction

- Worry about one step at a time.

ProsViews

Page 8: 4 recursion

X = 2133Y = 2312ac = bd = (a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Stack Frame: A particular execution of one routine on one particular input instance.

Page 9: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 10: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

X = 21Y = 23ac = bd = (a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 11: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

X = 21Y = 23

ac = ?bd = (a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 12: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

X = 21Y = 23

ac = ?bd = (a+b)(c+d) = XY =

X = 2Y = 2XY=

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 13: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

X = 21Y = 23

ac = ?bd = (a+b)(c+d) = XY =

X = 2Y = 2XY = 4

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 14: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

X = 21Y = 23ac = 4bd = (a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 15: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

X = 21Y = 23ac = 4

bd = ?(a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 16: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

X = 21Y = 23ac = 4

bd = ?(a+b)(c+d) = XY =

X = 1Y = 3XY = 3

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 17: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 18: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

X = 21Y = 23ac = 4bd = 3

(a+b)(c+d) = ?XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 19: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

X = 21Y = 23ac = 4bd = 3

(a+b)(c+d) = ?XY =

X = 3Y = 5XY = 15

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 20: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15

XY = ?

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 21: 4 recursion

X = 2133Y = 2312

ac = ?bd = (a+b)(c+d) = XY =

X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 22: 4 recursion

X = 2133Y = 2312ac = 483bd = (a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 23: 4 recursion

X = 2133Y = 2312ac = 483

bd = ?(a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 24: 4 recursion

X = 2133Y = 2312ac = 483

bd = ?(a+b)(c+d) = XY =

X = 33Y = 12

ac = ?bd = (a+b)(c+d) = XY = 15

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 25: 4 recursion

X = 2133Y = 2312ac = 483

bd = ?(a+b)(c+d) = XY =

X = 33Y = 12

ac = ?bd = (a+b)(c+d) = XY = 15

X = 3Y = 1XY = 3

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 26: 4 recursion

X = 2133Y = 2312ac = 483

bd = ?(a+b)(c+d) = XY =

X = 33Y = 12ac = 3

bd = ?(a+b)(c+d) = XY = 15

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 27: 4 recursion

X = 2133Y = 2312ac = 483

bd = ?(a+b)(c+d) = XY =

X = 33Y = 12ac = 3

bd = ?(a+b)(c+d) = XY = 15

X = 3Y = 2XY = 6

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 28: 4 recursion

X = 2133Y = 2312ac = 483

bd = ?(a+b)(c+d) = XY =

X = 33Y = 12ac = 3bd = 6

(a+b)(c+d) = ?XY = 15

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

Page 29: 4 recursion

X = 2133Y = 2312ac = 483

bd = ?(a+b)(c+d) = XY =

X = 33Y = 12ac = 3bd = 6

(a+b)(c+d) = ?XY = 396

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Stack of Stack Frames

An so on ….

Page 30: 4 recursion

Stack of Stack Frames Representation of an Algorithm

• Trace what actually occurs in the computer

• Concrete. • It is what students

attempt to describe when told not to give code.

• Described in words it is impossible to follow who is doing what.

• Does not explain why it works.

• Demonstrates for only one of many inputs.

Pros: Cons:

Page 31: 4 recursion

Different Representationsof Recursive Algorithms

Code - Implement on Computer

Stack of Stack Frames - Run on Computer

Tree of Stack Frames - View entire computation

Friends & Strong Induction

- Worry about one step at a time.

ProsViews

Page 32: 4 recursion

X = 2133Y = 2312ac = 483bd = 396(a+b)(c+d) = 1890XY = 4931496

X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

X = 54Y = 35ac = 15bd = 20(a+b)(c+d) = 72XY = 1890

X = 2Y = 2XY=4

X = 1Y = 3XY=3

X = 3Y = 5XY=15

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

X = 5Y = 3XY=15

X = 4Y = 5XY=20

X = 9Y = 8XY=72

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Tree of Stack Frames

Page 33: 4 recursion

Stack of Stack Frames Representation of an Algorithm

• View the entire computation.

• Good for computing the running time.

• Must describe entire tree. – For each stack frame

• input instance • computation• solution returned.

– who calls who• Structure of tree may be

complex.

Pros: Cons:

Page 34: 4 recursion

Different Representationsof Recursive Algorithms

Code - Implement on Computer

Stack of Stack Frames - Run on Computer

Tree of Stack Frames - View entire computation

Friends & Strong Induction

- Worry about one step at a time.

ProsViews

Page 35: 4 recursion

X = 2133Y = 2312ac = 483bd = 396(a+b)(c+d) = 1890XY = 4931496

X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

X = 54Y = 35ac = 15bd = 20(a+b)(c+d) = 72XY = 1890

X = 2Y = 2XY=4

X = 1Y = 3XY=3

X = 3Y = 5XY=15

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

X = 5Y = 3XY=15

X = 4Y = 5XY=20

X = 9Y = 8XY=72

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong InductionOne Friend for each

stack frame.

Each worries only about his job.

Page 36: 4 recursion

X = 2133Y = 2312ac = 483bd = 396(a+b)(c+d) = 1890XY = 4931496

X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

X = 54Y = 35ac = 15bd = 20(a+b)(c+d) = 72XY = 1890

X = 2Y = 2XY=4

X = 1Y = 3XY=3

X = 3Y = 5XY=15

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

X = 5Y = 3XY=15

X = 4Y = 5XY=20

X = 9Y = 8XY=72

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong InductionWorry about one

step at a time.Imagine that you are one specific

friend.

Page 37: 4 recursion

X = 33Y = 12ac = bd = (a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong Induction•Consider your input instance

Page 38: 4 recursion

X = 33Y = 12ac = ?bd = ?(a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong Induction•Consider your input instance•Allocate work

•Construct one or more subinstances

X = 3Y = 1XY=?

X = 3Y = 2XY=?

X = 6Y = 3XY=?

Page 39: 4 recursion

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong Induction•Consider your input instance•Allocate work

•Construct one or more subinstances•Assume by magic your friends give you the answer for these.

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

Page 40: 4 recursion

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong Induction•Consider your input instance•Allocate work

•Construct one or more subinstances•Assume by magic your friends give you the answer for these.

•Use this help to solve your own instance.

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

Page 41: 4 recursion

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong Induction•Consider your input instance•Allocate work

•Construct one or more subinstances•Assume by magic your friends give you the answer for these.

•Use this help to solve your own instance.•Do not worry about anything else.

•Who your boss is.•How your friends solve their instance.X = 3

Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

Page 42: 4 recursion

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong Induction

This technique is often referred to as

Divide and Conquer

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

Page 43: 4 recursion

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong InductionConsider generic instances.

ac bd (a+b)(c+d)

MULT(X,Y):

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Remember!Do not worry about

•Who your boss is.•How your friends solve their instance.

Page 44: 4 recursion

X = 2133Y = 2312ac = 483bd = 396(a+b)(c+d) = 1890XY = 4931496

X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

X = 54Y = 35ac = 15bd = 20(a+b)(c+d) = 72XY = 1890

X = 2Y = 2XY=4

X = 1Y = 3XY=3

X = 3Y = 5XY=15

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

X = 5Y = 3XY=15

X = 4Y = 5XY=20

X = 9Y = 8XY=72

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong InductionThis solves the

problem for every possible instance.

Page 45: 4 recursion

Friends & Strong InductionRecursive Algorithm:

•Assume you have an algorithm that works.•Use it to write an algorithm that works.

Page 46: 4 recursion

Friends & Strong InductionRecursive Algorithm:

•Assume you have an algorithm that works.•Use it to write an algorithm that works.

If I could get in,I could get the key.

Then I could unlock the door so that I can get in.

Circular Argument!

Page 47: 4 recursion

Friends & Strong InductionRecursive Algorithm:

•Assume you have an algorithm that works.•Use it to write an algorithm that works.

To get into my houseI must get the key from a smaller house

Page 48: 4 recursion

X = 33Y = 12ac = ?bd = ?(a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong Induction

•Allocate work •Construct one or more subinstances

X = 3Y = 1XY=?

X = 3Y = 2XY=?

X = 6Y = 3XY=?

Each subinstance must be a smaller instance

to the same problem.

Page 49: 4 recursion

Friends & Strong InductionRecursive Algorithm:

•Assume you have an algorithm that works.•Use it to write an algorithm that works.

Use brute force to get into the smallest house.

Page 50: 4 recursion

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong Induction

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Use brute force to solve the base case instances.

Page 51: 4 recursion

Friends & Strong InductionCarefully write the specifications for the problem.

Preconditions: Set of legal instances (inputs)

Why?

Postconditions: Required output

Page 52: 4 recursion

Friends & Strong InductionCarefully write the specifications for the problem.

Preconditions: Set of legal instances (inputs)

•To be sure that we solve the problem for every legal instance.

•So that we know –what we can give to a friend.

Postconditions: Required output •So that we know –what is expected of us.–what we can expect from our friend.

Related to Loop Invariants

Page 53: 4 recursion

Induction

S

i S S S S i S i

nS n

( )

, [ ( ) , ( ) , ( ) , . . . , ( ) ( )]

( )

0

0 1 2 1

S

iS i S i

nS n

( )

( ) ( )

( )

0

1

Strong Induction

Page 54: 4 recursion

Friends & Strong Induction

S

i S S S S i S i

nS n

( )

, [ ( ) , ( ) , ( ) , . . . , ( ) ( )]

( )

0

0 1 2 1

S n( )

•Induction Hypothesis:

``The recursive algorithm works for every instance of size n.''

``The algorithm works for every instance of any size.''

Base case

size i

Page 55: 4 recursion

Friends & Strong Induction Representation of an Algorithm

• Worry about one step at a time.

• An abstraction within which to develop, think about, and describe algorithms in such way that their correctness is transparent.

• Students resist it.• Students expect too much from

their friends.Each subinstance must be a smaller instanceto the same problem.

• Students expect too much from their boss.You only know your instance.

You do not know your boss’s instance.

Pros: Cons:

Page 56: 4 recursion

Output

Try explaining what this algorithm does by tracing it out.

I dare you!!!It is much easier to

TAKE ONE STEP AT A TIME

Page 57: 4 recursion

n=1 X

n=2 Y

Output

Page 58: 4 recursion

n=1 X

n=2

n=3

YA ? B ? C

Output

Page 59: 4 recursion

n=1 X

n=2

n=3

YA Y B X C

Output

Page 60: 4 recursion

n=1 X

n=2

n=3

YAYBXC

Output

Page 61: 4 recursion

n=1 X

n=2

n=3

n=4

YAYBXC

A ? B ? C

Output

Page 62: 4 recursion

n=1 X

n=2

n=3

n=4

YAYBXC

A AYBXC B Y C

Output

Page 63: 4 recursion

n=1 X

n=2

n=3

n=4

YAYBXC

AAYBXCBYC

Output

Page 64: 4 recursion

n=1 X

n=2

n=3

n=4

n=5

YAYBXC

AAYBXCBYC

A ? B ? C

Output

Page 65: 4 recursion

n=1 X

n=2

n=3

n=4

n=5

YAYBXC

AAYBXCBYC

A AAYBXCBYC B AYBXC C

Output

Page 66: 4 recursion

n=1 X

n=2

n=3

n=4

n=5

YAYBXC

AAYBXCBYC

AAAYBXCBYCBAYBXCC

Output

Page 67: 4 recursion

Towers of Hanoi

Page 68: 4 recursion

Towers of Hanoi

Page 69: 4 recursion

Towers of Hanoi

Get a job at the Towers of Hanoi company at level n=1,2,3,4,…..

Or get transferred in as the president& you decide what your vice president does.

Page 70: 4 recursion

Towers of Hanoi

Time: T(1)=1, T(n) = 2T(n-1) + 1 = (2n)

Page 71: 4 recursion

Time: T(1) =

2T(n-1) +3T(n-1) + T(n-2) + 3T(n) =

11 T(2) =

= 2(n) defined?

Page 72: 4 recursion

Sorting Problem Specification• Precondition: The input is a list of n values with the same value possibly repeated.

• Post condition: The output is a list consisting of the same n values in non-decreasing order.

88 149825

62

52

79

3023

31 14,23,25,30,31,52,62,79,88,98

Page 73: 4 recursion

Recursive Sorts

• Given list of objects to be sorted

• Split the list into two sublists.

• Recursively have a friend sort the two sublists.

• Combine the two sorted sublists into one entirely sorted list.

Page 74: 4 recursion

Minimal effort splitting Lots of effort recombining

Lots of effort splittingMinimal effort recombining

Four Recursive Sorts

Merge Sort Insertion Sort

Quick Sort Selection Sort

Size of Sublists

n/2,n/2 n-1,1

Page 75: 4 recursion

Merge Sort

88 149825

62

52

79

3023

31

Divide and Conquer

Page 76: 4 recursion

Merge Sort

88 149825

62

52

79

3023

31Split Set into Two

(no real work)

25,31,52,88,98

Get one friend to sort the first half.

14,23,30,62,79

Get one friend to sort the second half.

Page 77: 4 recursion

Merge Sort

Merge two sorted lists into one

25,31,52,88,98

14,23,30,62,79

14,23,25,30,31,52,62,79,88,98

Page 78: 4 recursion
Page 79: 4 recursion

Merge Sort Sort

Time: T(n) = = (n log(n))

2T(n/2) + (n)

Page 80: 4 recursion

Quick Sort

88 149825

62

52

79

3023

31

Divide and Conquer

Page 81: 4 recursion

Quick Sort

88 149825

62

52

79

3023

31

Partition set into two using randomly chosen pivot

14

2530

23

31

88 9862

79≤ 52 ≤

Page 82: 4 recursion

Quick Sort

14

2530

23

31

88 9862

79≤ 52 ≤

14,23,25,30,31

Get one friend to sort the first half.

62,79,98,88

Get one friend to sort the second half.

Page 83: 4 recursion

Quick Sort

14,23,25,30,31

62,79,98,88

52

Glue pieces together. (No real work)

14,23,25,30,31,52,62,79,88,98

Page 84: 4 recursion
Page 85: 4 recursion

Quick Sort

88 149825

62

52

79

3023

31

Let pivot be the first element in the list?

14

2530

23

88 9862

79≤ 31 ≤

52

Page 86: 4 recursion

Quick Sort

≤ 14 ≤

14,23,25,30,31,52,62,79,88,98

23,25,30,31,52,62,79,88,98

If the list is already sorted, then the slit is worst case unbalanced.

Page 87: 4 recursion

Quick Sort

Best Time:

Worst Time:

Expected Time:

T(n) = 2T(n/2) + (n) = (n log(n))

Page 88: 4 recursion

Quick Sort

T(n) = 2T(n/2) + (n) = (n log(n))

Best Time:

Worst Time:

Expected Time:

= (n2)T(n) = T(0) + T(n-1) + (n)

Page 89: 4 recursion

Quick Sort

T(n) = 2T(n/2) + (n) = (n log(n))

Best Time:

T(n) = T(0) + T(n-1) + (n)Worst Time:

Expected Time:

= (n2)

T(n) = T(1/3n) + T(2/3n) + (n)= (n log(n))

Page 90: 4 recursion

Quick SortExpected Time: T(n) = T(1/3n) + T(2/3n) + (n)

= (n log(n))

Top work (n)

2nd level3rd level

# levels =

(n)(n)

1/31/31/3

2/3

2/3

2/3

2/3

(log(n))3/2

log (n)

Page 91: 4 recursion

Kth Element Problem Specification• Precondition: The input is a list of n values and an integer k.

• Post condition: The kth smallest in the list. (or the first k smallest)

88 149825

62

52

79

3023

31

14,23,25,30,31,52,62,79,88,98

k=3

Output: 25

Page 92: 4 recursion

88 149825

62

52

79

3023

31

Partition set into two using randomly chosen pivot

14

2530

23

31

88 9862

79≤ 52 ≤

Kth Element Problem Specification

Page 93: 4 recursion

14

2530

23

31

88 9862

79≤ 52 ≤

Kth Element Problem Specification

k=3 r=5 elements on left

14,23,25,30,31

Get one friend to find k=3rd element in first half.

Output: 25

Page 94: 4 recursion

14

2530

23

31

88 9862

79≤ 52 ≤

Kth Element Problem Specification

k=8 r=5 elements on left

Get one friend to find k=8-5=3rd element in second half.

Output: 79

52,62,79,98,88

Page 95: 4 recursion
Page 96: 4 recursion

T(n) =

Kth Element Problem Specification

= (n)1 T(n/2) + (n)Best Time:

Expected Time:

Worst Time:n

log a/log b # of base cases == n0 = 1n

log 1/log 2 =

Page 97: 4 recursion

T(n) = = (n)

1 T(n/2) + (n)

Expected Time:

Kth Element Problem Specification

T(n) = 1 T(n-1) + (n)Worst Time:= (n2)

Best Time:

Page 98: 4 recursion

T(n) = = (n)

1 T(n/2) + (n)

T(n) = 1 T(2/3n) + (n)Expected Time: = (n)

Kth Element Problem Specification

T(n) = 1 T(n-1) + (n)Worst Time:= (n2)

Best Time:

Page 99: 4 recursion

Recursion on Trees

3

8

1

3 2

2

7

6

5

9

4

1

Page 100: 4 recursion

Recursion on TreesA binary tree is: - the empty tree - a node with a right and a left subtrees.

3

8

1

3 2

2

7

6

5

9

4

1

tree because

Page 101: 4 recursion

Recursion on TreesA binary tree is: - the empty tree - a node with a right and a left subtrees.

3

8

1

3 2

2

7

6

5

9

4

1

treetree

node

Page 102: 4 recursion

Recursion on TreesA binary tree is: - the empty tree - a node with a right and a left subtrees.

3

8

1

3 2

2

7

6

5

9

4

1

tree because

Page 103: 4 recursion

Recursion on TreesA binary tree is: - the empty tree - a node with a right and a left subtrees.

3

8

1

3 2

2

7

6

5

9

4

1

tree tree

node

Page 104: 4 recursion

Recursion on TreesA binary tree is: - the empty tree - a node with a right and a left subtrees.

3

8

1

3 2

2

7

6

5

9

4

1

tree because

Page 105: 4 recursion

Recursion on TreesA binary tree is: - the empty tree - a node with a right and a left subtrees.

3

8

1

3 2

2

7

6

5

9

4

1

tree

nodetree

Page 106: 4 recursion

Recursion on TreesA binary tree is: - the empty tree - a node with a right and a left subtrees.

3

8

1

3 2

2

7

6

5

9

4

1tree becauseempty

Page 107: 4 recursion

Recursion on Treesnumber of nodes = ?

3

8

1

3 2

2

7

6

5

9

4

1

65

Get help from friends

Page 108: 4 recursion

Recursion on Treesnumber of nodes

3

8

1

3 2

2

7

6

5

9

4

1

= number on left + number on right + 1= 6 + 5 + 1 = 12

65

Page 109: 4 recursion

Recursion on Trees

Base Case ?

3

8

1

3 2

2

7

6

5

9

4

1

number of nodes

1

Are we done?

Page 110: 4 recursion

Recursion on Trees

Base Case ?

3

8

1

3 2

2

7

6

5

9

4

1

number of nodes

?

Page 111: 4 recursion

Recursion on Trees

Base Case ?

3

8

1

3 2

2

7

6

5

9

4

1

number of nodes

0

Better base case!

Page 112: 4 recursion

Recursion on Trees

Base Case ?

3

8

1

3 2

2

7

6

5

9

4

1

number of nodes

1

Does this still need to be a base case?

Page 113: 4 recursion

Recursion on Trees

3

8

1

3 2

2

7

6

5

9

4

1

00

number of nodes= number on left + number on right + 1= 0 + 0 + 1 = 1

No!

Page 114: 4 recursion

Recursion on Trees

3

8

1

3 2

2

7

6

5

9

4

1

00

Most programmers don’t use the empty tree as a base case. This causes a lot more work.

Page 115: 4 recursion

Recursion on Trees

Page 116: 4 recursion

Recursion on Trees

3

Designing Program/Test Cases

generic generic

3

generic

3 0

00

0+0+1=1

n1 n2

n1 + n2 + 1

0n1

n1 + 1

Same code works!

Same code works!

Try same code

Try same code

Page 117: 4 recursion

Time: T(n) = = (n)

T(left) + T(right) + (1)

Recursion on Trees

One stack frame for each node in the tree

Page 118: 4 recursion

Recursion on Treessum of nodes = ?

3

8

1

3 2

2

7

6

5

9

4

1

2325

Get help from friends

Page 119: 4 recursion

Recursion on Treessum of nodes

3

8

1

3 2

2

7

6

5

9

4

1

= sum on left + sum on right + value at root= 23 + 25 + 3 = 51

2325

Page 120: 4 recursion

Recursion on Trees

Base Case ?

3

8

1

3 2

2

7

6

5

9

4

1

sum of nodes = ?

0

Page 121: 4 recursion

Recursion on Trees

Page 122: 4 recursion

Recursion on Trees

3

generic generic

3

generic

3 0

s1 s2

s1 + s2 + 3

0s1

s1 + 3

00

0+0+ 3 =3

Designing Program/Test Cases

Page 123: 4 recursion

Recursion on Treesmax of nodes = ?

3

8

1

3 2

2

7

6

5

9

4

1

89

Get help from friends

Page 124: 4 recursion

Recursion on Treesmax of nodes

3

8

1

3 2

2

7

6

5

9

4

1

= max( max on left, max on right, value at root)= max( 8, 9, 3) = 9

89

Page 125: 4 recursion

Recursion on Trees

Base Case ?

3

8

1

3 2

2

7

6

5

9

4

1

max of nodes = ?

?

Page 126: 4 recursion

3+4+2+8+…Sum so far is 17

Sum so far is 0

Recursion on Trees

NewSum = OldSum + nextElement = 0 + 3 = 3

Page 127: 4 recursion

3*4*2*3*…Product so far is 72

Product so far is 1

Recursion on Trees

NewProd = OldProd × nextElement = 1 × 3 = 3

Page 128: 4 recursion

Max(3,4,2,3,…Max so far is 4

Max so far is -

Recursion on Trees

NewMax = max(OldMax, next Element) = max( - , 3 ) = 3

Page 129: 4 recursion

Recursion on Trees

Page 130: 4 recursion

Recursion on Treesheight of tree = ?

3

8

1

3 2

2

7

6

5

9

4

1

34

Get help from friends

Page 131: 4 recursion

Recursion on Treesheight of tree

3

8

1

3 2

2

7

6

5

9

4

1

= max(height of left,height on right) + 1= max(3,4) + 1 = 4

34

Page 132: 4 recursion

Recursion on Trees

Base Case ?

3

8

1

3 2

2

7

6

5

9

4

1

height of tree

?

Page 133: 4 recursion

Recursion on Trees

3

8

1

3 2

2

7

6

5

9

4

1

height of tree?

2 nodes or 1 edge

0 nodes? edges

Base Case ?

Page 134: 4 recursion

Recursion on Trees

3

8

1

3 2

2

7

6

5

9

4

1

Work it out backwards

? edges

height == max(height of left,height on right) + 1= max(?,?) + 1

??

0 edge

Page 135: 4 recursion

Recursion on Trees

3

8

1

3 2

2

7

6

5

9

4

1

Work it out backwards

-1edges

height =

= max(height of left,height on right) + 1= max(-1,-1) + 1

-1-1

0 edge

Page 136: 4 recursion

Recursion on Trees

Page 137: 4 recursion

Recursion on Treesnumber of leaves = ?

3

8

1

3 2

2

7

6

5

9

4

1

32

Get help from friends

Page 138: 4 recursion

Recursion on Treesnumber of leaves

3

8

1

3 2

2

7

6

5

9

4

1

= number on left + number on right= 3 + 2 = 5

32

Page 139: 4 recursion

Recursion on Trees

Base Case ?

3

8

1

3 2

2

7

6

5

9

4

1

number of leaves

0

Page 140: 4 recursion

Recursion on Trees

3

generic generic

3

generic

3 0

L1 L2

L1 + L2

0L1

L1

00

0+0 = 0

Needs to be another base case.

1 No!

Designing Program/Test Cases

Page 141: 4 recursion

Recursion on Trees

Page 142: 4 recursion

Recursion on Trees

Page 143: 4 recursion

Recursion on Trees

Page 144: 4 recursion

Recursion on Trees

?

Drops the tree.

Page 145: 4 recursion

38

25

17

4 21

31

28 35

51

42

40 49

63

55 71

Define Problem: Binary Search Tree• PreConditions

– Key 25– A binary search tree.

– PostConditions– Find key in BST (if there).

Page 146: 4 recursion

38

25

17

4 21

31

28 35

51

42

40 49

63

55 71

Binary Search Tree

Its left children ≤ Any node ≤ Its right children

≤ ≤

Page 147: 4 recursion

Define Loop Invariant• Maintain a sub-tree.

• If the key is contained in the original tree, then the key is contained in the sub-tree.

key 17

38

25

17

4 21

31

28 35

51

42

40 49

63

55 71

Page 148: 4 recursion

Define Step• Cut sub-tree in half.• Determine which half the key would be in.• Keep that half.

key 17

38

25

17

4 21

31

28 35

51

42

40 49

63

55 71

If key < root,then key is in left half.

If key > root,then key is in right half.

If key = root,then key is found

Page 149: 4 recursion

Recursion on Trees

Page 150: 4 recursion

Recursion on Trees

Page 151: 4 recursion

Time: T(n) = = (n log(n))

2T(n/2) + (

Recursion on Trees

Computing max is too much work.

n)

Page 152: 4 recursion

Recursion on Trees

Extra info from belowExtra info from below

Page 153: 4 recursion

Time: T(n) = = (n log(n))

2T(n/2) + (

Recursion on Trees

Computing max is too much work.

n)1

n

Page 154: 4 recursion

Recursion on Trees

Extra info from above-

Page 155: 4 recursion

Heaps, Heap Sort, &Priority Queues

Page 156: 4 recursion

Heap Definition•Completely Balanced Binary Tree•The value of each node each of the node's children. •Left or right child could be larger.

Where can 1 go?Maximum is at root.

Where can 8 go?

Where can 9 go?

Page 157: 4 recursion

Heap Data StructureCompletely Balanced Binary Tree

Implemented by an Array

Page 158: 4 recursion

Make Heap

Get help from friends

Page 159: 4 recursion

Heapify

?Maximum needs

to be at root.Where is the maximum?

Page 160: 4 recursion

Find the maximum. Put it in place

?

Repeat

Heapify

Page 161: 4 recursion

Heap

Running Time:

Heapify

Page 162: 4 recursion
Page 163: 4 recursion
Page 164: 4 recursion

Make Heap

Get help from friends

T(n) = 2T(n/2) + log(n)

Running time:

= (n)

Heapify

Heap

Page 165: 4 recursion

Heaps Heap?

Another algorithm

Page 166: 4 recursion

?Heap

Page 167: 4 recursion

?Heap

Page 168: 4 recursion

?

Page 169: 4 recursion

Heap

Page 170: 4 recursion

Running Time:

i

log(n) -i2log(n) -i

Page 171: 4 recursion

Priority Queues

Page 172: 4 recursion

Insertion Sort

Largest i values are sorted on side.Remaining values are off to side.

6,7,8,9<3

415

2

Exit

79 km 75 km

Exit

Max is easier to find if a heap.

Insertion

Page 173: 4 recursion

Heap Sort

Largest i values are sorted on side.Remaining values are in a heap.

Exit

79 km 75 km

Exit

Page 174: 4 recursion

Heap Data Structure

Heap Array

5 3 4 2 1

98

76

Heap

Array

Page 175: 4 recursion

Heap SortLargest i values are sorted on side.Remaining values arein a heap

Exit

Put next value where it belongs.

79 km 75 km

Exit

Heap

?

Page 176: 4 recursion

Heap Sort

Page 177: 4 recursion

Heap Sort?

? ?

? ?

? ?

Sorted

Page 178: 4 recursion

Heap Sort

Running Time:

Page 179: 4 recursion

Recursion on TreesEvaluate Equation Tree = ?

127

Get help from friends

Page 180: 4 recursion

Recursion on TreesEvaluate Equation Tree

127

= rootop( value on left, value on right )= rootop(12,7) = 12 + 7 = 19

Page 181: 4 recursion

Recursion on TreesEvaluate Equation Tree

7

Base Case ?

Page 182: 4 recursion

Recursion on Trees

Page 183: 4 recursion

Derivatives

Page 184: 4 recursion
Page 185: 4 recursion
Page 186: 4 recursion
Page 187: 4 recursion
Page 188: 4 recursion
Page 189: 4 recursion
Page 190: 4 recursion

Recursion on TreesPrinting a Tree

When I taught this one year, a student needed just this algorithm for his job.

Page 191: 4 recursion

Recursion on Trees

Typing line by line?

Get help from friends

Page 192: 4 recursion

Recursion on Trees

One stack frame prints:His input gives:

his treewhether left or rightsting for each line

Page 193: 4 recursion

Recursion on Trees

He gives his friends:their treeswhether left or rightsting for each line

Page 194: 4 recursion
Page 195: 4 recursion
Page 196: 4 recursion
Page 197: 4 recursion
Page 198: 4 recursion

Parsing

(1×x-x×1/(x×x)×x-(x/x)×1)/(x×x)Input:

Output:

Page 199: 4 recursion

Parsing

Page 200: 4 recursion

Parsing

Page 201: 4 recursion

Parsing

Page 202: 4 recursion

Parsing

Page 203: 4 recursion

Parsing

Page 204: 4 recursion

Recursive Images

if n=0, draw

else draw

And recursivelyDraw here with n-1

if n=1

n=0

Page 205: 4 recursion

Recursive Images

if n=0, draw

else draw

And recursivelyDraw here with n-1

if n=2

n=1

Page 206: 4 recursion

Recursive Images

if n=0, draw

else draw

And recursivelyDraw here with n-1

if n=3

n=2

Page 207: 4 recursion

Recursive Images

if n=0, draw

else draw

And recursivelyDraw here with n-1

if n=30

Page 208: 4 recursion

Recursive Images

if n=1if n=2if n=3if n=4if n=5

if n=0

Page 209: 4 recursion

Recursive Images

if n=1

if n=0

if n=2if n=3if n=4if n=5

Page 210: 4 recursion

Recursive Images

L(n) = 4/3 L(n-1) = (4/3)n

Page 211: 4 recursion
Page 212: 4 recursion
Page 213: 4 recursion
Page 214: 4 recursion
Page 215: 4 recursion
Page 216: 4 recursion
Page 217: 4 recursion

Please feel free to ask questions!

Please give me feedback

so that I can better serve you.

Thanks for the feedback that you

have given me already.

Page 218: 4 recursion

It is too hard.

Feed Back

• Yes it is.

• Are you reading?

• Are you thinking about it on your own?

• Are you talking to friends?

• I have taught both harder and easier.

• Then students work harder and less,

• learn more material or less material.

• And say it is hard the same.

Page 219: 4 recursion

Other classes I can just show up without working between classes

and follow.This should be like that.

Feed Back

No. I facilitate learning. Not spoon feed.

Page 220: 4 recursion

It is too abstract. .

=

•I hope that you will see the simplicity and the beauty in it.

Feed Back

•Yes it is.

•My goal is to teach you to think abstractly.

Page 221: 4 recursion

Could you trace out the algorithms on examples?

Feed Back

•We don’t really have time.

•This would be good for you to do on your own.

•I do try to, when I think it will help.

Page 222: 4 recursion

Could you give more details of the code?

Feed Back

•It is not a coding course.

•I want to give you the big picture not of one algorithm but algorithms in general.

•It’s a trade off. One can get lost in the details.

Page 223: 4 recursion

I can’t follow the lectures.

•Are you reading before class.

Feed Back

•Ask questions.

I am so lost I don’t know what to ask.

•Tell me where you first got lost. •Ask me to slow down.

Page 224: 4 recursion

Assignments are too hard.

Feed Back

Yes. I want you to think about new and hard things on your own.

•Sometimes this is a mistake.•I want you to have thought about material before we teach it so that you appreciate it.

Assignments need material not covered in class.

Page 225: 4 recursion

Feed Back

Thanks for the feedback that you

have given me.

Page 226: 4 recursion

End