0% found this document useful (0 votes)
20 views11 pages

M01S05 Daa-Unit-1-Time Space Tradeoff

Uploaded by

androicons
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)
20 views11 pages

M01S05 Daa-Unit-1-Time Space Tradeoff

Uploaded by

androicons
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/ 11

MODULE 01: SESSION 05 19CS403 - DESIGN AND ANALYSIS OF ALGORITHMS

Module 1

Session 1 Characteristics of algorithm


Session 2 Analysis of algorithm - Time and Space
Session 3 Asymptotic analysis of complexity bounds
Session 4 Best, average and worst-case behaviour
Performance measurements of Algorithm: Time and
Session 5
space trade-off
Analysis of recursive algorithms through recurrence
Session 6
relations - Substitution method
Analysis of recursive algorithms through Recursion tree
Session 7
method and Masters’ theorem

TIME SPACE TRADEOFF 1


MODULE 01: SESSION 05 19CS403 - DESIGN AND ANALYSIS OF ALGORITHMS

SESSION 5: Performance measurements of Algorithm: Time


and space trade-off

TIME SPACE TRADEOFF 2


MODULE 01: SESSION 05 19CS403 - DESIGN AND ANALYSIS OF ALGORITHMS

Time & Space Complexity - Time Space Tradeoff

Calculating Time & Space Complexities - Time Space Tradeoff

Practically, time and space complexity can be reduced only to certain

levels, as reduction of time increases the space and vice-versa. This is

known as Time-Space trade-off.

Video Link

TIME SPACE TRADEOFF 3


MODULE 01: SESSION 05 19CS403 - DESIGN AND ANALYSIS OF ALGORITHMS

Time & Space Complexity - Time Space Tradeoff

Example: Reversing an Array


First Method

• Take two arrays, one for the input and the other for the output.
• Read the elements of the first array in reverse linear order and place them in
the second array linearly from the beginning.

Second Method
• Swap the first and the last elements, then swap the next two immediate
elements one from each end, and so on. This process is repeated until all the
elements of the array get swapped.
• No extra array is used.

TIME SPACE TRADEOFF 4


MODULE 01: SESSION 05 19CS403 - DESIGN AND ANALYSIS OF ALGORITHMS

Time & Space Complexity - Time Space Tradeoff

First Method

• An extra array of size ‘n’ is used int reverse(int ary1[ ], int n)


• The values of ary1 are assigned
{
to ary2 in reverse order
int ary2[];
• Total space requirement for the
for i=0 to n-1
first algorithm is ‘2n’
• Time complexity - based on the ary2[i]=ary1[(n-1) - i];

number of assignment statements }

TIME SPACE TRADEOFF 5


MODULE 01: SESSION 05 19CS403 - DESIGN AND ANALYSIS OF ALGORITHMS

Time & Space Complexity - Time Space Tradeoff

Second Method
int reverse (int ary1[ ], int n)
• Only one array of size ‘n’ and a {
temporary variable ‘temp’, inside int k = floor(n/2);
for i=0 to k-1
the swap function is used.
swap (ary1[i], ary1[(n-1) – i )];
• Total space - n+1 }
• For each swapping - 3 swap (int a, int b)
{
assignments are required
int temp = a;
• Number of swapping - Atmost a = b;
half-time the size of the array b = temp;
• Time complexity - 3n/2 }

TIME SPACE TRADEOFF 6


MODULE 01: SESSION 05 19CS403 - DESIGN AND ANALYSIS OF ALGORITHMS

0 1 2 3 4
5 6 7 8 9

0 1 2 3 4
9 8 7 6 5

i=0, ary2[0]=ary1[(4) - 0], ary2[0]=ary1[4]


i=1, ary2[1]=ary1[(4) - 1], ary2[1]=ary1[3]
i=2, ary2[2]=ary1[(4) - 2], ary2[2]=ary1[2]
int reverse(int ary1[ ], int n)
i=3, ary2[3]=ary1[(4) - 3], ary2[3]=ary1[1]
i=4, ary2[4]=ary1[(4) - 4], ary2[4]=ary1[0]
{

int ary2[];

for i=0 to n-1

ary2[i]=ary1[(n-1) - i];

TIME SPACE TRADEOFF 7


MODULE 01: SESSION 05 19CS403 - DESIGN AND ANALYSIS OF ALGORITHMS

0 1 2 3 4
5 6 7 8 9

i=0, swap(ary1[0], ary1[(4) – 0]), swap(ary1[0], ary1[4])


int reverse (int ary1[ ], int n)
i=1, swap(ary1[1], ary1[(4) – 1]), swap (ary1[1], ary1[3])
{
Swap(a,b) swap(5,9)
int k = floor(n/2); //2
Temp=5
for i=0 to k-1 //(for i=0;i<=k-1;i++)
a=9
swap (ary1[i], ary1[(n-1) – i]);
b=5
}
swap (int a, int b)
{
int temp = a;
a = b;
b = temp;
}

TIME SPACE TRADEOFF 8


MODULE 01: SESSION 05 19CS403 - DESIGN AND ANALYSIS OF ALGORITHMS

Time & Space Complexity - Time Space Tradeoff

 In both the methods any attempt in reducing space leads to an


increase in the time taken by the algorithm and vice versa.
 The first method involved more space with lesser time while the
second approach reduced space considerably but with increase in
time. This is an example of time-space trade off.

Method Time Space


1 n 2n
2 3. n+1

TIME SPACE TRADEOFF 9


MODULE 01: SESSION 05 19CS403 - DESIGN AND ANALYSIS OF ALGORITHMS

Time & Space Complexity - Time Space Tradeoff

Try yourself

1. Write algorithms for the following and analyze the space taken by each of the
algorithms. Show that the concept of Time-Space tradeoff holds. Is it possible to
reduce the amount of memory and increase the speed of an algorithm
simultaneously?
 Find the leaders in the given set of elements (the element which is larger
than the elements ahead of it
 Remove duplicates in a list of elements
 Sort the elements given in the list
 Reversing a list of elements
 Find the number of occurrences of an element
 Produce a sorted list from 2 unsorted lists
2. List the functions in the increasing order of asymptotic complexity. n^2, 2^n ,
nlogn, n^3

TIME SPACE TRADEOFF 10


MODULE 01: SESSION 05 19CS403 - DESIGN AND ANALYSIS OF ALGORITHMS

SESSION 6: Analysis of Recursive Algorithms through


Recurrence Relations - Substitution Method

TIME SPACE TRADEOFF 11

You might also like