0 ratings 0% found this document useful (0 votes) 33 views 42 pages ADA Unit 1 Notes
The document discusses algorithms, defining them as step-by-step processes for solving problems, and outlines their key characteristics such as input, output, definiteness, finiteness, and effectiveness. It covers various aspects of algorithm development, including devising, validating, analyzing, and testing algorithms, along with performance analysis concerning time and space complexity. Additionally, it explains different algorithm representation methods like natural language, flowcharts, and pseudocode, and introduces concepts of recursive algorithms and performance evaluation techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save ADA Unit 1 Notes For Later a
S
(Argonthvo 3 Algosithms ase Fundamental +o computes
ey) oa engineesing. An odgosithro is nomed
Fosomn cen th — cenbusy pessian mathematician Abu Jato
Mohammed ibo Musa-al- Khowasizmi. Et is simply a
set of ules tox costing out some task , either by hand
OF, mose us ,0n Amachine. tn athes Wosds , an
algerithm is used by a computer to silve a problem.
pennies An obgasithin is: step - by -step process
of solving a pasticulas problem. Tt defines finite , well -
defined and unambiguous sesies oF instsuctions fox accomplishiy
a task. Trpuds ut oud
Ane | aigosithm piCzedt
Poopesties (or) Charactexsistics @«) Attzibutes of alqosithry ;
+ Input > An abgosithro oy fake 2e1 os move number
of inputs:
Outputs : An olgosithra “must produee atleast one oukpat.
Definiteness: Each instsuction must be defined cleasly
and un ambi quous:
Finiteness + An od.qerithro maust be tesminated successfull
aftes g finite number of steps:
Effectiveness: The algor thea should poodice cossect and
%
accuratt Reaulbs¢ Arecas’ to study an algosithrn: 2
These ore fous’ cowas to study ax abgoxithm
| Hod to devise abgorithms.
2 How to validate abqoathms.
3% How to analipe algosithrns:
uw How to test O ps mM, |
Ie How to clevise olgovithms: 2
=> Devise an algorithm means designing Gun algosithro oa
writing os creating. or de veloping an algostthra-
> “These ose ditfesent desqn | techniques to develop am alge
le Divide amd Congues. bp tan
2+ Gyreedy Method
2. Dynamic Prograraring.
yw, Back tacking, :
45: Branch and, Bound.
2» How to validate algorithry:
once algosthm, is designed, it 1s necessary to ghow the
jit cornputes the covrect answer fos oll degel inpuls - this
process is Known os algovithm validation.
3. How to’ analyze . abgovithrn:
this field of study ts alo Known as analysis ot
algorithms oF performance analysis: Ft wefen +p a task
of determining how much computing fme and storage
ew algonthrn requires:How to test apsogtam :
/ Testing A proysarg consists of two phases
ie Debug ging
2 Poof li nay | Pewfosmnance measusment.
Debugging $ Tt is a psocess of etecubng progiam 09 sample
dota sets: To determine whethes faulty results occurs
tn cossect them.
and, it 30
Pook ling | Pewlosmnance measusement ¢
executing a cossect proymrm on cha 5
“t takes to com
Tt isa poocess ot
ets amd measuring
the time and space, pute the esults:
Algosithm can expressed 13
i Natural Jounguage
2. Plow chart
3. Pseudo code
4 Propmrnming Janquage-
Pseudo code s
> Psede code is one of the wpe to describe an algovithin-
> Pseudo code cues both natumal ond poogsaroming
Jonquage to dessihe on algorithrn.
=> Pictorial representation of an algowthm is known flw«
> Textual eprerentaton of an algorithm is know as pseudo code.
hast:S- These ase two
Pseudo cate specitications: , "
Comments begins with “//'
of the line-
and continue until the end
Blocks are indicated with matching byaces ff A
Compound statement CCollection of statements) can be
Bepresented as a Hock. The bod y or procedure ox erick
also fosms a Hock-
An identities begins with a letter
Assignment of values to various % done using asi Joes
statement
Assignment of — Catoble) 1 = C&apsesion)+
boolean values, taue and falie . ty
oder +0 psocuce these valiux the logical opesatot, and,
ot, Not and elational opesations
4,7,4,2, 4 oe
Ptovides.
6 Elements of maltidimentiond May ane accessed usin:
fi
eu se
ev] Sogie A etouctures of Pseudo code :
[. Sequence Jogi Tstaucton ase prorfrmed ‘in sequence.
2- Selection Sogic 3 Tt abso called as decision Joxic
3s Ttesation Jogie? Tt psodece the Loops
@g. TF THEN ~ ELSE
Tr MEN
\ARTLE Block
FOR LOOR
REPEAT UNTIE- . Input omd output o9e done wings the inst suctions
) xead and write
Al gosit hm Name C Paseme ter Jit )
{
j
Recursive algosithms ¢
> These ave two types of secussive algorithms.
le Divect secursive algorithms.
D+ Indisect secussive algosithmns-
\- Diveck Tecussive algosithrn : An algowthm called by itsdir
Known as direct Secussive algosithms
Void main c)
{
po actual
4. fact(n 1 aa.guments
= ax fact)
po formal
* oct Cint 9) omguments mera
thon ee)
cseturn V5,
che nx tactin)
sseturn C nx-fact (n-1) )5 po
1 2 Pactin)
| stack€4% The while loop takes the following -fosro
2 ‘while Ceondition) do
statement 15
1
statement 15
~> Fox loop takes the Allowing, own
Fox Vasi= Value! te voluer step do
t
Statement | 5
Statement 23
> The ‘repeat -until staternent takes the Following fore
se peat
t
statement 15
statement 25
j
until Condition );
> A Condition statement hax the following -fams
TF Cconditon ) ther
statement 14
ebe
statement 257
Tndivect Recursive algorithm + An algogthr cablad indivactly
© is Known as indisect Secussive algonthm.
Void main’ )
WS} home ( )
i i
AL) CSEL)Y
5
{ eee AIMLC)
{
ed) Bse)s
{ \
AC) 5)
4 psc )
{
CSE 05
j
+ Pexfosmance Analipis ¢
> Determining time ond space of an algovithmo ope quivses
is known ax pestormance analysis
Pestosmance amaligis contains two components:
'" Time Complexity .
2- Space complerity.
+ Time Compleritys Time sequised tor an algovithrn to
perform a given task is Known ais time complexity,
Number of Steps sequired oe an algovithro perfosrn &
given task 3 Known as time complenity.. s 7 me
Time complenity = compile fame + Ruo
— Compile {me dees not depends on ‘input valuss
—> Compile time constant ox fued- &
™ Execution time depends on instance charactemist Run tme ts not dived that means it varies.
~ We can detesmine time complexity of an algorithm 10 two ony
\ Table method Cusing Cost 1) Faequancy]
2° Count variable method:
'* Table method :
> Fint, detesmine the number of steps rx etecution (sle) of
the statement-
—? Determine the total number of times each show statement
is executed.
™ Number of tma a statement evecuter is lenown aa Cost
Low Frequency ot that statement-
> & caralining this two quantities to que total contnuaton of
each statement is obtained.
= & adding the contsibutions of all the statements step
count tor entre algorithm is obtained
243 Find tme cornplenity dos the que algorithm using
table method.statement
ste Fréquenes — totak steps
Algosithro Sur (a9) 0 = t —
t ° : Jo |
S12 0:05 \ ' : |
dos it=\ to ndo \ (nti) ont |
gs. 2 st ali; 1 n ‘ | n
wetusm 95 ' ' | 1
I oO °
Time compleaity of this algo
-s ants ‘is number of steps wequived for this algorithm +o
pestosm the 3 -task-
TR now then 2N4B= 7,
> Te nas then zntd= 12.
whese 9 is instance characteristics: (input, value )
<> Lhen 'n’ value tncxeases, time complenity also incseascs
Can time ) -
> Determaine the time corn plerity foxy the gen algonthro
addition of two matrices -
smo statement ste treque ey Teta steps ]
\ fetgonithen add ( o,b.6,/9) | 0 - a
aly ° a io
3 | toy fel to m do \ mth ™4+)\ |
4 [hx J-1 ton do ' ment) rant |
s | etiil- ata + bli, \ an on {
° S ! a |
6] y
— — i 7 a {
Cornplenity of This algorithm is iam]3 Write an algoxithrn to tind nth tibonace! nurnber ov 3
Find complerity-
j
Statement
Algorithm Fibonacci On)
a2 | 4
> if0n41) then
‘ write (N) 3
a else
= i
™ fai205 fite15
fox f=2 to ndo
4
tna fittes
fos tii fist;
‘
write a);
To analyze the time Complerity of an
omd not
to consider two Cases ne}
Case 144)
ry oqo
-ep
°
°
0
algovit hny , we neel
when nay, tines 3 and 4 get executed once each. since cac
of onelt), sotatal step cunt fer
line has an steps fry ereeution
this care i
Cove 22 nv
The total steps fos this Case ig unt] -
4 ‘
isd thd meang time com plexity ts 2 (constant ),
&Count
>
Variable method:
In this method,
count Vawable %s used in the pxgtam
=> Count variable is a global variable ‘mitialized with avale
"Rew’. This is done sb that each, Hime a staternent
original progsam is executed valine at count
eq,
of that statement-
Algosithrn Sum (4,9)
count = count +1
// count is global Jt is ‘nits Wy 2e00
Count = count +1
4, Il tos loop
{st.- sear
Countte count+t 5
Count i=
Count + =
count+\* J) last me Fos tox Laop
count ars i) fos
}
the setusn statement:
Out pul; Tk nes
Sa12.* Space Complenity
t memory it mean.”
—> Space complenity iy the amount © og
to sun
| > Space needed by on algoathmn % the mem ot the
Fol lowing Cornponents- a
\- Fined part: ‘
2+ Variable past.
|. Fired Past:
> &Fited past that consisting of the space needed bg inct uct
variables, constants ete.
2+ Vasiable Part +
—> A variable past that consists of the space need by nefesence
variables and the secursion stack space amd instance chenacte
> The space equisernent SCP) ot any algorithm 'p’ may be written
as se) # C+ Sp. Cinstance characteristics)
where ¢iS constant.
=> when analyzing the space complerity of an algorithm we
Concentrate on estimating ‘instance charactesistics (Sp) -
> Fos any given problem we need to detesmine hich instance
characteristics to vse te measure the space vequisements:
eqs Find space complenity tox given algorithm.
algosithm sum (ain) I Tterative function fox urn ob oacy of
i che ments.
St= St atils
y \ seturn Ss 5» ve given algosithrn consisting of thsee variables (9,1,5) ond
one arnay vee
The space needed by each variable I wosd since it iS
of integes type so, 3 wosds of space required bx three
variables: \
+ The space needed by an astay a’ is 1 words since it contans
‘on’ eloments:
° The space completity of this algosithm is ScP)= C+ Se
S 2 3to
‘sum (n)
2- Find the space complenity fox qiven abgonthrn.
[sum of avsay of elements using Feeursive function J}
Alqgovithrn Reum Cain)
{
if (n 40) then
seturn 05
ehie
teturn CReum CON-1) 4a Cn}
1
; 5 ‘ cursive Function , So Becussive
> This algorithrn consists of se Func
function unex Stack concept’
—> The secursion stack space includes space for the formal
pasameters, local variables amd the seturn addvess
= Assume that the seturn addvey sequives only one word of memay
~~ Each coll to Roum Feqyriver atleast theee words (value of n,
setusn adders and pointer to army ‘a! )-
~> Since the depth of -securssion i nti So, *ecurcsion stack space
needed iS 2 3Cn+l)-> Pevtormance evaluation can be divided into to moje §
| Psiosi chemeestimates.
2+ Posteviosi testing,
Is Paiows Estimates:
™ These ase aio known as pestosmance analipis : Estimating
time and space sequired fos an algorithm . This is to pestosm
a task is Known as pestormance amaligis: This cam be
Pestormed hefose execution.
2- Postesiosi testing
> It is abto known ax pestosmance measusement- Estimating
time and space sequised toy a program to pesform a
tark is Known as pestosrnance meassement- This Com he
pestosmed dussing erecution of pvoqsam>
* Best case, wosst case, Avesuge care 19 me, complerity,
Best Case +
minimury Oumbes of steps executed on a given payametes(ny
is Known ag het case»
€531. Time Complexity ot \inear Search in bert case is OW
2° Time Complexity of Binary Search 19 best cae is OO
Avesage Case s
Avesage number of Steps eve cuted 6N A given pasarnetey isenowy
Os Average Coue-
&4: In Lineay Search Ocn) 4 In inary Seach O Clegn)-
@
% ‘a,2
vst Cases
Ndr rawr, numbers ot steps
is Kknoon as wort Cae
executed On a guen gervametes
Eqs | Time complenity of lineas Seach in wost care 6 OLN):
2+ The tme cornplevity ot binay Seasch 19 worst care is Olvgn,
¥ Ordes of quowth +
Meauuring pestormance of an algorithm in -relaton with
Input sige(n) ts lenown as osder of qnooth:
256
65536
4244961296
Example fos) best care , wort, Car, ond average case «
Hese N25 10 20 30 4O %0
x Linea Search &
“es Given “Inpuk is 10- One Cornpannsion | sequited to Search
an erent {0 So, Hrne Complenity of linecey Search
bet case ts OO-
in> Gwen input 8 50. ‘5’ comparisions is sequised: to sea. '
the elements 50: tHese input sige n=5 So, Hime Comp:
of Linear seasch is O(n)
~> Given input values ate (0,20,30, 40, 50
12,3,4,5 Comparisions ave required +o seach» the lament
10, 20,30, 40150 sespectiuely. ae
142424 YH5 715
Avg > ye5 P
pontt) ne a
oO. th. BO - 0m
neglected
2 Consides the elements '0/20,75+30) 35, 50, 46-
Binary Search 3
> Given ‘pat i$ 20. ‘I! Comparision js sequited to Seasch
this element. So tme complenity of binary seach
mm worst care ig a Loyn
log] = 2-%1 © 3 Cappsonimately )
> 1,292,33,3,3 Comparisions are ssequired to searth the
echoments 30,20, 40, 10, 25, 35,50 Resrpectved4-
Qgi+
14 2424 3434343 = 17 total compasisons
v
avg- a: Tyr % 3 (Appwnimately )
+ Aayrptotic Notations ¢
aoe eee
con be eupressed sn diffesent
Time compleni ty
notations: They ode
notations Known As
\- Gig oh (O)
2- Omega Cr)
3 Teta (0)
4. Little oh Co)
s Little omega Cas)
| Big oh (0) Notaton +
—+ Bia oh notation ‘the
asymptotic
funtion fone O90) [wood as
in) 1s big oh ok gen] if ard only if thexe exists
Positive — congtanrts ‘c' amd Me such that
fin eceyn) tet oll n, no
Convest the giver function -f(n)2 20+ into
Exomple :
fone 6X9)
big ob notation.
4cn) = 20+3 ants 2 69
fej. Cl 24s Lan
523(F) nl
: 2 = OCaC .
@ ue on , ") 726 O-7
Cees qealt) my
ae wet) ny
23,> TNhis statement -ftn)= OC4en)) states only thot gen) 's
OO Upper ‘ound value of #00) -foxr all nano:
> Value of 'c? must be least ar possible
The function 243 -O0n) it these exists positive conctants
Coe omd Mm=3 such thot 243 4 3n oe Nie Ey
» Fiy)= 6-24 Into big ob notation,
fin) 6% 4n>
finy= Ogen)
€f an. Ji)
G57
Men 4
fone Cx qin)
6.2% nr 2cr
6274 nr 2 7,29
"S214 CT) nay
286 28 CT) ner
ST £56 CF) nas
7 "eer) nayOmega Notation (1) ¢ A-function -frn)> agen) Hh
here enists positive constants c and M such thot
din) 2 C¥ Gm) VO, ZMH
Xv
_ 4en)
—~ (x am
oe te Fanction, fon) = Bnar into Omega
feny= andr
fon) = Alqen))
any2 = SL On)
eas
£m) 2 Cx GW)
4) > on
anta 2 an
sz a nar)
«
w
6 mez ()
N24 n=3
Oe ee ee
constants 23, No2\ such that ant? 2 an A ane2|
2 The function qin) % only \owey bond on tin).
possibl
> value ch © Mae be, Mone as possible
Mis.3. Theta) Notation:
=
The tunction #eny= OC 90nd) He these exist positive
Constants ¢,, Cr and a. such that
Sie gon) 2 tony e K2* FM) Yn, n>n,
C.* Fn)
Jo hon
——— ¢, * Zin)
E91 Convert the function 3n+2 ‘into 'Q’ notation
Given
fen) = 342
40n)= O@«n))
ant+r= Otn)
CR Gen) 2 fen) a ce
tiny> 4 4cn)
ce 3
Cty
* an)
fin) 2 Cix qn)
Sn4+2 > 3n anar> a*n
S>3 M1 a) Sn42 5 un
26 nme2 Ct) 524 ™\ Cr) Near
ne
fre! Ere 21) non,
"412 nary ~
Len
C3 Goh
Renee Mo = 2,3, 4,-- .
> The function 3n4r =O) HE there enists Positive
12> 4 Brmy=2 Such that 304 3ntr 2 un
=
constants C23
Pa nortty, The ‘Q” notation is more precious than bth OG 9"
é notations -
The fen) is OC gerd) HF gen) is both an upper omd
lower bound on fen):
4 Little oh(0) 2 The -function -Pen) = oC gen) iff “these
“o_o
Gp ty hen)
2 O
291 Convert 3n+2 Into little ‘ob’.
fcn) =3n47
fen) = of 9n))
an4rz = 0 (n>)
lr Fm)
No Jn) -
ue Bn42r _ tt 30 wD
wee nog cs
> Lt axblyaxlyt
noe " =
2 > Oro
Nth
Lr 20
hod Wr5. Little “Omega” (2)? The function ten) = 04 Ca)'
a kt do)
~~ so.
NA fin)
€gs Pony. an*43nd1 Convert this function inte bsg oF.
ten) = OC gen)
tin). Otnr)
c=4
n= 4
#0) 2c * gin)
BM4ang | 2 cent
> Time Complerity of selection sort is big'oh notation
Oe
* Towers of Hanoi Poeblers is an erample of secursive
algosithm.
> These war adiamond -towey Clabelled ‘A’ ) with 64
Golden disks. The disks were of decreasing sige
ond wese Staked Mm the towers in decreasing order
A SiZe Fem — bottom to top.: esides this towes these wese two othes diamond
Vtowess L labelled @' yt")
“Since the time of Creation Beahman Priests have been
attempting to move the dlisk Prom towers 1A’ “to -towey
Using tower ‘UO wese intermediate Stosies.
> 8 the disks wese vey heavy they can moved only one
ata time.
> In addition, at no time can adisk on a topot smalls
disk.
Algorithrn Towers of Hanoi Ln, x, ¥, 2)
{ FCn>1) then
oe
Towers of Hanoi Cn-1, 4) 2.4)5
write (“move top disk tom towes", x, “to top ottover¥)
Towers of Homi (n-1, 2,4, %)5
5 recurrence elation of tywex of ttenoi is 2TCn-1)+)
eS @ascme cthat tomer Ay consists OF 3 clisce amd move
dise fam ‘a’ to ‘@’ by using ‘ec’ as intesmediate
ne rn wve
|
| sey om
a a ¢