SPACE COMPLEXITY & TIME COMPLEXITY

36
SPACE COMPLEXITY & TIME COMPLEXITY 1

description

SPACE COMPLEXITY & TIME COMPLEXITY. ALGORITHMS COMPLEXITY. What Is An Algorithm??. An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Analysis of Algorithms or Performance Analysis. - PowerPoint PPT Presentation

Transcript of SPACE COMPLEXITY & TIME COMPLEXITY

Page 1: SPACE COMPLEXITY & TIME COMPLEXITY

SPACE COMPLEXITY &

TIME COMPLEXITY

1

Page 2: SPACE COMPLEXITY & TIME COMPLEXITY

ALGORITHMS COMPLEXITY

2

Page 3: SPACE COMPLEXITY & TIME COMPLEXITY

WHAT IS AN ALGORITHM??

An algorithm is a step-by-step procedure for solving a problem in a finite amount of time.

3

Page 4: SPACE COMPLEXITY & TIME COMPLEXITY

ANALYSIS OF ALGORITHMS OR

PERFORMANCE ANALYSIS

Program performance is the amount of computer memory and time needed to run a program.

4

Page 5: SPACE COMPLEXITY & TIME COMPLEXITY

CRITERIA FOR MEASUREMENT

Two criteria are used to judge algorithms: (i)time complexity (ii)space complexity.

Space Complexity of an algorithm is the amount of memory it needs to run to completion.

Time Complexity of an algorithm is the amount of CPU time it needs to run to completion.

5

Page 6: SPACE COMPLEXITY & TIME COMPLEXITY

SPACE COMPLEXITY Memory space S(P) needed by a

program P, consists of two components:

A fixed part: needed for instruction space (byte code), simple variable space, constants space etc. c

A variable part: dependent on a particular instance of input and output data. Sp(instance)

S(P) = c + Sp(instance)

6

Page 7: SPACE COMPLEXITY & TIME COMPLEXITY

TIME COMPLEXITY

Time required T(P) to run a program P also consists of two components:

A fixed part: compile time which is independent of the problem instance c.

A variable part: run time which depends on the problem instance tp(instance)

T(P) = c + tp(instance)

7

Page 8: SPACE COMPLEXITY & TIME COMPLEXITY

SPACE COMPLEXITY

8

Page 9: SPACE COMPLEXITY & TIME COMPLEXITY

SPACE COMPLEXITY OF SIMPLE

ARITHMETIC FUNCTION

9

Page 10: SPACE COMPLEXITY & TIME COMPLEXITY

int fun(int p, int q, int r){return (p+q)/r*8+(p*r)*(p+q+r)-

(p*q*r)+89;}o Above function which is named as

fun take p,q,r as an argument or input and return a output of a some simple arithmetic operations.

o According to the classification given, this function has only fixed space requirements. 10

Page 11: SPACE COMPLEXITY & TIME COMPLEXITY

o Therefore,

[S fun(I) is a variable space requirement]

o Therefore, space complexity of simple arithmetic function is only equal to fixed space requirement.

S fun(I)=0

11

Page 12: SPACE COMPLEXITY & TIME COMPLEXITY

SPACE COMPLEXITY OF ITERATIVE FUNCTION

12

Page 13: SPACE COMPLEXITY & TIME COMPLEXITY

int sum(int array[ ],int n){ int i , temp=0; for (i=0 ; i<n ; i++) { temp=temp + array[i]; } return temp;}

13

Page 14: SPACE COMPLEXITY & TIME COMPLEXITY

o In above function, we want to add a numbers although the output is simple but for input values we need a array.

o Therefore, variable space requirement depends upon the programming languages. Different programming languages have different array passing method :

o 1.Pascal like programming languages pass array by value . So in Pascal language, variable space requirement is,

14

Page 15: SPACE COMPLEXITY & TIME COMPLEXITY

where n is the size of an array.2. In C language, it passes array by it’s

base address(address of first element). C does not copy the array. Therefore,

where n is size of array.

S sum(I)=S sum(n)

S sum(n)=0

15

Page 16: SPACE COMPLEXITY & TIME COMPLEXITY

SPACE COMPLEXITY OF RECURSIVE FUNCTION

16

Page 17: SPACE COMPLEXITY & TIME COMPLEXITY

Following function also adds a list of number but in that particular function summation is handled recursively.

This means that compiler must save the parameter, local variable and return address for recursive call.

17

Page 18: SPACE COMPLEXITY & TIME COMPLEXITY

/* Recursive function for summing list of number */

float rsum (float list [ ], int n){ if(n) return rsum (list , n-1 )+ list[n-1]; return 0; }

18

Page 19: SPACE COMPLEXITY & TIME COMPLEXITY

Space for given example needed for one recursive call is number of bytes required for parameter and return address.

We can find the sizeof operator to find the number of bytes by each type.

Following table show the number of bytes required for one recursive call

