You are on page 1of 27

VIJAYALAKSHMIiDINESH

2214101657

MASTERiOFiCOMPUTERiAPPLICATIONi(MCA)

THIRDiSEMESTER

DCA7104

ANALYSIS AND DESIGN OF ALGORITHMS


SET-1i&iSETi2
Q1.A)iiExplainiamortizedianalysisiwithitheihelpiofianiexample
Answer
AmortizediAnalysisiisiusediforialgorithmsiwhereianioccasionalioperationiisiveryislow,ibuti
mostiofitheiotherioperationsiareifaster.iIniAmortizediAnalysis,iweianalyzeiaisequenceiofiop
erationsiandiguaranteeiaiworst-caseiaverageitimeithatiisilowerithanitheiworst-
caseitimeiofiaiparticularlyiexpensiveioperation.i
TheiexampleidataistructuresiwhoseioperationsiareianalyzediusingiAmortizediAnalysisiarei
HashiTables,iDisjointiSets,iandiSplayiTrees.i
Amortizedianalysisiisiaitechniqueiusediinicomputeriscienceitoianalyzeitheiaverage-
caseitimeicomplexityiofialgorithmsithatiperformiaisequenceiofioperations,iwhereisomeioper
ationsimayibeimoreiexpensiveithaniothers.iTheiideaiisitoispreaditheicostiofitheseiexpensivei
operationsioverimultipleioperations,isoithatitheiaverageicostiofieachioperationiisiconstantior
iless.
Foriexample,iconsideritheidynamiciarrayidataistructureithaticanigrowiorishrinkidynamically
iasielementsiareiaddedioriremoved.iTheicostiofigrowingitheiarrayiisiproportionalitoitheisize
iofitheiarray,iwhichicanibeiexpensive.iHowever,iifiweiamortizeitheicostiofigrowingitheiarra
yioveriseveraliinsertions,itheiaverageicostiofieachiinsertionibecomesiconstantioriless.
Amortizedianalysisiprovidesiaiusefuliwayitoianalyzeialgorithmsithatiperformiaisequenceiofi
operationsiwhereisomeioperationsiareimoreiexpensiveithaniothers,iasiitiprovidesiaiguarante
ediupperiboundionitheiaverageitimeicomplexityiofieachioperation,iratherithanitheiworst-
caseitimeicomplexity.
Amortized analysis is a method used in computer science to analyze the average
performance of an algorithm over multiple operations. Instead of analyzing the worst-case
time complexity of an algorithm, which gives an upper bound on the running time of a single
operation, amortized analysis provides an average-case analysis of the algorithm by
considering the cost of several operations performed over time
Theikeyiideaibehindiamortizedianalysisiisitoispreaditheicostiofianiexpensiveioperationioveri
severalioperations.iForiexample,iconsideriaidynamiciarrayidataistructureithatiisiresizediwhe
niitirunsioutiofispace.iTheicostiofiresizingitheiarrayiisiexpensive,ibutiiticanibeiamortizediov
eriseveraliinsertionsiintoitheiarray,isoithatitheiaverageitimeicomplexityiofianiinsertioniopera
tioniisiconstant.
Amortizedianalysisiisiusefuliforidesigningiefficientialgorithmsiforidataistructuresisuchiasidy
namiciarrays,ipriorityiqueues,iandidisjoint-
setidataistructures.iItiprovidesiaiguaranteeithatitheiaverage-
caseitimeicomplexityiofianioperationiisiconstant,ieveniifisomeioperationsimayibeiexpensive
.
Letiusiconsiderianiexampleiofisimpleihashitableiinsertions.iHowidoiweidecideionitableisize
?iThereiisiaitrade-offibetweenispaceianditime,iifiweimakeihash-
tableisizeibig,isearchitimeibecomesilow,ibutitheispaceirequiredibecomesihigh.

Theisolutionitoithisitrade-
offiproblemiisitoiuseiDynamiciTablei(oriArrays).iTheiideaiisitoiincreaseitheisizeiofitheitabl
eiwheneveriitibecomesifull.iFollowingiareitheistepsitoifollowiwhenitheitableibecomesifull.i
1)iAllocateimemoryiforilargeritableisize,itypicallyitwiceitheiolditable.i
2)iCopyitheicontentsiofitheiolditableitoiainewitable.i
3)iFreeitheiolditable.i
Ifitheitableihasispaceiavailable,iweisimplyiinsertiainewiitemiinitheiavailableispace.i
1.b)iWhatiisitheigeneraliplanitoianalyzeitheiefficiencyiofirecursiveialgorithms?
Answer
GeneraliPlaniforiAnalyzingitheiTimeiEfficiencyiof
RecursiveiAlgorithms
1.iDecideioniaiparameteri(oriparameters)iindicatingianiinput’sisize.
2.iIdentifyitheialgorithm’sibasicioperation.
3.iCheckiwhetheritheinumberiofitimesitheibasicioperationiisiexecutedicanivary
onidifferentiinputsiofitheisameisize;iifiitican,itheiworst-case,iaverage-case,
andibest-caseiefficienciesimustibeiinvestigatediseparately.
4.iSetiupiairecurrenceirelation,iwithianiappropriateiinitialicondition,iforithe
numberiofitimesitheibasicioperationiisiexecuted.
5.iSolveitheirecurrenceior,iatileast,iascertainitheiorderiofigrowthiofiitsisolution.
EXAMPLEi1:iComputeitheifactorialifunctioniF(n)i=in!iforianiarbitraryinon
negativeiintegerin.iSincein!=i1•.i.i.i.i•i(ni−i1)i•ini=i(ni−i1)!i•in,iforini≥i1iand
0!=i1ibyidefinition,iweicanicomputeiF(n)i=iF(ni−i1)i•iniwithitheifollowing
recursiveialgorithm.(NDi2015)iALGORITHMF(n)
//Computesin!irecursively
//Input:iAinonnegativeiintegerin
//Output:iTheivalueiofin!
ifini=i0ireturni1
elseireturniF(ni−i1)i*in
Algorithmianalysis
•iForisimplicity,iweiconsideriniitselfiasianiindicatoriofithisialgorithm’siinput
size.ii.e.1.
•iTheibasicioperationiofitheialgorithmiisimultiplication;iwhoseinumberiof
executionsiweidenoteiM(n).iSinceitheifunctioniF(n)iisicomputediaccording
toitheiformulaiF(n)i=iF(ni−1)•niforini>0.
•iTheinumberiofimultiplicationsiM(n)ineededitoicomputeiitimustisatisfyithe
Equality

