Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
25 views34 pages

Lect 02 CSE1201 Introduction

Uploaded by

merajshafi2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views34 pages

Lect 02 CSE1201 Introduction

Uploaded by

merajshafi2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

CSE 1201

Data Structure
Week-1
Lecture 02
Instructor:
Md. Nazrul Islam Mondal
Dept of CSE, RUET
Mathematical Notation
Mathematical Notation
Mathematical Notation
Algorithm Notation

Algorithm & Flowchart


Algorithm Notation

Algorithm & Flowchart


Algorithm Complexities
Types of Complexities
1. Best Case: the minimum value of c(n) for any possible input.
For linear search, best case c(n)=1
2. Worse Case: the maximum value of c(n) for any possible input.
For linear search, worse case c(n)=n
3. Average Case: the average value of c(n) for any possible input.
For linear search, average case c(n)=(n+1)/2
Algorithm Complexities
Big O notation

Time taken for a particular program is different in


different computers. So time complexity should be
defined mathematically which is known as Big O().
Algorithm Complexities
Big O notation…. contd

c.g(n)

let
f(n)=2n+3 and g(n)=n
Now 2n+3<=c.n, it is true for
c=5 and n=1
So upper limit of f(n)=O(n)
n0

Example Example
Let f(n)=2n2+5 Let f(n)=5n5+3n2+9
Then Time complexity=O(n2) Then Time complexity=O(n5)
Algorithm Complexities
Big  notation

let c.g(n)
f(n)=2n+3 and g(n)=n
Now 2n+3>=c.n, it is true for c=1 and n=1
So lower limit of f(n)=(n)
n0
Example
Let f(n)=90n2+5n+6
Here f(n)>80n2 so, f(n)= (n2)
Algorithm Complexities
Big  notation…. contd

let
f(n)=2n+3 and g(n)=n
Now 1.n<=2n+3<=5.n, it is true for c1=1, c2=5
and n=1
So average case of f(n)=(n) n0
Algorithm Complexities
Example: Linear Search

Here
Time Complexity =  (1) [best case]
= O(n) [worst case]
= (n+1/2) =(n) [average case
Analyzing Algorithms
We often ask for a algorithm:
– How much time does it take to finish
(running time)  Time complexity
– How much space does it take (memory
use)  Space complexity

int a[10]={1,2,3,
……,10};
Time(s)
int sum(int a[], int
n){
int sum=0;
for(int i=0;i<n;i++)
sum=sum+a[i]; n
Analyzing Algorithms
Ex 0: Find the complexity of the following function?

int a[10][10];
T=O(1)+O(1)=c1+c2
int sum(int a[])
=c3=1xc3
{
Complexity= O(1)
int sum=0; O(1)
printf(“sum=%d”,sum) O(1)
}
Analyzing Algorithms
Ex 1: Find the complexity of the following function?

int a[10];
T=O(1)+nO(1)+O(1)
int sum(int a[],n)
=c1+nc2+c3
{
=c4+nc2
int sum=0; O(1)
=nc2
for(i=0;i<n;i++)
O(1) Complexity= O(n)
sum=sum+a[i];
printf(“sum=%d”,sum) O(1)
}
Analyzing Algorithms
Ex 2. Find the complexity of the following function?

void A(n){
s = 1 3 6 10 ….
int i=0, s=0;
i= 1 2 3 4…
while(s<=n){
Suppose i=k when s>n
i=i+1;  k(k+1)/2 > n
s=s+i;  k2+k > 2n
printf(“%d\n”,s);  k=O(n)
}
Complexity= O(n)
}
Analyzing Algorithms
Ex 3 Find the complexity of the following function?

void A(n){
int i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
for(k=1;k<=100;k++)
printf(“CSE\n”);
}
Analyzing Algorithms
Ex 4. Find the complexity of the following function?

void A(n){ When i=1


int i,j,k; Then j executes 1 time
for(i=1;i<=n;i++) And k executes 100 times
for(j=1;j<=i;j++)
for(k=1;k<=100;k++) When i=2
printf(“CSE\n”); Then j executes 2 times
} And k executes 2*100 times

When i=3
Then j executes 3 times
Total=100+2*100+3*100+…+n*100 And k executes 3*100 times
=100 (1+2+3+……..+n)
=100 [n(n+1)/2] When i=n
=O(n2) Then j executes n times
And k executes n*100 times
Analyzing Algorithms
Ex 5. Find the complexity of the following function?

void A(n){
int i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=i2;j++)
for(k=1;k<=n/2;k++)
printf(“CSE\n”);
}
Analyzing Algorithms
Ex 5. Find the complexity of the following function?

void A(n){ When i=1


int i,j,k; Then j executes 1 time
for(i=1;i<=n;i++) And k executes n/2*1 times
for(j=1;j<=i2;j++)
for(k=1;k<=n/2;k++) When i=2
printf(“CSE\n”); Then j executes 4 times
} And k executes n/2*4 times

When i=3
Then j executes 9 times
Total = n/2 (1+4+9+ …..+ n2 ) And k executes n/2*9 times
=n/2 [n(n+1)(2n+1)/6]
=O( n4 ) When i=n
Then j executes n2 times
And k executes n/2*n2 times
Analyzing Algorithms
Ex 6. Find the complexity of the following function?

void A(n){
int i;
for(i=1;i<n;i=i*2)
printf(“CSE\n”);
}
Analyzing Algorithms
Ex 6. Find the complexity of the following function?

void A(n){ i = 1 2 4 8 ….. N


int i; = 20 21 23 24 …. 2k
for(i=1;i<n;i=i*2)
printf(“CSE\n”);  2k = n
}  k = log2(n)

 Complexity = O(log2n)
Analyzing Algorithms
Ex 7. Find the complexity of the following function?

void A(n){
int i,j,k;
for(i=n/2;i<=n;i++)
for(j=1;j<=n/2;j++)
for(k=1;k<=n;k=k*2)
printf(“CSE\n”);
}
Analyzing Algorithms
Ex 7. Find the complexity of the following function?

void A(n){ i loops executes n/2 times


int i,j,k; j loop executes n/2 times
for(i=n/2;i<=n;i++) k loop executes log2n
for(j=1;j<=n/2;j++) times
for(k=1;k<=n;k=k*2) Total = n/2 * n/2 * log2n
printf(“CSE\n”);
= O( n2log2n )
}
Algorithm Complexities
Subalgorithm
Q1: A(n) _____ has
different properties

A Field
B Entity
C File
D Record ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q2: Which in NOT a
primitive data structure?

A Boolean
B Arrays
C Integer
D Character ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q2: Which is a non-linear
data structure?

A Arrays
B List
C Stack
D Graph ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q4: Which operation accesses
each record exactly once so
that contain item may be
processed?
A Inserting
B Deleting
C Searching
D Traversing ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q5: _____ involves
arranging the records in a
logical order

A Merging
B Sorting
C Searching
D Traversing ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q6: Complexity is a
function of ______

A Input data size


B Output data size
Amount of time
C taken
D Total space taken ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q7: Which symbol is used
for decision making?

A
B
C
D ST
52
30
8
3
1
16
14
18
9
23
11
19
7
27
25
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP
Q8: Identify the
device?

A HDD
B SDD
C SSD
D RAM ST
2
8
3
1
16
14
5
18
9
23
11
19
7
27
25
30
22
15
10
4
28
26
12
17
21
20
6
24
29
13
OP

You might also like