Recursion & Performance. Recursion. Recursion. Recursion. Where Recursion Shines. Breaking a Problem Down

Similar documents
Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

DESIGN AND ANALYSIS OF ALGORITHMS

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Introduction and Overview

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance

CS 310 Advanced Data Structures and Algorithms

Week 12: Running Time and Performance

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

UNIT 1 ANALYSIS OF ALGORITHMS

Lecture 10: Recursion vs Iteration

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

An algorithm is a sequence of instructions that one must perform in order to solve a wellformulated

Solving problems by recursion

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

Algorithms and Data Structures

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

Choice of C++ as Language

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

STUDENT LESSON A9 Recursion

Lesson 12: Recursion, Complexity, Searching and Sorting. Modifications By Mr. Dave Clausen Updated for Java 1_5

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Computer Algorithms. Introduction to Algorithm

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

Chapter 15: Recursion

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS


Introduction to Data Structure

CS240 Fall Mike Lam, Professor. Algorithm Analysis

L.J. Institute of Engineering & Technology Semester: VIII (2016)

CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms. Catie Baker Spring 2015

CSC 8301 Design and Analysis of Algorithms: Exhaustive Search

CS 206 Introduction to Computer Science II

Analysis of Algorithms. CSE Data Structures April 10, 2002

Recursive Definitions

6.001 Notes: Section 4.1

Algorithmic Analysis. Go go Big O(h)!

Analysis of Algorithms Part I: Analyzing a pseudo-code

Measuring algorithm efficiency

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Algorithm Analysis CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Introduction to Programming I

Lecture 1. Introduction

Classic Data Structures Introduction UNIT I

Recall from Last Time: Big-Oh Notation

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

DEEPIKA KAMBOJ UNIT 2. What is Stack?

CS240 Fall Mike Lam, Professor. Algorithm Analysis

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

INDEX. Cambridge University Press How to Think About Algorithms Jeff Edmonds Index More information

Programming & Data Structure

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

Module 05: Types of recursion

Outline and Reading. Analysis of Algorithms 1

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 7 Complexity and Orders of Growth TODAY PROBLEM SOLVING: ALGORITHMS COMPARISON OF ALGORITHMS 7/10/2012

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Algorithms and Data Structures

Analysis of Algorithm. Chapter 2

Recursion. CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

NCS 301 DATA STRUCTURE USING C

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

Recursion. Chapter 7

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

We will use the following code as an example throughout the next two topics:

CSE 373: Data Structures and Algorithms

Chapter 2: Complexity Analysis

Chapter Fourteen Bonus Lessons: Algorithms and Efficiency

アルゴリズムの設計と解析 (W4022) 教授 : 黄潤和 広野史明 (A4/A10)

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

More Complicated Recursion CMPSC 122

O(n): printing a list of n items to the screen, looking at each item once.

5105 BHARATHIDASAN ENGINEERING COLLEGE

Data Structures and Algorithms

DESIGN AND ANALYSIS OF ALGORITHMS

6/4/12. Recursive void Methods. Chapter 11. Vertical Numbers. Vertical Numbers. Vertical Numbers. Algorithm for Vertical Numbers

Module 10. Recursion. Adapted from Absolute Java, Rose Williams, Binghamton University

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

Basic Data Structures (Version 7) Name:

Algorithms: Efficiency, Analysis, techniques for solving problems using computer approach or methodology but not programming

Recursion Chapter 3.5

CSC 222: Object-Oriented Programming. Spring 2012

Recursion. Recursion is: Recursion splits a problem:

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

Elementary maths for GMT. Algorithm analysis Part I

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Transcription:

Recursion & Performance Recursion Part 7 The best way to learn recursion is to, first, learn recursion! Recursion Recursion Recursion occurs when a function directly or indirectly calls itself This results in a loop However, it doesn't use iterative structures such as For or While loops This can greatly simply programming tasks Commonly used to traverse a graph, tree, or run complex calculations but can also create pitfalls 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 3 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 4 Breaking a Problem Down Where Recursion Shines Recursion allows a problem to be broken down into smaller instances of themselves Each call will represent a smaller, simpler, version of the same problem Eventually, it will reach a "base case" which will not require any more recursive calls When the program can be broken into smaller pieces, recursion is a great solution Examples: graph traversal searching, etc. state machines sorting many math problems 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 5 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 6 1

