You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* add .get() method to get all (filtered) results as an array, from current position to end
* add magic squares combinatorial example and update n-queens example
* update examples/readme
Copy file name to clipboardexpand all lines: examples/README.md
+164-29
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,13 @@
1
1
# Combinatorial Solutions to Popular Problems using Abacus
2
2
3
3
4
-
The solutions, to popular problems, below exhibit the combinatorial capabilities of solving complex combinatorial problems of `Abacus`. Sometimes a solution is found by exhaustive search, other times better than full exhaustive search can be achieved and other times the solution does not require search at all but simply smart composition and manipulation of appropriate combinatorial objects.
4
+
The solutions, to popular problems, below exhibit the combinatorial capabilities, for solving complex combinatorial problems, of `Abacus` library. Sometimes a solution is found by exhaustive search, other times better than full exhaustive search can be achieved and other times the solution does not require search at all but simply smart composition and manipulation of appropriate combinatorial objects.
5
5
6
6
7
7
### Contents
8
8
9
9
*[N-Queens Problem](#n-queens)
10
+
*[Magic Squares](#magic-squares)
10
11
*[Knapsack Problem](#knapsack)
11
12
*[TSP Problem](#tsp)
12
13
@@ -19,74 +20,208 @@ see associated file: `examples/n_queens.js`
19
20
20
21
**Exhaustive Search**
21
22
22
-
Let's assume we have some utility methods which allow us to check if a certain potential solution configuration is valid `isValid` and also (we need that as well) a method (`toRowColumn`) to map numeric patterns of combinatorial objects to `(row,column)` pairs on a hypothetical `NxN` grid.
23
+
Let's assume we have some utility methods which allow us to check if a certain potential solution configuration is valid `is_valid` and also (we need that as well) a method (`row_column`) to map numeric patterns of combinatorial objects to `(row,column)` pairs on a hypothetical `NxN` grid. Also assume we have a utility method to make a `NxN` square grid with symbols `X` on `(row,col)` positions where a Queen is placed (`make_grid`)
23
24
24
-
With these utitlities available we can start directly using an exhaustive search among all configurations of placing `N` queens on distinct positions on an `NxN` grid.
25
+
With these utilities available we can start directly using an exhaustive search among all configurations of placing `N` queens on distinct positions on an `NxN` grid.
25
26
26
27
27
28
```javascript
28
-
o=Abacus.Combination(N*N, N, {output:toRowColumn}).filterBy(isValid);
29
+
solutions=Abacus.Combination(N*N, N, {output:row_column}).filterBy(is_valid).get();
However searching among all combinations is inefficient, we can be a little smarter and assume from the beginning that each queen is placed on different row (or column). Then we simply check among all permutations of assigning each queen on a specific (different) column.
52
+
However searching among all combinations as above is inefficient, we can be a little smarter and assume from the beginning that each queen is placed on different row (or column). Then we simply check among all permutations of assigning each queen on a specific (different) column.
4 Queens solutions (reduced exhaustive search): END
63
+
2 Solutions for 4 Queens (reduced exhaustive search):
64
+
O X O O
65
+
O O O X
66
+
X O O O
67
+
O O X O
68
+
---
69
+
O O X O
70
+
X O O O
71
+
O O O X
72
+
O X O O
62
73
```
63
74
75
+
By the way let's **prove** there is **no solution for 3-Queens** using previous method. Piece of cake, simply set `N=3`!
76
+
77
+
```text
78
+
0 Solutions for 3 Queens (reduced exhaustive search):
79
+
```
80
+
81
+
64
82
**Exploiting Symmetries**
65
83
66
-
If we only need to find one solution, then there is an interesting connection between **pan-diagonal latin/magic squares** and **n-queens problems**. Specificaly if we have a pan-diagonal latin or magic square of order `N` then we can have (at least) one solution for the `N` Queens problem simply by placing a queen on each cell which contains only the symbol/number `s` (whatever we choose to be).
84
+
If we only need to find one solution, any solution, then there is an interesting connection between **pan-diagonal latin/magic squares** and **n-queens problems**. Specificaly if we have a pan-diagonal latin or magic square of order `N` then we can have (at least) one solution for the `N` Queens problem simply by placing a queen on each cell which contains the symbol/number `s` (whatever we choose that to be) from the available `N` different symbols/numbers.
67
85
68
-
Since `Abacus` can generate `LatinSquares` and also will try to generate pan-diagonal latin squares if possible (for example for `N=5` it is possible), then we can generate a solution to the 5-Queens problem as follows:
86
+
Since `Abacus` can generate `LatinSquare`s and also generates **pan-diagonal latin squares** by default if possible (for example for `N=5` it is possible), then we can generate a solution to the 5-Queens problem as follows:
69
87
70
88
```javascript
71
-
o=Abacus.LatinSquare.make(N);
72
-
echo(''+N+' Queens solution (pan-diagonal latin square): START');
8 Solutions for 3x3 Magic Squares (exhaustive search):
134
+
2 7 6
135
+
9 5 1
136
+
4 3 8
137
+
---
138
+
2 9 4
139
+
7 5 3
140
+
6 1 8
141
+
---
142
+
4 3 8
143
+
9 5 1
144
+
2 7 6
145
+
---
146
+
4 9 2
147
+
3 5 7
148
+
8 1 6
149
+
---
150
+
6 1 8
151
+
7 5 3
152
+
2 9 4
153
+
---
154
+
6 7 2
155
+
1 5 9
156
+
8 3 4
157
+
---
158
+
8 1 6
159
+
3 5 7
160
+
4 9 2
161
+
---
162
+
8 3 4
163
+
1 5 9
164
+
6 7 2
165
+
```
166
+
167
+
By the way, let's **prove** that there are no `2x2` magic squares (under standard definition). It's super easy and fast.
168
+
Setting `N=2` we get **zero solutions**:
169
+
170
+
```text
171
+
0 Solutions for 2x2 Magic Squares (exhaustive search):
172
+
```
173
+
174
+
**Reduced Search**
175
+
176
+
Exhaustive search is very inefficient as `N` grows (even for small N such as 4 or 5 we get an enormous search space).
177
+
We can try something else. We can generate a magic square (for example using `Abacus.MagicSquare.make(N)`) and try to permute its rows and columns and see if we get different magic squares. This is considerably faster, but might not generate all possible magic squares of order `N`.
0 commit comments