1 Recursion Jeff Edmonds York University COSC 2011 Lecture 5 Ruler Example One Step at a Time Stack of Stack Frames Friends and Strong Induction Recurrence.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

CSC 421: Algorithm Design & Analysis
CS Divide and Conquer/Recurrence Relations1 Divide and Conquer.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
Using Divide and Conquer for Sorting
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Chapter 19: Searching and Sorting Algorithms
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Analysis of Recursive Algorithms
Recursion Credits: Jeff Edmonds, Ping Xuan. 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)
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion Chapter 5 Outline Induction Linear recursion –Example 1: Factorials –Example 2: Powers –Example 3: Reversing an array Binary recursion –Example.
COMP s1 Computing 2 Complexity
1 What NOT to do I get sooooo Frustrated! Marking the SAME wrong answer hundreds of times! I will give a list of mistakes which I particularly hate marking.
Relevant Mathematics Lecture 2 Classifying Functions The Time Complexity of an Algorithm Adding Made Easy Recurrence Relations.
Relevant Mathematics Jeff Edmonds York University COSC 3101 Lecture skipped. Lecture skipped. (Done when needed) Logic Quantifiers The Time Complexity.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C Two Blocks: –16:00-17:30 –17:30-19:00 The exam will be 1.5 hours in length. You can attend.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Fundamentals of Algorithm Analysis Dr. Steve Goddard CSCE 310J: Data Structures &
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Recursive Algorithms Introduction Applications to Numeric Computation.
Recursion Jeff Edmonds York University COSC 6111 Lecture 3 Friends & Steps for Recursion Derivatives Recursive Images Multiplying Parsing Ackermann.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Getting Started Introduction to Algorithms Jeff Chastine.
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
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
COSC 3101Winter, 2004 COSC 3101 Design and Analysis of Algorithms Lecture 2. Relevant Mathematics: –The time complexity of an algorithm –Adding made easy.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
(c) , University of Washington18a-1 CSC 143 Java Searching and Recursion N&H Chapters 13, 17.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
1 Jeff Edmonds York University COSC 2011 Abstract Data Types Positions and Pointers Loop Invariants System Invariants Time Complexity Classifying Functions.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Sorting & Lower Bounds Jeff Edmonds York University COSC 3101 Lecture 5.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
How to Think about Algorithms
Jeff Edmonds York University
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
CS 3343: Analysis of Algorithms
CSC 421: Algorithm Design & Analysis
UNIT I ITERATIVE AND RECURSIVE ALGORITHMS
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Data Structures & Algorithms
Data Structures Review Session
CSC 421: Algorithm Design & Analysis
Design and Analysis of Algorithms
CSC 421: Algorithm Design & Analysis
Divide and Conquer Merge sort and quick sort Binary search
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

1 Recursion Jeff Edmonds York University COSC 2011 Lecture 5 Ruler Example One Step at a Time Stack of Stack Frames Friends and Strong Induction Recurrence Relations Towers of Hanoi Check List Merge & Quick Sort Simple Recursion on Trees Binary Search Tree Things not to do Heap Sort & Priority Queues Trees Representing Equations Iterate over all s-t Paths Recursive Images

2 Andranik Mirzaian Draw Ruler drawLine( location, length ) put curser at location loop j = 1..length print(“-”)  1/4 , 3

3 Andranik Mirzaian Draw Ruler drawLine( location, length ) put curser at location loop j = 1..length print(“-”) drawRuler() drawLine(0,4) print(0) loop i = drawLine(i,4) print(i) drawInterval(  i-1,i ,length-1)

4 Andranik Mirzaian Draw Ruler drawLine( location, length ) put curser at location loop j = 1..length print(“-”) drawRuler() drawLine(0,4) print(0) loop i = drawLine(i,4) print(i) drawInterval(  i-1,i ,length-1)  1,2 , 3  0,1 , 3

5 Andranik Mirzaian Draw Ruler drawRuler() drawLine(0,4) print(0) loop i = drawLine(i,4) print(i) drawInterval(  i-1,i ,length-1) drawLine( location, length ) put curser at location loop j = 1..length print(“-”) drawInterval(  a,b , length ) if ( length > 0 ) then mid = (a+b)/2 drawInterval (  a,mid , length-1 ) drawLine( mid, length ) drawInterval (  mid,b , length-1 )  0,1 , 4  0,1  4

6 Andranik Mirzaian drawInterval(  a,b , length ) if ( length > 0 ) then mid = (a+b)/2 drawInterval (  a,mid , length-1 ) drawLine( mid, length ) drawInterval (  mid,b , length-1 ) Draw Ruler drawRuler() drawLine(0,4) print(0) loop i = drawLine(i,4) print(i) drawInterval(  i-1,i ,length-1) drawLine( location, length ) put curser at location loop j = 1..length print(“-”)

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

8 Friends & Strong Induction Recursive Algorithm: Assume you have an algorithm that works. Use it to write an algorithm that works. To get into my house I must get the key from a smaller house

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

10 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. Advice: Do not trace it out. Trust your friend. Do your job.

11 PreconditionPostcondition On Step At a Time next I implored you to not worry about the entire computation. next It can be difficult to understand where computation go. i-1 i i i 0 T+1  x/4   3y (x-3  x 1 )  y Strange(x,y): x 1 =  x/4  ; y1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x1; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 );

12 Fibonacci(n): If n≤1 then return(n); else return( Fibonacci(n-1) + Fibonacci(n-2) ); … … On Step At a Time Golden Ratio

13 Fibonacci(n): If n≤1 then return(n); else return( Fibonacci(n-1) + Fibonacci(n-2) ); On Step At a Time Cool enough But its entire computation can be understood in its entirety. In fact, it can be computed sequentially or by a formula. We need something harder.

14 Representations of Recursive Algorithms Pros Views Code Tracing out stack of stack frames Tree of stack frames Friends (strong induction) I need a higher level of understanding I am not a computer Crazy making Too much Must trust. Cons How implement for a computer How runs on computer View entire computation Worry about one step at a time Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 );

15 Code Representation Pros Views Code I need a higher level of understanding Cons How implement for a computer Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); Precise and succinct Perception that being able to code is the only thing needed to get a job. (false) I am not a computer Prone to bugs Language dependent

16 x = 30; y = 5 x 1 = 7; y 1 = 15 f 1 = Tracing out Stack of Stack Frames Stack Frame: A particular execution of one routine on one particular input instance. x = 7; y = 15 x 1 = 1; y 1 = 45 f 1 = x = 1; y = 45 return 45 Strange(30,5) = Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 );

17 x = 30; y = 5 x 1 = 7; y 1 = 15 f 1 = Tracing out Stack of Stack Frames Stack Frame: A particular execution of one routine on one particular input instance. Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); x = 7; y = 15 x 1 = 1; y 1 = 45 f 1 = x 2 = 4; y 2 = 15 f 2 = 45 x = 1; y = 45 return 45 x = 4; y = 15 x 1 = 1; y 1 = 45 f 1 = Strange(30,5) =

