0% found this document useful (0 votes)
1K views

DAA Questions & Answers

This document provides answers to frequently asked data structures and algorithms (DAA) interview questions. It discusses key DAA concepts like algorithms, asymptotic notation, time complexity, sorting algorithms like bubble sort, selection sort, quicksort, and merge sort. It also covers searching algorithms like linear search and binary search. Other topics include NP-completeness, algorithm efficiency, orders of algorithms, and dynamic programming.

Uploaded by

Tushar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

DAA Questions & Answers

This document provides answers to frequently asked data structures and algorithms (DAA) interview questions. It discusses key DAA concepts like algorithms, asymptotic notation, time complexity, sorting algorithms like bubble sort, selection sort, quicksort, and merge sort. It also covers searching algorithms like linear search and binary search. Other topics include NP-completeness, algorithm efficiency, orders of algorithms, and dynamic programming.

Uploaded by

Tushar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

DAA Interview Questions and Answers

A list of top frequently asked DAA Interview Questions and answers are given


below.

1) What is Algorithm?
The name 'Algorithm' refers to the sequence of instruction that must be followed to
clarify a problem.

The logical description of the instructions which may be executed to perform an


essential function.

Algorithms are usually generated independent of primary languages, i.e., an


algorithm can be achieved in more than one programming language.

00:27/05:45
10 Sec

2) What is Asymptotic Notation?


A way to represents the behavior of functions in the limit or without bounds.

The notations are described in terms of methods whose domains are the set of
natural numbers N= {0, 1, 2}

Such notations are convenient for defining the worst-case running time function T(n).

It can also be extended to the domain of the real number.

3) What is the time complexity of Algorithm?


The time complexity of an algorithm denoted the total time needed by the program
to run to completion. It is generally expressed by using the big O notation.

4) Explain the algorithms for Bubble sort and give a suitable


example.
In bubble sort technique the list is split into two sub-lists sorted and unsorted. The
smallest component is bubbled from unsorted sub-list. After moving the smallest
component, the imaginary wall moves one element ahead. The bubble sort was
initially written to bubble up the highest item in the list. But there is no difference
whether the highest / lowest item is bubbled. This technique is simple to understand
but time-consuming. In this type, two successive components are compared, and
swapping is done. Thus, step-by-step entire array items are checked, given a list of 'n'
elements the bubble sort needed up to n-1 passes to sort the record.

5) Explain the algorithm for selection sort and give a suitable


example.
In selection sort, the list is split into two sub-lists sorted and unsorted. These two lists
are split by an imaginary wall. We find the smallest item from unsorted sub-list and
swap it to the starting. And the wall moves one item ahead, as the sorted file is
increases and an unsorted file is decreased.

Assume that we have a file on n elements. By applying a selection sort, the first item
is compared with all remaining (n-1) elements. The smallest item is placed at the first
location. Again, the second item is compared with the remaining (n-1) elements. At
the time of the comparison, the smaller item is swapped with a bigger item. Similarly,
the entire array is checked for smallest component, and then swapping is done
accordingly. Here we need n-1 passes or repetition to rearrange the data completely.

6) Explain the algorithms for QUICK sort (partition exchange


sort) and give a suitable example.
Quicksort is based on division. It is also called a partition exchange sorting. The basic
concept of quick sort method is to pick one item from an array and rearranges the
remaining item around it. This element split the main list into two sublists. This
chosen item is known as a pivot. Once the pivot is selected, then it shifts all the
components less than pivot to the left of value pivot, and all the items higher than
the pivot are shifted to the right side. This process of choosing pivot and division the
list is tested recursively until sub-lists consisting of only one element.

7) What is NP-Complete?
An NP-Complete problem is a problem in NP that is as difficult as any other trouble
in this class because any other dispute in NP can be decreased to it in Polynomial-
time.

8) Differentiate Time Efficiency and Space Efficiency?


Time Efficiency measured by estimate the number of times the essential algorithms
functions are executed. Space Efficiency is measured by calculating the number of
additional memory units consumed by the algorithm.

9) What is the Order of Algorithm?


The order of algorithm is standard documentation of an algorithm that has been
developed to represent a task that bound the estimated time for algorithms. The
order of an algorithm is a method of defining its efficiency. It is commonly referred
to as O-notation.

10) What is Brute Force?


Brute Force is a straightforward method for solving problem, usually directly based
on the problem's statement and descriptions of the concepts involved.

11) What are the various criteria used to improve the


effectiveness of the algorithm?
Input- Zero or more quantities are externally provided.

Output- At least one quantity is composed

Definiteness- Each instruction is simple and unambiguous

Finiteness- If we trace out the instructions of an algorithm, then for all step the
algorithm complete after a finite number of steps

Effectiveness- Every instruction must be elementary.

