recursive algorithms introduction applications to numeric computation

245
Recursive Algorithms

Upload: andrew-shepherd

Post on 13-Jan-2016

248 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Recursive Algorithms Introduction Applications to Numeric Computation

Recursive Algorithms

Page 2: Recursive Algorithms Introduction Applications to Numeric Computation

Introduction

Applications to Numeric Computation

Page 3: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 3

Complex Numbers

• Remember how to multiply 2 complex numbers?

• (a+bi)(c+di) = [ac –bd] + [ad + bc] i

• Input: a,b,c,d Output: ac-bd, ad+bc

• If a real multiplication costs $1 and an addition cost a penny, what is the cheapest way to obtain the output from the input?

• Can you do better than $4.02?

Page 4: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 4

Johann Carl Friedrich Gauss (* 30. April 1777 in Braunschweig, † 23. Februar 1855 in Göttingen)

• Input: a,b,c,d Output: ac-bd, ad+bc

• m1 = ac

• m2 = bd

• A1 = m1 – m2 = ac-bd

• m3 = (a+b)(c+d) = ac + ad + bc + bd

• A2 = m3 – m1 – m2 = ad+bc

Gauss’ Method:

Total Cost?

$3.05!

Page 5: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 5

Question

• The Gauss method saves one multiplication out of four. It requires 25% less work.

• Could there be a context where performing 3 multiplications for every 4 provides a more dramatic savings?

• Let’s back up a bit.

Page 6: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 6

How to add 2 n-bit numbers.

**

**

**

**

**

**

**

**

**

**

**

+

Page 7: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 7

How to add 2 n-bit numbers.

**

*

**

**

**

**

**

* **

**

**

**

**

+

Page 8: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 8

How to add 2 n-bit numbers.

**

*

**

**

**

**

* **

* **

*

**

**

**

**

+

Page 9: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 9

How to add 2 n-bit numbers.

**

*

**

**

**

* **

* **

*

* **

*

**

**

**

**

+

Page 10: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 10

How to add 2 n-bit numbers.

**

*

**

**

* **

* **

*

* **

*

* **

*

**

**

**

**

+

Page 11: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 11

How to add 2 n-bit numbers.

**

*

* **

*

* **

*

* **

*

* **

*

* **

*

* **

*

* **

*

* **

*

* **

*

***

*

+*

*

Page 12: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 12

Time complexity of grade school addition

**

*

*

**

*

*

**

*

*

**

*

*

**

*

*

**

*

*

**

*

*

**

*

*

**

*

*

**

*

+*

*

*

**

*

On any reasonable computer adding 3 bits can be done in constant time.

( ) ( )T n n

Page 13: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 13

Is there a faster way to add?

• QUESTION: Is there an algorithm to add two n-bit numbers whose time grows sub-linearly in n?

Page 14: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 14

Any algorithm for addition must read all of the input bits

– Suppose there is a mystery algorithm that does not examine each bit

– Give the algorithm a pair of numbers. There must be some unexamined bit position i in one of the numbers

– If the algorithm returns the wrong answer, we have found a bug

– If the algorithm is correct, flip the bit at position i and give the algorithm this new input.

– The algorithm must return the same answer, which now is wrong.

Page 15: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 15

So any algorithm for addition must use time at least linear in

the size of the numbers.

Grade school addition is essentially as good as it can be.

Page 16: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 16

How to multiply 2 n-bit numbers.

X* * * * * * * *

* * * * * * * *

* * * * * * * * * * * * * * *

* * * * * * * * * * * * * * * *

* * * * * * * * * * * * * * * *

* * * * * * * * * * * * * * * *

* * * * * * * * * * * * * * * * *

n2

Page 17: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 17

How to multiply 2 nHow to multiply 2 n--bit numbers.bit numbers.

X* * * * * * * * * * * * * * * *

* * * * * * * ** * * * * * * *

* * * * * * * ** * * * * * * *

* * * * * * * ** * * * * * * *

* * * * * * * ** * * * * * * *

* * * * * * * * * * * * * * * *

n2

I get it! The total time is bounded by

cn2.

Page 18: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 18

How to multiply 2 n-bit numbers:Kindergarten Algorithm

a × b = a + a + a + ... + a

b

T(n) = θ(bn)

Fast?

(2 )!nn

Page 19: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 19

Grade School Addition: Linear timeGrade School Multiplication: Quadratic time

Kindergarten Multiplication: Exponential time

# of bits in numbers

tim

e

Page 20: Recursive Algorithms Introduction Applications to Numeric Computation

End of Lecture 6

March 23, 2009

Page 21: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 21

Neat! We have demonstrated that multiplication is a harder problem

than addition.

Mathematical confirmation of our common sense.

Page 22: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 22

Don’t jump to conclusions!We have argued that grade school multiplication uses more time than

grade school addition. This is a comparison of the complexity of two

algorithms.

To argue that multiplication is an inherently harder problem than

addition we would have to show that no possible multiplication algorithm runs in

linear time.

Page 23: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 23

Grade School Addition: θ(n) timeGrade School Multiplication: θ(n2) time

Is there a clever algorithm to multiply two numbers

in linear time?

Page 24: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 24

Despite years of research, no one knows!

Page 25: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 25

Is there a faster way to multiply two numbers

than the way you learned in grade school?

Page 26: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 26

Good question!

Page 27: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 27

Recursive Divide And Conquer

• DIVIDE a problem into smaller subproblems

• CONQUER them recursively

• GLUE the answers together so as to obtain the answer to the larger problem

Page 28: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 28

Multiplication of 2 n-bit numbers

• X =

• Y =

• X = a 2n/2 + b Y = c 2n/2 + d

• XY = ac 2n + (ad+bc) 2n/2 + bd

a b

c d

Page 29: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 29

Multiplication of 2 n-bit numbers

• X =

• Y =

• XY = ac 2n + (ad+bc) 2n/2 + bd

a b

c d

MULT(X,Y):

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

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

RETURN

MULT(a,c) 2n + (MULT(a,d) + MULT(b,c)) 2n/2 + MULT(b,d)

Page 30: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 30

Time required by MULT

• T(n) = time taken by MULT on two n-bit numbers

• What is T(n)?

Page 31: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 31

Recurrence Relation

•T(1) = k for some constant k

•T(n) = 4 T(n/2) + k’ n + k’’ for some constants k’ and k’’

MULT(X,Y):

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

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

RETURN

MULT(a,c) 2n + (MULT(a,d) + MULT(b,c)) 2n/2 + MULT(b,d)

Page 32: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 32

For example

• T(1) = 1

• T(n) = 4 T(n/2) + n

• How do we unravel T(n) so that we can determine its growth rate?

