2023 solved
2023 solved
An algorithm design technique means a unique approach or mathematical method for creating
algorithms and solving problems. A suitable algorithm design method is used based on the
nature of the problem. An algorithm created with the right design technique can solve the
problem much more efficiently with respect to the computational power required.
A divide-and-conquer algorithm recursively breaks down a problem into two or more sub-
problems of the same or related type, until these become simple enough to be solved directly.
In a weighted graph G=(V,E),the weight W of a sub graph is the sum of the weights of the
edges in the sub graph . A minimum spanning tree for a weighted graph is a spanning tree
with a minimum weight.
Hashing is the process of mapping keys to their appropriate locations in the hash table. It is
the effective technique of searching values in an array or in hash table.
Hashing in data structure is a two step process
I. Hash function converts item into a small integer or hash value. the integer is
used as index to store the original data.
II. It stores data in a hash table. we use hash key to locate data quickly.
State any two differences between greedy algorithm and dynamic programming.
Backtracking is a technique use to solve problems that involves searching through large
complex search space. The basic idea of backtracking is to try all possible solutions to a
problem and then backtrack if a solution is found to be invalid.
Brach and Bound technique: this technique involves dividing the search space of a problem
into smaller sub problems and using bounds to eliminate sub problems that cannot contain the
optimal solution.
PART-B
The step count method is one of the method to analyze the algorithm. In this method, we
count number of times one instruction is executing. A step is any computational unit that is
independent of selected characteristics.
}
return sum; //the step count for return=1
} //total steps= 2n +3
Ex 1 2 and 3 Permutations
Algorithm MFKanpsack(i,j)
Input: a non-negative integer I indicating the number of the first items being considered and
non negative j indicating the knapsack’s capacity.
Output: The values of an optimal feasible subset of first i items.
Note: the weights are w[1 ......n] ,values are v[0.....n, 0.......w]
Initialise first row and first column entries with zero and remaining entries with -1.
This is a method to multiply two numbers .this works by repeatedly dividing one of the
numbers by 2 and doubling the other number until the first number becomes 1. The final
result is the sum of the second number in each step where the first number is odd.
Algorithm
Step 1: Write the two numbers to be multiplied at the top of two columns.
Step 2 : In the left column repeatedly the last number by 2 ,discarding any remainder and write
the result below. Stop when number reaches 1.
Step 3: In the right column ,repeatedly double the last number and write the result below.
Step 4: Cross out all rows where the number in the left column is even.
Step 5: Sum up the remaining numbers in the right column. The result is the product of the
initial two numbers.
Example: Multiplication of 18 by 7.
Column 1 Column2
18 7
9 14
4 28(cross out)
2 56(cross out)
1 112
Sum of the remaining numbers in the right column 7+14+112=133 . so 18* 7=133
11. Explain Decision Trees for Insertion Sort Algorithm.
In the insertion sort ,the decision trees reveal all possible key comparisons sequences
for 3 distinct numbers.
The length of that path represented the number of key comparisons performed by
sorting algorithm.
Given a decision tree the height of the tree represent the longest length of a root to
leaf path.
When we come to leaf sorting algorithm has determined the sorted order.
PART-C
12. Explain in detail the mathematical analysis of algorithm for Matrix Multiplication.
3. Reason:
As each row of one matrix is multiplied with each column of the other with
respective elements ,three for loops : one for traversing the input matrices and the
remaining two for initialising the output matrix and traversing all the three
matrices are to be used.
4. Relation for number of times the basic operation is used.
T(n)=∑𝑛−1
𝑖=0 ∑𝑛−1
𝑗=0 ∑𝑛−1
𝑘=0 1
5. Basic formula involved in solving the relation
T(n)= ∑𝑛−1
𝑖=0 ∑𝑛−1
𝑗=0 ∑𝑛−1
𝑘=0 1
= ∑𝑛−1
𝑖=0 ∑𝑛−1
𝑗=0 n [sum of ‘n’ 1’s]
𝑛−1
=∑𝑖=0 n2 [sum of ‘n’ n’s]
=n3
Therefore T(n)= n3.
the time complexity in finding the multiplication of two matrices =O(n3)
13. Explain in detail the Strassen’s Matrix Multiplication method. Compare its
14. Apply Wars hall’s algorithm to find the transitive closure of the digraph defined by
the following Adjacency matrix:
0 1 0 0
0 0 1 0
0 0 0 1
0 0 0 0
Solution
0 0 0 1
0 0 0 0
First row and first column values of D0 as such we calculate the remaining values for D1
k =1
i=2 j=2 D1[2,2]= D0[2, 2] or (D0[2, 1] and D(0)[1,2 ]) = 0 or( 0 and 1)= 0 or 0 = 0
i=2 j=3 D1[2,3]= D0[2, 3] or (D0[2, 1] and D(0)[1,3 ]) = 1 or( 0 and 0)= 1or 0 = 1
i=2 j=4 D1[2,4]= D0[2, 4] or (D0[2, 1] and D(0)[1,4 ]) = 0 or( 0 and 0)= 0 or 0 = 0
i=3 j=2 D1[3,2]= D0[3, 2] or (D0[3, 1] and D(0)[1,2]) = 0 or( 0 and 1)= 0 or 0 = 0
i=3 j=3 D1[3,3]= D0[3, 3] or (D0[3, 1] and D(0)[1,3 ]) = 0 or( 0 and 0)= 0 or 0 = 0
i=3 j=4 D1[3,4]= D0[3, 4] or (D0[3, 1] and D(0)[1,4 ]) = 1 or( 0 and 0)= 1 or 0 = 1
i=4 j=2 D1[4 ,2]= D0[4, 2] or (D0[4, 1] and D(0)[1,2 ]) = 0 or( 0 and 1)= 0 or 0 = 0
i=4 j=3 D1[4,3]= D0[4, 3] or (D0[4, 1] and D(0)[1,3 ]) = 0 or( 0 and 0)= 0 or 0 = 0
i=4 j=4 D1[4,4]= D0[4, 4] or (D0[4, 1] and D(0)[1,4 ]) = 0 or( 0 and 0)= 0 or 0 = 0
0 1 0 0
0 0 1 0
D1= 0 0 0 1
0 0 0 0
Second row and second column values of D1 as such we calculate the remaining values
for D2
k=2
i=1 j=1 D2[1,1]= D1[1, 1] or (D1[1, 2] and D(1)[2,1]) = 0 or( 1 and 0)= 0 or 0 = 0
i=1 j=3 D2[1,3]= D1[1, 3] or (D1[1, 2] and D(1)[2,3]) = 0 or( 1 and 1)= 0 or 1 = 1
i=1 j=4 D2[1,4]= D1[1, 4] or (D1[1, 2] and D(1)[2,4]) = 0 or( 1 and 0)= 0 or 0 = 0
i=3 j=1 D2[3,1]= D1[3, 1] or (D1[3, 2] and D(1)[2,1]) = 0 or( 0 and 0)= 0 or 0 = 0
i=3 j=3 D2[3,3]= D1[3, 3] or (D1[3, 2] and D(1)[2,3]) = 0 or( 0 and 1)= 0 or 0 = 0
i=3 j=4 D2[3,4]= D1[3, 4] or (D1[3, 2] and D(1)[2,4]) = 1 or( 0 and 0)= 1 or 0 = 1
i=4 j=1 D2[4,1]= D1[4, 1] or (D1[4, 2] and D(1)[2,1]) = 0 or( 0 and 0)= 0 or 0 = 0
i=4 j=3 D2[4,3]= D1[4, 3] or (D1[4, 2] and D(1)[2,3]) = 0 or( 0 and 1)= 0 or 0 = 0
i=4 j=4 D2[4,4]= D1[4, 4] or (D1[4, 2] and D(1)[2,4]) = 0 or( 0 and 0)= 0 or 0 = 0
0 1 1 0
D2=
0 0 1 0
0 0 0 1
0 0 0 0
Third row and Third column values of D2 as such we calculate the remaining values for
D3
K=3
i=1 j=1 D3[1,1]= D2[1, 1] or (D2[1, 3] and D(2)[3,1]) = 0 or( 1 and 0)= 0 or 0 = 0
i=1 j=2 D3[1,2]= D2[1, 2] or (D2[1, 3] and D(2)[3,2]) = 1or( 1 and 0)= 1 or 0 = 1
i=1 j=4 D3[1,4]= D2[1, 4] or (D2[1, 3] and D(2)[3,4]) = 0 or( 1 and 1)= 0 or 1 = 1
i=2 j=1 D3[2,1]= D2[2, 1] or (D2[2, 3] and D(2)[3,1]) = 0 or( 1 and 0)= 0 or 0 = 0
i=2 j=2 D3[2,2]= D2[2, 2] or (D2[2, 3] and D(2)[3,2]) = 0 or( 1 and 0)= 0 or 0 = 0
i=2 j=4 D3[2,4]= D2[2, 4] or (D2[2, 3] and D(2)[3,4]) = 0 or( 1 and 0)= 0 or 0 = 0
i=4 j=1 D3[4,1]= D2[4, 1] or (D2[4, 3] and D(2)[3,1]) = 0 or( 0 and 0)= 0 or 0 = 0
i=4 j=2 D3[4,2]= D2[4, 2] or (D2[4, 3] and D(2)[3,2]) = 0 or( 0 and 0)= 0 or 0 = 0
i=4 j=4 D3[4,4]= D2[4,4] or (D2[4, 3] and D(2)[3,4]) = 0 or( 0 and 1)= 0 or 0 = 0
D3= 0 1 1 1
0 0 1 0
0 0 0 1
0 0 0 0
Fourth row and Fourth column values of D3 as such we calculate the remaining values
for D4
K=4
i=1 j=1 D4[1,1]= D3[1, 1] or (D3[1, 4] and D(3)[4,1]) = 0 or( 1 and 0)= 0 or 0 = 0
i=1 j=2 D4[1,2]= D3[1, 2] or (D3[1, 4] and D(3)[4,2]) = 1 or( 1 and 0)= 1 or 0 = 1
i=1 j=3 D4[1,3]= D3[1, 3] or (D3[1, 4] and D(3)[4,3]) = 1 or( 1 and 0)= 1 or 0 = 1
i=2 j=1 D4[2,1]= D3[2, 1] or (D3[2, 4] and D(3)[4,1]) = 0 or( 0 and 0)=0 or 0 = 0
i=2 j=2 D4[2,2]= D3[2, 2] or (D3[2, 4] and D(3)[4,2]) = 0 or( 0 and 0)=0 or 0 = 0
i=2 j=3 D4[2,3]= D3[2, 3] or (D3[2, 4] and D(3)[4,3]) = 1 or( 0 and 0)=1 or 0 = 1
i=3 j=1 D4[3,1]= D3[3, 1] or (D3[3, 4] and D(3)[4,1]) = 0 or( 1 and 0)=0 or 0 = 0
i=3 j=2 D4[3,2]= D3[3,2] or (D3[3, 4] and D(3)[4,2]) = 0 or( 1 and 0)=0 or 0 = 0
i=3 j=3 D4[3,3]= D3[3, 3] or (D3[3, 4] and D(3)[4,3]) = 0 or( 1 and 0)=0 or 0 = 0
0 1 1 1
D4 = 0 0 1 0
0 0 0 1
0 0 0 0