Вы находитесь на странице: 1из 19

B.E./B.Tech.

DEGREE EXAMINATION, MAY/JUNE 2015


Fourth Semester
Computer Science and Engineering
CS 6402 — DESIGN AND ANALYSIS OF ALGORITHMS
(Regulation 2013)

Time : Three hours Maximum : 100 Marks

Answer ALL questions


PART A — (10 *2 = 20 Marks)

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

2. Write down the properties of asymptotic notation.


Transitive. If f(n) = Θ(g(n)) and g(n) = Θ(h(n)), then f(n) = Θ(h(n))
Reflexivity. f(n) = Θ(f(n))
Symmetry. f(n) = Θ(g(n)) if and only if g(n) = Θ(f(n))
Transpose Symmetry. f(n) = O(g(n)) if and only if g(n) = Ω(f(n))

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

4. Derive the complexity of binary search algorithm.


Cworst (n) = Cworst (n/2) + 1 …..(1)
Assume n = 2k where k= 1,2,………
Substitute n = 2k in equation 1,
Cworst (2k) = Cworst (2k /2) + 1
Cworst (2k) = Cworst (2k-1) + 1
Using the Backward Substitution method
Cworst (2k) = Cworst (2k-2) + 2
Cworst (2k) = Cworst (2k-3) + 3
.
.
Cworst (2k) = Cworst (2k-k) + k
= Cworst (20) + k
=1+k
Cworst (2k) = 1 + k
We assume n = 2k
Take log on both sides, we can get
log2n
Time Complexity of Binary Search is O(log n)
5. Write down the optimization technique used for Warshall’s algorithm. State the
rules and assumptions which are implied behind that.
Optimization technique used for Warshall’s algorithm is dynamic programming. A sub-
solution of the problem is constructed from previously found ones. DP solutions have a
polynomial complexity which assures a much faster running time than other techniques like
backtracking, brute-force etc.

6. List out the memory functions used under dynamic programming.


Memory function algorithm for the knapsack problem initializes V[i, j] to -1 except row 0
and column 0, which is initialize to 0.
Algorithm MFKnapsack(i, j) // i, j represent the sub problem
if V[i, j] < 0 // meaning not already calculated
if j < Weights[i] then
value ← MFKnapsack(i-1, j)
else
value ← max(MFKnapsack(i-1, j), Values[i] + MFKnapsack(i-1, j-Weights[i])
V[i, j] ← value // put valid value in the table for both cases
return V[i, j]

7. What do you meant by ‘perfect matching’ in bipartite graph?


A matching or independent edge set in a graph is a set of edges without common
vertices. It may also be an entire graph consisting of edges without common vertices.
Bipartite matching is a special case of a network flow problem.

8. Define flow ‘cut’.


In a flow network, an s–t cut is a cut that requires the source and the sink to be in
different subsets, and its cut-set only consists of edges going from the source's side to the
sink's side. The capacity of an s–t cut is defined as the sum of capacity of each edge in
the cut-set.
9. How NP Hard problems are different from NP Complete?
10. NP-complete problems are the “hardest” in NP: if any NP-complete problem is p-time
solvable, then all problems in NP are p-time solvable.

11. Define Hamiltonian Circuit problem.


A Hamiltonian cycle, Hamiltonian circuit, vertex tour or graph cycle is a cycle
that visits each vertex exactly once (except for the vertex that is both the start and end,
which is visited twice). A graph that contains a Hamiltonian cycle is called a Hamiltonian
graph.

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

Count the number of occurrences of each character in the pattern


for instance, "abba" would be (a, 2) (b, 2)
take the GCD of the numbers (in this case, that would be 2). the length of the string must
be divisible by the GCD.
find the occurrences of each letter in the string.
for instance "redblueredblue" would be (r, 2) (e, 4) (d, 2) (b, 2) (l, 2) (u, 2)
each number here also must be divisble by the previous GCD.
This doesn't solve anything, but does manage to eliminate a lot of strings in a fairly short amount
of time.
This process takes O(n) time, where n is the length of the string.

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

The (unique) minimal convex set containing X


The intersection of all convex sets containing X
The set of all convex combinations of points in X.
The union of all simplices with vertices in X.
It is not obvious that the first definition makes sense: why should there exist a
unique minimal convex set containing X. However, the second definition, the intersection of all
convex sets containing X is well-defined, and it is a subset of every other convex set Y that
contains X, because Y is included among the sets being intersected. Thus, it is exactly the unique
minimal convex set containing X. Each convex set containing X must (by the assumption that it
is convex) contain all convex combinations of points in X, so the set of all convex combinations
is contained in the intersection of all convex sets containing X. Conversely, the set of all convex
combinations is itself a convex set containing X, so it also contains the intersection of all convex
sets containing X, and therefore the sets given by these two definitions must be equal.
According to Carathéodory's theorem, if X is a subset of an N-dimensional vector
space, convex combinations of at most N + 1 points are sufficient in the definition above.
Therefore, the convex hull of a set X of three or more points in the plane is the union of all the
triangles determined by triples of points from X, and more generally in N-dimensional space the
convex hull is the union of the simplices determined by at most N + 1 vertices from X.

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:

For N=1, number of possible numbers would be 10 (0, 1, 2, 3, …., 9)


For N=2, number of possible numbers would be 36
Possible numbers: 00,08 11,12,14 22,21,23,25 and so on.
If we start with 0, valid numbers will be 00, 08 (count: 2)
If we start with 1, valid numbers will be 11, 12, 14 (count: 3)
If we start with 2, valid numbers will be 22, 21, 23,25 (count: 4)
If we start with 3, valid numbers will be 33, 32, 36 (count: 3)
If we start with 4, valid numbers will be 44,41,45,47 (count: 4)
If we start with 5, valid numbers will be 55,54,52,56,58 (count: 5)
We need to print the count of possible numbers.

Solution 1(Based on DFS):


N = 1 is trivial case, number of possible numbers would be 10 (0, 1, 2, 3, …., 9)
For N > 1, we need to start from some button, then move to any of the four direction (up, left,
right or down) which takes to a valid button (should not go to *, #). Keep doing this until N
length number is obtained (depth first traversal).

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.

13)B)I) Let A={l/119,m/96,c/247,g/283,h/72,f/77,k/92,j/19} be the letter and its frequency of


