0% found this document useful (0 votes)
3K views9 pages

Daa Two Mark Questions

This document provides an overview of algorithms and data structures. It defines key concepts like algorithm properties, complexity analysis, and common algorithmic strategies like divide-and-conquer, greedy methods, and dynamic programming. Examples are given for specific algorithms like binary search, quicksort, Dijkstra's algorithm, and knapsack problem solutions. Asymptotic notations and their definitions are also outlined.

Uploaded by

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

Daa Two Mark Questions

This document provides an overview of algorithms and data structures. It defines key concepts like algorithm properties, complexity analysis, and common algorithmic strategies like divide-and-conquer, greedy methods, and dynamic programming. Examples are given for specific algorithms like binary search, quicksort, Dijkstra's algorithm, and knapsack problem solutions. Asymptotic notations and their definitions are also outlined.

Uploaded by

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

DESIGN AND ANALYSIS OF ALGORITHMS

TWO MARK QUESTIONS


UNIT I INTRODUCTION

1. Define 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 are the properties of an algorithm?


 Finiteness
 Input
 Output
 Effectiveness
 Non Ambiguity
 Range of Input
 Multiplicity
 Speed

3. What are the methods are used for specifying an algorithm?


 Natural languages
 Flow chart
 Pseudo code

4. What are the two factors to measure the efficiency of an algorithm?


Time efficiency-> The amount of time to take execute the algorithm’s basic operation
Space efficiency-> The amount of memory required to store the algorithm and the amount of memory
required to store the input for that algorithm.

5. How to measure the running time?


• Find the basic operation of the algorithm.
• Compute the time required to execute the basic operation.
• Compute how many times the basic operation is executed. The Time complexity calculated by the
following formula
T(n) = Cop * C(n) Where,
Cop – Constant (The amount of time required to execute the basic operation)
C(n) – How many times the basic operation is executed.
6. Name the three asymptotic Notations.
(i) Big Oh
(ii) Big Omega
(iii) Big Theta

7. Define Big-Oh Notation.


A function t(n) is said to be in O(g(n)), denote 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) ≤ c.g(n) for all n ≥ n0.

8. Define Big-Omega Notation.


A function t(n) is said to be in Ω (g(n)), denote 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) ≥ c.g(n) for all n ≥ n0.

9. Define Big Theta (or) θNotation


A function t(n) is said to be in O(g(n)), denote t(n) € θ(g(n)), if t(n) is bounded both above and below by
some constant multiple of g(n) for all large n, i.e., if there exists some positive constant c1 and c2 and
some nonnegative integer n0 such that
C2.g(n) ≤ t(n) ≥ c1.g(n) for all n ≥ n0.

10. What do you mean by divide and conquer?


The divide-and-conquer strategy solves a problem by:
1. Breaking it into sub problems that are themselves smaller instances of the same type of
problem
2. Recursively solving these sub problems
3. Appropriately combining their answers

11. Write the examples of Divide and Conquer.


 Binary Search
 Quick Sort
 Merge Sort
12. Write down the formula for finding the time complexity using divide and conquer.
T(n)=a(n/b)+f(n)
Where, T(n) is the running time of the given algorithm
A is the number of sub problems
B is the sub problem size
F(n) is the time to combine the solutions of sub problems

13. Write an algorithm for binary search.


procedure: int binarySearch(int arr[], int value, int left, int right)
while (left <= right) do
middle = (left + right) / 2;
if (arr[middle] == value)
return middle;
else if (arr[middle] > value)
right = middle - 1;
else
left = middle + 1;
return -1;

14. Compare the time complexity of quick sort, heap sort, Merge sort and Selection sort.
Analysis Quick Merge Heap Selection
Best O(nlogn) O(nlogn) O(nlogn) O(n2)
Average O(nlogn) O(nlogn) O(nlogn) O(n2)
Worst O(n2) O(nlogn) O(nlogn) O(n2)

15. Write an algorithm for selection sort.


procedure SELECTION_SORT (A)
for i ← 1 to n-1 do
min j ← i;
min x ← A[i]
for j ← i + 1 to n do
If A[j] < min x then
min j ← j
min x ← A[j]
A[min j] ← A [i]
A[i] ← min x

16. Define Heap.


Heap is a binary tree that is completely filled on all levels except possibly the lowest, which is filled
from the left to right. It is also called as a complete binary tree.

17. List out the drawbacks of binary search algorithm.


The disadvantage of binary search in any language is that you must provide an ordered list, usually an
array, for the algorithm to search. Arrays are either static in size or, if dynamic, require special
processing when they grow in size. A linked list does not work, because you need to make random
access to the elements.

