0% found this document useful (0 votes)
3 views27 pages

MLINPYTHON

An algorithm is a defined set of rules or instructions for problem-solving and is crucial in programming as it underpins the functionality of applications. Key properties of algorithms include clear inputs and outputs, definiteness, finiteness, effectiveness, and independence from programming languages. The document also discusses the advantages and disadvantages of algorithms, performance analysis through time and space complexity, and methods for analyzing algorithms using asymptotic notation.

Uploaded by

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

MLINPYTHON

An algorithm is a defined set of rules or instructions for problem-solving and is crucial in programming as it underpins the functionality of applications. Key properties of algorithms include clear inputs and outputs, definiteness, finiteness, effectiveness, and independence from programming languages. The document also discusses the advantages and disadvantages of algorithms, performance analysis through time and space complexity, and methods for analyzing algorithms using asymptotic notation.

Uploaded by

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

What is Algorithm

He word Algorithm means “a process or set of rules to


be followed in calculations or other problem-solving
operations”. Therefore Algorithm refers to a set of
rules/instructions that step-by-step define how a work is
to be executed upon in order to get the expected results.
Why study Algorithm ?
The importance of algorithms is very high in today's
world but in reality, what we focus on is the result, be it
ios apps, android apps, or any other application. The
reason we have these resultant applications is the
Algorithm. If programming a building, then the
algorithm is the pillar programming is standing on, and
without pillars, there is no building. But why do we go
for algorithms instead of going for the application
directly? Let's get that from an example. Let's suppose
we are building something, and we have the result in
mind. We are not an expert, but still, we bring all the
necessary items and design that thing. It also looks like

Topperworld.in
what we had in mind. But it does not fulfill the purpose
we built it for. Do we have any use of it? This is what's
an algorithm for a program because it provides meaning
to the program. There is much reason to study
algorithms as it is used in almost every digital
application we use today. To showcase the value
algorithms have, here we have some of its applications.

Properties of Algorithm
All Algorithms must satisfy the following criteria -
1) Input
There are more quantities that are extremely supplied.
2) Output
At least one quantity is produced.
3) Definiteness
Each instruction of the algorithm should be clear and
unambiguous.

4) Finiteness
The process should be terminated after a finite number
of steps.

5) Effectiveness
Every instruction must be basic enough to be
carried out theoretically or by using paper and
pencil

Topperworld.in
For example,suppose you are cooking a recipe and you chop
vegetables which are not be used in the recipe then it is a waste of
time.

6)Independent
An algorithm should have step-by-step directions, which
should be independent of any programming code. It
should be such that it could be run on any of the
programming languages.

What are the Characteristics of an Algorithm


i) Clear and Unambiguous: Algorithm should be
clear and unambiguous. Each of its steps should
be clear in all aspects and must lead to only one
meaning.

Topperworld.in
ii) Well-Defined Inputs: If an algorithm says to take
inputs, it should be well-defined inputs.

iii) Well-Defined Outputs: The algorithm must


clearly define what output will be yielded and it
should be well-defined as well.
iv) Finite-ness: The algorithm must be finite, i.e. it
should not end up in an infinite loops or similar.

v) Feasible: The algorithm must be simple, generic


and practical, such that it can be executed upon
with the available resources. It must not contain
some future technology, or anything.

vi) Language Independent: The Algorithm designed


must be language-independent, i.e. it must be just
plain instructions that can be implemented in any
language, and yet the output will be same, as
expected.

Topperworld.in
Advantages of Algorithms:

i)It is easy to understand.

ii) Algorithm is a step-wise representation of a solution


to a given problem.
iii)In Algorithm the problem is broken down into
smaller pieces or steps hence, it is easier for the
programmer to convert it into an actual program.

Disadvantages of Algorithms:

Topperworld.in
i)Writing an algorithm takes a long time so it is time-
consuming.
ii)Branching and Looping statements are difficult to
show in Algorithms.
Performance Analysis of Algorithm
There are two types are:
i) Time Complexity
ii) Space Complexity
Time Complexity
Time complexity is the amount of time taken
by an algorithm to run, as a function of the
length of the input. It measures the time taken
to execute each statement of code in an
algorithm. Upskilling with the help of
an introduction to algorithms free course will
help you understand time complexity clearly.
.

Topperworld.in
.

What Is Space Complexity?

When an algorithm is run on a computer, it necessitates


a certain amount of memory space. The amount of
memory used by a program to execute it is represented
by its space complexity. Because a program requires
memory to store input data and temporal values while
running, the space complexity is auxiliary and input
space.

Topperworld.in
Analyzing Algorithm

To analyze a programming code or algorithm, we must