Page 33: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 33

Technique 1: (Substitution)

• Recurrence:

• Guess:

• Proof:

(1) 1

( ) 4 ( / 2) , 2,4,8

T

T n T n n n

2(*) ( ) 2T n n n

(*) (1) 2 1 1T

2 2

2 2

Now suppose (*) is satisfied for / 2.

( / 2) 2 / 2 / 2 / 2 / 2

Then by the recurrence relation,

( ) 4 ( / 2) 2 2 2 .

Thus (*) is also satisfied for n

n

T n n n n n

T n T n n n n n n n

Page 34: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 34

T(n)

Technique 2: Recursion Tree

• T(n) = n + 4 T(n/2)

•T(1) = 1

n

(n/2)

= n

T(n/2) T(n/2) T(n/2) T(n/2)

=

T(1) 1=

Page 35: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 35

n

T(n/2) T(n/2) T(n/2) T(n/2)

T(n) =

Page 36: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 36

n

T(n/2) T(n/2) T(n/2)

T(n) =

n/2

T(n/4) T(n/4) T(n/4) T(n/4)

Page 37: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 37

nT(n) =

n/2

T(n/4) T(n/4) T(n/4) T(n/4)

n/2

T(n/4) T(n/4) T(n/4) T(n/4)

n/2

T(n/4) T(n/4) T(n/4) T(n/4)

n/2

T(n/4) T(n/4) T(n/4) T(n/4)

Page 38: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 38

nT(n) =

n/2 n/2 n/2n/2

11111111111111111111111111111111 . . . . . . 111111111111111111111111111111111

n/4 n/4 n/4n/4n/4n/4n/4n/4n/4n/4n/4n/4n/4n/4n/4 n/4

Page 39: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 39

Level i is the sum of 4i copies of n/2i

1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1

. . . . . . . . . . . . . . . . . . . . . . . . . .

n/2 + n/2 + n/2 + n/2

n

n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4

0

1

2

i

log n2

Page 40: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 40

Level i is the sum of 4 i copies of n/ 2 i

1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1

. . . . . . . . . . . . . . . . . . . . . . . . . .

n/ 2 + n/ 2 + n/ 2 + n/ 2

n

n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4

0

1

2

i

lo g n2

=1n

= 4n/2

= 16n/4

= 4i n/2i

= 4lognn/2logn

=22logn1=n2

log

log 1 2 2

0

2 2 1 2 1 2 ( )n

i n

i

n n n n n n n

Page 41: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 41

Level i is the sum of 4 i copies of n/ 2 i

1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1

. . . . . . . . . . . . . . . . . . . . . . . . . .

n/ 2 + n/ 2 + n/ 2 + n/ 2

n

n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4

0

1

2

i

lo g n2

=1n

= 4n/2

= 16n/4

= 4i n/2i

= 4lognn/2logn

=22logn1=n2

2Geometric I ncreasing - dominated by last term: ( ) ( ( )) ( )f n f n n

Page 42: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 42

Divide and Conquer MULT: θ(n2) time Grade School Multiplication: θ(n2) time

All that work for nothing!

Page 43: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 43

MULT revisited

• MULT calls itself 4 times. Can you see a way to reduce the number of calls?

MULT(X,Y):

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

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

RETURN

MULT(a,c) 2n + (MULT(a,d) + MULT(b,c)) 2n/2 + MULT(b,d)

Page 44: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 44

Gauss’ Idea: Input: a,b,c,d Output: ac, ad+bc, bd

• A1 = ac

• A3 = bd

• m3 = (a+b)(c+d) = ac + ad + bc + bd

• A2 = m3 – A1- A3 = ad + bc

Page 45: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 45

Gaussified MULT (Karatsuba 1962)

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

(More precisely: T(n) = 2 T(n/2) + T(n/2 + 1) + kn)

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 e2n + (MULT(a+b, c+d) – e - f) 2n/2 + f

Page 46: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 46

nT(n) =

n/2

T(n/4)T(n/4)T(n/4)T(n/4)

n/2

T(n/4)T(n/4)T(n/4)T(n/4)

n/2

T(n/4)T(n/4)T(n/4)T(n/4)

n/2

T(n/4)T(n/4)T(n/4)T(n/4)

Page 47: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 47

n

T(n/2) T(n/2) T(n/2)

T(n) =

Page 48: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 48

n

T(n/2) T(n/2)

T(n) =

n/2

T(n/4)T(n/4)T(n/4)

Page 49: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 49

nT(n) =

n/2

T(n/4)T(n/4)T(n/4)

n/2

T(n/4)T(n/4)T(n/4)

n/2

T(n/4)T(n/4)T(n/4)

Page 50: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 50

Level i is the sum of 3i copies of n/2i

1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1

. . . . . . . . . . . . . . . . . . . . . . . . . .

n/2 + n/2 + n/2

n

n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4

0

1

2

i

log n2

Page 51: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 51

=1n

= 3n/2

= 9n/4

= 3i n/2iLevel i is the sum of 3 i copies of n/ 2 i

1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1

. . . . . . . . . . . . . . . . . . . . . . . . . .

n/ 2 + n/ 2 + n/ 2

n

n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4 + n/ 4

0

1

2

i

lo g n2 = 3lognn/2logn

=nlog31

log3 1.58...Geometric I ncreasing - dominated by last term: ( ) ( ( )) ( ) ( )f n f n n n

Page 52: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 52

Dramatic improvement for large n

Not just a 25% savings!

θ(n2) vs θ(n1.58..)

Page 53: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 53

Grade-School Multiplication

X* * * * * * * *

* * * * * * * *

* * * * * * * * * * * * * * *

* * * * * * * * * * * * * * * *

* * * * * * * * * * * * * * * *

* * * * * * * * * * * * * * * *

* * * * * * * * * * * * * * * * *

n2

2 2

2

multiplies + additions

( ) 2 bit operations

n n

T n n

Page 54: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 54

Gaussified MULT (Karatsuba 1962)

T(n) ≈ 3 T(n/2) + kn

What is k?

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 e2n + (MULT(a+b, c+d) – e - f) 2n/2 + f

e.g., k=8

Page 55: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 55

Dramatic improvement for large n

Not just a 25% savings!

θ(2n2) vs θ(8n1.58..)

Example:

A networking simulation requires 10 million multiplications of 16-bit integers.

Suppose that each bit operation takes 4 picosec on your machine (realistic).

Grade School Multiplication Time = 2 days 9 hours (do it over the weekend!)

Karatsuba Multiplication Time = 5.4 minutes (just enough time to grab a coffee!)

MATLAB takes 0.07 seconds on my machine (don’t blink!)