19

Page 20: SPACE COMPLEXITY & TIME COMPLEXITY

TYPE NAME BYTES

Parameter : array pointer

List [ ] 4

Parameter : integer n 4

Return address 4

TOTAL 12

20

Page 21: SPACE COMPLEXITY & TIME COMPLEXITY

TIME COMPLEXITY

21

Page 22: SPACE COMPLEXITY & TIME COMPLEXITY

TIME COMPLEXITY OF ITERATIVE FUNCTION

22

Page 23: SPACE COMPLEXITY & TIME COMPLEXITY

The time T(P) taken by a program P, is the sum of its compile time and its run time.

The compile time is similar to the fixed space component since it does not depend upon instance characteristics.

The program execution time is denoted by Tp.

To determine Tp we require a detailed knowledge of compiler’s attributes. That is we must know how the compiler translates our source program into object code.

23

Page 24: SPACE COMPLEXITY & TIME COMPLEXITY

Here for ex. we have a simple program that adds and subtracts numbers. Let n denote the instance characteristics, then

Tp(n)=Ca ADD(n)+Cs SUB(n)+Cl LDA(n)+Cst STA (n)

where, Ca, Cs, Cl, Cst are the constants that

refer the time needed to perform each operation and ADD, SUB, LDA, STA are the number of operations performed when program is run with instance characteristics n.

24

Page 25: SPACE COMPLEXITY & TIME COMPLEXITY

A program is syntactically or semantically meaningful program segment whose execution time is independent of instance characteristics.

Here, we want to obtain step count for sum function in iterative function. We have to count only executable statements. Following code shows where to place the count statements

float sum(float list[ ], int n) { float tempsum =0; count++; /*f or assignment*/

25

Page 26: SPACE COMPLEXITY & TIME COMPLEXITY

int i; for(i=0; i<n; i++) { count++; /*For for loop*/ tempsum+= list[ ]; count++; /*for assignment*/ } count++; /*last execution*/ count++; /* for return */ return tempsum; }

26

Page 27: SPACE COMPLEXITY & TIME COMPLEXITY

In above code, count variable is initialized to 0 initially. Then its final value will be,

2n+3. So, each invocation of sum executes a

total of 2n+3 steps. Now, we will construct a table, which is

step count table. We will first enter steps for statement. Next figure out the frequency and then the total steps and then final step count.

27

Page 28: SPACE COMPLEXITY & TIME COMPLEXITY

STATEMENTS S/E FREQUENCY

TOTAL STEPS

float sum(float list[ ],int n){ float tempsum=0; int i; for(i=0; i<n; i++) tempsum+=list[i]; return tempsum;}

00101110

0010

n+1n10

0010

n+1n10

TOTAL 2n+328

Page 29: SPACE COMPLEXITY & TIME COMPLEXITY

TIME COMPLEXITY OF

RECURSIVE SUMMING A

LIST OF NUMBERS

29

Page 30: SPACE COMPLEXITY & TIME COMPLEXITY

/* recursive function without count statements */

float rsum (float list [ ] ,int n) { if (n) return rsum (list,n-1)+ list [n-1] ; return 0 ; }

30

Page 31: SPACE COMPLEXITY & TIME COMPLEXITY

/* recursive function with count statements added*/

float rsum (float list [ ] ,int n) { count++ ; // for if conditional if (n) count++ ; // for return & rsum invocation return rsum (list,n-1) + list [n-1] ; } count++ ; return list [0] ;}

31

Page 32: SPACE COMPLEXITY & TIME COMPLEXITY

HOW TO DETERMINE STEP COUNT FOR THIS FUNCTION :

For the boundary condition of n=0 :- Only if conditional statement

& second return statement are executed .

So the total step count for n=0 is 2. For n>0 :- The if conditional statement

& the first return statement are executed .

So each recursive call with n>0 adds two to the step count.

32

Page 33: SPACE COMPLEXITY & TIME COMPLEXITY

So the step count for the function is

With the help of the following table we can also prove the recursive summing function .

2n+2

33

Page 34: SPACE COMPLEXITY & TIME COMPLEXITY

STATEMENTS S/E FREQUENC

Y

TOTAL STEPS

float rsum (float list [ ] ,int n) { if (n) return rsum (list,n-1)+ list [n-1] ; return 0 ; }

001110

00

n+1n10

00

n+1n10

TOTAL - - 2n+2

34

Page 35: SPACE COMPLEXITY & TIME COMPLEXITY

The recursive function has lower step count then its iterative counterpart.

Recursive function typically run slower than the iterative version & takes more time than those of the iterative function.

Recursive function also uses more memory space for its each call.

For space complexity, iterative function is better than recursive function.

For time complexity , recursive function is better than iterative function.

conclusion

35

Page 36: SPACE COMPLEXITY & TIME COMPLEXITY

THANK YOU !!!

36