Recursion and Dynamic Programming CSC 172 SPRING 2004 LECTURE 9.

Slides:



Advertisements
Similar presentations
Dynamic Programming 25-Mar-17.
Advertisements

Introduction to Recursion and Recursive Algorithms
Types of Algorithms.
15-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
Data Structures and Algorithms (60-254)
CSC 427: Data Structures and Algorithm Analysis
16-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
CSE115/ENGR160 Discrete Mathematics 02/28/12
1 Dynamic Programming Jose Rolim University of Geneva.
Recursion.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
CSE 326 Analyzing Recursion David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Scoping and Recursion CSC 171 FALL 2004 LECTURE 12.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Recitation 10. Induction Introduction. Induction is useful in proving properties of recursive algorithms, like their execution times. It is also the.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion More on Project 2 Reading L&C 10.1 – 10.4.
CS 206 Introduction to Computer Science II 10 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
Fundamental in Computer Science Recursive algorithms 1.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
CSC 172 DATA STRUCTURES. DYNAMIC PROGRAMMING TABULATION MEMMOIZATION.
Dynamic Programming CSC 172 SPRING 2002 LECTURE 6.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
Mathematics Review Exponents Logarithms Series Modular arithmetic Proofs.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
Fundamentals of Algorithms MCS - 2 Lecture # 7
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Dynamic programming  top-down vs. bottom-up  divide & conquer vs. dynamic programming  examples:
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Dynamic Programming. Algorithm Design Techniques We will cover in this class: ◦ Greedy Algorithms ◦ Divide and Conquer Algorithms ◦ Dynamic Programming.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Topic 25 Dynamic Programming "Thus, I thought dynamic programming was a good name. It was something not even a Congressman could object to. So I used it.
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
PROBLEM-SOLVING TECHNIQUES Rocky K. C. Chang November 10, 2015.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Math Review 1.
Fundamental Data Structures and Algorithms Ananda Guna March 18, 2003 Dynamic Programming Part 1.
Dynamic Programming. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable.
6/12/2016 Prepared by Dr.Saad Alabbad1 CS100 : Discrete Structures Proof Techniques(2) Mathematical Induction & Recursion Dr.Saad Alabbad Department of.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CS212: Data Structures and Algorithms
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Dynamic Programming 26-Apr-18.
Recursion Lakshmish Ramaswamy.
CSC 172 DATA STRUCTURES.
Applied Algorithms (Lecture 17) Recursion Fall-23
Data Structures and Algorithms
CS201: Data Structures and Discrete Mathematics I
Dynamic Programming 23-Feb-19.
Recursion Taken from notes by Dr. Neil Moore
Presentation transcript:

Recursion and Dynamic Programming CSC 172 SPRING 2004 LECTURE 9

Grad TA help sessions?  What is a good time?  Office hours are kind of early  Would people like 5ish?  Induction  Big-Oh  Recurrence  Code Style review

Project 1  General Observations  Your “style” should travel with you  Go to the writing place  Some people paraphrased Bently, but got it wrong  While big-Oh analysis was not required, some notion of absraction and generalization is appropriate  Some people lost large amounts because of no disclaimer

Recursive definition WWhat is a list? AA list can be “empty” AA list can be an item, followed by a list.

Recursive definition  What is a Tree?  A tree can be “empty”  A tree can be a node with “children” that are trees.

Recursive definition  What is a number? (integer)  A single digit is a number {0,1,2,3,4,5,6,7,8,9}  A number is a digit followed by a number

Recursive Definition  What is a simple (4 func) arithmetic expression ?  (7+5)* (3-2)  A number is an arithmetic expression  If X is an ae, then (X) is an ae  If X and Y are both aes,  then X+Y, X-Y, X*Y, X/Y are all ae

Recursion  We often think of recursion as a “programming technique”  Recursion is also a design pattern for data structures  Recursion is a way to think about problem solving

A Theological Aside Disclaimer: you don’t have to believe  Can computer science inform theology?  Christian notion of “Trinity”?  "A three-fold personality existing in one divine being or substance; the union in one God of Father, Son, and Holy Spirit as three infinite, co-equal, co- eternal persons; one God in three persons."  from about 400 AD to about 1800 AD, countless people were put to death for refusing to believe in the idea of "one God in three persons."

Serpinski Triangle This design is called Sierpinski's Triangle, after the Polish mathematician Waclaw Sierpinski who described it in Among these is its fractal or self-similar character. The large blue triangle consists of three smaller blue triangles, each of which itself consists of three smaller blue triangles, each of which..., a process of subdivision which could, with adequate screen resolution, be seen to continue indefinitely.

Serpinski Triangle What is a Serpinski Triangle? A Serpinski Triangle is a triangle made up of 3 Serpinski triangles.

