Basic Dats Structures: Abs cares. stacks, queues, inked lists and
Storrs
eves, hashing, binary trees,
balanced Bess, se
Searching: Internal sod external searchang, use of bashing and balancing techniques.
im
Memory Management: Garbage collection algor@hens for
sorage allocation for objects with muxed size, budily system
equal sized bh ¢
a
Physical Devices: Chcactersiss of storage devices such as disks and tapes, Ih
indice Bask: File Symem Opermions Create, open, close, extend, delete, read-
Siock wmnblck, peviecons mechemsms. 51.
Gomis srstems. indexme using B-vee, B+ tree and thes vanants, hashing ~ hash
Secu. cols bandime methods extendible heshme 12L
ae bf
2Data Structures
eA data structure Isa scheme for organizing data in the memory
ofacomputer. j«
data structures include lists,
«Some of the more commonly used
and graphs.
arrays, stacks, queues, heaps, trees,
«The way In which the data Is organized affects the
ofa program for different tasks.
mala |
«Computer programmers decide which data structures to use
based on the nature of the data and the processes that need to
be performed on that data.40-Mar-23
Data Stmucture can be classified as:
Y linear
Y non-linear
Linear (elements arranged in sequential in memory
location) i.e. array & linear link-list
Non-linear such as a tree and graph.
Operation: 7
“Traver.. . Searching, Inserting, Deleting, Sorting,
Merging
Array is used to store a fix size for data and a link-list the
data can be varies in size.An algorithm takes the input to a problem and transforms it to
the output using a function.
A problem can have many algorithms.
A computer program is an instance, or concrete
representation, for an algorithm in some programming
language.
Analgoritt: esses the following properties:
Litmus 0. ects \
TL. It must be composed of a series of concrete steps.
There can be no ambiguity as to which step will be performed next.
mm.
IV. Itmust be composed of a finite number of steps. ©
VE
Tt must terminate.
40-Mar-23There are often man
problem. How do we
* At the heart of com
e b \puter program design are two (sometimes
conflicting) goals:
°
'y approaches (algorithms) to solve a
choose between them?
1. To design an algorithm that is easy to understand, code, debug
2. To design an algorithm that makes efficient use of the computer's
' Tesources.
© Note(1) >1 ut algorithm that is easy to understand, code, debug.
is the con 7 vare Engineering.
© Note(2) > Ju design an algorithm that makes efficient use of the
computer’s resources, is the concern of data structures and algorithm
analysis.
10-Mar-23oe
10-Mar-23
In mathematics,
behaviour of a fu
particular value or infinity,
functions.
Big O notation is used to describe the limiting
nction when the argument tends towards a
usually in terms of simpler
In computer science, big O notation is used fo classify
algorithms by how they respond (e.g., in their processing time
or working space requirements) to changes in input size.
Big O notation characterizes functions according to their
growth rates different functions with the same growth rate
may be re ved using the same O notation.
A description of a function in terms of big O notation usually
only ane an upper bound on the growth rate of the
function.algorithm,
A description ofa function in terms of big O notation usually
only ‘Provides an upper bound on the growth rate of the
ction.
> Associated with bi
eusing the symbols
bounds on
g O notation aré several related notations,
2, Q, @, and ©, to describe other kinds of
mptotic growth rates.
A more detailed explanation of Big O analysis: it measures
the efficiency of an algorithm based on the time it takes for
the algorithm to run as a function of the input size.- ° vt is a sum of several terms,
the term with the larg
kept, and all others omitted.
Tf f(x) is a product of several factors,
that do not depend on x) are omitted.
~ For example, let f(x) = 6x! -2x°+ 5, and suppose \
this function, using 0 Notation, to describe its
approaches infinity. This function is the sum of thr.
a and 5.
ie any constants (te:
°
i
Of these three terms, the one with the highest groy
with the largest exponentas a function of x, namely
= Now w apply the second rule: 6x* is a prox
Whicl. x‘ factor does not depend on x. Omittin
in the simplified form x4.
= Thus, we say that f[x) is a big-oh of (x*) or matheme
{ Sx) = OC").
- We may confirm this calculation using the fo
Sx) = 6x* - 2x3 + 5 and g(x) = x*. Applying the for
40-Mar-28Pove, the statement that f{x) = O(x4) i
orseveral terms, the term wit
S omitted,
tof several factors,
d on x) are omitted.
let f(x) = 6x4 -2334 5, and su
, Using O notation,
finity. This function i:
th the largest growth rate is
any constants (terms in the product
PPose we wish to simplify
to describe its growth rate as x
is the sum of three terms; 6x!, =238,
\
> terms, the one with the highest growth rate is the one
st exponentas a function of x, namely 6x4,
\pply the second rule: 6x* is a product of 6 and x* in
factor does not depend pn x. Omitting this factor results
ied form x4.
that f(x) is a big-oh of (x*) or mathematically we can write
this calculation using the formal definition: let
ae and g(x) = x‘. Applying the formal definition
atement that f(x) = O(x') jjEt JUS = xt ~2x84 5, and suppose we wish to simplify
using 0 Notation, to describe its growth rate as x
inity. This function is the sum of three terms; 6x4, -2x3,
tms, the one with the highest growth rate is the one
Onent as a function of x, namely 6x4,
the second rule: 6x‘ is a product of 6 and x‘ in
actor does not depend on x. Omitting this factor results
ed form x‘.
hat f(x) is a big-oh of (x*) or mathematically we can write
firm this calculation using the formal definition: let
x? + 5 and g(x) = x*. Applying the fc finition from
tement that f[x) = O(x*) is equivalent to its expansion, 49Time Complexity
* The number of
executes during its
computer science,
(machine) instructions which a
running time
5 S Program
is called its time complexity in
Time complexity of an algorithm quantifies the amount of time
taken by an algorithm to run as a
function of the length of the
string representing the input.
eB CO} nly expressed using bip O notation, which
exclude vients and lower order terms. When expressed
this wa ) - time complexity is said to be described
asymptotically, i.¢., as the input size goes to infinity.
‘ ane 2 f size
~ Eg, if the time required by an algorithm on all inputs of siz
a at most sa 3n, the asymptotic time complexity is
O(n").
~~ §€©€6€«63§$ ee_
elementary operations performed by the algorithm, where an
elementary operation
: takes a fixed amount of time to
perform.
t
Thus the amount of time taken and the number of e!
lementary
Operations performed by the algorithm differ by at most a
constant factor. \
Big Onot s used in computer algorithms as a worst-case
scenariv i:....\ration. Constants are not considered as we are
only concerned on how a function f(x) can grow respectively
to g(x) in a way so, f(x) = O(g(x))
@ ‘=i
40-Mar-23complexity, of an algorithm, denoted as Ten, Which is defined as
the maximum amount of time taken on any input of size n,
1L-Constant time complexity
+ An algorithm is ‘sid 16'be constant time (ado written as (1)
time) if the value of T(n) is bounded by a value that does not
depend on the size of the input.
Eg Stack, hashing scheme
2-Logarithmic time complexity
* Analgo i s said to take logarithmic time if T@) = O(og n).
Dueitoin . of the binary numeral system by computers, the
logarithm is frequently base 2. ;
¢ Eg- Algorithms taking logarithmic time are commonly found in
operations on binary trees or when using binary search.
o epee.
10-Mar-23.D
dimensional arrays
ress 2000 + 4S
\ can be represented as a
are also sometimes calledagsasscesean
a) Very simple
if full use of memory
b) Economy ~
ne same time
) Random accessed att
+ Disadanlase ay
ory ifn not fully used
+ wasting MmHomogeneous data:
a) Elements are Tepres
b) Elements are saved i
locations.
Number of elements, N-> length or Se of an array.
a
If:
UB: upper bound ( the largest index)
LB: lower bound (the smallest index)
Then: N=UB-LB+1
Length = N = UB when LB = 1
nted through indexes.
e
tial in memory
in sequen«In FORTRAN, PL/I an
as, 1,2,3, «+92 is
i = written symbolically
+ Allelements 10 Armay are written sym!
* the subscript. *e. Al, A2, A3, fea
d BASIC > A(1), A(2), «+» ACN)
InC,CH, Java > A[0), A[ 1], ---+ A[N-1] , subscript starts from
0, LB=0, UB=N-!
In visual basic the array can start from any number like
5,10,15,100 or it may be like A[5],A[10],A[15],A[100]
31 {» — Ingeneral, index function:
Loc (X[iJ) = Loo(X{LB)) + w*(i-LB);
cation required.
h of memory lo
d character: 1 byte.
where w is lengtl
4 byte, integer: 2 byte an
For real number:
Example:
IF LB = 5, Loc(X{LB]) = 1200, and w=
1B ec eiah + (8-5) w=4,find Loc(X[8]) ?
=1212on Lal
[add counter]Dnsertion Algorithm
at the back is easy
iddle requires the
if there is a Space. Insert
«Insert item
tem in the m movement of all elements
as in Figure |
to the right
® Arravinderes New item
iE ry
Ne eis k MAX _LIST-1
a MAX_LIST
items
ADT list positions
Mas Rig S
igure 1; Shitting items for insertion at position 3
. 35,Peon Agony
s Delete item
(a)
A Array indexes
Delete 19,
=
7 MAX_LIST-
MAX_LIST
size
items
°
ADT list positions
23.
37« Example algorithm:
[DELETE(LA. N, K. ITEM)
1, ITEM=LA[K]
2. Repeat for 1 = K to N-2 IfLB=0
2.1 [Shift element 1+ 1, forward]
LAI] =LA(I+1]
3. fend of loop}
4. [Reset Nin LA]
N=N-118-Mar-23
\
Binary search algorithm is efficient if the array is sorted,
|
A bina
ry search is used whenever the list starts to become
large. |
The binary search starts by testing the data in the element
at the middle of the array to determine if the target is in the
firstc: « md half of the list.18-Mar-23
If it is in the first half, we do not nee
half. If itis in the second half, we do n
half. In other words we eliminate hal
consideration. We Tepeat this process until we find the
target or determine thatit is not in the list
d to check the second,
jot need to test the first
If the list from further
To find the middle of the list, We need three variables, one
to identify the beginning of the list, one to identify the
midcie he list} and one to identify the end of the list.
We analyze two cases here: the target is in the list (target
found) and the target is notin the list (target not found).
atyY Searc gorithm
° Target found ‘case: Assu;
sorted list as follows;
a[0] a1] a(2] a[3] a[4) af:
» DT hola]
me we want to find 22 ina
a[6} a7] a8} a[9) af10} af1a)
e © The « - indexes are first, mid and last. Given first as 0
and last as 11, mid is calculated as follows:
mid = (first + last) / 2
mid =(0+11)/2=11/2=5
18-Mar-23
42ary Search Algorithm
» At index location 5, the target is
(22> 21). Therefore, eliminate the array locations 0 through
5 (mid is automatically eliminated), To narrow our search,
We assign mid + 1 to first and Tepeat the search.
first mid Tast =a
7]
0 5 || ) 44
Be ¥
greater than the list value
mh a{1] a[2] a[3] a[4] a[5] af6) a[7} a[8] a9] a[10] ait]
4 | 7] 8 |10] 14] 21 [22 [36] 62 [7 si jor]
22>21Binary Search Algorithm — |
The next loop calculates mid with the néw value for first and
determines that the midpoint is now 8 as follows:
18-Mar-23
mid = (6 + 11)/2=17/2=
a[O] ali] a[2] [3] a[4] a[5]
8
/
first
mid
8 [aa
[4 [7 [s Dolial
a[7] _a[8] a[9] a[10) afi)When we test the tar,
discover that the tar
et to the value at mid
and recalculate mi@. This step effe
elements 8 through 11 from c
arrived at index location 6, wh
This stops the search,
ctively eliminates
onsideration. We have now
Ose value matches our target.
first mid last
a
/
a[0} alt] al2] a[3) af4] als: 7] a[8} [9] af10] aft)
22 equals 22
18-Mar-23 _ s
)
}* These settin;
follows:
mid = (3 + 4)/2=7/2=3
igs of first and last set the mid index to 3 as
first mid last
eee
a[0} afi] af2} al3} a[4] alseS thal
value, so we set first to mid + aS
location 4
* follows:
€ target is greater than the list
or 4. We now test the data at
and discover that 11 < 14, The mid is as calculated as
last
SIC} 9(0) af2] af3} af4) af5] a6} 217) af8) af9) af20) af1)
[io[ [at [22617
first mid last
18-Mar-23mid +
di discover that 11
ent values; in other woBinary Search Algorithm
Example algorithm: (variables used)
DATA - sorted array :
| IZEM - Info
AB LB — lower bound
UB - upper bound
Sis’ Location
MID - idle Location
LAST - last Location
50
18-Mar-23~ ee Variables]
T= LB, LAST= UB;
Pape = (ST+LAST)2;
- Repeat 3 and 4 DO sT <=
- IfITEM For example, Minx chain
bys ordering can be soh _
logarithmic time on a Paral . Solved in poly
el Random Access Machine
‘Linear time
An algorithm is said to take linear time, or
O(n) time, if its time
complexity is O(n).
* Informally, this means that for large enough input sizes the
Tuning time increases linearly with the size of the inputComplexity 9} {N algorithm, Aeno}
the Maximum amount Of time taken SNany inp
‘ ae time Complexity
* An algorithm is sai ‘0 be constant time (ago written
plo Ime (also writ
time) if the Value of T(n) is bounded by a value cates a
depend on the size of the input, 7
Eg Stack, hashing scheme \ ;
2-Logarithmic time complexity
An algorithm is said to take Logarithmic time if 11) = O(log ni),
Due to the use of the binary numeral system by computers, the
logarithm is frequently base 2, ;
* Eg- Algorithms taking logarithmic time are suman found in
operations on binary trees or when using binary search,
ted as. Tw), Which js Aefined as
Nut of size n,re “me ea ‘
Organis, Sed hehe
ee “n aa (on A an oh ay ae coin
* Itis a
ae Ay ae ae ie a Le, it Contains Clements
“* The data type cota
double aug
ray ¢ inbuilt i
even user defn aa that is crea ie ee Sloat char,
Or a class,
aS a structure
is t0 oragnize i
logether as a gToUp to
geneous data
Perform operations on data in a
collective manner Since traversal and retrieval becomes easy.
* Whenever a
Homogeneous list(One Dimensi
Dimensional) has to be implemented we make
ional or Multi-
use of array.
18A, Insertig
F deletion
' display ji
D. cnt Order.
4
Arh Nines
So; Orting (bubpy blea ee Sjust aR; aR
Used | fee man
SI™ulations y ting
re ests of a ‘single share, red
ayer ie rnsferrin cad
® Necessary ri
Tesource (Printer, disk,
asynchronous}
Teceived a eee Tate ag
Processor §(10b uffers Ts), eg.
y Not
file 0 so eeeen two
8+ Dipes, file
ill us 4 queue to hold People
"eg vie 2 Tepre esentatie pe
TS, iPod
cs PL a ae wi at bon = gle cb mes , play
list, ayli.
Rl the front ofthe] ist
4A aueue
Used for
's just ay
simu
Many j
lations. ti
~ Call cent
Het phone systems wi yc gEt 0 hl peone
Inline untita seryigs Tepresentatveietea
~ Buffers on wy
playlist Playlist th
from the front of th
P3 layers an
orftkebe
helis.
i portable cp
Players Pod
add songs to
the end playthe Yanabley f0r ong thread
% . NCUlioy OO Can by
that mepted by Another Competi ing es ad The an
ae tatty Sed Is to Allocate the 5p
local, Varig able k.
Even W only y TERISLETS are A fo hold tem NPOFATY Values
Within the Subrouting 7 ‘ariable stack Of some Sort iy
required (9 Save register values ™{Ps calling route bey .
they are destroy odae by Speci ial ha King 1S ty hy
Xpress a i ms st compiler aie we a ney
8 operation” Sire Stagg TS N pee
* Th the Case of
Interpreted ed language, (NO stacks ary 0
Contains the pe e1 pend ne US that avvait ison oS Ries
Sperations, © other g Contains the ‘Meme input tee ay
penidin ng SPerations,
8Ssociated with the
Ina Compiled language, the compiler keg
during Us instruction
pt track of the Beading operations
inst feneration, and the
evaluation stack toho
wanwane Uses @ Single ANPRSiOn
Nd Iermediate results«oy of Ee ie a in
r indire ti
N Wasp
tine for
co ;
nt Calling ts its alt Rat
e
Ving the
etme a Subroutine ig
Ously saved Tetum
allocated stack automatically at ac}
call, TECUrsive Toutines May call themselves Without any
Se
probArray is
° st
ganise, sequentially (ee
chunk of Memory), 7
It is a homo, neo: le. i i
Which are of the Sie eR oe MW etl “Tene
a° The datq ‘spe of array can
double and d ine:
€ven user defi
Ora class, C
Sense that the
Mother in a Continuous
data js
be inbuilt like int, float char,
d that is CTeated using a structure
°
The main use of an array is to Oragnize homoge
iy) gnize
together as a Stroup to perform Operations on
ealtcciae Manner since traversal and Tetrieval becomes easy,
i mensional or Multi-
lenever a homogeneous list(One Dimensional
Da has to be implemented we make use of array,
8Unearp
A Array
B Stack
G queve
D. Unkedtist
Ata, Structure
tS
* Noniline
AL Tree
B. Graph
>
Ae data st vucture.* Space complexity = The am
algorithm to run to completion
m* Some algorithms may be more efficient i
into memory
‘ount of memory Tequired by an
.
data completely loaded
1. Fixed part: The size required to store certain
q
data/variables, that is independent of the size of the
problem: eg. name of the data collection
°
1 ize Is
. Variable part: Space needed by variables, whose 3
4 Heserdencoh Ree of the problem: eg actual textAn algorithm is said
fo mn ae
O(log ny, for some er Polplogarithmic time if Tin)
>For example, matrix chain
> Tha ordering can be solved ye
logarithmic time on’ Paralle| Random Access Machine '
. ‘Linear
* An algorithm is said to take linear time, or O(n) time, if its time
complexity is O(n). -
* Informally, this means that for larg:
Tunning lime increases linearly with the
enough input sizes the
size of the input