ADA QB Sol
ADA QB Sol
Unit-1
1.What is an algorithm?
3. What is pseudocode?
Ans:-The functions 2n and n! are exponential growth functions, because these two
functions grow so fast that their values become astronomically large even for rather
smaller values of n.
The worst-case efficiency of an algorithm is its efficiency for the worst-case input of size
n, which is an input or inputs of size n for which the algorithm runs the longest among
all possible inputs of that size.
Ans:-The average case efficiency of an algorithm is its efficiency for an average case
input of size n. It provides information about an algorithm behavior on a “typical” or
“random” input.
Ans:-A function t(n) is said to be in O(g(n)), denoted by 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 exists some positive
constant c and some nonnegative integer n0 such that
T(n) ≤ cg(n) for all n ≥ n0
A function t(n) is said to be in Ω(g(n)), denoted by t(n) ɛΩ(g(n)), if t(n) is bounded below
by some constant multiple of g(n) for all large n, i.e., if there exists some positive
constant c and some nonnegative integer n0 such that T(n) ≥ cg(n) for all n ≥ n0
A function t(n) is said to be in Θ(g(n)), denoted by t(n) ɛΘ(g(n)), if t(n) is bounded both
above & below by some constant multiple of g(n) for all large n, i.e., if there exists some
positive constants c1 & c2 and some nonnegative integer n0 such that c2g(n) ≤ t(n) ≤
c1g(n) for all n ≥ n0
Q. Show that
By substitution method we have
• Start with an empty left hand and the cards face down on the table.
• Then remove one card at a time from the table, and insert it into the correct
position in the left hand.
• To Þnd the correct position for a card, compare it with each of the cards already
Unit-2
Ans:- Both quicksort and mergesort use the divide-and-conquer technique in which the
given array is partitioned into subarrays and solved. The difference lies in the technique
that the arrays are partitioned. For mergesort the arrays are partitioned according to their
position and in quicksort they are partitioned according to the element values.
Ans:- Binary search is a remarkably efficient algorithm for searching in a sorted array. It
works by comparing a search key K with the arrays middle element A[m]. if they match
the algorithm stops; otherwise the same operation is repeated recursively for the first half
of the array if K < A[m] and the second half if K > A[m].
Ans: - knapsack problem is a problem in combinatorial optimization: Given a set of items, each
with a weight and a value, determine the count 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 useful items.
5. What is greedy technique?
Ans:- Greedy technique suggests a greedy grab of the best alternative available in the hope that
asequence of locally optimal choices will yield a globally optimal solution to the entire problem.
The choice must be made as follows .
Feasible : It has to satisfy the problem’s constraints ._
Locally optimal : It has to be the best local choice among all feasible
choices available on that step. .
Irrevocable : Once made, it cannot be changed on a subsequent step of the algorithm
Ans:- A sorting algorithm based on divide and conquer. Its worst-case running time has
a lower order of growth than insertion sort. Because we are dealing with subproblems, we state
each subproblem as sorting a subarray A[p . . r ]. Initially, p = 1 and r = n, but these values
change as we recurse through subproblems.
To sort A[p . . r ]:
Divide by splitting into two subarrays A[p . . q] and A[q + 1 . . r ], where q is the
halfway point of A[p . . r ].
Conquer by recursively sorting the two subarrays A[p . . q] and A[q + 1 . . r ].
Combine by merging the two sorted subarrays A[p . . q] and A[q + 1 . . r ] to produce
a single sorted subarray A[p . . r ]. To accomplish this step, we.ll deÞne a
procedure MERGE(A, p, q, r ).The recursion bottoms out when the subarray has just 1 element, so
that it.s trivially sorted.
MERGE-SORT(A, p, r )
if p < r _ Check for base case
then q ← (p + r)/2 _ Divide
MERGE-SORT(A, p, q) _ Conquer
MERGE-SORT(A, q + 1, r ) _ Conquer
MERGE(A, p, q, r ) _ Combine
Initial call: MERGE-SORT(A, 1, n)
[It is astounding how often students forget how easy it is to compute the halfway
point of p and r as their average (p + r)/2. We of course have to take the ßoor
to ensure that we get an integer index q. But it is common to see students perform
calculations like p +(r − p)/2, or even more elaborate expressions, forgetting the
easy way to compute an average.]
QUICKSORT(A, p, r )
if p < r
then q ←PARTITION(A, p, r )
QUICKSORT(A, p, q − 1)
QUICKSORT(A, q + 1, r )
Initial call is QUICKSORT(A, 1, n).
Partitioning
Partition subarray A[p . . r ] by the following procedure:
PARTITION(A, p, r )
x ← A[r ]
i←p−1
for j ← p to r − 1
do if A[ j ] ≤ x
then i ← i + 1
exchange A[i ] ↔ A[ j ]
exchange A[i + 1] ↔ A[r ]
return i + 1
• PARTITION always selects the last element A[r ] in the subarray A[p . . r ] as the
Fractional knapsack problem: Like the 0-1 knapsack problem, but can take fraction
of an item.
FRACTIONAL-KNAPSACK(v,w,W)
load ← 0
i←1
while load < W and i ≤ n
do if wi ≤ W − load
then take all of item i
else take (W − load)/wi of item i
add what was taken to load
i ←i + 1
Greedy doesn.t work for the 0-1 knapsack problem. Might get empty space, which
lowers the average value per pound of the items taken.
i123
vi 60 100 120
wi 10 20 30
vi/wi 6 5 4
W = 50.
Greedy solution:
• Take items 1 and 2.
No leftover capacity.
Unit-3
1. what is Dynamic Programming
Ans:- • Not a speciÞc algorithm, but a technique (like divide-and-conquer).
• Developed back in the day when .programming. meant .tabular method. (like
linear programming). Doesn.t really refer to computer programming.
• Used for optimization problems:
Four-step method
1. Characterize the structure of an optimal solution.
2. Recursively deÞne the value of an optimal solution.
3. Compute the value of an optimal solution in a bottom-up fashion.
4. Construct an optimal solution from computed information.
Time and space requirements are high, since storage is needed for all level.
Optimality should be checked at all levels.
Routing a postal van to pick up mail from boxes located at n different sites. Using a robot
arm to tighten the nuts on some piece of machinery on an assembly line.
Production environment in which several commodities are manufactured on
the same set of machines.
The way we parenthesize a chain of matrices can have a dramatic impact on the cost of
evaluating the product. Consider first the cost of multiplying two matrices. The standard
algorithm is given by the following pseudocode. The attributes rows and columns are the
numbers of rows and columns in a matrix.
MATRIX-MULTIPLY(A, B)
1 if columns[A] ≠ rows[B]
2 then error "incompatible dimensions"
3 else for i ← 1 to rows[A]
4 do for j ← 1 to columns[B]
5 do C[i, j] ← 0
6 for k ← 1 to columns[A]
7 do C[i, j] ← C[i, j] + A[i, k] · B[k, j]
8 return C
MATRIX-CHAIN-ORDER(p)
1 n ← length[p] - 1
2 for i ← 1 to n
3 do m[i, i] ← 0
4 for l ← 2 to n ▹l is the chain length.
5 do for i ← 1 to n - l + 1
6 do j ← i + l - 1
7 m[i, j] ← ∞
8 for k ← i to j - 1
9 do q ← m[i, k] + m[k + 1, j] + pi-1 pkpj
10 if q < m[i, j]
11 then m[i, j] ← q
12 s[i, j] ← k
13 return m and s
PRINT-OPTIMAL-PARENS(s, i, j)
1 if i = j
2 then print "A"i
3 else print "("
4 PRINT-OPTIMAL-PARENS(s, i, s[i, j])
5 PRINT-OPTIMAL-PARENS(s, s[i, j] + 1, j)
6 print ")"
PRINT-ALL-PAIRS-SHORTEST-PATH(Π, i, j)
1 if i = j
2 then print i
3 else if πij = NIL
4 then print "no path from" i "to" j "exists"
5 else PRINT-ALL-PAIRS-SHORTEST-PATH(Π, i, πij)
6 print j
Now, let be the minimum weight of any path from vertex i to vertex j that contains at
most
m edges. When m = 0, there is a shortest path from i to j with no edges if and only if i = j.
Thus,
For m ≥ 1, we compute as the minimum of (the weight of the shortest path from i to j
consisting of at most m - 1 edges) and the minimum weight of any path from i to j
consisting
of at most m edges, obtained by looking at all possible predecessors k of j. Thus, we
recursively define
EXTEND-SHORTEST-PATHS(L, W)
1 n ← rows[L]
2 let be an n × n matrix
3 for i ← 1 to n
4 do for j ← to n
5 do
6 for k ← 1 to n
7 do
8 return L′
This section shows how depth-first search can be used to perform a topological sort of a
directed acyclic graph, or a "dag" as it is sometimes called. A topological sort of a dag G
=
(V, E) is a linear ordering of all its vertices such that if G contains an edge (u, v), then u
appears before v in the ordering.
the topologically sorted dag as an ordering of vertices along a horizontal line such that all
directed edges go from left to right.
Unit – 4
1.Explain Backtracking
The principal idea is to construct solutions one component at a time and evaluate such
partially constructed candidates as follows. If a partially constructed solution can be
developed further without violating the problem's constraints, it is done by taking the first
remaininig legitimate option for the next component.
If there is no legitimate option for the next component, no alternatives for any remaining
component need to be considered. In this case, the algorithm backtracks to replace the
last component of the partially constructed solution with its next option
2.What are the requirements that are needed for performing Backtracking?
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.
All the paths from the root of the organization tree to all the nodes is called as state space of
the problem.
The graph coloring problem asks us to assign the smallest number of colors to vertices of
a graph so that no two adjacent vertices are the same color.
Let G be a graph and m be a given positive integer. We want to discover whether 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.
Find the most valuable subset of n items of given positive integer weights and values that
fit into a knapsack of a given positive integer capacity.
Live Node:
A node which has been generated and all of whose children have not yet been
generated is called as a live node.
Dead node: It is defined as a generated node, which is to be expanded further all
of whose children have been generated.
E – node :Any live node whose children are currently being
generated is called as a E – node.
Unit-5
1.Define Branch-and-Bound method.
The term Branch-and-Bound refers to all the state space methods in which all children of
the E-node are generated before any other live node can become the E- node.
It is a DFS like state space tree search in which the list of live node is a last in first out order. The
unexplored node are stored in stack.
The problems whose solutions have computing times are bounded by polynomials of small
degree.
Any problem for which the answer is either zero or one is called decision
problem.
Class P is a class of decision problems that can be solved in polynomial time by(deterministic)
algorithms. This class of problems is called polynomial.
Class NP is the class of decision problems that can be solved by nondeterministic polynomial
algorithms.Most decision problems are in NP. First of all, this class includes all the problems in
P. This class of problems is called Nondeterministic polynomial.
The notion of an NP-hard problem can be defined more formally by extending the
notion of polynomial reducability to problems that are not necessary in class NP
including optimization problems.