Mi(ni−i1)imultiplicationsiareispentitoicomputeiF(ni−i1),iandioneimore
MultiplicationiisineededitoimultiplyitheiresultibyiniiRecurrenceirelations
TheilastiequationidefinesitheisequenceiM(n)ithatiweineeditoifind.iThis
equationidefinesiM(n)inotiexplicitly,ii.e.,iasiaifunctioniofin,ibutiimplicitlyiasiaifunctioniofiit
sivalueiatianotheripoint,inamelyini−i1.iSuchiequationsiareicalledirecurrenceirelationsiorirec
urrences.
Solveitheirecurrenceirelationi(n)i=i(ni−i1)i+i1,ii.e.,itoifindianiexplicitiformula
fioiriM(n)iinitermsiofinionly.
Toidetermineiaisolutioniuniquely,iweineedianiinitialiconditionithatitellsiustheivalueiwithiwh
ichitheisequenceistarts.iWeicaniobtainithisivalueibyiinspectingtheiconditionithatimakesithei
algorithmistopiitsirecursiveicalls:
ifini=i0ireturni1.
Thisitellsiusitwoithings.iFirst,isinceitheicallsistopiwhenini=i0,itheismallestvalueiofiniforiwh
ichithisialgorithmiisiexecutediandihenceiM(n)idefinediisi0.Second,ibyiinspectingitheipseudo
code’siexitingiline,iweicaniseeithatiwhenini=i0,
theialgorithmiperformsinoimultiplications.

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Thisitellsiusitwoithings.iFirst,isinceitheicallsistopiwhenini=i0,itheismallest
valueiofiniforiwhichithisialgorithmiisiexecutediandihenceiM(n)idefinediisi0.
Second,ibyiinspectingitheipseudocode’siexitingiline,iweicaniseeithatiwhenini=i0,
theialgorithmiperformsinoimultiplications
Thus,itheirecurrenceirelationiandiinitialiconditioniforitheialgorithm’sinumberiof
multiplications
M(n):
M(n)i=iM(ni−i1)i+i1
forini>0,iM(0)=0ifor
ni=0.
Methodiofibackwardisubstitutions
M(n)i=iM(ni−i1)+1isubstituteiM(ni−i1)i=iM(ni−i2)i+1
=i[M(ni−i2)i+i1]+i1
=iM(ni−i2)+2isubstituteiM(ni−i2)i=iM(ni−i3)i+1
=i[M(ni−i3)i+i1]+i2
=iM(ni−i3)i+i3

=iM(ni−ii)i+ii

=iM(ni−in)i+in
=in.
ThereforeM(n)=n