Weiss’s fundamental rules of recursion 1. Base case: always have at least one case that can be solved without recursion 2. Make progress: Any recursive call must progress toward the base case 3. “You gotta believe”: Always assume that the recursive call works. (In proofs “BTIH”). 4. Compound Interest rule: Never duplicate work by solving the same instance of a problem in separate recursive calls.

Example : Fibonacci Number // Bad algorithm public static long fib (int n) { if (n <= 1) return n; else return fib (n-1) + fib(n-2); }

Why is it bad?

Dynamic Programming  If you can mathematically express a problem recursively, then you can express it as a recursive algorithm.  However, sometimes, this can be inefficiently expressed by a compiler  Fibonacci numbers  To avoid this recursive “explosion” we can use dynamic programming

Example Problem: Making Change  For a currency with coins C 1,C 2,…C n (cents) what is the minimum number of coins needed to make K cents of change?  US currency has 1,5,10, and 25 cent denominations  Anyone got a 50-cent piece?  We can make 63 cents by using two quarters & 3 pennies  What if we had a 21 cent piece?

63 cents  25,25,10,1,1,1  Suppose a 21 cent coin?  21,21,21 is optimal

Recursive Solution 1. If we can make change using exactly one coin, then that is a minimum 2. Otherwise for each possible value j compute the minimum number of coins needed to make j cents in change and K – j cents in change independently. Choose the j that minimizes the sum of the two computations.

public static int makeChange (int[] coins, int change){ int minCoins = change; for (int k = 0;k<coins.length;k++) if (coins[k] == change) return 1; for (int j = 1;j<= change/2;j++) { int thisCoins = makeChange(coins,j) +makeChange(coins,change-j); if (thisCoins < minCoins) minCoins = thisCoins; } return minCoins; }// How long will this take?

How many calls? 63¢ 62¢2¢2¢ 1¢1¢ 61¢31¢32¢...

How many calls? 63¢ 3¢3¢ 1¢1¢ 4¢4¢61¢62¢... 2¢2¢

How many calls? 63¢ 1¢1¢ 3¢3¢ 1¢1¢ 4¢4¢61¢62¢... 2¢2¢ 1¢

How many calls? 63¢ 1¢1¢ 3¢3¢ 1¢1¢ 4¢4¢61¢62¢... 2¢2¢ 1¢ 3¢3¢1¢1¢4¢4¢ 61¢... 2¢2¢

How many times do you call for 2¢? 63¢ 1¢1¢ 3¢3¢ 1¢1¢ 4¢4¢61¢62¢... 2¢2¢ 1¢ 3¢3¢1¢1¢4¢4¢ 61¢... 2¢2¢

Some Solutions 1(1) & 62(21,21,10,10) 2(1,1) & 61(25,25,10,1).. 21(21) & 42(21,21) …. 31(21,10) & 32(21,10,1)

Improvements?  Limit the inner loops to the coins 1 & 21,21,10,10 5 & 25,21,10,1,1 10 & 21,21,10,1 21 & 21,21 25 & 25,10,1,1,1 Still, a recursive branching factor of 5 How many times do we solve for 52 cents?

public static int makeChange (int[] coins, int change){ int minCoins = change; for (int k = 0;k<coins.length;k++) if (coins[k] == change) return 1; for (int j = 1;j<= coins.length;j++) { if (change < coins[j]) continue; int thisCoins = 1+makeChange(coins,change-coins[j]); if (thisCoins < minCoins) minCoins = thisCoins; } return minCoins; }// How long will this take?

How many calls? 63¢ 58¢53¢62¢42¢38¢ 48¢43¢52¢32¢13¢ 57¢52¢61¢41¢37¢

Tabulation aka Dynamic Programming  Build a table of partial results.  The trick is to save answers to the sub-problems in an array.  Use the stored sub-solutions to solve the larger problems

DP for change making  Find optimum solution for 1 cent  Find optimum solution for 2 cents using previous  Find optimum solution for 3 cents using previous  …etc.  At any amount a, for each denomination d, check the minimum coins for the (previously calculated) amount a-d  We can always get from a-d to a with one more coin

public static int makeChange (int[] coins, int differentcoins, int maxChange, int[] coinsUsed, int [] lastCoin){ coinsUsed[0] = 0; lastCoin[0]=1; for (int cents = 1; cents <= maxChange; cents++) { int minCoins = cents; int newCoin = 1; for (int j = 0;j<differentCoins;j++) { if (coins[j] > cents) continue; if (coinsUsed[cents – coins[j]]+1 < minCoins){ minCoins=coinsUsed[cents – coins[j]]+1; newCoin = coins[j]; } coinsUsed[cents] = minCoins; lastCoin[cents] = newCoin; }

Dynamic Programming solution  O(NK)  N denominations  K amount of change  By backtracking through the lastCoin[] array, we can generate the sequence needed for the amount in question.