Unino&Find Algo Approximation Notes
Unino&Find Algo Approximation Notes
01.COIN-ROW PROBLEMS:-
There is a row of n coins whose values are some positive integers c1,c2,...,cn, not necessarily distinct.
The goal is to pick up the maximum amount of money subject to the constraint that no two coins adjacent
in the initial row can be picked up.
Let F(n) be the maximum amount that can be picked up from the row of n coins. To derive a recurrence
for F(n), The largest amount we can get from the first group is equal to cn +F(n−2)—the value of the nth
coin plus the maximum amount we can pick up from the first n−2 coins. The maximum amount we can
get from the second group is equal to F(n−1) by the definition of F(n).
ALGORITHM CoinRow(C[1..n])
//Applies formula bottom up to find the maximum amount of money that can be
picked up from a coin row without picking two adjacent coins.
//Input: Array C[1..n] of positive integers indicating the coin values.
//Output: The maximum amount of money that can be picked up.
F[0]←0;
F[1]←C[1]
for i ←2to n do
F[i]←max(C[i]+F[i −2],F[i −1])
return F[n]
EXAMPLE:-
This is by far superior to the alternatives the straightforwarod top down application of
recurrence and solving the problem by exhaustive search.
02.CHANGE MAKING PROBLEM:-
Consider the general instance of the following well-known problem. Give change for
amount n using the minimum number of coins of denominations d1<...<dm.
consider a dynamic programming algorithm for the general case, assuming availability
of unlimited quantities of coins for each of the m denominations d1 < d2<……..dm
where d1=1.
Let F(n) be the minimum number of coins whose values add up to n; it is convenient to
define F(0) =0. The amount n can only be obtained by adding one coin of
denominationdj to the amount n−dj for j =1,2,...,m such that n≥dj. Therefore, we can
consider all such denominations and select the one minimizing F(n−dj)+1.Since 1is a
constant, and find the smallest F(n−dj) first and then add 1 to it. Hence, we have the
following recurrence for F(n):
F(n) = min j(n≥dj) {F(n− dj)}+1 for n>0, F(0) =0.
ALGORITHM FOR CHANGE-MAKING
PROBLEM:-
ALGORITHM ChangeMaking(D[1..m],n)
//Applies dynamic programming to find the minimum number of coins of denominations d1 < d2<…dm where d1=1
that add up to a given amount n.
//Input: Positive integer n and array D[1..m]of increasing positive integers indicating the coin denominations where
D[1]= 1.
//Output: The minimum number of coins that add up to n .
F[0]←0
for i ←1to n do
temp←∞; j ←1
while j ≤m and i ≥ D[j] do
temp←min(F[i −D[j]],temp)
j ←j+1
F[i]← temp+1
return F[n]
Example:-
MinCoinChange to amount n = 6 and coin denominations 1, 3, and 4.
F[0] = 0
F[1] = min{F[1 – 1]} + 1 = 1
F[2] = min{F[2 – 1]} + 1 = 2
F[3] = min{F[3 – 1], F[3 – 3]} + 1 = 1
F[4] = min{F[4 – 1], F[4 – 3], F[4 – 4]} + 1 = 1
F[5] = min{F[5 – 1], F[5 – 3], F[5 – 4]} + 1 = 2
F[6] = min{F[6 – 1], F[6 – 3], F[6 – 4]} + 1 = 2
To find the coins of an optimal solution, we need to backtrace the computa tions to see which of the
denominations produced the minima in formula.
The last application of the formula (for n = 6), the minimum was produced by d2 =3. The second minimum
(for n=6−3) was also produced for a coin of that denomination. Thus, the minimum-coin set for n = 6 is two
3’s
TIME AND SPACE COMPLEXITY FOR
CHANGE MAKING PROBLEM:-
The time and space efficiencies of the algorithm are obviously O(nm) and 0(n),
respectively.
TIME COMPLEXITY=0(nm)
SPACE COMPLEXITY=0(n)
03.COIN-COLLECTING PROBLEM:-
Several coins are placed in cells of an n×m board no more than one coin per cell.A
robot located in the upper left cell of the board, needs to collect as many of the coins as
possible and bring them to the bottom right cell. On each step, the robot can move
either one cell to the right or one cell down from its current location. When the robot
visits a cell with a coin, it always picks up that coin.
Design an algorithm to find the maximum number of coins the robot can collect and a
path it needs.
Let F(i,j)be the largest number of coins the robot can collect and bring to the cell (i, j) in the
Ith row and jth column of the board. It can reach this cell either from the adjacent cell (i −
1,j) above it or from the adjacent cell (i, j − 1) to the left of it.
The largest numbers of coins that can be brought to these cells are
F(i−1,j)andF(i,j−1),respectively.
There are no adjacent cells above the cells in the first row, and there are no adjacent cells to
the left of the cells in the first column. For those cells, we assume that F(i− 1,j)and F(i,j−1)
are equal to 0 for their nonexistent neighbors.
The largest number of coins the robot can bring to cell (i, j) is the maximum of these two
numbers plus one possible coin at cell (i, j) itself. In other words, we have the following
formula for F(i,j):
F(i,j)=max{F(i−1,j),F(i,j −1)}+cij for 1≤i ≤n, 1≤j ≤m
F(0,j)=0 for 1≤j ≤m and F(i,0)=0 for1≤i ≤n,
where cij = 1if there is a coin in cell (i, j), and cij = 0 otherwise.
ALGORITHM FOR COIN-COLLECTING
PROBLEM:-
ALGORITHM RobotCoinCollection(C[1..n, 1..m])
//Applies dynamic programming to compute the largest number of coins a robot can collect on an n × m board by
starting at (1, 1) and moving right and down from upper left to down right corner.
//Input: Matrix C[1..n, 1..m] whose elements are equal to 1 and 0 for cells with and without a coin, respectively.
//Output: Largest number of coins the robot can bring to cell (n, m)
F[1, 1]←C[1,1];
for j ←2 to m do
F[1,j]←F[1,j−1]+C[1,j]
for i ←2 to n do
F[i, 1]←F[i −1,1]+C[i,1]
for j ←2 to m do
F[i, j]←max(F[i −1,j],F[i,j −1])+C[i,j]
return F[n, m]
Tracing the computations backward makes it possible to get an optimal path: if
F(i−1,j)>F(i,j−1), an optimal path to cell (i, j) must come down from the adjacent cell
above it; if F(i− 1,j) an optimal path to cell (i, j) must come from the adjacent cell on
the left; and if F(i− 1,j)= F(i,j− 1), it can reach cell (i, j) from either direction. This
yields two optimal paths for the instance.
If ties are ignored, one optimal path can be obtained in (n+m) time.
EXAMPLE:- (a) Coins to collect.
(b) Dynamic programming algorithm results.
(c) Two paths to collect 5 coins, the maximum number of coins
possible.
TIME AND SPACE COMPLEXITY FOR
COIN-COLLECTING PROBLEM:-
Computing the value of F(i,j) byformula for each cell of the table takes constant time, the
time efficiency of the algorithm is 0(nm). Its space efficiency is obviously also 0(nm).
TIME COMPLEXITY=0(nm)
SPACE COMPLEXITY=0(nm)
THANK YOU