Q2.iDiscussitheirecursionistepitoicomputeiquicksortiusingiDivideiandiConqueristrategy.
Answer
quicksort uses divide-and-conquer, and so it's a recursive algorithm. The way that quicksort
uses divide-and-conquer is a little different from how merge sort does. In merge sort, the
divide step does hardly anything, and all the real work happens in the combine step.
Quicksort is the opposite: all the real work happens in the divide step. In fact, the combine
step in quicksort does absolutely nothing
Quicksortihasiaicoupleiofiotheridifferencesifromimergeisort.iQuicksortiworksiiniplace.iAnd
iitsiworst-caseirunningitimeiisiasibadiasiselectionisort'siandiinsertionisort's:iΘ(n2)
Theta,ileftiparenthesis,in,isquared,irightiparenthesis.iButiitsiaverage-
caseirunningitimeiisiasigoodiasimergeisort's:iΘ(nlog2n)\
iTheta,ileftiparenthesis,in,ilog,istartibase,i2,iendibase,in,irightiparenthesis.
Soiwhyithinkiaboutiquicksortiwhenimergeisortiisiatileastiasigood?iThat'sibecauseitheiconsta
ntifactorihiddeniinitheibig-
Θinotationiforiquicksortiisiquiteigood.iInipractice,iquicksortioutperformsimergeisort,iandiiti
significantlyioutperformsiselectionisortiandiinsertionisort.
Hereiisihowiquicksortiusesidivide-and-
conquer.iAsiwithimergeisort,ithinkiofisortingiaisubarrayiarray[p..r],iwhereiinitiallyitheisuba
rrayiisiarray[0..n-1].
Divideibyichoosingianyielementiinitheisubarrayiarray[p..r].iCallithisielementitheipivot.
Rearrangeitheielementsiiniarray[p..r]isoithatiallielementsiiniarray[p..r]ithatiareilessithaniorie
qualitoitheipivotiareitoiitsileftiandiallielementsithatiareigreaterithanitheipivotiareitoiitsiright.
iWeicallithisiprocedureipartitioning.iAtithisipoint,iitidoesn'timatteriwhatiorderitheielementsi
toitheileftiofitheipivotiareiinirelationitoieachiother,ianditheisameiholdsiforitheielementsitoit
heirightiofitheipivot.iWeijusticareithatieachielementiisisomewhereionitheicorrectisideiofithe
ipivot.
Asiaimatteriofipractice,iwe'llialwaysichooseitheirightmostielementiinitheisubarray,iarray[r],i
asitheipivot.iSo,iforiexample,iifitheisubarrayiconsistsiofi[9,i7,i5,i11,i12,i2,i14,i3,i10,i6],ithe
niweichoosei6iasitheipivot.iAfteripartitioning,itheisubarrayimightilookilikei[5,i2,i3,i6,i12,i7,
i14,i9,i10,i11].iLetiqibeitheiindexiofiwhereitheipivotiendsiup.
Conqueribyirecursivelyisortingitheisubarraysiarray[p..q-
1]i(allielementsitoitheileftiofitheipivot,iwhichimustibeilessithanioriequalitoitheipivot)iandiar
ray[q+1..r]i(allielementsitoitheirightiofitheipivot,iwhichimustibeigreaterithanitheipivot).
Combineibyidoinginothing.iOnceitheiconqueristepirecursivelyisorts,iweiareidone.iWhy?iAll
ielementsitoitheileftiofitheipivot,iiniarray[p..q-
1],iareilessithanioriequalitoitheipivotiandiareisorted,iandiallielementsitoitheirightiofitheipivo
t,iiniarray[q+1..r],iareigreaterithanitheipivotiandiareisorted.iTheielementsiiniarray[p..r]ican'ti
helpibutibeisorted!
Thinkiaboutiouriexample.iAfterirecursivelyisortingitheisubarraysitoitheileftiandirightiofithei
pivot,itheisubarrayitoitheileftiofitheipivotiisi[2,i3,i5],ianditheisubarrayitoitheirightiofitheipiv
otiisi[7,i9,i10,i11,i12,i14].iSoitheisubarrayihasi[2,i3,i5],ifollowedibyi6,ifollowedibyi[7,i9,i1
0,i11,i12,i14].iTheisubarrayiisisorted.
Theibaseicasesiareisubarraysiofifewerithanitwoielements,ijustiasiinimergeisort.iInimergeisor
t,iyouineveriseeiaisubarrayiwithinoielements,ibutiyouicaniiniquicksort,iifitheiotherielements
iinitheisubarrayiareiallilessithanitheipivotiorialligreaterithanitheipivot.
Let'sigoibackitoitheiconqueristepiandiwalkithroughitheirecursiveisortingiofitheisubarrays.iA
fteritheifirstipartition,iweihaveisubarraysiofi[5,i2,i3]iandi[12,i7,i14,i9,i10,i11],iwithi6iasithe
ipivot.
Toisortitheisubarrayi[5,i2,i3],iweichoosei3iasitheipivot.iAfteripartitioning,iweihavei[2,i3,i5].
iTheisubarrayi[2],itoitheileftiofitheipivot,iisiaibaseicaseiwheniweirecurse,iasiisitheisubarrayi
[5],itoitheirightiofitheipivot.
Toisortitheisubarrayi[12,i7,i14,i9,i10,i11],iweichoosei11iasitheipivot.iAfteripartitioning,iwei
havei[7,i9,i10]itoitheileftiofitheipivotiandi[14,i12]itoitheiright.iThenitheisubarraysiareisorte
d,iresultingiini[7,i9,i10],ifollowedibyi11,ifollowedibyi[12,i14].
Hereiisihowitheientireiquicksortialgorithmiunfolds.iArrayilocationsiiniblueihaveibeenipivots
iinipreviousirecursiveicalls,iandisoitheivaluesiinitheseilocationsiwillinotibeiexaminediorimo
vediagain:
3a.iAnalyseiBruteforceiStringimatchingialgorithmiandiobtainiitsitimeicomplexity.
Answer
Bruteiforceisearchiisitheimosticommonisearchialgorithmiasiitidoesinotirequireianyidomaini
knowledge;iallirequirediisiaistateidescription,ilegalioperators,itheiinitialistate,ianditheidescri
ptioniofiaigoalistate.iItidoesinotiimproveiperformanceiandientirelyireliesionicomputingipow
eritoitryioutipossibleicombinations.
Theibruteiforceialgorithmisearchesiallitheipositionsiinitheitextibetweeni0iandin-
m,iwhetheritheioccurrenceiofitheipatternistartsithereiorinot.iAfterieachiattempt,iitishiftsithei
patternibyiexactlyi1ipositionitoitheiright.iTheitimeicomplexityiofithisialgorithmiisiO(m*n).i
Ifiweisearchiforinicharactersiiniaistringiofimicharacters,iitiwillitakein*mitries.
Let’siseeiaiclassiciexampleiofiaitravelingisalesmanitoiunderstanditheialgorithmieasily.
Supposeiaisalesmanineedsitoitravelitoi10idifferenticitiesiiniaicountry,iandiheiwantsitoideter
mineitheishortestipossibleiroutesioutiofiallitheipossibleicombinations.iHereibruteiforceialgo
rithmisimplyicalculatesitheidistanceibetweeniallitheicitiesiandiselectsitheishortestione.
Anotheriexampleiisitoiattemptitoibreakithei5-
digitipassword;ithenibruteiForceimayitakeiupitoi105iattemptsitoicrackitheicode.
BruteiForceiSort
Initheibruteiforceisortitechnique,itheidatailistiisiscannedimultipleitimesitoifinditheismallesti
elementiinitheilist.iAfterieachiiterationioveritheilist,iitireplacesitheismallestielementiatitheit
opiofitheistackiandistartsitheinextiiterationifromitheisecondismallestidataiinitheilist.
Theiaboveistatementicanibeiwritteniinipseudo-codeiasifollows.

