Professional Documents
Culture Documents
Mini
Mini
Terminates when a base case is reached. Terminates when a condition is proven to be false.
Each recursive call requires extra space on the Each iteration does not require extra space
stack frame (memory).
If we get infinite recursion, the program may run An infinite loop could loop forever since there is
out of memory and result in stack overflow. no extra memory being created.
Solutions to some problems are easier to Iterative solutions to a problem may not always
formulate recursively. be as obvious as a recursive solution.
Q.2 What is recursion? And explain different application of recursion with following programmers (1)
Fibonacci Series (2) factorial of number
1) Any function which calls itself is called recursive.
2) A recursive method solves a problem by calling a copy of itself to work on a smaller problem. This is called
the recursion step.
3) The recursion step con result in many more such recursive calls.
4) It is important to ensure that the recursion terminates.
5) Each time the function calls itself with a slightly simpler version of the original problem. The sequence of
smaller problems must eventually converge on the base case.
Q. 10 compare and contrast linear and binary search with respect to time and space complexity
Q.3 Explain the concept of divide and concure? State its advantages and disadvantages?
1) Divide and Conquer is an important algorithm design technique based on recursion.
2) The D & C algorithm works by recursively breaking down a problem into two or more sub problems of the
same type, until they become simple enough to be solved directly
3) The solutions to the sub problems are then combined to give a solution to the original problem.
4) The D & C strategy solves a problem by:
a) Divide: Breaking the problem into sub problems that are themselves smaller instances of the same type
of problem.
b) Recursion: Recursively solving these sub problems.
c) Conquer: Appropriately combining their answers.
5) It’s not possible to solve all the problems with the Divide & Conquer technique.
6) As per the definition of D & C, the recursion solves the subproblems which are of the same type.
7) For all problems it is not possible to find the subproblems which are the same size and D & C is not a
choice for all problems.
8) 18.6 Advantages of Divide and Conquer
a) D & C is a powerful method for solving difficult problems.
b) Dividing the problem into subproblems so that subproblems can be combined again is a major
difficulty in designing a new algorithm. For many such problems D & C provides a simple solution.
9) Disadvantages of Divide and Conquer
a) D & C approach is that recursion is slow.
b) the D & C approach needs a stack for storing the calls
c) Another problem with D & C is that, for some problems, it may be more complicated than an iterative
approach.
10) Application : 1) Merge sort 2) Strassen matrix
Q.4 4 Explain the concept of dynamic programming state advantages and Disadvantages
1) (DP) is a simple technique but it can be difficult to master.
2) One easy way to identify and solve DP problems is by solving as many problems as possible.
3) The term Programming is not related to coding but it is from literature, and means filling tables (similar to
Linear Programming).
4) Dynamic programming and memoization work together.
5) The main difference between dynamic programming and divide and conquer is that in the case of the
latter, sub problems are independent, whereas in DP there can be an overlap of sub problems.
6) DP reduces the exponential complexity to polynomial complexity (O(n2), O(n3), etc.)
7) major components of DP are: 1) Recursion: Solves sub problems recursively. 2) Memoization: Stores
already computed values in table
8) Dynamic Programming = Recursion + Memoization
9) Basically there are two approaches for solving DP problems (1) Bottom-up dynamic programming (2) Top-
down dynamic programming
10) Examples of Dynamic Programming Algorithms
a) Chain matrix multiplication
b) Subset Sum
c) Travelling salesman problem
d) Algorithms on graphs can be solved efficiently: Bellman-Ford algorithm for finding the shortest
distance in a graph
Q.5 Explain the concept of Backtracking programming state advantages and Disadvantages
1) Backtracking is an improvement of the brute force approach.
2) It systematically searches for a solution to a problem among all available options.
3) In backtracking, we start with one possible option out of many available options and try to solve the
problem if we are able to solve the problem with the selected move then we will print the solution else we
will backtrack and select some other option and try to solve it. If none if the options work out we will claim
that there is no solution for the problem.
4) Backtracking allows us to deal with situations in which a raw brute-force approach would explode into an
impossible number of options to consider.
5) Backtracking can be thought of as a selective tree/graph traversal method.
6) What’s interesting about backtracking is that we back up only as far as needed to reach a previous decision
point with an as-yet-unexplored alternative.
7) In general, that will be at the most recent decision point.
8) Sometimes the best algorithm for a problem is to try all possibilities. This is always slow, but there are
standard tools that can be used to help.
9) Application of BP : 1
a) Binary Strings: generating all binary strings
b) Generating k – ary Strings
c) N-Queens Problem
d) The Knapsack Problem
e) Generalized Strings
f) Graph Coloring Problem