0% found this document useful (0 votes)
9 views19 pages

WhatsApp Documents - FDS Unit 2 Notes

The document discusses linear data structures using sequential organization, focusing on arrays and their implementation in programming languages like C and Python. It covers definitions, types, operations such as insertion, deletion, and merging of arrays, as well as concepts related to multi-dimensional arrays and ordered lists. Additionally, it provides examples of Python code for various array operations, including traversing, merging, and matrix manipulations.

Uploaded by

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

WhatsApp Documents - FDS Unit 2 Notes

The document discusses linear data structures using sequential organization, focusing on arrays and their implementation in programming languages like C and Python. It covers definitions, types, operations such as insertion, deletion, and merging of arrays, as well as concepts related to multi-dimensional arrays and ordered lists. Additionally, it provides examples of Python code for various array operations, including traversing, merging, and matrix manipulations.

Uploaded by

Thakur Venkatesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit II

Linear Data Structure Using Sequential Organization


Concept of Sequential Organization
In computer science, sequential access means that a group of elements (such as data in
a memory array or a disk file or on magnetic tape data storage) is
accessed in a
predetermined, ordered sequence.
Sequential access is sometimes the only way of accessing the data, for example if it is
on atape.

1 8

Fig- Sequential Access


Overview of Array
Array is referred as the Sequential Organization that means the data in arrays is stored
in some Sequence.
If we want to store names of all the students in a class we can make use o a array to

store the names in sequential form.

Definition of an array
An array is a set of consecutive memory location with contains similar data elements.
Or

Array is a collection of similar type of elements


An array as a data structure is defined as a set of pairs ( index, value) such that with
each index a value is associated.
index - indicates the location of an element in an array.
value - indicates the actual value of that data element.
Declaration of an array in 'C+': data types name_of array[size]
Example int a[20]:
Here 'a' is the name of the array inside the square bracket size of the array is given
This array is of integer type in array 'a'.
The Array can be one dimension, two dimensions or multi- dimensional.

One dimension
The one dimensional array 'a' is declared as int ai0].
Value storeI Ín the a r r a

40

1
Index used to nd e l e e n t

Syntax data types name_of arrayl size]