iiiiiiiiiiiii
Hereitheiproblemiisiofisize’in,’ianditheiessentialioperationiisithei‘if’itestiwhereitheidataiite
msiareibeingicomparediinieachiiteration.iThereiwillibeinoidifferenceibetweenitheiworstiand
ibesticasesiasitheinoiofiswapsiisialwaysin-1.
BruteiForceiStringiMatching
If all the characters in the pattern are unique, then Brute force string matching can be applied
with the complexity of Big O(n), where n is the string’s length. Brute force String matching
compares the pattern with the substring of a text character by character until it gets a
mismatched character. When a mismatch is found, the substring’s remaining character is
dropped, and the algorithm moves to the next substring
Theibelowipseudo-codesiexplainitheistring-
matchingilogic.iHere,itheialgorithmisearchesiforiaiPipatterni[0…m-1]iinitheitextiT[0….n-
1].
TheiworsticaseiwouldibeiwheniaishiftitoianotherisubstringiisinotimadeiuntilitheiMThicomp
arison.
iiiiiiiiiiiiiiiiiiiiiii
3b.iWriteiairecursiveialgorithmitoicomputeiGCDiofitwoinumbersiusingiEuclid’sialgori
thm.
Answer
Euclid’sialgorithmi(oriEuclideanialgorithm)iisiaimethodiforiefficientlyifindingitheigreatestic
ommonidivisori(GCD)iofitwoinumbers.iTheiGCDiofitwoiintegers,iXiandiY,iisitheilargestin
umberithatidividesibothiXiandiYiwithoutileavingiairemainder.
RecallithatitheiGreatestiCommoniDivisori(GCD)iofitwoiintegersiAiandiBiisitheilargestiinte
gerithatidividesibothiAiandiB.
TheiEuclideaniAlgorithmiisiaitechniqueiforiquicklyifindingitheiGCDiofitwoiintegers.
TheiAlgorithm
TheiEuclideaniAlgorithmiforifindingiGCD(A,B)iisiasifollows:
IfiAi=i0itheniGCD(A,B)=B,isinceitheiGCD(0,B)=B,iandiweicanistop.ii
IfiBi=i0itheniGCD(A,B)=A,isinceitheiGCD(A,0)=A,iandiweicanistop.ii
WriteiAiiniquotientiremainderiformi(Ai=iB⋅Qi+iR)
FindiGCD(B,R)iusingitheiEuclideaniAlgorithmisinceiGCD(A,B)i=iGCD(B,R)
Example:
FinditheiGCDiofi270iandi192
A=270,iB=192
Ai≠0
Bi≠0
Useilongidivisionitoifindithati270/192i=i1iwithiairemainderiofi78.iWeicaniwriteithisias:i27
0i=i192i*i1i+78
FindiGCD(192,78),isinceiGCD(270,192)=GCD(192,78)
A=192,iB=78
Ai≠0
Bi≠0
Useilongidivisionitoifindithati192/78i=i2iwithiairemainderiofi36.iWeicaniwriteithisias:
192i=i78i*i2i+i36
FindiGCD(78,36),isinceiGCD(192,78)=GCD(78,36)
A=78,iB=36
Ai≠0
Bi≠0
Useilongidivisionitoifindithati78/36i=i2iwithiairemainderiofi6.iWeicaniwriteithisias:
78i=i36i*i2i+i6
FindiGCD(36,6),isinceiGCD(78,36)=GCD(36,6)
A=36,iB=6
Ai≠0
Bi≠0
Useilongidivisionitoifindithati36/6i=i6iwithiairemainderiofi0.iWeicaniwriteithisias:
36i=i6i*i6i+i0
FindiGCD(6,0),isinceiGCD(36,6)=GCD(6,0)
A=6,iB=0
Ai≠0
Bi=0,iGCD(6,0)=6
Soiweihaveishown:
GCD(270,192)i=iGCD(192,78)i=iGCD(78,36)i=iGCD(36,6)i=iGCD(6,0)i=i6
GCD(270,192)i=i6
UnderstandingitheiEuclideaniAlgorithm
IfiweiexamineitheiEuclideaniAlgorithmiweicaniseeithatiitimakesiuseiofitheifollowingiprope
rties:
GCD(A,0)i=iA
GCD(0,B)i=iB
IfiAi=iB⋅Qi+iRiandiB≠0itheniGCD(A,B)i=iGCD(B,R)iwhereiQiisianiinteger,iRiisianiintege
ribetweeni0iandiB-1
TheifirstitwoipropertiesiletiusifinditheiGCDiifieitherinumberiisi0.iTheithirdipropertyiletsiusi
takeiailarger,imoreidifficultitoisolveiproblem,iandireduceiititoiaismaller,ieasieritoisolveipro
blem.
TheiEuclideaniAlgorithmimakesiuseiofitheseipropertiesibyirapidlyireducingitheiproblemiint
oieasieriandieasieriproblems,iusingitheithirdiproperty,iiuntiliitiisieasilyisolvedibyiusingionei
ofitheifirstitwoiproperties.
Weicaniunderstandiwhyitheseipropertiesiworkibyiprovingithem.
WeicaniproveithatiGCD(A,0)=Aiisiasifollows:
TheilargestiintegerithaticanievenlyidivideiAiisiA.
Alliintegersievenlyidividei0,isinceiforianyiinteger,iC,iweicaniwriteiCi⋅i0i=i0.iSoiweicanicon
cludeithatiAimustievenlyidividei0.
TheigreatestinumberithatidividesibothiAiandi0iisiA.
TheiproofiforiGCD(0,B)=Biisisimilar.i(Sameiproof,ibutiweireplaceiAiwithiB).
ToiproveithatiGCD(A,B)=GCD(B,R)iweifirstineeditoishowithatiGCD(A,B)=GCD(B,A-B).

ProofithatitheiGCD(B,C)ievenlyidividesiA
TheiGCD(B,C),ibyidefinition,ievenlyidividesiB.iAsiairesult,iBimustibeisomeimultipleiofiG
CD(B,C).ii.e.iM⋅GCD(B,C)=BiwhereiMiisisomeiinteger
TheiGCD(B,C),ibyidefinition,ievenlyidividesiC.iAsiairesult,iiCimustibeisomeimultipleiofiG
CD(B,C).ii.e.iN⋅GCD(B,C)=CiwhereiNiisisomeiinteger
A-B=Cigivesius:
B+C=A
M⋅GCD(B,C)i+iN⋅GCD(B,C)i=iA
(Mi+iN)⋅GCD(B,C)i=iA
SoiweicaniseeithatiGCD(B,C)ievenlyidividesiA.
Aniillustrationiofithisiproofiiisishowniinitheifigureibelow

4a.iDifferentiateibetweenibottom-upianditop-downiheapiconstruction
Answer
IniSystemiDesign,ithereiareitwoitypesiofiapproachesifollowedinamely,itheiBottom-
UpiModelianditheiTop-DowniModel.
Theibottom-
upimodeliisioneiiniwhichitheidifferentipartsiofiaisystemiareidesignediandidevelopediandithe
niallitheseipartsiareiconnecteditogetheriasiaisingleiunit.
Onitheiotherihand,itheitop-
downimodeliisioneiiniwhichitheiwholeisystemiisidecomposediintoismallerisub-
components,ithenieachiofitheseipartsiareidesignediandidevelopeditillitheicompletedisystemii
sidesigned.
WhatiisiBottom-UpiModel?
Bottom-Up Model is a system design approach where the parts of a system are defined in
details. Once these parts are designed and developed, then these parts or components are
linked together to prepare a bigger component. This approach is repeated until the complete
system is built.TheiadvantageiofiBottom-
UpiModeliisiinimakingidecisionsiativeryilowilevelianditoidecideitheire-
usabilityioficomponents.
WhatiisiTop-DowniModel?
Top-
DowniModeliisiaisystemidesigniapproachiwhereitheidesignistartsifromitheisystemiasiaiwhol
e.iTheicompleteisystemiisithenidividediintoismallerisub-applicationsiwithimoreidetails.
Eachipartiagainigoesithroughitheitop-
downiapproachitillitheicompleteisystemiisidesignediwithiallitheiminuteidetails.iTopDownia
pproachiisialsoitermediasibreakingiaibiggeriproblemiintoismalleriproblemsiandisolvingithe
miindividuallyiinirecursiveimanner.

