lecture 7 - university of washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡...

39
Lecture 7: Analyzing Recursive Code CSE 373: Data Structures and Algorithms

Upload: others

Post on 12-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Lecture 7: Analyzing Recursive Code

CSE 373: Data Structures and

Algorithms

Page 2: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Administrivia

Project 1 Part 1 due Wednesday

Exercise 1 due Friday.

Page 3: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Where Are We?

Analyzing Code:

So far:- Writing a Code Model

- Simplifying to ๐‘‚, ฮฉ, ฮ˜

- Formally proving ๐‘‚, ฮฉ, ฮ˜

- Worst case vs. Best case

This week- Recursive Code

- Dictionaries!

Page 4: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Binary Search

int binarySearch(int[] arr, int toFind, int lo, int hi){

if( hi < lo )

return -1;

if(hi == lo)

if(arr[hi] == toFind)

return hi;

return -1;

int mid = (lo+hi) / 2;

if(arr[mid] == toFind)

return mid;

else if(arr[mid] < toFind)

return binarySearch(arr, toFind, mid+1, hi);

else

return binarySearch(arr, toFind, lo, mid-1);

}

Page 5: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Binary search runtime

For an array of size N, it eliminates ยฝ until 1 element remains.N, N/2, N/4, N/8, ..., 4, 2, 1

- How many divisions does it take?

Think of it from the other direction:- How many times do I have to multiply by 2 to reach N?

1, 2, 4, 8, ..., N/4, N/2, N

- Call this number of multiplications "x".

2x = N

x = log2 N

Binary search is in the logarithmic complexity class.

HUNTER SCHAFER - CSE 143 SU 19

Page 6: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Moving Forward

The analysis is correct! But itโ€™s a little ad hoc.

It works great for binary search, but weโ€™re going to deal with more complicated recursive code in this course.

We need more powerful tools.

Page 7: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

ModelLetโ€™s start by just getting a model. Let ๐‘‡(๐‘›) be our model for the worst-case running time of binary search.

int binarySearch(int[] arr, int toFind, int lo, int hi){

if( hi < lo )

return -1;

if(hi == lo)

if(arr[hi] == toFind)

return hi;

return -1;

int mid = (lo+hi) / 2;

if(arr[mid] == toFind)

return mid;

else if(arr[mid] < toFind)

return binarySearch(arr, toFind, mid+1, hi);

else

return binarySearch(arr, toFind, lo, mid-1);

}

๐‘‡ ๐‘› =

2 if ๐‘› = 04 if ๐‘› = 1

5 + ๐‘‡๐‘›

2othwerwise

Page 8: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Recurrence

Our code is recursive.

It makes sense that our model will be recursive too!

A recursive definition of a function is called a recurrence.

Itโ€™s a lot like recursive code:

-At least one base case and at least one recursive case.

-The cases of your recurrence usually correspond exactly to the cases of the code.

-Input size should be getting smaller.

Page 9: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Write a recurrence

int recursiveFunction(int n){

if(n < 3)

return 3;

for(int int i=0; i < n; i++)

System.out.println(i);

int val1 = recursiveFunction(n/3);

int val2 = recursvieFunction(n/3);

return val1 * val2;

}๐‘‡ ๐‘› = แ‰

2 if ๐‘› < 3

2๐‘‡๐‘›

3+ ๐‘› otherwise

pollEV.com/cse373su19

Write a recurrence for the

running time of recursiveFunction

Page 10: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Recurrence to Big-O

Alright whatโ€™s the big-O?

Thereโ€™s another similarity between recursive functions and recursive code.

Itโ€™s still really hard to tell what the big-O is just by looking at it.

๐‘‡ ๐‘› = แ‰2 if ๐‘› < 3

2๐‘‡๐‘›

3+ ๐‘› otherwise

Page 11: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

I canโ€™t tell what the big-O is. What do we do?

Itโ€™s ok.

Mathematicians and computer scientists have been hard at work.

And theyโ€™ve written books.

And the answer to our problem is in one of them.

Page 12: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Master Theorem