12) Explain the algorithms for Merge sort and give a suitable
example.
The basic concept of merge sort has split the list into two smaller sub-lists of
relatively equal size. Recursively repeat this method until only one item is left in the
sub-list. After this, various sorted sub-lists are combined to form a sorted parent list.
This method goes on recursively till the original sorted list arrived.

13) What is a Linear Search?


Linear search method is also called a sequential search technique. The linear search is
a technique of searching an item in a list in sequence. In this technique, the array is
searched for the required item from the starting of the list/array or the last
component to the first component of the array and continues until the element is
found or the entire list/array has been searched.

Advantages
1. It is an easy and conventional technique of searching for information. The
linear or sequential name implies which the elements are stored in a
systematic manner.
2. The item in the list can be in any order. i.e., the linear search can be tested on
the sorted or unsorted linear data structure.

Disadvantages

1. This procedure is insufficient when a large number of item is present in the


list.
2. It consumes more time and decreases the retrieval rate of the system.

Time complexity: O(n)

14) What is Binary Search?


Binary search is higher than the linear search. However, it cannot be tested on the
unsorted data structure. The binary search is based on the method divide-and-
conquer. The binary search begins by testing the data in the middle component of
the array. This determines an object is whether in the first half or second half. If the
object is in the first half, we do not need to check the second half and if it is in the
second half no need to check in first half. Similarly, we repeat this procedure until we
find an object in the list or not found from the list. Here we need three variables to
identify first, last, and middle elements.

To implement a binary search technique, the item must be in sorted order.

Binary Search is performed as follows:

o The key is compared with an element in the middle position of an array


o If the key matches with an element, return it, and stop.
o If the key is less than mid position element, then the element to be found
must be in the first half of the array, otherwise it must be in the second half of
the array.
o Repeat the method for lower (or upper half) of the array until the component
is found.

15) Explain the algorithms for insertion sort and give a suitable
example.
Both the selection and bubble sorts exchange items. But insertion sort does not
exchange items. In insertion sort, the item is inserted at an appropriate place similar
to card insertion. Here the list is split into two parts sorted and unsorted sub-lists. In
each pass, the first component of unsorted sublist is picked up and moved into the
sorted sublist by inserting it in a relevant position. Suppose we have 'n' items, we
need n-1 passes to sort the items.

16) What do you mean by the optimal solution?


Given the problem with inputs, we recover a subset that appeases come constraints.
Any subset that satisfies these constraints is known as a feasible solution. A feasible
solution, which either maximizes or minimizes a given purpose method is known as
an optimal solution.

17) Why Hashing?


Suppose we have a huge information set stored in an array. The amount of time
needed to lookup an item in the array is either O(log n) or O(n) based on whether
the array is sorted or not. If the array is sorted, then a procedure such as a binary
search can be used to search the array. Otherwise, the array must be searched
linearly. Either method may not be desirable if we need to process a very high data
set. Therefore we discuss a new procedure called hashing that allows us to update
and fetch any entry in constant time O(1). The constant time or O(1) performance
defines the amount of time to operate does not depend on data size n.

18) Explain how the encryption algorithm works?


Encryption is the step of converting plaintext into a secret code format called
"Cliphertext". To convert the content, the algorithm uses a string of bits referred to
as "keys" for estimation. The larger the key, the higher the number of potential
patterns for generating the ciphertext. Most encryption algorithm use codes fixed
blocks of input that have a length of about 64 to 128 bits, while some uses stream
technique.

19) What is Dynamic Programming?


DP is another method for problems with optimal substructure: An optimal solution to
a problem include optimal solutions to subproblems. This doesn't necessarily define
that every optimal solution to a subproblem will contribute to the primary solution.

For divide and conquer (top-down), the subproblems are independent, so we can


resolve them in any order.
For the greedy technique (bottom-up), we can always choose the "right"
subproblem by a greedy choice.

In dynamic programming, we solve many subproblems and save the results: not all
of them will contribute to solving the bigger problem. Because of optimal
substructure, we can be sure that at least some of the subproblems will be useful.

20) What is the Knapsack Problem?


Given n elements of known weights wiand values vi, i=1, 2? n, and a knapsack of
capacity W, find the most valuable subsets of the elements that fit the knapsack. It is
convenient to order the elements of a given instance in descending order by their
value-to-weight ratios. Then the first element gives the best payoff per weight unit,
and the last one gives the worst payoff per weight unit.

21) What is Warshall's Algorithm?


Warshall's algorithm is a function of dynamic programming procedure, which is used
to find the transitive closure of a directed graph.

22) What is a Greedy Algorithm?


A greedy technique for an optimization problem always makes the option which look
best at the moment and add it to the current subsolution.

23) List the advantage of the greedy algorithm.

