From b1d070a785b789cadf6b5e18b4386aeebf89ddd4 Mon Sep 17 00:00:00 2001 From: Quan Truong <147225279+quanxtruong@users.noreply.github.com> Date: Wed, 19 Jun 2024 08:13:17 -0500 Subject: [PATCH 1/7] Create code.js --- Simple Recursive/Tower Of Hanoi /code.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 Simple Recursive/Tower Of Hanoi /code.js diff --git a/Simple Recursive/Tower Of Hanoi /code.js b/Simple Recursive/Tower Of Hanoi /code.js new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/Simple Recursive/Tower Of Hanoi /code.js @@ -0,0 +1 @@ + From 9c6633c0c5ba15152c012b7e67e0d191e5b8f654 Mon Sep 17 00:00:00 2001 From: quanxtruong Date: Thu, 20 Jun 2024 22:33:09 -0500 Subject: [PATCH 2/7] towerOfHanoi working--may add comments --- Simple Recursive/Tower Of Hanoi /code.js | 86 ++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/Simple Recursive/Tower Of Hanoi /code.js b/Simple Recursive/Tower Of Hanoi /code.js index 8b137891..0f5799ca 100644 --- a/Simple Recursive/Tower Of Hanoi /code.js +++ b/Simple Recursive/Tower Of Hanoi /code.js @@ -1 +1,87 @@ +// import visualization libraries { +const { Tracer, Array2DTracer, LogTracer, Layout, VerticalLayout } = require('algorithm-visualizer'); +// } +// define tracer variables { +const towerTracer = new Array2DTracer('Towers Of Hanoi'); +const logTracer = new LogTracer('Progress'); +// } + +let discs = 3; // just change the value of discs and the visuals will reflect how many discs are stacked! +const towers = (function createTowers(N) { + const startState = Array(N).fill().map(() => Array(3).fill(0)); + for (let k = 1, i = 0; k <= discs; k++, i++) { + startState[i][0] = k; + } + return startState; +}(discs)); + + +function solveHanoi(n, from_rod, to_rod, aux_rod) { + // Base Case + if (n === 0) { + return; + } + + solveHanoi(n - 1, from_rod, aux_rod, to_rod); + + // Logger { + logTracer.println(`Move disk ${n} from rod ${from_rod} to rod ${to_rod}
`); + // } + + const fromIndex = parseInt(from_rod, 10) - 1; + const toIndex = parseInt(to_rod, 10) - 1; + + // Find topmost disk: from_rod -> to_rod + let disk = 0; + for (let i = 0; i < discs; i++) { + if (towers[i][fromIndex] !== 0) { + disk = towers[i][fromIndex]; + towers[i][fromIndex] = 0; + // visualize the moves { + towerTracer.select(i,fromIndex); + Tracer.delay() + towerTracer.patch(i, fromIndex, 0); + towerTracer.depatch(i, fromIndex) + towerTracer.deselect(i,fromIndex); + // } + break; + } + } + for (let i = discs - 1; i >= 0; i--) { + if (towers[i][toIndex] === 0) { + towers[i][toIndex] = disk; + // visualize the moves { + towerTracer.select(i,toIndex); + Tracer.delay(); + towerTracer.patch(i, toIndex, disk); + Tracer.delay(); + towerTracer.depatch(i, toIndex); + Tracer.delay() + towerTracer.deselect(i,toIndex); + // } + break; + } + } + + solveHanoi(n - 1, aux_rod, to_rod, from_rod); +} + +(function main() { + // visualize { + Layout.setRoot(new VerticalLayout([towerTracer, logTracer])); + towerTracer.set(towers); + logTracer.println() + Tracer.delay(); + // } + solveHanoi(discs, '1', '3', '2'); +})(); + + + + + + + + + \ No newline at end of file From 0bf36a59761d4737ba71326919e8d162a42d92ee Mon Sep 17 00:00:00 2001 From: quanxtruong Date: Fri, 21 Jun 2024 08:34:28 -0500 Subject: [PATCH 3/7] changed the title for more accuracy --- Simple Recursive/Towers Of Hanoi /README.md | 28 +++++++++++++++++++ .../code.js | 0 2 files changed, 28 insertions(+) create mode 100644 Simple Recursive/Towers Of Hanoi /README.md rename Simple Recursive/{Tower Of Hanoi => Towers Of Hanoi }/code.js (100%) diff --git a/Simple Recursive/Towers Of Hanoi /README.md b/Simple Recursive/Towers Of Hanoi /README.md new file mode 100644 index 00000000..8e995cdd --- /dev/null +++ b/Simple Recursive/Towers Of Hanoi /README.md @@ -0,0 +1,28 @@ +# Towers Of Hanoi + +Visualize your own code here! + +## Learning About Tracers + +The project [Algorithm Visualizer](https://github.com/algorithm-visualizer) has a visualization library in each +supported language ([JavaScript](https://github.com/algorithm-visualizer/tracers.js) +, [C++](https://github.com/algorithm-visualizer/tracers.cpp), +and [Java](https://github.com/algorithm-visualizer/tracers.java)) to visualize codes. + +There are five tracers in the library to visualize different types of data: + +- [Array1DTracer](https://github.com/algorithm-visualizer/algorithm-visualizer/wiki/Array1DTracer) +- [Array2DTracer](https://github.com/algorithm-visualizer/algorithm-visualizer/wiki/Array2DTracer) +- [ChartTracer](https://github.com/algorithm-visualizer/algorithm-visualizer/wiki/ChartTracer) +- [GraphTracer](https://github.com/algorithm-visualizer/algorithm-visualizer/wiki/GraphTracer) +- [LogTracer](https://github.com/algorithm-visualizer/algorithm-visualizer/wiki/LogTracer) + +There are also randomizers to help you create random data. + +Check out the [API reference](https://github.com/algorithm-visualizer/algorithm-visualizer/wiki) for more information. + +## Making Your Visualization Public + +If you think other people would find your visualization useful, you can add it to the side menu +by [contributing to `algorithm-visualizer/algorithms`](https://github.com/algorithm-visualizer/algorithms/blob/master/CONTRIBUTING.md) +. \ No newline at end of file diff --git a/Simple Recursive/Tower Of Hanoi /code.js b/Simple Recursive/Towers Of Hanoi /code.js similarity index 100% rename from Simple Recursive/Tower Of Hanoi /code.js rename to Simple Recursive/Towers Of Hanoi /code.js From e8e4e02a7db1b728119cea0403b083efc1c63f34 Mon Sep 17 00:00:00 2001 From: quanxtruong Date: Fri, 21 Jun 2024 08:39:50 -0500 Subject: [PATCH 4/7] changed the title, starting adding README --- Simple Recursive/Tower Of Hanoi/README.md | 3 ++ .../code.js | 0 Simple Recursive/Towers Of Hanoi /README.md | 28 ------------------- 3 files changed, 3 insertions(+), 28 deletions(-) create mode 100644 Simple Recursive/Tower Of Hanoi/README.md rename Simple Recursive/{Towers Of Hanoi => Tower Of Hanoi}/code.js (100%) delete mode 100644 Simple Recursive/Towers Of Hanoi /README.md diff --git a/Simple Recursive/Tower Of Hanoi/README.md b/Simple Recursive/Tower Of Hanoi/README.md new file mode 100644 index 00000000..bb70edd9 --- /dev/null +++ b/Simple Recursive/Tower Of Hanoi/README.md @@ -0,0 +1,3 @@ +# Tower Of Hanoi + +The **Tower of Hanoi** are a puzzle \ No newline at end of file diff --git a/Simple Recursive/Towers Of Hanoi /code.js b/Simple Recursive/Tower Of Hanoi/code.js similarity index 100% rename from Simple Recursive/Towers Of Hanoi /code.js rename to Simple Recursive/Tower Of Hanoi/code.js diff --git a/Simple Recursive/Towers Of Hanoi /README.md b/Simple Recursive/Towers Of Hanoi /README.md deleted file mode 100644 index 8e995cdd..00000000 --- a/Simple Recursive/Towers Of Hanoi /README.md +++ /dev/null @@ -1,28 +0,0 @@ -# Towers Of Hanoi - -Visualize your own code here! - -## Learning About Tracers - -The project [Algorithm Visualizer](https://github.com/algorithm-visualizer) has a visualization library in each -supported language ([JavaScript](https://github.com/algorithm-visualizer/tracers.js) -, [C++](https://github.com/algorithm-visualizer/tracers.cpp), -and [Java](https://github.com/algorithm-visualizer/tracers.java)) to visualize codes. - -There are five tracers in the library to visualize different types of data: - -- [Array1DTracer](https://github.com/algorithm-visualizer/algorithm-visualizer/wiki/Array1DTracer) -- [Array2DTracer](https://github.com/algorithm-visualizer/algorithm-visualizer/wiki/Array2DTracer) -- [ChartTracer](https://github.com/algorithm-visualizer/algorithm-visualizer/wiki/ChartTracer) -- [GraphTracer](https://github.com/algorithm-visualizer/algorithm-visualizer/wiki/GraphTracer) -- [LogTracer](https://github.com/algorithm-visualizer/algorithm-visualizer/wiki/LogTracer) - -There are also randomizers to help you create random data. - -Check out the [API reference](https://github.com/algorithm-visualizer/algorithm-visualizer/wiki) for more information. - -## Making Your Visualization Public - -If you think other people would find your visualization useful, you can add it to the side menu -by [contributing to `algorithm-visualizer/algorithms`](https://github.com/algorithm-visualizer/algorithms/blob/master/CONTRIBUTING.md) -. \ No newline at end of file From 27d5f2e340dae9690da789a72bcc84ea6b2f84e9 Mon Sep 17 00:00:00 2001 From: quanxtruong Date: Fri, 21 Jun 2024 09:03:37 -0500 Subject: [PATCH 5/7] half of readme done, need to complete algorithm explanations --- Simple Recursive/Tower Of Hanoi/README.md | 34 ++++++++++++++++++++++- Simple Recursive/Tower Of Hanoi/code.js | 31 +++++++++++++-------- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/Simple Recursive/Tower Of Hanoi/README.md b/Simple Recursive/Tower Of Hanoi/README.md index bb70edd9..5e5512d4 100644 --- a/Simple Recursive/Tower Of Hanoi/README.md +++ b/Simple Recursive/Tower Of Hanoi/README.md @@ -1,3 +1,35 @@ # Tower Of Hanoi -The **Tower of Hanoi** are a puzzle \ No newline at end of file +The **Tower of Hanoi** is a mathematical game or puzzle consisting of `3` rods and +a `N` number of disks of various diameters, which can slide onto any rod. +The puzzle begins with the disks stacked on one rod in order of decreasing size, +the smallest at the top. In this case, each *rod* is represented by a *column of the 2D Array or Matrix*, +with the original peg being the column indexed as `0` and the destination peg being column indexed as `2`. +The objective of the puzzle is to move the entire stack to one of the other rods, obeying the following rules: + +`1) `Only one disk may be moved at a time. + +`2) `Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod. + +`3) `No disk may be placed on top of a disk that is smaller than it. + +For example, the following is the steps of a solution for The Tower of Hanoi with `4` disks: + +![Tower of Hanoi](https://www.numerit.com/samples/hanoi/fig3.gif) + +The expected output is a matrix that has all of the disks in column `2` +in increasing order, that being the smallest valued disk and the top and the highest +valued disk at the bottom. + +For example, the following is the input and output matrix for a 4 disk Tower of Hanoi soluion. +``` +Input Output +{ 1, 0, 0, 0} { 0, 0, 0, 1} +{ 2, 0, 0, 0} ----------> { 0, 0, 0, 2} +{ 3, 0, 0, 0} ----------> { 0, 0, 0. 3} +{ 4, 0, 0, 0} { 0, 0, 0, 4} +``` + +The visualizer provides assistance in visualizing the moves while simultaneously confirming all rules are abided by in each step. + +--- diff --git a/Simple Recursive/Tower Of Hanoi/code.js b/Simple Recursive/Tower Of Hanoi/code.js index 0f5799ca..30aae468 100644 --- a/Simple Recursive/Tower Of Hanoi/code.js +++ b/Simple Recursive/Tower Of Hanoi/code.js @@ -7,15 +7,21 @@ const towerTracer = new Array2DTracer('Towers Of Hanoi'); const logTracer = new LogTracer('Progress'); // } -let discs = 3; // just change the value of discs and the visuals will reflect how many discs are stacked! +let disks = 4; // just change the value of disks, build, and then the visuals will reflect how many disks are stacked! const towers = (function createTowers(N) { const startState = Array(N).fill().map(() => Array(3).fill(0)); - for (let k = 1, i = 0; k <= discs; k++, i++) { + for (let k = 1, i = 0; k <= disks; k++, i++) { startState[i][0] = k; } return startState; -}(discs)); +}(disks)); +// Logger { +logTracer.println("Tower of Hanoi Puzzle: Each column represents one of the three rods"); +logTracer.println(" Smaller valued \"disks\" cannot stack on bigger valued \"disks\""); +logTracer.println("-------------------------------------------------------------------"); +logTracer.println("Solution with " + disks + " disks:"); +// } function solveHanoi(n, from_rod, to_rod, aux_rod) { // Base Case @@ -34,7 +40,7 @@ function solveHanoi(n, from_rod, to_rod, aux_rod) { // Find topmost disk: from_rod -> to_rod let disk = 0; - for (let i = 0; i < discs; i++) { + for (let i = 0; i < disks; i++) { if (towers[i][fromIndex] !== 0) { disk = towers[i][fromIndex]; towers[i][fromIndex] = 0; @@ -48,7 +54,7 @@ function solveHanoi(n, from_rod, to_rod, aux_rod) { break; } } - for (let i = discs - 1; i >= 0; i--) { + for (let i = disks - 1; i >= 0; i--) { if (towers[i][toIndex] === 0) { towers[i][toIndex] = disk; // visualize the moves { @@ -71,17 +77,18 @@ function solveHanoi(n, from_rod, to_rod, aux_rod) { // visualize { Layout.setRoot(new VerticalLayout([towerTracer, logTracer])); towerTracer.set(towers); - logTracer.println() Tracer.delay(); + logTracer.println("Starting execution"); + logTracer.println("------------------"); // } - solveHanoi(discs, '1', '3', '2'); + solveHanoi(disks, '1', '3', '2'); })(); - - - - - + + + + + \ No newline at end of file From c844b8748bd319013deb9f2d8ee5761d30f00eb5 Mon Sep 17 00:00:00 2001 From: quanxtruong Date: Sun, 23 Jun 2024 16:24:36 -0500 Subject: [PATCH 6/7] TOH algorithm and README finished --- Simple Recursive/Tower Of Hanoi/README.md | 88 ++++++++++++++++++++++- Simple Recursive/Tower Of Hanoi/code.js | 18 ++--- 2 files changed, 94 insertions(+), 12 deletions(-) diff --git a/Simple Recursive/Tower Of Hanoi/README.md b/Simple Recursive/Tower Of Hanoi/README.md index 5e5512d4..3f40d689 100644 --- a/Simple Recursive/Tower Of Hanoi/README.md +++ b/Simple Recursive/Tower Of Hanoi/README.md @@ -7,11 +7,11 @@ the smallest at the top. In this case, each *rod* is represented by a *column of with the original peg being the column indexed as `0` and the destination peg being column indexed as `2`. The objective of the puzzle is to move the entire stack to one of the other rods, obeying the following rules: -`1) `Only one disk may be moved at a time. +- Only one disk may be moved at a time. -`2) `Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod. +- Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod. -`3) `No disk may be placed on top of a disk that is smaller than it. +- No disk may be placed on top of a disk that is smaller than it. For example, the following is the steps of a solution for The Tower of Hanoi with `4` disks: @@ -33,3 +33,85 @@ Input Output The visualizer provides assistance in visualizing the moves while simultaneously confirming all rules are abided by in each step. --- + +## Iterative Solution + +The iterative solution is equivalent to the execution of the following sequence of steps repeatedly until the Tower of Hanoi is solved: + +`1) `Move one disk from peg A to peg B or vice versa, whichever move is legal. + +`2) `Move one disk from peg A to peg C or vice versa, whichever move is legal. + +`3) `Move one disk from peg B to peg C or vice versa, whichever move is legal. + +Following this approach, the stack will end up on peg B if the number of disks is odd and peg C if it is even. + +Below is a visualization of the iterative solution with 6 disks: + +![Iterative Solution](https://upload.wikimedia.org/wikipedia/commons/8/8d/Iterative_algorithm_solving_a_6_disks_Tower_of_Hanoi.gif) + +--- + +## Recursive Solution + +This is the solution used for this current iteration of the Tower of Hanoi algorithm visualizer. + +The Tower of Hanoi is a puzzle that boils down to a collection of smaller sub-problems. +For the Towers of Hanoi: + +- Label the pegs A, B, C. + +- Let n be the total number of disks. + +- Number the disks from 1 (smallest, topmost) to n (largest, bottom-most). + +Assuming all n disks are distributed in valid arrangements among the pegs; assuming there are m top disks on a source peg, and all the rest of the disks are larger than m, so they can be safely ignored; to move m disks from a source peg to a target peg using a spare peg, without violating the rules: + +`1)` Move m − 1 disks from the source to the spare peg, by the *same general solving procedure*. Rules are not violated, by assumption. This leaves the disk m as a top disk on the source peg. + +`2)` Move the disk m from the source to the target peg, which is guaranteed to be a valid move, by the assumptions — a simple step. + +`3)` Move the m − 1 disk that we have just placed on the spare, from the spare to the target peg by the same general solving procedure, so they are placed on top of the disk m without violating the rules. + +The base case is to move 0 disks (in steps 1 and 3), that is, do nothing that does not violate the rules (which in this case is just a simple return statement). + +The full Tower of Hanoi solution then moves n disks from the source peg A to the target peg C, using B as the spare peg. + +Below is a general psuedo-code used to implement the recursive solution: +``` +RecursiveHanoi(n, s, a, d) { + // INPUT + // n = the number of disks + // s = the source rod + // a = the auxiliary rod + // d = the destination rod + + // Base Case + if (n == 0) { + return + } + + // 1) + RecursiveHanoi(n - 1, s, d, a) + + // 2) + move from s peg to d peg + + // 3) + RecursiveHanoi(n - 1, a, s, d) +} +``` +--- + +## Time and Space Complexity +- **Time**: ![](https://latex.codecogs.com/svg.latex?O(2^N)) +- **Space**: ![](https://latex.codecogs.com/svg.latex?O(N)) + +where `N = number of disks` + +## References +- [IIT Kanpur](https://www.iitk.ac.in/esc101/08Jan/lecnotes/lecture32.pdf) +- [GeeksForGeeks](https://www.geeksforgeeks.org/c-program-for-tower-of-hanoi/) +- [Wikipedia](https://en.wikipedia.org/wiki/Tower_of_Hanoi) + + diff --git a/Simple Recursive/Tower Of Hanoi/code.js b/Simple Recursive/Tower Of Hanoi/code.js index 30aae468..cc2eed5f 100644 --- a/Simple Recursive/Tower Of Hanoi/code.js +++ b/Simple Recursive/Tower Of Hanoi/code.js @@ -32,7 +32,7 @@ function solveHanoi(n, from_rod, to_rod, aux_rod) { solveHanoi(n - 1, from_rod, aux_rod, to_rod); // Logger { - logTracer.println(`Move disk ${n} from rod ${from_rod} to rod ${to_rod}
`); + logTracer.println(`Moving disk ${n} from rod ${from_rod} to rod ${to_rod}
`); // } const fromIndex = parseInt(from_rod, 10) - 1; @@ -83,12 +83,12 @@ function solveHanoi(n, from_rod, to_rod, aux_rod) { // } solveHanoi(disks, '1', '3', '2'); })(); - - - - - - - - \ No newline at end of file + + + + + + + + \ No newline at end of file From 309ffb4b3650a83cc3044d2f0651779ef6285573 Mon Sep 17 00:00:00 2001 From: quanxtruong Date: Sun, 23 Jun 2024 16:29:37 -0500 Subject: [PATCH 7/7] TOH final touchups --- Simple Recursive/Tower Of Hanoi/code.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Simple Recursive/Tower Of Hanoi/code.js b/Simple Recursive/Tower Of Hanoi/code.js index cc2eed5f..e6454a01 100644 --- a/Simple Recursive/Tower Of Hanoi/code.js +++ b/Simple Recursive/Tower Of Hanoi/code.js @@ -7,7 +7,7 @@ const towerTracer = new Array2DTracer('Towers Of Hanoi'); const logTracer = new LogTracer('Progress'); // } -let disks = 4; // just change the value of disks, build, and then the visuals will reflect how many disks are stacked! +let disks = 5; // just change the value of disks, build, and then the visuals will reflect how many disks are stacked! const towers = (function createTowers(N) { const startState = Array(N).fill().map(() => Array(3).fill(0)); for (let k = 1, i = 0; k <= disks; k++, i++) { @@ -82,13 +82,16 @@ function solveHanoi(n, from_rod, to_rod, aux_rod) { logTracer.println("------------------"); // } solveHanoi(disks, '1', '3', '2'); + // logger { + logTracer.println("Execution Finished: all pegs moved from source peg to destination peg (rod 1 --> rod 3)"); + // } })(); - - - - - - - - \ No newline at end of file + + + + + + + + \ No newline at end of file