CSE 373 SP 18 - KASEY CHAMPION 12

๐‘‡ ๐‘› = แ‰๐‘‘ if ๐‘› is at most some constant

๐‘Ž๐‘‡๐‘›

๐‘+ ๐‘“ ๐‘› otherwise

Given a recurrence of the following form, where ๐‘Ž, ๐‘, ๐‘, and ๐‘‘ are constants:

Where ๐‘“ ๐‘› is ฮ˜ ๐‘›๐‘

๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›๐‘log๐‘ ๐‘Ž < ๐‘

log๐‘ ๐‘Ž = ๐‘ ๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›๐‘ log ๐‘›

log๐‘ ๐‘Ž > ๐‘ ๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›log๐‘ ๐‘Ž

If

If

If

then

then

then

Page 13: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Master Theorem

๐‘‡ ๐‘› = แ‰๐‘‘ if ๐‘› is at most some constant

๐‘Ž๐‘‡๐‘›

๐‘+ ๐‘“ ๐‘› otherwise

Given a recurrence of the following form, where ๐‘Ž, ๐‘, ๐‘, and ๐‘‘ are constants:

Where ๐‘“ ๐‘› is ฮ˜ ๐‘›๐‘

๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›๐‘log๐‘ ๐‘Ž < ๐‘

log๐‘ ๐‘Ž = ๐‘ ๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›๐‘ log ๐‘›

log๐‘ ๐‘Ž > ๐‘ ๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›log๐‘ ๐‘Ž

If

If

If

then

then

then

๐‘‡ ๐‘› = แ‰2 if ๐‘› < 3

2๐‘‡๐‘›

3+ ๐‘› otherwise

log3 2 < 1Weโ€™re in case 1๐‘‡ ๐‘› โˆˆ ฮ˜(๐‘›)

Page 14: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Binary Search Trees

Page 15: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Binary Search Trees

We have one more algorithm analysis topicโ€ฆ

But first a little bit about binary search trees.

Page 16: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Review: Trees!

A tree is a collection of nodes- Each node has at most 1 parent and 0 or more children

Root node: the only node with no parent, โ€œtopโ€ of the tree

Leaf node: a node with no children

Edge: a pointer from one node to another

Subtree: a node and all it descendants

Height: the number of edges contained in the longest path from root node to some leaf node

CSE 373 SP 18 - KASEY CHAMPION 16

1

2 5

3 6 7

4 8

Page 17: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Review: Maps

map: Holds a set of distinct keys and a collection of values, where each key is associated with one value.- a.k.a. "dictionary"

CSE 373 19 SU - ROBBIE WEBER

key value

โ€œyou" 22

key value

โ€œin" 37

key value

โ€œthe" 56

key value

โ€œat" 43

map.get("the") 56

Dictionary ADT

put(key, item) add item to

collection indexed with key

get(key) return item

associated with key

containsKey(key) return if key

already in use

remove(key) remove item

and associated key

size() return count of items

state

behavior

Set of items & keys

Count of items

supported operations:- put(key, value): Adds a given item into

collection with associated key,

- if the map previously had a mapping for the given key, old value is replaced

- get(key): Retrieves the value mapped to the key

- containsKey(key): returns true if key is already associated with value in map, false otherwise

- remove(key): Removes the given key and its mapped value

Page 18: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Implement a Dictionary

Binary Search Trees allow us to:- quickly find what weโ€™re looking for

- add and remove values easily

Letโ€™s use them to implement dictionaries!

CSE 373 SP 18 - KASEY CHAMPION 18

10

โ€œfooโ€

7

โ€œbarโ€

12

โ€œbazโ€

9

โ€œshoโ€

5

โ€œfoโ€

15

โ€œsupโ€

13

โ€œbooโ€

8

โ€œbyeโ€

1

โ€œhiโ€

Page 19: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Binary Search Tree

Invariants- Things that are always true.

- The way you make sure your data structure works and is efficient.

Binary Search Tree invariants:

-For every node with key ๐‘˜:

-The left subtree has only keys smaller than ๐‘˜.

-The right subtree has only keys greater than ๐‘˜.