Recursion Overhead Okay, What Happens? Recursion, while powerful, is costly on computer resources Each time a function calls another function an activation record is placed on the stack Huh? You will learn about that in CSC 130 When a function A calls B A is temporarily suspended information about A local variables, position, etc is stored for later the computer then transfers control to B When function B completes the information saved about function A is read function A continues from where it left off 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 7 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 8 Danger: Never Ending Danger: Accidental Recursion If you break down a task into smaller parts at some point, it should become a single value If not, the function will never end and will recurse forever at least until the computer runs out of resources Accidental recursion is a common mistake my beginner programmers It might be done directly or indirectly for example: A calls B, B calls C, C calls A so, it is important to organize your code carefully 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 9 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 10 Results of These Dangers Example: To infinity but not beyond Either will crash your program function will recurse forever eventually all memory is exhausted You will see either "stack overflow" error "heap exhaustion" error void toinfinity() System.out.println("To infinity!"); toinfinity(); System.out.println("and beyond!"); We never get here! 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 11 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 12 2

Designing a Recursive Function Example: Factorials Does the problem lend itself to recursion? can the problem be broken down into smaller instances of itself? is there a iterative version that is better Is there a base case? is there a case where recursion will stop? remember: ALWAYS have a stopping point! Factorials are classic mathematical problem that lends itself easily to recursion If you don't remember, a factorial of n is defined as the value of n multiplied by all lesser integers 1 For example: 5! 5 4 3 2 1 120 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 13 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 14 Example: Factorials Example: Factorials It should be easy to observe that n! can be defined as n (n 1)! So, n! can be computed by multiplying n by the factorial of one less than it 4! 4 3! 4 3 2! 4 3 2 1 int fact(int n) if (n == 1) base case return 1; else return n * fact(n 1); Recursion 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 15 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 16 Example Factorial Iteration vs. Recursion fact(4) 4 * fact(3) 6 fact(3) 3 * fact(2) 2 fact(2) 2 * fact(1) fact(1) 1 1 Any program that can be expressed using recursion, can be done through iteration The recursive solution will often be far simpler more "eloquent" to read but is never more efficient due to the overhead of calling functions 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 17 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 18 3

Some Well-known Problems Historical Perspective Sorting Searching Shortest paths in a graph Minimum spanning tree Primality testing Some really cool solutions! 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 20 Some Well-known Problems Fibonacci Numbers Traveling salesman problem Knapsack problem Chess Towers of Hanoi Program termination Rabbits tend to reproduce like well rabbits Mathematician Fibonacci analyzed this situation and created a mathematical system to predict this phenomena It is used today in finance, simulation, and several computer science algorithms 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 21 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 22 Fibonacci Numbers Fibonacci Numbers The problem: start with a pair of rabbits at month #2, the rabbits begin to reproduce the female gives birth to a new pair of rabbits: one male and one female babies mature at the same rate and will have babies Fibonacci number sequences predict the total pairs after n months The problem: start with a pair of rabbits at month #2, the rabbits begin to reproduce the female gives birth to a new pair of rabbits: one male and one female babies mature at the same rate and will have babies Fibonacci number sequences predict the total pairs after n months 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 23 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 24 4

Fibonacci Numbers Example: Fibonacci Numbers After two months, the female gives birth creating a new pair. then they get pregnant again! This continues forever.. Sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34,... if n == 1 then Fib(n) = 1 if n == 2 then Fib(n) = 1 if n > 2 then Fib(n) = Fib(n-2) + Fib(n-1) int fib(int n) if (n == 1 n == 2) return 1; else return fib(n-2) + fib(n-1); Recursion 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 25 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 26 Greatest Common Denominator Euclid s Algorithm A common problem in computer science is finding the greatest (or least) common denominator for two integers For example, the GCD of 64 and 40 is 8 Euclid (of geometry fame) created an ingenious algorithm for finding the greatest common divisor Euclid's algorithm is recursive You reapply the expression below until the second value of gcd(m,n) is zero. In this case, m will be the CGD gcd(m,n) gcd(n, m mod n) 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 27 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 28 Euclid s Algorithm Examples 60 and 24 gcd(60, 24) gcd(24,12) gcd(12, 0) the result is 12 84 and 20 gcd(84, 20) gcd(20, 4) gcd(4, 0) result is 4 These might seem trivial, but it can find HUGE numbers quite easily Uh, "O" Order of Growth 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 29 5