18. Name any four algorithm’s basic efficiency classes.


 O(1)
 O(n)
 O(logn)
 O(n logn)
 O(n2)
 O(n3)
 O(n!)
n
 O(2 )

UNIT II GREEDY METHOD

1. Define greedy algorithm.


Greedy method is an approach to find the optimized solutions for the given problem. The solutions are
constructed through a sequence of steps, each step expanding a partially constructed solution so far, until
a complete solution to the problem is reached.

2. What are the applications of greedy method?


 Optimal Storage on tapes
 Knapsack problem
 Minimum Spanning Tree
 Finding Shortest Path

3. Write an algorithm for optimanl storage on tapes.


Pseudo tapes(n,m) // n is the no. of programs and m is the no. of tapes
{
j=0;
for(i=1 to n do)
{
write ("append", i "to the permutation of the tape", j)
j=(j+1) mod m
}
}

4. Define Knapsack problem


Knapsack problem is one of the optimization problems in which given a set of items each with a cost
and a value, determine the number of each item to include in a collection so that the total cost does not
exceed some given cost and the total value is as large as possible.

5. Write an algorithm fro Knapsack problem.


Algorithm Knapsack_greedy(W,n)
For i:=1 to n do
If(w[i]<W) then
X[i]=1.0
W=W-w[i]
If(i<=n) then
X[i]=W/w[i];

6. What are the three different ways to implement knapsack problem?


 Greedy method
 Backtracking
 Branch and Bound

7. Define Minimum Spanning Tree.


A minimum spanning tree is a spanning tree, but has weights or lengths associated with the edges, and
the total weight of the tree (the sum of the weights of its edges) is at a minimum.

Graph Minimum Spanning Tree

8. What are the methods are used to implement the Minimum Spanning Tree?
 Prim’s algorithm
 Kruskal’s algorithm

9. Define Prim’s algorithm( For Kruskal’s algorithm also).


Prim's algorithm is an algorithm that finds a minimum spanning tree for a connected weighted
undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex,
where the total weight of all the edges in the tree is minimized. Prim's algorithm is an example of a
greedy algorithm.

10. Compare Prims and Kruskal’s algorithm


Prim Kruskal
Looks Harder Looks easier
Easy to implement Hard to implement
More efficient Less efficient

11. Define Dijkstras algorithm.


Dijkstra’s algorithm is used to find the shortest paths from a single source vertex to all other vertices in
a weighted, directed graph. All weights must be nonnegative. Dijkstra's algorithm keeps two sets of
vertices:
S the set of vertices whose shortest paths from the source have already been determined
V-S the remaining vertices.

UNIT III
1. Define dynamic programming.
Dynamic programming is a method for efficiently solving a broad range of search and optimization
problems which exhibit the characteristics of overlapping sub problems and optimal substructure.
2. What are the applications of dynamic programming?
 All pairs shortest path problem (Floyd’s algorithm)
 Optimal Binary Search Tree
 Multistage Graph

3. Differentiate divide and conquer & dynamic programming.

Divide and Conquer Dynamic programming


The divide-and-conquer strategy solves a Dynamic programming is a method for
problem by breaking it into sub problems that efficiently solving a broad range of search and
are themselves smaller instances of the same optimization problems which exhibit the
type of problem, recursively solving these sub characteristics of overlapping sub problems
problems and appropriately combining their and optimal substructure
answers.
Top down approach Bottom up approach
Less efficient More efficient
Duplicate solutions may be obtained Duplicate solutions is avoided totally
It splits input at specific deterministic points It splits input at every possible split points
usually in the middle rather than at a particular point

4. Differentiate Greedy and dynamic programming

Greedy method Dynamic programming


A set of feasible solutions and picks up the No special set of feasible solutions
optimal solution
Solution is generated without revising It considers all possible sequences in order to
previously generated solutions obtain the optimal solution
No guarantee of getting optimal solution Guaranteed to generate optimal solution

5. What do you mean by principles of optimality?


It states that “ In an optimal sequence of decisions or choices, each subsequences must also be optimal”
6. Define Floyd’s algorithm.
Floyd’s Algorithm is applied to solving the all–pairs shortest paths problem in graphs,
both directed and undirected.

It can be used to determine the shortest path from one specific vertex to all other vertices. Floyd’s
Algorithm is normally applied to weighted graphs, but can be extended to
unweighted graphs simply by assigning a weight of 1 to each edge. Floyd's algorithm runs with a time
complexity of O(n3).

7. Define weight matrix(adjacency matrix).

