0% found this document useful (0 votes)
60 views86 pages

DAA UNIT-IV-Dynamic Porgramming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views86 pages

DAA UNIT-IV-Dynamic Porgramming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Syllabus:

Dynamic Programming: Introduction, DP Techniques, Applications – Matrix Chain


Multiplication, Optimal Binary Search Tree, All Pairs Shortest Paths, Travelling Salesperson
Problem, Climbing Stairs, Min Cost Climbing Stairs, Maximum Sub Array, Number of Corner
Rectangles, 0/1 Knapsack Problem.
Strings: Introduction, Count Substrings with Only One Distinct Letter, Valid Word
Abbreviation, Longest Repeating Substring, Longest Common Subsequence, Longest Increasing
Subsequence.

PART-1: Dynamic Programming. Page No: 02

PART-2: Strings using Dynamic Programming. Page No: 69

KMIT Page No:1


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

PART-1: Dynamic Programming.

Introduction to Dynamic Programming:

Dynamic Programming (DP) is a powerful algorithmic technique used to solve problems by


breaking them down into simpler subproblems. It is particularly useful for optimization problems
where the solution can be expressed in terms of solutions to overlapping subproblems. By storing
the results of these subproblems, dynamic programming avoids redundant computations, leading
to significant performance improvements.

Key Principles of Dynamic Programming

1. Optimal Substructure:
A problem has an optimal substructure if an optimal solution to the problem can be
constructed from optimal solutions to its subproblems. For instance, in the shortest path
problem, the shortest path from A to C through B involves the shortest path from A to B
and B to C.

2. OverlappingSubproblems:
Dynamic programming is effective when a problem can be divided into smaller
subproblems that are solved repeatedly. Unlike divide-and-conquer (e.g., mergesort), DP
reuses the results of overlapping subproblems rather than solving them independently.

3. Memoization and Tabulation:


Memoization: A top-down approach(Recursion) where subproblem results are stored in a
data structure (like a dictionary or array) as they are computed, and reused as needed.
Tabulation: A bottom-up approach where subproblem results are computed iteratively
and stored in a table.

Memoization vs Tabulation:

 Memoization is indeed the natural way of solving a problem, so coding is easier in


memoization when we deal with a complex problem. Coming up with a specific order
while dealing with lot of conditions might be difficult in the tabulation.
 Also think about a case when we don't need to find the solutions of all the subproblems.
In that case, we would prefer to use the memoization instead.
 However, when a lot of recursive calls are required, memoization may cause memory
problems because it might have stacked the recursive calls to find the solution of the
deeper recursive call but we won't deal with this problem in tabulation.
 Generally, memoization is also slower than tabulation because of the large recursive
calls.

KMIT Page No:2


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Two Approaches of Dynamic Programming

There are two approaches of the dynamic programming. The first one is the top-down approach
and the second is the bottom-up approach. Let's take a closer look at both the approaches.

Top-Down Approach

The way we solved the Fibonacci series was the top-down approach. We just start by
solving the problem in a natural manner and stored the solutions of the subproblems along the
way. We also use the term memoization, a word derived from memo for this.

In other terms, it can also be said that we just hit the problem in a natural manner and hope that
the solutions for the subproblem are already calculated and if they are not calculated, then we
calculate them on the way.
Java Program for Fibonacci (Top Down Approach) :

class Fib
{
static int[] F = new int[50]; //array to store fibonacci terms

public static void initF()


{
for(int i=0; i<50; i++)

KMIT Page No:3


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

{
F[i] = -1;
}
}
public static int dynamicFibonacci(int n)
{
if (n==0)
{
F[n] = 0;
}
else if (n == 1)
{
F[n] = 1;
}
else {
F[n] = dynamicFibonacci(n-1) + dynamicFibonacci(n-2);
}
}
return F[n];
}

public static void main(String[] args)


{
initF();
System.out.println(dynamicFibonacci(46));
}
}

Bottom-Up Approach
The other way we could have solved the Fibonacci problem was by starting from the
bottom i.e., start by calculating the 2nd2nd term and then 3rd3rd and so on and finally
calculating the higher terms on the top of these i.e., by using these values.

We use a term tabulation for this process because it is like filling up a table from the start.

KMIT Page No:4


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Fibonacci series using Dynamic Programming(Bottom-up approach).

class Fib
{
static int[] F = new int[50]; //array to store fibonacci terms

public static int fibonacciBottomUp(int n)


{
F[n] = 0;
F[1] = 1;

for(int i=2; i<=n; i++)


{
F[i] = F[i-1] + F[i-2];
}
return F[n];
}

public static void main(String[] args)


{
System.out.println(fibonacciBottomUp(46));
}
}
 As said, we started calculating the Fibonacci terms from the starting and ended up using
them to get the higher terms.
 Also, the order for solving the problem can be flexible with the need of the problem and is
not fixed. So, we can solve the problem in any needed order.
 Generally, we need to solve the problem with the smallest size first. So, we start by sorting
the elements with size and then solve them in that order.
 Let's compare memoization and tabulation and see the pros and cons of both.
Applications:

1. Matrix Chain Multiplication.


2. Optimal Binary Search Tree.
3. All Pairs Shortest Paths.
4. Travelling Salesperson Problem.
5. 0/1 Knapsack Problem.
6. Climbing Stairs.
7. Min Cost Climbing Stairs.
8. Maximum Sub Array.
9. Number of Corner Rectangles.

KMIT Page No:5


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

1. Matrix Chain Multiplication:

 Suppose, We are given a sequence (chain) (A1, A2……An) of n matrices to be multiplied,


and we wish to compute the product (A1A2…..An).
 We can evaluate the above expression using the standard algorithm for multiplying pairs
of matrices as a subroutine once we have parenthesized it to resolve all ambiguities in how
the matrices are multiplied together.
 Matrix multiplication is associative, and so all parenthesizations yield the same product.
 For example, if the chain of matrices is (A1, A2, A3, A4) then we can fully parenthesize
the product (A1A2A3A4) in five distinct ways:
1:-(A1(A2(A3A4))) ,
2:-(A1((A2A3)A4)),
3:- ((A1A2)(A3A4)),
4:-((A1(A2A3))A4),
5:-(((A1A2)A3)A4) .
 We can multiply two matrices A and B only if they are compatible. the number of
columns of A must equal the number of rows of B.
 If A is a p x q matrix and B is a q x r matrix,the resulting matrix C is a p x r matrix.
 The time to compute C is dominated by the number of scalar multiplications is pqr.
 We shall express costs in terms of the number of scalar multiplications.
 For example, if we have three matrices (A1,A2,A3) and its cost is
(10x100),(100x5),(5x500) respectively.
 So we can calculate the cost of scalar multiplication is 10*100*5=5000 if ((A1A2)A3),
10*5*500=25000 if (A1(A2A3)), and so on cost calculation.

Note that in the matrix-chain multiplication problem, we are not actually multiplying
matrices. Our goal is only to determine an order for multiplying matrices that has the
lowest cost.
That is here is minimum cost is 5000 for above example.
 So problem is we can perform a many time of cost multiplication and repeatedly the
calculation is performing. So this general method is very time consuming and tedious.
 So we can apply Dynamic programming for solve this kind of problem.

Dynamic Programming Solutione:

The idea is to solve this problem by computing the minimum number of scalar multiplications
required for each subproblem and using the results to construct the solution to the larger
problem.

KMIT Page No:6


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Steps in the DP Algorithm:


1. Define Subproblems:
 Let m[i][j] represent the minimum number of scalar multiplications required to
multiply matrices Ai to Aj.
2. Recursive Relation:
 For i ≤ j

Here, k represents the position where the sequence is split for optimal multiplication.
3. Base Case:
 m[i][i]= 0, because multiplying a single matrix requires no operations.
4. Tabulation:
 Use a bottom-up approach to fill a 2D table, where m[i][j] is computed using
previously calculated subproblem solutions.
Algorithm in Pseudocode:

MatrixChainOrder (p[], n):


Let m[][] be a 2D array of size n x n

For i = 1 to n:
m[i][i] = 0 // Cost of multiplying one matrix is zero

For length = 2 to n: // Length of the chain


For i = 1 to n - length + 1:
j = i + length - 1
m[i][j] = Infinity
For k = i to j - 1:
// Cost of splitting at k
cost = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j]
If cost < m[i][j]:
m[i][j] = cost

Return m[1][n-1] // Minimum cost to multiply the chain

KMIT Page No:7


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Example of Matrix Chain Multiplication


Example: We are given the sequence {4, 10, 3, 12, 20, and 7}. The matrices have size 4 x 10,

10 x 3, 3 x 12, 12 x 20, 20 x 7. We need to compute M [i,j], 0 ≤ i, j≤ 5. We know M [i, i] = 0 for

all i.

Let us proceed with working away from the diagonal. We compute the optimal solution for the
product of 2 matrices.

In Dynamic Programming, initialization of every method done by ‘0’.So we initialize it by ‘0’.It

will sort out diagonally.


We have to sort out all the combination but the minimum output combination is taken into

consideration.

Calculation of Product of 2 matrices:


1. m (1,2) = m1 x m2
= 4 x 10 x 10 x 3
= 4 x 10 x 3 = 120

2. m (2, 3) = m2 x m3
= 10 x 3 x 3 x 12

KMIT Page No:8


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

= 10 x 3 x 12 = 360

3. m (3, 4) = m3 x m4
= 3 x 12 x 12 x 20
= 3 x 12 x 20 = 720

4. m (4,5) = m4 x m5
= 12 x 20 x 20 x 7
= 12 x 20 x 7 = 1680

 We initialize the diagonal element with equal i,j value with ‘0’.
 After that second diagonal is sorted out and we get all the values corresponded to it

Now the third diagonal will be solved out in the same way.

Now product of 3 matrices:

M [1, 3] = M1 M2 M3
1. There are two cases by which we can solve this multiplication:
(M1 x M2) + M3, M1+ (M2x M3)
2. After solving both cases we choose the case in which minimum output is there.

M [1, 3] =264
As Comparing both output 264 is minimum in both cases so we insert 264 in table and

( M1 x M2) + M3 this combination is chosen for the output making.

KMIT Page No:9


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

M [2, 4] = M2 M3 M4
1. There are two cases by which we can solve this multiplication:
(M2x M3)+M4, M2+(M3 x M4)
2. After solving both cases we choose the case in which minimum output is there.

