§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 <= 1 ) return 1; else.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Recursion.
Algorithm Design approaches Dr. Jey Veerasamy. Petrol cost minimization problem You need to go from S to T by car, spending the minimum for petrol. 2.
Logistics HW due next week Midterm also next week on Tuesday
For(int i = 1; i
Tree Recursion Traditional Approach. Tree Recursion Consider the Fibonacci Number Sequence: Time: , 1, 1, 2, 3, 5, 8, 13, 21,... /
Dynamic Programming (DP)
Homework – Chapter 1 作業解答. Problem 1 Given the Fibonacci number as … where the next Fibonacci number will be the sum of its previous.
void count_down (int count) { for(i=count; i>1; i--) printf(" %d\t", count); } printf("A%d\n", count); if(count>1) count_down(count-1); printf("B%d\n",
Dynamic Programming From An
Fig. 4-1, p Fig. 4-2, p. 109 Fig. 4-3, p. 110.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
Fibonacci with Loop int fib1(int i) { int f1 = 1; int f2 = 1; int f3;
Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F.
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
P.464. Table 13-1, p.465 Fig. 13-1, p.466 Fig. 13-2, p.467.
P449. p450 Figure 15-1 p451 Figure 15-2 p453 Figure 15-2a p453.
Fig. 11-1, p p. 360 Fig. 11-2, p. 361 Fig. 11-3, p. 361.
Local Alignment Tutorial 2. Conditions –Division to sub-problems possible –(Optimal) Sub-problem solution usable (many times?) –“Bottom-up” approach Dynamic.
Table 6-1, p Fig. 6-1, p. 162 p. 163 Fig. 6-2, p. 164.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
1 CSE1301 Computer Programming Lecture 27 Recursion (Part 1)
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
1 times table 2 times table 3 times table 4 times table 5 times table
RECURSIVE PATTERNS WRITE A START VALUE… THEN WRITE THE PATTERN USING THE WORDS NOW AND NEXT: NEXT = NOW _________.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
Recursion Fall 2008 Dr. David A. Gaitros
Recursion.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
Recursion When to use it and when not to use it. Basics of Recursion Recursion uses a method Recursion uses a method Within that method a call is made.
Recursion, pt. 1 The Foundations. What is Recursion? Recursion is the idea of solving a problem in terms of itself. – For some problems, it may not possible.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
Dynamic Programming.
Unit 5 – Series, Sequences, and Limits Section 5.2 – Recursive Definitions Calculator Required.
Dynamic Programming. Many problem can be solved by D&C – (in fact, D&C is a very powerful approach if you generalize it since MOST problems can be solved.
Infinite Geometric Series Recursion & Special Sequences Definitions & Equations Writing & Solving Geometric Series Practice Problems.
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
Dynamic Programming (DP) By Denon. Outline Introduction Fibonacci Numbers (Review) Longest Common Subsequence (LCS) More formal view on DP Subset Sum.
Tables Learning Support
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
Questions 4) What type of algorithmic problem-solving technique (greedy, divide-and-conquer, dynamic programming)
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Recursion Function calling itself
Lecture 12.
5.13 Recursion Recursive functions Functions that call themselves
C Functions -Continue…-.
Rod cutting Decide where to cut steel rods:
Sum of natural numbers class SumOfNaturalNumbers {
Dynamic Programming 1 Neil Tang 4/20/2010
Multiplication table. x
Fibonacci Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 Definition:
Times Tables.
Intro to Recursion.
Data Structures and Algorithms
מבני נתונים ויעילות אלגוריתמים
Recursion Recursion is a math and programming tool
أنماط الإدارة المدرسية وتفويض السلطة الدكتور أشرف الصايغ
How tall is the table?.
Affine Gap Alignment - An improved global alignment
Dynamic Programming 1 Neil Tang 4/15/2008
This is not an advertisement for the profession
3 times tables.
6 times tables.
Presentation transcript:

§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 <= 1 ) return 1; else return Fib( N - 1 ) + Fib( N - 2 ); } T(N)  T(N – 1) + T(N – 2) T(N)  F(N) 1/17

§3 Dynamic ProgrammingF6 F2 F1F0 F3 F1 F2 F1F0 F2 F1F0 F3 F1F2 F1F0 F3 F1 F2 F1F0 F4 F5 Trouble-maker : The growth of redundant calculations is explosive. Solution : Record the two most recently computed values to avoid recursive calls. int Fibonacci ( int N ) { int i, Last, NextToLast, Answer; if ( N <= 1 ) return 1; Last = NextToLast = 1; /* F(0) = F(1) = 1 */ for ( i = 2; i <= N; i++ ) { Answer = Last + NextToLast; /* F(i) = F(i-1) + F(i-2) */ NextToLast = Last; Last = Answer; /* update F(i-1) and F(i-2) */ } /* end-for */ return Answer; } T(N) = O(N) 2/17

§3 Dynamic Programming 2. Ordering Matrix Multiplications 〖 Example 〗 Suppose we are to multiply 4 matrices M 1 [ 10  20 ]  M 2 [ 20  50 ]  M 3 [ 50  1 ]  M 4 [ 1  100 ]. If we multiply in the order M 1 [ 10  20 ]  ( M 2 [ 20  50 ]  ( M 3 [ 50  1 ]  M 4 [ 1  100 ] ) ) Then the computing time is 50  1   50   20  100 = 125,000 If we multiply in the order ( M 1 [ 10  20 ]  ( M 2 [ 20  50 ]  M 3 [ 50  1 ] ) )  M 4 [ 1  100 ] Then the computing time is 20  50   20   1  100 = 2,200 Problem: In which order can we compute the product of n matrices with minimal computing time? 3/17

§3 Dynamic Programming Let b n = number of different ways to compute M 1  M 2     M n. Then we have b 2 = 1, b 3 = 2, b 4 = 5,    Let M ij = M i     M j. Then M 1n = M 1     M n = M 1i  M i+1 n bnbn bibi bnibni )( 4 nn n n Ob  /* Catalan number */ Suppose we are to multiply n matrices M 1     M n where M i is an r i  1  r i matrix. Let m ij be the cost of the optimal way to compute M i     M j. Then we have the recurrence equations: There are only O( N 2 ) values of M ij. If j – i = k, then the only values M xy required to compute M ij satisfy y – x < k. 4/17

§3 Dynamic Programming /* r contains number of columns for each of the N matrices */ /* r[ 0 ] is the number of rows in matrix 1 */ /* Minimum number of multiplications is left in M[ 1 ][ N ] */ void OptMatrix( const long r[ ], int N, TwoDimArray M ) { int i, k, Left, Right; long ThisM; for( Left = 1; Left <= N; Left++ ) M[ Left ][ Left ] = 0; for( k = 1; k < N; k++ ) /* k = Right - Left */ for( Left = 1; Left <= N - k; Left++ ) { /* For each position */ Right = Left + k; M[ Left ][ Right ] = Infinity; for( L = Left; L < Right; L++ ) { ThisM = M[ Left ][ L ] + M[ L + 1 ][ Right ] + r[ Left - 1 ] * r[ L ] * r[ Right ]; if ( ThisM < M[ Left ][ Right ] ) /* Update min */ M[ Left ][ Right ] = ThisM; } /* end for-L */ } /* end for-Left */ } T(N) = O(N 3 ) To record the ordering please refer to Figure on p.388 5/17

§3 Dynamic Programming 3. Optimal Binary Search Tree —— The best for static searching (without insertion and deletion) Given N words w 1 < w 2 < …… < w N, and the probability of searching for each w i is p i. Arrange these words in a binary search tree in a way that minimize the expected total access time. 〖 Example 〗 Given the following table of probabilities: word probability doubleelseifintfloatforwhile Greedy Method if float elsefor while int double AVL Method Time = 2.43 for else doublefloat int ifwhile Time = 2.70 Optimal Tree float double else if forwhile int Time = /17

§3 Dynamic Programming T i j ::= OBST for w i, ……, w j ( i < j ) c i j ::= cost of T i j ( c i i = 0 ) r i j ::= root of T i j ( r i i = 0 ) w i j ::= weight of T i j = ( w i i = p i ) T 1N with root r 1N, weight w 1N, and cost c 1N. wkwk Lwi…wk1Lwi…wk1 R w k+1 …w j T i j c i j = ?p k + cost( L ) + cost( R )+ weight( L ) + weight( R ) = p k + c i, k  1 + c k+1, j + w i, k  1 + w k +1, j = w i j + c i, k  1 + c k +1, j T i j is optimal  r i j = k is such that 7/17

§3 Dynamic Programming word probability doubleelseifintfloatforwhile double..doublewhile..whileint..intif..iffloat..floatfor..forelse..else 0.22double0.18else0.20float0.05for0.25if0.02int0.08while double..else 0.58double else..float 0.56float float..for 0.30float for..if 0.35if if..int 0.29if int..while 0.12while double..float 1.02else else..for 0.66float float..if 0.80if for..int 0.39if if..while 0.47if double..for 1.17else else..if 1.21float float..int 0.84if for..while 0.57if double..if 1.83float else..int 1.27float float..while 1.02if double..int 1.89float else..while 1.53float double..while 2.15float double else forwhile if int T(N) = O(N 3 ) Please read on p.419 for an O( N 2 ) algorithm. 8/17

§3 Dynamic Programming 4. All-Pairs Shortest Path For all pairs of v i and v j ( i  j ), find the shortest path between. Method 2 Define D k [ i ] [ j ] = min{ length of path i  { l  k }  j } and D  1 [ i ] [ j ] = Cost [ i ] [ j ]. Then the length of the shortest path from i to j is D N  1 [ i ] [ j ]. Algorithm Start from D  1 and successively generate D 0, D 1,..., D N  1. If D k  1 is done, then either  k  the shortest path i  { l  k }  j  D k = D k  1 ; or  k  the shortest path i  { l  k }  j = { the S.P. from i to k }  {the S.P. from k to j }  D k [ i ] [ j ] = D k  1 [ i ] [ k ] + D k  1 [ k ] [ j ] Method 1 Use single-source algorithm for |V| times. T = O( |V| 3 ) – works fast on sparse graph. 9/17

§3 Dynamic Programming /* A[ ] contains the adjacency matrix with A[ i ][ i ] = 0 */ /* D[ ] contains the values of the shortest path */ /* N is the number of vertices */ /* A negative cycle exists iff D[ i ][ i ] < 0 */ void AllPairs( TwoDimArray A, TwoDimArray D, int N ) { int i, j, k; for ( i = 0; i < N; i++ ) /* Initialize D */ for( j = 0; j < N; j++ ) D[ i ][ j ] = A[ i ][ j ]; for( k = 0; k < N; k++ ) /* add one vertex k into the path */ for( i = 0; i < N; i++ ) for( j = 0; j < N; j++ ) if( D[ i ][ k ] + D[ k ][ j ] < D[ i ][ j ] ) /* Update shortest path */ D[ i ][ j ] = D[ i ][ k ] + D[ k ][ j ]; } T(N) = O(N 3 ), but faster in a dense graph. To record the paths please refer to Figure on p.393 Works if there are negative edge costs, but no negative-cost cycles. 10/17

Bonus Problem 2 Diagon Alley §3 Dynamic Programming Due: Monday, January 15 th, 2007 at 10:00pm Detailed requirements can be downloaded from Courseware Download 11/17

§5 Backtracking Algorithms A sure-fire way to find the answer to a problem is to make a list of all candidate answers, examine each, and following the examination of all or some of the candidates, declare the identified answer. Suuuure — if the list is finite and it is possible to identify the answer following the examinations. AND, if there are not too many candidates. Backtracking enables us to eliminate the explicit examination of a large subset of the candidates while still guaranteeing that the answer will be found if the algorithm is run to termination. The basic idea is that suppose we have a partial solution ( x 1,..., x i ) where each x k  S k for 1  k  i < n. First we add x i+1  S i+1 and check if ( x 1,..., x i, x i+1 ) satisfies the constrains. If the answer is “yes” we continue to add the next x, else we delete x i and backtrack to the previous partial solution ( x 1,..., x i  1 ). pruning §4 Randomized Algorithms – self-study 12/17

§5 Backtracking Algorithms Find a placement of 8 queens on an 8  8 chessboard such that no two queens attack. Two queens are said to attack iff they are in the same row, column, diagonal, or antidiagonal of the chessboard Q Q Q Q Q Q Q Q Q i ::= queen in the i-th row x i ::= the column index in which Q i is Solution = ( x 1, x 2,..., x 8 ) = ( 4, 6, 8, 2, 7, 1, 3, 5 ) Constrains:  S i = { 1,2,3,4,5,6,7,8 } for 1  i  8 This implies 8 8 candidates in the solution space.  x i  x j if i  j  ( x i  x j ) / (i  j )   1 This implies that the solution must be a permutation of 1, 2,..., 8. Thus the number of candidates in the solution space is reduced to 8!. For the problem with n queens, there are n! candidates in the solution space. 1. Eight Queens 13/17

§5 Backtracking Algorithms Method: Take the problem of 4 queens as an example Step 1: Construct a game tree x1 = 1234 x2 = x3 = x4 = ! leaves Each path from the root to a leaf defines an element of the solution space. 14/17

§5 Backtracking Algorithms x1 = 1234 x2 = x3 = x4 = Step 2: Perform a depth-first search ( post-order traversal ) to examine the paths Q QQ QQ Q Q Q Q Q QQQ Q Q ( 2, 4, 1, 3 ) Note: No tree is actually constructed. The game tree is just an abstract concept. 15/17

§5 Backtracking Algorithms 2. The Turnpike Reconstruction Problem Given N points on the x-axis with coordinates x 1 < x 2 < …< x N. Assume that x 1 = 0. There are N ( N – 1 ) / 2 distances between every pair of points. Given N ( N – 1 ) / 2 distances. Reconstruct a point set from the distances. 〖 Example 〗 Given D = { 1, 2, 2, 2, 3, 3, 3, 4, 5, 5, 5, 6, 7, 8, 10 } Step 1: N ( N – 1 ) / 2 = 15 implies N = ? 6 Step 2: x 1 = 0 and x 6 = 10 x 3 = 6x 2 = 4x 3 = 4x 4 = 6 x 3 = 5 x 4 = 7x 2 = 3 x 5 = 8x 2 = 2 Step 3: find the next largest distance and check ( 0, 3, 5, 6, 8, 10 ) Please read Figure and for details. 16/17

§5 Backtracking Algorithms 17/17 Bonus Problem 3 Review of Programming Contest Rules Due: Monday, January 15 th, 2007 at 10:00pm Detailed requirements can be downloaded from Courseware Download