Skip to content

Commit 04f1aec

Browse files
committed
Added examples
1 parent 36e6c36 commit 04f1aec

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
1-
# Hanoi-Matlab
1+
# Hanoi Matlab
2+
3+
A simple function that generates a graph object of the possible moves in a game of Hanoi with `numOfPegs` pegs and `numOfDisks` disks.
4+
5+
## Explanation of the graph
6+
Each node represents a possible state of the Hanoi game and each node is represented by a string with `numOfDisks` characters where the `k`-th element represents the position of the `k`-th biggest disk, so ABC means the biggest disk is on the first peg, the second biggest is on the second one and the smallest one is on the third peg.
7+
8+
Each edge represents a valid move in the game.
9+
10+
## Examples
11+
12+
### 2D Graph
13+
```
14+
G = hanoi(3, 3);
15+
plot(G);
16+
```
17+
18+
![3_3.jpg](images/3_3.jpg)
19+
20+
### 3D Graph
21+
```
22+
G = hanoi(2, 4);
23+
p = plot(G);
24+
layout(p, 'force3');
25+
view(3);
26+
```
27+
28+
![2_4_3d.jpg](images/2_4_3d.jpg)
29+
30+
### Shortest path
31+
```
32+
G = hanoi(3, 3);
33+
p = plot(G);
34+
path = shortestpath(G, 'AAA', 'CCC');
35+
highlight(p, path, 'NodeColor', 'r', 'EdgeColor', 'r');
36+
```
37+
38+
![3_3_shortest.jpg](images/3_3_shortest.jpg)

hanoi.m

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
% Returns a graph object representing the possible moves in a game of Hanoi
2+
% with numOfDisks disks and numOfPegs pegs.
3+
%
4+
% Each node is a state of the Hanoi game and each edge is a move.
5+
%
6+
% A state is represented with a vector whreich has numOfDisks elements and
7+
% the k-th element represents the current position of (numOfDisks - k)-th
8+
% disk. So if we have a game of 3 disks and 3 pegs and the state is [1 2
9+
% 3], the smallest disk is on the third peg and the biggest is on the first
10+
% peg.
111
function G = hanoi(numOfDisks, numOfPegs)
212
numOfNodes = numOfPegs^numOfDisks;
313
numOfEdges = 0;
@@ -12,7 +22,6 @@
1222
toVector = numberToVectorInBase(to - 1);
1323

1424
if isValidEdge(fromVector, toVector)
15-
%disp([getNameOfNode(fromVector), ' ', getNameOfNode(toVector)]);
1625
beginEdge(numOfEdges + 1) = from;
1726
endEdge(numOfEdges + 1) = to;
1827
numOfEdges = numOfEdges + 1;
@@ -24,6 +33,15 @@
2433

2534
G = graph(beginEdge, endEdge, ones(1, numOfEdges), names);
2635

36+
% Checks if an edge is a valid move in Hanoi
37+
% For it to be valid, it needs to satisfy three conditions:
38+
%
39+
% 1) Only one disk can be moved in one move
40+
% 2) You cannot move a disk which has a smaller disk on it
41+
% 3) You cannot move a disk to a peg which already has a smaller disk
42+
% on it
43+
%
44+
% Returns 1 if it is valid, 0 if it is invalid
2745
function validEdge = isValidEdge(fromVector, toVector)
2846
numOfChanges = 0;
2947
firstChanged = -1;

images/2_4_3d.jpg

20.1 KB
Loading

images/3_3.jpg

18.3 KB
Loading

images/3_3_shortest.jpg

18.4 KB
Loading

0 commit comments

Comments
 (0)