18 x = 30; y = 5 x 1 = 7; y 1 = 15 f 1 = Tracing out Stack of Stack Frames Stack Frame: A particular execution of one routine on one particular input instance. Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); x = 7; y = 15 x 1 = 1; y 1 = 45 f 1 = x 2 = 4; y 2 = 15 f 2 = return x = 1; y = 15 return 15 x = 4; y = 15 x 1 = 1; y 1 = 45 f 1 = x 2 = 1; y 2 = 15 f 2 = return Strange(30,5) =

19 x = 30; y = 5 x 1 = 7; y 1 = 15 f 1 = x 2 = 9; y 2 = 5 f 2 = Tracing out Stack of Stack Frames Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); 105 Stack Frame: A particular execution of one routine on one particular input instance. x = 9; y = 5 x 1 = 2; y 1 = 15 f 1 = x = 2; y = 15 return 30 Strange(30,5) =

20 Stack Frame: A particular execution of one routine on one particular input instance. Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); x = 9; y = 5 x 1 = 2; y 1 = 15 f 1 = x 2 = 3; y 2 = 5 f 2 = return x = 3; y = 5 return 15 x = 30; y = 5 x 1 = 7; y 1 = 15 f 1 = x 2 = 9; y 2 = 5 f 2 = return Strange(30,5) = 150 I am not a computer! This is crazy making Tracing out Stack of Stack Frames

21 Trace what actually occurs in the computer Concrete. It is what students attempt to describe when told not to give code. Described in words it is impossible to follow who is doing what. Does not explain why it works. Demonstrates for only one of many inputs. Pros:Cons: Tracing out Stack of Stack Frames

22 Tree of Stack Frames x = 30; y = 5 x 1 = 7; y 1 = 15 f 1 = x 2 = 9; y 2 = 5 f 2 = return Strange(30,5) = 150 x = 1; y = 15 return 15 x = 7; y = 15 x 1 = 1; y 1 = 45 f 1 = x 2 = 4; y 2 = 15 f 2 = return x = 1; y = 45 return 45 x = 4; y = 15 x 1 = 1; y 1 = 45 f 1 = x 2 = 1; y 2 = 15 f 2 = return x = 1; y = 45 return 45 x = 9; y = 5 x 1 = 2; y 1 = 15 f 1 = x 2 = 3; y 2 = 5 f 2 = return x = 3; y = 5 return x = 2; y = 15 return 30

23 View the entire computation. Good for computing the running time. Must describe entire tree. –For each stack frame input instance computation solution returned. –who calls who Structure of tree may be complex. Pros:Cons: Tree of Stack Frames

24 Representations of Recursive Algorithms Pros Views Code Tracing out stack of stack frames Tree of stack frames Friends (strong induction) Must trust. Cons Worry about one step at a time Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 );

25 x = 30; y = 5 x 1 = 7; y 1 = 15 f 1 = x 2 = 9; y 2 = 5 f 2 = return x = 1; y = 15 return 15 x = 7; y = 15 x 1 = 1; y 1 = 45 f 1 = x 2 = 4; y 2 = 15 f 2 = return x = 1; y = 45 return 45 x = 4; y = 15 x 1 = 1; y 1 = 45 f 1 = x 2 = 1; y 2 = 15 f 2 = return x = 1; y = 45 return 45 x = 9; y = 5 x 1 = 2; y 1 = 15 f 1 = x 2 = 3; y 2 = 5 f 2 = return x = 3; y = 5 return x = 2; y = 15 return 30 One Friend for each stack frame. Each worries only about his job. Imagine that you are one. Friends & Strong Induction

26 Consider your input instance If it is small enough solve it on your own. Allocate work –Construct one or more sub-instances It must be smaller and meet the precondition –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. –Micro-manage friends by tracing out what they and their friend’s friends do. –Who your boss is. X = 7 Y = 15 XY = 105 X = 9 Y = 5 XY = 45 Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); ? Know Precond: ints x,y Postcond: ??? Friends & Strong Induction x = 30; y = 5 x 1 = 7; y 1 = 15 f 1 = 105 x 2 = 9; y 2 = 5 f 2 = 45 return 150

27 Consider generic instances.  x/4   3y (x-3  x 1 )  y Strange(x,y): x 1 =  x/4  ; y1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x1; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); Strange(x,y): If x < 4 then return( xy ); Remember! Do not worry about Who your boss is. How your friends solve their instance. Friends & Strong Induction Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 );

28 x = 30; y = 5 x 1 = 7; y 1 = 15 f 1 = x 2 = 9; y 2 = 5 f 2 = return x = 1; y = 15 return 15 x = 7; y = 15 x 1 = 1; y 1 = 45 f 1 = x 2 = 4; y 2 = 15 f 2 = return x = 1; y = 45 return 45 x = 4; y = 15 x 1 = 1; y 1 = 45 f 1 = x 2 = 1; y 2 = 15 f 2 = return x = 1; y = 45 return 45 x = 9; y = 5 x 1 = 2; y 1 = 15 f 1 = x 2 = 3; y 2 = 5 f 2 = return x = 3; y = 5 return x = 2; y = 15 return 30 Friends & Strong Induction This solves the problem for every possible instance.

29 Worry about one step at a time. An abstraction within which to develop, think about, and describe algorithms in such way that their correctness is transparent. Students resist it. Students expect too much from their friends. Each sub-instance must be a smaller instance to the same problem. Students expect too much from their boss. You only know your instance. You do not know your boss’s instance. Pros:Cons: Friends & Strong Induction

30 So that we know –what is expected of us. –what we can expect from our friend. To be sure that we solve the problem for every legal instance. So that we know –what we can give to a friend. Carefully write the specifications for the problem. Required output : Set of legal instances (inputs) : Friends & Strong Induction

31 If you want more from your friends, change the pre and/or postconditions Be sure to document it. Be sure you and all of your friends are using the same pre/postconditions. Be sure to handle these changes yourself. Friends & Strong Induction

32 x = 30; y = 5 x 1 = 7; y 1 = 15 f 1 = x 2 = 9; y 2 = 5 f 2 = return Strange(30,5) = 150 x = 1; y = 15 return 15 x = 7; y = 15 x 1 = 1; y 1 = 45 f 1 = x 2 = 4; y 2 = 15 f 2 = return x = 1; y = 45 return 45 x = 4; y = 15 x 1 = 1; y 1 = 45 f 1 = x 2 = 1; y 2 = 15 f 2 = return x = 1; y = 45 return 45 x = 9; y = 5 x 1 = 2; y 1 = 15 f 1 = x 2 = 3; y 2 = 5 f 2 = return x = 3; y = 5 return x = 2; y = 15 return 30 Friends & Strong Induction product x  y Know Precond: ints x,y Postcond: ???

