DAA Solved Notes
DAA Solved Notes
Ans: Data Structure is about organising and managing data effectively such that we can
perform specific operation efficiently, while Algorithm is a step-by-step procedure to be
followed to reach the desired output.
Ans:
Ans: A recursive algorithm is an algorithm which calls itself with "smaller (or
simpler)" input values, and which obtains the result for the current input by applying
simple operations to the returned value for the smaller (or simpler) input.
Q4: Inplace sorting ?
Ans: A sort algorithm in which the sorted items occupy the same storage as the
original ones. These algorithms may use o(n) additional memory for bookkeeping, but
at most a constant number of items are kept in auxiliary memory at any time. Also
known as sort in place.
Ans: A sorting algorithm is said to be stable if two objects with equal keys appear in
the same order in sorted output as they appear in the input array to be sorted.
Ans:
Ans:
Kruskal's algorithm's time complexity is O(E log V), V being the number of vertices.
Q10: Greedy Method vs Dynamic Programming ?
Ans:
Ans: The Minimum Spanning Tree is the one whose cumulative edge weights have the
smallest value, however. Think of it as the least cost path that goes through the entire
graph and touches every vertex.
Q12: Divide And Conquer ?
Ans: Divide and conquer is a recursive problem. Solving approach which break a
problem into smaller sub problems, and finally combines the solution to the subproblem
to solve the original problem.
This method usually allows us to reduce the time complexity to a large extent.
Ans:
Ans:
Breadth first search is a graph traversal The Depth First Search (DFS) is an
algorithm that starts traversing the algorithm for traversing or searching tree
graph from root node and explores all or graph data structures which uses the
the neighbouring nodes. Then, it selects idea of backtracking. It explores all the
the nearest node and explore all the nodes by going forward if possible or uses
unexplored nodes. The algorithm follows backtracking.
the same process for each of the nearest
node until it finds the goal. Note: It can be implemented using
a stack.
Q15: Digraph ?
Ans: A directed graph (or digraph) is a set of vertices and a collection of directed edges
that each connects an ordered pair of vertices. We say that a directed edge points from
the first vertex in the pair and points to the second vertex in the pair
Q16: Prims vs Kruskal ?
Ans:
Prims: Kruskal:
• Prim’s Algorithm is faster for dense • Kruskal’s Algorithm is faster for sparse
graphs. graphs.
• Prim’s algorithm works by choosing • Kruskal’s algorithm selects least weight
the adjacent vertices from the selected edges instead of using adjacency list, it
set of vertices organizes the edges by their weights.
• The time complexity of Prim’s algorithm • Kruskal’s algorithm runs in O(log V)
is O(V2 ) time.
Ans:
Dijkstra: Bellman:
• Dijkstra’s Algorithm doesn’t work • Bellman Ford’s Algorithm works
when there is negative weight edge. when there is negative weight edge,
• It is less time consuming. it also detects the negative weight
• Greedy approach is taken to cycle.
implement the algorithm. • It is more time consuming than
Dijkstra’s algorithm.
• Dynamic Programming approach is
taken to implement the algorithm.
Ans: The string-matching automaton is a very useful tool which is used in string
matching algorithm. It examines every character in the text exactly once and reports
all the valid shifts in O (n) time.
Ans: The KMP algorithm is an efficient string matching algorithm. It is a linear time
algorithm for the string matching problem. It is used to find a pattern in the text.
For example: A naïve algo. For sorting numbers scan all numbers to find the smallest
one, puts it aside, and so on. But it is not an efficient algorithm.
Q21: What is Divide and Conquer approach ?
Ans: In divide and conquer algorithm recursively breaks down a problem into two or
more sub-problems of the same or related type, then each problem is solved
independently.
Ans: The Floyd-warshall algorithm is an algorithm for finding shortest paths in a directed
weighted graph with positive or negative edge weights.
Q24: What are different types of notation used for representation of complexities?
Ans: The process in which a function calls itself directly or indirectly is called recursion
and the corresponding function is called as recursive function. Using recursive
algorithm, certain problems can be solved quite easily.
Q28: What are the different complexities according to which we analyze any
algorithm?
Ans: The complexity of an algorithm computes the amount of time and spaces required
by an algorithm for an input of size (n).
The complexity of an algorithm can be divided into two types. The time complexity and
the space complexity.
Q29: What is Merge Sort ? and is insertion sort better than the merge sort ?
Ans: Merge Sort is a Divide and Conquer algorithm. It divides the input array into two
halves, calls itself for the two halves, and then merges the two sorted halves.
Insertion Sort is preferred for fewer elements. It becomes fast when data is already
sorted or nearly sorted because it skips the sorted values. Efficiency: Considering
average time complexity of both algorithm we can say that Merge Sort is efficient in
terms of time and Insertion Sort is efficient in terms of space.
Ans: A feasible solution is a set of values for the decision variables that satisfies all of
the constraints in an optimization problem.
An optimal solution is a feasible solution where the objective function reaches its
maximum (or minimum) value – for example, the most profit or the least cost.
Q32: Write down three cases of Master Method for solving recurrences. ?
Ans:
Q33: Which sorting algorithm will perform better if array is already sorted?
Ans: Insertion sort runs much more efficiently if the array is already sorted or "close to
sorted." Insertion sort performs O(n2 ) swaps in the average and worst case.
Ans: The all pair shortest path algorithm is also known as Floyd-Warshall algorithm.
Q37: Write an algorithm using Recursive function to find the sum of n numbers ?
Ans:
int recurSum(int n)
{
if (n <= 1)
return n;
return n + recurSum(n - 1);
}
Ans:
P PROBLEMS NP PROBLEMS
P problems are set of problems which can be NP problems are the problems which
solved in polynomial time by deterministic can be solved in non-deterministic
algorithms. polynomial time.
The problem belongs to NP, if it’s easy
The problem belongs to class P if it’s easy to
to check a solution that may have been
find a solution for the problem.
very tedious to find.
Solution to NP problems cannot be
P Problems can be solved and verified in obtained in polynomial time, but if the
polynomial time. solution is given, it can be verified in
polynomial time.
NP problems are superset of P
P problems are subset of NP problems.
problems.
It is not known whether P=NP. However, If P≠NP, there are problems in NP that
many problems are known in NP with the are neither in P nor in NP-Complete.
property that if they belong to P, then it can
be proved that P=NP.
All the NP problems are non-
All P problems are deterministic in nature.
deterministic in nature.
Selection sort, linear search TSP, Knapsack problem.