1. The greedy method produces a feasible solution


2. The greedy method is very easy to solve a problem
3. The greedy method implements an optimal solution directly

24) What is Minimum Spanning Trees?


A spanning tree for a linked graph is a tree whose vertex set is the same as the vertex
set of the given graph, and whose edge set is a subgroup of the edge set of the
given graph. i.e., any linked graph will have a spanning tree.

Weight of a spanning tree w (T) is the total of weights of all edges in T. The Minimum
spanning tree (MST) is a spanning tree with the smallest feasible weight.
25) What is Kruskal?s Algorithm?
This is a greedy method. A greedy method chooses some local optimum (i.e.,
selection an edge with the smallest weight in an MST).

Kruskal's algorithm works as follows:

Take a graph with 'n' vertices, keep on adding the shortest (least cost) edge, while
avoiding the generation of cycles, until (n - 1) edges have been added. Frequently
two or more edges may have the same rate. The order in which the edges are
decided, in this method, does not matter. Different Minimum spanning tree may
result, but they will all have the same total price, which will always be the minimum
cost.
26) What is Sorting Network?
A sorting network is a numerical model of a network of wires and comparator
modules that is used to sort a series of numbers. Each comparator connects two
wires and sorts the values by outputting the smaller value to one wire, and the higher
to the other. The main difference between sorting networks and comparison
sorting algorithms is that with a sorting network, the series of comparisons is set in
advance, regardless of the result of previous comparisons. This independence of
comparison series is useful for parallel execution of the methods.

27) What is Floyd's algorithm?


Floyd's algorithm is a function, which is used to find all the pairs shortest paths
problem. Floyd's algorithm is relevant to both directed and undirected weighted
graph, but they do not include a cycle of a negative length.

28) What is prim's algorithm?


Prim's algorithm is a greedy and efficient technique, which is used to find the
minimum spanning the tree of a weighted linked graph.

29) How efficient is prim's algorithm?


The efficiency of the prim's methods depends on the data structure chosen for the
graph.

30) What is Dijkstra's Algorithm?


Dijkstra's algorithm solves the single-source shortest path method of finding shortest
paths from a given vertex (the source), to another vertex of a weighted graph or
digraph. Dijkstra's algorithm implements a correct solution for a graph with non-
negative weights.

31) What are the huffman trees?


A Huffman tree is a binary tree which reduces the weighted path length from the
root to the leaves, including a set of predefined weights. The essential application of
Huffman trees is a Huffman code.

32) What do you mean by Huffman code?


A Huffman code is an optimal prefix tree variable-length encoding technique which
assign bit strings to characters based on their frequency in a given text.

33) List the advantage of Huffman's encoding?


Huffman's encoding is one of the essential file compression techniques.

1. It is easy
2. It is flexibility
3. It implements optimal and minimum length encoding

34) What is dynamic Huffman coding?


In dynamic Huffman coding, the coding tree is updated each time a new character is
read from the source text. Dynamic Huffman n-coding used to overcome the
disadvantage of the simplest version.

35) What is backtracking?


Depth-first node generation with bounding method is known as backtracking. The
backtracking technique has its virtue the ability to yield the solution with far fewer
than m trials.
36) Write the difference between the Dynamic programming
and Greedy method.
Dynamic programming

1. Many numbers of decisions are generated.


2. It gives an optimal solution always

Greedy method

1. Only one sequence of decision is generated.


2. It does not require to provide an optimal solution always.

37) What is the use of Dijkstra's algorithm?


Dijkstra's procedure is used to solve the single-source shortest-paths method: for a
given vertex called the source in a weighted linked graph, find the shortest path to all
its other vertices. The single-source shortest-paths process asks for a family of paths,
each leading from the source to various vertex in the graph, though some direction
may have edges in common.

38) What is meant by n-queen Problem?


The problem is to area n-queens on an n-by-n chessboard so that no two queens
charge each other by being same row or in the same column or the same diagonal.

39) What is the state-space tree?


The processing of backtracking is resolved by constructing a tree of choices being
made. This is known as state-space tree. Its root describes an initial state before the
search for a solution starts. The nodes of the first level in the tree describe the
choices made for the first element of the solution, the nodes in the second level
describe the choices for the second element, and so on.

40) What is the assignment problem?


Assigning 'n' people to 'n' jobs so that the total price of the assignment is as low as
possible. The instance of the problem is particularized as an n-by-n cost matrix C so
that the problem can be described as select one element in each row of the matrix so
that no two selected items are in the same column and the total is the smallest
possible.
DESIGN AND ANALYSIS OF ALGORITHMS

UNIT I INTRODUCTION

Definition and properties of an algorithm- Analysis of algorithms. Divide