M [2, 4] = 1320
As Comparing both output 1320 is minimum in both cases so we insert 1320 in table and

M2+(M3 x M4) this combination is chosen for the output making.

M [3, 5] = M3 M4 M5
1. There are two cases by which we can solve this multiplication:
(M3 x M4) + M5, M3+ ( M4xM5)
2. After solving both cases we choose the case in which minimum output is there.

M [3, 5] = 1140
As Comparing both output 1140 is minimum in both cases so we insert 1140 in table and ( M3 x

M4) + M5this combination is chosen for the output making.

Now Product of 4 matrices:


M [1, 4] = M1 M2 M3 M4

There are three cases by which we can solve this multiplication:


1. ( M1 x M2 x M3) M4
2. M1 x(M2 x M3 x M4)
3. (M1 xM2) x ( M3 x M4)

KMIT Page No:10


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

After solving these cases we choose the case in which minimum output is there

M [1, 4] =1080
As comparing the output of different cases then ‘1080’ is minimum output, so we insert 1080 in

the table and (M1 xM2) x (M3 x M4) combination is taken out in output making,
M [2, 5] = M2 M3 M4 M5

There are three cases by which we can solve this multiplication:


1. (M2 x M3 x M4)x M5
2. M2 x( M3 x M4 x M5)
3. (M2 x M3)x ( M4 x M5)

After solving these cases we choose the case in which minimum output is there

M [2, 5] = 1350
As comparing the output of different cases then ‘1350’ is minimum output, so we insert 1350 in

the table and M2 x( M3 x M4xM5)combination is taken out in output making.

Now Product of 5 matrices:

M [1, 5] = M1 M2 M3 M4 M5

There are five cases by which we can solve this multiplication:


1. (M1 x M2 xM3 x M4 )x M5
2. M1 x( M2 xM3 x M4 xM5)

KMIT Page No:11


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

3. (M1 x M2 xM3)x M4 xM5


4. M1 x M2x(M3 x M4 xM5)

After solving these cases we choose the case in which minimum output is there

M [1, 5] = 1344
As comparing the output of different cases then ‘1344’ is minimum output, so we insert 1344 in

the table and M1 x M2 x(M3 x M4 x M5)combination is taken out in output making.

Final Output is:

So we can get the optimal solution of matrices multiplication….

Java Program For Chain Matrix Multiplication using memoization:


ChainMatrixMul.java

class ChainMatrixMul
{
public static int matrixChainMultiplication(int[] dims, int i, int j, int[][] lookup)
{
// base case: one matrix
if (j <= i + 1)
{
return 0;
}
int min = Integer.MAX_VALUE;

// if the subproblem is seen for the first time, solve it and store its result in a lookup table
if (lookup[i][j] == 0)
{
for (int k = i + 1; k <= j - 1; k++)

KMIT Page No:12


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

{
// recur for `M[i+1]…M[k]` to get an `i × k` matrix
int cost = matrixChainMultiplication(dims, i, k, lookup);

// recur for `M[k+1]…M[j]` to get an `k × j` matrix


cost += matrixChainMultiplication(dims, k, j, lookup);

// cost to multiply two `i × k` and `k × j` matrix


cost += dims[i] * dims[k] * dims[j];

if (cost < min) {


min = cost;
}
}
lookup[i][j] = min;
}
// return the minimum cost to multiply `M[j+1]…M[j]`
return lookup[i][j];
}

public static void main(String[] args)


{
// Matrix `M[i]` has dimension `dims[i-1] × dims[i]` for `i=1…n`
// input is 10 × 30 matrix, 30 × 5 matrix, 5 × 60 matrix

int[] dims = { 10, 30, 5, 60 };

// lookup table to store the solution to already computed subproblems

int[][] lookup = new int[dims.length][dims.length];

System.out.print("The minimum cost is " +


matrixChainMultiplication(dims, 0, dims.length - 1, lookup));
}
}

Output:

The minimum cost is 4500

The time complexity of the above top-down solution is O(n3) and requires O(n2) extra space,
where n is the total number of matrices.

KMIT Page No:13


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Java Program For Chain Matrix Multiplication using bottom-up approach :

ChainMatrixMul.java

class ChainMatrixMul
{
public static int matrixChainMultiplication(int[] dims)
{
int n = dims.length;

int[][] c = new int[n + 1][n + 1];

for (int len = 2; len <= n; len++) // subsequence lengths


{
for (int i = 1; i <= n - len + 1; i++)
{
int j = i + len - 1;
c[i][j] = Integer.MAX_VALUE;

for (int k = i; j < n && k <= j - 1; k++)


{
int cost = c[i][k] + c[k + 1][j]
+ dims[i - 1] * dims[k] * dims[j];

if (cost < c[i][j]) {


c[i][j] = cost;
}
}
}
}
return c[1][n - 1];
}
public static void main(String[] args)
{
// Matrix `M[i]` has dimension `dims[i-1] × dims[i]` for `i=1…n`
// input is 10 × 30 matrix, 30 × 5 matrix, 5 × 60 matrix
int[] dims = { 10, 30, 5, 60 };

System.out.print("The minimum cost is " + matrixChainMultiplication(dims));


}
}
Output:

The minimum cost is 4500

KMIT Page No:14


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

2. Optimal Binary Search Tree:

 Optimal Binary Search Tree extends the concept of Binary search tree.
 Binary Search Tree (BST) is a nonlinear data structure which is used in many scientific
applications for reducing the search time.
 In BST, left child is smaller than root and right child is greater than root. This arrangement
simplifies the search procedure.
 Optimal Binary Search Tree (OBST) is very useful in dictionary search.
 The probability of searching is different for different words. OBST has great application in
translation.
 A Binary Search Tree (BST) is a tree where the key values are stored in the internal nodes.
 The external nodes are null nodes.
 The keys are ordered lexicographically, i.e. for each internal node all the keys in the left sub-
tree are less than the keys in the node, and all the keys in the right sub-tree are greater.
 When we know the frequency of searching each one of the keys, it is quite easy to compute
the expected cost of accessing each node in the tree.
 An optimal binary search tree is a BST, which has minimal expected cost of locating each
node.
 Search time of an element in a BST is O(n), whereas in a Balanced-BST search time
is O(log n).
 Again the search time can be improved in Optimal Cost Binary Search Tree, placing the most
frequently used data in the root and closer to the root element, while placing the least frequently
used data near leaves and in leaves.

 Given a fixed set of identifiers, we wish to create a binary search tree organization.

KMIT Page No:15


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

 We may expect different binary search trees for the same identifier set to have different
performance characteristics.
 The tree of Figure(a) in, the worst case, requires four comparisons to find an identifier, where
as the tree of Figure(b) requires only three.
On the average the two trees need 12/5 and 11/5 comparisons respectively.
For example in the case of tree(a), it takes 1,2,2,3 and 4 comparisons, respectively,
to find the identifiers for, do, while, int & if.
Thus the average number of comparisons is (1+2+2+3+4)/5=12/5.
 This calculation assumes that Each identifier is searched for with equal probability and that no
unsuccessful searches(i.e.searches for identifiers not in the tree) are made.
 In a general situation, we can expect different identifiers to be searched for with different
frequencies (or probabilities).
 In addition, we can expect unsuccessful searches also to be made.
 Let us assume that the given set of identifiers is {a1,a2,……,an} with a1 < a2 < ……< an.
 Let p(i) be the probability with which we search for ai.
 Let q(i) be the probability that the identifier ‘x’ being searched for is such that ai< x < ai+1,
where 0 ≤ i ≤ n.
Then, Ʃ0 ≤ i ≤ n q(i) the probability of an unsuccessful search.
Clearly,
Ʃ0 ≤ i ≤ n p(i)+ Ʃ0 ≤ i ≤ n q(i) =1.
 Given this data, we wish to construct an optimal binary search tree for {a1,a2,….. an,}.
 In obtaining a cost function for binary search trees, it is useful to add a ‘’fictitious’’ node in
place of every empty sub tree in the search tree. Such nodes, called “External nodes”. All other
nodes are internal nodes.
 In Fig. Square boxes indicates External Nodes.

 If a binary search tree represents n identifiers, then there will be exactly ‘n’ internal
nodes and n+1 (fictitious) external nodes.
 Every internal node represents a point where a successful search may terminate.
 Every external node represents a point where an unsuccessful search may terminate.
 The expected cost contribution from the internal node for ai is

KMIT Page No:16


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

p(i) * level(a i ).
 The expected cost contribution from the External node for Ei is
q(i) * level((E i ) -1).
 The preceding discussion leads to the following formula for the expected Cost of a binary
search tree

Ʃ1 ≤ i ≤ n p(i) * level(a i)+Ʃ0 ≤ i ≤ n q(i) * level((E i ) -1)=1.


 Example: The possible binary search trees for the identifier set (a1,a2,a3)= (do, if, while)
are given if Figure5.1

 With p(1)= 0.5,p(2) = 0.1,p(3) = 0.05, q(0)= 0.15, q(1) =0.1,q(2)= 0.05 and q(3)=0.05
 For instance ,cost (tree a) can be computed as follows. The contribution from successful
searches(internal nodes) is
=3 * 0.5+2*0.1+1*0.05
=1.75.
 the contribution from unsuccessful searches(external nodes) is
=3 * 0.15+3 * 0.1+2 * 0.05+1*0.05
= 0.90.
 Cost (tree ‘a’)=1.75+0.90=2.65.
 All the other costs can also be calculated in a similar manner
Cost (tree ‘a’)=1.75+0.90=2.65.
Cost (tree ‘b’)=1.9
Cost (tree ‘c’)=1.5.
Cost (tree ‘d’)=2.05.
Cost (tree ‘e’)=1.6.
 Tree ‘c’ is optimal with this assignment of p's and q's.

KMIT Page No:17


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

 To apply dynamic programming to the problem of obtaining an optimal binary search


tree, we need to view the construction of such a tree as the result of a sequence of
decisions and then observe that the principle of optimality holds when applied to the
problem state resulting from a decision.
 A possible approach to this would be to make a decision as to which of the ‘ai’s’ should
be assigned to the root node of the tree.

 If the tree is optimal, then above equation must be minimum.


 Hence, cost(l) must be minimum over all binary search trees containing a1, a2,…….. ak-1