Page 56: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 56

Multiplication Algorithms

Kindergarten n2n

Grade School n2

Karatsuba n1.58…

Fastest Known

(Schönhage-Strassen algorithm, 1971)n logn loglogn

Page 57: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 57

What a difference a single recursive call makes!

• What are the underlying principles here?

• How can we systematically predict which recursive algorithms are going to save time, and which are not?

Page 58: Recursive Algorithms Introduction Applications to Numeric Computation

Recurrence Relations

T(1) = 1

T(n) = a T(n/b) + f(n)

Page 59: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 59

Recurrence Relations Time of Recursive Program

procedure Eg(int n)

if(n1) then

put “Hi”

else

loop i=1..f(n)

put “Hi”

loop i=1..a

Eg(n/b)

• Recurrence relations arise from the timing of recursive programs.

• Let T(n) be the # of “Hi”s on an input of “size” n.

Page 60: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 60

Recurrence Relations Time of Recursive Program

Given size 1, the program outputs T(1)=1 Hi’s.

Given size n, the stackframe outputs f(n) Hi’s.

Recursing on a instances of size n/b generates aT(n/b) “Hi”s.

For a total of T(1) = 1; T(n) = a·T(n/b) + f(n) “Hi”s.

procedure Eg(int n)

if(n1) then

put “Hi”

else

loop i=1..f(n)

put “Hi”

loop i=1..a

Eg(n/b)

Page 61: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 61

Technique 1: (Substitution)

• Recurrence:

• Guess:

• Proof:

(1) 1

( ) 4 ( / 2) , 2,4,8

T

T n T n n n

2(*) ( ) 2T n n n

(*) (1) 2 1 1T

2 2

2 2

Now suppose (*) is satisfied for / 2.

( / 2) 2 / 2 / 2 / 2 / 2

Then by the recurrence relation,

( ) 4 ( / 2) 2 2 2 .

Thus (*) is also satisfied for n

n

T n n n n n

T n T n n n n n n n

Page 62: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 62

More Generally, and Formally

(1) 1 & ( ) 4 ( / 2 )T T n T n n 2Hypothesis: ( ) ( )T n n

2

1Suppose that lower bound holds f or / 2 , i.e., / 2 ( / 2 )i c i T i

2 21 2 0 0 1 2i.e., , , 0 : , ( )c c n n n cn T n c n

LowerStep 1. Bound

2

1

,

( ) 4 /

S

2

ubstituti

4 / 2

ng

T i T i i c i i 2

1

14

2i

c i

2

14 / 4 / 2 1/ 4c i i i

2

1 2 1c i i i 1

1Suppose that

2c

21

Then ( ) 2 12

T i i i i 21 12 2

i 212

i 21c i Thus lower bound holds f or i!

Page 63: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 63

To Summarize2

1I f lower bound holds f or / 2 , i.e., / 2 ( / 2 )i c i T i

1

1with ,

2c

21Then lower bound holds f or , i.e., ( )i c i T i

Page 64: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 64

Base Case

Does lower bound hold f or 1?i

2 21

1 1(1) ( ) 1

2 2 Yes!c i T i

By induction, must also hold f or 2,3,4,5,...i

Follow similar process to prove upper bound.

e.g.,

1 2,3

2 4,5

3 6,7

i i

i i

i i

Page 65: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 65

•Recurrence Relation:

T(1) = 1 & T(n) = 4T(n/2) + n

•Guess: T(n) = an2 + bn + c

•Verify: Left Hand Side Right Hand Side

T(1) = a+b+c

T(n)

= an2 +bn+c

1

4T(n/2) + n

= 4 [a (n/2)2 + b (n/2) +c] + n

= an2 +(2b+1)n + 4c

Solving Technique 2Guess Form and Calculate Coefficients

Page 66: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 66

•Recurrence Relation:

T(1) = 1 & T(n) = 4T(n/2) + n

•Guess: T(n) = an2 + bn + c

•Verify: Left Hand Side Right Hand Side

T(1) = a+b+c

T(n)

= an2 +bn+c

1

4T(n/2) + n

= 4 [a (n/2)2 + b (n/2) +c] + n

= an2 +(2b+1)n + 4c

Solving Technique 2Guess Form and Calculate Coefficients

c=4c c=0

Page 67: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 67

•Recurrence Relation:

T(1) = 1 & T(n) = 4T(n/2) + n

•Guess: T(n) = an2 +bn + 0

•Verify: Left Hand Side Right Hand Side

T(1) = a+b+c

T(n)

= an2 +bn+c

1

4T(n/2) + n

= 4 [a (n/2)2 + b (n/2) +c] + n

= an2 +(2b+1)n + 4c

Solving Technique 2Guess Form and Calculate Coefficients

b = 2b+1 b = -1

Page 68: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 68

•Recurrence Relation:

T(1) = 1 & T(n) = 4T(n/2) + n

•Guess: T(n) = an2 - 1n + 0

•Verify: Left Hand Side Right Hand Side

T(1) = a+b+c

T(n)

= an2 +bn+c

1

4T(n/2) + n

= 4 [a (n/2)2 + b (n/2) +c] + n

= an2 +(2b+1)n + 4c

Solving Technique 2Guess Form and Calculate Coefficients

a=a

Page 69: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 69

•Recurrence Relation:

T(1) = 1 & T(n) = 4T(n/2) + n

•Guess: T(n) = an2 - 1n + 0

•Verify: Left Hand Side Right Hand Side

T(1) = a+b+c

T(n)

= an2 +bn+c

1

4T(n/2) + n

= 4 [a (n/2)2 + b (n/2) +c] + n

= an2 +(2b+1)n + 4c

Solving Technique 2Guess Form and Calculate Coefficients

a+b+c=1a-1+0=1a=2

2( ) 2T n n n

Page 70: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 70

•Recurrence Relation:

T(1) = 1 & T(n) = aT(n/b) + f(n)

Solving Technique 3Approximate Form and

Calculate Exponent

which is bigger?

Guess

Page 71: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 71

•Recurrence Relation:

T(1) = 1 & T(n) = aT(n/b) + f(n)

•Guess: aT(n/b) << f(n)

•Simplify: T(n) f(n)

Solving Technique 3Calculate Exponent

In this case, the answer is easy. T(n) = (f(n))

Page 72: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 72

•Recurrence Relation:

T(1) = 1 & T(n) = aT(n/b) + f(n)

•Guess: aT(n/b) >> f(n)

•Simplify: T(n) aT(n/b)

Solving Technique 3Calculate Exponent

In this case, the answer is harder.

Page 73: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 73

•Recurrence Relation:

T(1) = 1 & T(n) = aT(n/b)