33 Induction Strong Induction Friends & Strong Induction

34 Induction Hypothesis: ``The recursive algorithm works for every instance of size n.'' ``The algorithm works for every instance of any size.'' Base case size i Friends & Strong Induction Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); If x < 4 then return( xy ); return = Strange( x 1, y 1 ) + Strange( x 2, y 2 ) = Strange( x 1, 3y ) + Strange( x - 3  x 1, y ) = x 1  (3y) + (x - 3  x 1 )  y = 3x 1 y + xy - 3x 1 y = xy

35 Give and solve the recurrence relation for the Running Time of this algorithm. T(n) = aT(n/b)+n c Friends & Strong Induction

36 Recurrence Relations  Time of Recursive Program With all this recursing and looping, how do we determine the running time of this recursive program? procedure Eg(I n ) n = |I n | if(n  1) then put “Hi” else loop i=1..n c put “Hi” loop i=1..a I n/b = I n cut in b pieces Eg(I n/b ) T(1) = 1 T(n) = a T(n/b) + n c Recurrence relations.

37 Recurrence Relations  Time of Recursive Program Let n be the “size” of our input. procedure Eg(I n ) n = |I n | if(n  1) then put “Hi” else loop i=1..n c put “Hi” loop i=1..a I n/b = I n cut in b pieces Eg(I n/b ) T(1) = 1 T(n) = a T(n/b) + n c

38 Recurrence Relations  Time of Recursive Program Let T(n) be the # of “Hi”s printed by me procedure Eg(I n ) n = |I n | if(n  1) then put “Hi” else loop i=1..n c put “Hi” loop i=1..a I n/b = I n cut in b pieces Eg(I n/b ) T(1) = 1 T(n) = a T(n/b) + n c

39 Recurrence Relations  Time of Recursive Program Let T(n) be the # of “Hi”s printed by me And by all of my friends. procedure Eg(I n ) n = |I n | if(n  1) then put “Hi” else loop i=1..n c put “Hi” loop i=1..a I n/b = I n cut in b pieces Eg(I n/b ) T(1) = 1 T(n) = a T(n/b) + n c

40 If my input is sufficiently small (according to my measure of size) then I print  (1) “Hi”s. Recurrence Relations  Time of Recursive Program procedure Eg(I n ) n = |I n | if(n  1) then put “Hi” else loop i=1..n c put “Hi” loop i=1..a I n/b = I n cut in b pieces Eg(I n/b ) T(1) = 1 T(n) = a T(n/b) + n c

41 I personally output n c “Hi”s, i.e. top level stack frame. Recurrence Relations  Time of Recursive Program procedure Eg(I n ) n = |I n | if(n  1) then put “Hi” else loop i=1..n c put “Hi” loop i=1..a I n/b = I n cut in b pieces Eg(I n/b ) T(1) = 1 T(n) = a T(n/b) + n c

42 I have a friends i.e. recurse a times. Recurrence Relations  Time of Recursive Program procedure Eg(I n ) n = |I n | if(n  1) then put “Hi” else loop i=1..n c put “Hi” loop i=1..a I n/b = I n cut in b pieces Eg(I n/b ) T(1) = 1 T(n) = a T(n/b) + n c

43 Let b be the factor by which I shrink the input before giving it to a friend. i.e. if my input has size n then my friends are of size n/b. Recurrence Relations  Time of Recursive Program procedure Eg(I n ) n = |I n | if(n  1) then put “Hi” else loop i=1..n c put “Hi” loop i=1..a I n/b = I n cut in b pieces Eg(I n/b ) T(1) = 1 T(n) = a T(n/b) + n c

44 By definition of T, the number of “Hi”s printed by one of my friends and all of his friends is T(n/b). Recurrence Relations  Time of Recursive Program procedure Eg(I n ) n = |I n | if(n  1) then put “Hi” else loop i=1..n c put “Hi” loop i=1..a I n/b = I n cut in b pieces Eg(I n/b ) T(1) = 1 T(n) = a T(n/b) + n c

45 Hence, the total number printed by me and my a friends is T(n) = a T(n/b) + n c Recurrence Relations  Time of Recursive Program procedure Eg(I n ) n = |I n | if(n  1) then put “Hi” else loop i=1..n c put “Hi” loop i=1..a I n/b = I n cut in b pieces Eg(I n/b ) T(1) = 1 T(n) = a T(n/b) + n c

46 Technique 1 Guess and Verify Recurrence Relation: T(1) = 1 & T(n) = 4T(n/2) + n Guess: G(n) = 2n 2 – n Verify: Left Hand SideRight Hand Side T(1) = 2(1) 2 – 1 T(n) = 2n 2 – n 1 4T(n/2) + n = 4 [2( n / 2 ) 2 – ( n / 2 )] + n = 2n 2 – n

47 Technique 2: Decorate The Tree n T(n/2) T(n) = T(n) = n + 4 T(n/2) n T(n/2) T(n)T(n) = T(1) T(1) = 1 1 =

48 n T(n/2) T(n)T(n) =

49 n T(n/2) T(n)T(n) = n/2 T(n/4)

50 n T(n)T(n) = n/2 T(n/4) n/2 T(n/4) n/2 T(n/4) n/2 T(n/4)

51 n T(n)T(n) = n/ …………………… n/4

52 Evaluating: T(n) = aT(n/b)+f(n) Level i h

53 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size i h

54 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size 0n 1 2 i h

55 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size 0n 1 n/bn/b 2 i h

56 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size 0n 1 n/bn/b 2 n/b2n/b2 i h

57 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size 0n 1 n/bn/b 2 n/b2n/b2 i n/bin/bi h n/bhn/bh

58 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size 0n 1 n/bn/b 2 n/b2n/b2 i n/bin/bi h n/bhn/bh

59 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size 0n 1 n/bn/b 2 n/b2n/b2 i n/bin/bi h n/b h = 1 base case

60 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size 0n 1 n/bn/b 2 n/b2n/b2 i n/bin/bi h n/b h = 1

61 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size 0n 1 n/bn/b 2 n/b2n/b2 i n/bin/bi h = log n / log b n/b h = 1 b h = n h log b = log n h = log n / log b

62 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size Work in stack frame 0n 1 n/bn/b 2 n/b2n/b2 i n/bin/bi h = log n / log b 1

63 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size Work in stack frame 0n f(n)f(n) 1 n/bn/bf(n/b)f(n/b) 2 n/b2n/b2 f(n/b2)f(n/b2) i n/bin/bi f(n/bi)f(n/bi) h = log n / log b 1T(1)

64 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size Work in stack frame # stack frames 0n f(n)f(n) 1 n/bn/bf(n/b)f(n/b) 2 n/b2n/b2 f(n/b2)f(n/b2) i n/bin/bi f(n/bi)f(n/bi) h = log n / log b n/bhn/bh T(1)