And E1,E2,…….. Ek-1.
 Similarly cost(r)must be minimum. If we use c(I , j) to represent the cost of an optimal
binary search tree tij containing ai+1,….aj And Ei,,…….. Ej.
 then for the tree to be optimal ,we must have
->cost(l) =c(0,k-1) & cost(r)= c(k , n).
 In addition, k must be chosen such that

KMIT Page No:18


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

p(k) + c(0,k -1)+c(k,n) +w(0,k -1)+w(k,n) is minimum.

 Above Equation can be solved for c(0,n) by first computing all c(i, j)such that j-i = 1.
 Next we can compute all c(i, j) such that j-i =2, then all c(i, j) with j-i = 3, and so on.
 If during this computation we record the root r(i,j) of each tree tij, then an optimal binary
search tree can be constructed from these r(i, j).

Note: r(i, j)is the value of ‘k’ that minimize, if i≠j

KMIT Page No:19


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Example: Let n = 4 and (a1,a2,a3,a4)=(do, if, int, while). Let p(1:4) = (3,3,1,1) and
q(0:4) = (2,3,1,1,1)
we have w(i, i)= q(i) and c(i, i)=0 and r(i, i)= 0,
0 ≤i ≤ 4. Using above equation and the observation w(i, j)=P(j)+q(j)+w(i,j-1),we get Sol:

 Compute Weight (W), Cost (C), Root (R) for j-i=1, j-i=2, j-i=3 & j-i=4
p1=3,p2=3,p3=1 & p4=1 q0=2,q1=3,q2=1,q3=1 & q4=1

Compute W,C,R for |j-i|=1

And Compute j-i=2 , j-i=3 and j-i=4 iterations


 For |j-i|=2

for k=1&2 ,Find C(0,2),w(0,2) and R(0,2) & Take Minimum Cost and Root Value

for k=2&3, Find C(1,3),w(1,3) and R(1,3) & Take Minimum Cost and Root Value

for k=3&4, Find C(2,4),w(2,4) and R(2,4) & Take Minimum Cost and Root Value

KMIT Page No:20


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

 For |j-i|=3

for k=1,2&3 ,Find C(0,3),w(0,3) and R(0,3) & Take Minimum Cost and Root Value

for k=2,3&4 ,Find C(1,4),w(1,4) and R(1,4) & Take Minimum Cost and Root Value

 For |j-i|=4
for k=1,2,3 & 4,Find C(0,4),w(0,4) and R(0,4) & Take Minimum Cost ,this is
total cost of BST and Minimum K Value i.e Root , this is root node for BST.

KMIT Page No:21


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

KMIT Page No:22


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

KMIT Page No:23


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

3. All Pairs Shortest Paths:

 Let G = (V, E) be a Directed graph with n vertices. Let cost be a cost Adjacency matrix
for G such that cost(i , i)=0, 1≤ i ≤n.
 Then cost(i, j) is the length (or cost) of edge (i, j) if (i, j) Є E(G).
And cost(i, j)= ∞ and if i≠ j and (i,j) ∉ E(G).
 The all-pairs shortest-path problem is to determine .a matrix ‘A’ such that A(i, j)is the
length of a shortest path from i to j.
 The matrix ‘A’ can be obtained by solving n single-source problems using the algorithm
Shortest Paths.Since each application of this procedure requires 0(n2) time.
 The matrix ‘A’ can be obtained in 0(n3)time.
 We obtain an alternate 0(n3) solution to this problem using the principle of optimality.
 Let us examine a shortest i to j path in G, i≠ j This path originates at vertex i and goes
through some intermediate vertices (possibly none) and terminates at vertex j.
 We can assume that this path contains no cycles for if there is a cycle, then this can be
deleted without increasing the path length(no cycle has negative length)
 If ‘k’ is an intermediate vertex on this Shortest path, then the sub paths from i to k and
from k to j must be shortest paths from i to k and k to j, respectively.
 Otherwise, the i to j path is not of minimum length. So, the principle of optimality holds.
 This alerts us to the prospect of using dynamic programming.
 If ‘k’ is the intermediate vertex with highest index, then the i to k path is a shortest i to k
path in G going through no vertex with index greater than k -1.
 Similarly the k to j path is a shortest k to j path in G going through no vertex of index
greater than k-1.
 We can regard the construction of a shortest i to j path as first requiring a decision as
to which is the highest indexed intermediate vertex k.
 Once this decision has been made, we need to find two shortest paths,
 One from i to k and the other from k to j.
 Neither of these may go through a Vertex with index greater than k -1.
 Using Ak (i, j) to represent the length of a shortest path from i to j going through no
vertex of index greater than k, we obtain

A(i, j)= min{ min1≤ k ≤n{AK-1(i, k) +AK-1 (k, j)},cost(i, j)}

Clearly, A0(i, j) = cost(i, j), 1≤ i ≤n , 1≤ j ≤n.


 We can obtain a recurrence for Ak(i, j) using an argument similar to that used before.
 A shortest path from i to j going through no vertex higher than ‘k’ either goes through
vertex k or it does not.
 If it does, Ak(i, j) = Ak-1(i, k) + Ak-1(k, j)

KMIT Page No:24


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

 If it does not, then no intermediate vertex has index greater than k-1.
- Hence Ak(i, j) = Ak-1(i, j)
Combining

Ak(i, j)=min{AK-1(i, j) , AK-1(i, k) +AK-1 (k, j)} k≥1

Pseudo code:

Complexity analysis of All Pairs Shortest Path Algorithm:


It is very simple to derive the complexity of all pairs’ shortest path problem from the above
algorithm. It uses three nested loops. The innermost loop has only one statement. The complexity
of that statement is Q(1).

The running time of the algorithm is computed as :

KMIT Page No:25


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Solution:
Optimal substructure formula for Floyd’s algorithm,

Dk [i, j] = min { Dk – 1 [i, j], Dk – 1 [i, k] + Dk – 1 [k, j] }

Iteration 1 : k = 1 :
D1[1, 2] = min { Do [1, 2], Do [1, 1] + Do [1, 2] }
= min {∞, 0 + ∞} = ∞

KMIT Page No:26


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

D1[1, 3] = min { Do [1, 3], Do [1, 1] + Do [1, 3] }


= min {3, 0 + 3} = 3
D1[1, 4] = min { Do [1, 4], Do [1, 1] + Do [1, 4] }
= min {∞, 0 + ∞} = ∞
D1[2, 1] = min { Do [2, 1], Do [2, 1] + Do [1, 1] }
= min {2, 2 + 0} = 2
D1[2, 3] = min { Do [2, 3], Do [2, 1] + Do [1, 3] }
= min {∞, 2 + 3} = 5
D1[2, 4] = min { Do [2, 4], Do [2, 1] + Do [1, 4] }
= min {∞, 2 + ∞} = ∞
D1[3, 1] = min { Do [3, 1], Do [3, 1] + Do [1, 1] }
= min {∞, 0 + ∞} = ∞
D1[3, 2] = min { Do [3, 2], Do [3, 1] + Do [1, 2] }
= min {7, ∞ + ∞} = 7
D1[3, 4] = min { Do [3, 4], Do [3, 1] + Do [1, 4] }
= min {1, ∞ + ∞} = 1
D1[4, 1] = min { Do [4, 1], Do [4, 1] + Do [1, 1] }
= min {6, 6 + 0} = 6
D1[4, 2] = min { Do [4, 2], Do [4, 1] + Do [1, 2] }
= min {∞, 6 + ∞} = ∞
D1[4, 3] = min { Do [4, 3], Do [4, 1] + Do [1, 3] }
= min {∞, 6 + 3} = 9

KMIT Page No:27


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Note : Path distance for highlighted cell is improvement over


original matrix.

Iteration 2 (k = 2) :

D2[1, 2] = D1 [1, 2] = ∞

D2[1, 3] = min { D1 [1, 3], D1 [1, 2] + D1 [2, 3] }


= min {3, ∞ + 5} = 3
D2[1, 4] = min { D1 [1, 4], D1 [1, 2] + D1 [2, 4] }
= min {∞, ∞ + ∞} = ∞
D2[2, 1] = D1 [2, 1] = 2
D2[2, 3] = D1 [2, 3] = 5
D2[2, 4] = D1 [2, 4] = ∞
D2[3, 1] = min { D1 [3, 1], D1 [3, 2] + D1 [2, 1] }
= min {∞, 7 + 2} = 9
D2[3, 2] = D1 [3, 2] = 7
D2[3, 4] = min { D1 [3, 4], D1 [3, 2] + D1 [2, 4] }
= min {1, 7 + ∞} = 1

KMIT Page No:28


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

D2[4, 1] = min { D1 [4, 1], D1 [4, 2] + D1 [2, 1] }


= min {6, ∞ + 2} = 6
D2[4, 2] = D1 [4, 2] = ∞
D2[4, 3] = min { D1 [4, 3], D1 [4, 2] + D1 [2, 3] }
= min {9, ∞ + 5} = 9

Iteration 3 (k = 3) :

D3[1, 2] = min { D2 [1, 2], D2 [1, 3] + D2 [3, 2] }


= min {∞, 3 + 7} = 10
D3[1, 3] = D2 [1, 3] = 3

D3[1, 4] = min { D2 [1, 4], D2 [1, 3] + D2 [3, 4] }


= min {∞, 3 + 1} = 4
D3[2, 1] = min { D2 [2, 1], D2 [2, 3] + D2 [3, 1] }
= min {2, 5 + 9} = 2
D3[2, 3] = D2 [2, 3] = 5

D3[2, 4] = min { D2 [2, 4], D2 [2, 3] + D2 [3, 4] }


= min {∞, 5 + 1} = 6

KMIT Page No:29


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

D3[3, 1] = D2 [3, 1] = 9

D3[3, 2] = D2 [3, 2] = 7

D3[3, 4] = D2 [3, 4] = 1

D3[4, 1] = min { D2 [4, 1], D2 [4, 3] + D2 [3, 1] }


= min {6, 9 + 9} = 6
D3[4, 2] = min { D2 [4, 1], D2 [4, 3] + D2 [3, 2] }
= min {∞, 9 + 7} = 16
D3[4, 3] = D2 [4, 3] = 9