•Guess: T(n) = cn

•Verify: Left Hand Side Right Hand Side

T(n)

= cn

aT(n/b)

= a [c (n/b) ]

= c a b n

Solving Technique 3Calculate Exponent

(log a/log b)= cn

1 = a b-

b = alog b = log a = log a/log b

Page 74: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 74

•Recurrence Relation:

T(1) = 1 & T(n) = 4T(n/2)

•Guess: T(n) = cn

•Verify: Left Hand Side Right Hand Side

T(n)

= cn

aT(n/b)

= a [c (n/b) ]

= c a b n

Solving Technique 3Calculate Exponent

(log a/log b)= cn

1 = a b-

b = alog b = log a = log a/log b

log4/ log2 2cn cn

Page 75: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 75

•Recurrence Relation:

T(1) = 1 & T(n) = aT(n/b) + f(n)

Solving Technique 3Calculate Exponent

If bigger then

T(n) = (f(n))

If bigger then(log a/log b)T(n) = (n )

And if aT(n/b) f(n) what is T(n) then?

Page 76: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 76

Technique 4: Recursion Tree Method

• T(n) = a T(n/b) + f(n)

f(n)

T(n/b) T(n/b) T(n/b) T(n/b)

T(n) =

T(1)

•T(1) = 1

1=

a

Page 77: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 77

f(n)

T(n/b) T(n/b) T(n/b) T(n/b)

T(n) =a

Page 78: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 78

f(n)

T(n/b) T(n/b) T(n/b)

T(n) =a

f(n/b)

T(n/b2)T(n/b2)T(n/ b2)T(n/ b2)

a

Page 79: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 79

f(n)T(n) =a

f(n/b)

T(n/b2)T(n/b2)T(n/ b2)T(n/ b2)

af(n/b)

T(n/b2)T(n/b2)T(n/ b2)T(n/ b2)

af(n/b)

T(n/b2)T(n/b2)T(n/ b2)T(n/ b2)

af(n/b)

T(n/b2)T(n/b2)T(n/ b2)T(n/ b2)

a

Page 80: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 8011111111111111111111111111111111 . . . . . . 111111111111111111111111111111111

f(n)T(n) =a

f(n/b)

T(n/b2) T(n/b2) T(n/ b2) T(n/ b2)

a

f(n/b)

T(n/b2) T(n/b2) T(n/ b2) T(n/ b2)

a

f(n/b)

T(n/b2) T(n/b2) T(n/ b2) T(n/ b2)

a

f(n/b)

T(n/b2) T(n/b2) T(n/ b2) T(n/ b2)

a

Page 81: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 81

Evaluating: T(n) = aT(n/b)+f(n)

Level

0

1

2

i

h

# Instances

Page 82: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 82

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

0

1

2

i

h

# Instances

Page 83: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 83

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

0 n

1

2

i

h

# Instances

Page 84: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 84

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

0 n

1 n/b

2

i

h

# Instances

Page 85: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 85

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

0 n

1 n/b

2 n/b2

i

h

# Instances

Page 86: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 86

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

0 n

1 n/b

2 n/b2

i n/bi

h n/bh

# Instances

Page 87: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 87

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

0 n

1 n/b

2 n/b2

i n/bi

h n/bh

# Instances

Page 88: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 88

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

0 n

1 n/b

2 n/b2

i n/bi

h n/bh = 1

base case

# Instances

Page 89: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 89

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

0 n

1 n/b

2 n/b2

i n/bi

h n/bh = 1

# Instances

Page 90: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 90

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

0 n

1 n/b

2 n/b2

i n/bi

h = log n/log b n/bh = 1

bh = nh log b = log nh = log n/log b

# Instances

Page 91: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 91

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

Work

in stack

frame

0 n

1 n/b

2 n/b2

i n/bi

h = log n/log b 1

# Instances

Page 92: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 92

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

Work

in stack

frame

0 n f(n)

1 n/b f(n/b)

2 n/b2 f(n/b2)

i n/bi f(n/bi)

h = log n/log b 1 T(1)

# Instances

Page 93: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 93

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

Work

in stack

frame

# stack frames

0 n f(n)

1 n/b f(n/b)

2 n/b2 f(n/b2)

i n/bi f(n/bi)

h = log n/log b n/bh T(1)

# Instances

Page 94: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 94

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

Work

in stack

frame

# stack frames

0 n f(n) 1

1 n/b f(n/b) a

2 n/b2 f(n/b2) a2

i n/bi f(n/bi) ai

h = log n/log b n/bh T(1) ah

# Instances

Page 95: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 95

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

Work

in stack

frame

# stack frames

0 n f(n) 1

1 n/b f(n/b) a

2 n/b2 f(n/b2) a2

i n/bi f(n/bi) ai

h = log n/log b n/bh T(1) ah

# Instances

Page 96: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 96

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

Work

in stack

frame

# stack frames

0 n f(n) 1

1 n/b f(n/b) a

2 n/b2 f(n/b2) a2

i n/bi f(n/bi) ai

h = log n/log b n/bh T(1) ah

# Instances

log / logn bha a log / loga bn

Page 97: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 97

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

Work

in stack

frame

# stack frames

Work in Level

0 n f(n) 1

1 n/b f(n/b) a

2 n/b2 f(n/b2) a2

i n/bi f(n/bi) ai

h = log n/log b n/bh T(1) nlog a/log b

# Instances

Page 98: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 98

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

Work

in stack

frame

# stack frames

Work in Level

0 n f(n) 1 1 · f(n)

1 n/b f(n/b) a a · f(n/b)

2 n/b2 f(n/b2) a2 a2 · f(n/b2)

i n/bi f(n/bi) ai ai · f(n/bi)

h = log n/log b n/bh T(1) nlog a/log b n · T(1)

log a/log b

Total Work T(n) = ∑i=0..h aif(n/bi)

# Instances

Page 99: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 99

Evaluating: T(n) = aT(n/b)+f(n)

= ∑i=0..h aif(n/bi)

If a Geometric Sum∑i=0..n xi θ(max(first term, last term))

Page 100: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 100

Evaluating: T(n) = aT(n/b)+f(n)

LevelInstance

size

Work

in stack

frame

# stack frames

Work in Level

0 n f(n) 1 1 · f(n)

1 n/b f(n/b) a a · f(n/b)

2 n/b2 f(n/b2) a2 a2 · f(n/b2)

i n/bi f(n/bi) ai ai · f(n/bi)

h = log n/log b n/bh T(1) nlog a/log b n · T(1)

log a/log b

Dominated by Top Level or Base Cases

Page 101: Recursive Algorithms Introduction Applications to Numeric Computation

End of Lecture 7