65 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size Work in stack frame # stack frames 0n f(n)f(n) 1 1 n/bn/bf(n/b)f(n/b) a 2 n/b2n/b2 f(n/b2)f(n/b2) a2a2 i n/bin/bi f(n/bi)f(n/bi) aiai h = log n / log b n/bhn/bh T(1) ahah

66 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size Work in stack frame # stack frames 0n f(n)f(n) 1 1 n/bn/bf(n/b)f(n/b) a 2 n/b2n/b2 f(n/b2)f(n/b2) a2a2 i n/bin/bi f(n/bi)f(n/bi) aiai h = log n / log b n/bhn/bh T(1) ahah

67 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size Work in stack frame # stack frames 0n f(n)f(n) 1 1 n/bn/bf(n/b)f(n/b) a 2 n/b2n/b2 f(n/b2)f(n/b2) a2a2 i n/bin/bi f(n/bi)f(n/bi) aiai h = log n / log b n/bhn/bh T(1) ahah a h = a = n log n / log b log a / log b

68 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size Work in stack frame # stack frames Work in Level 0n f(n)f(n) 1 1 n/bn/bf(n/b)f(n/b) a 2 n/b2n/b2 f(n/b2)f(n/b2) a2a2 i n/bin/bi f(n/bi)f(n/bi) aiai h = log n / log b n/bhn/bh T(1) n log a / log b

69 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size Work in stack frame # stack frames Work in Level 0n f(n)f(n) 1 1 · f(n) 1 n/bn/bf(n/b)f(n/b) a a · f(n/b)a · f(n/b) 2 n/b2n/b2 f(n/b2)f(n/b2) a2a2 a 2 · f(n/b 2 ) i n/bin/bi f(n/bi)f(n/bi) aiai a i · f(n/b i ) h = log n / log b n/bhn/bh T(1) n log a / log b n · T(1) log a / log b Total Work T(n) = ∑ i=0..h a i × f(n/b i )

70 = ∑ i=0..h a i × f(n/b i ) If a Geometric Sum ∑ i=0..n x i = θ(max(first term, last term)) Evaluating: T(n) = aT(n/b)+f(n)

71 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size Work in stack frame # stack frames Work in Level 0n f(n)f(n) 1 1 · f(n) 1 n/bn/bf(n/b)f(n/b) a a · f(n/b)a · f(n/b) 2 n/b2n/b2 f(n/b2)f(n/b2) a2a2 a 2 · f(n/b 2 ) i n/bin/bi f(n/bi)f(n/bi) aiai a i · f(n/b i ) h = log n / log b n/bhn/bh T(1) n log a / log b n · T(1) log a / log b Dominated by Top Level or Base Cases

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

73 Time for top level: Time for base cases: Dominated?: c = 1 < 2 = log a / log b θ(n ) = log a / log b θ(n ) = θ(n 2 ) log 4 / log 2 Hence, T(n) = ? = θ(base cases) = θ(n ) = θ(n 2 ). log a / log b Evaluating: T(n) = aT(n/b) + n c = 4T(n/2) + n n c = = n 1 1 If we reduce the number of friends from 4 to 3 is the savings just 25%?

74 n T(n)T(n) = n/2 T(n/4) n/2 T(n/4) n/2 T(n/4) n/2 T(n/4)

75 Time for top level: Time for base cases: Dominated?: c = 1 < 1.58 = log a / log b θ(n ) = log a / log b θ(n ) = θ(n 1.58 ) log 3 / log 2 Hence, T(n) = ? = θ(base cases) = θ(n ) = θ(n 1.58 ). log a / log b Evaluating: T(n) = aT(n/b) + n c = 3T(n/2) + n n c = = n 1 1 Not just a 25% savings! θ(n 2 ) vs θ(n )

76 Time for top level: n 2, c=2 Time for base cases: Dominated?: c = 1.58 = log a / log b θ(n ) = log a / log b θ(n ) = θ(n 1.58 ) log 3 / log 2 Hence, T(n) = ? = θ(top level) = θ(n 2 ). Evaluating: T(n) = aT(n/b) + n c = 3T(n/2) + n 2 2 >

77 Time for top level: n 1.58, c=1.58 Time for base cases: Dominated?: c = 1.58 = log a / log b Hence, all θ(logn) layers require a total of θ(n 1.58 ) work. The sum of these layers is no longer geometric. Hence T(n) = θ(n 1.58 logn) θ(n ) = log a / log b θ(n ) = θ(n 1.58 ) log 3 / log 2 Evaluating: T(n) = aT(n/b) + n c = 3T(n/2) + n 1.58

78 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size Work in stack frame # stack frames Work in Level 0n f(n)f(n) 1 1 · f(n) 1 n/bn/bf(n/b)f(n/b) a a · f(n/b)a · f(n/b) 2 n/b2n/b2 f(n/b2)f(n/b2) a2a2 a 2 · f(n/b 2 ) i n/bin/bi f(n/bi)f(n/bi) aiai a i · f(n/b i ) h = log n / log b n/bhn/bh T(1) n log a / log b n · T(1) log a / log b All levels the same: Top Level to Base Cases

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

80 Logs log 27 log 9 =

81 Logs log ? 27 log ? 9 = Which base?

82 Logs log 2 27 log 2 9 = It does not matter. log 2 c · log c 27 · log c 9 = log c 27 log c 9 Which is easiest?

83 Logs log 3 27 log 3 9 = log log = 3 2 Please no calculators in exams. And I wont ask log 25 log 9

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

85  x/4   3y (x-3  x 1 )  y Strange(x,y): x 1 =  x/4  ; y1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x1; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); Evaluating: T(n) = aT(n/b)+n c The number of friends is a = 2. T(n) = 2T(n/b)+n c

86 Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); Evaluating: T(n) = aT(n/b)+n c The subinstances need to be smaller, but y 1 =3y is bigger! But we get to decide what is the “size” of the instance as long as –It gets smaller for every friend x 1 ≤ x/4 x 2 = x - 3  x 1  x - 3  x/4 = x/4 Hence, we can let “size” be x. Each subinstance size is x/4, i.e. b=4. –If it is sufficiently small, then we must solve it on our own. If x < 4 then return( xy ); T(n) = 2T(n/b)+n c T(x) = 2T(x/4)+x c

87 Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); Evaluating: T(n) = aT(n/b)+n c The work I personally do is denoted f(x) = x c. –  x/4  requires removing 2 lower order bits. –We will charge  (1) for additions –Hence, f(x) =  (1) = x c for c=0. T(x) = 2T(x/4)+x c T(x) = 2T(x/4)+x 0