and Conquer -The general method- Binary search- Finding maximum and
minimum element- Analysis of Merge sort- Analysis of Quick sort- Analysis
of Selection sort- Analysis of Heap sort

1. What is an algorithm?
An algorithm is a sequence of unambiguous instructions for solving a problem.
i.e., for obtaining a required output for any legitimate input in a finite amount of time

2. What do you mean by Amortized Analysis?


Amortized analysis finds the average running time per operation over a worst case
sequence of operations. Amortized analysis differs from average-case performance in that
probability is not involved; amortized analysis guarantees the time per operation over
worst-case performance.

3. What are important problem types? (or) Enumerate some important types of
problems.
1. Sorting 2. Searching
3. Numerical problems 4. Geometric problems
5. Combinatorial Problems 6. Graph Problems
7. String processing Problems
4. Name some basic Efficiency classes
1. Constant 2. Logarithmic 3. Linear 4. nlogn
5. Quadratic 6. Cubic 7. Exponential 8. Factorial

5. What are algorithm design techniques?


Algorithm design techniques ( or strategies or paradigms) are general approaches
to solving problems algorithmatically, applicable to a variety of problems from different
areas of computing. General design techniques are:
(i) Brute force (ii) divide and conquer
(iii) decrease and conquer (iv) transform and concquer
(v) greedy technique (vi) dynamic programming
(vii) backtracking (viii) branch and bound

6. How is an algorithm’s time efficiency measured?


Time efficiency indicates how fast the algorithm runs. An algorithm’s time
efficiency is measured as a function of its input size by counting the number of times its
basic operation (running time) is executed. Basic operation is the most time consuming
operation in the algorithm’s innermost loop.

7. What is Big ‘Oh’ 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 integers n0 such that
t(n) ≤ cg(n) for all n≥ n0

8. What is an Activation frame?


It is a storage area for an invocation of recursive program (parameters, local
variables, return address/value etc.). Activation frame allocated from frame stack pointed
by frame pointer.

9. Define order of an algorithm


Measuring the performance of an algorithm in relation with the input size n is
known as order of growth.

10. What is recursive call?


Recursive algorithm makes more than a single call to itself is known as recursive
call. An algorithm that calls itself is direct recursive.An algorithm”A” is said to be
indirect recursive if it calls another algorithm which in turn calls “A”.

11. What do you mean by stepwise refinement?


In top down design methodology the problem is solved in sequence (step by step)
is known as stepwise refinement.

12. How is the efficiency of the algorithm defined?


The efficiency of an algorithm is defined with the components.
(i) Time efficiency -indicates how fast the algorithm runs
(ii) Space efficiency -indicates how much extra memory the algorithm needs

13. Define direct recursive and indirect recursive algorithms.


Recursion occurs where the definition of an entity refers to the entity itself.
Recursion can be direct when an entity refers to itself directly or indirect when it refers to
other entities which refers to it. A (Directly) recursive routine calls itself. Mutually
recursive routines are an example of indirect recursion. A (Directly) recursive data type
contains pointers to instances of the data type.

14. What are the characteristics of an algorithm?


Every algorithm should have the following five characteristics
(i) Input
(ii) Output
(iii) Definiteness
(iv) Effectiveness
(v) Termination
Therefore, an algorithm can be defined as a sequence of definite and effective
instructions, which terminates with the production of correct output from the given input.
In other words, viewed little more formally, an algorithm is a step by step formalization
of a mapping function to map input set onto an output set.

15. What do you mean by time complexity and space complexity of an algorithm?
Time complexity indicates how fast the algorithm runs. Space complexity deals with
extra memory it require. Time efficiency is analyzed by determining the number of
repetitions of the basic operation as a function of input size. Basic operation: the
operation that contributes most towards the running time of the algorithm The running
time of an algorithm is the function defined by the number of steps (or amount of
memory) required to solve input instances of size n.

16. Define Big Omega Notations


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 for all n ≥ n0

17. What are the different criteria used to improve the effectiveness of algorithm?
(i) The effectiveness of algorithm is improved, when the design, satisfies the following
constraints to be minimum.
Time efficiency - how fast an algorithm in question runs.
Space efficiency – an extra space the algorithm requires
(ii) The algorithm has to provide result for all valid inputs.

18. Analyze the time complexity of the following segment:


for(i=0;i<N;i++)
for(j=N/2;j>0;j--)
sum++;
Time Complexity= N * N/2
= N2 /2
Є O(N2)

19. write general plan for analyzing non-recursive algorithms.


i. Decide on parameter indicating an input’s size.
ii. Identify the algorithm’s basic operation
iii. Checking the no.of times basic operation executed depends on size of input.if it
depends on some additional property,then best,worst,avg.cases need to be investigated
iv. Set up sum expressing the no.of times the basic operation is executed.
(establishing order of growth)
20. How will you measure input size of algorithms?
The time taken by an algorithm grows with the size of the input. So the running
time of the program depends on the size of its input. The input size is measured as the
number of items in the input that is a parameter n is indicating the algorithm’s input size.

