fix: clang-format for hamiltons_cycle. (#1054)

This commit is contained in:
Filip Hlasek 2020-08-27 07:31:34 -07:00 committed by GitHub
parent eee5f9495d
commit ea3071d52a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,49 +28,49 @@
* @return `false` if there is no Hamiltonian cycle in the graph * @return `false` if there is no Hamiltonian cycle in the graph
*/ */
bool hamilton_cycle(const std::vector<std::vector<bool>> &routes) { bool hamilton_cycle(const std::vector<std::vector<bool>> &routes) {
const size_t n = routes.size(); const size_t n = routes.size();
// height of dp array which is 2^n // height of dp array which is 2^n
const size_t height = 1 << n; const size_t height = 1 << n;
std::vector<std::vector<bool>> dp(height, std::vector<bool>(n, false)); std::vector<std::vector<bool>> dp(height, std::vector<bool>(n, false));
// to fill in the [2^i, i] cells with true // to fill in the [2^i, i] cells with true
for (size_t i = 0; i < n; ++i) { for (size_t i = 0; i < n; ++i) {
dp[1 << i][i] = true; dp[1 << i][i] = true;
}
for (size_t i = 1; i < height; i++) {
std::vector<size_t> zeros, ones;
// finding positions with 1s and 0s and separate them
for (size_t pos = 0; pos < n; ++pos) {
if ((1 << pos) & i) {
ones.push_back(pos);
} else {
zeros.push_back(pos);
}
} }
for (size_t i = 1; i < height; i++) {
for (auto &o : ones) { std::vector<size_t> zeros, ones;
if (!dp[i][o]) { // finding positions with 1s and 0s and separate them
continue; for (size_t pos = 0; pos < n; ++pos) {
} if ((1 << pos) & i) {
ones.push_back(pos);
for (auto &z : zeros) { } else {
if (!routes[o][z]) { zeros.push_back(pos);
continue; }
} }
dp[i + (1 << z)][z] = true;
}
}
}
bool is_cycle = false; for (auto &o : ones) {
for (size_t i = 0; i < n; i++) { if (!dp[i][o]) {
is_cycle |= dp[height - 1][i]; continue;
if (is_cycle) { // if true, all subsequent loop will be true. hence }
// break
break; for (auto &z : zeros) {
if (!routes[o][z]) {
continue;
}
dp[i + (1 << z)][z] = true;
}
}
} }
}
return is_cycle; bool is_cycle = false;
for (size_t i = 0; i < n; i++) {
is_cycle |= dp[height - 1][i];
if (is_cycle) { // if true, all subsequent loop will be true. hence
// break
break;
}
}
return is_cycle;
} }
/** /**
@ -79,16 +79,16 @@ bool hamilton_cycle(const std::vector<std::vector<bool>> &routes) {
* @return None * @return None
*/ */
static void test1() { static void test1() {
std::vector<std::vector<bool>> arr{ std::vector<std::vector<bool>> arr{
std::vector<bool>({true, true, false, false}), std::vector<bool>({true, true, false, false}),
std::vector<bool>({false, true, true, false}), std::vector<bool>({false, true, true, false}),
std::vector<bool>({false, false, true, true}), std::vector<bool>({false, false, true, true}),
std::vector<bool>({false, false, false, true})}; std::vector<bool>({false, false, false, true})};
bool ans = hamilton_cycle(arr); bool ans = hamilton_cycle(arr);
std::cout << "Test 1... "; std::cout << "Test 1... ";
assert(ans); assert(ans);
std::cout << "passed\n"; std::cout << "passed\n";
} }
/** /**
@ -101,17 +101,17 @@ static void test1() {
* @return None * @return None
*/ */
static void test2() { static void test2() {
std::vector<std::vector<bool>> arr{ std::vector<std::vector<bool>> arr{
std::vector<bool>({true, true, false, false}), std::vector<bool>({true, true, false, false}),
std::vector<bool>({false, true, true, true}), std::vector<bool>({false, true, true, true}),
std::vector<bool>({false, false, true, false}), std::vector<bool>({false, false, true, false}),
std::vector<bool>({false, false, false, true})}; std::vector<bool>({false, false, false, true})};
bool ans = hamilton_cycle(arr); bool ans = hamilton_cycle(arr);
std::cout << "Test 2... "; std::cout << "Test 2... ";
assert(!ans); // not a cycle assert(!ans); // not a cycle
std::cout << "passed\n"; std::cout << "passed\n";
} }
/** /**
@ -120,17 +120,17 @@ static void test2() {
* @return None * @return None
*/ */
static void test3() { static void test3() {
std::vector<std::vector<bool>> arr{ std::vector<std::vector<bool>> arr{
std::vector<bool>({true, true, true, true}), std::vector<bool>({true, true, true, true}),
std::vector<bool>({true, true, true, true}), std::vector<bool>({true, true, true, true}),
std::vector<bool>({true, true, true, true}), std::vector<bool>({true, true, true, true}),
std::vector<bool>({true, true, true, true})}; std::vector<bool>({true, true, true, true})};
bool ans = hamilton_cycle(arr); bool ans = hamilton_cycle(arr);
std::cout << "Test 3... "; std::cout << "Test 3... ";
assert(ans); assert(ans);
std::cout << "passed\n"; std::cout << "passed\n";
} }
/** /**
@ -140,8 +140,8 @@ static void test3() {
* @param argv commandline array of arguments (ignored) * @param argv commandline array of arguments (ignored)
*/ */
int main(int argc, char **argv) { int main(int argc, char **argv) {
test1(); test1();
test2(); test2();
test3(); test3();
return 0; return 0;
} }