March 25, 2009

Page 102: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 102

Master Theorem: I ntuition

Suppose ( ) ( / ) ( ), 1, 1T n aT n b f n a b

Work at top level ( ) f nlog log / logWork at bottom level number of base cases = b a a bn n

logRunning time = max(work at top, work at bottom) = max( ( ), )b af n n

I f they are equal, then all levels are important:

logRunning time = sum of work over all levels = logb an n

Page 103: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 103

Theorem 4.1 (Master Theorem)

Suppose ( ) ( / ) ( ), 1, 1T n aT n b f n a b

log0 such that ( )1. ) F (I b af n O n logT ( ) ( E )H N b aT n n

log2 ( ) (. I F ) b af n nlog( ) (THE l ) gN ob aT n n n

log0 such that ( ) ( )3 . I F b af n n

AND

0 01, >0 such that ( / ) ( ) c n af n b cf n n n

( )THE ( (N ))T n f n

Dominated by base cases

Work at each level is comparable:

Sum work over all levels

Dominated by top level work

Additional regularity condition

Page 104: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 104

Theorem 4.1 (Master Theorem)

Suppose ( ) ( / ) ( ), 1, 1T n aT n b f n a b

log0 such that ( )1. ) F (I b af n O n logT ( ) ( E )H N b aT n n

log2 ( ) (. I F ) b af n nlog( ) (THE l ) gN ob aT n n n

log0 such that ( ) ( )3 . I F b af n n

AND

0 01, >0 such that ( / ) ( ) c n af n b cf n n n

( )THE ( (N ))T n f n

2e.g., ( ) 4 ( / 2) ( ) log log 4 2bT n T n f n a

e.g., 0.01

2( ) ?f n n

1.97( ) ?f n n

Page 105: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 105

Example 2: ( ) 4 ( / 2) 2nT n T n

4

2

a

b

log 2b an n

( ) 2nf n

logThus ( ) ( ) (Case 3: dominated by top level)b af n n

Page 106: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 106

Theorem 4.1 (Master Theorem)

Suppose ( ) ( / ) ( ), 1, 1T n aT n b f n a b

log0 such that ( )1. ) F (I b af n O n logT ( ) ( E )H N b aT n n

log2 ( ) (. I F ) b af n nlog( ) (THE l ) gN ob aT n n n

log0 such that ( ) ( )3 . I F b af n n

AND

0 01, >0 such that ( / ) ( ) c n af n b cf n n n

( )THE ( (N ))T n f n

Dominated by top level work

4

2

a

b

log 2b an n

( ) 2nf n

But what about this?

Page 107: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 107

Example 2: ( ) 4 ( / 2) 2nT n T n

4

2

a

b

log 2b an n

( ) 2nf n

logThus ( ) ( ) (Case 3: dominated by top level)b af n n

0 0

1, >0 such that ( / ) ( )

Additional regularity condition:

c n af n b cf n n n

n/ 2Thus we require that 4 2 2nc / 24 2 nc

0

1Let 6

2n c

0regularity condition holds f or 6, 0.5n c

Thus ( ) ( ( )) (2 ) nT n f n

Page 108: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 108

5Example 3: ( ) 4 ( / 2) logT n T n n n

4

2

a

b log 2b an n

5( ) logf n n n

logThus ( ) ( ) (Case 1: dominated by base cases)b af n O n

log 2Thus T(n)= ( ) = (n )b an

Page 109: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 109

Theorem 4.1 (Master Theorem)

Suppose ( ) ( / ) ( ), 1, 1T n aT n b f n a b

log0 such that ( )1. ) F (I b af n O n logT ( ) ( E )H N b aT n n

log2 ( ) (. I F ) b af n nlog( ) (THE l ) gN ob aT n n n

log0 such that ( ) ( )3 . I F b af n n

AND

0 01, >0 such that ( / ) ( ) c n af n b cf n n n

( )THE ( (N ))T n f n

Dominated by base cases

4

2

a

b log 2b an n

5( ) logf n n n

Page 110: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 110

2Example 4: ( ) 4 ( / 2)T n T n n

4

2

a

b log 2b an n

2( )f n n

logThus ( ) ( ) (Case 2: all levels signifi cant)b af n n

log 2Thus ( ) ( log ) = (n log )b aT n n n n

Page 111: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 111

Theorem 4.1 (Master Theorem)

Suppose ( ) ( / ) ( ), 1, 1T n aT n b f n a b

log0 such that ( )1. ) F (I b af n O n logT ( ) ( E )H N b aT n n

log2 ( ) (. I F ) b af n nlog( ) (THE l ) gN ob aT n n n

log0 such that ( ) ( )3 . I F b af n n

AND

0 01, >0 such that ( / ) ( ) c n af n b cf n n n

( )THE ( (N ))T n f n

Work at each level is comparable:

Sum work over all levels

4

2

a

b log 2b an n

2( )f n n

Page 112: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 112

0 5 10 15 200

5

10

15

20

25

30

35

n

f(n)

e.g. ( ) ( / 2) (1 .8cos ) T n T n n n

log 02Here log log 1 0 1 b a

ba n n

and ( ) (1 .8cos ) .2 ( ) f n n n n n

logThus ( ) , suggesting that Case 3 applies. b af n n

But does the regularity condition hold?

Master Theorem Case 3: When the Regularity Condition Fails

Page 113: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 113

e.g. ( ) ( / 2) (1 .8cos ) T n T n n n

0 5 10 15 200

5

10

15

20

25

30

35

n

f(n)

Does the regularity condition hold?

0We require that ( / ) ( ) for some constant 1, . af n b cf n c n n

( / 2) ( ) f n cf n

( / 2)(1 .8cos / 2 ) (1 .8cos ) n n cn n

(1/ 2)(1 .8cos / 2 ) (1 .8cos ) n c n

0 0Given arbitrary , select an

such that is even and / 2 is odd

n n n

n n

Then we require that (1/ 2)(1 .8) (1 .8)

.9 .2 4.5

c

c c

Thus the regularity condition does not hold.

Master Theorem Case 3: When the Regularity Condition Fails

Page 114: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 114

0 5 10 15 200

5

10

15

20

25

30

35

n

f(n)

So what is the solution?

Master Theorem Case 3: When the Regularity Condition Fails

Note that ( ) (1 .8cos ) ( ) f n n n n

( ) ( / 2) (1 .8cos ) T n T n n n

So in this case,

( ) ( ( )) ( ), despite f ailure of the reg. condition.T n f n n

Are there f ailures of the reg. condition

that result in ( ) ( ( ))?

:

T

Ques

n

on

f

i

n

t

Page 115: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 115

Master Theorem Case 3: When the Regularity Condition Fails

Are there f ailures of the reg. condition

that result in ( ) ( ( ))?

:

T

Ques

n

on

f

i

n

t

32

22

( ) 2 ( / 2) ( )

when log is evenwhere ( )

when log is odd

Consider T n T n f n

n nf n

n n

Think about this puzzle and ask yourself :

1. I s the fi rst condition of Case 3 satisfi ed?

2. I s the second (regularity) condition of Case 3 satisfi ed?

3. I s ( ) ( ( ))?T n f n

Let’s sleep on it.

Page 116: Recursive Algorithms Introduction Applications to Numeric Computation

Central Algorithmic Techniques

Recursion

Page 117: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 117

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 118: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 118

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 119: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 119

Code Representation of an Algorithm

• Runs on computers

• Precise and succinct

• I am not a computer

• I need a higher level of intuition.

• Prone to bugs

• Language dependent

Pros: Cons:

Page 120: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 120

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 121: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 121

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 122: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 122

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 123: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 123

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 124: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 124

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 125: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 125

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 126: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 126

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 127: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 127

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 128: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 128

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 129: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 129

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 130: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 130

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 131: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 131

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 132: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 132

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 133: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 133

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 134: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 134

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 135: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 135

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 136: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 136

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 137: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 137

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 138: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 138

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 139: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 139

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 140: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 140

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 141: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 141

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 142: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 142

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 143: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 143

Stack of Stack Frames Representation of an Algorithm

• Traces what actually occurs in the computer

• Concrete.

• Described in words it is impossible to follow

• Does not explain why it works.

• Demonstrates for only one of many inputs.

Pros: Cons:

Page 144: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 144

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 145: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 145

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 146: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 146

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

Pros: Cons:

Page 147: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 147

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 148: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 148

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 Induction

One Friend for each stack frame.

Each worries only about his job.

Page 149: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 149

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 Induction

Worry about one step at a time.

Imagine that you are one specific

friend.

Page 150: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 150

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 151: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 151

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 152: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 152

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 153: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 153

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 154: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 154

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, e.g.,

•Who your boss is.

•How your friends solve their instance.X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

Page 155: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 155

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 156: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 156

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

Page 157: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 157

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 Induction

This solves the problem for every possible instance.

Page 158: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 158

Friends & Strong Induction

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

Page 159: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 159

Friends & Strong Induction

Recursive 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 160: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 160

Friends & Strong Induction

Recursive 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 161: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 161

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 162: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 162

Friends & Strong Induction

Recursive 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 163: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 163

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 164: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 164

Friends & Strong Induction

Carefully write the specifications for the problem.

Preconditions: Set of legal instances (inputs)

Why?

Postconditions: Required output

Page 165: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 165

Friends & Strong Induction

Carefully 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 166: Recursive Algorithms Introduction Applications to Numeric Computation

Applications of Recursion

Another Numerical Computation Example

Page 167: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 167

• Given two integers, what is their greatest common divisor?

• e.g., gcd(56,24) =

The Greatest Common Divisor (GCD) Problem

divides d a

8

Given

N

, :

otation:

|

d a

d a

:k a kd

| a

Imp

nd

ortant Property:

| | ,d a d b d ax by x y

All integers divide 0: |Note 0: d d

Page 168: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 168

Euclid’s Trick

gcd( , ) gc 2d( , )a b a b b

gcd( ,

C

) gcd( ,

onseque :

)

nce

a b a b b

gcd( , ) gc 3d( , )a b a b b

. .,e g

| a

Imp

nd

ortant Property:

| | ,d a d b d ax by x y

Use this property to make the GCD problem eaId sea: ier!

Good!

Better!

Too Far!

optiWhat mal is the choice?

gcd( , ) gc modd( , )a b a b b modgcd 56,24 56 24( ) gcd( ) gcd(,24 8 4),2

56,24 56 3 24gcd( ) ,24gcd( ) gc )6, 4d( 1 2

56,24 56 2 24,24gcd( ) gcd( ) gcd(8,24)

56,24 56 24,2gcd( ) gcd( ) gcd( )4 32,24

Euclid of Alexandria, "The Father of Geometry" c. 300 BC

Page 169: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 169

Euclid’s Algorithm (circa 300 BC)

<Precondition: and are positive integers>

<Postcondition

if 0 th

Euclid(a,b)

return(

en

else

: returns gcd( , )>

)

return(Euclid( , od ))

m

a

b

b

a

a

b

b

b

a

met, siPrecondi nce moti don a b

Postcondition met, since

2. Otherwise, gcd( , ) gcd( , mod )a b b a b

3. Algorithm halts, since 0 mod a b b

1. 0 gcd( , ) gcd( ,0)b a b a a

Page 170: Recursive Algorithms Introduction Applications to Numeric Computation

End of Lecture 8

March 30, 2009

Page 171: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 171

Time Complexity

return(

E

)

uclid(a,

if 0 then

else

return(Euclid( , mod ))

b

)

a

b a b

b

2nd argument drops by factor of at least 2 every 2 iteraC tlaim: ions.

Proof:Iteration Arg 1 Arg 2

1 mod

2 mod mod( mod )

i a b

i b a b

i a b b a b

mod / 2. Then mod( moCa dse ) d / 2 o1 m: a b b b a b a b b

mod / 2. Then mod( mod ) /Case 2: 2 b a b b b a b b

Page 172: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 172

Time Complexity

return(

E

)

uclid(a,

if 0 then

else

return(Euclid( , mod ))

b

)

a

b a b

b

Let total number of recursive calls to Euclid.k

Each stackf rame must compute mod , which takes more than constant time.a b

2I t can be shown that the resulting time complexity is ( ) ( ).T n O n

Let input size number of bits used to represent and .n a b

/ 2 / 2Then 2 2 .k nb k n

Page 173: Recursive Algorithms Introduction Applications to Numeric Computation

Applications of Recursion

Data Organization

Page 174: Recursive Algorithms Introduction Applications to Numeric Computation

A Simple Example:

The Tower of Hanoi

Page 175: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 175

Tower of Hanoi

This job of mine is a bit daunting.Where do I start?

And I am lazy.

Page 176: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 176

Tower of Hanoi

At some point,the biggest disk

moves.I will do that job.

Page 177: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 177

Tower of Hanoi

To do this, the other disks must be in the

middle.

Page 178: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 178

Tower of Hanoi

How will these move?

I will get a friend to do it.And another to

move these.I only move the

big disk.

Page 179: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 179

Tower of Hanoi

Page 180: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 180

Tower of Hanoi

Time: T(1) = 1, T(n) = ≈ 2(2T(n-2)) ≈ 4(2T(n-3))

≈ 2T(n-1)≈ 4T(n-2)≈ 8T(n-3)≈ 2i T(n-i)≈ 2n

1 + 2T(n-1)

Page 181: Recursive Algorithms Introduction Applications to Numeric Computation

More Data Organization Examples

Sorting

Page 182: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 182

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 183: Recursive Algorithms Introduction Applications to Numeric Computation

Example: Merge Sort

Page 184: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 184

Merge Sort

88 149825

62

52

79

3023

31

Divide and Conquer

Page 185: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 185

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 186: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 186

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 187: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 187

Merge Sort

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

2T(n/2) + (n)

Page 188: Recursive Algorithms Introduction Applications to Numeric Computation

Example: Quick Sort

Page 189: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 189

Quick Sort

88 149825

62

52

79

3023

31

Divide and Conquer

Page 190: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 190

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 191: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 191

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 192: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 192

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 193: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 193

Page 194: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 194

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 195: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 195

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 list is worst case unbalanced.

Page 196: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 196

Quick Sort

Best Time:

Worst Time:

Expected Time:

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

Page 197: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 197

Quick Sort

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

Best Time:

Worst Time:

Expected Time:

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

Page 198: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 198

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) = (nlog(n))(The proof is not difficult, but it’s a little long)