Iteration 4 (k = 4) :
D4[1, 2] = min { D3 [1, 2], D3 [1, 4] + D3 [4, 2] }
= min {10, 4 + 16} = 10
D4[1, 3] = min { D3 [1, 3], D3 [1, 4] + D3 [4, 1] }
= min {3, 4 + 9} = 3
D4[1, 4] = D3 [1, 4] = 4
D4[2, 1] = min { D3 [2, 1], D3 [2, 4] + D3 [4, 1] }
= min {2, 6 + 6} = 2

KMIT Page No:30


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

D4[2, 3] = min { D3 [2, 3], D3 [2, 4] + D3 [4, 3] }


= min {5, 6 + 9} = 5
D4[2, 4] = D3 [2, 4] = 6
D4[3, 1] = min { D3 [3, 1], D3 [3, 4] + D3 [4, 1] }
= min {9, 1 + 6} = 7
D4[3, 2] = min { D3 [3, 2], D3 [3, 4] + D3 [4, 2] }
= min {7, 1 + 16} = 7
D4[3, 4] = D3 [3, 4] = 1

D4[4, 1] = D3 [4, 1] = 6

D4[4, 2] = D3 [4, 2] = 16

D4[4, 3] = D3 [4, 3] = 9

Final distance matrix is,

KMIT Page No:31


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Java Program for All Pairs Shortest Path: AllPiarsShortestpath.java

import java.util.ArrayList;
import java.util.List;

class AllPiarsShortestpath
{
// Recursive function to print path of given vertex `u` from source vertex `v`
private static void printPath(int[][] path, int v, int u, List<Integer> route)
{
if (path[v][u] == v)
{
return;
}
printPath(path, v, path[v][u], route);
route.add(path[v][u]);
}

// Function to print the shortest cost with path information between


// all pairs of vertices
private static void printSolution(int[][] path, int n)
{
for (int v = 0; v < n; v++)
{
for (int u = 0; u < n; u++)
{
if (u != v && path[v][u] != -1)
{
List<Integer> route = new ArrayList<>();
route.add(v);
printPath(path, v, u, route);
route.add(u);
System.out.printf("The shortest path from %d —> %d is %s\n",
v, u, route);
}
}
}
}

// Function to run the Floyd–Warshall algorithm

KMIT Page No:32


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

public static void floydWarshall(int[][] adjMatrix)


{
// base case
if (adjMatrix ==null || adjMatrix.length == 0) {
return;
}

// total number of vertices in the `adjMatrix`


int n = adjMatrix.length;

// cost[] and path[] stores shortest path


// (shortest cost/shortest route) information
int[][] cost = new int[n][n];
int[][] path = new int[n][n];

// initialize cost[] and path[]


for (int v = 0; v < n; v++)
{
for (int u = 0; u < n; u++)
{
// initially, cost would be the same as the weight of the edge
cost[v][u] = adjMatrix[v][u];

if (v == u) {
path[v][u] = 0;
}
else if (cost[v][u] != Integer.MAX_VALUE) {
path[v][u] = v;
}
else {
path[v][u] = -1;
}
}
}

// run Floyd–Warshall
for (int k = 0; k < n; k++)
{
for (int v = 0; v < n; v++)
{

KMIT Page No:33


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

for (int u = 0; u < n; u++)


{
// If vertex `k` is on the shortest path from `v` to `u`,
// then update the value of cost[v][u] and path[v][u]

if (cost[v][k] != Integer.MAX_VALUE
&& cost[k][u] != Integer.MAX_VALUE
&& (cost[v][k] + cost[k][u] < cost[v][u]))
{
cost[v][u] = cost[v][k] + cost[k][u];
path[v][u] = path[k][u];
}
}

// if diagonal elements become negative, the


// graph contains a negative-weight cycle
if (cost[v][v] < 0)
{
System.out.println("Negative-weight cycle found!!");
return;
}
}
}

// Print the shortest path between all pairs of vertices


printSolution(path, n);
}
public static void main(String[] args)
{
// define infinity
int I = Integer.MAX_VALUE;

// given adjacency representation of the matrix


int[][] adjMatrix = new int[][]
{
{ 0, I, -2, I },
{ 4, 0, 3, I },
{ I, I, 0, 2 },
{ I, -1, I, 0 }
};

KMIT Page No:34


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

// Run Floyd–Warshall algorithm


floydWarshall(adjMatrix);
}
}
Output:

The shortest path from 0 —> 1 is [0, 2, 3, 1]


The shortest path from 0 —> 2 is [0, 2]
The shortest path from 0 —> 3 is [0, 2, 3]
The shortest path from 1 —> 0 is [1, 0]
The shortest path from 1 —> 2 is [1, 0, 2]
The shortest path from 1 —> 3 is [1, 0, 2, 3]
The shortest path from 2 —> 0 is [2, 3, 1, 0]
The shortest path from 2 —> 1 is [2, 3, 1]
The shortest path from 2 —> 3 is [2, 3]
The shortest path from 3 —> 0 is [3, 1, 0]
The shortest path from 3 —> 1 is [3, 1]
The shortest path from 3 —> 2 is [3, 1, 0, 2]

KMIT Page No:35


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

4. Travelling Salesperson Problem:

Problem Statement:
A traveler needs to visit all the cities from a list, where distances between all the cities are
known and each city should be visited just once. What is the shortest possible route that he visits
each city exactly once and returns to the origin city?
 Travelling salesman problem is the most notorious computational problem.
 We can use brute-force approach to evaluate every possible tour and select the best one.
For n number of vertices in a graph, there are (n - 1)! number of possibilities.
 Instead of brute-force using dynamic programming approach, the solution can be obtained in
lesser time, though there is no polynomial time algorithm.
 Let G = (V,E) be a directed graph with edge costs Cij.
 The variable Cij is defined such that
 Cij > 0 for all i and j and Cij = ∞ if (i, j) ∉ E.
 Let |V| = n and assume n > 1.
 A tour of G is a directed simple cycle that includes every vertex in V.
 The cost of a tour is “the sum of the cost of the edges on the tour”.
 The traveling sales person problem is to find a tour of minimum cost.
 The traveling sales person problem finds application in a variety of situations.
Example 1:
 Suppose we have to route a postal van to pick up mail from mail boxes located at ‘n’ different
sites.
 An ‘n + 1’ vertex graph can be used to represent the situation.
 One vertex represents the post office from which the Postal van starts and to which it must
return.
 Edge(i, j)is assigned a cost equal to the distance from site ‘i’ to site ‘j’.
 The route taken by the postal van is a tour, and we are interested in finding a tour of minimum
length.
Example2:
 suppose we wish to use a robot arm to tighten
 the nuts on some piece of machinery on an assembly line.
 The arm will start from its initial position (which is over the first nut to be tightened),
successively move to each of the remaining nuts, and return to the initial position.
 The path of the arm is clearly a tour on a graph in which vertices represent the nuts.
 A minimum-cost tour will minimize the time needed for the arm to complete its task (note
that only the total arm movement time is variable; the nut tightening time is independent of the
tour).

KMIT Page No:36


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Example 3:
 From a production environment in which several
 Commodities are manufactured on the same set of machines.
 The manufacture proceeds in cycles. In each production cycle, ‘n’ different commodities are
produced.
 When the machines are changed from production of commodity ‘i’ to commodity ‘j’, a
change over cost Cij is incurred.
 It is desired to find a sequence in which to manufacture these commodities.
 This sequence should minimize the sum of changeover costs(the remaining production costs
are sequence independent).
 Since the manufacture proceeds cyclically, It is necessary to include the cost of starting the
next cycle.
 -This is just the change over cost from the last to the first commodity.
 -Hence, this problem can be regarded as a traveling sales person problem on an ‘n’ vertex
graph with edge
 cost Cij's being the change over cost from commodity ‘i’ to commodity ’j’.

In the following discussion we shall, without loss of generality, regard a tour to be a simple
path that starts and ends at vertex ‘1’.
 Every tour consists of an edge (i, k) for some k ∈V-{1}. and a path from vertex ‘k’ to vertex
‘1’.
 The path from vertex ‘k’ to vertex ‘1’ goes through each vertex in V-{1,k} exactly once.
 It is easy to see that if the tour is optimal, then the path from ‘k’ to ‘1’ must be a shortest ‘k’
to ‘1’ path going through all vertices in V-{1,k} .
 Hence, the principle of optimality holds.
 Let g(i, S) be the length of a shortest path starting at vertex ‘i’ ,going through all vertices in
‘S’, and terminating at vertex ‘1’.
 The function g(1 ,V-{1}) is the length of an optimal sales person tour.
 From the principal of optimality it follows that

g(1,V-{1})=min {c + g(k,V -{1,k})}  Eq-1