DifferenceibetweeniBottom-UpiModeliandiTop-DowniModel
Key Bottom-UpiModel Top-DowniModel
Focus IniBottom- IniTop-
UpiModel,itheifocusiisioniidentifyingi downiModel,itheifocusiisionibreakingitheib
andiresolvingismallestiproblemsiandi iggeriproblemiintoismallerioneiandithenire
theniintegratingithemitogetheritoisolv peatitheiprocessiwithieachiproblem.
eitheibiggeriproblem.
Language Bottom- Top-
UpiModeliisimainlyiusedibyiobjectiori DowniModeliisifollowedibyistructuraliprogr
entediprogrammingilanguagesilikeiJa ammingilanguagesilikeiC,iFortranietc.
va,iC++ietc.
Redundan Bottom- Top-
cy Upimodeliisibetterisuitediasiitiensure downimodelihasihighiratioiofiredundancyi
siminimumidatairedundancyiandifocu asitheisizeiofiprojectiincreases.
siisionire-usability.

Interaction Bottom- Top-


Upimodelihaveihighiinteractivityibetw downimodelihasitighticouplingiissuesiandil
eenivariousimodules. owiinteractivityibetweenivariousimodules.
Approach Bottom- Top-
upimodeliisibasedionicompositioniap downimodeliisibasedionidecompositioniap
proach. proach.
Issues IniBottom- IniTop-
Up,isometimesiitiisidifficultitoiidentifyi Down,iitimayinotibeipossibleitoibreakitheip
overallifunctionalityiofisystemiiniinitial roblemiintoisetiofismalleriproblems.
istages.
Conclusion
Theimostisignificantidifferenceibetweenitheitwoitypesiofimodelsiisithatitheibottom-
upimodeliisibasedionitheicompositioniapproach,iwhileitheitop-
downimodeliisibasedionitheidecompositioniapproach.
Anotheriimportantidifferenceibetweenitheitwoiisithatitheitop-
downimodeliisimainlyiusediinistructuraliprogrammingilikeiCiprogramming,iwhereasitheibot
tom-upiapproachiisifollowediiniobject-orientediprogrammingilikeiC++,iJava,ietc.
i4.b)iExplainitheitwoitypesioficollisioniresolutioniinihashing.
Answer
Hashingiisitheiprocessiofitransformingidataiandimappingiititoiairangeiofivaluesiwhichicanib
eiefficientlyilookediup.
Inithisiarticle,iweihaveiexploreditheiideaioficollisioniinihashingiandiexploredidifferenticolli
sioniresolutionitechniquesisuchias:
OpeniHashingi(Separateichaining)
ClosediHashingi(OpeniAddressing)
LineriProbing
Quadraticiprobing
Doubleihashing

Hashitable:iaidataistructureiwhereitheidataiisistoredibasediuponiitsihashedikeyiwhichiisiobt
ainediusingiaihashingifunction.
Hashifunction:iaifunctioniwhichiforiaigivenidata,ioutputsiaivalueimappeditoiaifixedirange.i
Aihashitableileveragesitheihashifunctionitoiefficientlyimapidataisuchithatiiticanibeiretrieved
iandiupdatediquickly.iSimplyiput,iassumeiSi=i{s1,is2,is3,i....,isn}itoibeiaisetiofiobjectsithati
weiwishitoistoreiintoiaimapiofisizeiN,isoiweiuseiaihashifunctioniH,isuchithatiforiallisibelon
gingitoiS;iH(s)i->ix,iwhereixiisiguaranteeditoilieiinitheirangei[1,N]
PerfectiHashifunction:iaihashifunctionithatimapsieachiitemiintoiaiuniqueisloti(noicollisions)
.
Followingiareitheicollisioniresolutionitechniquesiused:
OpeniHashingi(Separateichaining)
ClosediHashingi(OpeniAddressing)
LineriProbing
Quadraticiprobing
Doubleihashing
1.iOpeniHashingi(Separateichaining)
Collisionsiareiresolvediusingiailistiofielementsitoistoreiobjectsiwithitheisameikeyitogether.
Supposeiyouiwishitoistoreiaisetiofinumbersi=i{0,1,2,4,5,7}iintoiaihashitableiofisizei5.
Now,iassumeithatiweihaveiaihashifunctioniH,isuchithatiH(x)i=ix%5
So,iifiweiwereitoimapitheigivenidataiwithitheigivenihashifunctioniwe'lligetitheicorrespondi
ngivalues
H(0)->i0%5i=i0
iiiiiH(1)->i1%5i=i1
iiiiiH(2)->i2%5i=i2
iiiiiH(4)->i4%5i=i4
iiiiiH(5)->i5%5i=i0
iiiiiH(7)->i7%5i=i2

 Clearlyi0iandi5,iasiwelliasi2iandi7iwillihaveitheisameihashivalue,iandiinithisicaseiwe'llisimplyi
appenditheicollidinivaluesitoiailistibeingipointedibyitheirihashikeys.

iiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Obviouslyiinipracticeitheitableisizeicanibeisignificantlyilargeianditheihashifunctionicanibeie
venimoreicomplex,ialsoitheidataibeingihashediwouldibeimoreicomplexiandinon-
primitive,ibutitheiideairemainsitheisame.
This is an easy way to implement hashing but it has its own demerits.
The lookups/inserts/updates can become linear [O(N)] instead of constant time [O(1)] if the
hash function has too many collisions.
It doesn't account for any empty slots which can be leveraged for more efficient storage and
lookups.Ideallyiweirequireiaigoodihashifunctionitoiguaranteeievenidistributioniofitheivalues
Say,iforiailoadifactor
λ=numberiofiobjectsistorediinitable/sizeiofitheitablei(canibei>1)
aigoodihashifunctioniwouldiguaranteeithatitheimaximumilengthiofilistiassociatediwithieachi
keyiisicloseitoitheiloadifactor.
Noteithatitheiorderiiniwhichitheidataiisistorediinitheilistsi(orianyiotheridataistructures)iisiba
sediuponitheiimplementationirequirements.iSomeigeneraliwaysiincludeiinsertioniorder,ifreq
uencyiofiaccessietc.
2.iClosediHashingi(OpeniAddressing)
Thisicollisioniresolutionitechniqueirequiresiaihashitableiwithifixediandiknownisize.iDuringi
insertion,iifiaicollisioniisiencountered,ialternativeicellsiareitriediuntilianiemptyibucketiisifou
nd.iTheseitechniquesirequireitheisizeiofitheihashitableitoibeisupposedlyilargerithanitheinum
beriofiobjectsitoibeistoredi(somethingiwithiailoadifactori<i1iisiideal).
Thereiareivariousimethodsitoifinditheseiemptyibuckets:
a.iLineriProbing
b.iQuadraticiprobing
c.iDoubleihashing
a.iLineariProbing
Theiideaiofilineariprobingiisisimple,iweitakeiaifixedisizedihashitableiandieveryitimeiweifac
eiaihashicollisioniweilinearlyitraverseitheitableiiniaicyclicimanneritoifinditheinextiemptyisl
ot.
Assumeiaiscenarioiwhereiweiintenditoistoreitheifollowingisetiofinumbersi=i{0,1,2,4,5,7}iin
toiaihashitableiofisizei5iwithitheihelpiofitheifollowingihashifunctioniH,isuchithatiH(x)i=ix
%5.
So,iifiweiwereitoimapitheigivenidataiwithitheigivenihashifunctioniwe'lligetitheicorrespondi
ngivalues
H(0)->i0%5i=i0
iiiiiH(1)->i1%5i=i1
iiiiiH(2)->i2%5i=i2
iiiiiH(4)->i4%5i=i4
iiiiiH(5)->i5%5i=i0
inithisicaseiweiseeiaicollisioniofitwoitermsi(0i&i5).iInithisisituationiweimoveilinearlyidown
itheitableitoifinditheifirstiemptyislot.iNoteithatithisilinearitraversaliisicycliciininature,ii.e.iin
itheieventiweiexhaustitheilastielementiduringitheisearchiweistartiagainifromitheibeginningiu
ntilitheiinitialikeyiisireached.
Inithisicaseiourihashifunctionicanibeiconsiderediasithis:
H(x,ii)i=i(H(x)i+ii)%N
whereiNiisitheisizeiofitheitableiandiiirepresentsitheilinearlyiincreasingivariableiwhichistartsi
fromi1i(untiliemptyibucketiisifound).
Despiteibeingieasyitoicompute,iimplementiandideliveribesticacheiperformance,ithisisuffersi
fromitheiproblemioficlusteringi(manyiconsecutiveielementsigetigroupeditogether,iwhichiev
entuallyireducesitheiefficiencyiofifindingielementsioriemptyibuckets).
b.iQuadraticiProbing
Thisimethodiliesiinitheimiddleiofigreaticacheiperformanceianditheiproblemioficlustering.iT
heigeneraliideairemainsitheisame,itheionlyidifferenceiisithatiweilookiatitheiQ(i)iincrementia
tieachiiterationiwhenilookingiforianiemptyibucket,iwhereiQ(i)iisisomeiquadraticiexpressioni
ofii.iAisimpleiexpressioniofiQiwouldibeiQ(i)i=ii^2,iiniwhichicaseitheihashifunctionilooksis
omethingilikeithis:
H(x,ii)i=i(H(x)i+ii^2)%N

Inigeneral,iH(x,ii)i=i(H(x)i+i((c1\*i^2i+ic2\*ii+ic3)))%N,iforisomeichoiceioficonstantsic1,i
c2,iandic3
Despiteiresolvingitheiproblemioficlusteringisignificantlyiitimayibeitheicaseithatiinisomeisitu
ationsithisitechniqueidoesinotifindianyiavailableibucket,iunlikeilineariprobingiwhichialways
ifindsianiemptyibucket.
Luckily,iweicanigetigoodiresultsifromiquadraticiprobingiwithitheirighticombinationiofiprobi
ngifunctioniandihashitableisizeiwhichiwilliguaranteeithatiweiwillivisitiasimanyislotsiinitheit
ableiasipossible.iIniparticular,iifitheihashitable'sisizeiisiaiprimeinumberianditheiprobingifun
ctioniisiH(x,ii)i=ii^2,itheniatileasti50%iofitheislotsiinitheitableiwillibeivisited.iThus,iifitheit
ableiisilessithanihalfifull,iweicanibeicertainithatiaifreeislotiwillieventuallyibeifound.

Alternatively,iifitheihashitableisizeiisiaipoweriofitwoianditheiprobingifunctioniisiH(x,ii)i=i(i
^2i+ii)/2,ithenieveryislotiinitheitableiwillibeivisitedibyitheiprobingifunction.

Assumeiaiscenarioiwhereiweiintenditoistoreitheifollowingisetiofinumbersi=i{0,1,2,5}iintoia
ihashitableiofisizei5iwithitheihelpiofitheifollowingihashifunctioniH,isuchithatiH(x,ii)i=i(x%
5i+ii^2)%5.
5.iExplainitheiFloyd’sialgorithmitoifinditheishortestipathiwithiitsicomplexity.
Answer
TheiFloyd-
Warshallialgorithmiisiaipopularialgorithmiforifindingitheishortestipathiforieachivertexipairii
niaiweightedidirectedigraph.
Iniallipairishortestipathiproblem,iweineeditoifindioutiallitheishortestipathsifromieachivertexi
toialliotheriverticesiinitheigraph.

Now,ilet’sijumpiintoitheialgorithm:

We’reitakingiaidirectediweightedigraphiG(V,iE)iasianiinput.iAndifirst,iweiconstructiaigraph
imatrixifromitheigivenigraph.iThisimatrixiincludesitheiedgeiweightsiinitheigraph.
Next,iweiinserti\textbf{\mathsf{0}}iinitheidiagonalipositionsiinitheimatrix.iTheirestiofitheip
ositionsiareifillediwithitheirespectiveiedgeiweightsifromitheiinputigraph.
Then,iweineeditoifinditheidistanceibetweenitwoivertices.iWhileifindingitheidistance,iweials
oicheckiifithere’sianyiintermediateivertexibetweenitwoipickedivertices.iIfithereiexistsianiint
ermediateivertexitheniweicheckitheidistanceibetweenitheiselectedipairiofiverticesiwhichigoe
sithroughithisiintermediateivertex.
Ifithisidistanceiwhenitraversingithroughitheiintermediateivertexiisilessithenitheidistanceibet
weenitwoipickediverticesiwithoutigoingithroughitheiintermediateivertex,iweiupdateitheishor
testidistanceivalueiinitheimatrix.

