Compsci 201 Recursion, Recurrences, & Trees Owen Astrachan Jeff Forbes October 25, 2017 10/25/17 Compsci 201, Fall 2017, Recurrences
CompSci 201, Fall 2017, Recurrences P is for … Parallel Better and more complicated than serial? Planning Come up with a set of actions that will achieve a goal Probabilistic Inference Markovian text recognition? PriorityQueue Efficiently finding minimum 10/25/17 CompSci 201, Fall 2017, Recurrences
CompSci 201, Fall 2017, Recurrences Plan for the Week How do we calculate the Big-Oh of recursive algorithms? What’s a tree and how does it help solve problems efficiently? 10/25/17 CompSci 201, Fall 2017, Recurrences
Big-Oh for recursive functions public int size(ListNode list) { if (list == null) return 0; return 1 + size(list.next); } What’s the Big Oh for a list with n nodes? Express cost of algorithm for input of size n as recurrence T(n) T(0) Base case! T(n) = if n > 0 Repeated substitution to solve for a closed form answer
Solving recurrence relations plug, simplify, reduce, guess, verify? T(n) = T(n-1) + 1 T(0) = 1 T(n) = T(n-k) + k Find the pattern! Now, let k=n, then T(n) = T(0)+n = 1+n get to base case, solve the recurrence: O(n) T(n-1) = T(n-1-1) + 1 T(n) = [T(n-2) + 1] + 1 = T(n-2)+2 T(n-2) = T(n-2-1) + 1 T(n) = [(T(n-3) + 1) + 1] + 1 = T(n-3)+3
CompSci 201, Fall 2017, Recurrences ListNRev revisited public ListNode nlist(int n) { if (n <= 0) return null; ListNode first = new ListNode(n); ListNode last = first; for(int k=0; k < n-1; k++) { last.next = new ListNode(n); last = last.next; } return first; 10/25/17 CompSci 201, Fall 2017, Recurrences
ListNRev revisited – circular public ListNode nlistCirc(int n) { if (n <= 0) return null; ListNode first = new ListNode(n); ListNode last = first; for(int k=0; k < n-1; k++) { last.next = new ListNode(n); last = last.next; } last.next = first; return last; 10/25/17 CompSci 201, Fall 2017, Recurrences
CompSci 201, Fall 2017, Recurrences ListNRev Complexity? What’s changed? What are the new recurrences for each version? public ListNode make(int n) { if (n <= 0) return null; ListNode list = nlist(n); ListNode last = list; while (last.next != null) last = last.next; last.next = make(n-1); return list; } public ListNode makeCirc(int n) { if (n <= 0) return null; ListNode list = nlistCirc(n); ListNode head = list.next; list.next = makeCirc(n-1); return head; } http://bit.ly/201-f17-1025-1 10/25/17 CompSci 201, Fall 2017, Recurrences
CompSci 201, Fall 2017, Recurrences Towers of Hanoi https://www.mathsisfun.com/games/towerofhanoi.html // Move numDisks disks from -> to using aux void move(int from, int to, int aux, int numDisks) { if (numDisks == 1) System.out.println(from + " to " + to); else { move(from, aux, to, numDisks-1); move(from, to, aux, 1); move(aux, to, from, numDisks-1); } How many recursive calls? How fast are we progressing to the solution? 10/25/17 CompSci 201, Fall 2017, Recurrences
CompSci 201, Fall 2017, Recurrences Towers of Hanoi https://www.mathsisfun.com/games/towerofhanoi.html // Move numDisks disks from -> to using aux void move(int from, int to, int aux, int numDisks) { if (numDisks == 1) System.out.println(from + " to " + to); else { move(from, aux, to, numDisks-1); move(from, to, aux, 1); move(aux, to, from, numDisks-1); } T(n-1) T(1) T(n-1) 10/25/17 CompSci 201, Fall 2017, Recurrences
CompSci 201, Fall 2017, Recurrences Hanoi Recurrence Plug and chug Let i = n - 1 10/25/17 CompSci 201, Fall 2017, Recurrences
Recognizing Recurrences Solve once, re-use in new contexts T must be explicitly identified n must be some measure of size of input/parameter T(n) is the time for algortihmto run on an n-element vector T(n) = T(n/2) + O(1) binary search O( ) T(n) = T(n-1) + O(1) sequential search O( ) T(n) = 2T(n/2) + O(1) tree traversal O( ) T(n) = 2T(n/2) + O(n) quicksort O( ) T(n) = T(n-1) + O(n) selection sort O( ) T(n) = 2T(n-1) + O(1) Towers of Hanoi O( ) Remember the algorithm, re-derive complexity log n n n n log n n2 2n
Another recurrence http://bit.ly/201-f17-boom int boom(int m, int x) { if (m == 0) return h(x); return boom(m-1, q(x)) + boom(m-1, r(x)); } Assume h(x), q(x), and r(x) are all O(1) Express cost of algorithm How do m and x matter? http://bit.ly/201-f17-boom
SpreadingNews APT: greedy, recursive https://www.cs.duke.edu/csed/newapt/spreadingnews.html
How to write SpreadingNews? Assume three subordinates: 5 min, 4 min, 7 min Who do we tell first? Second? How long does it take us to finish? Suppose subordinates take 3 min, 3 min, 3 min? How do we computer how long it takes for sub? Do we have a way of doing this? Is there some way out of always asking again? Putting ideas into code: greedy and recursion! Where is base case? http://bit.ly/201-f17-1025-2
CompSci 201, Fall 2017, Recurrences Towards Trees Trees are powerful recurring structure Quick-union and SpreadingNews have tree structure Binary search trees are used in TreeSet and TreeMap 10/25/17 CompSci 201, Fall 2017, Recurrences
Binary Tree Node myRoot = null; public class Node { public int myValue; public Node myLeft; public Node myRight; public Node(int val) { myValue = val; }
Binary Tree
Binary Tree 1 2