Order of Growth Order of Growth One property of functions that we are interested in its rate of growth Rate of growth doesn't simply mean the "slope" of the line associated with a function Instead, it is more like the curvature of the line What is important is how an algorithm's time grows as n In computer science several types of growth occur 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 31 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 32 Order of Growth Several Growth Functions Algorithms will fall into one of these categories for worst-case, best-case, and average-case Examples: how faster will it run on computer that is twice as fast? how long does it take with double the input size? There are several functions In increasing order of growth, they are: Constant 1 Logarithmic log n Linear n Log Linear n log n Quadratic n 2 Exponential 2 n 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 33 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 34 Growth Rates Compared Classifications n = 1 2 4 8 16 1 1 1 1 1 1 log n 0 1 2 3 4 n 1 2 4 8 16 n log n 0 2 8 24 64 n 2 1 4 16 64 256 n 3 1 8 64 512 4096 2 n 2 4 16 256 65536 Using the known growth rates algorithms are classified using three notations these allows you to see, quickly, the advantages/disadvantages of an algorithm Major notations: Big-O Big-Theta Big-Omega 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 35 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 36 6

Order of Growth Big-O Notation Name Meaning O ( n ) Θ ( n ) Ω ( n ) Big-O Big-Theta Big-Omega class of functions f(n) that grow no faster than n class of functions f(n) that grow at same rate as n class of functions f(n) that grow at least as fast as n So, Big-O notation gives an upper bound on growth of an algorithm We will use Big-O almost exclusively rather than the other two 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 37 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 38 Big-O O(1) The following means that the growth rate of f(n) is no more than the growth rate of n This is one of the classifications mentioned earlier f(n) is O(n) Represents a constant algorithm It does not increase / decrease depending on the size of n Examples appending to a linked list (with an end pointer) array element access practically all simple statements 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 39 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 40 O(log n) O(log n) Examples Represents logarithmic growth These increase with n, but the rate of growth diminishes For example: for base 2 logs, the growth only increases by one each time n doubles Searching for an item on a sorted array (e.g. a binary search) Traversing a sorted tree 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 41 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 42 7

O(n) O(n log n) Represents an algorithm that grows linearly with n Very common in programming for iteration Examples: finding an item in a linked list merging two sorted arrays Represents an algorithm that has "log linear" growth These algorithms grow based on both n and n's log value 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 43 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 44 O(n log n) Examples O(n 2 ) Quick Sort Heap Sort Merge Sort Fourier transformation Represents an algorithm that has "exponential" growth These algorithms grow dramatically fast depending on the size of n Avoid for large values of n! 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 45 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 46 O(n 2 ) Examples Growth: 1 to 10 Bubble Sort, Selection Sort, etc. matrix multiplication merging unsorted arrays 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 47 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 48 8

Growth: 1 to 100 Growth: 1 to 1000 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 49 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 50 Time Given a 1 Microsecond Op Why it is O-some! n O(log n) O(n) O(n log n) O(n 2 ) 10 0.000003 0.000010 0.000033 0.000100 100 0.000007 0.000100 0.000664 0.010000 1,000 0.000010 0.001000 0.009966 1.000000 10,000 0.000013 0.010000 0.132877 100.000000 100,000 0.000017 0.100000 1.660964 6.94 days 1,000,000 0.000020 1.000000 19.931569 1.9 years 10,000,000 0.000023 10.000000 232.534966 190.2 years These classes make it is easy to compare algorithms for efficiency making decisions on which algorithm to use determining the scalability of an algorithm So, if two algorithms are the same class they have the same rate of growth both are equally valid solutions 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 51 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 52 Asymptotic Analysis Big-O Math Time for a "Big-O" headache Any algorithm can be analyzed and its complexity/growth can be written an a simple mathematical expression Asymptotic analysis of an algorithm determines the running time in big-o notation 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 54 9

Asymptotic Analysis Asymptotic Analysis 1. Find the worst-case number of primitive operations executed as a function of the input size 2. Eliminate meaningless values the base rate in found Example: If we analyze an algorithm and find it executes 12 * n 1 constant factors and lower-order terms dropped they become meaningless for large values of n remember, this is a growth rate it will be "O(n) 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 55 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 56 Examples Test Your Might 3000n + 7 is O(n) 2n 5 + 3n 3 + 5 is O(n 5 ) 7n 3-2n + 3 is O(n 3 ) for(i = 0; i < 100; i++) total += values[i]; O(1) 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 57 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 58 Test Your Might Test Your Might for(x = 0; x < n; x++) sum += score[x]; for(x = 0; x < n; x++) sum -= score[x]; O(n) for (x = 0; x < n; x++) for (y = 0; y < x; y++) sum += x - y; O(n 2 ) 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 59 5/3/2018 Sacramento State - Cook - CSc 28 - Spring 2018 60 10