88 Evaluating: T(n) = aT(n/b)+n c Time for top level: Time for base cases: Dominated?: c = 0 = log a / log b θ(x ) = log a / log b θ(x ) = θ(x 1/2 ) log 2 / log 4 Hence, T(x) = ? = θ(base cases) = θ(x ) = θ(x 0.5 ). log a / log b x c = = x 0 0 T(x) = 2T(x/4)+x 0

89 Multiplication Algorithms Kindergarten x = n2 n Grade Schooln2n2 Fastest Knownn logn 3*4= Strange (2 friends) x 1/2

90 Evaluating: T(n) = aT(n/b)+n c Time for top level: Time for base cases: Dominated?: c = 0 < ½ = log a / log b θ(x ) = log a / log b θ(x ) = θ(x 1/2 ) log 2 / log 4 Hence, T(x) = ? = θ(base cases) = θ(x ) = θ(x 0.5 ). log a / log b x c = = x 0 0 If we reduce the number of friends from 2 to 1 is the savings just 50%? T(x) = 2T(x/4)+x 0

91 Evaluating: T(n) = aT(n/b)+n c T(x) = T(x/4)+x 0 Time for top level: Time for base cases: (Note if I have one friend and so does he, then there is only one base case.) Dominated?: c = 0 = log a / log b Hence, all θ(logx) layers require a total of θ(1) work. The sum of these layers is no longer geometric. Hence T(x) = θ(logx) = θ(logX). We should charge θ(logX) instead of θ(1) for additions. Then the total time is θ(log 2 X) = θ(n 2 ) where n = # of bits in input. θ(n ) = log a / log b θ(n ) = θ(n 0 ) = 1 log 1 / log 4 n c = = n 0 0 1

92 Strange(x,y): If x < 4 then return( xy ); x 1 =  x/4  ; y 1 = 3y; f 1 = Strange( x 1, y 1 ); x 2 = x - 3  x 1 ; y 2 = y; f 2 = Strange( x 2, y 2 ); return( f 1 +f 2 ); Evaluating: T(n) = aT(n/b)+n c x 1 and x 2 are both about x/4. T(x) = T(x/4)+x 0 1 y 1 =3y and y 2 = y but was just to make the program strange. If the two friends are “identical”, why have two? Strange(x,y): If x≤0 then return( 0 ); f = Strange(  x / 2 , y ); If x is even return( 2  f ); else return( 2  f+y ); y even x = x/2 x/2 y x/2 x/2 f f y odd x = x/2x/2 y x/2x/2 1 f f y

93 Multiplication Algorithms Kindergarten x = n2 n Grade Schooln2n2 Fastest Knownn logn 3*4= Strange (2 friends) x 1/2 Strange (1 friends) n2n2

94 The Overhead Costs of Recursion Many problems are naturally defined recursively. This can lead to simple, elegant code. However, recursive solutions entail a cost in time and memory: each recursive call requires that the current process state (variables, program counter) be pushed onto the system stack, and popped once the recursion unwinds. This typically affects the running time constants, but not the asymptotic time complexity (e.g., O(n), O(n 2 ) etc.) Thus recursive solutions may still be preferred unless there are very strict time/memory constraints.

95 Output Try explaining what this algorithm does by tracing it out. I dare you!!! It is much easier to TAKE ONE STEP AT A TIME

96 n=1 X n=2 Y Output

97 n=1 X n=2 n=3 Y A ? B ? C Output

98 n=1 X n=2 n=3 Y A Y B X C Output

99 n=1 X n=2 n=3 Y AYBXC Output

100 n=1 X n=2 n=3 n=4 Y AYBXC A ? B ? C Output

101 n=1 X n=2 n=3 n=4 Y AYBXC A AYBXC B Y C Output

102 n=1 X n=2 n=3 n=4 Y AYBXC AAYBXCBYC Output

103 n=1 X n=2 n=3 n=4 n=5 Y AYBXC AAYBXCBYC A ? B ? C Output

104 n=1 X n=2 n=3 n=4 n=5 Y AYBXC AAYBXCBYC AAAYBXCBYCBAYBXCC Output

105 n=1 X n=2 n=3 n=4 n=5 Y AYBXC AAYBXCBYC AAAYBXCBYCBAYBXCC Output

106 Time: T(1) =  2T(n-1) +3 T(n-1) + T(n-2) + 3 T(n) = 1 1 T(2) = = 2  (n) defined?

107 Towers of Hanoi This job of mine is a bit daunting. Where do I start? And I am lazy.

108 Towers of Hanoi At some point, the biggest disk moves. I will do that job.

109 Towers of Hanoi To do this, the other disks must be in the middle.

110 Towers 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.

111 Towers of Hanoi

112 Towers of Hanoi Get a job at the Towers of Hanoi company at level n=1,2,3,4,….. Or get transferred in as the president & you decide what your vice president does.

113 Towers 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) ≈ 2 i T(n-i) ≈ 2 n 1 + 2T(n-1)

114 Evaluating: T(n) = aT(n-b)+f(n) h = ? n-hb n-ib n-2b n-b T(0) f(n-ib) f(n-2b) f(n-b) f(n) n Work in Level # stack frames Work in stack frame Instance size a aiai a2a2 a 1 n/bn/b a · T(0) a i · f(n-ib) a 2 · f(n-2b) a · f(n-b) 1 · f(n) n/bn/b |base case| = 0 = n-hb h = n / b i Level Likely dominated by base cases Exponential

115 Evaluating: T(n) = 1T(n-b)+f(n) h = ? Work in Level # stack frames Work in stack frame Instance size f(0) f(n-ib) f(n-2b) f(n-b) f(n) n-b n n-hb n-ib n-2b T(0) f(n-ib) f(n-2b) f(n-b) f(n) h = n / b i Level Total Work T(n) = ∑ i=0..h f(b·i) = θ(f(n)) or θ(n · f(n))

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

117 Check Lists for Recursive Programs This is the format of “all” recursive programs. Don’t deviate from this. Or else!

118 Check Lists for Recursive Programs This is input instance is your mission. You must return this output. (An x, y, & z in every computation path.) Focus on your mission.

119 Check Lists for Recursive Programs The & must document these variables and what you must do.

120 Check Lists for Recursive Programs To get help, you construct an instance for each friend It must be valid and smaller. And pass them to them.

121 Check Lists for Recursive Programs Each friend returns a solution his sub-instance. Trust him to do this. But don’t expect him to do or know anything else.

122 Check Lists for Recursive Programs Combine their solutions to construct a solution to your instance. Return your solution.

123 Check Lists for Recursive Programs If your solution is too small to get solved by the general technique solve it yourself. Be sure you handle every legal instance.

124 Check Lists for Recursive Programs You rarely need to change the values of these variable beyond their initial setting (this is not an iterative algorithm.) Your rarely need additional input, output, or local variable. But it you do document them.

125 Check Lists for Recursive Programs Have NO global variables. If you change the value of a local variable n, neither your friends nor your boss get that value.

