Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 768770d

Browse files
committed
Update README.md's (mostly from trekhleb/javascript-algorithms)
1 parent be2f769 commit 768770d

File tree

48 files changed

+1684
-236
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1684
-236
lines changed
Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
1-
# Knight’s tour problem
2-
A knight's tour is a sequence of moves of a knight on a chessboard such that the knight visits every square only once. If the knight ends on a square that is one knight's move from the beginning square (so that it could tour the board again immediately, following the same path), the tour is closed, otherwise it is open.
1+
# Knight's Tour Problem
32

4-
## Complexity
5-
* **Time**: Worst ![](https://latex.codecogs.com/svg.latex?O(8^{N^{2}}))
6-
* **Space**: Worst ![](https://latex.codecogs.com/svg.latex?O(N^2))
3+
A **knight's tour** is a sequence of moves of a knight on a chessboard
4+
such that the knight visits every square only once. If the knight
5+
ends on a square that is one knight's move from the beginning
6+
square (so that it could tour the board again immediately,
7+
following the same path), the tour is **closed**, otherwise it
8+
is **open**.
9+
10+
The **knight's tour problem** is the mathematical problem of
11+
finding a knight's tour. Creating a program to find a knight's
12+
tour is a common problem given to computer science students.
13+
Variations of the knight's tour problem involve chessboards of
14+
different sizes than the usual `8×8`, as well as irregular
15+
(non-rectangular) boards.
16+
17+
The knight's tour problem is an instance of the more
18+
general **Hamiltonian path problem** in graph theory. The problem of finding
19+
a closed knight's tour is similarly an instance of the Hamiltonian
20+
cycle problem.
21+
22+
![Knight's Tour](https://upload.wikimedia.org/wikipedia/commons/d/da/Knight%27s_tour_anim_2.gif)
23+
24+
An open knight's tour of a chessboard.
25+
26+
![Knight's Tour](https://upload.wikimedia.org/wikipedia/commons/c/ca/Knights-Tour-Animation.gif)
27+
28+
An animation of an open knight's tour on a 5 by 5 board.
729

830
## References
9-
* [Wikipedia](https://en.wikipedia.org/wiki/Knight%27s_tour)
31+
32+
- [trekhleb/javascript-algorithms](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/uncategorized/knight-tour)
33+
- [Wikipedia](https://en.wikipedia.org/wiki/Knight%27s_tour)
34+
- [GeeksForGeeks](https://www.geeksforgeeks.org/backtracking-set-1-the-knights-tour-problem/)
Lines changed: 113 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,117 @@
1-
# N Queens Problem
2-
The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other.
1+
# N-Queens Problem
32

4-
## Applications
5-
* Puzzle Solving
6-
* Searching
3+
The **eight queens puzzle** is the problem of placing eight chess queens
4+
on an `8×8` chessboard so that no two queens threaten each other.
5+
Thus, a solution requires that no two queens share the same row,
6+
column, or diagonal. The eight queens puzzle is an example of the
7+
more general *n queens problem* of placing n non-attacking queens
8+
on an `n×n` chessboard, for which solutions exist for all natural
9+
numbers `n` with the exception of `n=2` and `n=3`.
710

8-
## Complexity
9-
* **Time**: Worst ![](https://latex.codecogs.com/svg.latex?O(N!))
10-
* **Space**: Worst ![](https://latex.codecogs.com/svg.latex?O(N))
11+
For example, following is a solution for 4 Queen problem.
12+
13+
![N Queens](https://cdncontribute.geeksforgeeks.org/wp-content/uploads/N_Queen_Problem.jpg)
14+
15+
The expected output is a binary matrix which has 1s for the blocks
16+
where queens are placed. For example following is the output matrix
17+
for above 4 queen solution.
18+
19+
```
20+
{ 0, 1, 0, 0}
21+
{ 0, 0, 0, 1}
22+
{ 1, 0, 0, 0}
23+
{ 0, 0, 1, 0}
24+
```
25+
26+
## Naive Algorithm
27+
28+
Generate all possible configurations of queens on board and print a
29+
configuration that satisfies the given constraints.
30+
31+
```
32+
while there are untried configurations
33+
{
34+
generate the next configuration
35+
if queens don't attack in this configuration then
36+
{
37+
print this configuration;
38+
}
39+
}
40+
```
41+
42+
## Backtracking Algorithm
43+
44+
The idea is to place queens one by one in different columns,
45+
starting from the leftmost column. When we place a queen in a
46+
column, we check for clashes with already placed queens. In
47+
the current column, if we find a row for which there is no
48+
clash, we mark this row and column as part of the solution.
49+
If we do not find such a row due to clashes then we backtrack
50+
and return false.
51+
52+
```
53+
1) Start in the leftmost column
54+
2) If all queens are placed
55+
return true
56+
3) Try all rows in the current column. Do following for every tried row.
57+
a) If the queen can be placed safely in this row then mark this [row,
58+
column] as part of the solution and recursively check if placing
59+
queen here leads to a solution.
60+
b) If placing queen in [row, column] leads to a solution then return
61+
true.
62+
c) If placing queen doesn't lead to a solution then umark this [row,
63+
column] (Backtrack) and go to step (a) to try other rows.
64+
3) If all rows have been tried and nothing worked, return false to trigger
65+
backtracking.
66+
```
67+
68+
## Bitwise Solution
69+
70+
Bitwise algorithm basically approaches the problem like this:
71+
72+
- Queens can attack diagonally, vertically, or horizontally. As a result, there
73+
can only be one queen in each row, one in each column, and at most one on each
74+
diagonal.
75+
- Since we know there can only one queen per row, we will start at the first row,
76+
place a queen, then move to the second row, place a second queen, and so on until
77+
either a) we reach a valid solution or b) we reach a dead end (ie. we can't place
78+
a queen such that it is "safe" from the other queens).
79+
- Since we are only placing one queen per row, we don't need to worry about
80+
horizontal attacks, since no queen will ever be on the same row as another queen.
81+
- That means we only need to check three things before placing a queen on a
82+
certain square: 1) The square's column doesn't have any other queens on it, 2)
83+
the square's left diagonal doesn't have any other queens on it, and 3) the
84+
square's right diagonal doesn't have any other queens on it.
85+
- If we ever reach a point where there is nowhere safe to place a queen, we can
86+
give up on our current attempt and immediately test out the next possibility.
87+
88+
First let's talk about the recursive function. You'll notice that it accepts
89+
3 parameters: `leftDiagonal`, `column`, and `rightDiagonal`. Each of these is
90+
technically an integer, but the algorithm takes advantage of the fact that an
91+
integer is represented by a sequence of bits. So, think of each of these
92+
parameters as a sequence of `N` bits.
93+
94+
Each bit in each of the parameters represents whether the corresponding location
95+
on the current row is "available".
96+
97+
For example:
98+
- For `N=4`, column having a value of `0010` would mean that the 3rd column is
99+
already occupied by a queen.
100+
- For `N=8`, ld having a value of `00011000` at row 5 would mean that the
101+
top-left-to-bottom-right diagonals that pass through columns 4 and 5 of that
102+
row are already occupied by queens.
103+
104+
Below is a visual aid for `leftDiagonal`, `column`, and `rightDiagonal`.
105+
106+
![](http://gregtrowbridge.com/content/images/2014/Jul/Screenshot-from-2014-06-17-19-46-20.png)
11107

12108
## References
13-
* [geeksforgeeks](http://www.geeksforgeeks.org/backtracking-set-3-n-queen-problem/)
109+
110+
- [trekhleb/javascript-algorithms](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/uncategorized/n-queens)
111+
- [Wikipedia](https://en.wikipedia.org/wiki/Eight_queens_puzzle)
112+
- [GeeksForGeeks](https://www.geeksforgeeks.org/backtracking-set-3-n-queen-problem/)
113+
- [On YouTube by Abdul Bari](https://www.youtube.com/watch?v=xFv_Hl4B83A&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
114+
- [On YouTube by Tushar Roy](https://www.youtube.com/watch?v=xouin83ebxE&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
115+
- Bitwise Solution
116+
- [Wikipedia](https://en.wikipedia.org/wiki/Eight_queens_puzzle)
117+
- [Solution by Greg Trowbridge](http://gregtrowbridge.com/a-bitwise-solution-to-the-n-queens-problem-in-javascript/)

0 commit comments

Comments
 (0)