CSE 373 SP 18 - KASEY CHAMPION 19

10

โ€œfooโ€

7

โ€œbarโ€

12

โ€œbazโ€

9

โ€œshoโ€

5

โ€œfoโ€

15

โ€œsupโ€

13

โ€œbooโ€

8

โ€œbyeโ€

1

โ€œhiโ€

Page 20: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

BST Invariants

Why write down invariants?

They help us write methods?

How does get(key) work?

- Is the current node the one weโ€™re looking for?

-Return itโ€™s value

-Is the current node null?

- Itโ€™s not in there

- Is the current nodeโ€™s key too small?

-Recurse on the right subtree

- Is the current nodeโ€™s key too big?

-Recurse on the left subtree

Binary Search Tree invariants:

For every node with key ๐‘˜:

The left subtree has only keys smaller than ๐‘˜.

The right subtree has only keys greater than ๐‘˜.

How does put(key, value) work?

Letโ€™s just put it anywhere?

No! Remember the invariants!

Also remember key might already be

in the dictionary.find first

If key is in there, overwrite the value

Otherwise, wherever we ended up is

where the new node should go.

Page 21: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Are These Binary Search Trees?

2

3

8

6

9

4

52

73

9

8 10

6

1

Page 22: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

BSTs as dictionaries

Letโ€™s figure out the worst case of get() for two different states our BST could be in.

Perfectly balanced โ€“ for every node, its

descendants are split evenly between

left and right subtrees.

Degenerate โ€“ for every node, all of its

descendants are in the right subtree.

9

2

1 3

6

5 7

4

8

10

12

15

14

11 13

1

2

3

4

15

โ€ฆ

Page 23: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

BSTs as dictionaries

Letโ€™s figure out the worst case of get() for two different states our BST could be in.

Perfectly balanced โ€“ for every node, its

descendants are split evenly between

left and right subtrees.

9

2

1 3

6

5 7

4

8

10

12

15

14

11 13

get() is a recursive method!

๐‘‡ ๐‘› = แ‰๐‘‡

๐‘›

2+ 1 if ๐‘› > 1

3 otherwise

๐‘‡ ๐‘› = แ‰๐‘‘ if ๐‘› is at most some constant

๐‘Ž๐‘‡๐‘›

๐‘+ ๐‘“ ๐‘› otherwise

Where ๐‘“ ๐‘› is ฮ˜ ๐‘›๐‘

log๐‘ ๐‘Ž = ๐‘ ๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›๐‘ log ๐‘›

log๐‘ ๐‘Ž > ๐‘ ๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›log๐‘ ๐‘Ž

If

If

๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›๐‘log๐‘ ๐‘Ž < ๐‘If then

then

then

log2 1 = 0 = ๐‘›0 Case 2

ฮ˜(๐‘›0 log ๐‘›) = ฮ˜(log ๐‘›) .

Page 24: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

BSTs as dictionaries

Letโ€™s figure out the worst case of get() for two different states our BST could be in.

Degenerate โ€“ for every node, all of its

descendants are in the right subtree.

1

2

3

4

15

โ€ฆ

๐‘‡ ๐‘› = แ‰๐‘‘ if ๐‘› is at most some constant

๐‘Ž๐‘‡๐‘›

๐‘+ ๐‘“ ๐‘› otherwise

Where ๐‘“ ๐‘› is ฮ˜ ๐‘›๐‘

log๐‘ ๐‘Ž = ๐‘ ๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›๐‘ log ๐‘›

log๐‘ ๐‘Ž > ๐‘ ๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›log๐‘ ๐‘Ž

If

If

๐‘‡ ๐‘› โˆˆ ฮ˜ ๐‘›๐‘log๐‘ ๐‘Ž < ๐‘If then

then

then

get() is a recursive method!

๐‘‡ ๐‘› = แ‰Š๐‘‡ ๐‘› โˆ’ 1 + 1 if ๐‘› > 13 otherwise

Master Theorem doesnโ€™t apply!

Page 25: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

I canโ€™t tell what the big-O is. What do we do?

Itโ€™s ok.