126 Sorting Problem Specification : The input is a list of n values with the same value possibly repeated. : The output is a list consisting of the same n values in non-decreasing order ,23,25,30,31,52,62,79,88,98

127 Recursive Sorts Given list of objects to be sorted Split the list into two sub-lists. Recursively have a friend sort the two sub-lists. Combine the two sorted sub-lists into one entirely sorted list.

128 Minimal effort splitting Lots of effort recombining Lots of effort splitting Minimal effort recombining Four Recursive Sorts Merge SortInsertion Sort Quick SortSelection Sort Size of Sub-lists n/2,n/2 n-1,1

129 Merge Sort Divide and Conquer

130 Merge Sort Split 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.

131 Merge Sort Merge two sorted lists into one 25,31,52,88,9814,23,30,62,7914,23,25,30,31,52,62,79,88,98

132

133 Merge Sort Sort Time: T(n) = =  (n log(n)) 2T(n/2) +  (n) Total work at any level =  (n) # levels =  (log(n))

134 Evaluating: T(n) = aT(n/b)+f(n) Level Instance size Work in stack frame # stack frames Work in Level 0n f(n)f(n) 1 1 · f(n) 1 n/bn/bf(n/b)f(n/b) a a · f(n/b) 2 n/b2n/b2 f(n/b2)f(n/b2) a2a2 a 2 · f(n/b 2 ) i n/bin/bi f(n/bi)f(n/bi) aiai a i · f(n/b i ) h = log n / log b n/bhn/bh T(1) n log a / log b n · T(1) log a / log b Total Work T(n) = ∑ i=0..h a i  f(n/b i ) 2T(n/2) +  (n) 2 i ·  (n/2 i ) =  (n) =  (n) · log(n) All levels basically the same.

135 Java Implementation Andranik Mirzaian

136 Java Implementation Andranik Mirzaian

137 Quick Sort Divide and Conquer

138 Quick Sort Partition set into two using randomly chosen pivot ≤ 52 ≤

139 Quick Sort ≤ 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.

140 Quick Sort 14,23,25,30,31 62,79,98,8852 Glue pieces together. (No real work) 14,23,25,30,31,52,62,79,88,98 Faster Because all done “in place” ie in the input array

141

142 Quick Sort Let pivot be the first element in the list? ≤ 31 ≤ 52

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

144 Quick Sort Best Time: Worst Time: Expected Time: T(n) = 2T(n/2) +  (n) =  (n log(n))

145 Quick Sort T(n) = 2T(n/2) +  (n) =  (n log(n)) Best Time: Worst Time: Expected Time: =  (n 2 ) T(n) = T(0) + T(n-1) +  (n)

146 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: =  (n 2 ) T(n) = T( 1 / 3 n) + T( 2 / 3 n) +  (n) =  (n log(n))

147 Quick Sort Expected Time: T(n) = T( 1 / 3 n) + T( 2 / 3 n) +  (n) =  (n log(n)) Top work  (n) 2 nd level 3 rd level # levels = (n)(n) (n)(n) 1/31/3 1/31/3 1/31/3 2/32/3 2/32/3 2/32/3 2/32/3  (log(n)) 3/23/2 log (n)

148 Java Implementation Andranik Mirzaian148

149 K th Element Problem Specification : The input is a list of n values and an integer k. : The k th smallest in the list ,23,25,30,31,52,62,79,88,98 k=3 Output: 25

Partition set into two using randomly chosen pivot ≤ 52 ≤ K th Element Problem Specification

≤ 52 ≤ K th Element Problem Specification k=3 r=5 elements on left 14,23,25,30,31 Get one friend to find k=3 rd element in first half. Output: 25

≤ 52 ≤ K th Element Problem Specification k=8 r=5 elements on left Get one friend to find k=8-5=3 rd element in second half. Output: 79 52,62,79,98,88

153

154 T(n) = K th Element Problem Specification =  (n) 1 T(n/2) +  (n) Best Time: Expected Time: Worst Time: n log a / log b # of base cases = = n 0 = 1 n log 1 / log 2 = n + n / 2 + n / 4 + n / 8 … +1

155 T(n) = =  (n) 1 T(n/2) +  (n) Expected Time: K th Element Problem Specification T(n) = 1 T(n-1) +  (n) Worst Time: ≈ n+T(n-1) ≈ n+(n-1)+T(n-2) ≈ n+(n-1)+(n-2)+T(n-3) ≈ n+(n-1)+(n-2)+(n-3)+…+1 ≈  (n 2 ) Best Time:

156 T(n) = =  (n) 1 T(n/2) +  (n) T(n) = 1 T( 2 / 3 n) +  (n) Expected Time: =  (n) K th Element Problem Specification T(n) = 1 T(n-1) +  (n) Worst Time: =  (n 2 ) Best Time:

157 N=7 N=2 N=1 N=2 N=1 N=2 N=1 N=4N=3 b4b4 b3b3 b 7 = b 3 × b 4 T(N) = 2T(N/2) + 1 =  (N) Size = log(b) + log(N) = 2  (n) Power bNbN

158 N=7 N=1 N=3 b3b3 b 7 = (b 3 ) 2 × b T(N) = 1T(N/2) + 1 =  (log(N)) Size = log(b) + log(N) =  (n) Power

159 Recursion on Trees (define)

160 Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left sub-tree tree because (define)

161 Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left sub-tree tree node (define)

162 Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left sub-tree tree because (define)

163 Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left sub-tree tree node (define)

164 Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left sub-tree tree because (define)

165 Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left sub-tree tree node tree (define)

166 Recursion on Trees A binary tree is: - the empty tree - a node with a right and a left sub-tree tree because empty (define)

167 Recursion on Trees number of nodes = ? Get help from friends (friends)

168 Recursion on Trees number of nodes = number on left + number on right + 1 = = (friends)

169 Recursion on Trees (communication) Being lazy, I will only consider my root node and my communication with my friends. I will never look at my children subtrees but will trust them to my friends. 6

170 Recursion on Trees (communication) We will only communicate through these walls  (1) info via the pre & post-conditions. 6

171 Recursion on Trees (communication) I know only what you tell me via the pre-condition. I tell you only what the post-condition instructs me to.

172 Recursion on Trees (communication) I certainly will tell you the answer for my subtree to the computational problem at hand

173 Recursion on Trees (communication) I can also tell you anything else about this subtree. Simply ask by changing the post-condition.

174 Recursion on Trees (communication) But remember, I know nothing about the outside world (root, other subtree, whether I am left or right.) except what you tell me.

175 Recursion on Trees (communication) I tell you extra information by changing the pre-condition. (Info about left subtree, the root, things my boss told me, …) 6

176 Recursion on Trees (communication) Of course if the pre or post-conditions change then I must fill this contract with my boss too. 6

177 Recursion on Trees Base Case ? number of nodes 1 Are we done? (base case)