distribution in a text file. Compute a suitable Huffman coding to compress the data
effectively.
Step 1: Arrange the letters based on their probability.
j h f k m l c g

19 72 77 92 96 119 247 283

Step 2:

91 169

19 72 77 92

215 530

96 119 247 283


Step 3:

260 745

91 169 215 530

j h f k m l c g

19 72 77 92 96 119 247 283

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

000 001 010 011 100 101 110 111

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)I) Suggest an approximation algorithm for travelling salesperson problem. Assume


that the cost function satisfies the triangle inequality.
There is no polynomial time solution available for this problem as the problem is a
known NP-Hard problem. There are approximate algorithms to solve the problem though. The
approximate algorithms work only if the problem instance satisfies Triangle-Inequality.
Triangle-Inequality: The least distant path to reach a vertex j from i is always to reach j directly
from i, rather than through some other vertex k (or vertices), i.e., dis(i, j) is always less than or
equal to dis(i, k) + dist(k, j). The Triangle-Inequality holds in many practical situations.
When the cost function satisfies the triangle inequality, we can design an
approximate algorithm for TSP that returns a tour whose cost is never more than twice the cost
of an optimal tour. The idea is to use Minimum Spanning Tree (MST). Following is the MST
based algorithm.
Algorithm:
Step 1 : Let 1 be the starting and ending point for salesman.
Step 2 : Construct MST from with 1 as root using Prim’s Algorithm.
Step 3 : List vertices visited in preorder walk of the constructed MST and add 1 at
the end.
Following are some important facts that prove the 2-approximateness.
1) The cost of best possible Travelling Salesman tour is never less than the cost of MST.
2) The total cost of full walk is at most twice the cost of MST.
3) The output of the above algorithm is less than the cost of full walk. In above algorithm, we
print preorder walk as output. In preorder walk, two or more edges of full walk are replaced with
a single edge. For example, 2-1 and 1-4 are replaced by 1 edge 2-4. So if the graph follows
triangle inequality, then this is always true.
From the above three statements, we can conclude that the cost of output produced
by the approximate algorithm is never more than twice the cost of best possible solution.

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

Job1 Job2 Job3 Job4

9 2 7 8

6 4 3 7

5 8 1 8

7 6 9 4 Start

lb = 10

a->1 a->2 a->3 a -> 4

lb = 17 lb = 10 lb = 20 lb = 18

b->1 b->3 b->4

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" )

15)B)II) Implement an algorithm for Knapsack problem using NP-Hard approach.


Given a set of items, each with a weight and a value, determine the number of each
item to include in a collection so that the total weight is less than or equal to a given limit and the
total value is as large as possible. It derives its name from the problem faced by someone who is
constrained by a fixed-size knapsack and must fill it with the most valuable items.

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.

Вам также может понравиться