MLINPYTHON
MLINPYTHON
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.
Topperworld.in
ii) Well-Defined Inputs: If an algorithm says to take
inputs, it should be well-defined inputs.
Topperworld.in
Advantages of Algorithms:
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
.
Topperworld.in
Analyzing Algorithm
Topperworld.in
Some Algorithm Control Structures are:
1. Sequencing
2. If-then-else
3. for loop
4. While loop
1. Sequencing:
Computation Time = tA + tB
= (max (tA,tB)
= (max (O (n), θ (n2)) = θ (n2)
Topperworld.in
2. If-then-else:
Topperworld.in
Total Computation = (max (tA,tB))
= max (O (n2), θ (n2) = θ (n2)
3. For loop:
The general format of for loop is:
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. 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:
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.
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
4. Master Method
1. Substitution Method:
The Substitution Method Consists of two main steps:
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.
Topperworld.in
Example 3: Consider the following recurrence
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)
In the function to the analysis of a recursive algorithm,
the constants and function take on the following
significance:
Topperworld.in