Recursion and Dynamic Programming. Biostatistics 615/815 Lecture 5



Similar documents
What Is Recursion? Recursion. Binary search example postponed to end of lecture

Analysis of Binary Search algorithm and Selection Sort algorithm

Biostatistics 615/815

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Section IV.1: Recursive Algorithms and Recursion Trees

Today s Topics. Primes & Greatest Common Divisors

CS 103X: Discrete Structures Homework Assignment 3 Solutions

8 Primes and Modular Arithmetic

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Zabin Visram Room CS115 CS126 Searching. Binary Search

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Intermediate Math Circles March 7, 2012 Linear Diophantine Equations II

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

8 Divisibility and prime numbers

Factoring Algorithms

Regression Verification: Status Report

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Data Structures. Algorithm Performance and Big O Analysis

Homework until Test #2

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

Loop Invariants and Binary Search

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

3 length + 23 size = 2

The Mixed Binary Euclid Algorithm

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

Pseudo code Tutorial and Exercises Teacher s Version

6.1 The Greatest Common Factor; Factoring by Grouping

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Recursion vs. Iteration Eliminating Recursion

Data Structures and Algorithms Written Examination

Lecture Notes on Linear Search

Kevin James. MTHSC 412 Section 2.4 Prime Factors and Greatest Comm

Section 4.2: The Division Algorithm and Greatest Common Divisors

10CS35: Data Structures Using C

Algorithms and Data Structures Written Exam Proposed SOLUTION

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Analysis of a Search Algorithm

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

CHAPTER 5. Number Theory. 1. Integers and Division. Discussion

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

GREATEST COMMON DIVISOR

APP INVENTOR. Test Review

Data Structures and Data Manipulation

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Lecture 13 - Basic Number Theory.

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Discrete Mathematics, Chapter 4: Number Theory and Cryptography

6. Standard Algorithms

2WB05 Simulation Lecture 8: Generating random variables

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

The last three chapters introduced three major proof techniques: direct,

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Dynamic Programming. Lecture Overview Introduction

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

Cryptography and Network Security Number Theory

I PUC - Computer Science. Practical s Syllabus. Contents

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Curriculum Map. Discipline: Computer Science Course: C++

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Searching Algorithms

Computer Systems Architecture

Digital Signature. Raj Jain. Washington University in St. Louis

Solving Problems Recursively

Chapter 1. Computation theory

Lecture 3: Finding integer solutions to systems of linear equations

Data Structure with C

csci 210: Data Structures Recursion

Moving from CS 61A Scheme to CS 61B Java

Binary Heap Algorithms

OPTIMAL BINARY SEARCH TREES

Algorithm Design and Recursion

Computer Science 210: Data Structures. Searching

How To Teach Math

15 Prime and Composite Numbers

Data Structures and Algorithms

PES Institute of Technology-BSC QUESTION BANK

Now is the time. For all good men PERMUTATION GENERATION. Princeton University. Robert Sedgewick METHODS

MATH 289 PROBLEM SET 4: NUMBER THEORY

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Symbol Tables. Introduction

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Chapter 7: Additional Topics

Grade 7/8 Math Circles Fall 2012 Factors and Primes

Example of a Java program

FACTORS AND MULTIPLES Answer Key

Sample Questions Csci 1112 A. Bellaachia

Chapter 5. Recursion. Data Structures and Algorithms in Java

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

FACTORING. n = fall in the arithmetic sequence

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Fast Sequential Summation Algorithms Using Augmented Data Structures

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

Binary Search Trees (BST)

Transcription:

Recursion and Dynamic Programming Biostatistics 615/815 Lecture 5

Last Lecture Principles for analysis of algorithms Empirical Analysis Theoretical Analysis Common relationships between inputs and running time Described two simple search algorithms

Recursive refers to A function that is part of its own definition e.g. Factorial( N) = N Factorial( N 1 1) if if N N > = 0 0 A program that calls itself

Key Applications of Recursion Dynamic Programming Related to Markov processes in Statistics Divide-and-Conquer Algorithms Tree Processing

Recursive Function in C int factorial (int N) { if (N == 0) return 1; else return N * factorial(n - 1); }

Key Features of Recursions Simple solution for a few cases Recursive definition for other values Computation of large N depends on smaller N Can be naturally expressed in a function that calls itself Loops are sometimes an alternative

A Typical Recursion: Euclid s Algorithm Algorithm for finding greatest common divisor of two integers a and b If a divides b GCD(a,b) is a Otherwise, find the largest integer t such that at + r = b GCD(a,b) = GCD(r,a)

Euclid s Algorithm in C int gcd (int a, int b) { if (a == 0) return b; return gcd(b % a, a); }

Evaluating GCD(4458, 2099) gcd(2099, 4458) gcd(350, 2099) gcd(349, 350) gcd(1, 349) gcd(0, 1)

Divide-And-Conquer Algorithms Common class of recursive functions Common feature Process input Divide input in smaller portions Recursive call(s) process at least one portion Recursion may sometimes occur before input is processed

