Algorith and Data Structure Revision - 1
Algorith and Data Structure Revision - 1
Define an algorithm
An algorithm is any well-defined computational procedure that takes
some value, or set of values, as input and produces some value, or set
of values, as output. An algorithm is thus a sequence of computational
steps that transform the input into the output.
2. Define a correct algorithm
An algorithm is said to be correct if, for every input instance, it halts
with the correct output. We say that a correct algorithm solves the
given computational problem. An incorrect algorithm might not halt at
all on some input instances, or it might halt with an incorrect answer.
3. Explain kinds of problems that are solved by algorithms
Manufacturing and other commercial enterprises often need to allocate
scarce resources in the most beneficial way.
An oil company may wish to know whereto place its wells in order to
maximize its expected profit. A political candidate may want to
determine where to spend money buying campaign advertising in order
to maximize the chances of winning an election.
An airline may wish to assign crews to flights in the least expensive way
possible, making sure thateach flight is covered and that government
regulations regarding crew scheduling are met.
An Internet service provider may wish to determine where to place
additional resources in order to serve its customers more effectively
4. Discuss briefly why Algorithms is considered as a technology
Of course, computers may be fast, but they are not infinitely fast. And memory
may be inexpensive, but it is not free. Computing time is therefore a bounded
resource, and so is space in memory. You should use these resources wisely,
and algorithms that are efficient in terms of time or space will help you do so
5. Represent the algorithm/pseudo code of insertion sort
int i;
for ( i = 0; i < n; ++i ) {
f( n );
}
This is a program that calls a function within a loop and we know the number
of instructions the called function performs, it's easy to determine the number
of instructions of the whole program. Explain briefly the number of
instructions of the whole program asymptotically.
Answer: f(n)=n2, as the first n is for the internal loop.Also the function is
called exactly n times.
5. List down any four common complexity classes with their algorithm
time complexity. Hit an algorithm with Θ( n ) is of complexity n.
Θ( 1 ) algorithm is a constant-time algorithm,
Θ( n ) is linear,
Θ( n2 ) is quadratic and
Θ( log( n ) ) is logarithmic).
6. Programs with a bigger Θ run slower than programs with a smaller
Θ.Explain.
Answer: This is because bigger Θ must have a high complexity (many loops)
that its needs to compute while a smaller Θ could be having just a constant
time algorithm which may or may not have a loop to consume its
computation time
7. Define a recurrence in algorithm.
A recurrence is an equation or inequality that describes a function in terms of
its value on smaller inputs. When the subproblems are large enough to solve
recursively, we call that the recursive case. Once the subproblems become
small enough that we no longer recurse, we say that the recursion “bottoms
out” and that we have gotten down to the base case. Sometimes, in addition to
subproblems that are smaller instances of the same problem, we have to solve
subproblems that are not quite the same as the original problem. We consider
solving such subproblems as part of the combine step. Recall that in divide-
and-conquer, we solve a problem recursively,applying three steps at each
level of the recursion:divide,conquer and combine. Recurrences can take
many forms. For example, a recursive algorithm might divide subproblems
into unequal sizes, such as a 2/3,1/3 split
1. Explain by example a recurrence relation
Answer: It’s the recursive part of a recursive definition of either a number
sequence or integer function. For example Recursive definition {fn } =
0,1,1,2,3,5,8,13,21,34,55,…of Fibonacci sequence is as follows
INITIALIZE:f0 = 0, f1 = 1
RECURSE: fn = fn-1+fn-2 for n > 1.The recursive definition part is the fn = fn-
1+fn-2 for n > 1 which is the recurrence relation consisting of an equation
that expresses each term in terms of lower terms
1. an = 2an-1
2. an = 2an-1 + 2n-3 - an-3
3. an = an-12
4. Partition function:
Answers
1. an = 2an-1: YES
2. an = 2an-1 + 2n-3 - an-3: YES
3. an = an-12: NO. Squaring is not a linear operation. Similarly an = an-
1an-2 and an cos(an-2) are non-linear.
4. Partition function:
NO. This is linear, but coefficients are not constant as C (n -1, n -1-i ) is a
non-constant function of n.