Example- int a<20]:
array _size][column_size] deletion elements
array
dimensional arrayn. element
logical the position
the shift
&operations. an S0
ADT. of
two array[row in
elements array
array to Inserting
The .a[3](3]....[3] have 3rd
a[3][3]....[31; an
...[sizen]
l][size2] array.
the the
a[3][3]. writing we after S0
at 40
name_of
a[3]14): instances of of
the from elements 3$
because
30 60
while number in elements means
int 2
int elements 3
40 35
New
element
as
declared data_types as of DataType
total the
activity
That 30
declared help 30
20 S0 array[size array. deletingdisplay
inserting element.
2
int 1
int with & complex
is is Abstract ,index an
written 20
20
|10
'a' Example 'a' Type create for forpossible.
Example for new 1
array arrayof size is is
form.
10 40
name Data word is is operation
operation
operation array 10
dimensional
is
Multi-dimensional Type of operation
iselement
of array for
column column reservedaarray an space
1 2
_types Abstract
Data AbstractDataType
Array in in
Insetion
Before Insertion
Affer
()-This Operations
on
Array a one.
ThisThis
An ()-This elements
element
create
dimensional
Two -
'0W"
Multi- data
dimensional Abstract
the
two row - ()-- by
Instances ) to increase
The in an of Create (
Insert Display
Delete of an order
be The as The use Operations Insert
Insertion
should make
Syntax Syntax Array in gets
We ahead
size
1.
We can implement array using
python program. Python doesn't support the
of array but we can concept
implement the array using lists. List is a sequence of values.
String is also a sequence of values but in string
this sequence is of characters. On
in case of list the values can be
of any type.
other-hand,
The values in the list are called
elements or items. These elements are separated by
comma & enclosed with square brackets.
Example - a=[10,20,30,40] #list of integers
B=["aaa","bbb","ccc'']#list of strings
Python Program
n=int(input("Enter how many elements youwant insert")
a-[]
i=0

for iin range(n):


item=int(input("Enter element in array"))
a.append(item)
possition-int(input("Enter loation where you want to insert an elements"))
value=int(input("Enter value to insert")
a-a[:possition]+[value]ta[possition:]
print("Resultant array is =",a)

2.Traversing List
The loop is used in list for traversing purpose. The for loop is used to traverse the list
element. Syntax - for variable in List:
Body
Example- a-['a, b'c'd','e]
for i in a:
print(i)
We can traverse the list using range() function. We can access each element of the list
using index of list.
If we want to increment each element of the list by one then we must pass index as
argument to for loop. This can be done using range() function as follows.
a-[10,20,30,40]
for iin range(len(a):
a[i] = a[i]+1
print(a) # (11,21,31,41]
3.Deleting an elements from array
hecause have to shift the
Dcleting an clemcnts fromn array is complex activity
an clement sizc gcts decremented by
clements to previous position. That mcans after Deleting
2
Onc.
10 20 30 35 40

Element at position 4 is deleted


20 30 40 S0
10

n=int(input("Enter how many elements you want insert"))


a=[)
i=0
for iin range(n):
item=int(input("Enter element in array"))
a.append(item)
pos=int(input("Enter location where you want to delete an elements"))
a-a[:pos]ta[pos+1:]
print("Resultant array is ==>",a)

4. Merging of two arrays


arranged in
Merging of two arrays results into a single array in which elements are
sorted order.

al-)
n=int(input("Enter how many elements you want insert in first array"))
for i inrange(0,n):
item=int(input("Enter element in array")
al.append(item)

a2=[]
m=int(input("Enter how many elements you want insert in second array"))
for i in range(0,m):
item=int(input("Enter element in array")
a2.append(item)

def mergeArray(al,a2,n,m):
a3-[None]*(n+m)
i-0
j-0
k=0
while i<n and j<m:
if al[i}<a2[iI:
a3[k]-al[i]
k=k+|
i=itl
else:
a3[k]-a2[i]
k=k+|

while i<n:
a3[k]=al[ij
k=k+l
i=itl
while /<n:
a3{k]-a2|il
k-k+|

print("Merge array")
for iin range(n tm):
print(str(a3{i),cnd =" ")
mergeArray(al,a2,n,m)

Storage Representation and their Address Calculation


The array can be represented using
i)Row major Representation ii) Column Major Representation

i) Row major Representation


called as Row major
If the elements are stored in row wise manner then it is
Representation.
dimensional array.
Example-if you want to store elements 10 20 30 40 50 60 then in two
1 2
10 20 3
40 50

elements in two dimensional


The elements will be stored horizontally. To access any
number. That is why we need two
array we must specify both its row number & column
index.
variables which is act as row index & column

In row major matrix, the element at a[O will be equal to


no.of column tcolumn index)* element-size
Address of [illil= base address + row index * total

declared in program. If the base address


Example- Consider integer array int arr[3][4]
arr[2][3]with row major Representation.
is 1050, find the address of the element
column tcolumn index)* element-size
base address + row index * total no.of
*2=1050+11*2=1072
a[2][3] =1050+(2*4+3)*2 = 1050+(8+3)
i) Column Major Representation Column Major
wise manner then it is called as
If elements are stored in column
Representation. will be tilled
want to store elements 10 20 30 40 5060 then the elements
Example - if you
follows
up by column wise manner as 40

element at ali]|li] will be equal to


InColumn major matrix, the index)* element-size
index * total no.of row trow
Column
Address of [i]l[i] = base address +
txample- Consider integer array int arri3114]declared in program. If the base address
S T0s0, find the address of the element arri213| with Column major Representation.
base address +Column index *total no.of row +row_index) *element-size
a(2][3]=10S0+(3*3+2)*2= 1050+(9+2)*2-1050+1 1*2=1072
Multidimensional Arrays
Multidimensional Array is an array having more than one dimension.
Popularlytwo & three dimensional array are used.
The Multi-dimensional array 'a' is declared as int a[3][3]...3]·
Syntax data types name_ of _array[size1][size2]....sizen]
Example- int a[3|[3]....[3];
Two-dimensional arrays
The Two-dimensional array is used to represent the matrix
row=int(input("Enter no. of row for matrix"))
col=int(input("Enter no. of column for matrix")
A=[[0 for iin range(0,row)] forj in
print("Enter element of matrix") range(0,col)]
for i in
for jrange(0,row):
in range(0,col):
ele=int(input("Enter Elemets for matrix")
A[i][]-ele
print("Matrix A=> ",A)

Program for performing addition of two matrix


def Add():
C=[[0 for iin range(0,row)] for j in
for iin range(0,col)]
for jrange(0,row):
in range(0,col):
C[J] = A[J0] +B[JGI
print("Matrix Addition==> ".C)
row=int(input("Enter no. of row for matrix")
col=int(input("Enter no. of column for matrix")
A-[[0 for iin range(0,row)] for jin
print("Enter element of matrix") range(0,col)]
for i in range(0,row):
forj inrange(0,col):
A[ili]= int(input('nEnter element
print("Matrix A==>",A) A{}):format(i, j)
row=int(input("Enter no. of row for matrix")
col=int(input("Enter no.of column for matrix")
B={[0 fori in range(0,row)] forjin
print("Enter element of matrix") range(0,col)]
for iin range(0,row):
forjin range(0,col):
B[ili]= int(input(nEnter element
print("Matrix B==>",B) A(}:.format(i, i)))
Add()
Matrix Multiplication
def Mul():
C=[[0 for i in range(0,row)] for j in range(0,col)]
for iin range(0,row):
for jin range(0,q):
for k in range(0,col):
C[iJU]-C[iJU]+ A[J[k]* B[k][)
print("Matrix Addition==>",C)
row=int(input("Enter no. of row for matrix")
col=int(input("Enter no. of column for matrix")
A-[[0 for iin range(0,row)] forj in range(0,col)]
print("Enter element of matrix")
for iin range(0,row):
for jin range(0,col):
A[i][] = int(input(\nEnter element A{}):.format(i, j))
print("Matrix A=>",A)
p=int(input("Enter no. of row for matrix")
q-int(input("Enter no. of column for matrix")
B-{[0 for i in range(0,p)] for j in range(0.4)]
print("Enter element of matrix")
for i in range(0,p):
for j in range(0,4):
B[iJI] = int(input(nEnter element A(}:.format(i, i))
print("Matrix B==> ",B)
Mul()

Transpose of Matrix
def Transpose():
C=[[0 for i in range(0,row)] for j in range(0,col)]
for iin range(0,row):
forj in range(0,col):
C[iJ] = AjJ[]
print(" Transpose of Matrix==>".C)
row-int(input("Enter no. of row for matrix")
col=int(input("Enter no. of column for matrix")
A=[[0 for iin range(0,row)] for j in range(0,col)]
print("Enter element of matrix")
for iin range(0,row):
for j in range(0,col):
A[JÜ] = int(input(nEnter element A{}:.format(i, i))
print("Matrix A ==> ",A)
Transpose()
Concept of Ordered List
sometimes called as linear
Ordered List is nothing but a set of clements such as list
list. Example 1-list of one digit number [0,1.2,3,4,......9)!)
Example 2- Days ina week
"Saturday"]
T"Sunday", "Monday", "Tuesday"" Wednesday", "Thursday", "Friday.
Withthis concept in mind let us fomally define the ordered list.
Definition
Ordered List is set of elements where set may be emepty or it can be written as a

collection of elements such as la,, a, a3 a4, ....... anJ

Operation on Ordered List


1) Display aList 2) Searching a particular elements from the a List
3) Insertion of any element in a List 4) deleting of any elements from the a List
Ordered List can be implemented using list in python. Various operation on list are
possible using following method.
Ordered List Methods
1. append)
append method adds the element at the end of the list
Example 1
a=[10,20,30]
print("Original elements in the list", a)
a.append(40)
print("List after adding 40 Elements in the list", a)
Output- Original elements in the list [10, 20, 30]
List after adding 40 Elements in the list [ 10, 20,
30, 40]
Example 2
b=[AB.C]
print("Original elements in the list", b)
b.append(D')
print("List after adding 40 Elements in the
output- Original clements in the list ['A', 'B,list", b)
List after adding 40 Elements in the C]
list ['A, 'B. C, 'D')
2.extend
The extend function takes the
list as an argument &
oldlist appends this list at the end of the
a=|10,20,30|
print("Original clements in the list", a)
b-|'A"BC|
print("Original clenments in the list", b)
a.extend(b)
print("List atter extend function in the list", a)
output- Original elements in the list | 10, 20,
Original elements in the list |'A, 'B, C 301
List afier extend funetion in the list ||0, 20, 30, 'A: B,C

sort()
The sort method arrange elements in increasing order.
a-[40.50.10,20,80,90|
print("Original clemcnts in the list", a)
a.sort()
print("List after sort function", a)

output-Original elements in the list 140, 50, 10, 20. 80, 90]
List after sort function [10, 20, 40, 50, 80, 90]

Insert()
desired position in the list..
This insert method allows us to insert the element at
a-[10, 20, 40, 50, 60, 70]
print("Original elements in the list", a)
a.insert(2,30)
print("List after insert 30 at index 2 function", a)
40, 50, 60, 701
m" output-Original elements in the list [10, 20.
30, 40, S0, 60, 70] "nn
List after insert 30at index 2 function [10, 20.,

delete) like
is carried out using various function
The deletion of any element from the list
pop, remove, del.
function.. the
element to be deleted then just use pop
If we know the index of the
function.
index as an argunent to the pop
a=[10, 20, 40, 50, 60, 70]
print("Original clements in the list", a)
a.pop(2) function", a)
print("List after pop 30 from index 2
40, 50, 60, 70]
output-Original elements inthe list [10, 20, 20, 50, 60, 70]
function |10,
List after pop 30 from index 2
argument to pop function then last element of the list
If you do not provide any
will be deleted.
a-[10, 20, 40, 50, 60, 70]
print("Originalelements in the list", a)
a.pop() no argument ", a)
print("List after pop function with
20, 40, 50, 60, 701
output-Originalelements in the list |10,
argument [10, 20, 40, 50,60]
Listafter pop function with no
3 2#
# 50))
used. be a.index(50) ",a.copy()
to funetion a.count( True False
is is ",a) using by
function
that 70] 50] 70] # 70] # obtained
List "Sham","Ram""V
"Pankaj",
value del 60, ", 60, ", [10,20,30,40,50] a= List 60, all(a))
a= a [10,20,30,40,50] 60, all(b) list
remove using 50, 50 50, 50 of a 50, 50, ", new
actuat of
40, ofIndex40, Count of 40, ",print("returns
print("Reverse is
print("Copy0, print("returns defineelement
tine deleted 20, 20, a.reverse) a-[10,20, 20,
the the a-[10,
print(" b-[10,
a a=[10,print(" &
the is at be createcachsequence.
deletedfunction element will wherc
70| 70| the
70| passed to
way
elegant
clement 60, if True list anotlher
60, item
60,
be remove 50, 70) one ".a) 50. or
to than 40,
20., matched items true returns new "ishwar""Rahul""
name-
40, 60, index list
clement element
20, [10, of the list are a of
the a)
20, 40,
more numberin the list function an make member
to |10,20,
clement", a) 3
and
|10,clement first is list|
the passed a) 4 items of the comprehension
to
list", list |10, remove list", to list the copy useful each the
2 2 Methods of of this
of
valueparameter he the thei.c. from the4 of of
shallowelements
in
element 3 in to indeX count order then to item
in 50 in to 70| in indexindex 2
elements Built-in
Functions:
List argument
an
as comprehension
List many operation
the list.
701 elementsremoveclements possibleelements from List
Python
the the the empty for compression
without
know the
60, 50 60, 4-1 del index Returns ReturnsReverse Returns
a the List is
This
List|=expression
name
the from 50, remove 50, # after
print("Originaloutput-Original all is some
after output-Original
is list
40, print("Original
a.remove(50) it 40, del If list.
applying Example
we means rname-
in
print("Listafier python
20, del print("List
a[2:4]
If removed 20, after reverse() existing title
|10, a=[10, index() count) copy)
That List In List for
all()
a
if titlc.startswith(R'):
name.apend(itle)
print("Another List rom name "rname)

with compression
name=
["ishwar""Rahul"."Rohit".if "Ramesh" "Pankaj""Sham", "Ram"."Vitthal"|
rname-[ title for title in name
title.startswith(R') |
print("Another List from name ",rname)

Single Variable Polynomial


APolynomial p(x) is the expression in variable x which is in the form of
(ax" + bx to--+k) where a.b.c.....k fall in real
number & 'n' is non-negative integer,
which is called as degree of the polynomial.
An important characteristics of polynomial is that each term
in the polynomial
expression consist of two parts.
1. coefficient 2. exponent
Polynomial Definition
Polynomial is the sum of terms where each term consists of
variable ,coefficient and
exponent.
Asingle variable Polynomial can be written as
( a, x"+ an-1X+ an-2 X to---.tajx +ao) where a, #0and degree of A(x) is n

Representation of Single Variable Polynomial using arrays


For representing a single variable polynomial one can make use of one
dimensional
array.
In singledimensional array the index of an array will act as the exponent and the
coefficient can be stored at that particular index which can be represented as
follows:
For e.g. :3x*+ 5x° + 7x+10x- 19
o-19
10

Coeticients of
polvnomials

iundes which acts as exponent of respectii e coethcieut


Fig- Polynomial Representation
This polynomial can be stored in single dimensional array.
Polynomial Addition
Algorithm:
Assume that the two polynomials say Aand Band the resultant polynomial storing
the addition result is stored in one
large array.
1. Set a pointer ito point to the first term in a
2. Set a pointer jto point to first term in a
polynomial A.
3. Set a pointer k to point to the first polynomial B.
position in array C.
4. Read the number of terms of Apolynomial in variable tl and read the number of terms of B
polynomial in variable t2 &read the n.
5.While i<tland j<t2 then
if exponent at i" position of Apolynomial is equal to the exponent at jh position of
polynomial B then
Add the coeficients at that position from both the polynomial and store the result in C
arrays coefficient field at position kcopy the exponent either pointed by ior j at position k in
C
array.
Increment i, jandk to point to the next position in the array A, Band C.
else

if the exponent position i is greater than the exponent at position j in


then
polynomial B

copy the coefficient at position ifrom A polynomial into coefficient field at position k
of array C copy the exponent pointed by iinto the exponent field at
position k in array C.
Increment i, k pointers.
else

Copy the coefficient at position j of B polynomial into


coefficient field at position k in C
array.
Copy the exponent pointed by jinto exponent field at position k in C array.
Incrementjand k pointers to point to the next position in array B andC.

while i< t

copy the coefficient at position iof into the coefficient field at position k in C.
copy the exponent pointed by iinto the exponent field at position k in C
array,
Increment iand k to point to the next position in arrays A and C.

while j < t2
Copy the cocfficient at posit ion j of B into the coctficicnt ficld at position kin C.
Copy thc exponent pointed by jinto exponcnt ficld at position k in C.
Increment j, k to point to the next position in Band C arrays.
8. Display the complete array Cas the addition of two given
9. Stop. polynomials.

Logic of Addition of two polynomials


Let us take polynomial A and B as follows :
A=3x' +2x' +x+ I
B=5x' +7x
3x³ 2x² x 1 A Polynomial

ipointer
5x3 |7x BPolynomial

Tipointer
CPolynomial
As the term pointed by i& j shows that both term have equal exponent. We can
perform the addition of the terms & we will store the result in the polynomial C.
3x2x* x 1 APolynomial
ipointer

Sx*7x B Polynomial
Tipointer
CPolynomial
k pointer

Now increment i,j,k pointer


3x3 1
A Polynomial

Tipointer
5x7xBPolynomial
i pointer
8 x3 C Polynomial

Tkpointer
2x*>7%. We will copy 2x* in
Now point Ipoints to 2x* & pointer jpoints to 7x as
polynomial C & we will increment i&k
APolynonial
ipointer

B Polynomial
Tipointer
2x C Polynomial

k pointer

As the term pointed by i &jshows that both term have equal exponent. We can pertorm the
addition of the terms & we will store the result in the polynomial C. Now increment 1j,k
pointer.
A Polvnomial

ipointer

73 B Polynomial

i pointer
2x² 8x
C Polynomial

k pointer

Now term in polynomial B is over. Hence we will copy remaining term polynomial Ato
polynomial C.

3x³2x x
A Polynomial
7x| BPolvnomial

8 x³ 2x+ 8x 1 C Polynonial

Thus the addition of polynomial A&Bis inpolynomial C.

Polynomial multiplication
Example - if two polynomials given as
3x'+ 2x+x+1
Sx'+7x
Then we will perform multiplication by multiplying each term of Polynomial Aby each
term of Polynomial B
3x'+ 2x' +x +1
5x'+ 7%

(15x°+ 10x + 5x +5x') (21x'+ 14x' 7x' +7x)


Multiplied Polynomial Aby 5x' Multiplied PolynomialA by 7x

Now rearranging the term


15x° + 10x + 26x*+19x'+ 7x +7x is the result.

Sparse Matrix
A matriX can be defined as a two dimensional array having 'm' column & 'n' rows
representing m * n matrix.
Sparse Matrix are those matrices that have the majority of their elements equal to
zero. In other words, the Sparse Matrix can be defined as the matrix that has greater number
of zero elements than the non-zero element. 1
Example 0

Fig-Sparse Matrix
Sparse matrix representation using array
The representation of Sparse Matrix will be a triplet only. That means it store rows,
column and values.

The 0" row will store that rows of the matrix , total column of the matrix & total
number of non-zero values.
Suppose a matrix is 4 x 5 & number of non-zero term are say 7.
Row- index of the row where non-zero element is located.
Column - index of the column where non-zero element is located.
Value - value of the non-zero element is located at index.
0

Row 0 0 3

Colunn 2 3 1

() value 7 3 6 3
2

Sparse matrix addition

Algorithm
1.Obtain the triplet form both Sparse matrix.
2. Create a new triplet to store result matrix.
3. Copy number of rows & column from any sparse matrix to result matrix.
4.Let i,j,k be the indices of sparse matrix 1,2,3 respectively.
5. Initialize i,j,k to I
6. Traverse both matrices from second row

if(row no. of matrixI== row no. of matrix2)


if(column no.of matrix|==column no. of matrix2 )
Make the addition ofnon-zero & store it into result matrix by
incrementing indices.
else
Which ever has less column value that to result matrix by incrementing
respective indices.

else

Compare row of both sparse matrix & which ever has less row value copy that to
result matrix by incrementing respectively indices.
7. Copy stop 6 till end of any matrix triplet.
8. Copy the reaming term of sparse matrix (if any) to resultant matrix.

Addition of sparse matrix


Row Column alue Row Column value
Row Column value
3 2
2 4
A=

B= 1 10
1
2 3
6 1

2 6

Transpose of sparse matrix


Simple Transpose of sparse matrix
Algorithm of Transpose of sparse matrix
1.Obtain the triplet form of sparse matrix.
2. Traverse the triplet from second row and consider column elements.
3. Check if column number is 0 then swap its row and
column & add it to transpose of
matrix.

4. Repeat above step for all rows.


5. Repeat step 3& 4 for the column values from 1,2,3 (total number of column).
Row Column alues Ro Column Values
3 3 1

1 1
3
2

Triplet of matrix Transpose of Sparse matrix

Fast transpose of sparse matrix


It is very fast but little complex method to find out
transpose of sparse matrix.
Algorithm of Fast transpose of sparse matrix
1.Obtain the triplet hompase attiN
).Create 2 onc dimensi0nal araY (otal
aray & index ay
3, Size of total aay is the
(otal number ot colunn in the
4. Atevery indeN of otal ut thc oripinal matrix,
lumber o ineN index Dcars i sccond column ol
matrix. sparse
5. Size of index array
size of otal array .
0. Assign indes|0|-l&
7. Now traverse the
indesli|- indesi-||| otalli|
sparse marix ion seeond row& cosider
8.
column clcments
Locativon indeslcolumn numberl
9. Location" indeN of' transpose natris
-swappcd row fiom original matrix
10, Inercasc index lcolunn
number| by I
||. Repcat step tiom 7to 10 for the
remaining triplet of'the original nmatrix.
Row Column Valucs
2 Ital Inde
3
(0 ou

1
I.ocaion

Row Colunn

1 0 1

Row Column alues


2
1 Loation

Row Column Values


Index
3

Row Column values Row (olumn Values


0 1
2 Locativn
1
(0
1

Row Column values Row Column vaues


3 2
1
Time and Space tradeoff
Time space trade-off is basically a situation where either a efficiency (memory
utilization) can be achieved at the cost of time or atime efficiency (performance efficiency)
can be achieved at the cost of memory.
1)ln less time by using more memory
2) In less memory by using more time.

Types of Tradeoff
1. Lookup tables vs. Recalculation
A common situation in an algorithm involving a lookup tables an implementation can include
the entire table which reduces computing time, but increases the amount of memory needed,
or it can compute table entries as needed, increasing computing time, but reducing memory
requirements

2. Compressed vs. uncompressed data


A space-time tradeoff can be applied to the problem of data storage. If data is stored
uncompressed, it takes nore space but takes less time.
Depending on the particular instance of the problem, either way is practical. There are
also rare instances where it is possible to directly work with compressed data, such as in the
case of compressed bitmap indices, where it is faster to work with compression than without
compression.

3. Re-rendering vs. stored images


Storing only the SVG source of a vector image and rendering it as a bitmap image
every time the page is requested would be trading time for space; more time used, but less
space.

Rendering the image when the page is changed and storing the rendered images
would be trading space for time; more space used, but less time. This technique is more
generally known as caching.

4. Smaller code vs. loop unrolling


Larger code size can be traded for hioher program speed. This
technique makes the
code longer for each iteration of aloop, but saves the
computation time required tor jumping
back to the beginning of the loop at the end each
iteration.

You might also like