AOA Handwritten Notes'
AOA Handwritten Notes'
Date
Page 1 C
imon Ansari 272
Space ime coOmpleai
Alorithnn:It is a proceolure to Solve
aproblenn io finite numbeu a
SHeps
Theue may be more Thon 1 olgorithm
fo peufornming he Same tesk
Eq: Sorting.
Hence,E isnecebsau to ole cicle
which algorithm s bette in wuhich
Sitvatton.
Alqorithns OMe
oUnalySeo with_2
factor-Space tinneiit tak es
less Space thenits executom_tim0
is hiqheu
Geneually, time analys/'sKi S as qivenmon-
importance while hmakin9 COmparitjcn
of algorithna of SOme
ASSMAte
Date
Poge
Aiman Anari 272
Space CompCxily
fs is olefined os Ommovnt f memony
memony
mqveut an algorithm to run.
Space Compleri s conmpute d
uSing
USin
2 factos Constant charactoristfcs
i I n stan Ce characteristicg
S Ct Sp
C Constoant
Sp Voujable
Example 1:
h e Space req uirement for this ctaement
i s S C+Sp
Heue, Sp 0
Sp
9 C+0
Example. 2
SuUm 0
or i 1 n
Fn)
Cagn)
Mo
elASSMAte
Date
Page
Aiman Ansari 2722
i. ThetaNotedicrn (o) (Aveaga Case Analysin)
J giA the areuage amovnt a ime an
algorithmn neeods.
Let
FCn) and 9(n)_be twonon-negatie functions
C and cz aue wo postrvne constrcdints,
Svch that
C 9n) 5 Fn) s Cz 9(n)
Heue, Fn) solenoted os oCgh))
The cuve for nortalon is
Tn)
Cgln)
Fn)
C n
n
no
classmate
Date
Page
Aiman AnNari 272
i. Biq-Oh Notation (0) (werst Case gnalys)
qired he niaximunm Omovntoyie an
algorithn necels
Heue,
h) C 9(n)
Fn)
n
hs
OCc) oC1)
lASSMALe
Date
Page 6
Aiman Anan 272
a at5
stalement reguins 2 time unit.
This
One fur additon onol Secono for
s siqnMent je ol2 ol2) = OC1)
i.Multiplicativ Constants 0re OMi tteol.
each ask nnsIn tinue TOn)= n, thn
4 tasks will run in O(4), which
Sinnplie to Oln) ie. olc n)-c.0[n)
oln)
ii. Addtien s perfermeol by -oaking Meadume
Maximum. e4:If 7 (n): n Om ol
Tn) onc two n
tasks to be executed seqventialy,then
he vebult is O(n)+oo')
on)
ie. olT): o(1) olT +T)
max fof)s ofr.)]
iv. Multiplicoction is use d when one task
Couuse Cnothon task tu b e exeoiuted
Some numbers otimes for cach
cach
iegrotion fitsel. e nedeol_loep
om).ofn) lmn)
elasSMAte
Data
Page
Aiman Anur 272
Exanmpleo
main C)
int a2
vnits
2 uniEs
1 adodiHon
1 a s sig nament
ot)to(2) of1t2)
ol2)
ol1)
main C)
int_i, n, aj
n 5
O 10
r i O; ien itt)l
tt
Recuenceo
When an algorithm containsarecMrSire
Call,its running inne s_desCribe d
by a e Currence egvation on recurrence
Mathe moutical tools are useol 7p olve
hese e curences
i. Subsituition Method
i. Kecursion Tme Method
ii Maoteu Yethod.
j. Substiturion method:
The Solution s 9ves sed onol mathematica
incluctions vsed to prone that h e
esE9UASis Coe ct o incoreet
tranap le 1:
Recumence rlation: Ttn) TCn-1) +
InitialCondition Tlo)= o
Sal:
na1, then Il1) -[1-1) + 1
T(O) +1
O 1
Enampe 2:
Recurrence relatten T(n) = T (n -1) t1
Initialconolitions T(o) 0
bMs/1616
n 2) n /2)
Cnia) na) a)
6A)otn)'(otn
rn)h' 3/n/) +9(nlue)*
n' 3n'/4 + n'/16 p
n 1 t 3/4t 2/16_t
oln)
elASSMAte
Date
Page-
Aiman Ansari 272
ii Masteu MMethod
to get he
I t s a direot way
Souttcn.
Mau method Lwarts Gnyorthe
ruMHendLA
ellow ing tpes
Tln) a (nlb)i FCn)
wheue a Sonstant a n da z
n zd and d i s Same constant
FCn) must be positie
Alow, b 2 2
hus a b TCase 1b]
T(h) ( loq n)
(nlogn)
18
Airnon Ansari 272
Selectton sort
Sort,the. Ourroluy interpre tel
interpre ted
In
In selecttor
as diriclec into two ports
Sorbeopart
Unsocteol pdct
Initi oully,
The socBeol part sempty.
The
The vhrorted partsThe entire amy
neyey pass itemation) the clteat
elemen1 S FovholOut (selec ked) and
i t is Swappea with the etmost
elemenb
Examp le 70 30 2 05 0 60 10 A0
Initially
Enply 70 30 20 50 6 0 1 0 A0
Sorteol Uhsorteol
10 70 30 2050 60 0
Sortecl un Sorted
10 20 70 30 $0 6 0 40
Soc ted
Unsor led
10 2030 70 S0 60 40
Sorteo
Un.Sorbed
ciassMAte
Sorteol
Unsor ted
0 20 30 40 50 70 6bo
Sorteo Unsorted
O 20 30 40 5O 60 7O
Sorteol Unsorteol
20 30 40 50 60 70 Sorteol
arrouy
Analysis ofSele ction Soct
Heue, or Selecting he Smalleotelement
homthe Uhs arted parz equines
SCanhing oll he 'n elements
. ie (n-1)COmparisdns
Initially 30 7 0 20 S0 40 10 60
Enopty
Sorted vneorteo
30 70 20 50 40 10 60
Sor teol unsarteol
30 70 20 50 40 60
10
SorEeel on
8ortte
elas5MAte.
Date
Page
Ariman Angari 272
20 30 70 9O 50 60
20 30 50 70 0 10 60
Sorted unsorteol
20 30 0 5O 70 O 60
Sor ted unsorte ol
O 20 30 40 So 70 60
9orted unsoreol
10 20 30 10 50 60 70 sarte c
orra1
alAsshA
Dete
Pege 22
Aiman Ansari 272
Analysls ayinseytio 90rt:
Bes Cose
When he aMucuy s alueady Sored
In ereuy pais(itation), Single
Compouisans S hmoode.
Hence, the to tal humbey a
lompoujsons OMe n
Thus, the tamplexil in beot Cae
s0/n)
elassmae
Dake
Bege 23
Aiman Ansor 272
Complexit class
oC) Constan ime
O(n) Lineartime
atogn), on logn) etc Logarnthmic -ie
o(n) uadratic time
o(n) Polynamial ime
0(kh) Exponential time
O(nl) Factorial Hme
wheue,
k constant and
n s h e input sizee
NP-Had Hordeat
P
NP-Complete Hard Medivnm NP
Hard NP--
Cornplete
NP Medium
P
Hardeat NP-
Hard
taay
acsmate
pote94.
Fege2
Aiman Ansari 272
*|P Class of Problemc
P-Polynomioud ie Solving
s Se Problenos that
Qun be 9olve o in polynomic
time
P proble m take 1inne hke O(b),
0ln). 0(n)
Exaople: inding marimum clement
in onOrray or to check.whetber
aSting s palindrame Or not
NP Class a Problemd
NP
Non-deer ministic fblynomial tinme
It
Saluing
ic a et_opcoblemA t h a t cant
be Saveel Palynonmial time
Example Sum o Svlbset Given0
Set umbeuS, do eo tbeue exis|
Subset whose SumszeMo 2
NP pabems
NP
OMe cbeckable in
palynamial ime
ie.given a Solutton
we Can Check
o_aprablena,
thot whetheu the
Solut io S COrre.ct on not i
palynomial ime
efasSMAte
( Dole
925S
a b e problemn
Divide a b C e
Svbproblem
A
Conqier D E
svbsoluton
a b e 9olutlon
gtrategyy
Consists ot 3Seps
Divide4 hearrayinto
Divide Sub-arrouys 2 elermen ts
with_ 2elernents
the Sub-arrou
two
ii) Congver: Sort two sub-aro
in)Combine:Merge the orteol Orouy
into a Sinqle
Example
L18 2 o Is12 a I S 6
Divide
pi LBl 2 ol15
Divide IS 6
pass2 18
Dividle
poss3
Divide
pogs 4
Nupe
pat 5 9 12 6I5
Merg
purd6 6 912 Is
Mrge
paus 7
o 211is1 6912s
Nerge
ps& LO216 112 1S 51
Numbe0pa n-1
efasata
2
Aiman Anrori 272
Combine (Al1.n]
n],low, mld, high)
k i lawj
midt1
while Lis:mid &d jsehigh){
ifALI] <e AljI)
temp lk] Alil
+t
J else
temp Lk] ALjl
k 4t
pivot first
first
last
while (isj)f
while(Ai]<:AlpiratJLk i s las)
it J
while (AljjèAlprvet]k4 j2irst) f
if
tennp Alil
Ali) Ajl
Aljl temp
3
temp Alpivat 1
Alpivot Aljl;
Al] temp
qvick Sort A, Arst, ji-1)i
gvick Sort (A, JA1, last)j
alssmate
Date
Puge 32
Aiman Rnaari 272
Pnalysis of Quick Sort
Yorst Case
hlost Case OCCure when the key
elements 9e placed CAt One end
and he O oy Size is (n-1-1parr
In 2 pass, h e key 9ets plarel
one end and Si2e
(n-2) remainA, andSo on
Hence, 7he to tal numbeu
CompauiSion s to Sort he Cumple te
(n-1+ (h-2) 2 1
nin-V2
0:5n - 0:5n
0 Sn
Oln)
Best Cale
he array 1s aluway Partiioned
ot he mid h e n h e olgorithm
gires the beat Cose effeciency
LDeuivotlan ic Sanme as thad o meuge Jort
Worst Case
elassmate
Date
Pege33
Aiman Ar3ari 272
Examp le:51,26, 93, 17, 77, 31, 44, S5, 20
54 26 93 1772 31 94 S5 2 0
pivet
54 26 93 77 31 49 55 20
pivot
ikj, s wop Ali] ond Alj1
54 26 20 17 77 31 44 55 2e-93
piro
ij Swop A[i and Aljl
S4 26 20 17 4 4 31 77 55 293
pivot
54 26 20 17 44 31 77 55 3
plret
ikj wop ALj] and pivet
31 26 20 17 44 59 77 S5 93
pirat pivel
31 26 20 17 44 54 72 93
pivvt
plvat Swap Aly] Alpm
SwopIlj] and Alpivet)
ASSMALe
Date
Pge
Poge 341
Aiman Ansari 272
17
pircd
26 20 31 44 511557792
17 26 20 3194 54 55 77 93
pivet
J
SwapAljland Alpiret
17 26 20 31 44 64 55 77 93
piret
Swap Alj]and Alpiret
17 20 26 31 44 5 4 55 77 93
elasSMALe
Date
Page 35S
Ainan Antaui 272
Univeusity Queation
P L,E
E, X A, M,
A M P L E
pivot
ijSwopAli)_ond Aljl
P L
E E A
pivet
X
E E M P
pivat
ij Swaplj] and Alpivet]
E E M P
pivet pvot
A E E Pivet P X
pivet
pivet
SwapAlpiret and alj] Swapiand Alil
A E P
pivet
Swap Alpivel and Aljl
A E L M
pivot
A E E L M P
pivrt
Sup Alplvot]_ond Al
AE E L M P X
elASSMAte
Date
Poge 36
Algori thm
maxmin li,J
ili:j)
mar min:AlIJ
Jelse
i
i lAi] A lj])
max AGI
min A ( i J
else
mar Alil
min Alj
3else i
mid (itj) 2
maxmin imia) /cc
new h20x m a x
new n in min
moaxmin (midt 1,jj
ifmax newmar){
mor = newmax
Jif min newmin)
min newmin
elassmate
Date
Page 383
max 20 max -
m i n : I0
min:-1 max 50
min IS
n o r 20
m i n : -4
mox 20
min - 6
mar 50
min -8
Analysis
For each half divided svb-arrmy, theue are
two reCursiveCalls ma de.
Hence, time required r conoprting
T[n) 27 n/2) 2c to Combine
2 0n/2) 2 olc)
o (n/2) o (c)
o (n/2)
=On)
Thua,
Thud he ime Complexity 1S O(n).
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
Date
Page 39
AimanAnsari 272
o Binary Search
11 3337 42 4S 29 100
i irst last)
Print ( Elemeni nav tound")
else
print(Element -found)
IASSMALe
Date
Page 41
Aiman Ansari -272
elemens in he CArro
Worst Case
The elerment to be senrcheel is
i nt h e array
eitheua no PyebenL
Or ib is presenL et the enc
op the CUMo when Cnly on
elemeNt remains
For example, isize o he Carrouy
is 256 hen he Searching
Cohtinues with redluue ed `ze o
256 128 6 32 16 8 2 1
6
. Nurmbe osearched
J09, 256
2logn
o(logn)
Camplexitieo O al divide and Conqueu
algarithms:
Algorithmn
dijkstro lg, w, s) {
initializeSingle-Source (g,s)
9 v
while
U extract-in q)
S S uu
Analys is
Thetime Complexily a Dijkstra's
Alqonithmdependo on the wra how
he minimun priori 2Ueue S
wo
imple menteo y2
The Complexi is02) fur narmal
the priory qveue isimplementol
VSing binauy he ap then
heap he
Complexily s of log r)
alASSMAte
Date
Page 46
Aiman Ansan 272
Example 1
2
2
) 1/B
6/A
a)6/
Sep 3: E Veutex
2/4
1/8
A 4/B
Step 4 GVeuex
2/A 1/B
6/E
-
5
sle 10/
Date
Page 47
Aiman Ansaui 272
Step 5: VeutexE
2/A
6
2
8/e
Sep 6 Veutex H
2/A 9/F
B
2
1/8 6/6
(D) 19/
5le
Step 7 VeutexC &/E
3/A 9/p
2 4/B 6/E
1o/H
2
8/F
Slep 8 : Veutex0
2/A
4/3 6/E
10/H
2
2
5/6
8/F
elasSMALe
Date
Page 48
Aiman Ansari 272
Example2
2 70
6 10
6 16/A
16/a
8/a 8/A 14/
Step 3: Veutcx G
22/A
22
16/A
16
(23/4
14/F
Stp 4: VeutexE
22/A 20/E
22
16 / J)23/G
8/A
clASSMA
Page
Aiman AnsaMi 272
Veulex C
Step 5 20/E
22/A
76
23/G
8/P
14/F sirdt
Veutex B
Siep 6
22/A 20/6
22
23/4
AJl6
14/F
Step 7: Veutex D
22/A 20/6
23/4
z/a 14/E
alAsSMAte
Date
Page SO
Aiman Ansari 272
example 4
s/p
Shp 3 Veutex E Step 4 Veutex B
3 3
3/A stb
7/0
Step 5 :Veu tex C /8
s/p
3/A
Dote
poge SL
Aiman Andaui 272
Exarmple 4 2
9
Step 1: Veutx 2/a
Is/a
Step 2: Veuttx b
2/a
2/4a
Step 3: Veutex e
2/4
3
5 /5/b
12/a
Step 1: Veutex C
2/4
3
5 /5/b
6/a a) n/c
elassMAte
Date
Page 52
fiman Ansari 272
Step 5 Veutex
2/4
5/6
6/a
119/c
ASSMALe
Date
Poge 53
Example 5
6
9ep 1 Veutex0 Stcp 2 Yeuter 1
4lo 12/1
4/0
8/
8/0
9/7
1S/7
1/6
elassmate
Date
Page $4
21/5
9/7
9tp 7: Veutex8
4/0 1 19/2
- -
2s
SHep8 Yeutex 3
4/0 12/1 19/2
-
2
2 n/6
/7
Step 9 2/ 19/2
1/0
14/2 21/s
-
6
/7