Daaunit I
Daaunit I
Daaunit I
1. Introduction:
Algorithm: The word algorithm came from the name of a Persian mathematician Abu Jafar
Mohammed Ibn Musa Al Khowarizmi (ninth century). An algorithm is simply s set of rules used
to perform some calculations either by hand or more usually on a machine (computer).
Definition: An algorithm is a finite set of instructions that accomplishes a particular task.
Another definition is a sequence of unambiguous instructions for solving a problem i.e, for
obtaining a required output for any legitimate (genuine) input in a finite amount of time.
In addition all algorithms must satisfy the following criteria (characteristics).
1. Input: zero or more quantities are externally supplied as input.
Consider Fibonacci numbers program, here aim of the problem is to display ten
Fibonacci numbers. No input is required; in the problem itself this is clearly mentioned as ten
Fibonacci values. So zero items required for input.
Another problem is displaying given numbers of evens, so user should accept how
many evens required. Based on the user input the number of evens is to be displayed. So, one data
item is required as input.
2. Output: At least one quantity is produced by given algorithm as output.
In the case of Fibonacci numbers program after executing the program, first ten
Fibonacci values displayed as output.
In second case, based on user input it should display given number of evens. An input of
negative number is wrong, should display proper error message as output. So this program
displays at least one output as error message, or number if outputs that show given number of
steps.
3. Definiteness: Each instruction is clear and unambiguous i.e. each step must be easy to
understand and convey only a single meaning.
4. Effectiveness: each instruction must be very basic, so that it can be carried out by a
person using only pencil and paper.
This step is common in both Fibonacci and primes. For example, if user enters a negative
numbers as input in evens, if you have a step like
Step: If N < 0 then
Go to ERROR
A wrong instruction given as go to ERROR, those kinds of instructions should not be there
in an algorithm.
5. Finiteness: If we can trace out the instructions of an algorithm then for all cases, the
algorithm terminate after a finite number of steps.
Either in the case of Fibonacci or even numbers problem should be solved in some number
of steps. For example, continuous display or Fibonacci series without termination leads to
abnormal termination.
Page 1
INTRODUCTION TO ALGORITHMS UNIT-1
Solution as an algorithm
Algorithm Design
technique
Prove Correctness
No
Yes
Analyse the algorithm
is it efficient No
Yes
1. Understand the problem: This is very crucial phase. If we did any mistake in this step
the entire algorithm becomes wrong. So before designing an algorithm is to understand the
problem first.
2. Solution as an algorithm (Exact vs approximation solving): Solve the problem exactly if
possible. Even though some problems are solvable by exact method, but they are not faster when
compared to approximation method. So in that situation we will use approximation method.
3. Algorithm techniques: In this we will use different design techniques like,
i) Divide-and-conquer
ii) Greedy method
iii) Dynamic programming
iv) Backtracking
v) Branch and bound…. etc.,
4. Prove correctness: once algorithm has been specified, next we have to prove its
correctness. Usually testing is used for proving correctness.
5. Analyze an algorithm: Analyzing an algorithm means studying the algorithm behavior
ie., calculating the time complexity and space complexity. If the time complexity of algorithm is
more then we will use one more designing technique such that time complexity should be
minimum.
6. Coding an algorithm: after completion of all phases successfully then we will code an
algorithm. Coding should not depend on any program language. We will use general notation
(pseudo-code) and English language statement. Ultimately algorithms are implemented as
computer programs.
3. Types of Algorithms:
There are four types of algorithms
1. Approximate algorithm.
2. Probabilistic algorithm.
3. Infinite algorithm.
4. Heuristic algorithm.
Page 2
INTRODUCTION TO ALGORITHMS UNIT-1
Page 3
INTRODUCTION TO ALGORITHMS UNIT-1
5 Specification of algorithm:
There are various ways by which we can specify an algorithm.
Using natural language
Pseudocode
Algorithm
Flow chart
Program (Using programming language)
It is very easy to specify an algorithm using natural language. But many times
specification of algorithm by using natural language is not clear, and may require brief
description.
Example: Write an algorithm to perform addition of two numbers.
Step 1: Read the first number, say ‘a’.
Step 2: Read the second number, say ‘b’.
Step 3: Add the two numbers and store the result in a variable ‘c’.
Step 4: Display the result.
Page 4
INTRODUCTION TO ALGORITHMS UNIT-1
3. The delimiters [;] are used at the end of the each statement.
4. An identifier begins with a letter. Example: sum, sum5, a; but not in 5sum, 4a etc.,.
5. Assignment of values to the variables is done using the assignment operators as := or .
6. There are two Boolean values TRUE and
FALSE. Logical operators: AND, OR, NOT.
Relational operators: <, ,
≥,,=,. Arithmetic operators: +,
-, *, /, %;
7. The conditional statement if-then or if-then-else is written in the following form.
If (condition) then (statement)
If (condition) then (statement-1) else (statement-2)
‘If’ is a powerful statement used to make decisions based as a condition. If a condition is
true the particular block of statements are execute.
Example
if(a>b) then
{
write("a is big");
}
else
{
write("b is big");
}
Page 5
INTRODUCTION TO ALGORITHMS UNIT-1
8. Case statement
case
{
:(condition -1): (statement-1)
:(condition -2): (statement-2)
:(condition -n): (statement-n)
..............
..............
else :(statement n+1);
}
If condition -1 is true, statement -1 executed and the case statement is exited. If
statement -1 is false, condition -2 is evaluated. If condition -2 is true, statement-2 executed
and so on. If none of the conditions are true, statement –(n+1) is executed and the case
statement is exited. The else clause is optional.
9. Loop statements:
For loop:
i). The general form of the for loop is
for variable:=value 1 to value n step do
{ Example:
Statement -1; for i:=1 to 10 do
Statement -1; {
....... write(i); //displaying numbers from 1 to 10
....... i:=i+1;
}
Statement -n;
}
ii). While loop:
The general form of the while loop is
while <condition> Example:
do
i:=1;
{
while(i<=10)do
<statement 1>
{
<statement 2>
write (i);//displaying numbers from 1 to 10
........ i:=1+1;
........ }
<statement n>
}
Note that the statements of while loop are executed as long as <condition> is true.
iii). Repeat-until loop:
The general form of repeat-until is-
repeat
Example
{
i:=1;
<statement 1>
repeat
<statement 2>
{
......
write (i);
......
i:=i+1;
<statement n>
}
}until <condition>
until (i>10);
Page 6
INTRODUCTION TO ALGORITHMS UNIT-1
Page 7
INTRODUCTION TO ALGORITHMS UNIT-1
6. Performance Analysis:
Performance analysis or analysis of algorithms refers to the task of determining the
efficiency of an algorithm i.,e how much computing time and storage an algorithm requires to
run (or execute). This analysis of algorithm helps in judging the value of one algorithm over
another.
To judge an algorithm, particularly two things are taken into consideration
1. Space complexity
2. Time complexity.
Space Complexity: The space complexity of an algorithm (program) is the amount of
memory it needs to run to completion. The space needed by an algorithm has the following
components.
Page 8
INTRODUCTION TO ALGORITHMS UNIT-1
1. Instruction Space.
2. Data Space.
3. Environment Stack Space.
Instruction Space: Instruction space is the space needed to store the compiled version of the
program instructions. The amount of instruction space that is needed depends on factors such as-
i). The compiler used to compile the program into machine code.
ii). The compiler options in effect at the time of compilation.
iii). The target computer, i.,e computer on which the algorithm run.
Note that, one compiler may produce less code as compared to another compiler, when the
same program is compiled by these two.
Data Space: Data space is the space needed to store all constant and variable values. Data
space has two components.
i). Space needed by constants, for example 0, 1, 2.134.
ii). Space needed by dynamically allocated objects such as arrays, structures, classes.
Environmental Stack Space: Environmental stack space is used during execution of
functions. Each time function is involved the following data are saved as the environmental
stack.
i). The return address.
ii). Value of local variables.
iii). Value of formal parameters in the function being invoked.
Environmental stack space is mainly used in recursive functions. Thus, the space requirement
of any program p may therefore be written as
Space complexity S(P) = C + Sp (Instance characteristics).
This equation shows that the total space needed by a program is divided into two parts.
Fixed space requirements(C) is independent of instance characteristics of the inputs and
outputs.
- Instruction space
- Space for simple variables, fixed-size structure variables, constants.
A variable space requirements (SP(1)) dependent on instance characteristics 1.
- This part includes dynamically allocated space and the recursion stack space.
Example of instance character is:
Examples: 1
Algorithm NEC (float x, float y, float z)
{
Return (X + Y +Y * Z + (X + Y +Z)) /(X+ Y) + 4.0;
}
In the above algorithm, there are no instance characteristics and the space needed by X, Y, Z is
independent of instance characteristics, therefore we can write,
S(XYZ) =3+0=3
One space each for X, Y and Z
Space complexity is O(1).
Examples: 2
Algorithm ADD ( float [], int n)
{
sum = 0.0;
for i=1 to n do
sum=sum+X[i];
return sum; }
Page 9
INTRODUCTION TO ALGORITHMS UNIT-1
Here, atleast n words since X must be large enough to hold the n elements to be summed.
Here the problem instances is characterized by n, the number of elements to be summed. So, we
can write,
S(ADD) =3+n
3-one each for n, I and sum
Where n- is for array X[],
Space complexity is O(n).
Time Complexity
The time complexity of an algorithm is the amount of compile time it needs to run to
completion. We can measure time complexity of an algorithm in two approaches
1. Priori analysis or compile time
2. Posteriori analysis or run (execution) time.
In priori analysis before the algorithm is executed we will analyze the behavior of the
algorithm. A priori analysis concentrates on determining the order if execution of statements.
In Posteriori analysis while the algorithm is executed we measure the execution time.
Posteriori analysis gives accurate values but it is very costly.
As we know that the compile time does not depend on the size of the input. Hence, we will
confine ourselves to consider only the run-time which depends on the size of the input and this
run-time is denoted by TP(n). Hence
The time (T(P)) taken by a program P is the sum of the compile time and execution time.
The compile time does not depend on the instance characteristics, so we concentrate on the
runtime of a program. This runtime is denoted by tp (instance characteristics).
The following equation determines the number of addition, subtraction, multiplication,
division compares, loads stores and so on, that would be made by the code for p.
tp(n) = CaADD(n)+ CsSUB(n)+ CmMUL(n)+ CdDIV(n)+……………..
where n denotes instance characteristics, and Ca, Cs, Cm, Cd and so on…..
As denote the time needed for an addition, subtraction, multiplication, division and so on,
and ADD, SUB, MUL, DIV and so on, are functions whose values are the number of additions,
subtractions, multiplications, divisions and so on. But this method is an impossible task to find
out time complexity.
Another method is step count. By using step count, we can determine the number if steps
needed by a program to solve a particular problem in 2 ways.
Method 1: introduce a global variable “count”, which is initialized to zero. So each time a
statement in the signal program is executed, count is incremented by the step count of that
statement.
Page
10
INTRODUCTION TO ALGORITHMS UNIT-1
Example:
Algorithm sum with count statement added
count:=0;
Algorithm Sum(a, Algorithm Sum(a,n)
n) {
{ s:=0;
s:=0; count:=count+1;
for i:=1 to n do
for i:=1 to n do {
{ count:=count +1;
s:=s+a[i];
s:=s+a[i]; count:=count+1;
}
}
count:=count+1; //for last time of for loop
count:=count+1; //for return statement
return s; return s;
} }
Thus the total number of steps are 2n+3
Method 2: The second method to determine the step count of an algorithm is to build a table
in which we list the total number of steps contributed by each statement.
Statement S/e Frequency Total steps
Ex:
1. Algorithm Sum(a, n) 0 - 0
2. { 0 - 0
3. s:=0; 1 1 1
5. s:=s+a[i]; 1 n n
6. return s; 1 1 1
7. } 0 - 0
The S/e (steps per execution) of a statement is the amount by which the count changes as
a result of the execution of that statement. The frequency determines the total number of times
each statement is executed.
Complexity of Algorithms:
1. Best Case: Inputs are provided in such a way that the minimum time is required to
process them.
2. Average Case: The amount of time the algorithm takes on an average set of inputs.
3. Worst Case: The amount of time the algorithm takes on the worst possible set of inputs.
Example: Linear Search
3 4 5 6 7 9 10 12 15
A 1 2 3 4 5 6 7 8 9
Best Case: If we want to search an element 3, whether it is present in the array or not. First, A(1)
is compared with 3, match occurs. So the number of comparisons is only one. It is observed that
search takes minimum number of comparisons, so it comes under best case.
Time complexity is O(1).
Page
11
INTRODUCTION TO ALGORITHMS UNIT-1
Average Case: If we want to search an element 7, whether it is present in the array or not.
First, A(1) is compared with 7 i,.e, (3=7), no match occurs. Next, compare A(2) and 7, no match
occurs. Compare A(3) and A(4) with 7, no match occurs. Up to now 4 comparisons takes place.
Now compare A(5) and 7 (i.,e, 7=7), so match occurs. The number of comparisons is 5. It is
observed that search takes average number of comparisons. So it comes under average case.
Note: If there are n elements, then we require n/2 comparisons.
. n
. . Time complexity is O = O(n) (we neglect constant)
Worst Case: If we want to search an element 15, whether it is present in the array or not.
First, A(1) is compared with 15 (i.,e, 3=15), no match occurs. Continue this process until either
element is found or the list is exhausted. The element is found at 9 th comparison. So number of
comparisons are 9.
Time complexity is O(n).
Note: If the element is not found in array, then we have to search entire array, so it comes under
worst case.
7. Asymptotic Notation:
Accurate measurement of time complexity is possible with asymptotic notation.
Asymptotic complexity gives an idea of how rapidly the space requirement or time requirement
grow as problem size increase. When there is a computing device that can execute 1000 complex
operations per second. The size of the problem is that can be solved in a second or minute or an
hour by algorithms of different asymptotic complexity. In general asymptotic complexity is a
measure of algorithm not problem. Usually the complexity of an algorithm is as a function
relating the input length to the number of steps (time complexity) or storage location (space
complexity). For example, the running time is expressed as a function of the input size ‘n’ as
follows.
f(n)=n4+100n2+10n+50 (running time)
There are four important asymptotic notations.
1. Big oh notation (O)
2. Omega notation ().
3. Theta notation ()
Let f(n) and g(n) are two non-negative functions.
Big oh notation
Big oh notation is denoted by ‘O’. it is used to describe the efficiency of an algorithm. It is
used to represent the upper bound of an algorithms running time. Using Big O notation, we can
give largest amount of time taken by the algorithm to complete.
Definition: Let f(n) and g(n) be the two non-negative functions. We say that f(n) is said to be
O(g(n)) if and only if there exists a positive constant ‘c’ and ‘n0‘ such that,
f(n)c*g(n) for all non-negative values of n, where n≥n0.
Here, g(n) is the upper bound for f(n).
Page
12
INTRODUCTION TO ALGORITHMS UNIT-1
4
Ex: Let f(n) = 2n + 5n2 + 2n +3
< 2n4 + 5n4 + 2n4 +3n4 c*g(n)
4
< (2+5+2+3)n
4 f(n)
< 12n .
.
. . f(n)=12n 4
4
This implies g(n)=n , n > 1
.
. . c=12 and n0 =1
. 4
. . f(n)=O(n )
n0
The above definition states that the function ‘f’ is almost ‘c’ times the function ‘g’ when ‘n’ is
greater than or equal to n0.
This notion provides an upper bound for the function ‘f’ i.,e, the function g(n) is an upper
bound on the value of f(n) for all n, where n≥ n0.
Example:
4 f(n)
Let f(n) = 2n + 5n2 + 2n +3
> 2n4 (for example as n ,
lower order oterms c*g(n)
are insignificant)
. 4
.. f(n) > 2n , n >1
. 4
. . g(n)=n , c=2 and n0 =1
. 4
. . f(n)= (n )
n0
Big Theta notation
The big theta notation is denoted by ‘’. It is in between the upper bound and lower
bound of an algorithms running time.
Definition: Let f(n) and g(n) be the two non-negetive functions. We say that f(n) is said to
be (g(n)) if and only if there exists a positive constants ‘c1’ and ‘c2’, such that,
c1g(n) f(n) c2g((n) for all non-negative values n, where n ≥ n0.
The above definition states that the function f(n) lies between ‘c 1’times the function g(n)
and ‘c2’, times the function g(n) where ‘c1’ and ‘c2’ are positive constants.
This notation provides both lower and upper bounds for the function f(n) i.,e, g(n) is both
lower and upper bounds on the value of f(n), for large n. in other words theta notation says that
f(n) is both O(g(n)) and (g(n)) for all n, where n≥n0.
Page
13
INTRODUCTION TO ALGORITHMS UNIT-1
This function f(n) = (g(n)) iff g(n) is both upper and lower bound an f(n).
Example:
c2*g(n)
f(n) = 2n4 + 5n2 + 2n +3
f(n)
2n4 2n4 + 5n2 + 2n +3 12n4
n0 n
Page
14
INTRODUCTION TO ALGORITHMS UNIT-1
.
. . g(n) = n4
.
. . c1=2, c2=12 and n0=1
.
. . f(n)=(n
4)
f(n)
nlim g(n) =0
Find the time complexity for sum f given array elememts The
aymptotic complexity of sum is as follows
Total (n)
Page
15
INTRODUCTION TO ALGORITHMS UNIT-1
n
Thus time complexity T(n) = 1
Page
16
INTRODUCTION TO ALGORITHMS UNIT-1
i=1
= 1+1+1 +1 n
=n
.
. . T(n) = O(n)
8 Probabilistic Analysis:
Probabilistic analysis of algorithms is an approach to estimate the complexity of an
algorithm. It uses the probability in the analysis of problems. It starts from an assumption about a
probabilistic distribution of the set of all possible inputs. This assumption is then used to design an
efficient algorithm or to compute an expected running time of a known algorithm.
The following is the simple example as probabilistic average case analysis.
Example: Consider linear search algorithm which searches a target element say x, in the
given list of size n. in the worst case, the algorithm will examine all n elements in the list before
terminating.
For a probabilistic average-case analysis, it is generally assumed that all possible
terminations are equally likely-that is, the probability that x, will be found at position 1 is 1/x at
position 2 is 1/n and so on.
The average search cost is therefore the sum of all possible search costs each multiplied
by their associated probability.
For example, if n=5, we would have
Average search cost=1/5(1 +2 +3 +4 +5)=3.
In general case we have
Average search cost =1/n(n(n+1)/2)=(n+1)/2
Probabilistic analysis is mainly useful in estimate running time of an algorithm, calculating
search costs in a searching algorithm etc.
9. Amortized Analysis:
Amortized analysis refers to finding the average running time per operation, over a worst
case sequence of operations. That is the main goal of amortized analysis is to analyze the time
per operation for a series of operations. Sometimes single operation might be expensive; in that
case amortized analysis specifies average over a sequence of operations. Amortized cost per
operation for a sequence of n operations is the total cost of operations divided by n.
For example, if we have 100 operations at cost 1, followed by one operation at cost 100,
then amortized cost per operation is 200/101 < 2. Amortized analysis does not allow random
selection of input.
The average case analysis and amortized analysis are different. In average case analysis,
we are averaging over all possible inputs whereas in amortized analysis we are averaging over a
sequence of operations.
Amortized analysis does not allow random selection of input.
There are several techniques used in amortized analysis.
1. Aggregate Analysis: In this type of analysis upper bound T(n) on the total cost of a
sequence of n operations is decided, then the average cost is calculated as T(n)/n.
2. Accounting Method: In this method the individual cost of each operation is determined,
by combining immediate execution time and its influence on the running time of future operations.
3. Potential Method: It is like the accounting method, but overcharges operations early to
compensate for undercharges later.
Page
17