DCA7104 - ADA Internal Assignment
DCA7104 - ADA Internal Assignment
i i i
INTERNAL IASSIGNMENT
Ans: i1
Stack iSpace iUtilisation iin iRecursive ivs. iNon-Recursive iAlgorithms: iAn iAnalysis iUsing ithe iFactorial
iFunction
Introduction:
The iconcept iof istack ispace iutilisation iis icrucial iin icomputer iscience, iespecially iwhen iunderstanding ithe
iperformance iand ilimitations iof ialgorithms. iThe istack iplays ia ipivotal irole iin iboth irecursive iand inon-
recursive ialgorithms, ibut itheir iusage ipatterns idiffer isignificantly. iA ifactorial ifunction iserves ias ian iexcellent
iexample ito idelve iinto ithis itopic, iillustrating ihow irecursive iand inon-recursive iimplementations iutilize istack
ispace idifferently.
Factorial iFunction:
The ifactorial iof ia inon-negative iinteger in iis idenoted ias in! iand iis ithe iproduct iof iall ipositive iintegers iless ithan
ior iequal ito in. iMathematically:
}
return in i* ifactorial(n i- i1);
}
ilocal ivariables iand ireturn iaddress. iThus, ithe istack ispace icomplexity iof ithe irecursive ifactorial ifunction iis
iO(n), imaking iit isusceptible ito istack ioverflow ierrors ifor isufficiently ilarge in idue ito ithe ilimited isize iof ithe
istack.
ionto iand ioff ithe istack. iThis iapproach isignificantly ireduces ithe istack ispace irequirements. iInstead iof igrowing
ilinearly iwith in, ithe istack ispace iusage iremains iconstant, iindependent iof ithe iinput isize. iHence, ithe istack
ispace icomplexity iof ithe inon-recursive ifactorial ifunction iis iO(1), ioffering ia iconstant ispace isolution
Performance iOverhead: iRecursive ialgorithms ioften iintroduce ioverhead idue ito ifunction icalls, iwhereas
inon-recursive ialgorithms, ias iseen iin ithe ifactorial iexample, ican ibe imore iefficient iin iterms iof iboth itime iand
ispace.
Clarity ivs. iPerformance: iRecursive isolutions iare ioften imore iintuitive iand ieasier ito iunderstand idue ito itheir
iinherent iself-referential inature. iHowever, ithey imight inot ialways ibe ithe imost iefficient iin iterms iof istack
ispace iand iexecution itime. iNon-recursive isolutions, ithough isometimes imore icomplex ito idesign, ican ioffer
Conclusion:
In ithe icontext iof ithe ifactorial ifunction, ithe idifference iin istack ispace iutilization ibetween irecursive iand inon-
recursive ialgorithms iis ievident. iWhile irecursive isolutions ican ibe iconceptually isimpler, ithey imight inot
ialways ibe ithe imost iefficient iin iterms iof istack ispace iand iperformance. iNon-recursive iapproaches, ion ithe
iother ihand, ican iprovide ioptimized isolutions iwith iconstant istack ispace irequirements. iUnderstanding ithese
idifferences iis iessential ifor idesigning iefficient ialgorithms iand iavoiding ipotential ipitfalls iassociated iwith
Ans: i2
Proposal ifor ian iEfficient iBook iSearch iAlgorithm ifor ithe iUniversity iLibrary
Introduction:
To iaddress ithe iinefficiencies iin ithe icurrent ibook isearch isystem iof ithe iuniversity ilibrary, iespecially iduring
ipeak ihours, iit's iessential ito idesign ian ialgorithm ithat ileverages ithe isorted inature iof ithe ibook ititles idatabase.
iA isorted idatabase ican isignificantly iexpedite isearch ioperations iby ienabling imore iefficient isearch istrategies
1. iOverview:
Binary isearch iis ia idivide-and-conquer isearch ialgorithm ithat ioperates ion isorted idata. iIt irepeatedly idivides
ithe isearch iinterval iin ihalf, icomparing ian ielement iin ithe imiddle iof ithe iinterval iwith ithe itarget ivalue, iuntil ithe
idesired ivalue iis ifound ior ithe isearch iinterval iis iempty.
a. iInitialization:
Set ithe isearch iinterval iboundaries: istart iand iend.
Calculate ithe imiddle iindex: imid i= i(start i+ iend) i/ i2.
b. iComparison:
Compare ithe imiddle ielement iof ithe icurrent iinterval iwith ithe itarget ivalue.
If ithe imiddle ielement iis iequal ito ithe itarget ivalue, ithe isearch iis isuccessful.
If ithe imiddle ielement iis igreater ithan ithe itarget ivalue, iadjust ithe iend iboundary ito imid i- i1 iand irepeat ithe
iprocess.
If ithe imiddle ielement iis iless ithan ithe itarget ivalue, iadjust ithe istart iboundary ito imid i+ i1 iand irepeat ithe
iprocess.
c. iIteration:
Continue ithe iprocess iuntil ithe istart iboundary iexceeds ithe iend iboundary, iindicating ithat ithe itarget ivalue iis
inot ipresent iin ithe isearch iinterval.
3. iPseudocode: i
function ibinarySearch(arr, itarget):
iiii start i= i0
iiii end i= ilength(arr) i- i1
Directorate of Online Education
i i i
4. iAdvantages:
Efficiency: iBinary isearch ioperates iin ilogarithmic itime icomplexity iO(log in), imaking iit ihighly iefficient ifor
ilarge idatasets.
Optimized ifor iSorted iData: iThe ialgorithm iis ispecifically idesigned ifor isorted idata, iensuring irapid isearch
ioperations.
Reduced iResponse iTimes: iBy ireplacing ithe iinefficient ilinear isearch iwith ibinary isearch, ithe ilibrary ican
isignificantly ireduce isearch iresponse itimes, iespecially iduring ipeak ihours.
5. iImplementation iConsiderations:
Database iMaintenance: iEnsure ithat ithe ibook ititles idatabase iremains isorted iat iall itimes. iIf inew ibooks iare
iadded ior ititles iare imodified, ithe idatabase ishould ibe ire-sorted iaccordingly.
User iInterface: iIntegrate ithe ibinary isearch ialgorithm iseamlessly iinto ithe iexisting iuser iinterface, iensuring ia
iuser-friendly iexperience ifor istudents iand istaff.
Testing: iConduct irigorous itesting ito ivalidate ithe ialgorithm's iperformance iunder ivarious iscenarios, iensuring
iits ireliability iand iefficiency.
Conclusion:
By iadopting ithe ibinary isearch ialgorithm, ithe iuniversity ilibrary ican iaddress ithe iinefficiencies iin iits icurrent
ibook isearch isystem, iespecially iduring ipeak ihours. iLeveraging ithe isorted inature iof ithe ibook ititles idatabase,
Directorate of Online Education
i i i
ibinary isearch ioffers ia ihighly iefficient iand ioptimized isolution, ienabling ifaster isearch iresponse itimes iand
ienhancing ithe ioverall iuser iexperience.
Ans:3 i
To itraverse ithe iprovided igraph iby iBreadth-First iSearch i(BFS) istarting ifrom ivertex i'a' iand iconstruct ithe iBFS
itree, iwe'll iutilize ia iqueue. iThe iBFS ialgorithm iwill isystematically iexplore ithe ivertices, ivisiting ineighbors iat
ithe icurrent idepth ilevel ibefore imoving ito ivertices iat ithe inext idepth ilevel.
Graph iRepresentation
Vertices: i{a,b,c,d,e,f,g}
Edges
(a,b)
(a,c)
(b,d)
(b,a) i[Reverse ifor isymmetry, ithough iBFS iwon't irevisit ia ivertex]
(b,f)
(c,a) i[Reverse ifor isymmetry]
(c,e)
(a,g)
BFS iTree iConstruction i(Edges iwill ibe inamed ias ithey iare idiscovered):
1) Start iat i‘a’ iand ivisit iits iadjacent ivertices i:b iand ic
• Edge i: i(a,b)
• Edge: i(a,c)
2) Dequeue i'b' iand ivisit iits iadjacent ivertices: id iand if. iAs ia iis ialready ivisited, iit's iskipped
a) Edge: i(b,d)
b) Edge: i(b,f)
3) Dequeue i'c' iand ivisit iits iadjacent ivertex: ie.
Edge: i(c,e)
4) Dequeue i'd'. iSince i'd' ihas ino iunvisited ineighbours, imove ion.
5) Dequeue i'f'. iSince i'f' ihas ino iunvisited ineighbours, imove ion.
6) Dequeue i'e'. iSince i'e' ihas ino iunvisited ineighbours, imove ion.
7) Dequeue i'g'. iSince i'g' ihas ino iunvisited ineighbors, imove ion.
After itraversing iall ireachable ivertices, ithe iBFS itree irooted iat i'a' iis iconstructed iwith ithe ifollowing
iedges:
(a,b)
(a,c)
(b,d)
(b,f)
(c,e)
This iBFS itree irepresents ithe iorder iin iwhich ivertices iare idiscovered iduring ithe ibreadth-first itraversal
istarting ifrom i'a'.
SET iII
Ans: i4 i
Certainly! iThe iknapsack iproblem iis ia iclassic ioptimization iproblem iin icomputer iscience iand
icombinatorial ioptimization. iGiven ia iset iof iitems, ieach iwith ia iweight iand ia ivalue, ithe igoal iis ito
idetermine ithe inumber iof ieach iitem ito iinclude iin ia icollection iso ithat ithe itotal iweight iis iless ithan ior
iequal ito ia igiven ilimit i(capacity) iand ithe itotal ivalue iis imaximized.
Directorate of Online Education
i i i
Capacity/Items 0 1 2 3 4 5
0 $0 $0 $0 $0 $0 $0
1 $0 $0 $12 $12 $12 $12
2 $0 $10 $12 $22 $22 $22
3 $0 $10 $12 $22 $30 $32
4 $0 $10 $15 $25 $30 $37
For iexample:
To ifill ithe icell ifor iCapacity i5 iand iItem i4:
We ihave ia ichoice. iWe ican ieither inot iinclude iitem i4 i(taking ithe ivalue ifrom ithe icell iabove, iwhich iis i$30) ior
iinclude iitem i4 i(which irequires i2 iunits iof icapacity, iso iwe ilook iat ithe ivalue ifrom iCapacity i3 iand isubtracting
iitem i4's iweight, ii.e., i$22 i- i$15 i= i$7). iThe imaximum iof ithese itwo ichoices igives ius i$37, iwhich iis ithe ivalue
iwe iput iin ithe icell ifor iCapacity i5 iand iItem i4.
Thus, ithe imaximum ivalue ithat ican ibe iobtained iwith ia icapacity iof i5 iis i$37, iand ithis iis iachieved iby itaking
iitems i2, i3, iand i4.
Ans: i5
Directorate of Online Education
i i i
Dijkstra's iAlgorithm:
Dijkstra's ialgorithm iis ia ifundamental ialgorithm ifor ifinding ithe ishortest ipath ifrom ia isource ivertex ito iall iother
ivertices iin ia iweighted igraph. iHere's ia imore igranular ibreakdown:
1. iInitialization:
Tentative iDistances: iAssign ia itentative idistance ivalue ito ievery ivertex iin ithe igraph.
Set ithe isource ivertex's i(A) idistance ito i0.
Set iall iother ivertices' idistances ito iinfinity.
Priority iQueue: iThis idata istructure iwill ihelp ius ikeep itrack iof ithe ivertices ibased ion itheir itentative idistances,
iensuring iwe ialways iprocess ithe ivertex iwith ithe ishortest itentative idistance ifirst.
2. iMain iLoop:
Pick iVertex: iExtract ithe ivertex iwith ithe ismallest itentative idistance ifrom ithe ipriority iqueue.
Update iNeighbors: iFor ithis ivertex, iconsider iall iits ineighbors iand iupdate itheir itentative idistances iif ia ishorter
ipath iis ifound ithrough ithe icurrent ivertex.
Tentative iDistances:
A: i0
B, iC, iD, iE, iF, iG, iH: i∞
Main iLoop:
Step i1: iVertex iA:
Consider iA's ineighbors: iB, iD, iand iE.
Distance ito iB ivia iA: i0 i+ i4 i= i4. iUpdate iB's itentative idistance ito i4.
Directorate of Online Education
i i i
Distance ito iD ivia iA: i0 i+ i2 i= i2. iUpdate iD's itentative idistance ito i2.
Distance ito iE ivia iA: i0 i+ i7 i= i7. iUpdate iE's itentative idistance ito i7.
Step i2: iVertex iD:
Among ithe ivertices iwith iupdated itentative idistances, iD ihas ithe ismallest i(2).
Consider iD's ineighbors: iG iand iH.
Distance ito iG ivia iD: i2 i+ i1 i= i3. iUpdate iG's itentative idistance ito i3.
Distance ito iH ivia iD: i2 i+ i4 i= i6. iUpdate iH's itentative idistance ito i6.
Step i3: iVertex iB:
Among ithe ivertices iwith iupdated itentative idistances, iB iis inext i(4).
Consider iB's ineighbors: iE.
Distance ito iE ivia iB: i4 i+ i2 i= i6. iUpdate iE's itentative idistance ito i6.
Step i4: iVertex iG:
G ihas ithe inext ismallest itentative idistance i(3), ibut iits ineighbors iare ialready iprocessed ior idon't ilead ito ishorter
ipaths.
For iinstance, ithe ishortest ipath ifrom iA ito iB iis iA i-> iD i-> iG i-> iH i-> iE i-> iB.
Time iComplexity iAnalysis
The iTime icomplexity iof iDjikstra’s ialgorithm idepends ion ithe ifollowing ioperations.
Directorate of Online Education
i i i
Extracting ithe iMinimum: iThis iOperations irefers ito iselecting ithe ivertex iwith ithe ismallest itentative
idistance ifrom ithe ipriority iqueue. iIn itypical iimplementations, ithis ioperation ihas ia itime icomplexity iof iO i(log
Relaxing iEdges: i iFor iEach ivertex, iwe ineed ito iexamine iits ineighbours iand ipotentially iupdate itheir itentative
idistances. iThe inumber iof itimes iwe ido ithis ioperation iis iproportional ito ithe inumber iof iEdges, iE
According ito ithis igraph, ilet's iAnalyze ithe itime icomplexity istep iby istep.
V= i8(A, iB, iC, iD, iE, iF, iG, iH)
E= i11(Number iof iEdges)
Using ithe iformula
O(V ilog iV i+E)
The itime icomplexity ibecomes.
O i(8 ilog i8 i+11) i= iO i(24)
Thus, ithe itime icomplexity ifor iDijkstra’s ialgorithm ion ithis ispecific igraph iis iO i(24), iwhich isimplifies ito ia
iconstant itime icomplexity. iHowever, iit’s iessential ito inote ithat iwhile ithe isimplified icomplexity imight ilook
iconstant, ithe iactual inumber iof ioperations i(In iterms iof iedge irelaxations) igrows iwith ithe igraph, isize. i
Ans:6 i
Backtracking:
Backtracking iis ia igeneral ialgorithmic itechnique ithat iinvolves isearching ifor ia isolution ito ia iproblem iby
iexploring iall ipossible icandidate isolutions. iIt iis ia isystematic iway iof isearching ithrough iall ipossible
The icore iidea ibehind ibacktracking iis ito ibuild isolutions iincrementally iand ibacktrack i(or iundo) ithe ichoices
imade iwhenever iwe ireach ia idead-end ior ifind ithat ithe icurrent ipath icannot ilead ito ia ivalid isolution.
Backtracking iis ian ieffective iapproach ito isolving ithe iN-Queens ipuzzle ibecause iit iallows ius ito iexplore
idifferent iplacements iof iqueens ion ithe ichessboard iand ibacktrack iwhenever iwe iencounter ia iconflict.
Recursive iExploration: iIf ia iqueen ican ibe iplaced isafely, imove ito ithe inext irow iand irepeat ithe iprocess
irecursively.
Backtrack: iIf iplacing ia iqueen iin ithe icurrent irow ileads ito ia iconflict ior iif iwe ihave iplaced iall iN iqueens,
ibacktrack ito ithe iprevious irow iand itry ia idifferent icolumn.
Base iCase: iIf iall iN iqueens iare iplaced isuccessfully, ia isolution iis ifound. iOtherwise, icontinue iexploring iother
ipossibilities.
Conclusion:
Backtracking iis ia ipowerful itechnique ifor isolving iproblems ithat iinvolve iexploring iall ipossible
iconfigurations. iIn ithe icontext iof ithe iN-Queens ipuzzle, ibacktracking iallows ius ito isystematically iplace
iqueens ion ithe ichessboard, iensuring ithat ino itwo iqueens ithreaten ieach iother. iBy ifollowing ia irecursive
iapproach iand ibacktracking, iwhen inecessary, iwe ican iefficiently ifind iall ivalid isolutions ito ithe ipuzzle.