Dynamic Programming and Perl Arrays Ellen Walker Bioinformatics Hiram College.

Slides:



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

Introduction to Algorithms 6.046J/18.401J/SMA5503
Dynamic Programming.
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
DYNAMIC PROGRAMMING ALGORITHMS VINAY ABHISHEK MANCHIRAJU.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Analysis of Algorithms Dynamic Programming. A dynamic programming algorithm solves every sub problem just once and then Saves its answer in a table (array),
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
15-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
16-May-15 Dynamic Programming. 2 Algorithm types Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
1 Dynamic Programming Jose Rolim University of Geneva.
Dynamic Programming Lets begin by looking at the Fibonacci sequence.
1 Longest Common Subsequence (LCS) Problem: Given sequences x[1..m] and y[1..n], find a longest common subsequence of both. Example: x=ABCBDAB and y=BDCABA,
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CPSC 311, Fall 2009: Dynamic Programming 1 CPSC 311 Analysis of Algorithms Dynamic Programming Prof. Jennifer Welch Fall 2009.
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
Introduction to Bioinformatics Algorithms Dynamic Programming: Edit Distance.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Dynamic Programming Code
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
for($i=0; $i/)
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
Minimum Spanning Network: Brute Force Solution
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
© 2004 Goodrich, Tamassia Dynamic Programming1. © 2004 Goodrich, Tamassia Dynamic Programming2 Matrix Chain-Products (not in book) Dynamic Programming.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
11-1 Matrix-chain Multiplication Suppose we have a sequence or chain A 1, A 2, …, A n of n matrices to be multiplied –That is, we want to compute the product.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
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.
1 Dynamic Programming 2012/11/20. P.2 Dynamic Programming (DP) Dynamic programming Dynamic programming is typically applied to optimization problems.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
CS 312: Algorithm Design & Analysis Lecture #23: Making Optimal Change with Dynamic Programming Slides by: Eric Ringger, with contributions from Mike Jones,
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Sequence Comparison Algorithms Ellen Walker Bioinformatics Hiram College.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Introduction to Bioinformatics Algorithms Divide & Conquer Algorithms.
1 Dynamic Programming Topic 07 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Dynamic Programming. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable.
Dynamic Programming Typically applied to optimization problems
Dynamic Programming 26-Apr-18.
Design & Analysis of Algorithm Dynamic Programming
Advanced Algorithms Analysis and Design
Advanced Design and Analysis Techniques
CS200: Algorithm Analysis
CSCE 411 Design and Analysis of Algorithms
Dynamic Programming.
Dynamic Programming.
Data Structures and Algorithms
Iteration: Beyond the Basic PERFORM
Dynamic Programming 23-Feb-19.
Trevor Brown DC 2338, Office hour M3-4pm
The structure of programming
Introduction to Algorithms: Dynamic Programming
Longest Common Subsequence
The structure of programming
Analysis of Algorithms CS 477/677
Longest Common Subsequence
Thinking procedurally
Analysis of Algorithms CS 477/677
Presentation transcript:

Dynamic Programming and Perl Arrays Ellen Walker Bioinformatics Hiram College

The Change Problem Given a set of coins Find the minimum number of coins to make a given amount

Brute Force (Recursive) Solution Find_change (amt){ if ((amt) < 0) return infinity else if (amt = 0) return 0 else best = infinity For each coin in the set result = find_change(amt - coin) if (result < best) best = result return (best+1) }

To write this in Perl We need to create an array of coins = (1, 5, 10, 20, 25); To access a coin: –$coin = $coins[2]; # gets 10 To loop through all coins: –foreach my $coin –Inside the loop, $coin is first $coins[0], then $coins[1], etc.

Brute Force Coins in Perl (main) = ( 1, 5, 10, 20, 25 ); print brute_change( 99 ); print "\n";

Subroutine to compute change (Part 1) sub brute_change { my $amt = if ($amt == 0){ return 0; } if ($amt < 0){ return ; }

Subroutine to compute change (Part 2) my $best = ; foreach my $coin my $count = brute_change($amt - $coin); print "coin is $coin, count is $count \n"; if ($best > $count) { $best = $count; } return $best+1; }

Why It Takes So Long For each value, we repeatedly compute the same result numerous times. For 99: 74, 79, 89, 94, 98 For 98: 73, 78, 88, 93, 97 For 97: 72, 76, 87, 92, 96 For 96: 71, 75, 86, 91, 95 For 95: 70, 74, 85, 82, 94

Save Time by Expending Space Instead of calling the function recursively as we need it… –Compute each amount from 1 to 99 –Save each result as we need it –Instead of doing a recursive computation, look up the value in the array of results This is called dynamic programming

Why it Works? By the order we’re computing in, we know that we have every value less than $amt covered when we get to $amt. We never need a value larger than $amt, so all the values we need are in the table.

Dynamic Programming in General Consider an impractical recursive solution Note which results are needed to compute a new result Reorder the computions so the needed ones are done first Save all results and look them up in the table instead of doing a recursive computation

The Bottom Line for Making Change Brute force solution considers every sequence. Since longest sequence is amt, this is O(coins amt ) Dynamic programming solution considers every value from 1 to amt, and for each amt, does a check for each coin. This is O(coins*amt)