Professional Documents
Culture Documents
Algorithm Introduction Part-1
Algorithm Introduction Part-1
1. Algorithms
2. Order
3. Analysis of Algorithm
Topic: Introduction 1
What is an algorithm?
Topic: Introduction 2
Thus an algorithm is
A simple, unambiguous, mechanical
procedure to carry out some task.
Topic: Introduction
4
Need of Algorithm:
• 7. The Algorithm gives a clear description of requirements and goal of
the problem to the designer.
• 8. A good design can produce a good solution.
• 9. To understand the flow of the problem.
• 10. To measure the behavior (or performance) of the methods in all
cases (best cases, worst cases, average cases)
• 11. With the help of an algorithm, we can also identify the resources
(memory, input-output) cycles required by the algorithm.
• 12. With the help of algorithm, we convert art into a science.
• 13. To understand the principle of designing.
• 14. We can measure and analyze the complexity (time and space) of
the problems concerning input size without implementing and running
it; it will reduce the cost of design.
Topic: Introduction 5
Algorithm vs Program:
A finite set of instructions that specifies a sequence of operations to be
carried out to solve a specific problem of a class of problem is called
an algorithm.
On the other hand, the Program doesn't have to satisfy the finiteness
condition.
For example, we can think of an operating system that continues in a
"wait" loop until more jobs are entered. Such a program doesn't
terminate unless the system crashes.
2. Pseudo code
3. Graphical
Topic: Introduction 7
Example – multiplying two positive integers A and
B
Usually:
45
19 (x
405
45 (+
855
Topic: Introduction 8
A different algorithm:
45 19 19
22 38
11 76 76
5 152 152
2 304
1 608 608 (+
855
Topic: Introduction 9
An instance of a problem is a specific assignment of values to
the parameters.
Although the above algorithm will not work if the first operand
is negative, this does not invalidate the algorithm since (-45, 19)
is not an instance of the problem being considered.
Topic: Introduction 10
Order:
Usually we use the frequency count to compare algorithms.
Consider the following 3 programs:
Topic: Introduction 12
Examples
Topic: Introduction 13
Algorithm Design Techniques:
Topic: Introduction 14
1. Divide and Conquer Approach:
It is a top-down approach. The algorithms which follow the divide
& conquer techniques involve three steps: Divide the original
problem into a set of sub problems. Solve every sub problem
individually, recursively. Combine the solution of the sub problems
(top level) into a solution of the whole original problem.
Topic: Introduction 15
2. Greedy Technique:
Greedy method is used to solve the optimization problem. An optimization
problem is one in which we are given a set of input values, which are required
either to be maximized or minimized (known as objective), i.e. some constraints
or conditions. Greedy Algorithm always makes the choice (greedy criteria)
looks best at the moment, to optimize a given objective.
The greedy algorithm doesn't always guarantee the optimal solution however it
generally produces a solution that is very close in value to the optimal.
Topic: Introduction 16
3. Dynamic Programming:
Dynamic Programming is a bottom-up approach we solve all
possible small problems and then combine them to obtain solutions
for bigger problems. This is particularly helpful when the number
of copying sub problems is exponentially large. Dynamic
Programming is frequently related to Optimization Problems.
Topic: Introduction 17
4. Branch and Bound:
Branch and bound is an algorithm design paradigm which is
generally used for solving combinatorial optimization problems.
These problems are typically exponential in terms of time
complexity and may require exploring all possible permutations in
worst case. The Branch and Bound Algorithm technique solves
these problems relatively quickly.
In Branch & Bound algorithm a given sub problem, which cannot
be bounded, has to be divided into at least two new restricted sub
problems. Branch and Bound algorithm are methods for global
optimization in non-convex problems. Branch and Bound
algorithms can be slow, however in the worst case they require
effort that grows exponentially with problem size, but in some
cases we are lucky, and the method coverage with much less effort.
Topic: Introduction 18
Topic: Introduction 19
5. Randomized Algorithms:
A randomized algorithm is defined as an algorithm that is allowed
to access a source of independent, unbiased random bits, and it is
then allowed to use these random bits to influence its computation.
Topic: Introduction 20
6. Backtracking Algorithm:
Backtracking Algorithm tries each possibility until they find the
right one. It is a depth-first search of the set of possible solution.
During the search, if an alternative doesn't work, then backtrack to
the choice point, the place which presented different alternatives,
and tries the next alternative.
Topic: Introduction 21
Loop Invariants
Topic: Introduction 22
Loop invariants
Loop Invariants has three properties:-
Topic: Introduction 23
To prove statement S about a loop is correct, define S concerning
series of smaller statement S0 ,S1... .Sk where,
Topic: Introduction 25
Maintainence:
( Mathematical Induction)
Insetion Sort
*Assume that it is true before a particular iteration. 1.For j=2 to A Length
2. key=A[j]
:Then prove that it remains true before beginning of
3. // Insert A[j] in the sorted
Next iteration.
sequence A[1…,j-1]
2. i=j-1
If suppose at jth iteration
Topic: Introduction 27