Add Hamiltonian cycle.

This commit is contained in:
Oleksii Trekhleb 2018-05-17 08:08:29 +03:00
parent 569c6ae452
commit d2c6d14acd
2 changed files with 2 additions and 0 deletions

View File

@ -91,6 +91,7 @@ function hamiltonianCycleRecursive({
cycle: currentCycle, cycle: currentCycle,
}); });
// BACKTRACKING.
// Remove candidate vertex from cycle path in order to try another one. // Remove candidate vertex from cycle path in order to try another one.
currentCycle.pop(); currentCycle.pop();
} }

View File

@ -72,6 +72,7 @@ function nQueensRecursive(solutions, previousQueensPositions, queensCount, rowIn
// Try to place all other queens as well. // Try to place all other queens as well.
nQueensRecursive(solutions, queensPositions, queensCount, rowIndex + 1); nQueensRecursive(solutions, queensPositions, queensCount, rowIndex + 1);
// BACKTRACKING.
// Remove the queen from the row to avoid isSafe() returning false. // Remove the queen from the row to avoid isSafe() returning false.
queensPositions[rowIndex] = null; queensPositions[rowIndex] = null;
} }