Tutorial Sheet of Computer Algorithms (BCA)
Tutorial Sheet of Computer Algorithms (BCA)
6. Give a recursive algorithm to compute the product of two positive integers m and n
using only addition.
7. Solve the following recurrence by master method
• T(n) = 9T(n/3) + n
• T(n) = 2T(n/2) + nlog n
8. Compare and differentiate quick sort and merge sort algorithm.
9. Illustrate the properties of greedy algorithms.
10. Compare the methods of ‘Backtracking’ and of ‘Branch and Bound’.
Module - I
1. Explain the step count method for computing time complexity of an algorithm using
the example of addition of two matrices. Illustrate the importance of time complexity
analysis of an algorithm.
2. Define and explain asymptotic notations with examples. How they are helpful in
expressing and comparing the efficiency of algorithms. Prove that
n2/2 –n/2 = θ (n2) and 100n + 5 = O(n2)
3. Explain the space complexity of an algorithm. Compute space complexity for following
algorithm:
a. float rsum(float list[ ], int n)
a. {
b. if (n) return rsum(list, n-1) + list[n-1];
1
c. return 0;
d. }
Module - II
1. Illustrate the recursive binary search algorithm with an example. Compute the time
complexity of this algorithm.
2. With a suitable example explain the recursive max-min algorithm. Evaluate the
performance of the algorithm.
3. Explain the divide and conquer approach using quick sort algorithm. Evaluate the
performance of algorithms and discuss the merits and limitations of the algorithm.
4. Elaborate the notions of best case, worst case, and average case of time complexities
of an algorithm, using an example of MERGESORT algorithm, show how it sorts a data
set A= ( 310, 285, 179, 652, 351, 423, 861,254)
Module - III
1. Design an algorithm for the fractional Knapsack problem. Analyze the time complexity
of Greedy Knapsack algorithm. Find an optimal solution to the knapsack instance n =7,
m = 15, (p1,p2,p3,…,p7) = (10,5,15,7,6,18,3), and (w1,w2,…,w7) = (2,3,5,7,1,4,1), such
that fraction of any object can be taken.
2. One greedy strategy is to consider the objects in order of nonincreasing desity pi /wi ,
and add the object into the knapsack if it fits. Show that this strategy doesn’t
necessarily yield an optimal solution.
3. You are given a set of n jobs. Associated with each job i is a processing time ti and a
deadline di by which it must be completed. A feasible schedule is a permutation of the
jobs such that if the jobs are processed in that order, then each job finishes by its
deadline. Define a greedy schedule to be one in which the jobs are processed in
nondecreasing order of deadlines.
4. Prove the correctness of Prims’s algorithm, Kruskal algorithm i.e. Prove that Prim’s
and Kruskal’s methods generate minimum-cost spanning trees.
5. Discuss a greedy solution to find Single source shortest paths. Illustrate its function
with a suitable example.
2
Module – IV
1. A topological sort of a directed acyclic graph (a graph without cycles) yields a list of
vertices such that if there is a path from vertex i to vertex j, then i precedes j in the
topological sort. In O() notation, what is the running time of a topological sort on a
graph with |V| vertices and |E| edges? Give a brief but precise explanation justifying
your answer.
2. Given an adjacency matrix representation of a graph how long does it take to compute
the out-degree of all vertices? How long does it take to compute in-degree of all
vertices? How long does it take to compute indegree and out-degree of a single
vertex? Using a suitable example, explain your answers.
3. Show the BFS tree that results from running BFS on the following graph. Start at vertex
“A” and examine edges in alphabetical order of destination vertex.
Module - V
1. The assignment problem is usually stated in this way: there are n people to be assigned
to n jobs. The cost of assigning the ith man to the jth job is COST (i,j). You are to
develop an algorithm which assigns every job to a person and at the same time
minimizes the total cost of the assignment.
2. State the basic principles of problem solving with branch and bound. Show how the
problem of 0/1 knapsack be solved with branch and bound. Can we solve this problem
using backtracking?
3. Discuss the backtracking method. Give a step-to-step trace for solving the n-queen
problem for 4 queens.