0 ratings 0% found this document useful (0 votes) 23 views 32 pages Design& Analysis of Algorithms Home Assignment
The document discusses the performance of recursive and non-recursive algorithms, focusing on their time and space complexities through case studies, particularly the Fibonacci and Binary Search algorithms. It highlights the advantages of non-recursive approaches in terms of efficiency and memory usage, especially for large input sizes. The conclusion emphasizes the effectiveness of non-recursive algorithms for large-scale applications due to their reduced overhead and better performance.
AI-enhanced title and description
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
Carousel Previous Carousel Next
Save Design& Analysis of algorithms Home assignment For Later Yr.
Analysts | Nor
ntoducHon !— a .
Overview > Thu case study alms to
co e 5
———j tempat the piupoumance of secur ve—
{ ve t L Jems,
I ; i :
_o| ttme comnlextty , space complextty 2 |
| -puracktoatl use cases.
I
| Deftnttton i
A) Recuueive Algauithm im A functton that
; $
calle ttself to golve gmallex tnstances
oy the puoblem unkl sacking a base
i case.
8) Non Recussive Algeutthm :- An algorithm
b h olver the l tng thewative
approaches such os Woops, utithout self -
aalling - - eee
abg ast thmsr—OFtbonacel
__ © Binatwy Ceauch _2 Methadalogy =
tthms j= ecutive non.
Tisuuive vemians ofthe elteuaing_
al (Hams: Frbonacet 7
© Bina Geaueh
+ Metures Evaluated :— PoC
~ Time Comnlextty — Theauetteat analysts
hour e i Mme geoues tuuth
{| (ze.
= ace lext emomy te
_tton , poou tng on seecuurtve stack “depth
jou mecurive algautthms =sssaaens
Pee
= Execubton Time > Meatued actiral
i) t im ‘n thon ‘time’
—}—_____
=| Memoze Use.ge = Monitewed memory
-conaummplt upton —ustng ‘ teacemalloc’
—
—de, Wbonacct_mecuusive (nr)
ty nad:
sectuun n et 7
Stet Uwn pibonaccl’ seecusesrtve (n-4) |
+ pboonacd seecuestve (n-2)
aime _complentty i= 0 (2) i
Cen cent due to sedundant ealeulats
Space comnlextty : o(h) BT,
Duaurback : Slow paw large n, tmpvac
Weal aulthout optimization (eg. memol~
zation). at an
_Non- Recususive Code
| dep prboracet_ non.sec(n):
a,b = a.
fou in stange(n): 7
_o,b-b,atb
JeekUe nn
——— — FyTime comp lexrthy = otn)
Space tom pleatly = 02)
+ Peformance = yous ote Lauger
value foe nm, seus tn Leeten Linear
Hme.
Result i The non-seecussl Ve &'bonacl
ies oxponentrally pas bee fou Lawge O
and ser Legs memory:
>
Binauy Geastech =
Recuustve Veeston Code :-
| def bingy. ec (ase lous, high, 2) ¢
vy high >= low
—
T
|
mid = Chigh + low) //2
uy que Prd] == %
Jerbusen med
oly auulmid] > x
mid-d , x)
ee yetuun, bun dy sec Come
soft
| ebee :
Subuun -1
Li
}+—,
El
eee)
a
See buen brow - sec Coss low oe
—
mid +t, Acgh pO |tH
+} high =o, tenCaue dd
uihtle louse high :
mtd =Chigh +lou) [J2
Ub ame Cmitd]==2:
Jertuun med
eu, gtth i med <2:
fours mid tet
else:
hugh = med -4
Jorbuen ~t
+ Purl memoance —> Both tmnplementabion
have o(logn) ttme romplesity , but the
{I
Non- uecustve version user leer mem
— Ct) ot the sucueiton velion used
(log n) apace foe Stack peandes.
+ Result:— he Mon-MeCUMIIERVE Yereston,
Le roaue naemauy epitctent uritowt
sa VuUliang A Uitarsena ge anaes eeeeaeeecu ne Rec cor
Algo Recustve |Non-Ree | MEX} None
' Time time | Space Space rc
Tseey (sec) (mB) MB)
@ i e
Frbonacd | 2.24 0.0002 2.0 ot Fi
(n= 35hioo» #
© Brnawy| 0-004 06-0008 3.0 0.05 |
}
JSecrech foe
5. Concluston i=
ei ec tve al th @ olben caster
to implement and jollours the natusel—
sbuuckuue of the puoblem Cogs Ei
Etbonacet , Brnasey Seasech) . Houreves —
th ey bend to use masse memory due —
to shack swecusecion , 9 can be enefpreient—
tose Losge input Gtzes as seen in fi
Ybona lg ase :
Rl fibenace’ aloauithm,
s | Non - sree ive 4 et
—t—Nen-wetuusive algorithars cee gents
ally reve elfievent tn bow met ol beth
1 Hime + apace They cavotd the overhead +
of _srecuseston , ledading ts betlee ———?~
ume Usage. “This maker them meth
Surtable \os« louge scale apptica Hen -~
a aSpectally tethen peefoscmance § mematery
pitmixatten atte Impetetant. vgn Souttin. g_alquie :
Qludy 2: Design J h
He Case 7 = and Conguese ,also
ustn ne |
develop golutrons for -pucblen using |
eeod yu Method. _—_______
ro Shute Methed. +
2. Tntuoduction i sescting ts a gundarne,
“yal puoblem tn cormputer sete ce uy
—_utheme. the obseckive tbo _amecn ge — |
_olunven tt af _o LUE ate ateaty LOO Specie
aude , typteally in_mon-dermeastng
ge non-tnceeaging audee. Tn this cose _
_study , we dutgn o satting alg crits ;
wing the Bivide ord Conquele paso
darn - I
One of the moot widely Known gautiny |
algauith mn, tn thir coteqauy ts Meuge —
Gayt , urthich ep pecren tly arty deestolys
ue cussively dividing them nie
amallue pouts 2 mewatna thre sotcted
Suh ULE.
2. Paoblem Deliniton Deetgn a saueting
—algari thin that atuanges o gqtven -
. ’ : 7
—— eso ef integer tn non-decwe air
——,widee _utihg the Bivid ws
EU shpiasarh ui Tee ee~ An aseseory ap integuu One fale =
Tay tengih_o. oe
“Output : — AL touted AEULOLY thenon= —_
deoreasing audese.
Divide and Conquue Approach = _
The Divide and Conquese, pated ign
bueoks down oo omnis puto b Lem
into emallee , mauve manageable
subpuoblena , solver each tndenendently
and then combinu thetu sol” to solve
the outgind puoblem. Menge saelt
bon vdeo eg a} applying this _struate-
gy ts sauktng.
Bivide Phase: The alqassi thine olivides
the owoy nko halves
+ het mid= n/l2 Ctntegee division).
+ The ay t& divided (nto a Lett
eubauuay gue fo.--mid-4] and a
sught gu ate cy que fmid...n-t].
|
Conguve Phase im Recut ively aprly :
ee ane mine J sught
Fach secusetve Coll pevetbex
Lavvedes the ub quays Unt they
t contoin ek only one element Chase case), |~ whch _¢g_enbewently ateled
= — Combine Phase :- Aftes gmting both
a the yt and sught aubdaseoue , the
c= IM Ue ges them to poren
_ to “atngle outed st Ott “The mae
tp involvec eompaeiing. the smatlet
| F |
elaments sf beth subsea § placing |
the cmaller element nto the ftnal
oa alg auth
E ALLOY « ——— — -
&. plgeutthm = “The Monge oort algorithm
joan 6 tbed rn el
. [poeudocode 1 _
ty let >= stight: 7
| Sut UE
t | mid = (Cloyb+ sight ) s/o
Mesgefout (auw, left ,mrd)
me MemgeSout Case , med $a, sulghlt
Muege (Cause, tle eoid julght)Testu.
1 Otte =
L38, acy 43, 3, pee 2) OM) aes
T_7
Tl) Divide 2 Splee the omeea tno bure
halves: [98,27,48] 4 [,5, 82,107
Holl :- [aa, 24, 43)
Tenis into [32¢] and [24,431
Lat, 143) uy tuubhee nlite neo Lot] ts]
[Right Hot, [3,9,32.82,10]
_8plvb into (3,9] 2 [82,40]
[3,37 is split two C33 4 [4]
| Te2,40] cs eplte into [a2] 2 [20]
ou : . t
_oustodye uchich ate inherently coreted.
© bv e ee
g [ee
t. Muuge Cot] 4 Tas] bo gece sod a aieeeeeee
2. Menge [92] woth [23,43] to get =
fot, 38 43]edge, Dod Muuge [al_and a 7 = a
L Muteg | Cioj to get bios)
a Mate i ee aie gotta, ion,
_& Final Meuge = ae er
: (2,9, 10, 821 to get
j La,g 10,99, 38,428 824 7
E Raretid lu wary a
s —
# ims omplesity olnalytit =
(nto tue halves , uilteh takes
constant tina. O(t)
Conquty atep — Seuting the ture haly
2
Combine Strep i Mewging tue
———— holt taker o> o(n) time
—The ousuoll Hime tonoplest ty ah |
Marge Seton tog.) ili ia
+ Sno Complexity :— Me Aue 1
. alto toput axe duc te the naeu ging!
re a oln)
| ee6 Gorlusion i= Meg: faut eyjrckiveley
——dirarnitiaty ths Bivids and tongues.
1 atuateg $4. aheurtasing oi spice
—-and_urbusrtnins jeu “yeubing towks
“pap 2.
2) Tntuoducheon 2 The Cte y Wuthed =
Wion algeuitors oprioath that builds
——4poicclubion niece by prece, alias |
chocetna the negt of
fcit troroediaty bentt .
Tn tis cose ghidy is _evnlewe bor
ke develop ioluttons us ng toe (seit d
Method ecuting ono. ola pie Lem 4
Ackiviky Qelection Problem
2) _tueblurn Sbatement = Ben,
wing Poe CGuedy Methed te zalve
tae aaa Sal ton Patoblero 2 given
Bo at eae sith rkaut pe
i bern tlitt the mostimum
Fee fy mene eventing ack vitter thot
can be pisjeuroid by o singdy puLeionF
3) Algauithm bag t _ AcktvEty. Sebtuton, “Ty
Puoblema ____— ne
- An : iy
au sant i eat Hatt th
Ta ee ti yaa aceag
Hhot stots alti toe lost selocted |
ackivity ends
Qbeps i=
a Toput i A list of ocbtvibes each
uit etait 3 fintth. tHtnoe
2 Outpub : The masinum Op. of +
i Non- wove opp in ackty (Hes rey
the othivittes that [vnihe the |
ths puevveurly grlictid ockivetier. ~
4) Pseudo code i—
i Lyi r Hyites :
| Sout ackivittes by tin ah Hint >
Fy
Ci icine7 Hoty Stasebs aftrre aso sushi /
g jth Lost ebimeemi elected acttetey —
| —binittres : Se z
i
i — — _-
ae activiy name | staut tm [enum]
+ Hme_ |
| at 5 9
ad 1 2
+ a3 3 4
L ah ° 6 a
| a5 $ Ks 4
ae & g
—Lobsective —» To compute dn execuHon
pli Sout the gtven activites tn ascending
oudise, according to thette paces a |
tHe
Hy { —
ad a
ag
ay © ea + |
p
ee
g 4 [
|
|.
Steno: Se i the pueot —achivi by} pion s.aséied
anuway actfI and add (tt to the salty)
» otuuroy , thus gal = $024 a
Steps. _Kepoat the step 4 & 5 jose the
5 wematning ockivities un actC
Atlocked achivrby ta giteotie, thon as
eguol tn the pnith tras « the
eer add rt to golf
Stepp: Gelect the nevt aebivity a octet
Luin add aa th the
Thus 401 -for ast
ol” aek.
- added +p col set
|
|
ic @eleck a5. stnee sCas)> 4 (add
25 gets added thus, sol $2,038,055 -D-Cdect ad. since sCat)< (a5) ,at nob
_, Stne s(ae) > as) this«te fe lextby 3 : -
Tire Comer 4 dod Ut oh Betty
5),
yuovl
zauted $Y by thete
ro_= oCn)
ieape it: When
i alueody-
. pint bime then
Where poo (ded (rat it not
Coge 2+
then Te* oC Legnd.
antled
spa compleatty -- o(1) , afnce in
autor Syroce 1 sit q ud.
Contluston i= She Cpredy method
4 POLOM LdAd Ob quick and eypraent i
i solve oplinitigation peta blo rves
Whe the Aellvely welectton tweob la
oby mdhtng the weally oplinal
y Chetce ab oath alep.VC ee
_ finding the dlitance — bet? district of
» Mohweathbeo 2tode using Bynarnte
apttmixzatton technique that solves peoble!
by bueaking them dou tinko strapless
gubpttoblems: and steserng the seg ult
eo} thee subpoblems te_avatel aeductant
compu tobisrs .
I Le l velo
| bet” voutous distercl: tn Mahaseoth tea
on. ‘alguien bo FaPRE the sharttert dita
[abate wing Floyd=Waxshall alge! tho
dy moanate appt o ach .
a closite eg. of a ma
| pevdop an_algauithm | ro eonpute he the
: I ahoutut dittances beturcen all paced ef
| digturct: tn Mohavorhtua state utting
“the dutancer between directly connected
Poll a taal aece ete ee CeCe
| “paroblem Statement —2 _Outnut = The tnol distance mate is
uitll puevide the showtert oigtanere
boll poles of dirtutete
. Stops :—
2 Outpuk = A moatuse contatning the b
a —lietatts i
_diztonee ft)
|
fl= natin (distance [Iti] —
aTan gsumdacode = —_____
| TT pinyditauehall Cdictuuete On te ie nee a
en ie D uth
eS | tnrbalrze dutance 1 on ote
ditrect dittonees |
E poh ro disteets :
pou f cn clisteercts + . al
L ose fun ctatuiets :
tH pf citj1= min(ptritja. piv ite] +
oe IfjJ) |
eben,
— D ; =}
5)._Bxoraplt != :
+ _Criven the distutete and distances = —
:
4 mumbat fo Pune +449 km — [ :
I Mumbo th Wath deen
a Pune to Nathtk : 993 kin a
4 Pune ko Nagpise + 860 km ____
4 Nashik to Nogpuse : £526 km __ : ~
4+ Noshik tr Aweangabad :250 Km |
Nogpux ts Auseangabad +984 km.foe
Lo,4149, 169, rnp try
Ci4g, 0, 293, 250, vn} I,
Lie, 998, 0 526,250),
Tin}, 860, 626,0, 289]
[inj wn} , 260, 289,07
~ ernal
i
Bistonce Matusp im
D =f £0,149, 166,670, 446]
Tihg,o,293, 860,623]
L166, 228, 0, 526, 260
[ 240, 50, 520, 0,289)
£416, 623, 26o og¢ 0A Eee E
witho, —wint_in_olv8) ian
yh i Sniennr of dirbeicts clue bo
_ the Hobee nested Loops chew ating ovue
—-all_potes of dibuictr jou —o ach tnbee-
mediate noderieduberct: oe
_ 4) Conclustoni—
The Floud-Warehall alg au! Hh na
—
_dynomee pus gieanamtng 0 poding i
| show
| pothi, it puosvrde oa complete sal”
ae distance colculotion ameng a
nebiiowk of & nodes (in thir cove, ditutctr)
]J
|
|
I
Come Study DUS an al gesthon |
. [i tng— Bat ktuacking—pei SS slubien a}
jolt “Kn apea th pee blem.——
___|| se old Ko wok aueblemr to
Tait om binotecol—obpptiotkgalien
| puoblem. The —eboa [lenge th to elec
Tystol value without etceedting—a—
——4h |
oan olthee be tnoliided to the Kbap-
ou excluded (wenuesented bio wo lite
of oo) Thit puroblenr has applica beds
and tegistrer.
2) Puoblino_skatenrenk =
re spe lienas bua ibd
—— hor oa _enecrpic orerght yaleinthe knapsack uutheut exceeding
the wut gt t capacity.
eee [xtgasei thm Quign =
_t Barkteocking — Anporanon :—
_ 4. Baling o uecumive approach |” by
_eeplone oll combinattora af there
a At eoch step, dedde wushethew te
include ow ewclude the cwurent tlenn
A atk —af the nue fetol wight
|
ie rrr e Ww, “backbeack
2a ae meen es oe wheneves
ee ee
ES tliat ere a copa by:
Brot = Ceo
ECOG roe ee ei Oe een
SESE eg apres CeCe ee Qh) nsec te
Fe
eaten ea (nelle
‘Cauauind Wa'ght=o, cunt value).
Explaree ae iamabin eA peBlt _-.4 77st t*. -.
(0,0) —__
oS
i tiset —__
(60,10) Co,o% ag
raed tat / AF
(Ga,40) (100,20) ae
(460, 30) rast / i
q3:t 13-0 teen 5o) aa
(280,60) (160,30) |
% v
B) Patudocode!—
punetinn Knapsack (kent, nw)!
moar Value =o T
4 function barktuack C1, ose ue cue valle
Ty fe=n: |
1} ou Ur <= nl: |
— av = |
cue valued ++
| Su butBs eae ae
op slaeuithm rpienty
ezydat all earsbin wiles 6) Cito
gy te 6132 Enopeack otegblerm
ding 2 oO
ats. pars. cusvided items £ fant
feriert
rk. et
4_enst itid deroonsty is
a the barktyarking6) Trme Com 1 al a
The bs ener age thm a esees
oll patitble combtnattons of ntkenat,——
_ ti beading tea —useuth sate HE comple: |
ety a, o(27) ote them. hot tuto |
+ lhtle thi: appeoact guauantees hat.
the aptimal sol? wittl ie pound , vk can.
be tneppirent {ase lavge aluesof no
_due to ox pontential gra uth tn conaputa:
tian Hime
+) Concluston i=
__“The backtuarking algacetthm opp
_exploset oll eambinpitens o4 cheno
how tre old Knapeack puab lem
_yetlding oa moaaeimunm yalie of
| optinaizgation artoblems dospite ee
p———fLext
rt —— ~ ~——--—— ——~
i
t—-- oe “Case Seudy —
solving the Sia
onl ya >
1. u i bt Fal
é
con gwen Hol genemataws Chess) t
— Hing ptutde-wandent number
atte pumintid by a secutuens
uation
Xn+i = (axntc)mod m _
wi este
X th the
OU Mnb ee
Pitt 4c tae tnesement =I
Sequence of peeida = sednden—