0% found this document useful (0 votes)
17 views

DCA7104 - ADA Internal Assignment

Uploaded by

Akshay Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

DCA7104 - ADA Internal Assignment

Uploaded by

Akshay Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Directorate of Online Education

i i i

INTERNAL IASSIGNMENT

NAME AKSHAY IIIIIIIIKUMAR IIIIIIIIMISHRA


PROGRAM MASTER IIIIIIIIOF IIIIIIIICOMPUTER IIIIIIIIAPPLICATION IIIIIIII(MCA)
ROLL IIIIIIIINUMBER 2214500139
SEMESTER III
COURSE IIIIIIIICODE IIIIIIII& IIIIIIIINAME DCA7104 I- IANALYSIS IAND IDESIGN IOF IALGORITHM
SEMESTER IIIIIIII 3

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:

n! i= in i× i(n-1) i× i(n-2) i× i... i× i3 i× i2 i× i1

Recursive iFactorial iImplementation:


The irecursive iapproach ito icalculating ithe ifactorial iof ia inumber iis iperhaps ithe imost iintuitive ibut ican ibe
iinefficient iin iterms iof istack ispace iusage. iConsider ithe ifollowing iC-style irecursive ifunction:

int ifactorial(int in) i{


if i(n i== i0 i|| in i== i1) i{
return i1;
Directorate of Online Education
i i i

}
return in i* ifactorial(n i- i1);
}

Analysis iof iRecursive iImplementation:


When iyou icall ifactorial(5), ithe ifunction iwill imake ia iseries iof irecursive icalls, ieach ipushing ia inew iframe ionto
ithe istack:

factorial(5) i-> ifactorial(4)


factorial(4) i-> ifactorial(3)
factorial(3) i-> ifactorial(2)
factorial(2) i-> ifactorial(1)
Only iafter ireaching ithe ibase icase i(n i== i1) iwill ithe imultiplication ioperations ibegin ito icompute ithe iresult. iFor
ilarge ivalues iof in, ithe istack ican igrow isubstantially. iEach irecursive icall iconsumes iadditional imemory ifor iits

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.

Non-Recursive iFactorial iImplementation:


A inon-recursive iapproach ican icompute ithe ifactorial iwithout ithe ioverhead iof imultiple ifunction icalls, ithereby
ioptimizing istack ispace iusage. iHere's ia iC-style inon-recursive ifunction ito icalculate ifactorial:

int ifactorial(int in) i{


int iresult i= i1;
for i(int ii i= i1; ii i<= in; ii++) i{
result i*= ii;
}
return iresult;
}
Directorate of Online Education
i i i

Analysis iof iNon-Recursive iImplementation:


The inon-recursive ifunction iutilizes ia isingle iloop ithat iiteratively imultiplies ithe inumbers ifrom i1 ito in ito
icompute ithe ifactorial. iThere iare ino ifunction icalls iinvolved, imeaning ithere's ino ineed ito ipush iand ipop iframes

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

iirrespective iof ithe ivalue iof in.

Comparison iand iContrast:


Stack iSpace iUtilisation: iThe irecursive iapproach irequires iadditional istack ispace iproportional ito ithe iinput
isize, ileading ito ipotential istack ioverflow iissues ifor ilarge iinputs. iIn icontrast, ithe inon-recursive imethod

imaintains ia iconstant istack ispace irequirement, iensuring iefficient imemory iutilization.

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

ibetter iperformance icharacteristics.

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

istack ioverflow ierrors.

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

ithan itraditional ilinear isearch imethods.


Directorate of Online Education
i i i

Algorithm: iBinary iSearch

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.

2. iSteps ifor iImplementation:

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

iiii while istart i<= iend:


iiiiiiii mid i= i(start i+ iend) i/ i2
iiiiiiii if iarr[mid] i== itarget:
iiiiiiiiiiii return imid
iiiiiiii elif iarr[mid] i< itarget:
iiiiiiiiiiii start i= imid i+ i1
iiiiiiii else:
iiiiiiiiiiii end i= imid i- i1

iiii return i-1 i i// iTarget inot ifound

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 iTraversal iand iBFS iTree iConstruction.


1. Start itraversal ifrom ivertex i'a'.
2. Add i'a' ito ithe iqueue.
3. While ithe iqueue iis inot iempty:
• Dequeue ia ivertex iv
• For ieach iadjacent ivertex iu iof iv ithat ihas inot ibeen ivisited
i) Mark iu ias ivisited.
ii) Enqueue iu iinto ithe iqueue.
iii) Add iedge i(v,u) ito ithe iBFS itree.
Directorate of Online Education
i i i

