Beruflich Dokumente
Kultur Dokumente
ANSWER KEY
PART-A
3.what is the tool for analyzing the time efficiency of a non recursive algorithm?
* Set up a sum expressing the number of times the algorithm’s basic operation is executed.
*Standard formulas and rules of sum manipulation,finds either closed form formula for the count or at least its
order of growth..
4.What is algorithm animation?
It is dyanamic algorithm visualization.It shows a continous movie like presentation of an algorithm’s operations.
10.what are the additional items required for branch and bound compare backtracking technique?
Backtracking:
(i) Traced using depth first search.
(ii) Decision problems can be solved.
(iii) If dead end is reached during solution then backtracking and try another solution.
(iv) Example:
Knapsack
Sum of subset
11.(a).(i) Discuss briefly the sequence of steps in designing and analyzing an algorithm.(10)
The first important step in problem solving approach is that to understand the problem.
That is, defining the problem statement, initially we must concentrate on what must be done
rather than how to do it.
An input to an algorithm specifies an instance of the problem the algorithm solves.
We need to specify the range of instances the algorithm needs to handle.
Ascertaining the capability of computational device:
Once we completely understand the problem, we need to ascertain the capabilities of the
computational device the algorithm is intended for.
Majority of algorithm are destined to be programmed for computers closely resembling
Von Neumann machine.
It can be assumed as a generic one-processor, random-access-machine (RAM).
An algorithm that can solve the problem exactly is called an exact algorithm.
The algorithm that can solve the problem approximately is called an approximation
algorithm.
The problems that can be approximately are
1. extracting square roots
2. solving non-linear equations
3. evaluate definite integrals
4. Algorithm for solving a problem exactly is not acceptable because it can be
slow due to is intrinsic complexity of that problem.
11.(a) (ii) .Explain some of the problem types used in the design of algorithm. (6)
The important problem types are:
a. Sorting
b. Searching
c. String processing
d. Graph problems
e. Combinatorial problems
f. Geometric problems
g. Numerical problems
Sorting
In sorting problem, we arrange the items of a given list in ascending order.
There must be a relation of total ordering.
We usually sort a list of numbers, characters from an alphabet, character strings
and records.
To sort a list of number or records, we need a piece of information, called key to
guide sorting.
The sorted list helps in searching.
Searching
The searching problem deals with finding a given value, called a search key in the
given set.
In searching algorithms, searching has to considered in conjunction with other
operations: addition and deletion from the data set of an item.
In such situations, data structures and algorithms should be chosen to strike a
balance among the requirements of each operation.
String processing
Graph problem
Combinatorial problem
Combinatorial problems are the most difficult problems in computing from both
the theoretical and the practical standpoints.
First, the number of combinatorial objects typically grows extremely fast with a
problem’s size, reaching unimaginable magnitudes even for moderate-sized
instances. Second, there are no known algorithms for solving most such problems
exactly in an acceptable amount of time.
Geometric problem
Geometric algorithms deals with geometric objects such as points, lines, and
polygons.
Ancient Greeks were very much interested in developing procedures for solving a
variety of geometric problems, including problems of constructing simple
geometric shapes-triangles, circles, and so on - with an unmarked ruler and a
compass.
The closest- pair problem is self- explanatory; given n points in the plane, find
the closest pair among them.
The convex hull problem asks to find the smallest convex polygon that
would include all the points of a given set.
Numerical problem
(b).(i). Explain the general frame work for analyzing the efficiency of algorithms. (8)
Analyzing frame work
Time efficiency indicates how fast an algorithm in question runs; space efficiency deals
with the extra space the algorithm requires.
Measuring an input’s size
An algorithm’s efficiency as a function of some parameter n indicating the algorithm’s
input size.
In most cases, selecting such a parameter is quite straightforward.
Units for measuring running time
The speed of a particular computer, dependence on the quality of program implementing
the algorithm and of the compiler used in generating the machine code, and the difficulty
of clocking the actual running time of the program.
Number of times each of the algorithm’s operation is executed.
Basic operation of an algorithm is usually the most time-consuming operation in the
algorithm’s innermost loop
Orders of growth
A different in running times on small inputs is not what really distinguishes efficient
algorithms from inefficient ones.
Algorithms that require an exponential number of operations are practical for solving
only problems of very small sizes.
Worst-case, best-case, and average-case efficiencies
The worst-case efficiency of an algorithm is its efficiency for the worst-case input of size
n, which is an input of size n for which the algorithm runs the longest among all possible
inputs of that size.
The best-case efficiency of an algorithm is its efficiency for the best-case input of size n,
which is an input of size n for which the algorithm runs the fastest among all possible
inputs of that size.
Neither the worst-case analysis nor its best-case counterpart yields the necessary
information about an algorithm’s behavior on a “typical” or “random” input. This is the
information that the average-case efficiency.
Another type of efficiency called amortized efficiency. It applies not to a single run of an
algorithm but rather to a sequence of operations performed on the same data structure.
Informal introduction:
O(g(n)) is the set of all functions with the smaller or same order of growth as g(n) .
E.g. n ε O(n2)
100n+5 ε O(n²), 1/2n(n-1) ε O(n²)
The first two functions are linear and hence have a smaller order of growth than g(n)=n² , the last is a quadratic
and hence has the same order of growth as n².
The second notation, Ω(g(n)) stands for the set of all functions with a large or same order of growth as g(n).
For e.g. n³ ε Ω(n²) , ½ n(n-1) ε Ω(n²) ,
but 100n+5not belongs to Ω(n²)
Finally Ө(g(n)) is the set of all functions that have the same order of growth as g(n).
O-notation:
A function t(n) is said to be in O(g(n)), denoted t(n) ε O(g(n)), if t(n) is bounded above by some constant
multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such
that
t(n)≤cg(n) for all n ≥ n0
for e.g. 100n+5≤100n+n(for all n≥5)=101n≤101n².
c=101 n0=5.
for e.g. 100n+5≤100n+5n(for all n≥1)=105n.
c=105 n0=1.
Graph:
Ω-notation:
A function t(n) is said to be in Ω (g(n)), denoted t(n) ε(g(n)),if t(n) is bounded below by some positive constant
multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such
that
T(n) ≥ cg(n)for all n ≥ n0.
e.g. n³≥n² for all n≥0,
c=1 and n0=0.
Graph:
Ө-notation:
A function t(n) is said to be in Ө(g(n)), denoted t(n)εӨ(g(n)), if t(n) is bounded both above and below by
some positive constant multiplies of g(n) for all large n, i.e., if there exist some positive constant c1,c2 and
some nonnegative integer n0 such that
c2g(n) ≤t(n) ≤c1g(n) for all n≥n0.
For e.g.
½n(n-1)εӨ(n²).
R.H.S
1/2n(n-1)=1/2n²-1/2n≤1/2n² for all n≥0.
L.H.S
1/2n(n-1)=1/2n²-1/2n≥1/2n²-1/2n1/2n (for all n≥2)=1/4 n² .
Hence c2=1/4 , c1=1/2 , n0=2.
Graph:
12.(a) (i). Design a recursive algorithm to compute the factorial function F(n)=n! For an
arbitrary non negative integer n and also derive the recurrence relation.(10)
Algorithm F(n)
// compute n! Recursively
// Input: A nonnegative integer n
// Output: The value of n!
If n=0 return 1
Be consistent
Be Interactive
Be clear and concise
Be forgiving to the user
Adapt to the knowledge level of the user
Emphasis the visual component
Keep the user interested
Incorporate both symbolic and iconic representations
Include algorithms analysis and comparisons with other algorithms for the same
problem
Include execution history
12.(b) (i) Design a non recursive algorithm for computing the product of two n * n
matrices and find the time efficiency of the algorithm.(10)
Example 1: Consider the problem of finding the value of the largest element in a list of
n numbers.
Algorithm: Max Element (A [0….n-1])
// Determines the value of the largest element in a given array
// Input: An array A [0….n-1] of real numbers
// output: The value of the largest element in A
maxval <- A[0]
for i<-1 to n-1 do
if A[i]>maxval
maxval<-A[i]
return maxval
Example 2: Consider the element uniqueness problem: check whether all the elements in a list
given array are distinct.
Algorithm: Unique Elements (A [0…n-1]
// Determines whether all the elements in a given array are distinct
// Input: An array A[0….n-1] of real numbers
// Output: Returns “true” if all the elements in A are distinct
// and “false” otherwise
For i<-0 to n-2 do
For j<-i+1 to n-1 do
If A[i]=A[ j] return false
Return true
Example 3: Given two n- by- n matrices A and B, find the time efficiency of the definition-based
algorithm for computing their product C=AB.
Algorithm: Matrix Multiplication (A[0…n-1, 0….n-1],B[0….n-1,0….n-1])
called amortized efficiency. It applies not to a single run of an algorithm but rather to a
sequence of operations performed on the same data structure.
12.(b). (ii).Write short note on algorithm visualization and its applications.(6)
Features of an animation
Be consistent
Be Interactive
Be clear and concise
Be forgiving to the user
Adapt to the knowledge level of the user
Emphasis the visual component
Keep the user interested
Incorporate both symbolic and iconic representations
Include algorithms analysis and comparisons with other algorithms for the same
problem
Include execution history
Applications
13.(a) (i).write a pseudo code for divide and conquer algorithm for merging tow
sorted arrays into a single sorted one. Explain with an example.(12)
13.(a) (ii). Set up and solve a recurrence relation for the number of key comparisons made
by the above pseudo code.(4)
Assume n is a power of 2, the recurrence relation for the number of key comparisons C(n) is:
C(n)=C(n/2) +C(n/2)+Cmerge (n) for n>1
In general, C (1)=0
Cmerge (n) - No. of key comparisons done in merging
In the worst case, each step, exactly one comparison is done (i.e.) (n-1)
Cworst (n) = 2C(n/2) +n-1 for n>1, Cworst (1)=0
Using Master’s theorem:
T(n)=a T(n/b) +f(n)
Here a=2, b=2 and to find the value of d;
d=1
Here a=b
14. (a) (i).Construct a minimum spanning tree using kruskal’s algorithm with your
own ekample.(10)
14.(a) (ii) Explain single L-rotation and of the double RL-rotation with general form.
(6)
The symmetric single left rotation or L-rotation is the mirror image of the single R-
rotation.
It is performed after a new key is inserted into the right sub tree of the right child of a tree
whose root had the balance of -1 before the insertion.
The double right-left rotation is the mirror image of the double LR-rotation and is left for
the exercises.
14.(b) solve the all_pairs shortest path problem for the diagraph with the weight
matrix given below(16).
The state space tree for the given instance of the subset sum problem S = {1, 3, 4,
5} and d=11
There is no solution to this instance of the problem.
15. (b) Solve the following instance of the Knapsack problem by the branch-and-
bound algorithm.
1 4 $40
2 7 $42
3 5 $25
4 3 $12
If we are given with n objects or items and a Knapsack or a bag in which subset of
items is to be placed. Each item has a known weight w1. The Knapsack has a
capacity W. Then the profit that is earned is v1. The objective is to obtain filling
of Knapsack with maximum profit earned. But it should not exceed W of the
Knapsack.
1 4 $40 10
2 7 $42 6
3 5 $25 5
4 3 $12 4
W=Knapsack capacity =10
We compute upper bound
Ub= v + (W-w) (vi+1 / wi+1)
Initially v=0, w=0 and vi+1= v1 =40
wi+1= w1=4
Capacity W=10
Ub = 0+(10-0) (40/4)
=(10) (10)
=100
=$100
Computation at node 0: No items have been closed selected. Both total weight of the items already selected
w and their total value v are equal to 0.
w=0 v=0 vi+1 / wi+1 = v1 / w1 =40/4=40
Capacity W=10
ub=v+(W-w) (vi+1/wi+1)
=0 + (10-0) (10)
=$100
Computation at node 1: It represents left child of the root, represents the subset that include item 1.
Total weight and value of the item already included are 4 and $40 respectively.
w=4 v=40 Capacity W=10
vi+1 /wi+1=next item to item 1
=v2/w2
=6
ub=v+(W-w) vi+1 /wi+1
=40+(10-4) 6
=40+36
=76
Computation at node 2: we assume item 1 is not selected.
v=0 w=0 , capacity W=10
vi+1 / wi+1 = v2/w2=6
ub=v+(W-w) (vi+1/wi+1)
=0+(10-0) (6)
=60
Computation at node 3: subset with item 1 and item 2 respectively.
Total weight w=4+7=11
Value v=40+42=82
vi+1 / wi+1 =v2/w2=5
capacity W=10
But the total weight W exceeds Knapsack capacity. Hence node 3 is terminated immediately.
Computation at node 8: subset with item 1, item 3 and without item 2 and item 4.
w=9
v=65
W=10
vi+1/wi+1=v5/w5=0
there is no next item
ub=65+(10-1)0=65
it is simply equal to the total value of these two items.