DAA-University 2mark Questions and Answers
DAA-University 2mark Questions and Answers
Unit 1
linearithmic,
Performing a Fast Fourier transform; heapsort, quicksort
loglinear, or
(best and average case), or merge sort
quasilinear
Inhomogenous recurrence?
The solution of linear recurrence with constant co-efficient becomes
more difficult when the recurrence is not homogenous, that is when linear
combination, is not equal to zero.
aotn + a1tn-1 + . . . aktn-k = bnp(n).
5.What are the control structures? ( APRIL 2015)
A control structure is a block of programming that analyzes variables and chooses a direction
in which to go based on given parameters. The term flow control details the direction the
program takes (which way program control "flows")
o linear search or sequential search is a method for finding a target value within a list.
o It sequentially checks each element of the list for the target value until a
match is found or until all the elements have been searched.
o binary search or half-interval search algorithm finds the position of a target
valuewithin a sorted array.
o The binary search algorithm can be classified as a dichotomy divide-and-conquer
search algorithm and executes in logarithmic time.
The principle of optimality states that an optimal sequence of decisions has the property that
whatever the initial state and decisions are, the remaining decisions must constitute an
optimal decision sequence with regard to state resulting from first decision.
9. Define order of growth. ( NOV 2015)
Order of growth in algorithm means how the time for computation increases when you
increase the input size. It really matters when your input size is very large. Order of
growth provide only a crude description of the behavior of a process. Depending on
the algorithm, the behaviour changes.
A function f(n) is said to be in O(g(n)), denoted by f(n) = O(g(n)), if there exist some positive
constant c and some non-negative integer no such that
Unit-2
Master Method is a direct way to get the solution. The master method works only for following type
of recurrences or for recurrences that can be transformed to following type.
In divide and conquer approach, a problem is divided into smaller problems, then
the smaller problems are solved independently, and finally the solutions of smaller problems
are combined into a solution for the large problem.
Generally, divide-and-conquer algorithms have three parts
Divide the problem into a number of sub-problems that are
smaller instances of the same problem.
Conquer the sub-problems by solving them recursively. If they
are small enough, solve the sub-problems as base cases.
Combine the solutions to the sub-problems into the solution for
the original problem.
Pros and cons of Divide and Conquer Approach
Divide and conquer approach supports parallelism as sub-problems are independent.
Hence, an algorithm, which is designed using this technique, can run on the
multiprocessor system or in different machines simultaneously.
In this approach, most of the algorithms are designed using recursion, hence memory
management is very high. For recursive function stack is used, where function state
needs to be stored.
Application of Divide and Conquer Approach
Following are some problems, which are solved using divide and conquer approach.
Finding the maximum and minimum of a sequence of numbers
Strassen’s matrix multiplication
Merge sort
Binary search
2. Define greedy technique ( APRIL 2016)
Greedy algorithm is an algorithmic paradigm based on heuristic that follows local optimal choice at
each step with the hope of finding global optimal solution.
Components of Greedy Algorithm
Greedy algorithms have the following five components −
A candidate set − A solution is created from this set.
A selection function − Used to choose the best candidate to be added to the solution.
A feasibility function − Used to determine whether a candidate can be used to contribute to
the solution.
An objective function − Used to assign a value to a solution or a partial solution.
A solution function − Used to indicate whether a complete solution has been reached.
3.State the best and worst case of binary search algorithm.(NOV 2016)
In graph theory, the shortest path problem is the problem of finding a path between two
vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is
minimized.
Linear search or sequential search is a method for finding a target value within a list. It sequentially
checks each element of the list for the target value until a match is found or until all the elements have
been searched.
Binary search or half-interval search algorithm finds the position of a target value within a sorted
array. The binary search algorithm can be classified as a dichotomic divide-and-conquer search
algorithm and executes in logarithmic time.
Suppose that a rec. algorithm divides a problem of size n into a parts, where each sub-
problem is of size n/b. Also suppose that a total number of g(n) extra operations are
needed in the conquer step of the algorithm to combine the solutions of the sub-problems
into a solution of the original problem. Let f(n) be the number of operations required to
solve the problem of size n. Then f satisfies the recurrence relation f(n)=a f(n/b)+g(n)
and it is called divide-and-conquer recurrence relation.
Example:
1) Consider the case in which a=2 and b=2. Let T(1)=2 & f(n)=n.
We have,
T(n) = 2T(n/2)+n
= 2[2T(n/2/2)+n/2]+n
= [4T(n/4)+n]+n
= 4T(n/4)+2n
= 4[2T(n/4/2)+n/4]+2n
= 4[2T(n/8)+n/4]+2n
= 8T(n/8)+n+2n
= 8T(n/8)+3n
The greedy method is straight forward design technique which has n inputs and to obtain a subset
that satisfies some constraints.
9.Analyze the various cases of complexity for Binary Search. (NOV 2014)
Greedy method is comparatively efficient than divide and conquer but there is no such
guarantee of getting optimum solution
In greedy method the optimum selection is without revising previously generated solutions
In many problems, Greedy algorithm fails to find an optimal solution, moreover it may
produce a worst solution. Problems like Travelling Salesman and Knapsack cannot be solved
using this approach.
The objective of the knapsack problem is to fill the knapsack with capacity ’m’ by including or
excluding full (or) fractional amounts of different n objects in which summation of weight of the
object must be less than or equal to the capacity of the knapsack ‘m' and summation of profit of the
object must be maximum
n
Maximize ∑ pixi
i=1
n
Subject to ∑ wixi<= m
i=1
xi ϵ {0,0-1,1}
where 1<=i<=n
ADDITIONAL QUESTIONS :
In this problem we have n jobs j1, j2, … jn each has an associated deadline d1, d2, … dn and
profit p1, p2, ... pn.
Profit will only be awarded or earned if the job is completed on or before the deadline.
We assume that each job takes unit time to complete.
The objective is to earn maximum profit when only one job can be scheduled or processed at
any given time.
14. What is spanning tree?List out the methods to construct spanning tree?
A spanning tree is a subgraph of Graph G, which has all the vertices (n vertices) covered
with minimum possible number of edges(n-1 edges) and does not have cycle .
Hence, a spanning tree does not have cycles and it cannot be disconnected..
1.Prim's method
2.Kruskal's method
15. what is optimal merge pattern?
Definition: Merge n sorted sequences of different lengths into one output while minimizing reads.
Only two sequences can be merged at once. At each step, the two shortest sequences are merged.
Formal Definition: Let D={n1, ... , nk} be the set of lengths of sequences to be merged. Take the two
shortest sequences, ni, nj∈ D, such that n≥ ni and n≥ nj ∀ n∈ D. Merge these two sequences. The new
set D is D' = (D - {ni, nj}) ∪ {ni+nj}. Repeat until there is only one sequence.
For example, let there be a set of sorted sequences of the following lengths: D={3,5,7,9,12,14,15,17}.
Building the optimal merge tree goes as follows.
3 5 7 9 12 14 15 17
8 7 9 12 14 15 17
/ \
3 5
15 9 12 14 15 17
/ \
8 7
/ \
3 5
15 21 14 15 17
/ \ / \
8 7 9 12
/ \
3 5
29 21 15 17
/ \ / \
14 15 9 12
/ \
8 7
/ \
3 5
29 21 32
/ \ / \ / \
14 15 9 12 15 17
/ \
8 7
/ \
3 5
50 32
/ \ / \
/ \ 15 17
29 21
/ \ / \
14 15 9 12
/ \
8 7
/ \
3 5
82
/ \
/ \
/ \
50 32
/ \ / \
/ \ 15 17
29 21
/ \ / \
14 15 9 12
/ \
8 7
/ \
3 5
We have 3 programs which are stored in a sequential magnetic tape,the length of the
programs are (l1.l2,l3).
The problem is to find the order of the program which minimize the mean retrieval
time of those programs.
Since the cost of multiplication is more expensive than addition,we can reformulate the
equation for Cij using fewer number of multiplication and more number of additions and subtractions.
The complexity of the Strasson method can be written as the following recurrence relation,
T(n)= { b if n==2
{T(n/2)tan2 if n>2
PART A
The principle of optimality states that an optimal sequence of decisions has the property that
whatever the initial state and decisions are, the remaining decisions must constitute an
optimal decision sequence with regard to state resulting from first decision.
A multistage graph G = (V, E) is a directed graph where vertices are partitioned
into k (where k > 1) number of disjoint subsets S = {s1,s2,…,sk}such that edge (u, v) is in
E, then u Є si and v Є s1 + 1 for some subsets in the partition and |s1| = |sk| = 1.
The vertex s Є s1 is called the source and the vertex t Є sk is called sink.
G is usually assumed to be a weighted graph. In this graph, cost of an edge (i, j) is
represented by c(i, j). Hence, the cost of path from source s to sink t is the sum of costs of
each edges in this path.
The multistage graph problem is finding the path with minimum cost from source s to
sink t.
6. What are the drawback of dynamic programming?( NOV 2015)
Time and space requirements are high, since storage is needed for all level
Optimality should be checked at all levels.
An old application of the TSP is to schedule the collection of coins from payphones
throughout a given region
To find shortest routes through selections of airports in the world
solver to construct radiation hybrid maps as part of their ongoing work in genome
sequencing.
ADDITIONAL:
Unit-4
Part-A
1 2 3 4
8 7 6 5
Cycle1:1,3,4,5,6,7,8,2,1
Cycle2:1,2,8,7,6,5,4,3,1.
1 2 3
5 4
Scheduling
Register allocation
Pattern Matching
The objective of the knapsack problem is to fill the knapsack with capacity ’m’ by including or
excluding full amounts of different n objectsin which summation of weight of the object must be less
than or equal to the capacity of the knapsack ‘m' and summation of profit of the object must be
maximum
n
Maximize ∑ pixi
i=1
n
Subject to ∑ wixi<= m
i=1
xi ϵ {0,1}
where 1<=i<=n
7.Give the implicit and explicit constraint for 8 queen problem. ( NOV 2015)
Explicit Constraints:
Si ={1,2,3,4,5,6,7,8},1 i 8
xiєSi
Solution space consist of 88 tuples.
Implicit Constraints:
i. No 2 xi’s can be the same.
i.e)All queens must be on different columns and diagonals
From the implicit constraints,
All solutions are permutations of the 8 tuple.It reduces the size of the solution space from
88 tuples to 8! tuples.
A vertex ‘v’ in a connected graph ‘G’ is an articulation point, iff the deletion of vertex v together with
all vertices incident to v disconnects the graph into 2 or more
non empty components.
8.What are the requirements that are needed for performing backtracking(April 2014)
To solve any problem using backtracking, it requires that all the solutions satisfy a complex set of
constraints. They are:
i. Explicit constraints.
ii. Implicit constraints.
Explicit Constraints:
Explicit constraints are rules that restrict each Xi to take values only from a given set.
All tupules that satisfy the explicit constraint define a possible solution space for I.
Implicit constraints:
The implicit constraint determines which of the tuples in the solution space I can actually
satisfy the criterion functions.
ADDITIONAL:
Explicit Constraints:
Explicit constraints are rules that restrict each Xi to take values only from a given set.
All tuples that satisfy the explicit constraint define a possible solution space for I.
Example
xi = 0 or 1 or Si={0,1}.
li ¿ xi ¿ ui or Si={a : li ¿ a ¿ ui }
Implicit constraints:
The implicit constraint determines which of the tuples in the solution space I can actually
satisfy the criterion functions.
We are given ‘n’ positive numbers called weights and we have to find all
combinations of these numbers whose sum is M. This is called as sum of subsets problem
.
11.What is graph coloring problem?
Let ‘G’ be a graph and ‘m’ be a given positive integer. If the nodes of ‘G’ can be
colored in such a way that no two adjacent nodes have the same color. Yet only ‘M’ colors
are used. So it’s called M-color ability decision problem.
Explicit Constraints:
xi є Si={0,1}
Implicit Constraints:
k n k
Bk(x1,x2,….xn)=true iff ∑ wixi + ∑ ≥ m and ∑ wixi + wk+1 ≤ m
i=1 i=k+1 i=1
Explicit Constraints:
The graph G can be colored using the smallest integer ‘m’. This integer is referred to
as chromatic number of the graph.
If ‘d’ is the degree of the given graph, then it can be colored with d+1 colors.
xi є Si={1,2,3,.. m colors}
Implicit Constraints:
The two adjacent nodes should not have same color
Explicit Constraints:
Let G=(V,E) be a connected graph with ‘n’ vertices. A Hamiltonian Cycle is a round trip path
along ‘n’ edges of G that visits every vertex once and returns to its starting position.
xi є Si={set of vertices Vi in G}
Implicit Constraints:
Each vertices should be visited only once.
Every vertex I G must be visited
There must be a path between the vertex n to vertex 1
The graph G can be colored using the smallest integer ‘m’. This integer is referred to
as chromatic number of the graph. If ‘d’ is the degree of the given graph, then it can be
colored with d+1 colors that is m=d+1
16.How do you check if two queens are in same column?
The function, which is used to check two queens are in same column is [i=,x(j)] which
gives position of the ith queen, where i represents the row and x (j) represents the column
position.
a[3,1],a[4,2],a[5,3],a[6,4],a[7,5],a[8,4]
Also, every element on the same diagonal that runs from upper right to lower left
has the same row + column value.
Suppose two queens are in same position (i,j) and (k,l) then two queens lie on the
same diagonal , if and only if |j-l|=|i-k|.
2 3
5 4
Articulation Point
A vertex ‘v’ in a connected graph ‘G’ is an articulation point, iff the deletion of vertex v together with
all vertices incident to v disconnects the graph into 2 or more
non empty components.
Biconnected Graph:
A graph ‘G’ is biconnected, iff it contains no articulation points.
Any problem for which the answer is either zero or one is called decision problem Any
problem that involves the identification of an optimal (maximum or minimum) value of a
given cost function is called optimization problem
Deterministic Algorithm:
The result(outcome)of the operation in the algorithm is uniquely defined with
in the algorithm
Non Deterministic Algorithm:
The outcomes of the operation in the algorithm are not uniquely defined but
are limited to specific set of possibilities
Unit-5
Part-A
Used to find optimal solution to many optimization problems, especially in discrete and
combinatorial optimization
Systematic enumeration of all candidate solutions, discarding large subsets of fruitless
candidates by using upper and lower estimated bounds of quantity being optimized
Branch and Bound solve these problems relatively quickly
A search strategy that uses a cost function ĉ (x) = f (h (x)) + ĝ(x) to select the next E-node
would always choose for its next E-node a live node with least ĉ (.). Hence such a search
strategy is called an LC – search.
o It is similar to the fractional knapsack problem but the only difference is,
o In fractional knapsack we can also take fraction of object (part of object)to fill the
o knapsack.
o In 0/1 knapsack either the object will fully be placed(x i=1) or the object is not placed
at
o all(xi=0).
o If n objects are given with weight wi and profit pi where i=1 to n and capacity of the
knapsack 'm' is also given.
o The objective of the problem is to fill the knapsack with the help of the given object
and the resulting profit must be maximum.
maximize -∑ pi xi 1<=i<=n
xi Є {0,1}.
Definition: - In Clique, every vertex is directly connected to another vertex, and the
number of vertices in the Clique represents the Size of Clique.
The following figure shows a graph that has a clique cover of size 3.
ADDITIONAL:
8 Puzzle Problem. The 8 puzzle consists of eight numbered, movable tiles set in a 3x3 frame.
One cell of the frame is always empty thus making it possible to move an adjacent numbered
tile into the empty cell.
The objective is to place the numbers on tiles to match final configuration using the empty
space.
We can slide four adjacent (left, right, above and below) tiles into the empty space. Such
a puzzle is illustrated in following diagram.
We have discussed following solutions
In cases of a minimization problem, a lower bound tells us the minimum possible solution if
we follow the given node. For example, in Job Assignment Problem, we get a lower bound by
assigning least cost job to a worker.
5.What is pruning?
Pruning is a technique in search algorithms that reduces the size of decision trees by
removing sections of the tree that provide little power to classify instances. Pruning reduces
the complexity of the final classifier, and hence improves predictive accuracy by the
reduction of overfitting.