21. Define the terms: pseudocode, flow chart


A pseudocode is a mixture of a natural language and programming language like
constructs. A pseudocode is usually more precise than natural language. A flowchart is a
method of expressing an algorithm by a collection of connected geometric shapes
containing descriptions of the algorithm’s steps.

22. write general plan for analyzing recursive algorithms.


i. Decide on parameter indicating an input’s size.
ii. Identify the algorithm’s basic operation
iii. Checking the no.of times basic operation executed depends on size of input.if it
depends on some additional property,then best,worst,avg.cases need to be investigated
iv. Set up the recurrence relation,with an appropriate initial condition,for the number
of times the basic operation is executed
v. Solve recurrence (establishing order of growth)

23. What do you mean by Combinatorial Problem?


Combinatorial Problems are problems that ask to find a combinatorial object-such
as permutation, a combination, or a subset--that satisfies certain constraints and has some
desired property.

24. Define Big Theta Notations


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 multiple of g(n) for all large n, i.e., if there
exist some positive constants c1 and c2 and some nonnegative integer n0 such that
c1 g(n) ≤t(n) ≤ c2g(n) for all n ≥ n0

25. What is performance measurement?


Performance measurement is concerned with obtaining the space and the time
requirements of a particular algorithm.

26. What is an algorithm?


An algorithm is a finite set of instructions that, if followed, accomplishes a
particular task. In addition, all algorithms must satisfy the following criteria:
1) input
2) Output
3) Definiteness
4) Finiteness
5) Effectiveness.

27. Define Program.


A program is the expression of an algorithm in a programming language.
Sometimes works such as procedure, function and subroutine are used synonymously
program.

28. What is recursive algorithm?


An algorithm is said to be recursive if the same algorithm is invoked in the body.

29. What is space complexity and time complexity ?


The space complexity of an algorithm is the amount of memory it needs to run to
completion. The time complexity of an algorithm is the amount of computer time it needs
to run to completion.

30. Give the two major phases of performance evaluation


Performance evaluation can be loosely divided into two major phases:
(i) a prior estimates (performance analysis)
(ii) a Posterior testing(performance measurement)

31. Define input size.


The input size of any instance of a problem is defined to be the number of
words(or the number of elements) needed to describe that instance.

32. Define best-case step count.


The best-case step count is the minimum number of steps that can be executed for
the given parameters.

33. Define worst-case step count.


The worst-case step count is the maximum number of steps that can be executed
for the given parameters.

34. Define average step count.


The average step count is the average number of steps executed an instances with
the given parameters.

35. Define Little “oh”.


The function f(n) = 0(g(n)) iff
Lim f(n) =0
n →∞ g(n)

36. Define Little Omega.


The function f(n) = ω (g(n)) iff
Lim f(n) =0
n →∞ g(n)

37. Write algorithm using iterative function to fine sum of n numbers.


Algorithm sum(a,n)
{ S : = 0.0
For i=1 to n do
S : - S + a[i];
Return S;
}

38. Write an algorithm using Recursive function to fine sum of n numbers,