Page 199: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 199

Expected Time Complexity for Quick Sort

Why is it reasonable to expect ( log ) time complex tQ: i y?n n

Because on average, the partition is not too unbalanA: ced.

Example: I magine a deterministic partition,

in which the 2 subsets are always in fi xed proportion, i.e.,

( 1) & ( 1), where , are constants, , [0...1], 1.p n q n p q p q p q

( 1)p n ( 1)q n

Page 200: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 200

Expected Time Complexity for Quick Sort

( 1)p n ( 1)q n

Then ( ) ( ( 1)) ( ( 1)) ( )T n T p n T q n n

wlog, suppose that .

Then recursion tree has depth k (log ) :

1 log / log(1/ )k

q p

n

q n k n q

( ) work done per level ( ) ( log ).n T n n n

Page 201: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 201

Properties of QuickSort

• In-place?

• Stable?

• Fast?

– Depends.

– Worst Case:

– Expected Case:

2( )n

( log ), with small constantsn n

Page 202: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 202

Heaps, Heap Sort, &Priority Queues

Page 203: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 203

Heapsort

• O(nlogn) worst case – like merge sort

• Sorts in place – like insertion sort

• Combines the best of both algorithms

Page 204: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 204

Heap Definition (MaxHeap)

• Balanced binary tree

