You are on page 1of 11

CS141- ALGORITHMS AND COMPLEXITY

CLASS 1- INTRODUCTION TO ALGORITHMS AND


ALGORITHM COMPLEXITY

1
What is an algorithm?
Algorithms
 An algorithm is a well defined steps for solving a particular
problem.
 The time and space it uses are the two major measures of the
efficiency of an algorithm.
 To evaluate an algorithm’s efficiency, logical units that
expresses a relationship between the size n and the amount of
time t required to process the data should be used.

2
Complexity Analysis
Complexity of Algorithms
 The complexity of an algorithm M is the function f(n) which
gives the running time and/or storage space requirement of the
algorithm in terms of the size n of the input data.
 There are three cases of complexity: worst case, average case
and best case.
 Worst case: finding the maximum value of f(n) for any
possible input

3
Complexity Analysis (contd..)
 Average case: the expected value of f(n)
 Best Case: The minimum possible value of f(n)
Web Link:
Download the book named Data Structures by Seymour Lipschutz with
the help the portal address mentioned below.
https://archive.org/details/DataStructuresWithCBySchaumLipschutz
Refer section 2.5 (page number: 2.14) for understanding complexity of
algorithms

4
Complexity Analysis (contd..)

 Consider M is an algorithm, and n is the size of the input data.


Complexity of M increases as n increases. Some f(n) values
are n, log2 n ,n log2 n

5
Complexity Analysis(linear loop)
Case 1: f(n)=n
Value of f(n) is equal to the number of times the loop executes
Example:
1. i = 1
2. Loop (i <= 5)
1. application code
2. i = i + 1
3. end loop

Answer: 5, thus f(n) = n

6
Complexity Analysis(logarithmic loop)
Take any number n; say, 16. How many times can you divide n by two before you get a number
less than one? For 16, we have that
16/2=8;
8/2=4;
4/2=2;
2/2=1;
Notice that this ends up taking four steps to complete. Interestingly, we also have that
log2 16 = 4. 

7
Complexity Analysis(logarithmic loop)
what about 128?
128/2=64;
64/2=32;
32/2=16;
16/2=8;
8/2=4;
4/2=2;
2/2=1;
his took seven steps, and log2 128 = 7.

8
Complexity Analysis(logarithmic loop)
In general,
n / 2i ≤ 1
n ≤ 2i
log2 n ≤ I

Thus any algorithm that repeatedly divides the input size by some
fraction will need O(log n) iterations to terminate.

9
Complexity Analysis
Refer scanned documents for further reading of complexity analysis
of iterative and recursive programs.

10
The End

11

You might also like