Chapter 1 - Introduction To Algorithms
Chapter 1 - Introduction To Algorithms
Problem: A problem can be a real-world problem or any instance from the real-world
problem for which we need to create a program or the set of instructions. The set of
instructions is known as an algorithm.
Algorithm: An algorithm will be designed for a problem which is a step by step procedure.
Input: After designing an algorithm, the required and the desired inputs are provided to the
algorithm.
Processing unit: The input will be given to the processing unit, and the processing unit will
produce the desired output.
Output: The output is the outcome or the result of the program.
Since it does not incorporate any standardized style or format, it can vary from one
company to another.
Error possibility is higher while transforming into a code.
It may require a tool for extracting out the Pseudocode and facilitate drawing
flowcharts.
It does not depict the design.
Priori Estimates: Obtain a function which bounds the algorithm’s complexity time. The
amount of time a single execution will take or the number of times a statement is executed.
However, it is possible to know the exact amount of time to execute any command unless:
the machine we are executing is known;
machine instruction set
time required by each machine instruction;
translation of the compiler from source to machine lang.
Posteriori Estimates: Something to do with memory spaces consumed.
Example: Use of arrays vs. linked-list
FOR loops. At most the running time of the statement inside the for-loop times the
number of iterations.
NESTED FOR loops. Analysis is done from the inner loop going outward. The total
running time of a statement inside a group of for loops is the running time of the
statement multiplied by the product of the sizes of all the for loops.
CONSECUTIVE STATEMENTS. The statement with the maximum running time.
IF/ELSE. Never more than the running time of the test plus the larger of the running
times of the conditional block of statements.
CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 17
EXAMPLE 1: FOR AND NESTED FOR LOOPS
Compute for the frequency count (f(n)) given the following code:
for(l=1; l<=c; l++){
for(j=c; j>=1; j--)
printf(“%d %d”, l, j);
for(k=1; k<=c; k++)
printf(“%d”, k);
}
CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 18
EXAMPLE 2: WHILE LOOPS
ctr = 1;
while ctr<=n-2{
for(k=1; k<=n; k++)
printf(“%d %d”, ctr, k);
ctr = ctr + 1;
}
if(x>2) {
x++;
printf(“%d”, x);
} else {
for(l=0; l<n; l++){
printf(“%d”, l);
x++;
}
}
CHAPTER 1: INTRODUCTION TO ALGORITHMS Monday, 20 February 2023 20
ANALYSIS OF ALGORITHM
Space Complexity: The space complexity can be understood as the amount of space
required by an algorithm to run to completion.
Time Complexity: Time complexity is a function of input size n that refers to the
amount of time needed by an algorithm to run to completion.
Worst-case time complexity: For 'n' input size, the worst-case time complexity can
be defined as the maximum amount of time needed by an algorithm to complete its
execution.
Average case time complexity: For 'n' input size, the average-case time complexity
can be defined as the average amount of time needed by an algorithm to complete its
execution.
Best case time complexity: For 'n' input size, the best-case time complexity can be
defined as the minimum amount of time needed by an algorithm to complete its
execution.
Big OH Notation (O). Big O notation is an asymptotic notation that measures the
performance of an algorithm by simply providing the order of growth of the function.
This notation provides an upper bound on a function which ensures that the function
never grows faster than the upper bound. So, it gives the least upper bound on a
function so that the function never grows faster than this upper bound.
If f(n) and g(n) are the two functions defined for positive integers, then f(n) = O(g(n))
as f(n) is big oh of g(n) or f(n) is on the order of g(n)) if there exists constants c and
no such that:
f(n) ≤ c.g(n) for all n ≥ no
Omega Notation (Ω). It basically describes the best-case scenario which is opposite
to the big o notation. It is the formal way to represent the lower bound of an
algorithm's running time. It measures the best amount of time an algorithm can
possibly take to complete or the best-case time complexity. It determines what is the
fastest time that an algorithm can run.
If f(n) and g(n) are the two functions defined for positive integers, then f(n) = Ω (g(n))
as f(n) is Omega of g(n) or f(n) is on the order of g(n)) if there exists constants c and
no such that:
f(n)>=c.g(n) for all n≥no and c>0
Theta Notation (θ). The theta notation mainly describes the average case scenarios. It
represents the realistic time complexity of an algorithm. Every time, an algorithm does not
perform worst or best, in real-world problems, algorithms mainly fluctuate between the
worst-case and best-case, and this gives us the average case of the algorithm.
Let f(n) and g(n) be the functions of n where n is the steps required to execute the program
then:
f(n)= θg(n)
The above condition is satisfied only if when:
c1.g(n)<=f(n)<=c2.g(n)
Analyze Examples 1, 2 and 3 (See slides 18, 19 and 20) using the following notations:
̶ Big OH Notation
̶ Omega Notation
̶ Theta Notation