Theinumberiofiiterationsiisiequalitoitheicardinalityiofitheivertexiset.iTheialgorithmireturnsit
heishortestidistanceifromieachivertexitoianotheriinitheigivenigraph.

AniExample
Let’sirunitheiFloyd-Warshallialgorithmioniaiweightedidirectedigraph:

Atifirst,iweiconstructiaigraphimatrixifromitheiinputigraph.iNext,iweiinserti\mathsf{0}itoithe
idiagonalipositionsiinitheimatrix,ianditheirestiofitheipositionsiwillibeifillediwithitheiedgeiw
eightsifromitheiinputigraph:
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
ii\[\begin{bmatrix}i0i&i\inftyi&i-
2i&i\inftyi\\i4i&i0i&i3i&i\infty\\i\inftyi&i\inftyi&i0i&i2i\\i\inftyi&i-
1i&i\inftyi&i0i\\i\end{bmatrix}i\quad\]
Now,iwe’reireadyitoistartitheiiteration.iTheicardinalityiofitheivertexisetiisi4.iWe’lliiterateith
eiloopsi\mathsf{4}itimes.
Let’sistartiwithitheifirstiloop.iForitheifirstiloopiki=1,ii=1,ij=i1iwe’llicheckiifiweishouldiupd
ateitheimatrix:

Asitheiloopivaluesidon’tisatisfyitheicondition,ithereiwillibeinoiupdateiinitheimatrix.
Let’sicontinue,inowiforitheivaluesiki=1,ii=1,ij=i2iandicheckiagain:
Thus,ithereiwillibeinoichangesiinitheimatrix.iInithisiway,iwe’llicontinueiandicheckiallipairi
ofivertices.
Let’sifast-forwarditoisomeivaluesithatiwillisatisfyitheidistanceicondition.
Foritheiloopivaluesiki=1,ii=2,ij=i3,iwe’lliseeithatitheiconditioniisisatisfied:

i6.a)iHowiareiHuffmanicodesiconstructed?
Answer

Huffman coding is a lossless data compression algorithm. The idea is to assign variable-
length codes to input characters, lengths of the assigned codes are based on the frequencies
of corresponding characters.

Theivariable-
lengthicodesiassigneditoiinputicharactersiareiPrefixiCodes,imeansitheicodesi(bitisequences)i
areiassignediinisuchiaiwayithatitheicodeiassigneditoioneicharacteriisinotitheiprefixioficodei
assigneditoianyiothericharacter.iThisiisihowiHuffmaniCodingimakesisureithatithereiisinoia
mbiguityiwhenidecodingitheigeneratedibitstream.i
Letiusiunderstandiprefixicodesiwithiaicounteriexample.iLetithereibeifouricharactersia,ib,icia
ndid,ianditheiricorrespondingivariableilengthicodesibei00,i01,i0iandi1.iThisicodingileadsitoi
ambiguityibecauseicodeiassigneditoiciisitheiprefixioficodesiassigneditoiaiandib.iIfitheicomp
ressedibitistreamiisi0001,itheide-
compressedioutputimayibei“cccd”iori“ccb”iori“acd”iori“ab”.