178 Recursion on Trees Base Case ? number of nodes ? (base case)

179 Recursion on Trees Base Case ? number of nodes 0 Better base case! (base case)

180 Recursion on Trees Base Case ? number of nodes 1 Does this still need to be a base case? (base case)

181 Recursion on Trees number of nodes = number on left + number on right + 1 = = 1 No! (base case)

182 Recursion on Trees Most programmers don’t use the empty tree as a base case. This causes a lot more work. (base case)

183 Recursion on Trees (code)

184 Recursion on Trees 3 Designing Program/Test Cases generic =1 n1n1 n2n2 n 1 + n n1n1 n Same code works! Try same code 0 Base Case (cases)

185 Time: T(n) = Recursion on Trees This only works for balanced tree. We don’t know how many nodes on left vs right. How do we solve this? aT(n/b) +  (n c ) = 2T(n/2) +  (1) =  (n) = T(left) + T(right) +  (1) = ?? (time)

186 Time: T(n) = Recursion on Trees ∑ stack frame Work done by stack frame X = 2133 Y = 2312 ac = 483 bd = 396 (a+b)(c+d) = 1890 XY = X = 21 Y = 23 ac = 4 bd = 3 (a+b)(c+d) = 15 XY = 483 X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 XY = 396 X = 54 Y = 35 ac = 15 bd = 20 (a+b)(c+d) = 72 XY = 1890 X = 2 Y = 2 XY=4 X = 1 Y = 3 XY=3 X = 3 Y = 5 XY=15 X = 3 Y = 1 XY=3 X = 3 Y = 2 XY=6 X = 6 Y = 3 XY=18 X = 5 Y = 3 XY=15 X = 4 Y = 5 XY=20 X = 9 Y = 8 XY=72 (time)

187 Recursion on Trees One stack frame for each node in the tree and for empty trees hang off And constant work per stack frame. =  (n) × Time: T(n) = ∑ stack frame Work done by stack frame  (1) =  (n) (time)

188 Recursion on Trees number of nodes = One friend for each sub-tree Many Children = 13 (friends) (mult-children)

189 Recursion on Trees (code) (mult-children)

190 Recursion on Trees 3 Designing Program/Test Cases generic =1 n1n1 n Same code works! Try same code generic n1n1 n3n3 n 1 + n 2 + n n2n2 But is this needed (if not the input) (cases) (mult-children)

191 Recursion on Trees (code) (mult-children)

192 Recursion on Trees One stack frame for each node in the tree And constant work per stack frame. =  (n) × Time: T(n) = ∑ stack frame Work done by stack frame  (1) =  (n)  (n) =  (n 2 ) (time) (mult-children)

