QP1
QP1
3. Discuss briefly the heuristics, union by rank and path compression, to improve the running time of
disjoint set data structure
Ans:
o Path Compression(collapsing rule)
Path compression is a way of flattening the structure of the tree whenever Find is used on it.
Since each element visited on the way to a root is part of the same set, all of these visited
elements can be reattached directly to the root.
Example:
Next time we perform Find(6) it will give us the answer in two steps instead of four prior to
the optimisation.
4. Consider the directed acyclic graph G=(V,E) given in the following figure.
6. Why Strassen‘s matrix multiplication algorithm is better than traditional divide and conquer algorithm
for multilpying two square matrices? What is the recurrence for the number of computational steps
taken by Strassen‘s algorithm and its time complexity?
Ans:
Divide and Conquer Matrix multiplication
For multiplying two matrices of size n x n using divide and conquer matrix multiplication
strategy, we make 8 recursive calls, each on a matrix with size n/2 x n/2.
Addition of two matrices take O(n2) time.
Time complexity of divide and conquer matrix multiplication= 8 T(n/2) + O(n2)
= O(n3)
For multiplying two matrices of size n x n using strassen‘s matrix multiplication strategy, we
make 7 matrix multiplications and 10 matrix additions and subtractions
Addition/Subtraction of two matrices takes O(n2) time.
Time complexity = 7 T(n/2) + O(n2) = O(nlog 7) = O(n2.81)
Therefore strassen‘s matrix multiplication strategy is better than traditional divide and
conquer matrix multiplication strategy.
T(n) = b if n<2
7 T(n/2) + c n2 Otherwise
1. Optimal Substructure: Here the problem can be broken down into smaller subproblems, and the
solution to the larger problem can be found by combining solutions to the smaller subproblems.
2. Overlapping Subproblems: This property means that the same subproblems are solved multiple
times in the process of solving the larger problem. Dynamic programming avoids redundant
calculations by storing the solutions to subproblems in a table and reusing them when needed.
4. State Transition: This refers to defining how solutions to larger problems can be built from
solutions to smaller subproblems. It involves defining the recurrence relation or formula that relates the
solution to a problem with the solutions to its subproblems.
Example:
Consider the problem of finding the n-th Fibonacci number, where Fibonacci sequence is defined as
follows: F(0) = 0, F(1) = 1, and F(n) = F(n-1) + F(n-2) for n ≥ 2.
Dynamic programming can be applied to efficiently compute the n-th Fibonacci number using
memoization or tabulation:
- Optimal Substructure: The Fibonacci sequence exhibits optimal substructure because the solution to
the n-th Fibonacci number can be constructed from the solutions to the (n-1)-th and (n-2)-th Fibonacci
numbers.
- Overlapping Subproblems: Without dynamic programming, the naive recursive solution to compute
Fibonacci numbers would result in exponential time complexity due to redundant calculations.
Dynamic programming avoids this by storing the results of solved subproblems.
By applying dynamic programming techniques, the time complexity of computing Fibonacci numbers
can be reduced from exponential to linear or linearithmic, making it much more efficient.
Backtracking uses a Depth first search. Branch and bound uses Depth first search/D
Search/Least cost search.
In backtracking, all the possible solutions are In branch and bound, based on search; bounding
tried. If the solution does not satisfy the values are calculated. According to the bounding
constraint, then we backtrack and look for another values, we either stop there or extend.
solution.
Applications of backtracking are n-Queens Applications of branch and bound are knapsack
problem, Sum of subset. problem, travelling salesman problem, etc.
Backtracking is more efficient than the Branch Branch n bound is less efficient.
and bound.
o There is no polynomial solution to find the Hamiltonian path from s to t in a given graph.
But there is a verification algorithm. So HAMPATh is a NP problem
NP- Hard
If a decision problem X is NP-Hard if every problem in NP is polynomial time reducible to
X.
Y ≤p X for every Y in NP
It means that X is as hard as all problems in NP.
If X can be solved in polynomial time, then all problems in NP can also solved in polynomial
time.
Class NP-Complete
If the problem is NP as well as NP-Hard, then that problem is NP Complete.
Example:
3-CNF-SAT
CLIQUE problem
VERTEX COVER problem
Algorithm findingA_LV(A, n)
{
repeat
{
Randomly choose one element out of n elements
}until(‘a’ is found)
}
The expected number of trials before success is 2.
Therefore the time complexity = O(1)
Algorithm findingA_MC(A, n, k )
{
i=0;
repeat
{
Randomly select one element out of n elements
i=i+1;
}until(i=k or ‘a’ is found);
}
o This algorithm does not guarantee success, but the run time is bounded. The
number of iterations is always less than or equal to k.
o Therefore the time complexity = O(k)
PART B
11.
a) Illustrate best case, average case and worst-case complexity with insertion sort algorithm.
Ans:
Algorithm InsertionSort(A,n)
{
for i=1 to n-1 do
{
j=i
while j>0 and A[j-1] > A[j] do
{
Swap(A[j], A[j-1])
j=j-1
}
}
}
Best case complexity: Best case situation occurs when the array itself is in sorted order. In this
case the while loop will not execute successfully. So the time complexity is proportional to number
of times the for loop will execute. It will execute O(n) time.
The best case time complexity = Ω(n)
Worst case complexity: Worst case situation occurs when the array itself is in reverse sorted
order. In this case the while loop will execute 1+2+3+ . .. .+(n-1)=n(n+1)/2 times.
The time complexity is proportional to number of times the while loop will execute. It will execute
O(n2) time.
The worst case time complexity = O(n2)
Average case complexity: Average case situation occurs when the while loop will iterate half of
its maximum iterations. In this case the while loop will execute [1+2+3+ . .. .+(n-1)]/2=n(n+1)/4
times.
The time complexity is proportional to number of times the while loop will execute. It will execute
O(n2) time.
The average case time complexity = Ɵ(n2)
b) Give the general idea of the substitution method for solving recurrences. Solve the following
recurrence using substitution method. 𝑇(𝑛)=2𝑇(𝑛/2)+𝑛
Ans:
Guess that T(n) = O(n log n)
As per O notation definition T(n) <= c n log n
By mathematical induction
If n=1, T(1) <= c x 1 x log 1 1 <= 0 It is false
If n=2, T(2) <= c x 2 x log 2 2 T(1) + 2 <= 2 c 4 <= 2c It is true
If n=3, T(3) <= c x 3 x log 3 2 T(1) + 3 <= c x 3 x log 3
5 <= 3 c log 3 It is true
This relation is true when n= 2, 3, 4 . . . . ., k
T(k) <= c k log k, where 2<= k <= n
T(n/2) <= c (n/2) log (n/2) 1
2)
n/2k = 1 2k = n k = log2(n)
T(n)= n + n + n + ……
=kn
= log2n xn
= nlog2n
= O (nlog2n)
b) Define Big Oh, Big Omega and Theta notations and illustrate them graphically
Ans:
Asymptotic Notations
It is the mathematical notations to represent frequency count.
Big Oh (O)
The function f(n) = O(g(n)) iff there exists 2 positive constants c and n 0 such that 0
≤ f(n) ≤ c g(n) for all n ≥ n0
It is the measure of longest amount of time taken by an algorithm(Worst case).
It is asymptotically tight upper bound
Omega (Ω)
The function f(n) = Ω (g(n)) iff there exists 2 positive constant c and n0 such that
f(n) ≥ c g(n) ≥ 0 for all n ≥ n0
It is the measure of smallest amount of time taken by an algorithm(Best case).
It is asymptotically tight lower bound
Theta (Ɵ)
The function f(n) = Ɵ (g(n)) iff there exists 3 positive constants c1, c2 and n0 such
that 0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n) for all n ≥ n0
It is the measure of average amount of time taken by an algorithm(Average case).
13.
a) Give Breadth First Search algorithm for graph traversal. Perform its complexity analysis.
Ans:
Algorithm BFS(G, u)
1. Set all nodes are unvisited
2. Mark the starting vertex u as visited and put it into an empty Queue Q
3. While Q is not empty
3.1 Dequeue v from Q
3.2 While v has an unvisited neighbor w
3.2.1 Mark w as visited
3.2.2 Enqueue w into Q
4. If there is any unvisited node x
4.1 Visit x and Insert it into Q. Goto step 3
Complexity
If the graph is represented as an adjacency list
o Each vertex is enqueued and dequeued atmost once. Each queue operation
take O(1) time. So the time devoted to the queue operation is O(V).
o The adjacency list of each vertex is scanned only when the vertex is dequeued.
Each adjacency list is scanned atmost once. Sum of the lengths of all
adjacency list is |E|. Total time spend in scanning adjacency list is O(E).
o Time complexity of BFS = O(V) + O(E) = O(V+ E).
o In a dense graph:
E=O(V2)
Time complexity= O(V) + O(V2) = O(V2)
If the graph is represented as an adjacency matrix
o There are V2 entries in the adjacency matrix. Each entry is checked once.
o Time complexity of BFS = O(V2)
b) Define AVL tree. Construct an AVL tree by inserting the keys: 44, 17, 32, 78, 50, 88, 48, 62, 54
into an initially empty tree. Write clearly the type of rotation performed at the time of each
insertion.
Ans:
14.
a) Give Depth First Serach algorith for graph traversal. Perform its time complexity analysis.
Ans:
Algorithm DFS(G, u)
1. Mark vertex u as visited
2. For each adjacent vertex v of u
2.1 if v is not visited
2.1.1 DFS(G, v)
Algorithm main(G,u)
1. Set all nodes are unvisited.
2. DFS(G, u)
3. For any node x which is not yet visited
3.1 DFS(G, x)
Complexity
If the graph is represented as an adjacency list
Each vertex is visited atmost once. So the time devoted is O(V)
Each adjacency list is scanned atmost once. So the time devoted is O(E)
Time complexity of DFS = O(V + E).
If the graph is represented as an adjacency matrix
There are V2 entries in the adjacency matrix. Each entry is checked once.
Time complexity of DFS = O(V2)
b) Perform DFS traversal on the following graph starting from node A. When multiple nodes are
available for next traversal choose nodes in alphabetical order. Classify the edges of the graph into
different category.
Ans:
Eg.(u,v),(v,y),(y,x),(w,z)
Forward Edge:It is an edge(u,v) such that v is descendant but not part of the DFS tree.
Eg.(u,x)
Backward Edge:It is an edge(u,v) such that v is ancestor of edge u but not part of
theDFS tree.
Eg. (x,v)
Cross Edge:It is an egde which connects two node such that they do not have any
ancestor and descendant relationship between them.
Eg.(w,y)
15.
a) Illustrate the divide and conquer approach by applying 2 way merge sort for the input array:
[15,12,14,17,11,13,12,16]. Write the recurrence for merge sort and give the complexity.
Ans:
b) Apply Dijkstra‘s algorithm for single source shortest path to solve the following graph. Assume the
source as node A.
Ans:
A B C D E F
A 0 ∞ ∞ ∞ ∞ ∞
D 4 ∞ 1 ∞ ∞
B 3 4 5 ∞
C 4 5 ∞
E 5 16
F 12
D D A D E
16.
a) Consider the following instance of Fractional Knapsack problem with 3 objects. The capacity of
the knapsack is 20 units. The weights and profits of the 3 items respectively are represented by the
vectors (w1,w2,w3) = (18,15,10) and (p1,p2,p3)=(25,24,15). Using a greedy strategy compute the
optimal solution to this instance.
Ans:
m = 20
n=3
i { 1, 2, 3 }
P = {25, 24, 15 }
W = {18, 15, 10 }
P/W= {1.39, 1.6, 1.5 }
Total Profit = Σ Pi * Xi
=25x0 + 24x1 + 15x1/2 = 31.5
Solution vector X ={0,1,1/2}
b) Apply Kruskal algorithm to find minimum cost spanning tree for the following graph
Ans:
This is the minimum cost spanning tree and its cost = 37
17.
a) Discuss Floyd-Warshall algorithm for all pair shortest path problem. Solve the following instance
using the algorithm
Ans:
Initially, the adjacency matrix is
Keep the 3rd row and 3rd column and diagonal elements of D₂ assuch
D₃(1,2) = min{ D₂(1,2),D₂(1,3) + D₂(3,2) } = min{ 1, 6+2 } = 1
D₃(2,1) = min{ D₂(2,1),D₂(2,3) + D₂(3,1) } = min{ 9, 5+1 } = 6
This matrix shows the shortest distance between every pair of vertice
b) Discuss the control abstraction used in backtracking design technique. Draw the state
space tree for 4-queens problem.
Ans:
Backtracking Control Abstraction
(x1, x2.,…… xi) be a path from the root to a node in a state space tree.
Generating Function T(x1, x2.,…… xi) be the set of all possible values for
xi+1 such that (x1, x2.,…… xi+1) is also a path to a problem state.
T(x1, x2.,…… xn) = φ
Bounding function Bi+1(x1, x2.,…… xi+1) is false for a path (x1, x2.,……
xi+1) from the root node to a problem state, then the path cannot be extended
to reach an answer node.
Thus the candidates for position i+1of the solution vector (x1, x2.,…… xn)
are those values which are generated by T and satisfy Bi+1.
The recursive version is initially invoked by Backtrack(1).
Algorithm Backtrack(k)
{ for (each x[k] ϵ T(x[1], . . . .x[k-1])
{
if(Bk(x[1], x[2], . . . . . ., x[k]) != 0) then
{
if(x[1], x[2], . . . . . ., x[k] is a path to an answer node)
then write(x[1:k])
if(k<n) then Backtrack(k+1)
}
}
}
}
}
return m[][] and s[][]
}
o Matrix Ai has dimensions pi-1 X pi for i =1,2, . . . n.
o Input to this algorithm is a sequence p = ( p0, p1, . . . . pn ), where p.length =
n + 1.
o The procedure uses 2 auxiliary tables
o m[1 .. n , 1...n] for storing the cost of matrix multiplication
o s[1..n-1 , 2 ...n] records which index of k achieved the optimal cost in
computing m[i,j] .
b) Define Travelling Salesman Problem (TSP). Apply branch and bound technique to
solve the following instance of TSP. Assume that the starting vertex as A. Draw the
state space tree for each step.
Ans:
19.
a) Prove that CLIQUE problem is NP Complete
Ans:
CLIQUE problem is NP Complete: Proof
Step 1: Write a polynomial time verification algorithm to prove that the given
problem is NP
o Algorithm: Let G= (V,E), we use the set V‘ ⊆ V of k vertices in the clique
as a certificate of G
1. Test whether V‘ is a set of k vertices in the graph G
2. Check whether for each pair (u,v) ∈ V‘, the edge (u,v) belongs to E.
3. If both steps pass, then accept. Otherwise reject.
o This algorithm will execute in polynomial time. Therefore CLIQUE
problem is a NP problem.
Step 2: Write a polynomial time reduction algorithm from 3-CNF-SAT problem
to CLIQUE problem(3-CNF-SAT ≤p CLIQUE)
o Algorithm
Let Ф = C1 ˄ C2 . . . . . ˄ Ck be a Boolean formula in 3CNF with k
clauses
Each clause Cr has exactly three distinct literals lr1, lr2, lr3.
Construct a graph G such that Ф is satisfiable iff G has a click of size k.
The graph G is constructed as follows
For each clause Cr = (lr1 V lr2 V lr3) in Ф, we place a triple of
vertices Vr1, Vr2 and Vr3 in to V.
Put an edge between Vri to Vsi if following two conditions hold
o Vri and Vsi in different triples( that is r!=s)
o lri is not a negation of lsi .
20.
a) Define approximation algorithm. Give an approximation algorithm for bin packing
using first fit heuristic and give its approximation ratio.
Ans:
First Fit Decreasing Algorithm
o Sort the items in the descending order of their size
o Apply First fit algorithm
o Time Complexity
Best case Time Complexity = θ(n log n)
Average case Time Complexity = θ(n2)
Worst case Time Complexity = θ(n2)
Example: bin capacity=10, sizes of the items are {5, 7, 5, 2, 4, 2, 5, 1, 6}.
o Arrange the items in the decreasing order of the weight
{7, 6, 5, 5, 5, 4, 2, 2, 1}
Number of bins required = 4
Randomized Algorithm: The output or the running time are functions of the
input and random bits chosen
Algorithm findingA_LV(A, n)
{
repeat
{
Randomly choose one element out of n elements
}until(‘a’ is found)
}
The expected number of trials before success is 2.
Therefore the time complexity = O(1)
Algorithm findingA_MC(A, n, k )
{
i=0;
repeat
{
Randomly select one element out of n elements
i=i+1;
}until(i=k or ‘a’ is found);
}
o This algorithm does not guarantee success, but the run time is
bounded. The number of iterations is always less than or equal to
k.
o Therefore the time complexity = O(k)