SlideShare a Scribd company logo
CHAPTER 8 Dynamic Programming
Algorithm 8.1.1 Computing the Fibonacci Numbers, Version 1 This dynamic-programming algorithm computes the Fibonacci number  f [ n ]. It uses the formulas f [1] = 1;  f [2] = 1;  f [ n ] =  f [ n  - 1] +  f [ n  - 2],  n   ≥  3. At the conclusion of the algorithm, the array  f  holds the first  n  Fibonacci numbers. Input Parameters:  n Output Parameters: None fibonacci1 ( n ) { //  f  is a local array f [1] = 1 f [2] = 1 for  i  = 3 to  n f [ i ] =  f [ i  - 1] +  f [ i  - 2] return  f [ n ] }
Algorithm 8.1.1 Computing the Fibonacci Numbers, Version 2 This dynamic-programming algorithm computes the Fibonacci number  f n  . It uses the formulas  f 1  = 1;  f 2  = 1;  f n  =  f n  - 1  +  f n  - 2 ,  n   ≥  3. It saves the two preceding Fibonacci numbers in the variables  f_twoback  and  f_oneback   in order to compute the next Fibonacci number. Input Parameters:  n Output Parameters: None fibonacci2 ( n ) { if ( n  == 1 ||  n  == 2) return 1 f_twoback  = 1 f_oneback  = 1 for  i  = 3 to  n  { f  =  f_twoback  +  f_oneback f_twoback  =  f_oneback f_oneback  =  f } return  f }
Computing the Fibonacci Numbers Recursively This algorithm computes the Fibonacci number  f n  recursively using the formulas  f 1  = 1;  f 2  = 1;  f n  =  f n  - 1  +  f n  - 2 ,  n   ≥  3. Input Parameters:  n Output Parameters: None fibonacci_recurs ( n ) { if ( n  == 1) return 1 if ( n  == 2) return 1 return  fibonacci_recurs ( n  - 2) +  fibonacci_recurs ( n  - 1) }
Computing the Fibonacci Numbers with Memoization This algorithm computes the Fibonacci number  f n  recursively (with memoization) using the formulas  f 1  = 1;  f 2  = 1;  f n  =  f n  - 1  +  f n  - 2 ,  n   ≥  3.
Input Parameters:  n Output Parameters: None memoized_fibonacci ( n ) { for  i  = 1 to  n results [ i ] = -1 // -1 means undefined return  memoized_fibonacci_recurs ( results , n ) } memoized_fibonacci_recurs ( results , n ) { if ( results [ n ] != -1) return  results [ n ] if ( n  == 1) val  = 1 else if ( n  == 2) val  = 1 else { val  =  memoized_fibonacci_recurs ( results , n  - 2) val  =  val  +  memoized_fibonacci_recurs ( results , n  - 1) } results [ n ] =  val return  val }
Algorithm 8.2.1 Coin Changing Using Dynamic Programming, Version 1 This dynamic-programming algorithm computes the minimum number of coins to make change for a given amount. The input is an array  denom  that specifies the denominations of the coins, denom [1] >  denom [2] > ··· >  denom [ n ] = 1, and an amount  A . The output is an array  C  whose value,  C [ i ][ j ], is the minimum number of coins to make change for the amount  j , using coins  i through  n , 1  ≤  i  ≤  n, 0  ≤  j  ≤  A.
Input Parameters:  denoma , A Output Parameters: None dynamic_coin_change1 ( denom , A , C ) { n  =  denom . last for  j  = 0 to  A C [ n ][ j ] =  j for  i  =  n  - 1 downto 1 for  j  = 0 to  A if ( denom [ i ] >  j  ||  C [ i  + 1][ j ] < 1 +  C [ i ][ j  -  denom [ i ]]) C [ i ][ j ] =  C [ i  + 1][ j ] else C [ i ][ j ] = 1 +  C [ i ][ j  -  denom [ i ]] }
Algorithm 8.2.2 Coin Changing Using Dynamic Programming, Version 2 This dynamic-programming algorithm computes the minimum number of coins to make change for a given amount and tracks which coins are used. The input is an array  denom  that specifies the denominations of the coins, denom [1] >  denom [2] > ··· >  denom [ n ] = 1, and an amount  A . The output consists of arrays  C  and  used . The value,  C [ i ][ j ], is the minimum number of coins to make change for the amount  j , using coins  I  through  n , 1  ≤  i  ≤  n, 0  ≤  j  ≤  A. The value,  used [ i ][ j ], is true or false to signify whether coin  i  appears in the smallest set of coins computed by Algorithm 8.2.1 for the amount  j  using only coins  i  through  n . The values of  i  and  j  satisfy 1  ≤  i  ≤  n, 0  ≤  j  ≤  A.
Input Parameters:  denom , A Output Parameters:  C , used dynamic_coin_change2 ( denom , A , C , used ) { n  =  denom . last for  j  = 0 to  A  { C [ n ][ j ] =  j used [ n ][ j ] = true } for  i  =  n  - 1 downto 1 for  j  = 0 to  A if ( denom [ i ] >  j  ||  C [ i  + 1][ j ] < 1 +  C [ i ][ j  -  denom [ i ]]) C [ i ][ j ] =  C [ i  + 1][ j ] used[ i ][ j ] = false else C [ i ][ j ] = 1 +  C [ i ][ j  -  denom [ i ]] used[ i ][ j ] = true } }
Algorithm 8.2.4 Computing a Minimum- Size Set of Coins for a Given Amount This algorithm outputs a minimum-size set of coins to make change for an amount  j  using any of coins  i  through  n  with denominations specified by Algorithm 8.2.2. The algorithm inputs the index  i , the amount  j , the array  denom  of Algorithm 8.2.2, and the array  used  computed by Algorithm 8.2.2.
Input Parameters:  i , j , denom , used Output Parameters: None optimal_coins_set ( i , j , denom , used ) { if ( j  == 0) return if ( used [ i ][ j ]) { println (“Use a coin of denomination ” +  denom [ i ]) optimal_coins_set ( i , j  -  denom [ i ], denom , used ) } else optimal_coins_set ( i  + 1, j , denom , used ) }
Algorithm 8.3.1 Optimal Matrix Multiplication This algorithm computes the minimum number of scalar multiplications to multiply a sequence of  n  matrices. The input is the array  size  that contains the sizes of the matrices to be multiplied. The first matrix is  size [0] ×  size [1]; the second is  size [1] ×  size [2]; and so on. The  n th matrix is  size [ n  - 1] ×  size [ n ]. The output is the array  s  whose value,  s [ i ][ j ], is the minimum number of scalar multiplications to multiply matrices  i  through  j . The value  ∞  is the largest available integer value.
Input Parameters:  size Output Parameters:  s opt_matrix_mult ( size , s ) { n  =  size . last for  i  = 1 to  n s [ i ][ i ] = 0 //  w  =  j  -  i for  w  = 1 to  n  - 1 for  i  = 1 to  n  -  w  { j  =  w  +  i s [ i ][ j ] = ∞ for  k  =  i  to  j  - 1 { q  =  s [ i ][ k ] +  s [ k  + 1][ j ] +  size [ i  - 1] *  size [ k ] *  size [ j ] if ( q  <  s [ i ][ j ]) s [ i ][ j ] =  q } } }
Algorithm 8.4.2 Computing the Length of a Longest Common Subsequence This dynamic-programming algorithm computes the length  c [ i ][ j ] of a longest common subsequence of  a [1], ... , a[ i ]  and  b [1], ... ,  b [ j ]  for  i  = 0, ... ,  m  and  j  = 0, ... ,  n . Input Parameters:  a , b Output Parameters:  c LCS ( a , b , c ) { m  =  a . last n  =  b . last for  i  = 0 to  m c [ i ][0] = 0 for  j  = 1 to  n c[0][ j ] = 0 for  i  = 1 to  m for  j  = 1 to  n if ( a [ i ] !=  b [ j ]) c [ i ][ j ] =  max ( c [ i  - 1][ j ], c [ i ][ j  - 1]) else c [ i ][ j ] = 1 +  c [ i  - 1][ j  - 1] }
Algorithm 8.4.3 Computing a Longest Common Subsequence This algorithm uses the array  c  computed by Algorithm 8.4.2 to output a longest common subsequence. The array  a  of length  m  is the first sequence input to Algorithm 8.4.2, and  n  is the length of the second sequence input to Algorithm 8.4.2. Input Parameters:  a , m  (length of  a ), n  (length of second sequence), c  (contains lengths of longest common subsequences) Output Parameters: None LCS_print ( a , m , n , c ) { if ( c [ m ][ n ] == 0) return if ( c [ m ][ n ] ==  c [ m  - 1][ n ]) LCS_print ( a , m  - 1, n , c ) else if ( c [ m ][ n ] ==  c [ m ][ n  - 1]) LCS_print ( a , m , n  - 1, c ) else { LCS_print ( a , m  - 1, n  - 1, c ) print ( a [ m ]) } }
Algorithm 8.5.3 Floyd’s Algorithm, Version 1 This algorithm computes the length of a shortest path between each pair of vertices in a simple, undirected, weighted graph  G . All weights are nonnegative. The input is the adjacency matrix  A  of  G . The output is the matrix  A  whose  ij th entry is the length of a shortest path from vertex  i  to vertex  j . Input Parameter:  A Output Parameter:  A all_paths ( A ) { n  =  A . last for  k  = 1 to  n  // compute  A ( k ) for  i  = 1 to  n for  j  = 1 to  n if ( A [ i ][ k ] +  A [ k ][ j ] <  A [ i ][ j ]) A [ i ][ j ] =  A [ i ][ k ] +  A [ k ][ j ] }
Algorithm 8.5.4 Floyd’s Algorithm, Version 2 This algorithm computes the length of a shortest path between each pair of vertices in a simple, undirected, weighted graph  G  and stores the vertex that follows the first vertex on each shortest path. All weights are nonnegative. The input is the adjacency matrix  A  of  G . The output is the matrix  A  whose  ij th entry is the length of a shortest path from vertex  i  to vertex  j  and the matrix  next  whose  ij th entry is the vertex that follows  i  on a shortest path from  i  to  j .
Input Parameter:  A Output Parameter:  A , next all_paths ( A , next ) { n  =  A . last // initialize next: if no intermediate // vertices are allowed  next [ i ][ j ] =  j for  i  = 1 to  n for  j  = 1 to  n next [ i ][ j ] =  j for  k  = 1 to  n  // compute  A ( k ) for  i  = 1 to  n for  j  = 1 to  n if ( A [ i ][ k ] +  A [ k ][ j ] <  A [ i ][ j ]) { A [ i ][ j ] =  A [ i ][ k ] +  A [ k ][ j ] next [ i ][ j ] =  next [ i ][ k ] } }
Algorithm 8.5.5 Finding a Shortest Path This algorithm outputs a shortest path from vertex  i  to vertex  j  in a simple, undirected, weighted graph  G . It assumes that matrix  next  has already been computed by Algorithm 8.5.4. Input Parameters:  next , i , j Output Parameters: None print_path ( next , i , j ) { // if no intermediate vertices, just  // print  i  and  j  and return if ( j  ==  next [ i ][ j ]) { print ( i  + “ ” +  j ) return } // output  i  and then the path from the vertex // after  i  ( next [ i ][ j ]) to  j print ( i  + “ ”) print_path ( next , next [ i ][ j ], j ) }
Algorithm 8.5.12 Warshall’s Algorithm This algorithm computes the transitive closure of a relation  R  on {1, ... ,  n }. The input is the matrix  A  of  R . The output is the matrix  A  of the transitive closure of  R . Input Parameters:  A Output Parameters:  A transitive_closure ( A ) { n  =  A . last for  k  = 1 to  n   for  i  = 1 to  n for  j  = 1 to  n A [ i ][ j ] =  A [ i ][ j ]    ( A [ i ][ k ]     A [ k ][ j ]) }

More Related Content

What's hot (20)

PPT
Dynamicpgmming
Muhammad Wasif
 
PPTX
Dynamic programming Basics
Kvishnu Dahatonde
 
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
PPT
Data Structure Insertion sort
Mahesh Dheravath
 
PPT
Alg1
luzenith_g
 
PDF
Cg lab cse-v (1) (1)
Surya Sukumaran
 
PDF
Dynamic programming
Jay Nagar
 
PPT
Introducción al Análisis y diseño de algoritmos
luzenith_g
 
PPTX
Greedy Algorithms
Amrinder Arora
 
DOC
algorithm Unit 5
Monika Choudhery
 
PDF
Computer graphics lab report with code in cpp
Alamgir Hossain
 
DOC
Computer graphics
Prianka Padmanaban
 
DOCX
Computer graphics
shafiq sangi
 
PPTX
Dynamic programming1
debolina13
 
PDF
Computer graphics practical(jainam)
JAINAM KAPADIYA
 
DOC
algorithm Unit 3
Monika Choudhery
 
PPT
Engineering ppt by venay magen
venaymagen19
 
PDF
Circular convolution Using DFT Matlab Code
Bharti Airtel Ltd.
 
DOC
algorithm Unit 4
Monika Choudhery
 
PDF
Computer graphics 2
Prabin Gautam
 
Dynamicpgmming
Muhammad Wasif
 
Dynamic programming Basics
Kvishnu Dahatonde
 
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
Data Structure Insertion sort
Mahesh Dheravath
 
Cg lab cse-v (1) (1)
Surya Sukumaran
 
Dynamic programming
Jay Nagar
 
Introducción al Análisis y diseño de algoritmos
luzenith_g
 
Greedy Algorithms
Amrinder Arora
 
algorithm Unit 5
Monika Choudhery
 
Computer graphics lab report with code in cpp
Alamgir Hossain
 
Computer graphics
Prianka Padmanaban
 
Computer graphics
shafiq sangi
 
Dynamic programming1
debolina13
 
Computer graphics practical(jainam)
JAINAM KAPADIYA
 
algorithm Unit 3
Monika Choudhery
 
Engineering ppt by venay magen
venaymagen19
 
Circular convolution Using DFT Matlab Code
Bharti Airtel Ltd.
 
algorithm Unit 4
Monika Choudhery
 
Computer graphics 2
Prabin Gautam
 

Viewers also liked (20)

PDF
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
Wei Xue
 
PPT
Dynamic programming in Algorithm Analysis
Rajendran
 
PPTX
Class warshal2
Debarati Das
 
PPT
35 algorithm-types
Kislay Bhardwaj L|PT,ECSA,C|EH
 
PPTX
Dynamic programming
Melaku Bayih Demessie
 
PPT
Introduction to-sql
BG Java EE Course
 
PPTX
Dynamic programming
Maher Alshammari
 
PPTX
Dynamic programming
Yıldırım Tam
 
PDF
Solving The Shortest Path Tour Problem
Nozir Shokirov
 
PPT
21 backtracking
Aparup Behera
 
PDF
DP
Subba Oota
 
PPT
5.5 back track
Krish_ver2
 
PPTX
Subset sum problem Dynamic and Brute Force Approch
Ijlal Ijlal
 
PPT
Covering (Rules-based) Algorithm
ZHAO Sam
 
PPT
Backtracking
Vikas Sharma
 
PPT
Lect6 csp
trivedidr
 
PPTX
Backtracking
Sally Salem
 
PPTX
Queue- 8 Queen
Ha Ninh
 
PPT
01 knapsack using backtracking
mandlapure
 
PPTX
Back tracking and branch and bound class 20
Kumar
 
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
Wei Xue
 
Dynamic programming in Algorithm Analysis
Rajendran
 
Class warshal2
Debarati Das
 
35 algorithm-types
Kislay Bhardwaj L|PT,ECSA,C|EH
 
Dynamic programming
Melaku Bayih Demessie
 
Introduction to-sql
BG Java EE Course
 
Dynamic programming
Maher Alshammari
 
Dynamic programming
Yıldırım Tam
 
Solving The Shortest Path Tour Problem
Nozir Shokirov
 
21 backtracking
Aparup Behera
 
5.5 back track
Krish_ver2
 
Subset sum problem Dynamic and Brute Force Approch
Ijlal Ijlal
 
Covering (Rules-based) Algorithm
ZHAO Sam
 
Backtracking
Vikas Sharma
 
Lect6 csp
trivedidr
 
Backtracking
Sally Salem
 
Queue- 8 Queen
Ha Ninh
 
01 knapsack using backtracking
mandlapure
 
Back tracking and branch and bound class 20
Kumar
 
Ad

Similar to Chap08alg (20)

PPT
Chap12alg
Munhchimeg
 
PPT
Chap12alg
Munkhchimeg
 
PPT
Chap05alg
Munkhchimeg
 
PPT
Chap05alg
Munhchimeg
 
PPT
Chap06alg
Munhchimeg
 
PPT
Chap06alg
Munkhchimeg
 
PPT
Asymptotic Notation
Lovely Professional University
 
PDF
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
PPTX
01 - DAA - PPT.pptx
KokilaK25
 
PPTX
3 analysis.gtm
Natarajan Angappan
 
PDF
Annotations.pdf
GauravKumar295392
 
PDF
Scientific Computing II Numerical Tools & Algorithms - CEI40 - AGA
Ahmed Gamal Abdel Gawad
 
PPT
Cgo2007 P3 3 Birkbeck
aiQUANT
 
PPT
A Dimension Abstraction Approach to Vectorization in Matlab
aiQUANT
 
PDF
Digital signal Processing all matlab code with Lab report
Alamgir Hossain
 
PDF
Fundamentals of Communication Systems 1st Edition Proakis Solutions Manual
nyzamdagion57
 
PPTX
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
PPT
Slide2
Thiti Sununta
 
PPTX
Intro to super. advance algorithm..pptx
ManishBaranwal10
 
PPTX
Study on Fundamentals of Raster Scan Graphics
Dr. Chandrakant Divate
 
Chap12alg
Munhchimeg
 
Chap12alg
Munkhchimeg
 
Chap05alg
Munkhchimeg
 
Chap05alg
Munhchimeg
 
Chap06alg
Munhchimeg
 
Chap06alg
Munkhchimeg
 
Asymptotic Notation
Lovely Professional University
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
01 - DAA - PPT.pptx
KokilaK25
 
3 analysis.gtm
Natarajan Angappan
 
Annotations.pdf
GauravKumar295392
 
Scientific Computing II Numerical Tools & Algorithms - CEI40 - AGA
Ahmed Gamal Abdel Gawad
 
Cgo2007 P3 3 Birkbeck
aiQUANT
 
A Dimension Abstraction Approach to Vectorization in Matlab
aiQUANT
 
Digital signal Processing all matlab code with Lab report
Alamgir Hossain
 
Fundamentals of Communication Systems 1st Edition Proakis Solutions Manual
nyzamdagion57
 
Algorithms - "Chapter 2 getting started"
Ra'Fat Al-Msie'deen
 
Intro to super. advance algorithm..pptx
ManishBaranwal10
 
Study on Fundamentals of Raster Scan Graphics
Dr. Chandrakant Divate
 
Ad

More from Munkhchimeg (20)

DOC
Protsesor
Munkhchimeg
 
PPT
Lecture916
Munkhchimeg
 
PPT
Lecture915
Munkhchimeg
 
PPT
Lecture914
Munkhchimeg
 
PPT
Lecture913
Munkhchimeg
 
PPT
Lecture911
Munkhchimeg
 
PPT
Lecture912
Munkhchimeg
 
PPT
Lecture910
Munkhchimeg
 
PPT
Lecture5
Munkhchimeg
 
PPT
Lecture9
Munkhchimeg
 
PPT
Lecture8
Munkhchimeg
 
PPT
Lecture7
Munkhchimeg
 
PPT
Lecture6
Munkhchimeg
 
PPT
Lecture4
Munkhchimeg
 
PPT
Lecture3
Munkhchimeg
 
PPT
Ded Algorithm
Munkhchimeg
 
PPT
Ded Algorithm1
Munkhchimeg
 
PPT
Tobch Lecture
Munkhchimeg
 
PPT
Lecture914
Munkhchimeg
 
PPT
Tobch Lecture
Munkhchimeg
 
Protsesor
Munkhchimeg
 
Lecture916
Munkhchimeg
 
Lecture915
Munkhchimeg
 
Lecture914
Munkhchimeg
 
Lecture913
Munkhchimeg
 
Lecture911
Munkhchimeg
 
Lecture912
Munkhchimeg
 
Lecture910
Munkhchimeg
 
Lecture5
Munkhchimeg
 
Lecture9
Munkhchimeg
 
Lecture8
Munkhchimeg
 
Lecture7
Munkhchimeg
 
Lecture6
Munkhchimeg
 
Lecture4
Munkhchimeg
 
Lecture3
Munkhchimeg
 
Ded Algorithm
Munkhchimeg
 
Ded Algorithm1
Munkhchimeg
 
Tobch Lecture
Munkhchimeg
 
Lecture914
Munkhchimeg
 
Tobch Lecture
Munkhchimeg
 

Recently uploaded (20)

PDF
Chennai Female Emcee Kani Profile Managed by Chennai Event Emcees and Enterta...
Chennai Event Emcees
 
PPTX
TQM Report.pptx How to Improve your self
ellengracedecir10099
 
PPTX
wavegamhjhvhjghjfyufffghfghing_finaldeck.pptx
sampleacc835
 
PDF
Unlock powerful secrets to attract Instagram followers now.pdf
Recent Follow
 
PDF
Mercury in Vedic Astrology: Mind & Communication
astrologystartss
 
PPTX
电子版本制作约克圣约翰大学毕业证学历认证学位证展示学历学位证制作
6b9ab940
 
PDF
Old Gmail vs New Gmail: Which Performs s
usawbrank
 
PPTX
Blackboard Anua-WPS skrlys;kdlxilkd.tkwiouelgjttwb Office.pptx
RisfaFadilahBerliana
 
PDF
Wondla_204_55_version11_Editedforfolio_.pdf
Giovanny Cardenas
 
PDF
Chennai Female Emcee Mohana Ravi Profile Managed by Chennai Event Emcees and ...
Chennai Event Emcees
 
PPTX
Problem-Solving-Involving-Sequences.pptx
dominicdaltoncaling2
 
PDF
Chennai Female Emcee Vaishali Profile Managed by Chennai Event Emcees and Ent...
Chennai Event Emcees
 
PPTX
PPP;O'P;IYHFDSDFGYUIKHTLIUYHGDFGHJILUYGT.pptx
dilludcruz
 
PDF
-----Rk Narayan's THE GUIDE.ppt.pdf-----
jayparmar101626
 
PPTX
EXP 401- On farm Advisory for Soil Health, Water Quality and Plant Nutrition....
AbedhSabu
 
PDF
Breaking into Television Writing: A Modern Guide for Aspiring Creators
All Writers Destination
 
PDF
Wedding Planners in India | Weddings & !
Weddingsandeventsbyamitesh
 
PDF
Regarding honorarium for the year 2025-26 human resources(XV-FC) approved und...
khankhan307705
 
PPTX
一比一还原日本工业大学毕业证/NIT毕业证书2025原版定制成绩单
e7nw4o4
 
PDF
TAIPANQQ SITUS MUDAH MENANG DAN MUDAH MAXWIN SEGERA DAFTAR DI TAIPANQQ DAN RA...
TAIPANQQ GACOR
 
Chennai Female Emcee Kani Profile Managed by Chennai Event Emcees and Enterta...
Chennai Event Emcees
 
TQM Report.pptx How to Improve your self
ellengracedecir10099
 
wavegamhjhvhjghjfyufffghfghing_finaldeck.pptx
sampleacc835
 
Unlock powerful secrets to attract Instagram followers now.pdf
Recent Follow
 
Mercury in Vedic Astrology: Mind & Communication
astrologystartss
 
电子版本制作约克圣约翰大学毕业证学历认证学位证展示学历学位证制作
6b9ab940
 
Old Gmail vs New Gmail: Which Performs s
usawbrank
 
Blackboard Anua-WPS skrlys;kdlxilkd.tkwiouelgjttwb Office.pptx
RisfaFadilahBerliana
 
Wondla_204_55_version11_Editedforfolio_.pdf
Giovanny Cardenas
 
Chennai Female Emcee Mohana Ravi Profile Managed by Chennai Event Emcees and ...
Chennai Event Emcees
 
Problem-Solving-Involving-Sequences.pptx
dominicdaltoncaling2
 
Chennai Female Emcee Vaishali Profile Managed by Chennai Event Emcees and Ent...
Chennai Event Emcees
 
PPP;O'P;IYHFDSDFGYUIKHTLIUYHGDFGHJILUYGT.pptx
dilludcruz
 
-----Rk Narayan's THE GUIDE.ppt.pdf-----
jayparmar101626
 
EXP 401- On farm Advisory for Soil Health, Water Quality and Plant Nutrition....
AbedhSabu
 
Breaking into Television Writing: A Modern Guide for Aspiring Creators
All Writers Destination
 
Wedding Planners in India | Weddings & !
Weddingsandeventsbyamitesh
 
Regarding honorarium for the year 2025-26 human resources(XV-FC) approved und...
khankhan307705
 
一比一还原日本工业大学毕业证/NIT毕业证书2025原版定制成绩单
e7nw4o4
 
TAIPANQQ SITUS MUDAH MENANG DAN MUDAH MAXWIN SEGERA DAFTAR DI TAIPANQQ DAN RA...
TAIPANQQ GACOR
 

Chap08alg

  • 1. CHAPTER 8 Dynamic Programming
  • 2. Algorithm 8.1.1 Computing the Fibonacci Numbers, Version 1 This dynamic-programming algorithm computes the Fibonacci number f [ n ]. It uses the formulas f [1] = 1; f [2] = 1; f [ n ] = f [ n - 1] + f [ n - 2], n ≥ 3. At the conclusion of the algorithm, the array f holds the first n Fibonacci numbers. Input Parameters: n Output Parameters: None fibonacci1 ( n ) { // f is a local array f [1] = 1 f [2] = 1 for i = 3 to n f [ i ] = f [ i - 1] + f [ i - 2] return f [ n ] }
  • 3. Algorithm 8.1.1 Computing the Fibonacci Numbers, Version 2 This dynamic-programming algorithm computes the Fibonacci number f n . It uses the formulas f 1 = 1; f 2 = 1; f n = f n - 1 + f n - 2 , n ≥ 3. It saves the two preceding Fibonacci numbers in the variables f_twoback and f_oneback in order to compute the next Fibonacci number. Input Parameters: n Output Parameters: None fibonacci2 ( n ) { if ( n == 1 || n == 2) return 1 f_twoback = 1 f_oneback = 1 for i = 3 to n { f = f_twoback + f_oneback f_twoback = f_oneback f_oneback = f } return f }
  • 4. Computing the Fibonacci Numbers Recursively This algorithm computes the Fibonacci number f n recursively using the formulas f 1 = 1; f 2 = 1; f n = f n - 1 + f n - 2 , n ≥ 3. Input Parameters: n Output Parameters: None fibonacci_recurs ( n ) { if ( n == 1) return 1 if ( n == 2) return 1 return fibonacci_recurs ( n - 2) + fibonacci_recurs ( n - 1) }
  • 5. Computing the Fibonacci Numbers with Memoization This algorithm computes the Fibonacci number f n recursively (with memoization) using the formulas f 1 = 1; f 2 = 1; f n = f n - 1 + f n - 2 , n ≥ 3.
  • 6. Input Parameters: n Output Parameters: None memoized_fibonacci ( n ) { for i = 1 to n results [ i ] = -1 // -1 means undefined return memoized_fibonacci_recurs ( results , n ) } memoized_fibonacci_recurs ( results , n ) { if ( results [ n ] != -1) return results [ n ] if ( n == 1) val = 1 else if ( n == 2) val = 1 else { val = memoized_fibonacci_recurs ( results , n - 2) val = val + memoized_fibonacci_recurs ( results , n - 1) } results [ n ] = val return val }
  • 7. Algorithm 8.2.1 Coin Changing Using Dynamic Programming, Version 1 This dynamic-programming algorithm computes the minimum number of coins to make change for a given amount. The input is an array denom that specifies the denominations of the coins, denom [1] > denom [2] > ··· > denom [ n ] = 1, and an amount A . The output is an array C whose value, C [ i ][ j ], is the minimum number of coins to make change for the amount j , using coins i through n , 1 ≤ i ≤ n, 0 ≤ j ≤ A.
  • 8. Input Parameters: denoma , A Output Parameters: None dynamic_coin_change1 ( denom , A , C ) { n = denom . last for j = 0 to A C [ n ][ j ] = j for i = n - 1 downto 1 for j = 0 to A if ( denom [ i ] > j || C [ i + 1][ j ] < 1 + C [ i ][ j - denom [ i ]]) C [ i ][ j ] = C [ i + 1][ j ] else C [ i ][ j ] = 1 + C [ i ][ j - denom [ i ]] }
  • 9. Algorithm 8.2.2 Coin Changing Using Dynamic Programming, Version 2 This dynamic-programming algorithm computes the minimum number of coins to make change for a given amount and tracks which coins are used. The input is an array denom that specifies the denominations of the coins, denom [1] > denom [2] > ··· > denom [ n ] = 1, and an amount A . The output consists of arrays C and used . The value, C [ i ][ j ], is the minimum number of coins to make change for the amount j , using coins I through n , 1 ≤ i ≤ n, 0 ≤ j ≤ A. The value, used [ i ][ j ], is true or false to signify whether coin i appears in the smallest set of coins computed by Algorithm 8.2.1 for the amount j using only coins i through n . The values of i and j satisfy 1 ≤ i ≤ n, 0 ≤ j ≤ A.
  • 10. Input Parameters: denom , A Output Parameters: C , used dynamic_coin_change2 ( denom , A , C , used ) { n = denom . last for j = 0 to A { C [ n ][ j ] = j used [ n ][ j ] = true } for i = n - 1 downto 1 for j = 0 to A if ( denom [ i ] > j || C [ i + 1][ j ] < 1 + C [ i ][ j - denom [ i ]]) C [ i ][ j ] = C [ i + 1][ j ] used[ i ][ j ] = false else C [ i ][ j ] = 1 + C [ i ][ j - denom [ i ]] used[ i ][ j ] = true } }
  • 11. Algorithm 8.2.4 Computing a Minimum- Size Set of Coins for a Given Amount This algorithm outputs a minimum-size set of coins to make change for an amount j using any of coins i through n with denominations specified by Algorithm 8.2.2. The algorithm inputs the index i , the amount j , the array denom of Algorithm 8.2.2, and the array used computed by Algorithm 8.2.2.
  • 12. Input Parameters: i , j , denom , used Output Parameters: None optimal_coins_set ( i , j , denom , used ) { if ( j == 0) return if ( used [ i ][ j ]) { println (“Use a coin of denomination ” + denom [ i ]) optimal_coins_set ( i , j - denom [ i ], denom , used ) } else optimal_coins_set ( i + 1, j , denom , used ) }
  • 13. Algorithm 8.3.1 Optimal Matrix Multiplication This algorithm computes the minimum number of scalar multiplications to multiply a sequence of n matrices. The input is the array size that contains the sizes of the matrices to be multiplied. The first matrix is size [0] × size [1]; the second is size [1] × size [2]; and so on. The n th matrix is size [ n - 1] × size [ n ]. The output is the array s whose value, s [ i ][ j ], is the minimum number of scalar multiplications to multiply matrices i through j . The value ∞ is the largest available integer value.
  • 14. Input Parameters: size Output Parameters: s opt_matrix_mult ( size , s ) { n = size . last for i = 1 to n s [ i ][ i ] = 0 // w = j - i for w = 1 to n - 1 for i = 1 to n - w { j = w + i s [ i ][ j ] = ∞ for k = i to j - 1 { q = s [ i ][ k ] + s [ k + 1][ j ] + size [ i - 1] * size [ k ] * size [ j ] if ( q < s [ i ][ j ]) s [ i ][ j ] = q } } }
  • 15. Algorithm 8.4.2 Computing the Length of a Longest Common Subsequence This dynamic-programming algorithm computes the length c [ i ][ j ] of a longest common subsequence of a [1], ... , a[ i ] and b [1], ... , b [ j ] for i = 0, ... , m and j = 0, ... , n . Input Parameters: a , b Output Parameters: c LCS ( a , b , c ) { m = a . last n = b . last for i = 0 to m c [ i ][0] = 0 for j = 1 to n c[0][ j ] = 0 for i = 1 to m for j = 1 to n if ( a [ i ] != b [ j ]) c [ i ][ j ] = max ( c [ i - 1][ j ], c [ i ][ j - 1]) else c [ i ][ j ] = 1 + c [ i - 1][ j - 1] }
  • 16. Algorithm 8.4.3 Computing a Longest Common Subsequence This algorithm uses the array c computed by Algorithm 8.4.2 to output a longest common subsequence. The array a of length m is the first sequence input to Algorithm 8.4.2, and n is the length of the second sequence input to Algorithm 8.4.2. Input Parameters: a , m (length of a ), n (length of second sequence), c (contains lengths of longest common subsequences) Output Parameters: None LCS_print ( a , m , n , c ) { if ( c [ m ][ n ] == 0) return if ( c [ m ][ n ] == c [ m - 1][ n ]) LCS_print ( a , m - 1, n , c ) else if ( c [ m ][ n ] == c [ m ][ n - 1]) LCS_print ( a , m , n - 1, c ) else { LCS_print ( a , m - 1, n - 1, c ) print ( a [ m ]) } }
  • 17. Algorithm 8.5.3 Floyd’s Algorithm, Version 1 This algorithm computes the length of a shortest path between each pair of vertices in a simple, undirected, weighted graph G . All weights are nonnegative. The input is the adjacency matrix A of G . The output is the matrix A whose ij th entry is the length of a shortest path from vertex i to vertex j . Input Parameter: A Output Parameter: A all_paths ( A ) { n = A . last for k = 1 to n // compute A ( k ) for i = 1 to n for j = 1 to n if ( A [ i ][ k ] + A [ k ][ j ] < A [ i ][ j ]) A [ i ][ j ] = A [ i ][ k ] + A [ k ][ j ] }
  • 18. Algorithm 8.5.4 Floyd’s Algorithm, Version 2 This algorithm computes the length of a shortest path between each pair of vertices in a simple, undirected, weighted graph G and stores the vertex that follows the first vertex on each shortest path. All weights are nonnegative. The input is the adjacency matrix A of G . The output is the matrix A whose ij th entry is the length of a shortest path from vertex i to vertex j and the matrix next whose ij th entry is the vertex that follows i on a shortest path from i to j .
  • 19. Input Parameter: A Output Parameter: A , next all_paths ( A , next ) { n = A . last // initialize next: if no intermediate // vertices are allowed next [ i ][ j ] = j for i = 1 to n for j = 1 to n next [ i ][ j ] = j for k = 1 to n // compute A ( k ) for i = 1 to n for j = 1 to n if ( A [ i ][ k ] + A [ k ][ j ] < A [ i ][ j ]) { A [ i ][ j ] = A [ i ][ k ] + A [ k ][ j ] next [ i ][ j ] = next [ i ][ k ] } }
  • 20. Algorithm 8.5.5 Finding a Shortest Path This algorithm outputs a shortest path from vertex i to vertex j in a simple, undirected, weighted graph G . It assumes that matrix next has already been computed by Algorithm 8.5.4. Input Parameters: next , i , j Output Parameters: None print_path ( next , i , j ) { // if no intermediate vertices, just // print i and j and return if ( j == next [ i ][ j ]) { print ( i + “ ” + j ) return } // output i and then the path from the vertex // after i ( next [ i ][ j ]) to j print ( i + “ ”) print_path ( next , next [ i ][ j ], j ) }
  • 21. Algorithm 8.5.12 Warshall’s Algorithm This algorithm computes the transitive closure of a relation R on {1, ... , n }. The input is the matrix A of R . The output is the matrix A of the transitive closure of R . Input Parameters: A Output Parameters: A transitive_closure ( A ) { n = A . last for k = 1 to n for i = 1 to n for j = 1 to n A [ i ][ j ] = A [ i ][ j ]  ( A [ i ][ k ]  A [ k ][ j ]) }