Chapter One
Chapter One
algorithms
1
JIMMA UNIVERSITY
JIMMA INSTITUTE OF TECHNOLOGY
FACULTY OF COMPUTING AND INFORMATICS
CHAPTER ONE
Introduction to Data
Structures and
Algorithms
Objective
2
Characteristics of algorithms
Algorithm analysis
Data Structure:
The way data is organized in a computer’s memory so
that it can be used effectively.
Algorithm :
The sequence of computational steps to solve a
problem.
Model
Memory Usage
Communication Bandwidth
Empirical:
It
uses actual clock-time.
Theoretical:
Time Complexity:
Determine the approximate number of operations required to
solve a problem of size n.
Space Complexity:
Determine the approximate memory required to solve a problem
of size n.
Benefits
21
1 for setup +
T (n)= 1+1+1+(1+n+1+n)+2n+1 =
4n+6 = O(n)
Example
27
void func()
Time Units to Compute
{
-------------------------------------------------
int x=0; int i=0;
1 for the first assignment statement: x=0;
int j=1; 1 for the second assignment statement: i=0;
cout<< ―Enter an Integer 1 for the third assignment statement: j=1;
value‖; cin>>n;
1 for the output statement. 1 for the input
while (i<n){ x++;
statement.
i++; }
In the first while loop:
while (j<n) {
n+1 tests
j++;
n loops of 2 units for the two increment
}} (addition) operations
In the second while loop:
n tests
n-1 increments
------------------------------------------------------
T-(n)= 1+1+1+1+1+n+1+2n+n+n-1 = 5n+5
= O(n)
Asymptotic Analysis
28
too complicated
3n2+4n+1 3 n2 n2
15 n2+6n 15 n2 n2
a n2+bn+c a n2 n2
Big Oh Notation, Ο
The notation Ο(n) is the formal way to express the upper bound of an
algorithm's running time. It measures the worst case time complexity or the
longest amount of time an algorithm can possibly take to complete.
Omega Notation, Ω
The notation Ω(n) is the formal way to express the lower bound of an
algorithm's running time. It measures the best case time complexity or the
best amount of time an algorithm can possibly take to complete.
Theta Notation, θ
The notation θ(n) is the formal way to express both the lower bound and
the upper bound of an algorithm's running time. It measures the average case
time complexity.
Average, Best, and Worst-Case
33
The time required by an algorithm falls under the following three types −
Average case:
— Average time required for program execution
Real-world distributions difficult to predict
Best case:
— Minimum time required for program execution
Seems unrealistic
Worst case:
— Maximum time required for program execution
Gives an absolute guarantee
On which input instances should the algorithm’s performance be Judged?
We will use the worst-case measure.
Common complexity classes
34
The most common complexity classes are (in ascending order of complexity):
• O(1),
•O(log n),
•O(n),
•O(n log n),
•O(n²),
•O(n3),
•O(2n)
35
o f
n d ne
e e ro
h
T ap te
ch