Compsci 201 Recursion, Recurrences, & Trees

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
A simple example finding the maximum of a set S of n numbers.
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Lecture 8 Analysis of Recursive Algorithms King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Analysis of Recursive Algorithms
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
CPS Wordcounting, sets, and the web l How does Google store all web pages that include a reference to “peanut butter with mustard”  What efficiency.
CPS 100, Spring Analysis: Algorithms and Data Structures l We need a vocabulary to discuss performance and to reason about alternative algorithms.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
CPS 100, Spring Trees: no data structure lovelier?
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
CSC 211 Data Structures Lecture 13
CompSci 102 Discrete Math for Computer Science April 17, 2012 Prof. Rodger.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Computer Science 101 A Survey of Computer Science Timing Problems.
CompSci 100e 6.1 Plan for the week l More recursion examples l Backtracking  Exhaustive incremental search  When we a potential solution is invalid,
CompSci Review of Recursion with Big-Oh  Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved.
CompSci Analyzing Algorithms  Consider three solutions to SortByFreqs, also code used in Anagram assignment  Sort, then scan looking for changes.
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Lecture #3 Analysis of Recursive Algorithms
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CPS 100, Spring Tools: Solve Computational Problems l Algorithmic techniques  Brute-force/exhaustive, greedy algorithms, dynamic programming,
CompSci 100e 7.1 Plan for the week l Recurrences  How do we analyze the run-time of recursive algorithms? l Setting up the Boggle assignment.
Algorithm Analysis 1.
Unit 6 Analysis of Recursive Algorithms
Theoretical analysis of time efficiency
Analysis of Recursive Algorithms
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Analysis of Algorithms
Analysis: Algorithms and Data Structures
Analysis of Algorithms
Compsci 201, Mathematical & Emprical Analysis
Randomized Algorithms
Tools: Solve Computational Problems
Recursion
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Building Java Programs
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Randomized Algorithms
Programming and Data Structure
Algorithm Efficiency and Sorting
Analysis of Algorithms
Building Java Programs
Recurrences (Method 4) Alexandra Stefan.
Building Java Programs
Compsci 201 Binary Trees Recurrence Relations
At the end of this session, learner will be able to:
Analysis of Recursive Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Analysis of Algorithms
Analysis of Recursive Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Chapter 11 Sets, and Selection
Presentation transcript:

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