• The value of each node each of the node's children.

• Left or right child could be next largest.

Where can 1 go?Maximum is at root.

Where can 8 go?

Where can 9 go?

Page 205: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 205

Some Additional Properties of Heaps

The height of a node of the heap is the number of edges on the

simple downard path f rom the node

( )

longe to a l a .st e f

h i i

h = 0

h = 1

h = 0

h = 2 h = 1

h = 3

The height of a heap is the height of the root.H

Page 206: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 206

Some Additional Properties of Heaps

2An -element heap has height logH nn

i = 1

i = 2

i = 3

i = 4 i = 5 i = 6 i = 7

i = 8 i = 9

Page 207: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 207

Some Additional Properties of Heaps

A heap of height has at least 2 nodes.HH n

i = 1

i = 2

i = 3

i = 4 i = 5 i = 6 i = 7

i = 8 i = 9

1A heap of height has at most 2 -1 nodes.HH n 12 2 1H Hn

Page 208: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 208

Heap Data StructureBalanced Binary Tree Implemented by an Array

Page 209: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 209

Make Heap

Get help from friendsNow we are just left with this problem

Page 210: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 210

Heapify

?Maximum must be at root.Where should the maximum be?

<pre-cond>: Left and right subtrees of A[i] are max heaps.

<post-cond>: Subtree rooted at i is a heap.

Max-Heapify(A, i, n)

i

Page 211: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 211

Find the maximum.

?

Repeat

Heapify

<pre-cond>: Left and right subtrees of A[i] are max heaps.

<post-cond>: Subtree rooted at i is a heap.

Max-Heapify(A, i, n)

i

Put it in place

Page 212: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 212

Heap

Running Time:

Heapify

<pre-cond>: Left and right subtrees of A[i] are max heaps.

<post-cond>: Subtree rooted at i is a heap.

Max-Heapify(A, i, n)

Page 213: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 213

<pre-cond>: Left and right subtrees of A[i] are max heaps.

<post-cond>: Subtree rooted at i is a heap.

Max-Heapify(A, i, n)

e.g., Max-Heapify (A,2,10)

Max-Heapify (A,4,10)

Max-Heapify (A,9,10)

Page 214: Recursive Algorithms Introduction Applications to Numeric Computation

End of Lecture 9

April 1, 2009

Page 215: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 215

MakeHeap

• MakeHeap uses Max-Heapify to reorganize the tree from bottom to top to make it a heap.

• MakeHeap can be written concisely in either recursive or iterative form.

Page 216: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 216

<pre-cond>:A[i n] is a balanced binary tree

<post-cond>:The subtree rooted at

if / 4

( , ( ), )

( , ( ), )

Max-Hea

is a hea

M

pif y

akeHeap

p

(

)

)