Mathematicians and computer scientists have been hard at work.

And theyโ€™ve written books.

And Iโ€™ve checked them all

Page 26: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

This meme is outdated/unfunny.

This meme is funny.

Page 27: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Donโ€™t Panic

The books donโ€™t have a nice theorem;

They do have methods for figuring out the big-O.

Page 28: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Unrolling

๐‘‡ ๐‘› = แ‰Š๐‘‡ ๐‘› โˆ’ 1 + 1 if ๐‘› > 13 otherwise

Idea: keep plugging the definition of ๐‘‡() into itself.

Until you find the pattern and can hit the base case.

Page 29: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Unrolling

๐‘‡ ๐‘› =

๐‘‡ ๐‘› = แ‰Š๐‘‡ ๐‘› โˆ’ 1 + 1 if ๐‘› > 13 otherwise

๐‘‡ ๐‘› โˆ’ 1 + 1[๐‘‡ ๐‘› โˆ’ 1 โˆ’ 1 + 1] + 1 = ๐‘‡ ๐‘› โˆ’ 2 + 1 + 1๐‘‡ ๐‘› โˆ’ 2 โˆ’ 1 + 1 + 1 + 1 = ๐‘‡ ๐‘› โˆ’ 3 + 1 + 1 + 1๐‘‡ ๐‘› โˆ’ 3 โˆ’ 1 + 1 + 1 + 1 + 1 = ๐‘‡ ๐‘› โˆ’ 4 + 1 + 1 + 1 + 1

๐‘‡ ๐‘› โˆ’ ๐‘– + ๐‘– for any ๐‘–.The thing we donโ€™t understand is ๐‘‡(). We can get rid of it by hitting the base case.

Set ๐‘– so that ๐‘› โˆ’ ๐‘– = 1. ๐‘– = ๐‘› โˆ’ 1

๐‘‡ ๐‘› โˆ’ ๐‘› โˆ’ 1 + ๐‘› โˆ’ 1

๐‘‡ 1 + ๐‘› โˆ’ 1 = 3 + ๐‘› โˆ’ 1 = ๐‘› + 2

๐‘‡ ๐‘› = ๐‘› + 2

Page 30: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

We did it!

For BSTs:

If weโ€™re in the case where everything is balanced, we have a much better dictionary.

But if have that degenerate BST, weโ€™re no better off than with an array or linked list.

For analyzing code:

We didnโ€™t just get the big-ฮ˜, we actually got an exact expression too!

Letโ€™s try another one!

Page 31: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

More Practice

public int dumbFindMax(int[] arr, int hi){

if(hi == 0)

return arr[0];

int maxInd = 0;

for(int i=0; i<hi; i++){

if(arr[i] > arr[maxInd])

maxInd=i;

}

return Math.max(arr[maxInd], dumbFindMax(arr, hi-1));

}

Write a recurrence to describe

the running time of this

function, then find the big-ฮ˜ for

the running time.

Page 32: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

๐‘‡ ๐‘› = แ‰Š๐‘‡ ๐‘› โˆ’ 1 + ๐‘› if ๐‘› โ‰ฅ 21 otherwise

Page 33: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise
Page 34: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

๐‘‡ ๐‘› = แ‰Š๐‘‡ ๐‘› โˆ’ 1 + ๐‘› if ๐‘› โ‰ฅ 21 otherwise

๐‘‡ ๐‘› โˆ’ 1 + ๐‘›๐‘‡ ๐‘› โˆ’ 1 โˆ’ 1 + ๐‘› โˆ’ 1 + ๐‘› = T n โˆ’ 2 + n โˆ’ 1 + n๐‘‡ ๐‘› โˆ’ 3 + ๐‘› โˆ’ 2 + ๐‘› โˆ’ 1 + ๐‘›๐‘‡ ๐‘› โˆ’ 4 + ๐‘› โˆ’ 3 + ๐‘› โˆ’ 2 + ๐‘› โˆ’ 1 + ๐‘›

๐‘‡ ๐‘› โˆ’ ๐‘– +

๐‘—=0

