59 - TOWERS OF HANOI PROBLEM - C PROGRAMMING

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends and welcome to our Channel so now let us see a one more example for the recursion so a very famous problem that is Thomas of an ID problem so as you know the problem statement there will be three hearts or pegs we can call it call them as either rods or pegs right so we will have the three takes or runs where the three discs will be dead so three two one right so we have to move these three discs from source to destination from source to destination so this is our problem statement so here we should follow some rules by moving this disc from source to destination so first first and foremost rule is only one disk should be moved at a time so Willie one disk should be moved at a time second higher this should not be placed on lower disk so based upon the size the hiatus should not be placed on lower size so here for example to can you clear some pretty but they can't be placed onto so this should not be happen and this can be happen so lower I mean higher this it should not be placed on lower this this is one constant and the third one so only topmost this we should be moon only compost disk should be moved so here if you consider this example we cannot move the disk third thing unless you move the disc one and two so you can't move the disc two unless you move the disc one so only the topmost this should be moved for all the disk should be moved from source to destination all this should be both from source to destination with few moves with fumes so we have to follow all these four conditions or rules for moving the discus from source to destination C so this can be happening by using the retention function let us see that so if you consider so this is a B and C so a is a source C is a destination B is an auxilary so by means of this auxilary we have to move the disc from here to see if there is only one is there is no option and it is very simple just move this disc from A to C as it is only one disc right if there are two discs if there are Buddhists so a B C if there are tourists first move this C 1 and to move one from A to B so run this one from A to B this tool from A to C and this one from B to C right so first remove this year and move to B this is the first aggression in the second iteration remove that this is from a C remove the disc from a mu to see this is the second direction third iteration remove the distal firm B place it on C this is the third iteration so in three simple steps we can get the result so let me draw the needle see if you consider this to describe to disturb their one and two so this is a big sea after first iteration as our rules only the top mostest should be more and first this should not be placed down lower this so one will be moved so this is two one will be moved from one to be so this is why right in the second iteration right this is y a b and c as here one is there now remove the disc from yeh and place it on C right in the third iteration remove the disc one from B and class it on C so this is a B and C this is the second one place it on place the first a disc on here so this is fun discs now what about the things if they're artists after the first iteration remove the faster disc so the year 1 2 & 3 remove the faster disc and place it on see faster disc in the second iteration a b c ii list will be more - so here 1 it is 2 and display right in the third iteration move the one of them above to this is one now this third Easter should be more from source to destination this is the third one second one first one so a B and C if your third here so for moving the last two days from source to destination with how to move the previous bit swim in previous discs - yeah - P so here the as it is there are three distance if we want to move the target is from source to destination first we have to move the remaining food is from source to Auslan and now we have to move the tourists from auxiliary to destination see in the next step so here we can observe yeah is having noticed means having pools please he is having thirties so here the third is two and move the one here right move the faster disc from D to e so here I would like so that you can move TLE understood so this one from A to C mr. Toole from A to B this one from C to B this three from A to C here this one from B to E right now next move this second disc to C so this two from B to C so that we will get three two now move the disc one from here to see so this one from A to C three two and so a so in simple steps we have moved all the three distance from A to C so here if you observe if you want to move the last days we have to move the remaining oil distance from source to oxygen so we will call this as source auxilary and destination source auxilary and destination right so the first system is if there is only one days we have to move it from Yale to C and if you are having more number of discs now move n minus 1 disk that was all the remaining discs except the last it is we have to go all the DS from source to auxilary with the help of destination so that we have done in this procedure so we have moved all the remaining two to disks that means this number to a distant one have been moved from source to auxilary so if you observe this step if you observe this step the two disks are moved from source to auxilary with the help of destination so after that move the last it is from PA to see now the next one next step is we have to move all that is from auxilary to destination with the help of source so with the help of source we are moving that is to induce one from B to C right so if you understood the simple logic we can write a simple program records show calls simple recursive quacks right so let us see that and then we will see the procedure for four disks first of all we will write a program writing all the recursive calls so that you will understand see ya for system if n is equal to one so if you n is equal to one move disc from source to destination si source and each destination source to destination next next one if n is more than one move all the N minus one disks open so N greater than party move n minus one disks from source to auxilary use the destination move n minus one this if you n is equal to three that this tutorial disk one should be moved from source to Godzilla with the help of this mission the next one obviously after completion of this one we will have only one disk in source so move disc from source to destination next again then in the second procedure we have to move all the N minus one disks from auxilary to destination move n minus one disks from auxiliary to destination using source so this is a simple procedure we need to follow right so now let us write the program so first main function right so I don't know I mean declare the value and therefore of this and rings a number of this so I will read the number of this key to Yin Yin next I will call the function right user-defined function so we call the user different function T or gate by passing the parameters the number of disks a which is a source C which is the destination V which is a auxilary right source - station and observe so close the main function now right under user-defined function tuh yeah comma here yeah we can replace this yeah as source C has destination be as auxilary so yeah will be assigned to so C will be our central to D and B will be to it so here sorry it in character source character destination character Godzilla Godzilla right so the letter A is assigned to yes C is assigned to D B is assigned to auxilary okay now what we have to do first we have to check for n is equal to here if your n is equal to M what we have to do but how to just move the disc from source to destination print if move this percentage D from percentage C 2 % HC krama here these yen C is source here C is distinguish hope you nostril positive C so it means a to destination means see a to see the disc will be directly moved from A to C here is a source sees the destination and B is the auxilary and simply later so we can stop then again like a sequel we have to record some right so if n is not equal to one apply the recursive function n minus 1 this to move n minus 1 disk from source right so put on ciliary with the help of this nation so we have to follow the same procedure AC and B right source destination Hogzilla so in this step the source is yes and the destination is auxilary so our in that particular recursive function that is station is on zealand so first we have to move the number n minus 1 this will come close to auxilary with their context mission so this will be recursive one and then after completion of this thing as we have seen the source will be having only one disk that is last it is simply we have to move that particular last it is from source to destination so again we have to write the same function printf move this percentage D from percentage C to % HC e n comma source common destination so what we understood we have to move n minus 1 distance from source to destination I mean here the registration is auxilary so strong seller with the help of destination so for this part I mean after completion of this recursion so we will get only one this in source so that we have to move from source to destination and know what is the next system we have to move - district from auxilary to destination with the help of so I can write the recursive call toh again these n minus one disks move from auxilary to destination using yes popularity to destination using yes so this is a simple program to implement this number soft on a problem so first n minus 1 this should be moved from source to auxilary with the help of destination then move the disc from source to destination now move the N minus 1 disk of an auxilary to destination so three steps so sedation so storms will early auxilary to destination right so now hope you understood this simple retention program now see the procedure for 4 disks so how to move four disks from source to destination in a step-by-step write see now we will see the procedure for four disks so before going to that once again we will see the rules and then we will move on to the procedure so these are the rules to be followed for solving this towers of Hanoi problem so the first rule is only one disk can be moved at a time and the second one is no larger larger this can appear on smaller disk and only one topmost disk can be moved from one tower and it can appear only at the topmost position of another tower and how to transfer all risk to the right hand tower in the fewest possible ways so these are the rules to be followed while solving this tower some finale so consider the three rods that is from rod auxilary in to Ron so we call the measure from as a source to as a destination and the middle one is auxilary so by means of this auxilary rod we have to move all the four disks from from broad to two rod now the first move so the first move is moving the disk one from from rod to auxilary rod now move the disc two from from road to tour on now move this one from auxilary or the two to rot so because here in the second iteration this tree is larger than this one and this too so discreet tree cannot be moved and placed on unclear order to rot so for that purpose disc one is smaller than this - so disc one will be moved to the disc one so now the next step this country will be more from firm harder to auxilary rod and the next iteration move the disc one from 2.2 from rod again move the disc two from - Radha to auxilary rod now move disc one from from rod to auxilary rod so up to now we have observed that n minus one rods are moved from source to auxilary rod here source is a from rod right from her to auxiliary rod we have moved all the three distance that means n minus one disks now in the next step the disc four is directly moved from source to destination right now our aim is mu again n minus one rods from source to destination I mean auxilary to destination so n minus one means all the three rods should be more from auxilary - to rod with the help of from God so move that this one from auxilary - to rod move that disc 2 from auxilary - from rock and now again the same constraint here disc 3 is larger than this one and disc 2 so this 2 is only rode in from rod this cone is the topmost rod in to lot so we can't move the disc 3 - either from rod or - rod so this one should be moved from - Radha - from rod now disc small smaller than disc four so disc tray will be moved from auxilary rod to to rot now move the disc one from from rod to auxilary rod so that de spittle will be directly moved to to rot now this one will be moved to Torah so this is the procedure to move all the four discs from from two to rod so here three steps we have seen first one is moving n minus-1 rods from from rod to auxilary rod move the last disc from from rod to to dot and move all the N minus one rods from auxilary not to two rod with the help of from rod so hope you understood the procedure now you can see the program implementing this towers of Hanoi so my laptop screen has been somewhat damaged so please adjust for the display so here I have declared the functions towers with a four parameters int character character and the character so first one is for number of disks second one is for source the third one is for destination and the fourth one is for auxilary now in the main function first I have read the number of disks so how many disks we have to perform this towers of Hanoi problem and then just I have made a function call towers number so in the number we are having the number of this and a I have labeled a character here here I am giving directly the value yay as a source so I have kept in inverted quotations and see as a destination and B as a auxilary now automatically whenever the control comes to this function call immediately the control moves to the function definition here I have written the function definition so int number character from pig so here a rod can also be called as a pig so from pig so here a will be assigned to from pink next to pick that is c is assigned to toothpick and auxilary pick B is assigned to auxilary pig so here we have to observe the order first we have to write the source next destination next auxilary so that's why I have written from two and auxilary so in that first one if n is equal to one that means here if n is equal to one that means only one disk is there directly we have to move that particular disk from from source to destination that is from peg 2 to paint next we have to call the references three steps to be taken first one we have to move all the N minus one paints I mean I'm n minus one disks from source to auxilary here observe our source is source destination this auxilary so I have written here from peg 2 exhilarate back with the help of two peg next in the source only one disk will be remaining we have to move that particular disk from from peg 2 to pick so this statement gives that moving off disk this from source 2 I mean from peg 2 to thing again we have to move all the N minus one disks which are in auxilary to to peg with the help of from peg so now here the source will be the auxilary destination will be the tool and auxilary will be the front so that's why I have written the order auxilary to and from so if you execute this one so if there are three this we will get a clear idea that is moving of this one from peg to peg see peg a to peg B taxi to peg B and then this three from peg to peg see disc one from peg to peg a disc two from peg B to peg see Peggy to peg C so here a is a source C is a destination and a B is a auxilary so if you again run this four finalists you can get the perfect result hope you understood the procedure for solving that Thomas off tonight so we have seen how to solve the three disks and how to solve the four DS and also we have implemented the program hope you understood everything so if you are having any doubts reading this C programming or these concepts so feel free to post your doubts in the comment section so that I will definitely try to clarify all your doubts and if you really understood my videos like my videos and share my videos with your friends and don't forget to subscribe to our channel so thanks for listening thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 87,346
Rating: undefined out of 5
Keywords: c programming, c language, compiler, c compiler, binary, object code, c code, coding, exe code, .c, .obj, .exe, c files, sundeep, saradhi, kanthety, cp, computer basics, programming, software, c programming language, c language for beginners, functions, recursion, towers of hanoi, toh, source, destination, pegs, disks, call by value, call by reference, parameter passing, program, computer trainee, computer training
Id: iluILbfzGKo
Channel Id: undefined
Length: 31min 54sec (1914 seconds)
Published: Tue Mar 13 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.