Algorithm Rsum (a,n)
{
If(n≤0) then
Return 0.0;
Else Return Rsum(a, n- 1) + a(n);

39. Define the divide an conquer method.


Given a function to compute on ‘n’ inputs the divide-and-comquer strategy
suggests splitting the inputs in to’k’ distinct susbsets, 1<k <n, yielding ‘k’ subproblems.
The subproblems must be solved, and then a method must be found to combine
subsolutions into a solution of the whole. If the subproblems are still relatively large, then
the divide-and conquer strategy can possibly be reapplied.

40. Define control abstraction.


A control abstraction we mean a procedure whose flow of control is clear but
whose primary operations are by other procedures whose precise meanings are left
undefined.
Write the Control abstraction for Divide-and conquer.
Algorithm DAndC(P)
{
if small(p) then return S(P);
else
{
divide P into smaller instance p1,p2,…pk, k ≥1;
Apply DAndC to each of these subproblems
Return combine (DAnd C(p1) DAnd C(p2),----, DAnd (pk));
}
}

41.What is the substitution method?


One of the methods for solving any such recurrence relation is called the
substitution method.

42. What is the binary search?


If ‘q’ is always chosen such that ‘aq’ is the middle element(that is, q=[(n+1)/2),
then the resulting search algorithm is known as binary search.

43. Give computing time for Binary search?


In conclusion we are now able completely describe the computing time of binary
search by giving formulas that describe the best, average and worst cases.
Successful searches
Θ(1) Θ (logn) Θ (Logn)
best average worst
Unsuccessful searches
Θ (logn)
best, average, worst

45. Define external path length?


The external path length E, is defines analogously as sum of the distance of all
external nodes from the root.

46. Define internal path length.


The internal path length ‘I’ is the sum of the distances of all internal nodes from the root.

47. What is the maximum and minimum problem?


The problem is to find the maximum and minimum items in a set of ‘n’ elements. Though
this problem may look so simple as to be contrived, it allows us to demonstrate
divideand-conquer in simple setting.

48. What is the Quick sort?


Quicksort is divide and conquer strategy that works by partitioning it’s input
elements according to their value relative to some preselected element(pivot). it uses
recursion and the method is also called partition –exchange sort.

49. Write the Analysis for the Quick sort.


O(nlogn) in average and best cases
O(n2) in worst case

50. what is Merge sort? and Is insertion sort better than the merge sort?
Merge sort is divide and conquer strategy that works by dividing an input array in to
two halves,sorting them recursively and then merging the two sorted halves to get the
original array sorted
Insertion sort works exceedingly fast on arrays of less then 16 elements, though for large
‘n’ its computing time is O(n2).

51. Write a algorithm for straightforward maximum and minimum?


Algorithm straight MaxMin(a,n,max,min)
//set max to the maximum and min to the minimum of a[1:n]
{
max := min: = a[i];
for i = 2 to n do
{
if(a[i] >max) then max: = a[i];
if(a[i] >min) then min: = a[i];
}
}
52. what is general divide and conquer recurrence?
Time efficiency T(n)of many divide and conquer algorithms satisfies the equation
T(n)=a.T(n/b)+f(n).This is the general recurrence relation.
53. What is Master’s theorem?
54. Write the algorithm for Iterative binary search?
Algorithm BinSearch(a,n,x)
//Given an array a[1:n] of elements in nondecreasing
// order, n>0, determine whether x is present
{
low : = 1;
high : = n;
while (low < high) do
{
mid : = [(low+high)/2];
if(x < a[mid]) then high:= mid-1;
else if (x >a[mid]) then low:=mid + 1;
else return mid;
}
return 0;
}

55. Describe the recurrence relation for merge sort?


If the time for the merging operation is proportional to n, then the computing time of
merge sort is described by the recurrence relation
T(n) = a n = 1, a a constant
2T (n/2) + n n >1, c a constant

UNIT II GREEDY METHOD

The general method- Optimal storage on tapes- Knapsack problem-


Minimum spanning trees- Single source shortest path method

1. Explain the greedy method.


Greedy method is the most important design technique, which makes a choice that
looks best at that moment. A given ‘n’ inputs are required us to obtain a subset that
satisfies some constraints that is the feasible solution. A greedy method suggests that
one can device an algorithm that works in stages considering one input at a time.

2. Define feasible and optimal solution.


Given n inputs and we are required to form a subset such that it satisfies some
given constraints then such a subset is called feasible solution. A feasible solution
either maximizes or minimizes the given objective function is called as optimal
solution

3. Write the control abstraction for greedy method.


Algorithm Greedy (a, n)
{
solution=0;
for i=1 to n do
{
x= select(a);
if feasible(solution ,x) then
solution=Union(solution ,x);
}
return solution;
}

4. What are the constraints of knapsack problem?


To maximize Σpixi
1≤i≤n
The constraint is : Σwixi ≥ m and 0 ≤ xi ≤ 1 1≤ i ≤ n
1≤i≤n
where m is the bag capacity, n is the number of objects and for each object i wi
and pi are the weight and profit of object respectively.

5. What is a minimum cost spanning tree?


A spanning tree of a connected graph is its connected acyclic subgraph that
contains all vertices of a graph. A minimum spanning tree of a weighted
connected graph is its spanning tree of the smallest weight where bweight of the
tree is the sum of weights on all its edges.
A minimum spanning subtree of a weighted graph (G,w) is a spanning subtree of
G of minimum weight w(T )= Σ w(e )
e€ T
Minimum Spanning Subtree Problem: Given a weighted connected undirected graph
(G,w), find a minimum spanning subtree

6. Specify the algorithms used for constructing Minimum cost spanning tree.
a) Prim’s Algorithm
b) Kruskal’s Algorithm

7. State single source shortest path algorithm (Dijkstra’s algorithm).


For a given vertex called the source in a weigted connected
graph,find shotrtest paths to all its other vertices.Dijikstra’s algorithm applies to
graph with non-negative weights only.

8. What is Knapsack problem?


