DAA Module1
DAA Module1
Module-1
What is an Algorithm?
An algorithm is a finite sequence of well-defined set of instructions to solve a
problem.
What are the characteristics of an algorithm?
1. Unambiguous− Algorithm should be clear and unambiguous. Each of its steps
and their inputs/outputs should be clear and must lead to only one meaning.
2. Input− An algorithm should have 0 or more well-defined inputs.
3. Output− An algorithm should have one or more well-defined outputs, and
should match the desired output.
4. Finiteness− Algorithms must terminate after a finite number of steps. The
algorithm should not run for infinity.
5. Representation – The same algorithm can be represented in several different
ways. Pseudo code is the most common way of representing an algorithm.
Pseudo code is a mixture of natural language and programming language
constructs. Flowchart is another way of representing an algorithm.
6. Feasibility− Should be feasible with the available resources.
7. Definiteness -Algorithms must specify every step and the order of the steps.
8. There may exist several algorithms for solving the same problem
Greedy Algorithms
Greedy Algorithms works step-by-step, and always chooses the steps which
provide immediate profit/benefit. It chooses the locally optimal solution, without
thinking about future consequences. Greedy algorithms may not always lead to the
optimal global solution, because it does not consider the entire data.
Dynamic Programming
Dynamic Programming is a technique in computer programming that helps to
efficiently solve a class of problems that have overlapping sub problems and optimal
substructure property. A problem is said to have optimal substructure if an optimal
solution can be constructed from optimal solutions of its sub problems. The solutions
to these sub problems can be saved for future reference (memorization) so that when
their solutions are required, they are at hand and we do not need to recalculate them.
This method of solving a solution is referred to as dynamic programming.
Back tracking
Backtracking is an algorithm design technique which is used to solve problems
in which a sequence of objects are chosen from a finite set so that the sequence
satisfies some criteria. This approach is used to solve problems that have multiple
solutions. Backtracking can be used to solve well-known problems such as n-queens
problem, sum of subsets, graph colouring problem, Hamiltonian cycle problem etc.
Randomized algorithm
Randomized algorithm is one whose behaviour depends on the inputs and the
random choices are made as part of its logic. As a result, the algorithm gives different
outputs even for the same input.
An algorithm may not have the same performance for different types of inputs. With
the increase in the input size, the performance will change. The study of change in
performance of the algorithm with the change in the order of the input size is defined
as asymptotic analysis.
The average case analysis is not easy to do in most practical cases and is rarely done.
In the average case analysis, we need to predict the mathematical distribution of all
possible inputs.
return -1;
}
****