notice that each instruction affects the overall
performance of the algorithm and therefore, each
instruction must be analyzed separately to analyze
overall performance. However, there are some algorithm
control structures which are present in each
programming code and have a specific asymptotic
analysis.

Topperworld.in
Some Algorithm Control Structures are:
1. Sequencing
2. If-then-else
3. for loop
4. While loop

1. Sequencing:

Suppose our algorithm consists of two parts A and B. A


takes time tA and B takes time tB for computation. The
total computation "tA + tB" is according to the sequence
rule. According to maximum rule, this computation time
is (max (tA,tB)).
Example:
Suppose tA =O (n) and tB = θ (n2).
Then, the total computation time can be calculated as

Computation Time = tA + tB
= (max (tA,tB)
= (max (O (n), θ (n2)) = θ (n2)

Topperworld.in
2. If-then-else:

The total time computation is according to the condition


rule-"if-then-else." According to the maximum rule, this
computation time is max (tA,tB).
Example:
Suppose tA = O (n2) and tB = θ (n2)
Calculate the total computation time for the following:

Topperworld.in
Total Computation = (max (tA,tB))
= max (O (n2), θ (n2) = θ (n2)

3. For loop:
The general format of for loop is:

1. For (initialization; condition; updation)


2.
3. Statement(s);

Topperworld.in
Complexity of for loop:
The outer loop executes N times. Every time the outer
loop executes, the inner loop executes M times. As a
result, the statements in the inner loop execute a total
of N * M times. Thus, the total complexity for the two
loops is O (N2)
Consider the following loop:

1. for i ← 1 to n
2. {
3. P (i)
4. }
If the computation time ti for ( PI) various as a function
of "i", then the total computation time for the loop is
given not by a multiplication but by a sum i.e.

1. For i ← 1 to n
2. {
3. P (i)
4. }
Takes

Topperworld.in
If the algorithms consist of nested "for" loops, then the
total computation time is
For i ← 1 to n
{
For j ← 1 to n
{
P (ij)
}
}
Example:
Consider the following "for" loop, Calculate the total
computation time for the following:

1. For i ← 2 to n-1
2. {
3. For j ← 3 to i
4. {
5. Sum ← Sum+A [i] [j]
6. }
7. }

Solution:

Topperworld.in
The total Computation time is:

While loop:
The Simple technique for analyzing the loop is to
determine the function of variable involved whose value
decreases each time around. Secondly, for terminating
the loop, it is necessary that value must be a positive
integer. By keeping track of how many times the value
of function decreases, one can obtain the number of
repetition of the loop. The other approach for analyzing
"while" loop is to treat them as recursive algorithms.
Algorithm:
1. 1. [Initialize] Set k: =1, LOC: =1 and MAX: = DA
TA [1]
2. 2. Repeat steps 3 and 4 while K≤N
3. 3. if MAX<DATA [k],then:
4. Set LOC: = K and MAX: = DATA [k]
5. 4. Set k: = k+1
6. [End of step 2 loop]
7. 5. Write: LOC, MAX
8. 6. EXIT

Topperworld.in
Example:
The running time of algorithm array Max of computing
the maximum element in an array of n integer is O (n).
Solution:

1. array Max (A, n)


2. 1. Current max ← A [0]
3. 2. For i ← 1 to n-1
4. 3. do if current max < A [i]
5. 4. then current max ← A [i]
6. 5. return current max.
The number of primitive operation t (n) executed by this
algorithm is at least.

1. 2 + 1 + n +4 (n-1) + 1=5n
2. 2 + 1 + n + 6 (n-1) + 1=7n-2

Topperworld.in
The best case T(n) =5n occurs when A [0] is the
maximum element. The worst case T(n) = 7n-2 occurs
when element are sorted in increasing order.
We may, therefore, apply the big-Oh definition with c=7
and n0=1 and conclude the running time of this is O (n).

Asymptotic Notation
Asymptotic Notation is used to describe the running
time of an algorithm - how much time an algorithm
takes with a given input, n.
Asymptotic Notation is a
way of comparing function that ignores constant factors
and small input sizes. Three notations are used to
calculate the running time complexity of an algorithm:
There are three different notations: big O, big Theta
(Θ), and big Omega (Ω).
Why is Asymptotic Notation Important?
1. They give simple characteristics of an algorithm's
efficiency.
2. They allow the comparisons of the performances of
various algorithms.
1) Big-O Notation
The Big-O notation describes the worst-case running
time of a program. We compute the Big-O of an
algorithm by counting how many iterations an

Topperworld.in
algorithm will take in the worst-case scenario with an
input of N. We typically consult the Big-O because we
must always plan for the worst case. For example,
O(log n) describes the Big-O of a binary search
algorithm.
1. f (n) ⩽ k.g (n)f(n)⩽k.g(n) for n>n0n>n0 in all case

For Example:

1. 1. 3n+2=O(n) as 3n+2≤4n for all n≥2


2. 2. 3n+3=O(n) as 3n+3≤4n for all n≥3
Hence, the complexity of f(n) can be represented as O (g
(n))
2) Big-Ω Notation
Big-Ω (Omega) describes the best running time of a
program. We compute the big-Ω by counting how many
iterations an algorithm will take in the best-case

Topperworld.in
scenario based on an input of N. For example, a Bubble
Sort algorithm has a running time of Ω(N) because in
the best case scenario the list is already sorted, and the
bubble sort will terminate after the first iteration.

F (n) ≥ k* g (n) for all n, n≥ n0

For Example:
f (n) =8n2+2n-3≥8n2-3
=7n2+(n2-3)≥7n2 (g(n))
Thus, k1=7
Hence, the complexity of f (n) can be represented as Ω
(g (n))
3. Theta (θ) Notation: The function f (n) = θ (g (n))
[read as "f is the theta of g of n"] if and only if there exists
positive constant k1, k2 and k0 such that
k1 * g (n) ≤ f(n)≤ k2 g(n)for all n, n≥ n0

Topperworld.in
3n+2= θ (n) as 3n+2≥3n and 3n+2≤ 4n, for n
k1=3,k2=4, and n0=2
Hence, the complexity of f (n) can be represented as θ
(g(n)).
Recurrence Relation
A recurrence is an equation or inequality that describes a
function in terms of its values on smaller inputs. To solve
a Recurrence Relation means to obtain a function defined
on the natural numbers that satisfy the recurrence.
For Example, the Worst Case Running Time T(n) of the
MERGE SORT Procedures is described by the
recurrence.
T (n) = θ (1) if n=1
2T + θ (n) if n>1

Topperworld.in
There are four methods for solving Recurrence:
1. Substitution Method

2. Iteration Method

3. Recursion Tree Method

4. Master Method
1. Substitution Method:
The Substitution Method Consists of two main steps:

1. Guess the Solution.


2. Use the mathematical induction to find the
boundary condition and shows that the guess is
correct.
For Example1 Solve the equation by Substitution
Method.

T (n) = T +n
We have to show that it is asymptotically bound by O
(log n).

Topperworld.in
Solution:
For T (n) = O (log n)
We have to show that for some constant c

1. T (n) ≤c logn.
Put this in given Recurrence Equation.

T (n) ≤c log + 1
≤c log + 1 = c logn-clog2 2+1
≤c logn for c≥1
Thus T (n) =O logn.
Example2. Consider the Recurrence

T (n) = 2T + n n>1
Find an Asymptotic bound on T.
Solution:

Topperworld.in
2. Iteration Methods
It means to expand the recurrence and express it as a
summation of terms of n and initial condition.
Example1: Consider the Recurrence

1. T (n) = 1 if n=1
2. = 2T (n-1) if n>1

Topperworld.in
3) Recurrence Tree Method: In this method,
we draw a recurrence tree and calculate the time
taken by every level of tree. Finally, we sum the
work done at all levels. To draw the recurrence
tree, we start from the given recurrence and keep
drawing till we find a pattern among levels. The
pattern is typically a arithmetic or geometric
series.
1. In general, we consider the second term in recurrence
as root.
2. It is useful when the divide & Conquer algorithm is
used.

