Project name: TOWER OF HANOI Mini Project Design and Analysis of Algoritm
!"#MITTED #$ %&mar Niles
College Roll No: 13438 University University Roll No: 1320801 Computer Science & Engineering RIMT Instit&te of Engineering and Tecnology Tecnology Mandi 'o(indgar
!"PER)I!ED #$
FR!"N#ER S"N$% ssistnt 'ro(essor Computer Science & Engineering R"!) "nstitute o( Engineering Engine ering & )ec*nology+ )ec*nology+ !n,i $o-in,gr*
R"!) "nstitute o( Engineering Engineer ing n, )ec*nology+ !n,i $o-in,gr*
A*%NOW+ED'MENT
)*e stis(ction t*t ccompnies t*t t*e success(ul completion o( ny ts. /oul, -e incomplete /it*out t*e mention o( person /*ose ceseless coopertion m,e it possi-le /*ose constnt gui,e n, encourgement cro/n ll e((orts /it* success " m grtitu,e to my proect gui,e !r Frmin,er Sing* (or t*e gui,nce+ inspirtion n, constructive suggestion t*t *elp(ul me in t*e preprtion o( my t*is mini proect
TA#+E OF *ONTENT
,./01234-
vervie/1 rigin o( )o/er o( %noi2 ppliction Source co,e3 utput Eplntion o( co,ing4 Conclusion5 6i-liogrp*y7
O5er5ie6
)*e )o/er o( %noi lso clle, t*e )o/er o( 6r*m or ucs is mt*emticl gme or pu99le "t consists o( t*ree ro,s+ n, num-er o( ,is.s o( ,i((erent si9es /*ic* cn sli,e onto ny ro, )*e pu99le strts /it* t*e ,is.s in net stc. in scen,ing or,er o( si9e on one ro,+ t*e smllest t t*e top+ t*us m.ing conicl s*pe )*e o-ective o( t*e pu99le is to move t*e entire stc. to not*er ro,+ o-eying t*e (ollo/ing simple rules: nly one ,is. cn -e move, t time Ec* move consists o( t.ing t*e upper ,is. (rom one o( t*e stc.s n, plcing it on top o( not*er stc. ie ,is. cn only -e move, i( it is t*e uppermost ,is. on stc. No ,is. my -e plce, on top o( smller ,is. it* t*ree ,is.s+ t*e pu99le cn -e solve, in seven moves )*e minimum num-er o( moves re;uire, to solve )o/er o( %noi pu99le is 2n < 1+ /*ere n is t*e num-er o( ,is.s • •
• •
)*e pu99le cn -e plye, /it* ny num-er o( ,is.s+ lt*oug* mny toy versions *ve roun, seven to nine o( t*em )*e minimum num-er o( moves re;uire, to solve )o/er o( %noi pu99le is 2n < 1+ /*ere n is t*e num-er o( ,is.s
Origin
)*e pu99le /s invente, -y t*e Frenc* mt*emticin =,our, ucs in 1883 )*ere is story -out n "n,in temple in >s*i ?is*/nt* /*ic* contins lrge room /it* t*ree time
1 )*e )o/er o( %noi is (re;uently use, in psyc*ologicl reserc* on pro-lem solving 2 "t is lso use, s 6c.up rottion sc*eme /*en per(orming computer ,t 6c.ups /*ere multiple tpesBme,i re involve, 3 )o/er o( %noi is populr (or tec*ing recursive lgorit*ms to -eginning progrmming stu,ents 4 )*e )o/er o( %noi is lso use, s test -y neuropsyc*ologists trying to evlute (rontl lo-e ,e(icits
An E8am7le Arrangement of Dis9s for te To6er of Hanoi
%ere is *ig*
s long s /e l/ys o-ey t*e rule t*t t*e lrger ,is.s remin on t*e -ottom o( t*e stc.+ /e cn use t*e t*ree steps -ove recursively+ treting ny lrger ,is.s s t*oug* t*ey /ere not even t*ere )*e only t*ing missing (rom t*e outline -ove is t*e i,enti(iction o( -se cse )*e simplest )o/er o( %noi pro-lem is to/er o( one ,is. "n t*is cse+ /e nee, move only single ,is. to its (inl ,estintion to/er o( one ,is. /ill -e our -se cse %ere is t*e lgorit*m to move t*e ,isc (rom source to ,estintion ,e( move)o/er*eig*t+(rom'ole+ to'ole+ /it*'oleD: i( *eig*t 1: move)o/er *eig*t<1+ (rom'ole+/it*'ole+to'oleD move#is.(rom'ole+to'oleD move)o/er*eig*t<1+/it*'ole+to'ole+(rom'oleD ,e( move#is.(p+tpD: printGmoving ,is. (romG+(p+GtoG+tpD move)o/er3+GG+G6G+GCGD
output :< moving ,is. (rom to 6 moving ,is. (rom to C moving ,is. (rom 6 to C moving ,is. (rom to 6 moving ,is. (rom C to moving ,is. (rom C to 6 moving ,is. (rom to 6
!o&rce code of To6er of Hanoi
BB 'rogrm )o "mplement )o/er ( %noi lgorit*m Using Recursion )*is progrm s*o/s t*e movements o( ,is. (rom one to/er to not*er /*en .ey is presse, Hinclu,eIiostrem* Hinclu,eIst,io* Hinclu,eIconio* clss to/er J int Kt1+Kt2+Kt3L int +y+9L pu-lic: voi, ,ispMto/erDL voi, moveM,is.int t+int tyDL voi, to*int n+int +int -+int cDL to/erint noDL to/erDL OL to/er :: to/erint noD J t1 ne/ intPnoQ1L t2 ne/ intPnoQ1L t3 ne/ intPnoQ1L noL y 9 0L (orint i0+no L iIno L iQQ+<
coutIIG :: GL (orint i0LiILiQQD J coutIIG GIIt1PiL O coutIIG T :: GL (ori0LiIyLiQQD J coutIIG GIIt2PiL O coutIIG :: GL (ori0LiI9LiQQD J coutIIG GIIt3PiL O getc*DL O voi, to/er :: to*int n+int t+int ty+int t9D J i(n1D J to*n<1+t+t9+tyDL moveM,is.t+tyDL ,ispMto/erDL to*n<1+t9+ty+tDL O
BB to y using 9
BB to y
O voi, to/er :: moveM,is.int t+int tyD J s/itc*tD J cse 1: J i(ty2D t2PyQQ t1P<<L else t3P9QQ t1P<<L
O -re.L cse 2: J i(ty1D t1PQQ t2P<
O&t7&t:
E87lanation of coding
)*is progrm uses recursion t*is progrm s*o/s t*e movement o( ,is. (rom one to/er to not*er "n t*is progrm o( )o/er o( %noi /e (irst crete clss to/er n, in t*is clss /e crete t*ree pointer vri-le ie )1+t2+t3 (or t*ree ro,s n, correspon,ingly /e cn t.e t*ree vri-les s c+y+9 *ere /e t.e t*ree met*o,s:< ?oi, ,ispMto/erDL ?oi, moveM,is.DL ?oi, to*DL %ere /e lso use t*e constructor n, ,estructor "n t*e constructor /e initili9es t*e vlues o( ec* vri-le "n t*e ,estructor /e use t*e ,elete met*o, to (reet*e llocte, memory "n t*e ,ispMto/erD met*o, /e ,isply t*e vlues o( t*e respective t1+t2 & t3 vri-les "n t*e to*D met*o, + it c*ec.s t*e con,ition /*et*er t*e input si9e is greter t*n 1 or not & t*en /e cll t*e to*D+ moveM,is.D+,ispMto/erD n, gin t*e to*D met*o, "n t*e moveM,is.D met*o, + /e cn use t*e s/itc* cse to move t*e ,is. -et/een source + ,est n, interme,ite no,es t lst in t*e min met*o, + /e enter t*e num-er o( ,is.s to move t*e ,is.s to ,estintion point + t*en t*e constructor is utomticlly clle, s /e crete t*e o-ect o( t*e to/er clss + t*en /e cll t*e ,ispMto/erD met*o, n, to*D met*o,
*oncl&sion
From t*e proper nlysis + t*is proect tells t*t /e cn move t*e num-er o( ,iscs (rom source to ,estintion ro, /it* t*e *elp o( one interme,ite ro, in suc* /y t*t smllest ,isc is plce, t t*e top o( t*e ro, n, lrgest one is plce, t t*e -ottom+ t*us m.ing conicl s*pe )*is is use, in psyc*ologicl reserc* on pro-lem solving )*is concept is lso use, in t*e ,evelopment o( t*e )URF (rme/or. (or t*e representtion o( *umn computer "nterction
#i(liogra7y
6oo.s :< cQQ progrmming -y E-lgurusmy #t structure -y tsc*um series e-site :< *ppyco,ingcom Co,ing(ocom