A bag or sack is given capacity and n objects are given. Each object has weight
wi and
profit pi .Fraction of object is considered as xi (i.e) 0<=xi<=1 .If fraction is 1 then
entire object is put into sack. When we place this fraction into the sack we get wixi
and pixi.
9. Write any two characteristics of Greedy Algorithm?
* To solve a problem in an optimal way construct the solution from given set of
candidates.
* As the algorithm proceeds, two other sets get accumulated among this one set
contains
the candidates that have been already considered and chosen while the other set
contains
the candidates that have been considered but rejected.

10. What is the Greedy approach?


The method suggests constructing solution through sequence of steps,each
expanding partially constructed solution obtained so far,until a complete solution is
reached. On each step,the choice must be
• Feasible(satisfy problem constraints)
• Locally optimal(best local choice among all feasible choices available on that
step)
• Irrevocable(once made,it cant be changed)

11. What are the steps required to develop a greedy algorithm?


* Determine the optimal substructure of the problem.
* Develop a recursive solution.
* Prove that at any stage of recursion one of the optimal choices is greedy choice.
Thus it is always safe to make greedy choice.
* Show that all but one of the sub problems induced by having made the greedy
choice are empty.
* Develop a recursive algorithm and convert into iterative algorithm.

12. Define forest.


Collection of sub trees that are obtained when root node is eliminated is known as
forest

13. Write the difference between the Greedy method and Dynamic programming.
Greedy method Dynamic programming
Only one sequence of decision is generated. Many number of decisions are generated.
It does not guarantee to give an It definitely gives an optimal solution always.
optimal solution always.

14.state the requirement in optimal storage problem in tapes.


Finding a permutation for the n programs so that when they are stored on the tape in
this order the MRT is minimized.This problem fits the ordering paradigm.

15. state efficiency of prim’s algorithm.


O(|v|2) (WEIGHT MATRIX AND PRIORITY QUEUE AS UNORDERED ARRAY)
O(|E| LOG|V|) (ADJACENCY LIST AND PRIORITY QUEUE AS MIN-HEAP)

16. State Kruskal Algorithm.


The algorithm looks at a MST for a weighted connected graph as an acyclic subgraph
with |v|-1 edges for which the sum of edge weights is the smallest.

17. state efficiency of Dijkstra’s algorithm.


O(|v|2) (WEIGHT MATRIX AND PRIORITY QUEUE AS UNORDERED ARRAY)
O(|E| LOG|V|) (ADJACENCY LIST AND PRIORITY QUEUE AS MIN-HEAP)

UNIT III
DYNAMIC PROGRAMMING

The General method- All pairs shortest path- Optimal binary tree-
Multistage graphs

1. Write the difference between the Greedy method and Dynamic programming.
Greedy method Dynamic programming
1.Only one sequence of decision is generated.
1.Many number of decisions are generated.
2.It does not guarantee to give an optimal
solution always.
2.It definitely gives an optimal solution
always.
2. Define dynamic programming.
Dynamic programming is an algorithm design method that can be used when a
solution to the problem is viewed as the result of sequence of decisions. It is technique
for solving problems with overlapping subproblems.
3. What are the features of dynamic programming?
• Optimal solutions to sub problems are retained so as to avoid recomputing of their
values.
• Decision sequences containing subsequences that are sub optimal are not
considered.
• It definitely gives the optimal solution always.
4. What are the drawbacks of dynamic programming?
• Time and space requirements are high, since storage is needed for all level.
• Optimality should be checked at all levels.
5. Write the general procedure of dynamic programming.
The development of dynamic programming algorithm can be broken into a
sequence of 4 steps.
1. Characterize the structure of an optimal solution.
2. Recursively define the value of the optimal solution.
3. Compute the value of an optimal solution in the bottom-up fashion.
4. Construct an optimal solution from the computed information.
6. Define principle of optimality.
It states that an optimal solution to any of its instances must be made up of
optimal solutions to its subinstances.
7. Define multistage graph
A multistage graph G =(V,E) is a directed graph in which the vertices are partitioned
in to K>=2 disjoint sets Vi,1<=i<=k.The multi stage graph problem is to find a minimum
cost paths from s(source ) to t(sink)
Two approach(forward and backward)
8. Define All pair shortest path problem
Given a weighted connected graph, all pair shortest path problem asks to find the
lengths of shortest paths from each vertex to all other vertices.
9.Define Distance matrix
Recording the lengths of shortest path in n x n matrix is called Distance matrix(D)
10.Define floyd’s algorithm
􀃖 To find all pair shortest path
􀃖 The algorithm computes the distance matrix of a weighted graph with n vertices
through series of n by n matrices :D(0)…D(k-1),D(k)…..D(n)
11. State the time efficiency of floyd’s algorithm
O(n3)
It is cubic
12. Define OBST
􀃖 Dynammic pgmg. Used
􀃖 If probabilities of searching for elements of a set are known then finding optimal
BST for which the average number of comparisons in a search is smallest
possible.
13. Define catalan number
The total number of binary search trees with n keys is equal to nth catalan
number
C(n)=(2n to n) 1/(n+1) for n>0,c(0)=1
14. State time and space efficiency of OBST
SPACE EFFICIENCY :QUADRATIC
TIME EFFICIENCY : CUBIC.

