





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Algorithms and pseudocode for calculating fibonacci numbers, binomial coefficients, and the length of the shortest path between nodes in a graph. The fibonacci sequence is generated using recursion and dynamic programming. Binomial coefficients are calculated using pascal's triangle and dynamic programming. The shortest path problem is solved using floyd's algorithm. The document also includes examples and java code translations.
Typology: Study notes
1 / 37
This page cannot be seen from the preview
Don't miss anything!






























With Divide and Conquer – top-down technique Divide instance into subinstances Solve subinstances Combine solutions to solve original instance May lead to several overlapping subinstances, causing an inefficient algorithm Dynamic programming - bottom-up technique Normally start with the smallest, simplest subinstances May combine subinstance solutions, obtaining answers to subinstances of increasing size Can take advantage of any overlapping subinstances o Keep track of known results in a table Printed on 2020-11-28 at 07:
Fib(0) = 0 Fib(1) = 1 n > 1 : Fib( n ) = Fib( n –1) + Fib( n –2) function Fib(n) if n < 2 then return n else return Fib ( n– 1) + Fib ( n– 2) n Fib(n) # calls # calls/Fib(n) 0 0 1 1 1 1 1 2 1 3 3 3 2 5 2. 4 3 9 3 5 5 15 3 6 8 25 3. 7 13 41 3. 8 21 67 3. 9 34 109 3. 10 55 177 3. 11 89 287 3. 12 144 465 3. 13 233 753 3. 14 377 1219 3. 15 610 1973 3. 16 987 3193 3. 17 1597 5167 3. 18 2584 8361 3. 19 4181 13529 3. 20 6765 21891 3. 21 10946 35421 3. 22 17711 57313 3. 23 28657 92735 3. 24 46368 150049 3. 25 75025 242785 3. Not surprising: nCalls( n ) = 1 + nCalls( n –1) + nCalls( n –2) Standard implementation is in fact an example of dynamic programming: combine earlier, simpler cases to generate later, more complex cases. k1=1 { will be fib(n-1) } k2=0 { will be fib(n) } for j = 1 to n k2 = k2 + k k1 = k2 – k1 {old k2} return k Printed on 2020-11-28 at 07:
Pascal’s Triangle Not necessary to fill entire table suffice to keep a vector of length k, representing current line update the vector left to right Here, the time to calculate takes a time in ( nk ) and space in ( k ) Printed on 2020-11-28 at 07: 0 1 2 3 ... k-1 k 0 1 1 1 1 2 1 2 1 : : n-1 C(n –1, k –1) C(n-1, k)
n C(n, k) n k
Alternative formulation: computing formula: !! ! n k k n k n ^ shows the symmetry: ^ n k n k n So we can use min( k , n – k ) for the calculation. Alternative formulation: for 0 < k < n : ^ 1 1 k n k n k n — see hand-out requires ( k ) (multiplications and divisions), as compared with dynamic programming requirement of ( nk ) additions. function C(n, k) ans = 1 k = min( k , n – k ) while k > 0 ans = ans * n / k n = n – 1 k = k – 1 return ans Printed on 2020-11-28 at 07:
Since person A wins any given toss with probability p , and loses with probability q
We can compute P(i, j): function P(i, j) if i = 0 then return 1 else if j = 0 then return 0 else return p P(i -1, j) + q P(i, j –1) Let T(k) be the worst case time needed to calculate P(i, j) , where k = i + j Then T(1) = c
where c and d are constants
The total number of recursive calls is therefore 2 - 2 Here, the time to calculate P(n,n) that person A will win (having not started the tossing) is Since 4T(k-2) + 2d + d, k > 2 n /(2n+1), the time to calculate P(n,n) is in O ( 4T(k-2) + 2d + d, k > 2 n ) and in
n /n ) This is not practical for large values of n i + j j 2n n 2n n
Using Pascal’s Triangle we can declare an array and fill the entries Rather than fill line by line, we work diagonal by diagonal function series(n, p) array P[0..n, 0..n]
{Fill from top left to main diagonal}
qP[k, s - k - 1] {Fill from below main diagonal to bottom right}
qP[s + k, n - k - 1] return P[n, n] Since this algorithm only had to fill an n x n array, execution time is
2 ) a storage space in (n) is sufficient
from Horowitz and Sahni, pp. 203 ff. G = ( N , E ) is a directed graph in which the nodes are partitioned into k ≥ 2 disjoint sets Vi , 1 ≤ I ≤ k. In addition, if < u , v > is an edge in E, then u Vi and v Vi +1 for some I , 1 ≤ I ≤ k. |V 1 | = |V k | = 1 The multistage graph problem is to find a minimum cost path from N 1 to Nn.
Nodes: {1..12} Stages: 5 — {1} , {2..5}, {6..8}, {9..11}, {12} Weighted Directed Edge List: (1, 2: 9) (1, 3: 7) (1, 4: 3) (1, 5: 2)
Note: extensive board work — applying dynamic programming to the above
We wish to make change for a given amount, based on currency/coinage whose values are contained in d[] (denomination). At the heart of this algorithm is the idea of finding the smallest number of currency units (coins from here on out) to make change for all amounts the specified amount. This table (c[1..n][0..amount]) is built from the smallest coin — presumably the unit coin — upwards as the outer loop. The inner loop examines amounts up to the specified amount. Using a coin of the current denomination will decrease the amount to be returned. Thus we can look back to see how many coins will be required to finish the transaction to get the total number of coins, should we choose to use one of this denomination. On the other hand, we can look in the preceeding row of the table to see how many coins would be used if we do not use a coin of this denomination and simply use those of the lower denominations. [Note: there is a sentinel row in c: c[0][k] = 0]
// Table generation --- dynamic programming portion for ( k = 0; k <= amt; k++ ) // Sentinel row c[0][k] = 0; for ( j = 1; j <= n; j++ ) { coin[j] = 0; c[j][0] = 0; // Insure column 0 holds zeroes. for ( k = 1; k <= amt; k++ ) if ( j == 1 ) if ( k < d[j] ) c[j][k] = Integer.MAX_VALUE; else c[j][k] = 1 + c[j][k-d[j]]; else if ( k < d[j] ) c[j][k] = c[j-1][k]; else c[j][k] = Math.min(c[j-1][k], 1 + c[j][k-d[j]]); }
Dynamic Programming: Change Generation /* The dynamic programming change algorithm — C++ Taken from Brassard and Bratley (1996), pp. 263- Author: Timothy Rolfe, based on Brassard and Bratley */ #include <iostream.h> #include <iomanip.h> #include <stdlib.h> // RAND_MAX const bool DEBUG = false; // For debugging: display the array contents void display (int *a, int nrow, int ncol) { int row, col; for ( row = 0; row < nrow; row++ ) { for ( col = 0; col < ncol; col++ ) cout << setw(3) << ( a[row][col] > 99? -1 : a[row][col] ); cout << endl; } } // Used in change() inline int Min(int p, int q) { return p > q? q : p; } /
Specimen program execution follows the Java version.