2≤k ≤ n 1k

 Generalizing (from above Equation , we obtain(for i∉S)

g(i , S) = minj∈S{c i j+ g( j , S -{ j})} Eq-2

KMIT Page No:37


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

 1st Equation can be solved for g(1,V-{1}) if we know g(k, V -{1, k}) for all choices of
‘k’.
 The ‘g’ values can be obtained by using Eq.2
 Clearly ,

g(i, Ø) = Ci1,1≤ i ≤ n. –>Eq-3


 Hence, we can use (2nd Equation ) obtain g(i , S) for all of size 1.
 Then we can obtain g(i ,S) for ‘S’ with |S|= 2, and so on.
 When |S| < n -1, the values of ‘i’ and ‘S’ for which g(i , S) is needed are such that i≠1,1
∉ S and i ∉ S.

KMIT Page No:38


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

KMIT Page No:39


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

KMIT Page No:40


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

KMIT Page No:41


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

5. 0/1 Knapsack Problem:

 Given a set of items, each having different weight and value or profit associated with it.
 Find the set of items such that the total weight is less than or equal to a capacity of the
knapsack and the total value earned is as large as possible.
 The knapsack problem is useful in solving resource allocation problem.
Let X = < x1, x2, x3, . . . . . , xn> be the set of n items. Sets W = <w1, w2, w3, . . . , wn> and
V = < v1, v2, v3, . . . , vn> are weight and value associated with each item in X.
Knapsack capacity is M unit.
 The knapsack problem is to find the set of items which maximizes the profit such that
collective weight of selected items does not cross the knapsack capacity.
 Select items from X and fill the knapsack such that it would maximize the profit.
Knapsack problem has two variations. 0/1 knapsack, that does not allow breaking of
items. Either add an entire item or reject it. It is also known as a binary knapsack.
Fractional knapsack allows breaking of items. Profit will be earned proportionally.

Mathematical formulation
We can select any item only ones. We can put items x i in knapsack if knapsack can
accommodate it. If the item is added to a knapsack, the associated profit is accumulated.

Knapsack problem can be formulated as follow :


Maximize sum=vi*xi subjected to sum=wi*xi <= M
Xi in(0,1) for binary knapsack
Xi in[0,1] for fractional knapsack.

Two approaches for solving knapsack using dynamic programming.


i. Using Set Representation.
ii. Using Tabular Method.

i. Using Set Representation:


In forward approach, dynamic programming solves knapsack problem as follow:
1. Set S0 = {(0, 0)}
2. S1i = {(p, w) | (p – pi) ∈ Si, (w – wi) ∈ Si }
We can obtain Si + 1 by invoking xi + 1
If xi = 0 (item xi is excluded) then S1i = Si
If xi = 1 (item xi is included) then S1i is computed by adding (pi + 1, wi + 1) in each state of
Si.

KMIT Page No:42


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Step-1: S i + 1 = MERGE_PURGE (Si, S1i). MERGE_PURGE does following:


Dominance Rule:
For two pairs (px, wx) ∈ Si + 1 and (py, wy) ∈ Si + 1, if px ≤ py and wx ≥ wy, we say
that (px, wx) is dominated by (py, wy). And the pair (px, wx) is discarded.
It also purges all the pairs (p, w) from Si + 1
if w > M, i.e. it weight exceeds
knapsack capacity.
Step-2: Repeat step 1 n times

Step-3: fn(M) = Sn. This will find the solution of KNAPSACK(1, n, M).

Purging Rule:
Step-4: for each pair (p, w) ∈ Sn

if (p, w) ∈ Sn , &
if (p, w) ∉ Sn – 1, then set xn = 1 otherwiae xn = 0 and
update p = p – xn and w = w – wn

Example-1: Solve the instance of 0/1 knapsack problem using dynamic Programming :
n = 4, M = 25, (P1, P2, P3 P4) = (10, 12, 14, 16), (W1, W2, W3, W4) = (9, 8, 12, 14)

Solution:
Knapsack capacity is very large, i.e. 25, so tabular approach won’t be suitable. We will use the
set method to solve this problem

Initially, S0 = { (0, 0) }
Iteration 1:
Obtain S10 by adding pair (p1, w1) = (10, 9) to each pair of S0
S10 = S0 + (10, 9) = {(10, 9)}
Obtain S1 by merging and purging S0 and S10
S1 = MERGE_PURGE (S0, S10)
= { (0, 0), (10, 9) }

Iteration 2:

KMIT Page No:43


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Obtain S11 by adding pair (p2, w2) = (12, 8) to each pair of S1


S11 = S1 + (12, 8) = {(12, 8), (22, 17)}
Obtain S2 by merging and purging S1 and S11
S2 = MERGE_PURGE(S1, S11)
= { (0, 0), (12, 8), (22, 17) }

Pair (10, 9) is discarded because pair (12, 8) dominates (10, 9)

Iteration 3:
Obtain S12 by adding pair (p3, w3) = (14, 12) to each pair of S2
S12 = S2 + (14, 12)
= { (14, 12), (26, 20), (36, 29) }

Obtain S3 by merging and purging S2 and S12 .


S3 = MERGE_PURGE (S2, S12 )
= { (0, 0), (12, 8), (22, 17), (14, 12), (26, 20) }

Pair (36, 29) is discarded because its w > M

Iteration 4:
Obtain S13 by adding pair (p4, w4) = (16, 14) to each pair of S3
S13 = S3 + (16, 14)
= { (16, 14), (28, 22), (38, 31), (30, 26), (42, 34) }

Obtain S4 by merging and purging S3 and S13.


S4 = MERGE_PURGE (S3, S13)
= { (0, 0), (12, 8), (14, 12), (16, 14), (22, 17), (26, 20), (28, 22) }

Pair (38, 31), (30, 26) ,and (42, 34) are discarded because its w > M

Find optimal solution


Here, n = 4.

Start with the last pair in S4, i.e. (28, 22)


(28, 22) ∈ S4 but (28, 22) ∉ S3

KMIT Page No:44


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

So set xn = x4 = 1
Update,

p = p – p4 = 28 – 16 = 12
w = w – w4 = 22 – 14 = 8
n=n–1=4–1=3

Now n = 3, pair (12, 8) ∈ S3 and (12, 8) ∈ S2


So set xn = x3 = 0
n=n–1=3–1=2

Now n = 2, pair(12, 8) ∈ S2 but (12, 8) ∉ S1


So set xn = x2 = 1
Update,

p = p – p2 = 12 – 12 = 0
w = w – w2 = 8 – 8 = 0
Problem size is 0, so stop.

Optimal solution vector is (x1, x2, x3, x3) = (0, 1, 0, 1) Thus, this approach selects pair (12, 8)
and (16, 14) which gives profit of 28.

Example-2: Generate the sets Si, 0 ≤ i ≤ 3 for following knapsack instance. N = 3,


(w1, w2, w3) = (2, 3, 4) and (p1, p2, p3) = (1, 2, 5) with M = 6. Find optimal solution.
Solution:
Initially, S0 = {(0, 0) }
Iteration 1:
Obtain S10 by adding pair (p1, w1) = (1, 2) to each pair of S0
S10 = S0 + (1, 2) = {(1, 2)}
Obtain S1 by merging and purging S0 and S10
S1 = MERGE_PURGE (S0, S10)
= { (0, 0), (1, 2) }

KMIT Page No:45


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Iteration 2:
Obtain S11 by adding pair (p2, w2) = (2, 3) to each pair of S1
S11 = S1 + (2, 3) = {(2, 3), (3, 5)}
Obtain S2 by merging and purging S1 and S11
S2 = MERGE_PURGE(S1, S11)
= { (0, 0), (1, 2), (2, 3), (3, 5) }

Iteration 3:
Obtain S12 by adding pair (p3, w3) = (5, 4) to each pair of S2
S12 = S2 + (5, 4) = {(5, 4), (6, 6), (7, 7), (8, 9) }
Obtain S3 by merging and purging S2 and S12 .
S3 = MERGE_PURGE (S2, S12)
= { (0, 0), (1, 2), (2, 3), (5, 4), (6, 6) }

Pair (7, 7) and (8, 9) are discarded because their w > M

Pair (3, 5) is discarded because pair (5, 4) dominates (3, 5)

Find optimal solution:


Here, n = 3.
Start with the last pair in S3, i.e. (6, 6)
(6, 6) ∈ S3 but (6, 6) ∉ S2
So set xn = x3 = 1
Update,

p = p – p3 = 6 – 5 = 1
w = w – w3 = 6 – 4 = 2
Now n = 2, pair(1, 2) ∈ S2 and (1, 2) ∈ S1
So set xn = x2 = 0
Now n = 1, pair(1, 2) ∈ S1 but (1, 2) ∉ S0
So set xn = x1 = 1

KMIT Page No:46


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Optimal solution vector is (x1, x2, x3) = (1, 0, 1) Thus, this approach selects pair (1, 2)
and (5, 4).

ii. Using Tabular Method:

If the weight of the item is larger than the remaining knapsack capacity, we skip the item, and
the solution of the previous step remains as it is. Otherwise, we should add the item to the
solution set and the problem size will be reduced by the weight of that item. Corresponding
profit will be added for the selected item.

Dynamic programming divides the problem into small sub-problems. Let V is an array of the
solution of sub-problems. V[i, j] represents the solution for problem size j with first i items. The
mathematical notion of the knapsack problem is given as :

Example: Find an optimal solution for following 0/1 Knapsack problem using dynamic
programming: Number of objects n = 4, Knapsack Capacity M = 5, Weights (W1, W2, W3, W4) =
(2, 3, 4, 5) and profits (P1, P2, P3, P4) = (3, 4, 5, 6).
Solution:
Solution of the knapsack problem is defined as,

KMIT Page No:47


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

We have the following stats about the problem,

Item Weight (wi) Value (vi)

I1 2 3

I2 3 4

I3 4 5

I4 5 6

 Boundary conditions would be V [0, i] = V[i, 0] = 0. Initial configuration of


table looks like.
j→

Item Detail 0 1 2 3 4 5

i=0 0 0 0 0 0 0

i=1 w1=2 v1=3 0

KMIT Page No:48


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

i=2 w2=3 v2=4 0

i=3 w3=4 v3=5 0

i=4 w4=5 v4=6 0

Filling first column, j = 1


V [1, 1] ⇒ i = 1, j = 1, wi = w1 = 2
As, j < wi, V [i, j] = V [i – 1, j]
V [1, 1] = V [0, 1] = 0

V [2, 1] ⇒ i = 2, j = 1, wi = w2 = 3
As, j < wi, V [i, j] = V [i – 1, j]
V [2, 1] = V [1, 1] = 0

V [3, 1] ⇒ i = 3, j = 1, wi = w3 = 4
As, j < wi, V [i, j] = V [i – 1, j]
V [3, 1] = V [2, 1] = 0

V [4, 1] ⇒ i = 4, j = 1, wi = w4 = 5
As, j < wi, V[i, j] = V[i – 1, j]
V [4, 1] = V [3, 1] = 0

Filling first column, j = 2


V[1, 2] ⇒ i = 1, j = 2, wi = w1 = 2, vi = 3
As, j ≥ wi, V [i, j]=max {V [i – 1, j], vi + V[i – 1, j – wi] }
= max {V [0, 2], 3 + V [0, 0]}

V[1, 2] = max (0, 3) = 3

KMIT Page No:49


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

V[2, 2] ⇒ i = 2, j = 2, wi = w2 = 3, vi = 4
As, j < wi, V [i, j] = V[i – 1, j]
V[2, 2] = V[1, 2] = 3

V[3, 2] ⇒ i = 3, j = 2, wi = w3 = 4, vi = 5
As, j < wi, V[i, j] = V[i – 1, j]
V[3, 2] = V [2, 2] = 3

V[4, 2] ⇒ i = 4, j = 2, wi = w4 = 5, vi = 6
As, j < wi, V[i, j] = V[i – 1, j]
V[4, 2] = V[3, 2] = 3

Filling first column, j = 3


V[1, 3] ⇒ i = 1, j = 3, wi = w1 = 2, vi = 3
As, j ≥ wi, V [i, j]=max {V [i – 1, j], vi + V [i – 1, j – wi] }
= max {V [0, 3], 3 + V [0, 1]}

V[1, 3] = max (0, 3) = 3

V[2, 3] ⇒ i = 2, j = 3, wi = w2 = 3, vi = 4
As, j ≥ wi, V [i, j] = max {V [i – 1, j], vi + V [i – 1, j – wi] }
= max {V [1, 3], 4 + V [1, 0]}

V[2, 3] = max (3, 4) = 4

V[3, 3] ⇒ i = 3, j = 3, wi = w3 = 4, vi = 5
As, j < wi, V [i, j] = V [i – 1, j]
V[3, 3] = V [2, 3] = 4

V[4, 3] ⇒ i = 4, j = 3, wi = w4 = 5, vi = 6
As, j < wi, V[i, j] = V[i – 1, j]
V[4, 3] = V [3, 3] = 4

Filling first column, j = 4


V[1, 4] ⇒ i = 1, j = 4, wi = w1 = 2, vi = 3

KMIT Page No:50


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

As, j ≥ wi, V [i, j]=max {V [i – 1, j], vi + V [i – 1, j – wi] }


= max {V [0, 4], 3 + V [0, 2]}

V[1, 4] = max (0, 3) = 3

V[2, 4] ⇒ i = 2, j = 4, wi = w2 = 3 , vi = 4
As, j ≥ wi, V [i, j] =max {V [i – 1, j], vi + V [i – 1, j – wi] }
= max {V [1, 4], 4 + V [1, 1]}

V[2, 4] = max (3, 4 + 0) = 4

V[3, 4] ⇒ i = 3, j = 4, wi = w3 = 4, vi = 5
As, j ≥ wi, V [i, j]=max {V [i – 1, j], vi + V [i – 1, j – wi] }
= max {V [2, 4], 5 + V [2, 0]}

V[3, 4] = max (4, 5 + 0) = 5

V[4, 4] ⇒ i = 4, j = 4, wi = w4 = 5, vi = 6
As, j < wi, V [i, j] = V [i – 1, j]
V[4, 4] = V [3, 4] = 5

Filling first column, j = 5


V [1, 5] ⇒ i = 1, j = 5, wi = w1 = 2, vi = 3
As, j ≥ wi, V [i, j] = max {V [i – 1, j], vi + V [i – 1, j – wi] }
= max {V [0, 5], 3 + V [0, 3]}

V[1, 5] = max (0, 3) = 3

V[2, 5] ⇒ i = 2, j = 5, wi = w2 = 3, vi = 4
As, j ≥ wi, V [i, j] =max {V [i – 1, j], vi + V [i – 1, j – wi] }
= max {V [1, 5], 4 + V [1, 2]}

V[2, 5] = max (3, 4 + 3) = 7

V[3, 5] ⇒ i = 3, j = 5, wi = w3 = 4, vi = 5
As, j ≥ wi, V [i, j] = max {V [i – 1, j], vi + V [i – 1, j – wi] }

KMIT Page No:51


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

= max {V [2, 5], 5 + V [2, 1]}

V[3, 5] = max (7, 5 + 0) = 7

V [4, 5] ⇒ i = 4, j = 5, wi = w4 =5, vi = 6
As, j ≥ wi, V [i, j] = max {V [i – 1, j], vi + V [i – 1, j – wi] }
= max {V [3, 5], 6 + V [3, 0]}

V[4, 5] = max (7, 6 + 0) = 7

Final table would be,

j→

Item Detail 0 1 2 3 4 5

i=0 0 0 0 0 0 0

i=1 w1=2 v1=3 0 0 3 3 3 3

i=2 w2=3 v2=4 0 0 3 4 4 7

i=3 w3=4 v3=5 0 0 3 4 5 7

i=4 w4=5 v4=6 0 0 3 4 5 7

KMIT Page No:52


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

KMIT Page No:53


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

KMIT Page No:54


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

KMIT Page No:55


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

KMIT Page No:56


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Complexity analysis

With n items, there exist 2n subsets, the brute force approach examines all subsets to find
the optimal solution. Hence, the running time of the brute force approach is O(2n). This is
unacceptable for large n.
Dynamic programming finds an optimal solution by constructing a table of size n ´ M, where n is
a number of items and M is the capacity of the knapsack. This table can be filled up in O(nM)
time, same is the space complexity.

 Running time of Brute force approach is O(2n).


 Running time using dynamic programming with memorization is O(n * M).

KMIT Page No:57


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

6. Climbing Stairs:
Given a staircase of N steps and you can either climb 1 or 2 steps at a given time. The task is to
return the count of distinct ways to climb to the top.
Note: The order of the steps taken matters.

Examples:
Input: N = 3
Output: 3

Explanation:
There are three distinct ways of climbing a staircase of 3 steps :
[1, 1, 1], [2, 1] and [1, 2].

Input: N=2 = 2
Output: 2
Explanation:
There are two distinct ways of climbing a staircase of 2 steps :
[1, 1] and [2].

Input: n = 4
Output: 5
(1, 1, 1, 1), (1, 1, 2), (2, 1, 1), (1, 2, 1), (2, 2)

KMIT Page No:58


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Java Program for Climbing Stairs Using Dynamic Programming(Bottom up Approach):


ClimbingStairs.java

import java.util.*;
class ClimbingStairs
{
public int climbStairs(int n)
{
if (n == 1) {
return 1;
}
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println(new ClimbingStairs().climbStairs(n));
}
}
Sample i/p & o/p:
Case=1
input =9
output =55

Case =2
input =2
output =2

Case =3
input =20
output =10946

KMIT Page No:59


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Java Program for Climbing Stairs Using Dynamic Programming (Top-Down Approach):
ClimbingStairs.java

import java.util.*;
class ClimbingStairs
{

public int climbStairs(int n)


{
int[] memo=new int[n+1];

if (n < 0)
{
return 0;
}
else if (memo[n] != 0)
{
// If we stored something in our cache reuse it and avoid recalculating everything
return memo[n];
}
else if (n == 0)
{
return 1;
}
else
{
// store our calculation inside our cache so we don't have to recalculate it again for //memo[n]
memo[n] = climbStairs(n-1) + climbStairs(n-2);
return memo[n];
}
}

public static void main(String[] args)


{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println(new Test().climbStairs(n));
}
}

KMIT Page No:60


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

7. Min Cost Climbing Stairs:

 You are given an integer array cost where cost[i] is the cost of i step on a staircase.
 Once you pay the cost, you can either climb one or two steps.
 You can either start from the step with index 0 , or the step with index 1 .
 Return the minimum cost to reach the top of the floor.

Example 1:
Input: cost = [10,15,20]
Output: 15

Explanation: You will start at index 1.


- Pay 15 and climb two steps to reach the top.
The total cost is 15.

Example 2:
Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Explanation: You will start at index 0.
- Pay 1 and climb two steps to reach index 2.
- Pay 1 and climb two steps to reach index 4.
- Pay 1 and climb two steps to reach index 6.
- Pay 1 and climb one step to reach index 7.
- Pay 1 and climb two steps to reach index 9.
- Pay 1 and climb one step to reach the top.
The total cost is 6.

Approach:

 At each step, we can consider the answer to be the combined cost of the current step, plus the
lesser result of the total cost of each of the solutions starting at the next two steps.
 This means that, thinking backwards, we can solve for the smallest problem first, and then
build down from there.
 For the last two steps, the answer is clearly their individual cost. For the third to last step, it's
that step's cost, plus the lower of the last two steps.
 Now that we know that, we can store that data for later use at lower steps. Normally, this
would call for a DP array, but in this case, we could simply store the values in-place.
 (Note: If we choose to not modify the input, we could create a DP array to store this
information at the expense of O(N) extra space.)
 So we should iterate downward from the end, starting at the third step from the end, and
update the values in cost[i] with the best total cost from cost[i] to the end.

KMIT Page No:61


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

 Then, once we reach the bottom of the steps, we can choose the best result
of cost[0] and cost[1] and return our answer.

Time Complexity: O(N) where N is the length of cost


Space Complexity: O(1) or O(N) if we use a separate DP array

Java Program For Minimum Cost Climbing Stairs Using Dynamic Programming

MinCostClimbingStairs.java
import java.util.*;

class MinCostClimbingStairs
{
public int minCostClimbingStairs(int[] cost)
{
int n = cost.length;
int[] dp = new int[n];
for (int i=0; i<n; i++) {
if (i<2) dp[i] = cost[i];
else dp[i] = cost[i] + Math.min(dp[i-1], dp[i-2]);
}
return Math.min(dp[n-1], dp[n-2]);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int ar[]=new int[n];
for(int i=0;i<n;i++)
ar[i]=sc.nextInt();
System.out.println(new MinCostClimbingStairs().minCostClimbingStairs(ar));
}
}
Sample i/p & o/p:
case =1
input =3
20 30 40
output =30

case =2
input =7
2 3 50 2 2 50 2
output =9

KMIT Page No:62


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

8. Maximum Sub Array:


Given an integer array nums , find the contiguous subarray (containing at least one number)
which has the largest sum and return its sum.
A subarray is a contiguous part of an array.

Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Example 2:
Input: nums = [1]
Output: 1

Example 3:
Input: nums = [5,4,-1,7,8]
Output: 23

Approach- Using Kadane’s Algorithm:

Kadane’s Algorithm is an iterative Dynamic programming algorithm. It calculates the


maximum sum subarray ending at a particular position by using the maximum sum subarray
ending at the previous position. Follow the below steps to solve the problem.

 Define two-variable currSum which stores maximum sum ending here and maxSum
which stores maximum sum so far.
 Initialize currSum with 0 and maxSum with INT_MIN.
 Now, iterate over the array and add the value of the current element to currSum and
check
o If currSum is greater than maxSum, update maxSum equals to currSum.
o If currSum is less than zero, make currSum equal to zero.
 Finally, print the value of maxSum.

The Time complexity of Kadane’s Algorithm is O(n).

KMIT Page No:63


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Example:

Java Program For Maximum Sub Array using Dynamic Programming:


MaxSubArray.java
import java.util.*;

class MaxSubArray
{
public int maxSubArray(int[] A)
{
int n = A.length;
int[] dp = new int[n];//dp[i] means the maximum subarray ending with A[i];
dp[0] = A[0];
int max = dp[0];
for(int i = 1; i < n; i++){
dp[i] = A[i] + (dp[i - 1] > 0 ? dp[i - 1] : 0);
max = Math.max(max, dp[i]);
}
return max;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int ar[]=new int[n];
for(int i=0;i<n;i++)
ar[i]=sc.nextInt();
System.out.println(new MaxSubArray(). maxSubArray(ar));
}
}

KMIT Page No:64


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Example:1
input =9
-2 1 -3 4 -1 2 1 -5 4
output =6

Example:2
input =2
1 -2
output =1

Example:3
input =20
6 -2 -7 7 -4 3 5 -5 7 -7 9 4 7 -5 -7 5 1 2 -3 3
output =26

KMIT Page No:65


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

9. Number of Corner Rectangles:


Given a grid where each entry is only 0 or 1, find the number of corner rectangles.
A corner rectangle is 4 distinct 1s on the grid that form an axis-aligned rectangle.
Note that only the corners need to have the value 1. Also, all four 1s used must be distinct.
Example 1:
Input:
grid = [[1, 0, 0, 1, 0],
[0, 0, 1, 0, 1],
[0, 0, 0, 1, 0],
[1, 0, 1, 0, 1]]
Output: 1
Explanation: There is only one corner rectangle, with corners grid[1][2], grid[1][4], grid[3][2],
grid[3][4].

Example 2:
Input:
grid = [[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]
Output: 9
Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle.

Example 3: Input: grid = [[1, 1, 1, 1]]


Output: 0
Explanation: Rectangles must have four distinct corners.

Approach:

1. Matrix Representation: The matrix is of size m x n, and we need to check for pairs of
rows that can form rectangles with 1s as corners.
2. Observations:
o A rectangle's top-left corner and bottom-left corner lie in the same column, as do
its top-right and bottom-right corners.
o For any two rows i and j, if column c1 and column c2 have 1s in both rows, they
form a rectangle.
3. Dynamic Programming Idea:
o Let dp[j] represent the count of columns where both row i and row j have a 1 up
to the current column.
o As we iterate through columns for rows i and j, count the number of rectangles
that can be formed using the pairs in dp[j].

KMIT Page No:66


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Algorithm:

 Iterate through all pairs of rows (i, j).


 For each column, check if both rows have 1.
 Use dp to store the number of common 1s in previous columns between rows i and j.

Java Program for Count Number Of Corner Rectangles Using dynamic Programming:
CountCornerRectangles.java
import java.util.*;

class CountCornerRectangles
{
public int countCornerRectangles(int[][] grid)
{
int m = grid.length, n = grid[0].length;
int[][] dp = new int[n][n];
int res = 0;
for(int i = 0;i < m;i++)
{
for(int j = 0;j < n;j++)
{
if(grid[i][j] != 1) continue;
for(int k = j+1;k < n;k++)
{
if(grid[i][k] == 1)
{
res += dp[j][k];
dp[j][k]++;
}
}
}
}
return res;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int grid[][]=new int[m][n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
grid[i][j]=sc.nextInt();
System.out.println(new CountCornerRectangles().countCornerRectangles(grid));
}

KMIT Page No:67


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Example-1:
input =3 4
1010
1111
0111
output =4

Example-2:
input =4 6
101111
101101
010111
111101
output =25

Example-3:
input =5 5
11111
11111
11111
11111
11111
output =100

Time Complexity: O(N*M2)


Auxiliary Space: O(M2)

KMIT Page No:68


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

PART-2 Strings: Introduction, Count Substrings with Only One Distinct Letter, Valid Word
Abbreviation, Longest Repeating Substring, Longest Common Subsequence, Longest Increasing
Subsequence.
Introduction:

Strings are an incredibly common type of data in computers. This page introduces the basics of
Java strings: chars, +, length(), and substring().

A Java string is a series of characters gathered together, like the word "Hello", or the phrase
"practice makes perfect". Create a string in the code by writing its chars out between double
quotes.

 String stores text -- a word, an email, a book


 All computer languages have strings, look similar
 "In double quotes"
 Sequence of characters ("char")

 String str = "Hello";


 This picture shows the string object in memory, made up of the individual chars H e l l o.
We'll see what the index numbers 0, 1, 2 .. mean later on.

String + Concatenation

Tthe + (plus) operator between strings puts them together to make a new, bigger string. The
bigger string is just the chars of the first string put together with the chars of the second string.

String a = "kit" + "ten"; // a is "kitten"

Strings are not just made of the letters a-z. Chars can be punctuation and other miscellelaneous
chars. For example in the string "hi ", the 3rd char is a space. This all works with strings stored
in variables too, like this:

KMIT Page No:69


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

String fruit = "apple";


String stars = "***";
String a = fruit + stars; // a is "apple*

String Length

The "length" of a string is just the number of chars in it. So "hi" is length 2 and "Hello" is length
5. The length() method on a string returns its length, like this:

String a = "Hello";
int len = a.length(); // len is 5

String Index Numbers

 Index numbers -- 0, 1, 2, ...


 Leftmost char is at index 0
 Last char is at index length-1

The chars in a string are identified by "index" numbers. In "Hello" the leftmost char (H) is at
index 0, the next char (e) is at index 1, and so on. The index of the last char is always one less
than the length. In this case the length is 5 and 'o' is at index 4. Put another way, the chars in a
string are at indexes 0, 1, 2, .. up through length-1. We'll use the index numbers to slice and dice
strings with substring() in the next section.

String Substring v1

 str.substring(start)
 Chars beginning at index start
 Through the end of the string
 Later: more complex 2-arg substring()

The substring() method picks out a part of string using index numbers to identify the desired
part. The simplest form, substring(int start) takes a start index number and returns a new string
made of the chars starting at that index and running through the end of the string:

KMIT Page No:70


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

String str = "Hello";


String a = str.substring(1); // a is "ello" (i.e. starting at index 1)
String b = str.substring(2); // b is "llo"
String c = str.substring(3); // c is "lo"

Above str.substring(1) returns "ello", picking out the part of "Hello" which begins at index 1 (the
"H" is at index 0, the "e" is at index 1).

Java String valueOf()

 The java string valueOf() method converts different types of values into string. By the help
of string valueOf() method, you can convert int to string, long to string, boolean to string,
character to string, float to string, double to string, object to string and char array to string.

Internal implementation
public static String valueOf(Object obj)
{
return (obj == null) ? "null" : obj.toString();
}

Signature

The signature or syntax of string valueOf() method is given below:

1. public static String valueOf(boolean b)


2. public static String valueOf(char c)
3. public static String valueOf(char[] c)
4. public static String valueOf(int i)
5. public static String valueOf(long l)
6. public static String valueOf(float f)
7. public static String valueOf(double d)
8. public static String valueOf(Object o)

Returns

String representation of given value

KMIT Page No:71


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Java String valueOf() method example


public class StringValueOfExample
{
public static void main(String args[])
{
int value=30;
String s1=String.valueOf(value);
System.out.println(s1+10);//concatenating string with 10
}
}

Output: 3010
3010

Java String valueOf(boolean bol) Method Example

This is a boolean version of overloaded valueOf() method. It takes boolean value and returns a
string. Let's see an example.

public class StringValueOfExample2


{
public static void main(String[] args)
{
// Boolean to String
boolean bol = true;
boolean bol2 = false;
String s1 = String.valueOf(bol);
String s2 = String.valueOf(bol2);
System.out.println(s1);
System.out.println(s2);
}
}

Output:
true
false

KMIT Page No:72


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Applications:

1. Count Substrings with Only One Distinct Letter.


2. Valid Word Abbreviation.
3. Longest Repeating Substring.
4. Longest Common Subsequence.
5. Longest Increasing Subsequence.

1. Count Substrings with Only One Distinct Letter:

Given a string s, return the number of substrings that have only one distinct letter.
Example 1:
Input: s = "aaaba"
Output: 8
Explanation: The substrings with one distinct letter are "aaa", "aa", "a", "b".
"aaa" occurs 1 time.
"aa" occurs 2 times.
"a" occurs 4 times.
"b" occurs 1 time.
So the answer is 1 + 2 + 4 + 1 = 8.
Example 2:
Input: s = "aaaaaaaaaa"
Output: 55
Approach:

1. Problem Understanding:
o You are given a string s, and you need to find all substrings where all characters
are the same.
o Example: In the string “aaa”, the valid substrings are:
 "a", "a", "a", "aa", "aa", "aaa".
 Total = 6.
2. Dynamic Programming Observation:
o Let dp[i] represent the number of substrings ending at index i where all characters
are the same.
o If s[i] == s[i-1], then dp[i] = dp[i-1] + 1. This is because the current character can
extend all valid substrings ending at i-1.
o If s[i] != s[i-1], then dp[i] = 1 since the substring consisting of s[i] itself is valid.
3. Final Result:
o The total number of substrings is the sum of all values in the dp array.

KMIT Page No:73


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Time Complexity:

o O(n), as we traverse the string once.


o O(1) additional space if the DP array is replaced with a simple variable.

Java Program for Count Substrings with Only One Distinct Letter using Dynamic
Programming: CountSubstringsWithOneDistinctLetter.java

import java.util.*;

public class CountSubstringsWithOneDistinctLetter


{
public static int countSubstrings(String s)
{
if (s == null || s.length() == 0)
{
return 0;
}
int n = s.length();
int dp = 0; // Tracks substrings ending at the current index
int total = 0; // Total count of substrings

for (int i = 0; i < n; i++)


{
if (i > 0 && s.charAt(i) == s.charAt(i - 1))
{
dp = dp + 1; // Extend previous substrings
}
else {
dp = 1; // Start a new substring series
}
total += dp; // Add dp to the total count
}
return total;
}
public static void main(String[] args) {
String s = "aaabaaa";
System.out.println("Count of substrings with one distinct letter: " + countSubstrings(s));
}
}

Example-1
Input:
abcde
Output:
5

KMIT Page No:74


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

2. Valid Word Abbreviation:

Given a non-empty string s and an abbreviation abbr , return whether the string matches with
the given abbreviation.
A string such as "word" contains only the following valid abbreviations:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2",
"2r1", "3d", "w3", "4"]

Notice that only the above abbreviations are valid abbreviations of the string "word" . Any other
string is not a valid abbreviation of "word”.
Note: Assume s contains only lowercase letters and abbr contains only lowercase letters and
digits.
Example 1:
Given s = "internationalization", abbr = "i12iz4n"
Return true.

Example 2:
Given s = "apple", abbr = "a2e"
Return false.

Java Program for Valid Word Abbreviation: ValidWordAbbreviation.java

import java.util.*;

class ValidWordAbbreviation
{
public boolean validWordAbbreviation(String word, String abbr)
{
int i = 0, j = 0;
while (i < word.length() && j < abbr.length())
{
if (word.charAt(i) == abbr.charAt(j)) {
++i;++j;
continue;
}
if (abbr.charAt(j) <= '0' || abbr.charAt(j) > '9')
{
return false;
}
int start = j;
while (j < abbr.length() && abbr.charAt(j) >= '0' && abbr.charAt(j) <= '9')
{
++j;

KMIT Page No:75


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

}
int num = Integer.valueOf(abbr.substring(start, j));
i += num;
}
return i == word.length() && j == abbr.length();
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
String t=sc.next();
System.out.println(new ValidWordAbbreviation().validWordAbbreviation(s,t));
}
}

Input:
internationalization
i12iz4n
Output:
true

KMIT Page No:76


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

3. Longest Repeating Substring:

Given a string S, find out the length of the longest repeating substring(s). Return 0 if no repeating
substring exists.
Example 1:
Input: "abcd"
Output: 0
Explanation: There is no repeating substring.
Example 2:
Input: "abbaba"
Output: 2
Explanation: The longest repeating substrings are "ab" and "ba", each of which occurs twice.
Example 3:
Input: "aabcaabdaab"
Output: 3
Explanation: The longest repeating substring is "aab", which occurs 3 times.
Example 4:
Input: "aaaaa"
Output: 4
Explanation: The longest repeating substring is "aaaa", which occurs twice.

Dynamic Programming Approach:


The idea is to compare the characters at index i and j of String s and the indices i and j. Two
cases arise:
1. If the characters of the string match (s[i-1] == s[j-1]) and their indices are different (i != j),
then make a recursive call for the remaining string (i-1 and j-1) and add 1 to the result.
longestRepeatingSubsequence(i, j, s) = 1 + longestRepeatingSubsequence(i-1, j-1, s)
2. If the characters do not match or the indices are the same, make two recursive calls:
longestRepeatingSubsequence(i, j, str) = max(longestRepeatingSubsequence(i-1, j,
str), longestRepeatingSubsequence(i, j-1, str))
Base Case:
 If either of the strings becomes empty (i == 0 or j == 0), return 0.

Using Top-Down DP (Memoization) – O(n^2) Time and O(n^2) Space

KMIT Page No:77


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Example: String=”AABB”

We can observe in the above table that the length of the longest repeated subsequence is 2

Now we will find out the LRS String

First, we will put '-' under the empty string as shown in the above table.
Case=1
lrs[i] is not equal to lrs[j] and i is not equal to j so lrs[i][j] would be the maximum of top and
left.
Case=2
lrs[i] is equal to lrs[j] and i is equal to j so lrs[i][j] would be the maximum of top and left.
Case=3
lrs[i] is equal to lrs[j] and 'i' is not equal to 'j' . Therefore, the lrs[i][i] would be equal to
(diagonal element + current element).

As we can observe in the above table that AB is the longest repeating subsequence. We have
basically found the LRS by using the constraint that (i==j) and s[i] == s[j] then it will not
contribute to the repeating subsequence otherwise it will contribute.

KMIT Page No:78


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Java Program for Longest Repeating Substring: LRS.java

import java.util.*
class LRS
{
static int longestRepeatingSubsequence(String s)
{
int n = s.length();
int[][] dp = new int[n + 1][n + 1];

for (int i = 1; i <= n; i++)


{
for (int j = 1; j <= n; j++) {
// If char match and indices are different
if (s.charAt(i - 1) == s.charAt(j - 1) && i != j)
{
dp[i][j] = 1 + dp[i - 1][j - 1];
}
else
{
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}

return dp[n][n];
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s = sc.next();
int res = longestRepeatingSubsequence(s);
System.out.println(res);
}
}

Input: abbaba
Output: 2

KMIT Page No:79


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

4. Longest Common Subsequence:

Given two strings text1 and text2 , return the length of their longest common subsequence.
A subsequence of a string is a new string generated from the original string with some
characters(can be none) deleted without changing the relative order of the remaining characters.
(eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings
is a subsequence that is common to both strings.
If there is no common subsequence, return 0.
Example 1:
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.

Example 2:
Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.

Example 3:
Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.

Approach:

The idea is to compare the characters Two Strings


Two cases arise:
1.If the characters of the string match (s[i] == s[j]) then make a recursive call for the
remaining string is s[i,j]= 1+s[i-1,j-1] to the result.
2. If the characters do not match or the indices are the same, make two recursive calls:
string is s[i,j]= max(s(i-1, j),s(i,j-1))
Base Case:
 If either of the strings becomes empty (i == 0 or j == 0), return 0.
The dynamic programming paradigm consists of two methods known as the top-down
approach and the bottom-up approach.

KMIT Page No:80


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

The top-down approach memorizes results in a recursive solution, whereas the bottom-up
approach builds a solution from the base case. Hence, for this particular problem, we will
formulate a bottom-up solution.

Problem Statement: Create a program to find out the length of longest common subsequence for
strings A = “ASDGHJPL”, B = “WSFDHPLJ”.

we starts comparing characters in sequences from beginning and apply above function rules for
lcs at each comparison. The table that is constructed when program finish is:

If characters at i and j are equal then we add 1 to value present in i-1 and j-1 cell. If character at i
and j position are not equal then we pick maximum of value at i-1, j or i, j-1. After table is filled
completely we check last value in table for length of LCS.

Length of longest common subsequence is: 5

KMIT Page No:81


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Java Program For Longest Common Sub Sequences: LCS.java

import java.util.*;

public class LCS


{
int lcs(char[] X, char[] Y, int m, int n)
{
int L[][] = new int[m + 1][n + 1];

for (int i = 0; i <= m; i++)


{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i - 1] == Y[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
return L[m][n];
}
/* Utility function to get max of 2 integers */
int max(int a, int b)
{
return (a > b) ? a : b;
}

public static void main(String[] args)


{
LongestCommonSubsequence lcs = new LongestCommonSubsequence();
Scanner sc=new Scanner(System.in);
String s1 = sc.next();
String s2 = sc.next();

char[] X = s1.toCharArray();
char[] Y = s2.toCharArray();
int m = X.length;
int n = Y.length;

System.out.println(new LCS().lcs(X, Y, m, n));


}
}
Input: s1= "abcde", s2 = "ace" Output: 3

KMIT Page No:82


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

5. Longest Increasing Subsequence:

The Longest Increasing Subsequence (LIS) problem is to find the length of the longest
subsequence of a given sequence such that all elements of the subsequence are sorted in
increasing order.

For example, the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33,
50, 60, 80}.

Example-1:
Input: arr[] = {5,4,1,2,3}
Output: Length of LIS = 3
Explanation: The longest increasing subsequence is 1,2,3

Example-2:
Input: arr[] = {7,5}
Output: Length of LIS = 1
Explanation: The longest increasing subsequences are {5} or {7}.

Dynamic Programming Approach


If we will draw the recursion tree for the above approach then we can easily see in the below
image, there are some overlapping subproblems and in which we can easily apply substructure
properties. Hence we can say we can store some state and use it in dynamic programming.

KMIT Page No:83


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Elements of Dynamic Programming Approach


Here the recursion approach is top-down. Hence we can use it in the implementation of our
dynamic programming bottom-up. What are the different states or elements that are possible for
dynamic programmatic
We have to define problem variables: There is only one parameter on which the state of the
problem depends i.e. which is N here, the size of the array.
We have to define size and table structure: For to write the code in a bottom-up manner we
have to first define the size and table structure.
Input: arr[] = {3, 10, 2, 11}
LIS[] = {1, 1, 1, 1} (initially)

Iteration-wise simulation:

1. arr[2] > arr[1] {LIS[2] = max(LIS [2], LIS[1]+1 = 2}


2. arr[3] < arr[1] {No change}
3. arr[3] < arr[2] {No change}
4. arr[4] > arr[1] {LIS[4] = max(LIS [4], LIS[1]+1 = 2}
5. arr[4] > arr[2] {LIS[4] = max(LIS [4], LIS[2]+1 = 3}
6. arr[4] > arr[3] {LIS[4] = max(LIS [4], LIS[3]+1 = 3}

Time complexity: O(N^2), Where N is the size of the array.


Space complexity: O(N)

KMIT Page No:84


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Java Program for Longest Increasing Subsequence Using Dynamic Programming:


LIS.java

import java.util.*;

public class LIS


{
public static int lengthOfLIS(int[] nums)
{
if (nums == null || nums.length == 0)
{
return 0;
}
int n = nums.length;
int[] dp = new int[n];

// Each element is an increasing subsequence of length 1


for (int i = 0; i < n; i++)
{
dp[i] = 1;
}

// Build the dp array where dp[i] represents the length of the LIS ending at index i
for (int i = 1; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (nums[i] > nums[j])
{
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
// The result is the maximum value in the dp array
int maxLength = 0;
for (int i = 0; i < n; i++)
{
maxLength = Math.max(maxLength, dp[i]);
}

return maxLength;
}

public static void main(String[] args)


{

KMIT Page No:85


DESIGN AND ANALYSIS OF ALGORITHMS UNIT-IV RKR21 Regulations

Scanner sc=new Scanner(System.in);


int n = sc.nextInt();
int[] nums = new int[n];

for(int i=0;i<n;i++)
{
nums[i]=sc.nextInt();
}
System.out.println(new LIS().lengthOfLIS(nums));
}
}

Example-1:
Input:
5,4,1,2,3
Output:
3

Example-2:
Input:
7,5
Output:
1

KMIT Page No:86

You might also like