0 ratings0% found this document useful (0 votes) 136 views21 pagesDAA-Unit 1
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
Design and Amalyed 84 Algosithms
Unit - 2
Tnveodu ctfon ~Algostthm Design ~ Fundamentals %
Algorithms —Corwectness % algosithm —Time Complex? hy
Analysts - Incestion Soxt — Line count, O©pexation count —
Algesithm Design paradigms — Designing an Algoxith
and 6 Analysd — Best, Wosst and Avesage Coase -
Asymptotic notattone based on grewth fun etione-
0,0, 8,10, - Mathematical Analyed - Induetion-
Recussence elatiens — Solution ey re cussence
welations — Substitution method- Recussion tree —
Socen rn plea
Lab! 1) Simple Algozithm — Tneéstion Sost
oe 2) Bubble Sost
3) Recusvence “ype ~Mexye Sort, Linea Seasich
Algesither Destgn Yeradligms: — General Approach to the
Construction 6b ebbeccent
2 Divide ancl Conquer - Mege Bolugons., to pevblems
2) Greedy approach
Wy Dynamte Frogrammtng ~ Optimizing +e Yecussive Approach,
5) Back Teaching
6) Bramch Qnd Bound
D Bate Forceracligrost ®
ingtonce ©6 the Ppsoblem to be
Aygertthm Design Te
DDivide and Conques: Given an
h
Bolved , aplit into Sevesal, Sruiley Bub Inz tances , Bolve eac
ne the
Sb the Bub tMAtances Independently And then Combi
Bub inatame Solutions Bo as to yield a Bolutien fos the
Ovigival ingeance
eq: Binaoy Search, Mesge Soxst
2) Dynamic Prog ramen ng ! Ie & an algertthm destgn method .
: yasu!
Used Ushen the Aolution toa povblem tan be viewed eo
& Feauence oF decisions
eg. Shostest path algozithm , Traveling Salesman poblen
We +ry to Sincl Optimal sequence oe decisions Can be
Sound by maning the decisions One ata time and never
making an exsoneous Cecision
B) Greedy method: The greedy Appsrach Suggests congtructtn
a elution though a Sauence &teps , Gach expanding
a postfally Constsucted olution Obtained so far, until a
complete Zolutfon to the problem ?g wea ched. On each &tep,
tne cheice Made must be
Vteastble — Hasto aatisfy the Prrblern’s Congteinte
aRocally optimal — It has +0 be the best locad chefce
among all feastbie chorces availasie on that Stop
3) izvevocable — Once made, ik eannot be Changed on
Bubsequent Ateps 64 the algorithm
= “Greedy” grab 6 the best alternative availale in the
jepe that a Reguence of Locally optimal Choices
will yfete a Optimar Zolutfon to the entive Porblem,
eg: Priro's Algoriton for mst, Krusbat's algorithr,
for mst, Dijrstra’s Shostest path algorithmh . 2 bars
>Bacictyacking: The principal fea & to construct Boluwens ra
a
Component at a time and evaluate such pantially const™
Jutio”
Canditates as Gollows? Th a Partially cone toucted $°
lems
can be developed further Without violating the ps?
Constraints, it can be deme by taning the $frst vemainey
legitimate optfon pr the next Component. Th there Z
ne legitimate opifon py the next Component;no altenain
Yer any vemaining Component need to be coneidexed
Tn th case , the algorithm backtmacus to veplace the
Rese component o% the pasifally constsucted olution
with Te next optfen
£g. n- Queens Problem , Subset sur povblem
S)Branch and Bound: Tt & om algostthen design paradigm
ushich & generally uced tor solving combinatorial
Optimization problems Ti & basecl on the princéple thet
the total 2er e@ teos?ble Bolutions Can be pastftiened
into Smaller Zubset ef Zolutfons. These grvaller subset,
Can then be evaluated Bystematically unt?l the lest
Selution & Pound
Branch and beund ab a Stepwise Enumeration
ob possible candidate Soluttons by exploring the entre
Bearch &pace tree. Before Constructing the #pace
© an upper and lower bound for a given
dvee, we 4€
c tne optimal Solution. At each level,
mm besead on
pale :
ee nees | enna ae decision bout cohich node to
include in the Solution sett Choose a node with
best bourd)
eg: Assignment Provlem, knapsack Porblen,
ana Traveling Saleeman preble»BD)
®o
@:;
Time Compleatty Analyste:
D) fextten, iro;t--9 a> fostizoy fens t4+4)
% seat; —n A atat; h
’
On) ot
BS} goocisir ten, f42)
i 3tat } = Jo Same for inexement %
% 5,10 or 20
Even though at's nla
Beny= 2B = On)
yy fovcéeo, Cen tat) = (n+
4 ter CJeos janjtp oh a
i start ~ nen en
3
B
(ty Cntid= an? 4onFI
= O¢n?)
forcizor, fens tae)
he
o
2 br c§zoy Retai+y
a Bkrt!
g
14043 tye +
nz nent)
OCr?)Finding Time Complessty 06 an Algovithm:
1) Thatwetfon Count
2> Operation Count C Basie Operations) — Bopmas
2) (Tavle method) Step count — A program Atep &
dejo an a syntatically / semantically meaningsal
Segment © a program that has an Execution time
Independent o% the instance Chasactesistics
Steps assigned 40 any pmogram Begment clepencls
on the Kind @& Statement
\) Comment — Zezo
» Assignment - One
D for-while ,yepeat —uvtil — Consider Step
counts only tps the Contool Post ©, the Statement
The tavie contaivs Sle and frequency
Sle - arneunt by which the count Changes as a result
S execution % that Statement
frea,- total no. && time each Statement & executed
eq
Algpsitnm Sum Can) Ye drequency total stepe
UL gso0, a rf :
for wet ton do 1 ny ny
Ses4ang; 1 ” n
telum 6 1 1 I
% © 2 ua ORE&
$s0q, Lotal Steps
Sle n=O noo n=0 nro
Algor?then Rsum (a) ec 2 ° °
% isa “w saa! tug on
% Cn £0) then ' 1 ' i t
Yelusn 0.0 ' 1 o ' c
else vetusn
Reum(am-Dtati +2 0 ' ° lax
oi ° es _ ° °
2 24x
Corsectnecs o an eulgostttonn s_
Proos)
D Proog by Paduction CDFrect
% Proog by Contradiction
3) Proo, by Looptnvaxient
4) Poot by Counter example CIndtvect Proof)
5) Pro, by eases
&) Proeg by Chain ©, ibe
D Frees by contraposttive
Proos by induction?
) Prove base case & TRoE
2) Assume that pey'n’, tt & TRvE
2) Ty +o prove for case ‘nt!’
4
242 Prove ire acon
ail
Case nals
aye
AssumeTay to prove gor case ott!
ba de (ne) CntttD
ia 2
2 te DEM 2 i4atat + On-bn
fet 2.
(ary) Cntin) 2 adae 4 Crt) #Cnd1)
2
(+0 Co) sitotae t+ ne(ne
2
nA Ont \
fon tra) nt ae nad
= 2
Cnty Cnta) = NCnti+ 2¢nH)
2
(Nh) Cnt)
2
Ls = RWS
Hence proved
Neely esc aa
Iavastent + Something thar & always TRE
Atter $rndtog loop tnvaxtent, we have to pave
1 Tntfalizattes —How dees the Pnavient get Pnittalized ?
2) Loop Matntenance — How dees the Tnvarient Changer
ak each parc of the loop?
2) Texmtnatton - Doee the loop stop? when?
Vroo, by Contrapostttver,
Form the Contraposttive Atalement , Prve Fk And
theseby prove exigtwal as TRIE
eg. ag A then B ADB
Then tL vB=> -va7, 2-ba+5 & even, then x & odd
Contrapositve: I, wc & even. then w*-6xts db odd
Let xz2a then (200 6C2a045
= 4ar-1245 = 2 Coar Za +ad4I
thes 2% -bx +5 B odd
Roos by counter exampet
Tdentify a case case Br fch Our aasumptten &
Mot TRvE
£9. Prove or dis pave
Faay] = Tal ttyl , Vx ¥y Cay znd
ig meh ant yok
Sot Lets
assume th %=-1, 953, %Y> -33-
Hen proved
Froog by cower t
Spitting the problem Pnto Subposits and tsy to
prove them one by one
Prom, by chain ob fbyes
Using mere than one conditions to psve
Prog Vroog by contradiction’
Try to pee that the given Statement & FALSE
and ay the Yesulk % not FALSE 7 then Bay that the
Btatemenk & TRUERecurrence velationss An equation or inequality that desewber 26
dancifon ta terms 06 th value on Smatles “in puts. ¢ Basie HP
Hew te ussite vecursence velations
tor a given code Recursive
at
Y void Vest Cint nd — Th) 4
$ 1 n=0
A Cnr) — Tone
peveg Or rdary — 4 Ten-p+a , 17°
Test tn -19; —Ten-y
f
[Rove Teno 2 te
Vota Test Cint vn)
»
Ten)
§ A Uno) a.
Fe dpvefco beng bee n+l
a : wag”
prirenc pa", ny n
oi
Test tn -05 — Thm -1D
y
th
Tonys Ten-10 + 2042
dl can be appreciated to
Tune ae
Tln-en , 070
) Wid Test Cint n>
— Tw mal
Tonjy=
: Leena
ay enre ! ne 270
XL dectsoptens te
B22 — lon
a partgeryd”, 2;
— login
Test cn-i05,
— Ten-1>
3
%
Tone Ten -1) 4+ 2legnt2
y canbe Approdmated-o
Tons top-rdlegn4) void Test Cint n)
=— Ten)
4 a cnn — |
by Chsos tans tat — n4]
x Bimt } — a»
4
Test Cnj2)," =Ten/a)
a Test cn]a)} —TCn}y
y
Tend= 2ttoI2) +n 42)
AL appsvrtimated to
Tend: 27 Cnjaytn
met
Tome
attnja)tn » 27!
Salva
g¢ Yetussence velations:
D Trevatfon method
2) Substitution method
3) Recusefon tree method
wy Master's thaewews:
Cuvseftutfon method:
Procoduse’
\. Guess the olution
2. Use the mathematical induction to find the
boundary Cond?tten and Bhow that the guess &
Coyseck:D Tend= Ten-p+
To ques the olution, We Can ike forward Aubetitutton
me thee
Toss tTtn-t — @
Ten-p = tTen-2) +1 -@D
Supstt tuting Oin®O we get
Ten) = TCn-a9 +141
Given
Tem: Ten-242 — ©
Substi turing Din © we get
Tims Tin-s)+s — ©
Loowing at ©,®D and ©, it & understood that,
-©
Gbtey K times Ton)= Tln-W +k
wk. E TCO =I
112 n-keo , nek oF
2. © becomes Tone TCn-n+n
TONM= TCODdtn= N+
K=n
2. Teny= OCH)
Step 4% Guess the Solutton, Tin)= OCn)
Step 23 We have to Show that for constant, ©
Timee.n
Traduction Base: Tte)=1
Anduciion Hy polhes& + TCn-D £ ecn-1)
To powe: T(n) 2 en
Ton = Ten-) 4
S cl(n-D+I
= en-c+ £En
Hence prved2 Ten)= et(nfa)tn
To quese the olution,
Tin)= 2T(2)tr -O
TCR) er B)+(8) -@
Subs trating © irO
Tin) = 2for( 2)+C2) | a
Tis ut (2)+an —@©
C#)= 2r( 3) +C§) -©
Substituting Ore
vem] 2103) (2) | +3
Tops er (B)+arn - ©
Compartag ©, © and ©
- K n
Menerat Sqpasten ST ins & (2
weet Tops!
ee eae k= log
. log n I
Tend= 2 T[ Tegn | +nlog n
CS ) iz
To pee that Ttn> 4 e-nlogn
Induction Base ¢ 7 cw=1
Induction +Hypotnesu . (5) 4 C3) 'eg( 3)
WK. Thnd= at C Btn
we(S ) log (2)+n
enlog (2) +n
In
Ol= enlogn- enlog stn
= enlogn-entn & cnlogn
Hence prvecl
5) Tine 3 ' cD n=l
(t(g)t . n>
Stepli Gece the solution’,
Tem= Oly)
Stepa: Pave that the guess 2 Corsect using tnduction.
Tnduction Bases T Ci) =!
Tnductton Hypotrest TAD) 4c CAIs)
To prove © Tek) £c.k & tue | VK en
Tonys oT (3) +I
e2e(2 ) +1
= enti Zen
Hence proved.Recasston Tree Method!
— Maxtng a good guess & Zometimes di gtteuit with
the substitution methed
~ Recussfon Tree method can be usea to deve a
goo quess
- Recursion trees how Rucceactve erpamsions Ot
vecusrentes ustng trees
- Recussfen Trees model the cost & arecussive
algosithm that & Composed g two poste
1. Cost a non Post
Yecussive
2. Cost eG wecurSive call on Smalles Input
ize
- A Tee node Vepresents the
~ To determine the total cost % the
cost ef a Bub-pro blem
Re cussion Teo,
evaluate
2) Cost o& Individual node at depin "i"
>) Sum up the cost op all nodes at clepth "t”
© Sum up all per level costs &% the Recursion
Tree
Excampie at
1 nz)
Teny=
” 2r( 3) 4 n>)
post
Root = Cost @ nom vecursive
TUn= ar (2) +n
TCR) CB )tF
UE) Es) a
af) 270)
n
nas
73) C3)®
- a 2) —2?vn
: ® ~ Jn
Q®& GC =o
+O)
Let TCI) occurs at depth’ k’. ce. Povblem 2728 reduces
to 4 at &* leven
Total cost= Cost © Leah nodes + Cost eg Internat
nodes
Total cost: (cost of a leas vede x Totad vo. & Leay nodes) +
Sum e, costs at @ach level & Intesnal nodes
= Le+I,
Fer this , we need to find the value 6 K in terms of >
Let eh
The cost % a leat node = Toe
No leay neces at ‘kt’ level = aX nodes
leg rosy?
*. Cost o leag vredtes (LF 2 =n
Le§ Lesan:
— Bese og Internat node =n at each level
No. % levels of internal nodes (ote k= K levels
Ige kenTotal Costs Le+Te
Tin) = nlogn tn
Ten) = OCnlegn)
\ :
eee
a (3) E02
vy TUn)= ot (2 jan
3) 1) *(3)= or(%)*
¥ Us)ea(S)
n
oo ~ Ler ug assume ab Kk lever ,
i Gy the porblern reduces t0T C1)
OOO w Geo
J > * sl pine a” =>
n& - Se 0
2 oe 2
(§ ( 4 2
2aT ethel
es
Teta no. &% lea nodes at K' level =2
\ :
login = i
oe oye (4) (4) (a)
Simce et cit & deeper
Lee Ki Roveh & the Rast Levet whexe TCID=!
on — leche 2 @ 2%
3
c Bs level + > ¢.a'n
su
3B
o
c29 > lwa2 iy eon
3 Bz
k
2. Ae kt levet an\=}
oe
2Xn ct >
2k K
n= 2
vy
2
E k K
logn = leg (25) = log 3 aleg 2
=) Klega - Klos 2
togn = K Ie5(2,)
Ke Neg.
veal)
k = log nh
a
Totar vo. 0% nodes ak last bok = a”
ig
TTorad Cost of dast leveb= 9°) - |
1 2
=n (2
ee *@)
Yotak .- © Intesnad nelos = n- K
2 nto
1. Totad cost opthe trees n 8) yy log
‘3
Z[sted bere: ®
—Applicatie Yor vewussence velations oF, the form
Tlnde at(B)tstn) , azi bel
There axe 3 Cases
Case dt Ip fen) = O(n 4°" ©) , €>0, then
Ten = © (ns) > “ominate the werk
Case a’ Tp fen) = OCn 8) then
log & Both no-e4 necies and
Ton) = O(n d log wm) a> Fem oe
coos at any level oe
Cue 38 Th feny= 2 Cn) 22
aF("/e) 2c ftn), @ 70 .cK Wosk on a level
Tiny = ©( fem) dominates
Eo
cB aeny= et ( 3) +1000n?
Comparing with TUnj= ar( 2) +Fen ,AZ1 b>I
Q=@,b=2 , ftny= 1000n?
Substituting the abeve in case 4
fen= O(n ‘aya-*:) =p log, % = log = 2
= O:tn i)
Th el, then PCny= © (m7) = 1000 n®
So, Case 4 holds
te Tends O(n %*)= ©Cn?)
Tin)= ©(n3) |Egt2! Tne ar (2) ston
When Compaseal with Thn)= AT (3) +n), axl, b>:
heve a=2 ,b=2, ftnd=1on
log a = log m= 1
Subattiuting the abo values fn cose 2
FOnd= Gln ?%*)
$en= OC n')= &Cn)
lon = @Cn)
Tha coze holds
e+ TlM= &Cnlogn )
Fgi8) Tome an (5 +n?
Wher Compared with Tn) = at (B)rFem, Atl, bl
heve @=2, b=0 y gCnzen®
log as log 2 = 1
Supetttuting the above Valuct tn Case a
+€
ene 2% Cn Po” >
rte)
= Gn ip €s!
then }$tnjy= 2 Cn?)
Hence Coase 3 holds
Cheching Second condition
2$(M/o) Se. 0?
e (ay 4 e.n®
ee BhiTh we choose ‘clas 1 , then
a
n 4 ne
zt Tt 2 True
Tem: OC Hen)
ah. ends OCn7)
Othey eramples*
Cases Tn)= AT(Z)4n
ce f Ten) = oe 2 yal
ase 2 n) C 2 J
Case 3% Tlnd= BT C2) +nlogn