Академический Документы
Профессиональный Документы
Культура Документы
1. Write an algorithm to find the number of binary digits in the binary representation
of a positive decimal integer?
bitcount(n):
count = 0
while n > 0:
count = count + 1
n = n & (n-1)
return count
3. Design a Brute Force algorithm for computing the value of a polynomial p(x)=anxn+
an-1xn-1+……+a1x…..a0 at a given point x0 and determine its worst case efficiency
class.
Algorithm BruteForcePolynomialEvaluation(P[0..n],x)
//The algorithm computes the value of polynomial P at a given point x
//by the “lowest-to-highest term” algorithm
//Input: Array P[0..n]of the coefficients of a polynomial of degree n,
// from the lowest to the highest, and a number x
//Output: The value of the polynomial at the point x
p ← P[0];
power ← 1
for I ← 1 to n
do
power ← power ∗ x
p ← p + P[i] ∗ power
return p
PART B
11)A) If you have to solve the searching problem for a list of n numbers, how can you take
advantages of the fact that the list is known to be sorted? Give separate answers for
(i) list represented as arrays
(ii) list represented as linked lists
Compare the time complexities involved in the analysis of both the algorithms.
All solutions provided here assume that all elements in array are distinct.
The idea is to find the pivot point, divide the array in two sub-arrays and call binary search.
The main idea for finding pivot is – for a sorted (in increasing order) and pivoted array, pivot
element is the only only element for which next element to it is smaller than it.
Using above criteria and binary search methodology we can get pivot element in O(logn) time.
11)B) Derive the worst case analysis of merge sort using suitable illustrations.
The recurrence relation is
T(n) = 2T(n/2) + cn
Assume n = 2k
T(2k) = 2T(2k /2) + c. 2k
T(2k) = 2T(2k-1) + c. 2k
T(2k) = 2 [ 2T(2k-2) + c. 2k-1) ] + c. 2k
T(2k) = 22T(2k-2) + 2. c. 2k-1 + c. 2k
T(2k) = 22T(2k-2) + 2. c. 2k /2 + c. 2k
T(2k) = 22T(2k-2) + 2. c. 2k
T(2k) = 23T(2k-3) + 3. c. 2k
.
.
.
T(2k) = 2kT(2k-k) + k. c. 2k
T(2k) = 2kT(1) + k. c. 2k
T(2k) = k. c. 2k [ T(1) = 0 ]
We assume n = 2k
Take log on both sides, we can get
Hence the average , best and worst case complexity of merge sort is O(n log n).
12)A)I) Solve the following using Brute Force Algorithm.
Find whether the given string follows the specified pattern and return 0 or 1
accordingly.
Examples :
(1) Pattern: “abba”, input:”redblueredblue’ should return 1
(2) Pattern: “aaaa”, input:”asdsdasdasd’ should return 1
(3) Pattern: “aabb”, input:”xyzabcxyzabc’ should return 0
12)A)II) Explain the convex hull problem and the solution involved behind it.
A set of points is defined to be convex if it contains the line segments connecting
each pair of its points. The convex hull of a given set X may be defined as
12)B) A pair contain two numbers, and its second number is on the right side of the first
one in an array. The difference of a pair is the minus result while subtracting the second
number from the first one. Implement a function which gets the maximal difference of all
pairs in an array (using divide and conquer method).
Algorithm:
hasArrayTwoCandidates (A[], ar_size, sum)
1) Sort the array in non-decreasing order.
2) Initialize two index variables to find the candidate
elements in the sorted array.
(a) Initialize first to the leftmost index: l = 0
(b) Initialize second the rightmost index: r = ar_size-1
3) Loop while l < r.
(a) If (A[l] + A[r] == sum) then return 1
(b) Else if( A[l] + A[r] < sum ) then l++
(c) Else r--
4) No candidates in whole array - return 0
Time Complexity: Depends on what sorting algorithm we use. If we use Merge Sort
or Heap Sort then nlogn in worst case. If we use Quick Sort then O(n^2) in worst case.
Example:
Let Array be {1, 4, 45, 6, 10, -8} and sum to find be 16
Sort the array
A = {-8, 1, 4, 6, 10, 45}
Initialize l = 0, r = 5
A[l] + A[r] ( -8 + 45) > 16 => decrement r. Now r = 10
A[l] + A[r] ( -8 + 10) < 2 => increment l. Now l = 1
A[l] + A[r] ( 1 + 10) < 16 => increment l. Now l = 2
A[l] + A[r] ( 4 + 10) < 14 => increment l. Now l = 3
A[l] + A[r] ( 6 + 10) == 16 => Found candidates (return 1)
13)A)I) Given the mobile numeric keypad. You can only press buttons that are up, left,
right or down to the first number pressed to obtain the subsequent numbers. You are not
allowed to press bottom row corner buttons (i.e. * and #). Given a number N , how many
key storkes will be involved to press the given number. What is the length of it? Which
dynamic programming technique could be used to find solution for this ? Explain each step
with the help of pseudo code and derive its time complexity.
Examples:
Solution 2(Recursive):
Mobile Keypad is a rectangular grid of 4X3 (4 rows and 3 columns). Let’s say
Count(i, j, N) represents the count of N length numbers starting from position (i, j)
If N = 1
Count(i, j, N) = 10
Else
Count(i, j, N) = Sum of all Count(r, c, N-1) where (r, c) is new
position after valid move of length 1 from current
position (i, j)
13)A)II) How do you construct a minimum spanning tree using kruskal’s algorithm?
Explain.
Given a connected and undirected graph, a spanning tree of that graph is a subgraph
that is a tree and connects all the vertices together. A single graph can have many different
spanning trees. A minimum spanning tree (MST) or minimum weight spanning tree for a
weighted, connected and undirected graph is a spanning tree with weight less than or equal to the
weight of every other spanning tree. The weight of a spanning tree is the sum of weights given to
each edge of the spanning tree. A minimum spanning tree has (V – 1) edges where V is the
number of vertices in the given graph.
Below are the steps for finding MST using Kruskal’s algorithm
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree
formed so far. If cycle is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
Step 2:
91 169
19 72 77 92
215 530
260 745
j h f k m l c g
Step 4:
1005
5
0 1
260
745
0 1 0 1
91 169
215 530
0 1 0 1 0 1 0 1
j h f k
m l c g
19 72 77 92
96 119 247 283
The resulting code word are as follows
j h f k m l c g
B)II) Write an algorithm to construct the optimal binary search tree given the roots r(i,j),
0 ≤i≤j≤n. Also prove that this could be performed in time O(n).
Algorithm OBST(P[1,2,……..,n])
Begin
for i <- 1 to n
C[i,i-1] <- 0
C[i,i] <- P[i]
R[I,i] <- i
end for
for i <- 1 to n+1
for j <- 0 to n
min <- ∞
for k <- i to j
if (C[i,k-1] + C[k+1,j]) < min
min <- C[i,k-1] + C[k+1,j]
kmin <- k
end if
end for
R[i,j] <- k
sum <- P[i]
for s <- i+1 to j
sum <- sum + P[i]
C[i,j] <- min + sum
end for
end for
for i <- 1 to n+1
for j <- 0 to n
write Cost[i,j]
end for
end for
for i <- 1 to n
for j <- 1 to n
write Root[i,j]
end for
end for
End
14)A) Write down the optimality condition and algorithmic implementation for finding M-
augmenting paths in bipartite graphs.
A matching in a graph is a sub set of edges such that no two edges share a vertex.
The maximum matching of a graph is a matching with the maximum number of edges. This is
very difficult problem.
Augmenting Path
In general, we can increase the size of the matching, M, by constructing a simple
path from V to U. Starting from a free vertex in V and ending in a free vertex in U. The path is
augmenting if the edges of the simple path alternate from edges in E-M and edges M, meaning
edges not in the matching and edges in the matching. The first edge is not in M, so the second
and every even number edge is in M.
Because the path begins in V and ends in U, the length of the path is odd. So,
augmenting the match, M, by adding the odd edges to M and subtracting the even edges from M
will increase the size of M by one.
Maximum Matching Algorithm
The theorem suggest a technique for iterative improvements of a matching, M. The
algorithm iteratively finds an augmenting path and augments the matching, M. We will find the
augmented path by performing several BFS traversals. Along the way, vertices are label, labels
for V vertices represent edges in the matching, M. Labels for U vertices represent edges not in
M, meaning in E-M.
The BFS begins on free V vertices, and ends when it finds a free U vertex. It can
follow the labels, adding new edges from M and removing edges from M. The BFS is achieved
by filling the queue first with all the free vertices in V. U vertices do not enter the queue unless
they have a mate.
14)B)I) Briefly describe on the stable marriage problem.
The stable marriage problem (also stable matching problem or SMP) is the problem
of finding a stable matching between two equally sized sets of elements given an ordering of
preferences for each element. A matching is a mapping from the elements of one set to the
elements of the other set.
There is an element A of the first matched set which prefers some given element B
of the second matched set over the element to which A is already matched, and B also prefers A
over the element to which B is already matched.
Stable marriage algorithm
Input: A set of n men and a set of n women along with rankings of the women by each man and
rankings of the men by each woman with no ties allowed in the rankings
Output: A stable marriage matching
Step 1 : Start with all the men and women being free.
Step 2 : While there are free men, arbitrarily select one of them and do the following:
The selected free man m proposes to w, the next woman on his preference list (who is the
highest-ranked woman who has not rejected him before). If w is free, she accepts the
proposal to be matched with m. If she is not free, she compares m with her current mate.
If she prefers to him, she accepts m’s proposal, making her former mate free; otherwise,
she simply rejects m’s proposal, leaving m free.
Step 3 : Return the set of n matched pairs.
14)B)II) How do you compute maximum flow for the following graph using Ford-
Fulkerson method?
1/1
Step 1: S S
1/10
S S
Step 2:
S S
S 2/6
2/5
S S
2/2
Step 3:
S S
S
2/9
2/2 2/3
2/2
Maximum Flow = 5
15)A)II) Explain how job assignment problem could be solved, given n tasks and n agents
where each agent has a cost to complete each task, using branch and bound technique.
It is a problem where each job will be assigned to each person. And no 2 jobs can be
assigned to same person and no 2 person should be assigned with the same job.
Select one element in each row of the cost matrix C so that:
• no two selected elements are in the same column
• the sum is minimized
Example
9 2 7 8
6 4 3 7
5 8 1 8
7 6 9 4 Start
lb = 10
lb = 17 lb = 10 lb = 20 lb = 18
lb = 13 lb = 14 lb = 17
15)B)I) The knight is placed on the first block of an empty board and, moving according to
the rules of chess , must visit each square exactly once. Solve the above problem using
backtracking procedure.
The Naive Algorithm is to generate all tours one by one and check if the generated
tour satisfies the constraints.
while there are untried tours
{
generate the next tour
if this tour covers all squares
{
print this path;
}
}
Backtracking works in an incremental way to attack problems. Typically, we start
from an empty solution vector and one by one add items (Meaning of item varies from problem
to problem. In context of Knight’s tour problem, an item is a Knight’s move).
When we add an item, we check if adding the current item violates the problem
constraint, if it does then we remove the item and try other alternatives. If none of the
alternatives work out then we go to previous stage and remove the item added in the previous
stage.
If we reach the initial stage back then we say that no solution exists. If adding an
item doesn’t violate constraints then we recursively add items one by one. If the solution vector
becomes complete then we print the solution.
Backtracking Algorithm for Knight’s tour
Following is the Backtracking algorithm for Knight’s tour problem.
If all squares are visited
print the solution
Else
a) Add one of the next moves to solution vector and recursively check if this
move leads to a solution. (A Knight can make maximum eight moves. We
choose one of the 8 moves in this step).
b) If the move chosen in the above step doesn't lead to a solution then remove
this move from the solution vector and try other alternative moves.
c) If none of the alternatives work then return false (Returning false will remove
the previously added item in recursion and if false is returned by the initial
call of recursion then "no solution exists" )
Computational Complexity
✓ The decision problem form of the knapsack problem is NP-complete, thus there is no
known algorithm both correct and fast (polynomial-time) on all cases.
✓ While the decision problem is NP-complete, the optimization problem is NP-hard, its
resolution is at least as difficult as the decision problem, and there is no known
polynomial algorithm which can tell, given a solution, whether it is optimal (which would
mean that there is no solution with a larger V, thus solving the NP-complete decision
problem).
✓ There is a pseudo-polynomial time algorithm using dynamic programming.
✓ Many cases that arise in practice, and "random instances" from some distributions, can
nonetheless be solved exactly.
There is a link between the "decision" and "optimization" problems in that if there
exists a polynomial algorithm that solves the "decision" problem, then one can find the
maximum value for the optimization problem in polynomial time by applying this algorithm
iteratively while increasing the value of k . On the other hand, if an algorithm finds the optimal
value of optimization problem in polynomial time, then the decision problem can be solved in
polynomial time by comparing the value of the solution output by this algorithm with the value
of k . Thus, both versions of the problem are of similar difficulty.