๐‘–โˆ’1

๐‘› โˆ’ ๐‘—

Plug in ๐‘– so ๐‘› โˆ’ ๐‘– is 1

๐‘‡ ๐‘› โˆ’ (๐‘› โˆ’ 1) +

๐‘—=0

๐‘›โˆ’1โˆ’1

๐‘› โˆ’ ๐‘— =

Page 35: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

1 +

๐‘—=0

๐‘›โˆ’2

๐‘› โˆ’ ๐‘— = 1 +

๐‘—=0

๐‘›โˆ’2

๐‘› โˆ’

๐‘—=0

๐‘›โˆ’2

๐‘—

= 1 + ๐‘› ๐‘› โˆ’ 1 โˆ’

๐‘—=0

๐‘›โˆ’2

๐‘—

= 1 + ๐‘› ๐‘› โˆ’ 1 โˆ’๐‘› โˆ’ 1 ๐‘› โˆ’ 2

2

= ๐‘›2 โˆ’ ๐‘› โˆ’๐‘›2

2+

3๐‘›

2โˆ’ 1

โˆˆ ฮ˜(๐‘›2)

๐‘‡ ๐‘› = แ‰Š๐‘‡ ๐‘› โˆ’ 1 + ๐‘› if ๐‘› โ‰ฅ 21 otherwise

Page 36: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

๐‘‡ ๐‘› = เต3๐‘‡

๐‘›

4+ ๐‘›2 if ๐‘› > 1

4 otherwise

We can unroll to get the answer here, but itโ€™s really easy to make a small algebra mistake.

If that happens we might not be able to find the pattern-Or worse find the wrong pattern.

Thereโ€™s a way to organize our algebra so itโ€™s easier to find the pattern.

Page 37: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

๐‘‡๐‘›

4+ ๐‘‡

๐‘›

4+ ๐‘‡

๐‘›

4+ ๐‘›2

๐‘‡ ๐‘›

๐‘‡๐‘›

16+ ๐‘‡

๐‘›

16+ ๐‘‡

๐‘›

16+

๐‘›

4

2

๐‘‡๐‘›

4 ๐‘‡๐‘›

4๐‘‡

๐‘›

4๐‘‡

๐‘›

16+ ๐‘‡

๐‘›

16+ ๐‘‡

๐‘›

16+

๐‘›

4

2๐‘‡

๐‘›

16+ ๐‘‡

๐‘›

16+ ๐‘‡

๐‘›

16+

๐‘›

4

2

Tree Method Practice

39

๐‘‡ ๐‘› =4 ๐‘คโ„Ž๐‘’๐‘› ๐‘› โ‰ค 1

3๐‘‡๐‘›

4+ ๐‘›2 ๐‘œ๐‘กโ„Ž๐‘’๐‘Ÿ๐‘ค๐‘–๐‘ ๐‘’

n2

n

4

2

โ€ฆ โ€ฆ

n

4

2 n

4

2

๐‘‡๐‘›

16๐‘‡

๐‘›

16๐‘‡

๐‘›

16๐‘‡

๐‘›

16๐‘‡

๐‘›

16๐‘‡

๐‘›

16๐‘‡

๐‘›

16๐‘‡

๐‘›

16๐‘‡

๐‘›

16

โ€ฆ โ€ฆ โ€ฆโ€ฆ โ€ฆ โ€ฆโ€ฆ โ€ฆ โ€ฆโ€ฆ โ€ฆ โ€ฆโ€ฆ โ€ฆ โ€ฆโ€ฆ โ€ฆ โ€ฆโ€ฆ โ€ฆ โ€ฆโ€ฆ โ€ฆ โ€ฆโ€ฆ

4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

Answer the following

questions:

1. What is the size of the

input on level ๐‘–?2. What is the work

done by each node

on the ๐‘–๐‘กโ„Ž recursive

level

3. What is the number

of nodes at level ๐‘–?4. What is the total work

done at the i^th

recursive level?

5. What value of ๐‘– does

the last level occur?

6. What is the total work

across the base case

level?