Floyd’s algorithm starts with the weight matrix for the weighted graph.
In the weighted graph, a weight matrix is defined as n×n matrix where
W(i,j)=0 if i=j.
W(i,j)=∞ if there is no edge between i and j.
W(i,j)=“weight of edge”
8. Define distance matrix.
The distance matrix is the square matrix consisting of shortest distances from every vertex Vi to vertex
Vj in floyd’ algorithm.
9. Write an algorithm for all pairs shortest path problem (floyd’s algorithm).

10. Define binary search tree.


A binary search tree is a tree where the key values are stored in the internal nodes, the external nodes
(leaves) are null nodes, and the keys are ordered lexicographically. I.e. for each internal node all the
keys in the left sub tree are less than the keys in the node, and all the keys in the right sub tree are
greater.

11. Define optimal binary search tree.


An OBST(optimal binary search tree) is a BST ( binary search tree) which has minimal expected cost.
Each optimal binary search tree is composed of a root and (at most) two optimal subtrees, the left and
the right. Time Complexity for finding Optimal BST is O(n3).

12. Define multistage graph.


A multistage graph G(V,E) is a directed graph in which the vertices are partitioned into k≥2 disjoint sets
and it find a minimum-cost path from s to t through the vertices in multistage. Many problems can be
formulated as multistage graph problem. An example: resource allocation problem. The time complexity
is O(|V|+|E|).

UNIT IV

1. Define backtracking.
Backtracking is a process of searching the solution to the problem by searching the solution space(the
set of all feasible solutions) for the given problem.

2. Name any 4 methods that are using backtracking.


 N-queen problem
 Subset sum problem
 Graph coloring
 Knapsack problem

3. What is state space tree?


The tree organization of the solution space by implementing the backtracking method for any problem is
defined as state space tree. Each node in the state space tree defines problem state. Solution states are
that problem states for which the path from the root to s defines a tuple in the solution space. Solution
space is partitioned into disjoint sub-solution space at each internal node

4. Define promising and non-promising node.


A node in the state space tree is promising if it corresponds to the partials constructed solution that may
lead to the complete solution otherwise the nodes are called non-promising. Leaves of the tree represent
either the non- promising dead end or complete solution found by the algorithm.

5. Define n-queen problem.


It is a method to place n queens in a n × n matrix by using backtracking so that no two queens are in the
same row, column, or diagonal.

6. What is subset sum problem?


An instance of the Subset Sum problem is a pair (S, t), where S = {x1, x2, ..., xn} is a set of positive
integers and t (the target) is a positive integer. The decision problem asks for a subset of S whose sum is
as large as possible, but not larger than t. This problem is NP-complete.

7. Define graph coloring.


The graph (or vertex) coloring problem, which involves assigning colors to vertices in a graph such that
adjacent vertices have distinct colors, arises in a number of scientific and engineering applications such
as scheduling, register allocation, optimization and parallel numerical computation.

8. Define knapsack problem.


The knapsack is nothing but a sack where in which we need to place the given items according to the
capacity of the knapsack. In case of backtracking we consider the profits but in dynamic programming
we consider weights.
UNIT V

1. Define branch and bound algorithm.


Branch and bound is a systematic method for solving optimization problems that applies where the
greedy method and dynamic programming fail. However, it is much slower. The general idea of B&B is
a BFS-like search for the optimal solution, but not all nodes get expanded.

2. Give the examples for branch and bound algorithm.


 0/1 Knapsack problem
 Traveling Salesman problem

3. Define traveling salesman problem.


Traveling salesperson problem is to find the shortest path in directed graph that starts at a given vertex,
visits each vertex in the graph exactly once. Such a path is called an optimal tour.

4. What is NP?
NP is the set of all decision problems (question with yes-or-no answer) for which the 'yes'-answers can
be verified in polynomial time where n is the problem size, and k is a constant) by a deterministic
Turing machine. Polynomial time is sometimes used as the definition of fast or quickly.

5. What is P?
P is the set of all decision problems which can be solved in polynomial time by a deterministic Turing
machine. Since it can solve in polynomial time, it can also be verified in polynomial time.
Therefore P is a subset of NP.

6. What is NP-Complete?
A problem x that is in NP is also in NP-Complete if and only if every other problem in NP can be
quickly (ie. in polynomial time) transformed into x. In other words:
 x is in NP, and
 Every problem in NP is reducible to x

7. What is NP-Hard?
NP-Hard are problems that are at least as hard as the hardest problems in NP. Note that NP Complete
problems are also NP-hard. However not all NP-hard problems are NP (or even a
Decision problem), despite having 'NP' as a prefix. That is the NP in NP-hard does not mean 'non
deterministic polynomial time'.

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