193 Recursion on Trees Time: T(n) = ∑ stack frame Work done by stack frame  (edge Tree ) (time) (mult-children) = ∑ stack frame  (# subroutine calls)  (node Tree )  (n) = ∑ node  (# edge in node)

194 We pass the recursive program a “binary tree”. But what type is it really? This confused Jeff at first. class LinkedBinaryTree { class Node { E element; Node parent; Node left; Node right; } Node root = null; Tree Recursion on Trees

195 One would think tree is of type LinkedBinaryTree. Then getting its left subtree, would be confusing. class LinkedBinaryTree { class Node { E element; Node parent; Node left; Node right; } Node root = null; Tree Recursion on Trees left_Tree (LinkedBinaryTree tree)

196 One would think tree is of type LinkedBinaryTree. Then getting its left subtree, would be confusing. class LinkedBinaryTree { class Node { E element; Node parent; Node left; Node right; } Node root = null; Tree Recursion on Trees left_Tree (LinkedBinaryTree tree)

197 It is easier to have tree be of type Node. But it is thought of as the subtree rooted at the node pointed at. The left child is then class LinkedBinaryTree { class Node { E element; Node parent; Node left; Node right; } Node root = null; Tree Recursion on Trees (Node tree) tree tree.left or tree.Getleft() or Tree.leftSub(tree) or leftSub(tree)

198 But the outside user does not know about pointers to nodes. class LinkedBinaryTree { class Node { E element; Node parent; Node left; Node right; } Node root = null; public int NumberNodes() { return NumberNodesRec( root ); } Tree Recursion on Trees tree private int NumberNodesRec (Node tree) NumberNodesRec

199 Recursion on Trees sum of nodes = ? Get help from friends (sum)

200 Recursion on Trees sum of nodes = sum on left + sum on right + value at root = = (sum)

201 Recursion on Trees Base Case ? sum of nodes = ? 0 (sum)

202 Recursion on Trees (sum)

203 Recursion on Trees 3 generic s1s1 s2s2 s 1 + s s1s1 s =3 Designing Program/Test Cases (sum)

204 Recursion on Trees max of nodes = ? Get help from friends (max)

205 Recursion on Trees max of nodes = max( max on left, max on right, value at root) = max( 8, 9, 3) = (max)

206 Recursion on Trees Base Case ? max of nodes = ? ? (max)

… Sum so far is 17 Sum so far is0 Recursion on Trees NewSum = OldSum + nextElement = = 3 (max)

208 3*4*2*3*… Product so far is 72 Product so far is1 Recursion on Trees NewProd = OldProd × nextElement = 1 × 3 = 3 (max)

209 True and True and False and … Conclusion so far is True Conclusion so far isTrue Recursion on Trees NewProd = OldProd and nextElement = True and True = True (max)

210 Max(3,4,2,3,… Max so far is 4 Max so far is -- Recursion on Trees NewMax = max(OldMax, next Element) = max( - , 3 ) = 3 (max)

211 Recursion on Trees (max)

212 Recursion on Trees height of tree = ? Get help from friends (height)

213 Recursion on Trees height of tree = max(height of left,height on right) + 1 = max(3,4) + 1 = (height)

214 Recursion on Trees Base Case ? height of tree ? (height)

215 Recursion on Trees height of tree? 2 nodes or 1 edge 0 nodes ? edges Base Case ? (height)

216 Recursion on Trees Work it out backwards ? edges height = = max(height of left,height on right) + 1 = max(?,?) + 1 ? ? 0 edge (height)

217 Recursion on Trees Work it out backwards -1 edges height = = max(height of left,height on right) + 1 = max(-1,-1) edge (height)

218 Recursion on Trees (height)

219 Recursion on Trees number of leaves = ? Get help from friends (#leaves)

220 Recursion on Trees number of leaves = number on left + number on right = = (#leaves)

221 Recursion on Trees Base Case ? number of leaves 0 (#leaves)

222 Recursion on Trees 3 generic L1L1 L2L2 L 1 + L 2 0 L1L1 L1L = 0 Needs to be another base case. 1 No! Designing Program/Test Cases (#leaves)

223 Recursion on Trees (#leaves)

224 Recursion on Trees (travers)

225 Recursion on Trees (copy)

226 Recursion on Trees ? Drops the tree. (deallocate)

Define Problem: Binary Search Tree –Key 25 –A binary search tree. – –Find key in BST (if there). Recursion on Trees (BST)

Binary Search Tree Its left children ≤ Any node ≤ Its right children ≤ ≤ (BST)

229 Define Loop Invariant Maintain a sub-tree. If the key is contained in the original tree, then the key is contained in the sub-tree. key (BST)

230 Define Step Cut sub-tree in half. Determine which half the key would be in. Keep that half. key 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 (BST)

231 Recursion on Trees (BST)

232 Things to Remember Things to Do and Things NOT TO DO

233 Trust your friends to solve sub-instances. The sub-instance given must be smaller and must be an instance to the same problem. Combine solution given by friend to construct your own solution for your instance. Focus on one step. Do not talk of their friends friends friends. Solve small instances on your own. I am obsessed with the Friends - Strong Induction View of Recursion. Do it!

234 Define pre & post conditions Don't have inputs or outputs that are not explained! Typical Test Answer

235 Typical Test Answer Call recursively on the correct types of input k,num,v

236 Typical Test Answer Call recursively Save the results (or don't bother calling) returning the correct types of output

237 Typical Test Answer Combine solutions given by friends to construct your own solution.

238 Typical Test Answer Return things of the correct types. Not an element In every path through code the root?

239 Typical Test Answer Sub-instances need to be smaller. “Size” is size of the tree Wrong base case

240 Typical Test Answer Still zero. The friend has his own variable n. What is the value of n here? No Global Variables.

241 Typical Test Answer No Global Variables. Maybe they mean to pass the new n in and out. Please don’t use these to pass out values when I am marking.

242 Typical Test Answer Looks like an iterative algorithm Which is it? Looks like an recursive algorithm

243 Recursion on Trees Evaluate Equation Tree = ?  Get help from friends 3+4  7 (3+4)  7

244 Recursion on Trees Evaluate Equation Tree 7 7 = root op ( value on left, value on right ) = root op (7,7) = 7  7 = 49  + (3+4)  7

245 Recursion on Trees Evaluate Equation Tree 7 Base Case ?  + (3+4)  7

246

247 Derivatives Input: a function f. Output: Its derivative df / dx.

248 Derivatives

249 Derivatives Input: a function f. Output: Its derivative df / dx.

250 Derivatives Input: a function f. Output: Its derivative df / dx.

251 Derivatives Input: a function f. Output: Its derivative df / dx.

252 Derivatives Input: a function f. Output: Its derivative df / dx.

253 Derivatives Input: a function f. Output: Its derivative df / dx.

254 Derivatives Input: a function f. Output: Its derivative df / dx. g

255 Simplify Input: a function f. Output: f simplified.

256 Simplify

257 Iterate over all s-t Paths Precondition: Directed Acyclic Graph Postcondition: Iterate over all s-t paths.

258 Iterate(G,s,t) loop over all edges Iterate(G,v,t) Iterate over all s-t Paths Precondition: Directed Acyclic Graph Postcondition: Iterate over all s-t paths. For each edge, I get a friend to iterate over all v-t paths.

259 Iterate(G,s,t) loop over all edges Iterate(G,v,t) Iterate over all s-t Paths Precondition: Directed Acyclic Graph Postcondition: Iterate over all s-t paths. For each edge, I get a friend to iterate over all v-t paths.

260 Iterate(G,s,t) loop over all edges Iterate(G,v,t) Iterate over all s-t Paths Precondition: Directed Acyclic Graph Postcondition: Iterate over all s-t paths. For each edge, I get a friend to iterate over all v-t paths.

261 Iterate(G,s,t) loop over all edges Iterate(G,v,t) Iterate over all s-t Paths Precondition: Directed Acyclic Graph Postcondition: Iterate over all s-t paths. For each edge, I get a friend to iterate over all v-t paths. The number of such paths likely is 2  (n)

262 Recursive Images if n=0, draw else draw And recursively Draw here with n-1 if n=1 n=0

263 Recursive Images if n=0, draw else draw And recursively Draw here with n-1 if n=2 n=1

264 Recursive Images if n=0, draw else draw And recursively Draw here with n-1 if n=3 n=2

265 Recursive Images if n=0, draw else draw And recursively Draw here with n-1 if n=30

266 Recursive Images if n=1if n=2if n=3 if n=4 if n=5 if n=0

267 Recursive Images if n=1 if n=0 if n=2if n=3if n=4 if n=5

268 Recursive Images L(n) = 4 / 3 L(n-1)    = ( 4 / 3 ) n

269

270

271

272

273

274 Tail Recursion Tail recursion occurs when a linearly recursive method makes its recursive call as its last step. The array reversal method is an example. Such methods can be easily converted to non-recursive methods (which saves on some resources). Example: Algorithm IterativeReverseArray(A, i, j ) Input: An array A and valid indices i & j Output: sub-array A[i..j] reversed while i < j do Swap A[i ] and A[ j ] i  i+1, j  j – 1 end EECS2011: Recursion 274 Last Update: Oct 1, 2014

275 Please feel free to ask questions! Please give me feedback so that I can better serve you. Thanks for the feedback that you have given me already.

276 End

277 Input: Output: s=6*8+((2+42)*(5+12)+987*7*123+15*54) Parsing

278 Parsing

279 Parsing

280 Parsing Algorithm: GetExp( s, i ) Input: s is a string of tokens i is a start index Output: p is a parsing of the longest valid expression j is the end index s=6*8+((2+42)*(5+12)+987*7*123+15*54)

281 Parsing Algorithm: GetTerm( s, i ) Input: s is a string of tokens i is a start index Output: p is a parsing of the longest valid term j is the end index s=6*8+((2+42)*(5+12)+987*7*123+15*54)

282 Parsing Algorithm: GetFact( s, i ) Input: s is a string of tokens i is a start index Output: p is a parsing of the longest valid factor j is the end index s=6*8+((2+42)*(5+12)+987*7*123+15*54)

283 Algorithm: GetExp( s, i ) s=6*8+((2+42)*(5+12)+987*7*123+15*54) p

284 Algorithm: GetExp( s, i ) Exp … p + + +

285 Algorithm: GetTerm( s, i ) s=6*8+((2+42)*(5+12)+987*7*123+15*54) p

286 Algorithm: GetTerm( s, i ) Term … p * * *

287 Parsing Algorithm: GetFact( s, i ) s=6*8+((2+42)*(5+12)+987*7*123+15*54)

288 Parsing Algorithm: GetFact( s, i ) Fact 42

289 Algorithm: GetFact( s, i ) s=6*8+((2+42)*(5+12)+987*7*123+15*54) p

290 Algorithm: GetFact( s, i ) Fact p ()

291 Parsing Stackframes  nodes in parse tree