CS-IT341 Lecture 3
CS-IT341 Lecture 3
Information Technology
CS/IT 341
Lecture 3
Asymptotic Growth Rate (1)
• Changing the hardware/software environment
• Affects T(n) by constant factor, but does not alter the growth rate of T(n)
5
Performance Classification
f(n) Classification
1
:
Constant run time is fixed, and does not depend upon n. Most instructions are
executed once, or only a few times, regardless of the amount of information being processed
log n Logarithmic: when n increases, so does run time, but much slower. Common in
programs which solve large problems by transforming them into smaller problems.
n Linear: run time varies directly with n. Typically, a small amount of processing is done on
each element.
n log n When n doubles, run time slightly more than doubles. Common in programs
which break a problem down into smaller sub-problems, solves them independently, then combines
solutions
n2 Quadratic: when n doubles, runtime increases fourfold. Practical only for small
problems; typically the program processes all pairs of input (e.g. in a double nested loop).
2n
6
Exponential: when n doubles, run time squares. This is often the result of a natural, “brute
force” solution.
A comparison of Growth-Rate Functions (1)
7
A comparison of Growth-Rate Functions (2)
8
A comparison of Growth-Rate Functions (3)
Size does Matter:
What happens if we increase the input size N?
9
A comparison of Growth-Rate Functions (4)
10
Approaches to Asymptotic Growth Rate
(Nested Loops)
i) Top-Down Approach
11
11
Example: Top-down vs. Bottom-up
for i 1 to n do
for j 1 to m do
ab
= 2 i 3 i
2
i 1 i 1
= 2*[(2n3+3n2+n)/6] + 3*[n(n+1)/2]
Step-1: Bottom while Loop (line 5 and 6) = O(n3)
j
while(j) = 1 j 1
k 0 Quadratic Series
Step-2: Inner For Loop (line 3 and 4)
2i 2i
for(i) = while( j ) j 1
j 1 j 1
2i 2i
= j 1 13
j 1 j 1
= [ 2i * (2i+1) /2 ] + 2i
2i2+3i
Data Structures Review
• Data structure is the logical or mathematical model of a
particular organization of data.
• Data structures let the input and output be represented in a
way that can be handled efficiently and effectively.
• Data may be organized in different ways.
Array
Linked list
14
Queue
Graph/Tree Stack
Arrays
Worst case: ‘x’ is located in last location of the array or is not there
at all.
T(n) = 1 + n + n + 1 +1
= 2n +3
= O(n)
17
Average case
Average Case: Assume that it is equally likely for ‘x’ to appear at
any position in array A,. Accordingly, the number of comparisons
can be any of the numbers 1,2,3,..., n, and each number occurs
with probability p = 1/n.
This agrees with our intuitive feeling that the average number of
comparisons needed to find the location of ‘x’ is approximately
equal to half the number of elements in the A list. 18
Linked List
A B C
Head
• LIFO
• Implemented using linked-list or arrays 2132
123
123
123
123
123
123
• FIFO
2132
• Implemented using linked-list or arrays
123
123
2544
33
0.05 N2 = O(N2)
3N = O(N)
Time (steps)
23
Input (size)
N = 60
Running Time vs. Time Complexity
• Running time is how long it takes a program to run.
n f(n) a n2 % of n2
125 2.8 2.7 94.7
250 11.0 10.8 98.2
500 43.4 43.1 99.3
1000 172.9 172.4 99.7
2000 690.5 689.6 99.9 26
Complexity Examples (1)
What does the following algorithm compute?
Comparisons: 2n + 2
Time complexity is O(n).
28
Model of Computation
Drawbacks:
• poor assumption that each basic operation takes constant time
• Adding, Multiplying, Comparing etc.
30
Exponent Review
x a x b x a b
xa a b
x
xb
( x a ) b x ab
x0 1
xn xn 2xn
2 n 2 n 2 * 2 n 2 n 1
( xy ) a x a y a
1 31
m
x m
x
Summation Review
N
x
i 1
i
The sum of numbers from 1 to N; e.g 1 + 2 + 3 … + N
i
x 2
i 1
Suppose our list has 5 number, and they are 1, 3, 2, 5, 6.
Then resulting summation will be 12 + 32 + 22 + 52 + 62 = 75
y N
a ( y x 1)a
ix
or a Na
i 1
The First constant Rule
6
2
i 0
14
N N
ax i a xi N
The Second constant Rule
N
6x y 6 y xi
i 1 i 1
i 32
i 1 i 1
N N N
(x
i 1
i y i ) xi y i
i 1 i 1
The Distributive Rule
Series Review
n 1
1 x
1 x x x
2 n for x 1
1 x
1
1 x x
2
for |x| < 1 33
1 x
The End
Questions?
34