Daa Unit 3 Digital Notes
Daa Unit 3 Digital Notes
Daa Unit 3 Digital Notes
This document is confidential and intended solely for the educational purpose of
RMK Group of Educational Institutions. If you have received this document
through email in error, please notify the system manager. This document
contains proprietary information and is intended only to the respective group /
learning community as intended. If you are not the addressee you should not
disseminate, distribute or copy through e-mail. Please notify the sender
immediately by e-mail if you have received this document by mistake and delete
this document from your system. If you are not the intended recipient you are
notified that disclosing, copying, distributing or taking any action in reliance on
the contents of this information is strictly prohibited.
22CS306
Design and Analysis of
Algorithms
Department: CSE
Batch/Year: 2023-2027/II
Created by:
Dr. D.Rajalakshmi, Associate Professor
Mr. K Mohana Sundaram , Asst.Professor
Mr. D Jayakumar, Asst Professor
Ms. J. Sherine Glory, Asst Professor
Table of Contents
Sl. Topics Page
No. No.
1. Contents 5
2. Course Objectives 6
6. CO-PO/PSO Mapping 14
Graphs
Searching
& Sorting
Syllabus
Syllabus L T P C
22CS306 DESIGN AND ANALYSIS OF ALGORITHMS
3 0 2 4
Unit I : INTRODUCTION 9+6
Notion of an Algorithm – Fundamentals of Algorithmic Problem Solving – Fundamentals of the
Analysis of Algorithmic Efficiency – Asymptotic Notations and their properties. Analysis
Framework – Empirical analysis - Mathematical analysis for Recursive and Non-recursive
algorithms.
List of Exercise/Experiments:
1.Perform the recursive algorithm analysis.
2.Perform the non-recursive algorithm analysis.
Unit II : BRUTE FORCE AND DIVIDE AND CONQUER 9+6
Brute Force – String Matching – Exhaustive Search - Knapsack Problem. Divide and Conquer
Methodology – Binary Search – Merge sort- Quick sort – Multiplication of Large Integers –
Closest-Pair and Convex Hull Problems. -Transform and Conquer Method: Heap Sort
List of Exercise/Experiments:
1.Write a program to search an element using binary search
2.Write a program to sort the elements using merge sort and find time complexity.
3.Write a program to sort the elements using quick sort and find time complexity.
4.Write a program to sort the elements using heap sort
Unit III : DYNAMIC PROGRAMMING 9+6
Dynamic programming – Principle of optimality - Floyd‘s algorithm – Multi stage graph -
Optimal Binary Search Trees – Longest common subsequence - Matrix-chain multiplication –
Travelling Salesperson Problem – Knapsack Problem and Memory functions.
List of Exercise/Experiments:
1.Solve Floyd’s algorithm
2.Write a program to find optimal binary search tree for a given list of keys.
3.Solve the multi-stage graph to find shortest path using backward and forward approach
4.Write a program to find the longest common subsequence
Unit IV : GREEDY TECHNIQUE AND ITERATIVE IMPROVEMENT 9+6
Greedy Technique - Prim‘s algorithm and Kruskal’s Algorithm, Huffman Trees. The Maximum-
Flow Problem – Maximum Matching in Bipartite Graphs- The Stable marriage Problem.
List of Exercise/Experiments:
1.Write a program to find minimum spanning tree using Prim’s algorithm
2.Implement Kruskal’s algorithm to find minimum spanning tree
3.Write a program to solve maximum flow problem
Unit V : BACKTRACKING AND BRANCH AND BOUND 9+6
P, NP, NP - Complete and NP Hard Problems. Backtracking – N-Queen problem - Subset Sum
Problem. Branch and Bound – LIFO Search and FIFO search - Assignment problem –
Knapsack Problem - Approximation Algorithms for NP-Hard Problems – Travelling Salesman
problem
List of Exercise/Experiments:
1.Write a program to implement sum of subset problem.
2.Write a program to solve N-Queen problem
3.Solve the assignment problem using branch and bound technique
4.Solve knapsack problem using branch and bound technique
Course Outcomes
Course Outcomes
CO# COs K Level
K6 Evaluation
K5 Synthesis
K4 Analysis
K3 Application
K2 Comprehension
K1 Knowledge
CO – PO/PSO Mapping
CO – PO /PSO Mapping Matrix
CO1 3 3 - 1 - - 2 2 2 - 2 3 2 -
CO2 3 2 - 2 2 - - 2 2 2 - 2 3 2 -
CO3 3 3 2 - 2 - - 2 2 2 - 2 3 2 -
CO4 3 2 2 2 - - - 2 2 2 - 2 3 3 -
CO5 3 2 2 - 2 - - 2 2 2 - 2 3 2 -
CO6 3 2 2 - - - - 2 2 2 - 2 2 2 -
Lecture Plan
Unit III
Lecture Plan – Unit 3 - DYNAMIC PROGRAMMING
Sl. Topic Num Proposed Actual CO Taxo Mode
No ber Date Lecture nomy of
. of Date Level Deliver
Perio y
ds
Dynamic
programming – Chalk &
1 Principle of 1 CO3 K3 Talk,
optimality-Floyd‘s Demo
algorithm
Chalk &
Multi stage graph –
2 1 CO3 K3 Talk,
Forward Approach
Demo
Chalk &
Multi stage graph –
3 1 CO3 K3 Talk,
Backward Approach
Demo
Chalk &
Optimal Binary
4 1 CO3 K3 Talk,
Search Trees
Demo
Chalk &
Longest common
5 1 CO3 K3 Talk,
subsequence
Demo
Chalk &
Matrix-chain
6 1 CO3 K3 Talk,
multiplication
Demo
Travelling Chalk &
7 Salesperson 1 CO3 K3 Talk,
Problem Demo
Chalk &
8 Knapsack Problem 1 CO3 K3 Talk,
Demo
Chalk &
9 Memory functions 1 CO3
Talk
Activity Based Learning
Unit III
Activity Based Learning
Word Hunt
Activity Based Learning
Activity Link
1 Dynamic Programming 23
2 Principle of Optimality 23
8 Matrix-chain multiplication 44
Floyd’s algorithm is an algorithm for finding shortest paths for all pairs in a weighted
connected graph (undirected or directed) with (+/-) edge weights.
We can generate the distance matrix with an algorithm that is very similar to
Warshall’s algorithm is called Floyd’s algorithm.
Floyd’s algorithm computes the distance matrix of a weighted graph with n vertices
through a series of n × n matrices: D(0), . . . , D(k−1), D(k), . . . , D(n)
The element dij(k) in the ith row and the jth column of matrix D(k) (i, j = 1, 2, . . . , n, k
= 0, 1,. . . , n) is equal to the length of the shortest path among all paths from the
ith vertex to the jth vertex with each intermediate vertex, if any, numbered not
higher than k.
The series starts with D(0), which does not allow any intermediate vertices in its
paths; hence, D(0) is simply the weight matrix of the graph.
As in Warshall’s algorithm, compute all the elements of each matrix D(k) from its
immediate predecessor D(k−1).
The last matrix in the series, D(n), contains the lengths of the shortest paths
among all paths that can use all n vertices as intermediate and hence is
nothing other than the distance matrix.
Let dij(k) be the element in the ith row and the jth column of matrix D(k). This
means that dij(k) is equal to the length of the shortest path among all paths
from the ith vertex vi to the jth vertex vj with their intermediate vertices
numbered not higher than k.
Unit III DYNAMIC PROGRAMMING
The length of the shortest path can be computed by the following recurrence:
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.
Let the vertex ‘s’ is the source, and ‘t’ the sink. Let c (i, j) be the cost of edge <i, j>. The cost
of a path from ‘s’ to ‘t’ is the sum of the costs of the edges on the path. The multistage graph
problem is to find a minimum cost path from ‘s’ to ‘t’. Each set Vi defines a stage in the
graph. Because of the constraints on E, every path from ‘s’ to ‘t’ starts in stage 1, goes to
stage 2, then to stage 3, then to stage 4, and so on, and eventually terminates in stage k.
Algorithm Fgraph(G, k, n, p)
// The input is a k-stage graph G = (V, E) with n vertices indexed in order or stages.
//E is a set of edges and c [i,j] is the cost of (i, j).
//p [1 : k] is a minimum cost path.
{
cost [n] :=0.0;
for j:= n - 1 to 1 step – 1 do
// compute cost[j] let r be a vertex such that (j, r) is an edge of G and c [j, r] + cost
[r] is minimum;
cost [j] := c [j, r] + cost[r]; d [j] :=r:
The multistage graph problem can also be solved using the backward approach. Let
bp(i, j) be a minimum cost path from vertex s to j vertex in Vi. Let B cost(i, j) be
the cost of bp(i, j).
Unit III DYNAMIC PROGRAMMING
From the backward approach we obtain:
EXAMPLE1:
Find the minimum cost path from s to t in the multistage graph of five stages shown below.
Do this first using forward approach and then using backward approach.
FORWARD APPROACH:
We use the following equation to find the minimum cost path from s to t: cost
(i, j) = min {c (j, l) + cost (i + 1,l)} l in Vi +1 <j, l>in E
Unit III DYNAMIC PROGRAMMING
Step 1
Step 1 uses the forwarded approach (cost (5,12) = 0 ).
Here, 5 represents the stage number and 12 represents a node in that stage. Since
there are no outgoing edges from vertex 12, the cost is 0.
Step 2
cost(4,9)=c(9,12)=4 , d[4,9]=12
cost(4,10)=c(10,12)=2, d[4,10]=12
cost(4,11)=c(11,12)= 5, d[4,11]=12
Step 3
cost(3,6)=min{c(6,9)+cost(4,9),c(6,10)+cost(4,10)}
min{6+4,5+2}
min{10,7}=7
cost(3,6)=7, d[3,6]=10
cost(3,7)=min{c(7,9)+cost(4,9),c(7,10)+cost(4,10} = min{4+4, 3+2}
min{8, 5}=5
cost(3,7)=5, d[3,7]=10
cost(3,8)=min{c(8,10)+cost(4,10),c(8,11)+cost(4,11)}=min{5+2,6+5}
min{7,11}=7
cost(3,8)=7, d[3,8]=10
Step 4
cost(2,2)=min{c(2,6)+cost(3,6),c(2,7)+cost(3,7),c(2,8)+cost(3,8)} min{4+7,2+5,1+7}
min{11,7,8}=7
cost(2,2)=7, d[2,2]=7
cost(2,3)=min{c(3,6)+cost(3,6),c(3,7)+cost(3,7)} min{2+7,7+5}
min{9,12}=9
cost(2,3)=9, d[2,3]=6
cost(2,4)=c(4,8)+cost(3,8)=11+7=18
cost(2,4)=18, d[2,4]=8
cost(2,5)=min{c(5,7)+cost(3,7),c(5,8)+cost(3,8)}=min{11+5,8+7}=min{16,15}=15
cost(2,5)=15 d(2,5)=8
Unit III DYNAMIC PROGRAMMING
Step-5
cost(1,1)=min{c(1,2)+cost(2,2),c(1,3)+cost(2,3),c(1,4)+cost(2,4),c(1,5)+cost(2,5)} min
{9+7,7+9,3+18,2+15}
min {16,16,21,17}=16
cost(1,1)=16, d[1,1]=2 or 3
To find the path
d[1,1]=2 or 3
d[2,2]=7 or d[2,3]=6
d[3,7]=10 or d[3,6]=10
d[4,10]=12
So the path is 1, 2, 7, 10, 12 with cost = 16 or 1,3,6,10,12 with cost = 16
Backward Approach
Step 1
Bcost(1,1)=0
Step 2
Bcost(2,2)=c(1,2)+bcost(1,1)=9+0=9 d(2,2)=1
Bcost(2,3)=c(1,3)+bcost(1,1)=7+0=7 d(2,3)=1
Bcost(2,4)=c(1,4)+bcost(1,1)=3+0=3 d(2,4)=1
Bcost(2,5)=c(1,5)+bcost(1,1)=2+0=2 d(2,5)=1
Step 3
Bcost(3,6)=min(Bcost(2,2)+c(2,6),Bcost(2,3)+c(3,6)} = min{9+4,7+2}=min{13,9}=9
Bcost(3,6)=9 d(3,6)=3
Bcost(3,7)=min(Bcost(2,2)+c(2,7),Bcost(2,3)+c(3,7),Bcost(2,5)+c(5,7)}
=min{9+2,7+7,2+11}=min{11,14,13}=11
Bcost(3,7)=11 d(3,7)=2
Bcost(3,8)=min(Bcost(2,2)+c(2,8),Bcost(2,4)+c(4,8),Bcost(2,5)+c(5,8)}
=min{9+1,3+11,2+8}=min{10,14,10}=10
Bcost(3,8)=10 d(3,8)=2 or 5
Step 4
Bcost(4,9)=min{Bcost(3,6)+c(6,9),Bcost(3,7)+c(7,9)}
=min(9+6,11+4}=min{15,15}=15
Bcost(4,9)=15 d(4,9)=6 or 7
Unit III DYNAMIC PROGRAMMING
Bcost(4,10)=min{Bcost(3,6)+c(6,10),Bcost(3,7)+c(7,10),Bcost(3,8)+c(8,10)}
=min(9+5,11+3,10+5}=min{14,14,15}=14
Bcost(4,10)=14 d(4,10)=6 or 7
Bcost(4,11)=Bcost(3,8)+c(8,11)
=10+6=16
Bcost(4,11)=16 d(4,11)=8
Step 5
Bcost(5,12)=min{Bcost(4,9)+c(9,12),Bcost(4,10)+c(10,12),Bcost(4,11)+c(11,12)}
=min{15+4,14+2,16+5}=min{19,16,21}=16
Bcost(5,12)=16 d(5,12)=10
d(5,12)=10
d(4,10)=6 or 7
d(3,6)=3 or d(3,7)=2
d(2,3)=1 or d(2,2)=1
So the path is 1, 3, 6, 10, 12 with cost = 16 or path is 1, 2, 7, 10, 12 with cost =16
A binary search tree is one of the most important data structures in computer
science. One of its principal applications is to implement a dictionary, a set of
elements with the operations of searching, insertion, and deletion.
Figure 3.2.Two out of 14 possible binary search trees with keys A, B, C, and D.
Consider four keys A, B, C, and D to be searched for with probabilities 0.1, 0.2, 0.4, and
0.3, respectively. Figure 3.6 depicts two out of 14 possible binary search trees
containing these keys.
The average number of comparisons in a successful search in the first of these trees
is 0.1 x 1+ 0.2 x 2 + 0.4 x 3+ 0.3 x 4 = 2.9, and for the second one it is
Unit III DYNAMIC PROGRAMMING
0.1 x 2 + 0.2x 1+ 0.4 x 2 + 0.3 x 3= 2.1.
Neither of these two trees is optimal.
The total number of binary search trees with n keys is equal to the nth Catalan
number,
c(n)=(2n)!/(n+1)!n!
Let a1, . . . , an be distinct keys ordered from the smallest to the largest
and let p1, . . . pn be the probabilities of searching for them. Let C(i, j) be the
smallest average number of comparisons made in a successful search in a binary
search tree Ti j
made up of keys ai, . . . , aj, where i, j are some integer indices, 1≤ i
≤ j ≤ n.
Consider all possible ways to choose a root ak among the keys ai, . . . , aj . For such
k−1
a binary search tree (Figure 3.7), the root contains key ak, the left subtree Ti
j
contains keys ai, . . . , ak−1 optimally arranged, and the right subtree T k+1 contains
keys ak+1, . . ., aj also optimally arranged.
If we count tree levels starting with 1 to make the comparison numbers equal the keys’
levels, the following recurrence relation is obtained:
Figure 3.3 Binary search tree (BST) with root ak and two optimal binary search
subtrees Ti k−1 and T k+1 j.
Unit III DYNAMIC PROGRAMMING
The two-dimensional table in Figure 3.6.3 shows the values needed for
computing C(i, j). They are in row i and the columns to the left of column j and in
column j and the rows below row i. The arrows point to the pairs of entries whose
sums are computed in order to find the smallest one to be recorded as the value of
C(i, j). This suggests filling the table along its diagonals, starting with all zeros
on the main diagonal and given probabilities Pi, 1≤ i ≤ n, right above it and moving
toward the upper right corner.
The algorithm’s space efficiency is clearly quadratic, ie, : Θ(n3); the time
efficiency of this version of the algorithm is cubic. It is Possible to reduce the
running time of the algorithm to Θ(n2) by taking advantage of monotonicity of
entries in the root table, i.e., R[i,j] is always in the range between R[i,j-1] and
R[i+1,j].
Key A B C D
Probability 0.1 0.2 0.4 0.3
Unit III DYNAMIC PROGRAMMING
Thus, out of two possible binary trees containing the first two keys, A
and B, the root of the optimal tree has index 2 (i.e., it contains B), and the
average number of comparisons in a successful search in this tree is 0.4.
Thus, the average number of key comparisons in the optimal tree is equal to 1.7.
Since R(1,4) = 3, the root of the optimal tree contains the third key, i.e., C. Its left
subtree is made up of keys A and B, and its right subtree contains just key D. To
find the specific structure of these subtrees, we find first their roots by
consulting the root table again as follows. Since R(1, 2) = 2, the root of
the optimal tree containing A and B is B, with A being its left child (and the
root of the one node.
Tree: R(1, 1) = 1). Since R(4, 4) = 4, the root of this one-node optimal tree is its only
key D. Figure 3.10 presents the optimal tree in its entirety.
Figure 3.5 Optimal binary search tree for the above example.
Unit III DYNAMIC PROGRAMMING
Assume that all the weights and the knapsack capacity are
positive integers; the item values do not have to be integers.
0 / 1 knapsack problem means, the chosen item should be either null or whole.
Recurrence relation that expresses a solution to an instance of the
knapsack problem:
Let us consider an instance defined by the first i items, 1≤ i ≤ n, with
weights w1, . . . , wi, values v1, . . . , vi , and knapsack capacity j, 1 ≤ j ≤ W.
Let F(i, j) be the value of an optimal solution to this instance, i.e., the
value of the most valuable subset of the first i items that fit into the knapsack of
capacity j.
We can divide all the subsets of the first i items that fit the knapsack of capacity
j into two categories: those that do not include the ith item and those that do.
Note the following:
1.Among the subsets that do not include the ith item, the value of an optimal
subset is, by definition, F(i − 1, j).
Among the subsets that do include the ith item (hence, j – wi ≥ 0), an
optimal subset is made up of this item and an optimal subset of the first i − 1 items
that fits into the knapsack of capacity j − wi. The value of such an optimal subset
is vi + F(i − 1, j − wi).
Thus, the value of an optimal solution among all feasible subsets of the first I
items is the maximum of these two values. Of course, if the ith item does not
fit into the knapsack, the value of an optimal subset selected from the first i items
is the same as the value of an optimal subset selected from the first i − 1
items.
Unit III DYNAMIC PROGRAMMING
Goal is to find F(n, W), the maximal value of a subset of the n given items that fit into the
knapsack of capacity W, and an optimal subset itself.
For F(i, j),compute the maximum of the entry in the previous row and the same
column and the sum of vi and the entry in the previous row and wi columns to the
left. The table can be filled either row by row or column by column.
The maximal value is F(4, 5) = $37. We can find the composition of an optimal
subset by backtracing (Back tracing finds the actual optimal subset. i.e.
solution), the computations of this entry in the table. Since F(4, 5) > F(3, 5), item
4 has to be included in an optimal solution along with an optimal subset for
filling 5 − 2 = 3 remaining units of the knapsack capacity.
Similarly, since F(1, 2) > F(0, 2), item 1 is the final part of the optimal
solution {item 1, item 2, item 4}. Solving an instance of the knapsack problem by
the dynamic programming algorithm.
Capacity j
i 0 1 2 3 4 5
0 0 0 0 0 0 0
w1 = 2, v1 = 12 1 0 0 12 12 12 12
w2 = 1, v2 = 10 2 0 10 12 22 22 22
w3 = 3, v3 = 20 3 0 10 12 22 30 32
w4 = 2, v4 = 15 4 0 10 15 25 30 37
Memory Functions
The direct top-down approach to finding a solution to such a recurrence leads to an
algorithm that solves common subproblems more than once and hence is very
inefficient.
The bottom up fills a table with solutions to all smaller subproblems, but each of
them is solved only once. An unsatisfying aspect of this approach is that
solutions to some of these smaller subproblems are often not necessary for getting a
solution to the problem given.
Unit III DYNAMIC PROGRAMMING
Since this drawback is not present in the top-down approach, it is natural to try to
combine the strengths of the top-down and bottom-up approaches. The goal is
to get a method that solves only subproblems that are necessary and does so only
once. Such a method exists; it is based on using memory functions.
This method solves a given problem in the top-down manner but, in addition,
maintains a table of the kind that would have been used by a bottom-up
dynamic programming algorithm.
Initially, all the table’s entries are initialized with a special “null” symbol to
indicate that they have not yet been calculated. Thereafter, whenever a new
value needs to be calculated, the method checks the corresponding entry in the
table first: if this entry is not “null,” it is simply retrieved from the table;
otherwise, it is computed by the recursive call whose result is then recorded in the
table. The following algorithm implements this idea for the knapsack problem.
After initializing the table, the recursive function needs to be called with i = n
(the number of items) and j = W (the knapsack capacity).
ALGORITHM MFKnapsack(i, j )
//Implements the memory function method for the knapsack problem
//Input: A nonnegative integer i indicating the number of the first items being
considered
// and a nonnegative integer j indicating the knapsack capacity
//Output: The value of an optimal feasible subset of the first i items
//Note: Uses as global variables input arrays Weights [1..n], Values[1..n],
// and table F[0..n, 0..W ] whose entries are initialized with −1’s except for
// row 0 and column 0 initialized with 0’s
if F[i, j ]< 0
if j <Weights[i]
value←MFKnapsack(i − 1, j)
else
value←max(MFKnapsack(i − 1, j),
Values[i]+ MFKnapsack(i − 1, j −Weights[i]))
F[i, j ]←value
return F[i, j ]
Unit III DYNAMIC PROGRAMMING
EXAMPLE 2 Let us apply the memory function method to the instance considered in
Example 1.
Capacity j
I 0 1 2 3 4 5
0 0 0 0 0 0 0
w1 = 2, v1 = 12 1 0 0 12 12 12 12
w2 = 1, v2 = 10 2 0 - 12 22 - 22
w3 = 3, v3 = 20 3 0 - - 22 - 32
w4 = 2, v4 = 15 4 0 - - - - 37
Only 11 out of 20 nontrivial values (i.e., not those in row 0 or in column 0) have been
computed. Just one nontrivial entry, V (1, 2), is retrieved rather than being
recomputed. For larger instances, the proportion of such entries can be significantly
larger.
Subsequence
Let us consider a sequence S = < s1, s2, s3, s4, …,sn>.
A sequence Z = < z1, z2, z3, z4, …,zm> over S is called a subsequence of S, if and only if
Common Subsequence
Suppose, X and Y are two sequences over a finite set of elements. We can say that Z is a
common subsequence of X and Y, if Z is a subsequence of both X and Y.
Unit III DYNAMIC PROGRAMMING
By Dynamic Programming:
Let X = < x1, x2, x3,…, xm> and Y = < y1, y2, y3,…, yn > be the sequences. To compute
the length of an element the following algorithm is used.
In this procedure, table C[m, n] is computed in row major order and another
table B[m,n] is computed to construct optimal solution.
m := length(X)
n := length(Y)
for i = 1 to m do
C[i, 0] := 0
for j = 1 to n do
C[0, j] := 0
for i = 1 to m do
for j = 1 to n do
if xi = yj
C[i, j] := C[i - 1, j - 1] + 1
B[i, j] := ‘D’
else
if C[i -1, j] ≥ C[i, j -1]
C[i, j] := C[i - 1, j]
B[i, j] := ‘U’
else
C[i, j] := C[i, j - 1]
B[i, j] := ‘L’
return C and B
Unit III DYNAMIC PROGRAMMING
if i = 0 and j = 0
return
if B[i, j] = ‘D’
Print-LCS(B, X, i-1, j-1)
Print(xi)
else if B[i, j] = ‘U’
Print-LCS(B, X, i-1, j)
else
Print-LCS(B, X, i, j-1)
To populate the table, the outer for loop iterates m times and the inner for loop
iterates n times.
Hence, the complexity of the algorithm is O(m, n), where m and n are the length of
two strings.
Example
In this example, we have two strings X = BACDB and Y = BDCB to find the longest
common subsequence.
Following the algorithm LCS-Length-Table-Formulation (as stated above), we have
calculated table C (shown on the left hand side) and table B (shown on the right hand
side).
In table B, instead of ‘D’, ‘L’ and ‘U’, we are using the diagonal arrow, left arrow and up
arrow, respectively. After generating table B, the LCS is determined by function LCS-
Print. The result is BCB.
Unit III DYNAMIC PROGRAMMING
Input: Matrices Ap×q and Bq×r (with dimensions p×q and q×r)
MATRIX-MULTIPLY(Ap×q , Bq×r)
1. for i ← 1 to p
2. for j ← 1 to r
3. C[i, j] ← 0
4. for k ← 1 to q
5. C[i, j] ← C[i, j] + A[i, k] · B[k, j]
6. return C
Unit III DYNAMIC PROGRAMMING
First compute matrices A1…k and Ak+1…n ; then multiply them to get the
final matrix A1…n
Key observation: parenthesizations of the sub chains A1A2…Ak and
Ak+1Ak+2…An must also be optimal if the parenthesization of the
chain A1A2…An is optimal (why?)
That is, the optimal solution to the problem contains within it the optimal
solution to sub problems.
Unit III DYNAMIC PROGRAMMING
Let m[i, j] be the minimum number of scalar multiplications necessary to compute Ai....j .
Suppose the optimal parenthesization of Ai..j splits the product between Ak and
Ak+1 for some integer k where i ≤ k < j
But… optimal parenthesization occurs at one value of k among all possible i ≤ k < j
Check all these and select the best one.
The algorithm computes the minimum-cost table m and the split table s
The optimal solution can be constructed from the split table s.
Each entry s[i, j ]=k shows where to split the product Ai Ai+1 …… Aj for the minimum
cost show how to multiply this matrix chain optimally.
Introduction to TSP:
In the TSP, given a set of cities and the distance between each pair of cities, a
salesman needs to choose the shortest path to visit every city exactly once and return
to the city from where he started.
Let’s take an example to understand the TSP in more detail:
Unit III DYNAMIC PROGRAMMING
Here, the nodes represent cities, and the values in each edge denote the distance between
one city to another. Let’s assume a salesman starts the journey from the city A. According
to TSP, the salesman needs to travel all the cities exactly once and get back to the city A by
choosing the shortest path. Here the shortest path means the sum of the distance between
each city travelled by the salesman, and it should be less than any other path.
With only 5 cities, the problem already looks quite complex. As the graph in
the example is a complete graph, from each city, the salesman can reach any other cities in
the graph. From each city, the salesman needs to choose a route so that he doesn’t have to
revisit any city, and the total distance travelled should be minimum.
Let’s discuss an algorithm to solve the TSP problem.
Dynamic Programming Approach for Solving TSP:
In this algorithm, take a subset N of the required cities needs to be visited,
distance among the cities and starting city s as inputs. Each city is identified by unique city
id like (1,2,3,4…n).
Algorithm: Traveling-Salesman-Problem
C ({1}, 1) = 0
for s = 2 to n do
for all subsets S Є {1, 2, 3, … , n} of size s and containing 1
C (S, 1) = ∞
for all j Є S and j ≠ 1
C (S, j) = min {C (S – {j}, i) + d(i, j) for i Є S and i ≠ j}
Return minj C ({1, 2, 3, …, n}, j) + d(j, i)
Initially, all cities are unvisited, and the visit starts from the city s. We assume
that the initial travelling cost is equal to 0. Next, the TSP distance value is calculated based
on a recursive function. If the number of cities in the subset is two, then the recursive
function returns their distance as a base case.
On the other hand, if the number of cities is greater than 2, then we’ll
calculate the distance from the current city to the nearest city, and the minimum distance
among the remaining cities is calculated recursively.
Unit III DYNAMIC PROGRAMMING
Finally, the algorithm returns the minimum distance as a TSP solution. Use a
dynamic approach to calculate the cost function Cost(). Using recursive calls, calculate the
cost function for each subset of the original problem.
Conclusion:
TSP is a popular NP-Hard problem, but depending on the size of the input cities, it is
possible to find an optimal or a near-optimal solution using various algorithms.
Illustrate the steps to solve travelling salesman problem with the following example:
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
S=Φ 4 8 8 9 0
Cost(2,Φ,1)=d(2,1)=5
Cost(3,Φ,1)=d(3,1)=6
Cost(4,Φ,1)=d(4,1)=8
Unit III DYNAMIC PROGRAMMING
Cost( i, s)=min{Cost(j, s-(j))+d[ i, j]}
S=1
Cost(2,{3},1)=d[2,3]+Cost(3,Φ,1)=9+6=15
Cost(2,{4},1)=d[2,4]+Cost(4,Φ,1)=10+8=18
Cost(3,{2},1)=d[3,2]+Cost(2,Φ,1)=13+5=18
Cost(3,{4},1)=d[3,4]+Cost(4,Φ,1)=12+8=20
Cost(4,{3},1)=d[4,3]+Cost(3,Φ,1)=9+6=15
Cost(4,{2},1)=d[4,2]+Cost(2,Φ,1)=8+5=13
S=2
Cost(2,{3,4},1)=min{d[2,3]+Cost(3,{4},1), d[2,4]+Cost(4,{3},1)}
Cost(3,{2,4},1)=min{d[3,2]+Cost(2,{4},1), d[3,4]+Cost(4,{2},1)}
Cost(4,{2,3},1)=min{d[4,2]+Cost(2,{3},1), d[4,3]+Cost(3,{2},1)}
S=3
In the dynamic algorithm for TSP, the number of possible subsets can be at most N*2N.
Each subset can be solved in O(N)times. Therefore, the time complexity of this algorithm
would be O(N2*2N).
Conclusion
TSP is a popular NP-Hard problem, but depending on the size of the input cities, it is
possible to find an optimal or a near-optimal solution using various algorithms.
Assignments
Unit 3
Assignments
Sl. Knowledge
Question CO#
No. Level
Solve the multi-stage graph problem using forward and
backward approach
1. CO3 K3
2. CO3 K3
overlapping subprograms. The smaller subprograms are solved only once and recording the
results in a table from which the solution to the original problem is obtained.
A Multistage graph is a directed graph in which the nodes can be divided into a set of
stages such that all edges are from a stage to next stage only. A multistage graph G = (V,
E) is a directed graph where vertices are partitioned into k (where k > 1) number of
disjoint subsets S = {s1,s2,…,sk}such that edge (u, v) is in E, then u Є si and v Є s1 + 1
for some subsets in the partition and |s1| = |sk| = 1. The vertex s Є s1 is called the source
and the vertex t Є sk is called sink.
4. Write the difference between the Greedy method and Dynamic programming.
(CO3,K1)
Greedy method
i. Only one sequence of decision is generated.
ii. It does not guarantee to give an optimal solution always.
Dynamic programming
● Many numbers of decisions are generated.
● It definitely gives an optimal solution always
Floyd(W[1..n,1..n])
//Implements Floyd’s algorithm for the all-pair shortest–path problem //Input The
weight matrix W of a graph
//Output The distance matrix of the shortest paths’ lengths D W
for k <- 1 to n do
for i <- 1 to n do
for j <- 1 to n do
D[i,j] = min{D[i,j], D[i,k] + D[k,j]}
A binary search tree which has minimal average comparisons for all its search keys is
called as optimal binary search tree.
Floyd’s algorithm is otherwise called as all pair shortest path algorithm. It is used to find
the shortest path between every pair of vertices.
15.What are the different approaches used to solve the multi-stage graph.
(CO3,K1)
Forward approach and Backward approach.
The nodes represent cities, and the values in each edge denote the distance
between one city to another. A salesman starts the journey from the city A.
According to TSP, the salesman needs to travel all the cities exactly once and get
back to the city A by choosing the shortest path. Here the shortest path means the
sum of the distance between each city travelled by the salesman, and it should be
less than any other path.
O(n3)
PART - A Questions
22. What is the use of root table? (CO3,K1)
Root table is used to find the root in the optimal binary search tree and subtrees.
O(nW)
24. How Floyd’s algorithm finds the shortest path between pair of vertices?
(CO3,K1)
25. Does Floyd’s algorithm work well on negative edge graph? (CO3.K1)
Floyd’s all pairs shortest paths algorithm works for graphs with negative edge weights
because the correctness of the algorithm does not depend on edge's weight being non-
negative.
30. What is the formula used for finding the average number of comparisons in
OBST? (CO3,K1)
Part B – Questions
Part-B Questions
Q. Questions CO K Level
No. Level
9 CO3 K3
10 CO3 K3
Part-B Questions
Q. Questions CO K Level
No. Level
Write the Huffman’s Algorithm. Construct the
Huffman’s tree for the following data and obtain its
Huffman’s Code.
11 CO3 K3
12 CO3 K3
13 CO3 K3
Supportive online
Certification courses
(NPTEL, Swayam,
Coursera, Udemy, etc.,)
Supportive Online Courses
3.Document Distance Algorithms- to identify the extent of similarity between two text
documents used by Search engines like Google, Wikipedia, Quora, and other websites
5. Databases caching common queries in memory: through dedicated cache tiers storing
data to avoid DB access, web servers store common data like configuration that can be
used across requests. Then multiple levels of caching in code abstractions within every
single request that prevents fetching the same data multiple times and save CPU cycles by
avoiding recomputation. Finally, caches within your browser or mobile phones that keep the
data that doesn't need to be fetched from the server every time.
6. Git merge. Document diffing is one of the most prominent uses of LCS.
8. Genetic algorithms.
Content Beyond Syllabus
Unit III Content Beyond Syllabus
WARSHALL’S ALGORITHM
A directed graph (or digraph) is a graph, or set of vertices connected by edges, where
the edges have a direction associated with them.
An Adjacency matrix A = {aij} of a directed graph is the boolean matrix that has 1 in its
ith row and jth column if and only if there is a directed edge from the ith vertex to the jth
vertex.
The transitive closure of a directed graph with n vertices can be defined as the n x n
boolean matrix T = {tij }, in which the element in the ith row and the jth column is 1 if
there exists a nontrivial path (i.e., directed path of a positive length) from the ith
vertex to the jth vertex; otherwise, tij is 0.
The transitive closure of a digraph can be generated with the help of depth-first
search or breadth-first search. Every vertex as a starting point yields the transitive
closure for all.
FIGURE 3.13.1 (a) Digraph. (b) Its adjacency matrix. (c) Its transitive closure.
Unit III Content Beyond Syllabus
The element rij(k) in the ith row and jth column of matrix R(k) (i, j = 1, 2, . .
. , n, k = 0, 1, . . ., n) is equal to 1 if and only if there exists a directed path of a
positive length from the ith vertex to the jth vertex with each intermediate vertex,
if any, numbered not higher than k.
R(1) contains the information about paths that can use the first vertex as
intermediate. it may contain more 1’s than R(0).
The last matrix in the series, R(n), reflects paths that can use all n vertices of
the digraph as intermediate and hence is nothing other than the digraph’s
transitive closure.
In general, each subsequent matrix in series has one more vertex to use as
intermediate for its paths than its predecessor.
The last matrix in the series, R(n), reflects paths that can use all n vertices of
the digraph as intermediate and hence is nothing other than the digraph’s
transitive closure.
All the elements of each matrix R(k) is computed from its immediate predecessor
R(k−1). Let r (k)
, the element in the ith row and jth column of matrix R(k), be
equal to 1. This means that there exists a path from the ith vertex vi to the
jth vertex vj with each intermediate vertex numbered not higher than k.
The first part of this representation means that there exists a path from v i to vk
(k−1)
with each intermediate vertex numbered not higher than k − 1 ( ihence, r =
1), and the second part means that there exists a path from v k to vj with each
intermediate vertex numbered not higher than k − 1 (hence, r kj(k−1) = 1).
Thus the following formula generates the elements of matrix R (k) from the
elements of matrix R(k−1):
Warshall’s algorithm’s time efficiency is only Θ(n3). Space efficiency is Θ(n2). i.e matrix size.
Application of Warshall’s algorithm to the digraph shown. New 1’s are in bold.
Assessment Schedule
(Proposed Date & Actual
Date)
Assessment Schedule
0 2 10 3 11 13
2 0 13 4 11 15
10 13 0 9 2 3
3 4 9 0 8 12
11 11 2 8 0 4
13 15 3 12 4 0
Given a set of positive integers, check if it can be divided into two subsets with equal sum.
S = {3, 1, 1, 2, 2, 1}
3. Bus Stop Prize (CO3,K6)
As a promotion to promote mass transit, Gotham city has awarded you a chance to win
some great prizes by riding one of their busses. The city bus has n stops on its schedule
(1 ≤ n ≤ 100, 000 – it’s a very long bus route). At each stop there is a prize that
has integer monetary value of
−100 ≤ pi ≤ 100. Yes, some prizes are negative. You get to pick the stop where you
board the bus (and hence win that first prize). Then you win each prize along the route
for each stop that you are still on the bus. You select some other stop to deboard the bus
(and win that prize too). Your goal is to determine where you should board and deboard
the bus in order to maximize your total prize winnings. Note that you may be willing to
visit a few negative stops if by doing so you reach bigger prizes down the road. You are
given the n prize winnings ahead of time to help you make your decision: p1, . . . , pn.
Develop a dynamic programming solution to determine the optimal point of boarding and
deboarding given a sequence of n prizes.
Read data from stdin. Your first input will be integer n followed by the integers 1..n
indicating the prizes at each bus stop. An example input is given below:
10
4 -1 5 -4 2 6 -8 -2 9 -3
Disclaimer:
This document is confidential and intended solely for the educational purpose of RMK Group of
Educational Institutions. If you have received this document through email in error, please notify the
system manager. This document contains proprietary information and is intended only to the
respective group / learning community as intended. If you are not the addressee you should not
disseminate, distribute or copy through e-mail. Please notify the sender immediately by e-mail if you
have received this document by mistake and delete this document from your system. If you are not
the intended recipient you are notified that disclosing, copying, distributing or taking any action in
reliance on the contents of this information is strictly prohibited.