Lecture 1 (C)
Lecture 1 (C)
FALL 2024
Lecture No:01(C)
Time Complexity of Algorithm and its
importance
...
– a write-only output tape Memory
Each memory register can hold an
output tape
arbitrary integer (*). . . .
Each tape cell can hold a single symbol
from a finite alphabet s. Instruction set:
x y, x y {+, -, *, div, mod
goto label
if y {<, , =, ,> , } z goto
x input, output y
3 halt
Space Complexity
The amount of memory required by an
algorithm to run to completion
– The term memory leaks refer to the
amount of memory required is larger
than the memory available on a given
system
4
Space Complexity (Cont !!!)
5
Space Complexity (cont’d)
S(P) = c + S(instance characteristics)
– c = constant
Example:
void float sum (float* a, int n)
{
float s = 0;
for(int i = 0; i<n; i++) {
s+ = a[i];
}
return s;
}
Constant Space:
one for n, one for a [passed by reference!], one for s, one for
i , constant space=c=4
6
Running Time of Algorithms
Running time
– depends on input size n
size of an array
polynomial degree
# of elements in a matrix
# of bits in the binary representation of the input
vertices and edges in a graph
7
Steps To determine Time
Complexity
11
Assignment Statement
Following an object No 0
reference
Returning from a method Yes 1
18
Primitive Operations -
Example 2
Count the number of primitive operations in
this program :
Primitive Yes/ Total
Int method() Operations No
{ i = 0; Assign a value to a
variable
a = 0;
Call a method
for (i=1;i<=n;i++) Arithmetic
{ printf(“%d”, i); operation
Algorithm ArrayMax(A,n)
{An array A storing N integers
and find the largest element in A.}
22
Run Time Analysis (Cont !!!)
Run Time Complexity
int method()
will be = c1+c2+c2 + c3
{
int a,b,large; --------- c1
+c2 + c2 + c4
a=2; --------- c2 = c1 + 4c2 + c3 +c4 (as
b=3; --------- c2 all constants)
if(a>b) -------- c3 =C
large=a; -------- c2 Note:- =You can say
else there should be 3c2
large = b; ------- c2 instead of 4c2
printf(“Large number is %d”, large);
(Coefficient have no
Return 0; ---------- c4 impact)
}
23
Run Time Analysis (Cont !!!)
int method()
{
int I, codd, ceven, a[5]={5,4,3,2,1}; --------- c1
ceven=0;codd=0; ----------- c2
Run Time Complexity
for(i=0;i<=4;i++) ------ c3
if(a[i]%2==0) -------- c4
will be = c1+c2 + n*
ceven++;-------- c5 (c3 +c4 + c5)+ c6
else = c1 + c2 + n*c +c6
codd++; ------- c5 = n*c + c
= nOdd %d”, ceven,
printf(“Total evens %d and Total
codd);
Return 0; ---------- c6
}
24
Run Time Analysis (Cont !!!)
int method()
{ Run Time Complexity
int I, a[5]; --------- c1 will be = c1 + n * c2 +
for(i=0;i<=4;i++) ------n*(c1+c3)
c2 + n * c2 +
Scanf(“%d”, &a[i]); c4
for(i=0;i<=4;i++) = c1 + 2n*c2 + n * c
A[i]=a[i]+5; -----------=c3c1 + n * c + n * c
for(i=0;i<=4;i++) = 2n*c +c1
print(“%d”, a[i]); = 2n
return 0; ---------- c4 =n
}
25
Types of Algorithm
Complexity
26
Types of Algorithm Complexity
(Example: Linear Search)
5 7 2 6 9 12 1
Worst Case Complexity:
– You want to search 1 in above array which is at location
N
– You need N steps
Best Case Complexity:
– You want to search 5 in above array which is at location
1
– You need 1 steps
Average Case Complexity:
– You want to search 2 or 9 etc in above array
27 – You need 3 steps for 2 and 5 steps for 9
Best, Worst, and Average
Case Complexity
Average Case
Complexity
Best Case
Complexity
N
(input
size)
28
Relationship between
complexity types and running
time of Algorithms
Worst case
– Provides an upper bound on running time
– An absolute guarantee that the algorithm would not run
longer, no matter what the inputs are
Best case
– Provides a lower bound on running time
– Input is the one for which the algorithm runs the fastest
Running Time
Average case time is often 80
difficult to determine. 60
We focus on the worst 40
case running time.
20
– Easier to analyze
0
– Crucial to applications 1000 2000 3000 4000
such as games, finance Input Size
and robotics
30
Summary