Recursive Binary Search int search(int a[], int value, int start, int stop) { // Search failed if (start > stop) return -1; // Find midpoint int mid = (start + stop) / 2; // Compare midpoint to value if (value == a[mid]) return mid; // Reduce input in half!!! if (value < a[mid]) return search(a, start, mid 1); else return search(a, mid + 1, stop); }

Recursive Maximum int Maximum(int a[], int start, int stop) { int left, right; // Maximum of one element if (start == stop) return a[start]; left = Maximum(a, start, (start + stop) / 2); right = Maximum(a, (start + stop) / 2 + 1, stop); // Reduce input in half!!! if (left > right) return left; else return right; }

An inefficient recursion Consider the Fibonacci numbers Fibonacci( N) = Fibonacci( N 0 1 1) + Fibonacci( N 2) if if N = 0 N = 1

Fibonacci Numbers int Fibonacci(int i) { // Simple cases first if (i == 0) return 0; if (i == 1) return 1; return Fibonacci(i 1) + Fibonacci(i 2); }

Terribly Slow! Time (seconds) Calculating Fibonacci Numbers Recursively 60 50 40 30 20 10 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Fibonacci Number Time

What is going on?

Faster Alternatives Certain quantities are recalculated Far too many times! Need to avoid recalculation Ideally, calculate each unique quantity once.

Dynamic Programming A technique for avoiding recomputation Can make exponential running times become linear!

Bottom-Up Dynamic Programming Evaluate function starting with smallest possible argument value Stepping through possible values, gradually increase argument value Store all computed values in an array As larger arguments evaluated, precomputed values for smaller arguments can be retrieved

Fibonacci Numbers in C int Fibonacci(int i) { int fib[large_number], j; fib[0] = 0; fib[1] = 1; for (j = 2; j <= i; j++) fib[j] = fib[j 1] + fib[j 2]; return fib[i]; }

Fibonacci With Dynamic Memory int Fibonacci(int i) { int * fib, j, result; if (i < 2) return i; fib = malloc(sizeof(int) * (i + 1)); fib[0] = 0; fib[1] = 1; for (j = 2; j <= i; j++) fib[j] = fib[j 1] + fib[j 2]; result = fib[i]; free(fib); return result; }

Top-Down Dynamic Programming Save each computed value as final action of recursive function Check if pre-computed value exists as the first action

Fibonacci Numbers // Note: savef should be a global array initialized all zeros int Fibonacci(int i) { // Simple cases first if (savef[i] > 0) return savef[i]; if (i <= 1) return i; // Recursion savef[i] = Fibonacci(i 1) + Fibonacci(i 2); return savef[i]; }

Much less recursion now

Dynamic Programming Top-down vs. Bottom-up In bottom-up programming, programmer has to do the thinking by selecting values to calculate and order of calculation In top-down programming, recursive structure of original code is preserved, but unnecessary recalculation is avoided.

Examples of Useful Settings for Dynamic Programming Calculating Binomial Coefficients Evaluating Poisson-Binomial Distribution

Binomial Coefficients The number of subsets with k elements from a set of size N + = 1 1 1 k N k N k N 1 0 = = N N N

Bottom-Up Implementation in C int Choose(int N, int k) { int i, j, M[MAX_N][MAX_N]; for (i = 1; i <= N; i++) { M[i][0] = M[i][i] = 1; for (j = 1; j < i; j++) M[i][j] = M[i - 1][j - 1] + M[i - 1][j]; } return M[N][k]; }

Top-Down Implementation (I) #define MAX_N 30 int choices[max_n][max_n]; void InitChoose() { int i, j; for (i = 0; i < MAX_N; i++) for (j = 0; j < MAX_N; j++) choices[i][j] = 0; }

Top-Down Implementation (II) int Choose(int N, int k) { // Check if this is an easy case if (N == k k == 0) return 1; // Or a previously examined case if (choices[n][k] > 0) return choices[n][k]; // If neither of the above helps, use recursion choices[n][k] = Choose(N 1, k - 1) + Choose(N 1, k); return choices[n][k]; }

Poisson-Binomial Distribution X 1, X 2,, X n are Bernoulli random variables Probability of success is p k for X k k X k has Poisson-Binomial Distribution

Recursive Formulation ) ( ) (1 1) ( ) ( 1) ( ) ( (0) ) (1 (0) (1) 1 (0) 1 1 1 1 1 1 1 1 i P p i P p i P j P p j P P p P p P p P j j j j j j j j j j j + = = = = =

Summary Recursive functions Arise very often in statistics Dynamic programming Bottom-up Dynamic Programming Top-down Dynamic Programming Dynamic program is an essential tool for statistical programming

Good Programming Practices Today s examples used global variables Variables declared outside a function Accessible throughout the program In general, these should be used sparingly Two alternatives are: Using static variables that are setup on the first call Using C++ to group a function and its data

Function with Built-In Initialization int Choose(int N, int k) { static int valid = 0, choices[max_n][max_n], i, j; // Check if we need to initialize data if (valid == 0) { for (i = 0; i < MAX_N; i++) for (j = 0; j < MAX_N; j++) choices[i][j] = 0; valid = 1; } // Check if this is an easy case if (N == k k == 0) return 1; // Or a previously examined case if (choices[n][k] > 0) return choices[n][k]; // If neither of the above helps, use recursion choices[n][k] = Choose(N 1, k - 1) + Choose(N 1, k); return choices[n][k]; }

C++ Declarations (for.h file) class Choose { public: // This function is called to initialize data // Whenever a variable of type Choose is created Choose(); // This function is called to calculate N_choose_k int Evaluate(int N, int k); private: int choices[max_n][max_n]; };

C++ Code (for.cpp file) Choose::Choose() { for (int i = 0; i < MAX_N; i++) for (int j = 0; j < MAX_N; j++) choices[i][j] = 0; } int Choose::Evaluate() { // Check if this is an easy case if (N == k k == 0) return 1; // Or a previously examined case if (choices[n][k] > 0) return choices[n][k]; // If neither of the above helps, use recursion choices[n][k] = Choose(N 1, k - 1) + Choose(N 1, k); return choices[n][k]; }

Using C++ Code #include Choose.h int main() { Choose choices; // Evaluate 20_choose_10 choices.evaluate(20, 10); // Evaluate 30_choose_10 choices.evaluate(30, 10); return 0; }

Reading Sedgewick, Chapters 5.1 5.3