(

,

, ,

,

i n then

MakeHeap A LEFT i n

MakeHe

i

ap A RI GHT i

A i n

n

A i n

Recursive MakeHeap

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

Running time:

= (n)i

n

p/ 2 is of arentn n

grandp/ 4 is ot f arenn n

Invoke as MakeHeap (A, 1, n)

Page 217: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 219

Heaps Heap?

<pre-cond>:A[1 n] is a balanced binary tree

<post-cond>:A[1 n] is a heap

:All subtrees roo

f o

te

M

r / 2 downto 1

Max-Heap

d

if y( , , )

akeHeap(

at 1 are heaps

, )

LI i n

i n

A i

n

n

A

<pre-cond>: Left and right subtrees of A[i] are max heaps.

<post-cond>: Subtree rooted at i is a heap.

Max-Heapify(A, i, n)

Iterative MakeHeap

Page 218: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 220

<pre-cond>: Left and right subtrees of A[i] are max heaps.

<post-cond>: Subtree rooted at i is a heap.

Max-Heapify(A, i, n)

Iterative MakeHeap

?Heap

<pre-cond>:A[1 n] is a balanced binary tree

<post-cond>:A[1 n] is a heap

:All subtrees roo

f o

te

M

r / 2 downto 1

Max-Heap

d

if y( , , )

akeHeap(

at 1 are heaps

, )

LI i n

i n

A i

n

n

A

Page 219: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 221

<pre-cond>: Left and right subtrees of A[i] are max heaps.

<post-cond>: Subtree rooted at i is a heap.

Max-Heapify(A, i, n)

Iterative MakeHeap

?Heap

<pre-cond>:A[1 n] is a balanced binary tree

<post-cond>:A[1 n] is a heap

:All subtrees roo

f o

te

M

r / 2 downto 1

Max-Heap

d

if y( , , )

akeHeap(

at 1 are heaps

, )

LI i n

i n

A i

n

n

A

Page 220: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 222

<pre-cond>: Left and right subtrees of A[i] are max heaps.

<post-cond>: Subtree rooted at i is a heap.

Max-Heapify(A, i, n)

Iterative MakeHeap

?

<pre-cond>:A[1 n] is a balanced binary tree

<post-cond>:A[1 n] is a heap

:All subtrees roo

f o

te

M

r / 2 downto 1

Max-Heap

d

if y( , , )

akeHeap(

at 1 are heaps

, )

LI i n

i n

A i

n

n

A

Page 221: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 223

<pre-cond>: Left and right subtrees of A[i] are max heaps.

<post-cond>: Subtree rooted at i is a heap.

Max-Heapify(A, i, n)

Iterative MakeHeap

Heap

<pre-cond>:A[1 n] is a balanced binary tree

<post-cond>:A[1 n] is a heap

:All subtrees roo

f o

te

M

r / 2 downto 1

Max-Heap

d

if y( , , )

akeHeap(

at 1 are heaps

, )

LI i n

i n

A i

n

n

A

Page 222: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 224

Iterative MakeHeap

Runtime:

2Height of heap log n

1I t can be shown that the number of nodes at height

2h

nh

Time to heapif y f rom node at height h ( )O h

2log

10

( ) ( )2

n

hh

nT n O h

2log

0 2

n

hh

hO n O n

<pre-cond>:A[1 n] is a balanced binary tree

<post-cond>:A[1 n] is a heap

:All subtrees roo

f o

te

M

r / 2 downto 1

Max-Heap

d

if y( , , )

akeHeap(

at 1 are heaps

, )

LI i n

i n

A i

n

n

A

Page 223: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 225

Iterative MakeHeap

• Recursive and Iterative MakeHeap do essentially the same thing: Heapify from bottom to top.

• Difference:

– Recursive is “depth-first”

– Iterative is “breadth-first”

Page 224: Recursive Algorithms Introduction Applications to Numeric Computation

Using Heaps for Sorting

Page 225: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 227

Selection Sort

Largest i values are sorted on the right.Remaining values are off to the left.

6,7,8,9<3

415

2

Max is easier to find if a heap.

Page 226: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 228

4 3

1 2

Heap Sort

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

<pre-cond>:A[1...n] is a list of keys

<post-cond>:A[1...n] is sorted in non-decr

HeapS

easin

or

g

t( , )

order

A n

Page 227: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 229

4 3

1 2

4 3

1 2

Heap Data Structure

Heap Array

5 4 3 1 2

98

76

Heap

Array

Page 228: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 230

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

Put next value where it belongs.

Heap

?

<pre-cond>: Left and right subtrees of A[i] are max heaps.

<post-cond>: Subtree rooted at i is a heap.

Max-Heapify(A, i, n)

Page 229: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 231

Heap Sort

?

? ?

? ?

? ?

Sorted

Page 230: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 232

Heap Sort<pre-cond>:A[1...n] is a list of keys

<post-cond>:A[1...n] is sorted in non-decreasing order

: [1 ] is a he

HeapSor

ap

[

M

t

akeHeap(A,n)

1 ] contains the largest keys in n

f or dow

on-dec

nt

r

( ,

o 2

)

LI A i

A i

A

n

i

n

n

exchange [1] [ ]

Max-H

easing

eapif y

orde

( ,1,

r

1)

A A i

A i

Running Time:

Page 231: Recursive Algorithms Introduction Applications to Numeric Computation

Other Applications of Heaps

Page 232: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 234

Priority Queue

• Maintains dynamic set, A, of n elements, each with a key.

• Max-priority queue supports:

1. MAXIMUM(A)

2. EXTRACT-MAX(A, n)

3. INCREASE-KEY(A, i, key)

4. INSERT(A, key, n)

• Example Application: Schedule jobs on a shared computer.

Page 233: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 235

Priority Queues cont’d...

• MAXIMUM(A):

• EXTRACT-MAX(A,n):

Page 234: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 236

Priority Queue cont’d...

• INCREASE-KEY(A, i, key):

• INSERT(A, key, n):

Page 235: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 237

Binary Search Trees

• Support many dynamic-set operations

• Basic operations take time proportional to height h of tree.

(log ) for balanced treen

( ) for worst-cased unbalanced treen

Page 236: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 238

38

25

17

4 21

31

28 35

51

42

40 49

63

55 71

Binary Search Tree

Left children ≤ Node ≤ Right children

≤ ≤

Page 237: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 239

BST Data Structure

Page 238: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 240

Insertion

<pre-cond>: is a BST, a node to be inserted

<post-cond>: is

Tree-I nser

a BST wit

t( ,

h insert

)

ed

T z

T z

T z

Page 239: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 241

38

25

17

4 21

31

28 35

51

42

40 49

63

55 71

Insertion

[ ] 36key z?

36( ) ( )T n h

Page 240: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 242

Building a Tree

<pre-cond>: is a set of nodes

<post-cond>: Returns a BST consisting of the node

NI L

f o

B

r in

Tree-I nsert

s in

uild-BS

,

T

( )

(Z)

T

z Z

T

Z

z

Z

For balanced tree, number of nodes inserted into tree of height is 2hh

Time for each insertion ( )h

2log

0

Thus ( ) 2 ( )n

h

h

T n h

( log )n n

Page 241: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 243

38

25

17

4 21

31

28 35

51

42

40 49

63

55 71

Searching the Tree• PreConditions

– Key 25– A binary search tree.

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

Page 242: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 244

Searching the Tree

• 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 243: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 245

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 244: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 246

Searching the Tree

<pre-cond>: is a BST, is a key to search f or

<post-cond>: returns the node matching if it exist

Tree-Sea

s or NI L

rch

ot

( , )

herwise

x k

x k

k

Runtime ( )h

Page 245: Recursive Algorithms Introduction Applications to Numeric Computation

COSC 3101, PROF. J. ELDER 247

Why use (balanced) binary search trees?

• What is the advantage over a sorted linear array?

– Search time is the same

– However, maintaining (inserting, deleting, modifying) is

(logn) for balanced BSTs

(n) for arrays