EXAMPLE PROVIDED BY CS 161 โ€“ JESSICA SU

HTTPS://WEB.STANFORD.EDU/CLASS/ARCHIVE/CS/CS161/CS161.1168/LECTURE3.PDF

๐‘›

16

2 ๐‘›

16

2 ๐‘›

16

2 ๐‘›

16

2 ๐‘›

16

2 ๐‘›

16

2 ๐‘›

16

2 ๐‘›

16

2 ๐‘›

16

2

Page 38: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Tree Method Practice

CSE 373 SP 18 - KASEY CHAMPION 40

Level (i)Number of

Nodes

Work per

Node

Work per

Level

0 1 ๐‘›2 ๐‘›2

1 3๐‘›

4

2 3

42๐‘›2

2 9๐‘›

42

2 32

44๐‘›2

base 3log4๐‘› 4 12log4๐‘›

1. What is the size of the input on level ๐‘–?

2. What is the work done by each node on the ๐‘–๐‘กโ„Ž

recursive level?

3. What is the number of nodes at level ๐‘–?

4. What is the total work done at the i^th

recursive level?

5. What value of ๐‘– does the last level occur?

6. What is the total work across the base case

level?

๐‘›

4

๐‘–

๐‘›

4๐‘–

2

๐‘‡ ๐‘› =4 ๐‘คโ„Ž๐‘’๐‘› ๐‘› โ‰ค 1

3๐‘‡๐‘›

4+ ๐‘๐‘›2 ๐‘œ๐‘กโ„Ž๐‘’๐‘Ÿ๐‘ค๐‘–๐‘ ๐‘’

Combining it all togetherโ€ฆ

3๐‘–

3๐‘–๐‘›

4

๐‘– 2

=3

16

๐‘–

๐‘›2

๐‘‡ ๐‘› =

๐‘–=0

log4 ๐‘› โˆ’13

16

๐‘–

๐‘›2 + 4๐‘›log43

๐‘›

4๐‘– = 1 ๐‘› = 4๐‘– ๐‘– = log4 ๐‘›

power of a log

๐‘ฅlog๐‘ ๐‘ฆ = ๐‘ฆlog๐‘ ๐‘ฅ4 โ‹… ๐‘›log4 3

5 Minutes

3log4 ๐‘› โ‹… 4

Page 39: Lecture 7 - University of Washingtonย ยท 5 7 4 8 10 12 15 14 11 13 get() is a recursive method! ๐‘‡ =แ‰ ๐‘‡ 2 +1if >1 3 otherwise ๐‘‡ =แ‰ if isatmostsomeconstant ๐‘‡ + otherwise

Tree Method Practice

CSE 373 SP 18 - KASEY CHAMPION 41

๐‘‡ ๐‘› =

๐‘–=0

log4 ๐‘› โˆ’13

16

๐‘–

๐‘›2 + 4๐‘›log43

๐‘‡ ๐‘› = ๐‘›2

316

log4 ๐‘›

โˆ’ 1

316

โˆ’ 1+ 4๐‘›log

43

๐‘‡ ๐‘› โˆˆ ๐‘‚(๐‘›2)

๐‘–=๐‘Ž

๐‘

๐‘๐‘“(๐‘–) = ๐‘

๐‘–=๐‘Ž

๐‘

๐‘“(๐‘–)

factoring out a constant

๐‘‡ ๐‘› = ๐‘›2

๐‘–=0

log4 ๐‘› โˆ’13

16

๐‘–

+ 4๐‘›log43

๐‘–=0

๐‘›โˆ’1

๐‘ฅ๐‘– =๐‘ฅ๐‘› โˆ’ 1

๐‘ฅ โˆ’ 1

finite geometric series

So whatโ€™s the big-ฮ˜โ€ฆ

๐‘‡ ๐‘› = ๐‘›2 โˆ’16

13

3

16

log4 ๐‘›

+16

13๐‘›2 + 4๐‘›log

43

Closed form:

๐‘‡ ๐‘› = ๐‘›2 โˆ’16

13๐‘› log4

316 +

16

13๐‘›2 + 4๐‘›log

43