UNIT 4 AND 5
UNIT IV BACKTRACKING

The General method- Solution space and tree organization- The Eight Queens problem-
Sum of subset problem- Graph coloring- Knapsack problem

UNIT V BRANCH AND BOUND

The General method- O/I Knapsack problem- Traveling sales person problem-Efficiency
consideration . NP Hard and NP Complete problems - Basic concepts

1. 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.

2.Define explicit constraint.


They are rules that restrict each x to take on values only from a give set. They i
depend on the particular instance I of the problem being solved. All tuples that satisfy the
explicit constraints define a possible solution space.

3. Define implicit constraint.


They are rules that determine which of the tuples in the solution space of I satisfy the
criteria function. It describes the way in which the x must relate to each other. i
4.Define state space tree.
The tree organization of the solution space is referred to as state space tree.
5.Define state space of the problem.
All the paths from the root of the organization tree to all the nodes is called as state
space of the problem
6.Define answer states.
Answer states are those solution states s for which the path from the root to s
defines a tuple that is a member of the set of solutions of the problem.
7.What are static trees?
The tree organizations that are independent of the problem instance being solved
are called as static tree.

8.What are dynamic trees?


The tree organizations those are independent of the problem instance being solved
are called as static tree.

9.Define a live node.


A node which has been generated and all of whose children have not yet been
generated is called as a live node.

10. Define a E – node.


E – node (or) node being expanded. Any live node whose children are currently
being generated is called as a E – node.

11.Define a dead node.


Dead node is defined as a generated node, which is to be expanded further all of
whose children have been generated.

12.,What are the factors that influence the efficiency of the backtracking algorithm?
The efficiency of the backtracking algorithm depends on the following four
factors. They are:
The ti i. me needed to generate the next x k
ii. The number of x satisfying the explicit constraints. k
iii. The time for the bounding functions B
iv. The number of x satisfying the B
k
k k.

13.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.

14.What are the searching techniques that are commonly used in Branch-and-Bound
method.
The searching techniques that are commonly used in Branch-and-Bound method are:
i. FIFO
ii. LIFO
iii. LC
iv. Heuristic search

15.State 8 – Queens problem.


The problem is to place eight queens on a 8 x 8 chessboard so that no two queen
“attack” that is, so that no two of them are on the same row, column or on the diagonal.

16.State Sum of Subsets problem.


Given n distinct positive numbers usually called as weights , the problem calls for finding
all the combinations of these numbers whose sums are m.

17. State m – colorability decision problem.


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.

18.Define chromatic number of the graph.


The m – colorability optimization problem asks for the smallest integer m for which the
graph G can be colored. This integer is referred to as the chromatic number of the graph.

19. Define a planar graph.


A graph is said to be planar iff it can be drawn in such a way that no two edges cross each
other.

20. What are NP- hard and Np-complete problems?


The problems whose solutions have computing times are bounded by polynomials of small
degree.
21. What is a decision problem?
Any problem for which the answer is either zero or one is called decision problem.

22. what is approximate solution?


A feasible solution with value close to the value of an optimal solution is called
approximate solution.

23. what is promising and non-promising nodes?


a node in a state space tree is said to be promising if it corresponds to a partially constructed
solution from which a complete solution can be obtained. The nodes which are not promising for
solution in a state space tree are called non-promising nodes.

24.Write formula for bounding function in Knapsack problem


In knapsack problem upper bound value is computed by the formula
UB = v + (W-w) * (vi+1/wi+1)

25. Write about traveling salesperson problem.


Let g = (V, E) be a directed. The tour of G is a directed simple cycle that includes every vertex in
V. The cost of a tour is the sum of the cost of the edges on the tour. The traveling salesperson
problem is to find a tour of minimum cost. In branch and bound technique of TSP problem Lower
bound lb= s/2

26. Write some applications of traveling salesperson problem.


-> 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.

27. Give the time complexity and space complexity of traveling salesperson problem.
Time complexity is O (n2 2n). Space complexity is O (n 2n).

28. Differentiate decision problem and optimization problem


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

29. what is class P and NP?


P is set of all decision problems solvable by deterministic algorithms in polynomial time.
NP is set of all decision problems solvable by non deterministic algorithms in polynomial time.

30. Define NP-Hard and NP-Complete problems


Problem L is NP-Hard if and only if satisfiability reduces to L.
A Problem L is NP-Complete if and only if L is NP-Hard and L belongs to NP.
n))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy