Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before...

26
Recurrence Relations

Transcript of Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before...

Page 1: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Recurrence Relations

Page 2: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Outline •  Announcements •  Big Oh review and warmup •  Recursion review •  Intro to Recurrence Relations •  Practice

Page 3: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

•  Recently, a tweet about Dropbox went viral

•  Dropbox blocks copyrighted material by hashing every shared file and comparing the result to a blacklist holding hashes of copyrighted material

•  Remember what we said about hashing functions…

Something that may interest you

Page 4: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Announcements •  Boggle •  due April 3 (tomorrow!)

•  Exam 2 •  April 9th (next Wednesday) •  April 7th – in-class review •  April 7th – exam review with Jimmy, 7pm in this

room •  Huffman •  Walkthru tonight with Jimmy, 7pm in this room

Page 5: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Review of Big Oh •  How “fast” is my algorithm? •  in terms of n – the size of input

•  Big–Oh – the growth rate as a function of n •  O(N2) •  O(N log N)

5

Page 6: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Warm Up // Assume strings has length n.!// Assume "robot" appears in strings.!!public int findRobot(String[] strings) {!!

int current = 0;!while (!strings[current].equals("robot")) {!

current++;!}!return current;!

!}!

6

Page 7: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Warm Up // Return index of v in sorted array of positive numbers,!// or -1 if not there.!// Search between the indices low and high.!!public int findInSorted(int[] sorted, int v, int low, int high) {!

!while (low < high) {!

int midpoint = (low + high) / 2;!if (v < sorted[midpoint]) {!

high = midpoint; // Search in the lower half.!} else if (v > sorted[midpoint]) {!

low = midpoint + 1; // Search in the upper half.!} else {!

return midpoint;!}!

}!return -1;!

}!7

Page 8: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

??? // Is the value v contained in the binary search tree rooted at node?!!public boolean BSTcontainsValue(int v, TreeNode node) {!

if (node == null) {!return false;!

}!!

if (node.value == v) {!return true;!

}!!

if (v < node.value) {!return BSTcontainsValue(v, node.left);!

} else {!return BSTcontainsValue(v, node.right);!

}!

}!8

Page 9: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

??? // Is the value v contained in the binary search tree rooted at node?!!public boolean BSTcontainsValue(int v, TreeNode node) {!

if (node == null) {!return false;!

}!!

if (node.value == v) {!return true;!

}!!

if (v < node.value) {!return BSTcontainsValue(v, node.left);!

} else {!return BSTcontainsValue(v, node.right);!

}!

}!9

What if the tree is balanced?

What if the tree is

unbalanced?

Page 10: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Review of Recursion •  What is recursion, at its core? •  Breaking hard problems down into easier

subproblems •  Solving easier subproblems •  Combining subproblem answers into hard

problem answer •  How do we determine running time for this?

10

Page 11: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Recurrence Relations •  Write a recurrence relation! •  Recurrence relation expresses the runtime of a

recursive algorithm in terms of the runtime of its constituent subproblems

•  A recurrence relation looks something like: T(n) = T(n/2) + O(n)

11

Page 12: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Recurrence Relations •  Let’s piece this together from what we know

about recursion. •  We know that recursion involves solving

subproblems with the same method •  Will also require additional logic to put

subproblem answers together •  Thus:

Runtime = Runtime of recursive calls + Runtime of additional logic

12

Page 13: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

??? // Is the value v contained in the binary search tree rooted at node?!!public boolean BSTcontainsValue(int v, TreeNode node) {!

if (node == null) {!return false;!

}!!

if (node.value == v) {!return true;!

}!!

if (v < node.value) {!return BSTcontainsValue(v, node.left);!

} else {!return BSTcontainsValue(v, node.right);!

}!

}!13

What if the tree is balanced?

What if the tree is

unbalanced?

Page 14: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Recurrence Relations

•  Use the notation T(n) to represent the runtime of the recursive algorithm for input of size n T(n) = Recursive calls +

additional logic •  How will we represent the

runtime of our recursive calls?

boolean contains(int v, TreeNode node) {!

if (node == null) {!return false;!

}!if (node.value == v) {!

return true;!}!if (v < node.value) {!

return contains(v, node.left);!

} else {!return contains(v, node.right);!

}!}!

14  

Page 15: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Recurrence Relations

•  Recursive call is the same algorithm, just with a different input size!

•  Thus: T(n) = T(n/2) + additional logic

•  What about the runtime of additional logic?

boolean contains(int v, TreeNode node) {!

if (node == null) {!return false;!

}!if (node.value == v) {!

return true;!}!if (v < node.value) {!

return contains(v, node.left);!

} else {!return contains(v, node.right);!

}!}!

15  

Page 16: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Recurrence Relations

•  Analyze the rest of the algorithm as you would normally!

•  Thus: T(n) = T(n/2) + O(1)

•  This is the recurrence relation!

•  What assumption does this answer make?

boolean contains(int v, TreeNode node) {!

if (node == null) {!return false;!

}!if (node.value == v) {!

return true;!}!if (v < node.value) {!

return contains(v, node.left);!

} else {!return contains(v, node.right);!

}!}!

16  

Page 17: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Recurrence Relations

•  Our answer assumes the tree is roughly balanced

•  What if it is completely unbalanced?

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

boolean contains(int v, TreeNode node) {!

if (node == null) {!return false;!

}!if (node.value == v) {!

return true;!}!if (v < node.value) {!

return contains(v, node.left);!

} else {!return contains(v, node.right);!

}!}!

17  

Page 18: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Recurrence Relations •  So we have our recurrence relations, and we

want to solve them into a Big Oh… •  Balanced: T(n) = T(n/2) + O(1) •  Unbalanced: T(n) = T(n-1) + O(1) •  How do we solve these?

18

Page 19: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Recurrence Relations

•  Balanced: T(n) = T(n/2) + O(1) = O(lg n) •  Unbalanced: T(n) = T(n-1) + O(1) = O(n) •  Let’s call it magic for now (you will learn

more about this in CS330)…

19

Recurrence   Example   Running  Time  

T(n)  =  T(n/2)  +  O(1)     Binary  Search     O(log  n)  

T(n)  =  T(n-­‐1)  +  O(1)     Linear  Search     O(n)  

T(n)  =  2T(n/2)  +  O(1)     Tree  traversal     O(n)  

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

T(n)  =  T(n-­‐1)  +  O(n)     BubbleSort   O(n2)  

Page 20: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Recurrence Relations •  So a quick recap before practice problems: •  Determine how the size of our input changes

when we make our recursive calls •  Determine the Big Oh of our additional logic •  Compare our recurrence relation to the chart to

find the final answer

20

T(n) = Recursive runtime + Additional logic T(n) = T(n/2) + Additional logic

T(n) = T(n/2) + O(1) T(n) = T(n/2) + O(1) = O(lg n)

Page 21: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Problem 1 //your tree is balanced!!public int height(TreeNode node) {!!

if (node == null) {!return 0;!

}!int leftHeight = height(node.left);!int rightHeight = height(node.right);!return Math.max(leftHeight, rightHeight) + 1;!

}!!

21

Page 22: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Problem 2 //your tree is not balanced!!public int height(TreeNode node) {!!

if (node == null) {!return 0;!

}!int leftHeight = height(node.left);!int rightHeight = height(node.right);!return Math.max(leftHeight, rightHeight) + 1;!

}!

22

Page 23: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Problem 3 //your tree is balanced!!public boolean isBalanced(TreeNode node) {!!

!if (node == null) {!! !return true;!!}!

!int left = height(node.left);!int right = height(node.right);!if (Math.abs(left - right) > 1) {!

return false;!}!

!return (isBalanced(node.left) &&

!isBalanced(node.right));!!}!

23

Page 24: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Problem 4 //your tree is not balanced!!public boolean isBalanced(TreeNode node) {!!

!if (node = null) {!! !return true;!!}!

!int left = height(node.left);!int right = height(node.right);!if (Math.abs(left - right) > 1) {!

return false;!}!

!return (isBalanced(node.left) &&

!isBalanced(node.right));!!}!

24

Page 25: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Problem 5 public int maximum(int[] values, int low, int high) {!!

if (low == high) {!return values[low];!

}!!

int mid = (low + high) / 2;!return Math.max(maximum(values, low, mid),

!maximum(values, mid+1, high));!!}!

25

Page 26: Recurrence Relations - Duke Computer ScienceRecurrence Relations • So a quick recap before practice problems: • Determine how the size of our input changes when we make our recursive

Problem 6 // Reverse the array values, between the indices low and high.!!public static void reverse(int[] values, int low, int high) {!!

if (low >= high) {!return;!

}!int temp = values[low];!values[low] = values[high];!values[high] = temp;!reverse(values, low+1, high-1);!

}!

26