Topperworld.in
3. It is sometimes difficult to come up with a good guess.
In Recursion tree, each root and child represents the cost
of a single subproblem.

For example consider the recurrence relation


Consider T (n) = 2T + n2
We have to obtain the asymptotic bound using recursion
tree method.
Solution: The Recursion tree for the above recurrence is

Topperworld.in
Example 3: Consider the following recurrence

Obtain the asymptotic bound using recursion tree


method.
Solution: The given Recurrence has the following
recursion tree

Topperworld.in
When we add the values across the levels of the recursion
trees, we get a value of n for every level. The longest path
from the root to leaf is

Topperworld.in
4) Master Method
The Master Method is used for solving the following
types of recurrence

T (n) = a T + f (n) with a≥1 and b≥1 be constant & f(n)


be a function and can be interpreted as
Let T (n) is defined on non-negative integers by the
recurrence.

T (n) = a T + f (n)
In the function to the analysis of a recursive algorithm,
the constants and function take on the following
significance:

o n is the size of the problem.


o a is the number of subproblems in the recursion.
o n/b is the size of each subproblem. (Here it is
assumed that all subproblems are essentially the
same size.)
o f (n) is the sum of the work done outside the
recursive calls, which includes the sum of dividing
the problem and the sum of combining the solutions
to the subproblems.

Topperworld.in

You might also like