Let's iPerform ithe itraversal.

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

For ithe iinstance iprovided iwith ia iCapacity iM=5

Item Weight Value


1 2 $12
2 1 $10
3 3 $20
4 2 $15

We ican isolve ithis iproblem iusing ia ibottom-up idynamic iprogramming iapproach.


Here's ihow ithe idynamic iprogramming itable iwould ilook ifor ithis iinstance:

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

To iexplain ithe itable:


The irows irepresent ithe iitems i(from i1 ito i4).
The icolumns irepresent ithe icapacities i(from i0 ito i5).
Each icell iin ithe itable irepresents ithe imaximum ivalue ithat ican ibe iobtained iwith ithe igiven icapacity
iconsidering ithe iitems iup ito ithat irow.

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.

Detailed iApplication ito ithe iGiven iProblem:


Initialization:
Start iat ivertex iA.

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.

Step i5: iVertex iE:


E ihas ithe inext ismallest itentative idistance i(6).
Consider iE's ineighbors: iF, iC, iand iH.
Distance ito iF ivia iE: i6 i+ i2 i= i8. iUpdate iF's itentative idistance ito i8.
Distance ito iC ivia iE: i6 i+ i4 i= i10. iUpdate iC's itentative idistance ito i10.
Distance ito iH ivia iE: i6 i+ i5 i= i11. iNo ichange ifor iH ias iits iexisting itentative idistance iis ishorter.
Steps i6-8: iFor ivertices iC, iF, iand iH, ino inew ishortest ipaths iare ifound, iso ithey iremain iunchanged.
Final iDistances iand iPaths:
From ithe iabove isteps, iwe ihave icomputed ithe ishortest idistances ifrom ivertex iA ito iall iother ivertices.

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

iV), iwhere iV iis ithe inumber iof ivertices.

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

iconfigurations ito ifind ithe icorrect ione.

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.

Application iin iSolving ithe iN-Queens iPuzzle:


The iN-Queens ipuzzle iis ia iclassic iproblem iwhere ithe iobjective iis ito iplace iN iqueens ion ian iN×N ichessboard
iin isuch ia iway ithat ino itwo iqueens ithreaten ieach iother. iIn iother iwords, ino itwo iqueens ishould ishare ithe isame

irow, icolumn, ior idiagonal.


Directorate of Online Education
i i i

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.

Steps ito iSolve iN-Queens iUsing iBacktracking:


Initialization: iStart iwith ian iempty ichessboard.
Place iQueen: iFor ieach irow, itry iplacing ia iqueen iin ia icolumn iwhere iit idoes inot iconflict iwith iany iother iqueen
i(i.e., ino iother iqueen iin ithe isame irow, icolumn, ior idiagonal).

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.

Pseudocode ifor iN-Queens iUsing iBacktracking:


function isolveNQueens(board, irow):
iiii if irow i== iN:
iiiiiiii return itrue i i// iBase icase: iAll iqueens iare iplaced
iiii

iiii for ieach icolumn iin iboard[row]:


iiiiiiii if iisSafe(board, irow, icolumn):
iiiiiiiiiiii // iPlace iqueen iat i(row, icolumn)
iiiiiiiiiiii board[row][column] i= i'Q'
iiiiiiiiiiii

iiiiiiiiiiii // iRecur ifor ithe inext irow


iiiiiiiiiiii if isolveNQueens(board, irow i+ i1):
iiiiiiiiiiiiiiii return itrue i i// iFound ia isolution
iiiiiiiiiiii

iiiiiiiiiiii // iBacktrack: iRemove iqueen ifrom i(row, icolumn)


iiiiiiiiiiii board[row][column] i= i'.'
Directorate of Online Education
i i i
iiii

iiii return ifalse i i// iNo ivalid iplacement ifound

function iisSafe(board, irow, icolumn):


iiii // iCheck iif iany iqueen iin ithe isame icolumn
iiii for ii i= i0 ito irow:
iiiiiiii if iboard[i][column] i== i'Q':
iiiiiiiiiiii return ifalse
iiii

iiii // iCheck iupper ileft idiagonal


iiii for ii, ij i= irow, icolumn; ii i>= i0 iand ij i>= i0; ii--, ij--:
iiiiiiii if iboard[i][j] i== i'Q':
iiiiiiiiiiii return ifalse
iiii

iiii // iCheck iupper iright idiagonal


iiii for ii, ij i= irow, icolumn; ii i>= i0 iand ij i< iN; ii--, ij++:
iiiiiiii if iboard[i][j] i== i'Q':
iiiiiiiiiiii return ifalse
iiii

iiii return itrue

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.

You might also like