SeeithisiforiapplicationsiofiHuffmaniCoding.i
ThereiareimainlyitwoimajoripartsiiniHuffmaniCoding
BuildiaiHuffmaniTreeifromiinputicharacters.
TraverseitheiHuffmaniTreeiandiassignicodesitoicharacters.
Algorithm:
TheimethodiwhichiisiuseditoiconstructioptimaliprefixicodeiisicallediHuffmanicoding.
iThisialgorithmibuildsiaitreeiinibottomiupimanner.iWeicanidenoteithisitreeibyiT
Let,i|c|ibeinumberiofileaves
|c|i-
1iareinumberiofioperationsirequireditoimergeitheinodes.iQibeitheipriorityiqueueiwhichican
ibeiusediwhileiconstructingibinaryiheap.
AlgorithmiHuffmani(c)
{
iiin=i|c|i
iiiQi=ici
iiiforii<-1itoin-1

iiido
iii{
iiiiiiitempi<-igetinodei()
iiiiiilefti(temp]iGet_mini(Q)irighti[temp]iGetiMini(Q)

iiiiiiai=ilefti[templibi=irighti[temp]
iiiiiiFi[temp]<-if[a]i+i[b]
iiiiiiinserti(Q,itemp)

iiii}

returniGet_mini(0)
}
StepsitoibuildiHuffmaniTree
Inputiisianiarrayiofiuniqueicharactersialongiwithitheirifrequencyiofioccurrencesiandioutput
iisiHuffmaniTree.i
1. Createiaileafinodeiforieachiuniqueicharacteriandibuildiaiminiheapiofiallileafinodesi(Min
iHeapiisiusediasiaipriorityiqueue.iTheivalueiofifrequencyifieldiisiuseditoicompareitwoin
odesiiniminiheap.iInitially,itheileastifrequenticharacteriisiatiroot)
2. Extractitwoinodesiwithitheiminimumifrequencyifromitheiminiheap.
i
3. Createiainewiinternalinodeiwithiaifrequencyiequalitoitheisumiofitheitwoinodesifrequen
cies.iMakeitheifirstiextractedinodeiasiitsileftichildianditheiotheriextractedinodeiasiitsirig
htichild.iAddithisinodeitoitheiminiheap.
4. Repeatisteps#2iandi#3iuntilitheiheapicontainsionlyioneinode.iTheiremaininginodeiisith
eirootinodeianditheitreeiisicomplete.
Letiusiunderstanditheialgorithmiwithianiexample:
characteriiiFrequency
iiiiaiiiiiiiiiiii5
iiiibiiiiiiiiiii9
iiiiciiiiiiiiiii12
iiiidiiiiiiiiiii13
iiiieiiiiiiiiiii16
iiiifiiiiiiiiiii45
Stepi1.iBuildiaiminiheapithaticontainsi6inodesiwhereieachinodeirepresentsirootiofiaitreei
withisingleinode.
Stepi2iExtractitwoiminimumifrequencyinodesifromiminiheap.iAddiainewiinternalinodeiwit
hifrequencyi5i+i9i=i14.i
i
Nowiminiheapicontainsi5inodesiwherei4inodesiareirootsiofitreesiwithisingleielementieach,i
andioneiheapinodeiisirootiofitreeiwithi3ielements
characteriiiiiiiiiiiFrequency
iiiiiiiciiiiiiiiiiiiiii12
iiiiiiidiiiiiiiiiiiiiii13
iInternaliNodeiiiiiiiii14
iiiiiiieiiiiiiiiiiiiiii16
iiiiiiifiiiiiiiiiiiiiiii45
Stepi3:iExtractitwoiminimumifrequencyinodesifromiheap.iAddiainewiinternalinodeiwithifre
quencyi12i+i13i=i25

Nowiminiheapicontainsi4inodesiwherei2inodesiareirootsiofitreesiwithisingleielementieach
,ianditwoiheapinodesiareirootiofitreeiwithimoreithanioneinodes
characteriiiiiiiiiiiFrequency
InternaliNodeiiiiiiiiii14
iiiiiiieiiiiiiiiiiiiiii16
InternaliNodeiiiiiiiiii25
iiiiiiifiiiiiiiiiiiiiii45
Stepi4:iExtractitwoiminimumifrequencyinodes.iAddiainewiinternalinodeiwithifrequencyi14
i+i16i=i30
Nowiminiheapicontainsi3inodes.

characteriiiiiiiiiiFrequency
InternaliNodeiiiiiiiii25
InternaliNodeiiiiiiiii30
iiiiiifiiiiiiiiiiiiiii45i
Stepi5:iExtractitwoiminimumifrequencyinodes.iAddiainewiinternalinodeiwithifrequencyi25
i+i30i=i55
Nowiminiheapicontainsi2inodes.

characteriiiiiFrequency
iiiiiiifiiiiiiiii45
InternaliNodeiiii55
Stepi6:iExtractitwoiminimumifrequencyinodes.iAddiainewiinternalinodeiwithifrequencyi45i
+i55i=i100

6b.iHowiwilliyouisolveithei4-QueensiproblemiusingitheiBacktrackingitechnique?
Answer
-iQueen'siproblem
Ini4-
iqueensiproblem,iweihavei4iqueensitoibeiplacedioniai4*4ichessboard,isatisfyingitheiconstra
intithatinoitwoiqueensishouldibeiinitheisameirow,isameicolumn,ioriinisameidiagonal.
Theisolutionispaceiaccordingitoitheiexternaliconstraintsiconsistsiofi4itoitheipoweri4,i4-
tuplesii.e.,iSii=i{1,i2,i3,i4}iandi1<=iIi<=4,iwhereasiaccordingitoitheiinternaliconstraintsithe
yiconsistiofi4!isolutionsii.e.,ipermutationiofi4.
Solutioniofi4i–iqueen’siwithitheihelpiofibacktracking
Weicanisolvei4-
queensiproblemithroughibacktrackingibyitakingiitiasiaiboundingifunctioni.iniuseitheicriterio
nithatiifi(x1,ix2,i…….,ixi)iisiaipathitoiaicurrentiE-
node,itheniallitheichildreninodesiwithiparent-
childilabelingsixi(i+1)iareisuchithati(x1,ix2,ix3,i…..,ix(i+1))irepresentsiaichessboardiconfig
urationiiniwhichinoiqueensiareiattacking.
Soiweistartiwithitheirootinodeiasitheionlyiliveinode.iThisitimeithisinodeibecomesitheiE-
nodeianditheipathiisi().iWeigenerateitheinextichild.iSupposeiweiareigeneratingitheichildiini
ascendingiorder.iThusitheinodeinumberi2iisigeneratediandipathiisinowi1ii.e.,itheiqueeni1iisi
placediinitheifirstirowiandiinitheifirsticolumn.
Now,inodei2ibecomesitheinextiE-
nodeiorilineinode.iFurther,itryitheinextinodeiinitheiascendinginodesii.e.,itheinodei3iwhichiis
ihavingix2i=i2imeansiqueeni2iisiplacediinitheisecondicolumnibutibyithisitheiqueeni1iandi2i
areionitheisameidiagonal,isoinodei3ibecomesideadihereisoiweibacktrackiitianditryitheinexti
nodeiwhichiisipossible.
Here,itheix2i=i3imeansitheiqueeni2iisiplacediinithei3rdicolumn.iAsiitisatisfiesiallitheiconstr
aintsisoiitibecomesitheinextiliveinode.
Afterithisitryiforinextinodei9ihavingix3i=i2iwhichimeansitheiqueeni3iplacediinithei2ndicolu
mn,ibutibyithisithei2iandi3iqueeniareionitheisameidiagonalisoiitibecomesidead.iNowiweitry
iforinextinodei11iwithix3i=i4,ibutiagainitheiqueensi2iandi3iareionitheisameidiagonalisoiitiis
ialsoiaideadinode
Now,itheinode13ibecomeitheinewiliveinodeiwithix2i=i4,imeansiqueeni2iisiplacediinithei4th
icolumn.iMoveitoitheinextinodei14.iItibecomesitheinextiliveinodeiwithix3i=i2imeansitheiqu
eeni3iisiplacediinithei2ndicolumn.iFurther,iweimoveitoitheinextinodei15iwithix4i=i3iasitheili
veinode.iButithisimakesitheiqueeni3iandi4ionitheisameidiagonaliresultingithisinodei15iisith
eideadinodeisoiweihaveitoibacktrackitoitheinodei14iandithenibacktrackitoitheinodei13iand
itryitheiotheripossibleinodei16iwithix3i=i3ibyithisialsoiweigetitheiqueensi2iandi3ionitheisa
meidiagonalisoitheinodeiisitheideadinode.

Soiweifurtheribacktrackitoitheinodei2ibutinoiotherinodeiisileftitoitryisoitheinodei2iisikilledi
soiweibacktrackitoitheinodei1ianditryianotherisub-
treeihavingix1i=i2iwhichimeansiqueeni1iisiplacediinithei2ndicolumn.

Nowiagainiwithitheisimilarireason,inodesi19iandi24iareikillediandisoiweitryiforitheinodei29i
withix2i=i4imeansitheiqueeni2iisiplacediinithei4thicolumnitheniweitryiforitheinodei30iwithi
x3i=i1iasiailiveinodeiandifinallyiweiproceeditoinextinodei31iwithix4i=i3imeansitheiqueeni4i
isiplacediini3rdicolumn.

Here,iallitheiconstraintsiareisatisfied,isoitheidesirediresultifori4iqueensiisi{2,i4,i1,i3}.

*************************************************************************

You might also like