The implementation of Hamilton's cycle dynamic solution for vertices number less than 20.
More...
#include <cassert>
#include <iostream>
#include <vector>
Go to the source code of this file.
The implementation of Hamilton's cycle dynamic solution for vertices number less than 20.
I use \(2^n\times n\) matrix and for every \([i, j]\) ( \(i < 2^n\) and \(j < n\)) in the matrix I store true if it is possible to get to all vertices on which position in i's binary representation is 1 so as \(j\) would be the last one.
In the the end if any cell in \((2^n - 1)^{\mbox{th}}\) row is true there exists hamiltonian cycle.
- Author
- vakhokoto
-
Krishna Vedala
Definition in file hamiltons_cycle.cpp.
◆ hamilton_cycle()
| bool hamilton_cycle |
( |
const std::vector< std::vector< bool > > & | routes | ) |
|
The function determines if there is a hamilton's cycle in the graph
- Parameters
-
| routes | nxn boolean matrix of \([i, j]\) where \([i, j]\) is true if there is a road from \(i\) to \(j\) |
- Returns
- true if there is Hamiltonian cycle in the graph
-
false if there is no Hamiltonian cycle in the graph
Definition at line 30 of file hamiltons_cycle.cpp.
30 {
31 const size_t n = routes.size();
32
33 const size_t height = 1 << n;
34 std::vector<std::vector<bool>>
dp(
height, std::vector<bool>(n,
false));
35
36
37 for (size_t i = 0; i < n; ++i) {
39 }
40 for (
size_t i = 1; i <
height; i++) {
41 std::vector<size_t> zeros, ones;
42
43 for (size_t pos = 0; pos < n; ++pos) {
44 if ((1 << pos) & i) {
45 ones.push_back(pos);
46 } else {
47 zeros.push_back(pos);
48 }
49 }
50
51 for (auto &o : ones) {
53 continue;
54 }
55
56 for (auto &z : zeros) {
57 if (!routes[o][z]) {
58 continue;
59 }
60 dp[i + (1 << z)][z] =
true;
61 }
62 }
63 }
64
65 bool is_cycle = false;
66 for (size_t i = 0; i < n; i++) {
68 if (is_cycle) {
69
70 break;
71 }
72 }
73 return is_cycle;
74}
◆ main()
Main function
Definition at line 140 of file hamiltons_cycle.cpp.
140 {
144 return 0;
145}
void test2(const std::string &text)
Self test 2 - using 8x8 randomly generated key.
void test1(const std::string &text)
Self test 1 - using 3x3 randomly generated key.
◆ test1()
this test is testing if hamilton_cycle returns true for graph: 1 -> 2 -> 3 -> 4
- Returns
- None
Definition at line 81 of file hamiltons_cycle.cpp.
81 {
82 std::vector<std::vector<bool>> arr{
83 std::vector<bool>({true, true, false, false}),
84 std::vector<bool>({false, true, true, false}),
85 std::vector<bool>({false, false, true, true}),
86 std::vector<bool>({false, false, false, true})};
87
89 std::cout << "Test 1... ";
90 assert(ans);
91 std::cout << "passed\n";
92}
bool hamilton_cycle(const std::vector< std::vector< bool > > &routes)
◆ test2()
this test is testing if hamilton_cycle returns false for
graph:
1 -> 2 -> 3
|
V
4 - Returns
- None
Definition at line 103 of file hamiltons_cycle.cpp.
103 {
104 std::vector<std::vector<bool>> arr{
105 std::vector<bool>({true, true, false, false}),
106 std::vector<bool>({false, true, true, true}),
107 std::vector<bool>({false, false, true, false}),
108 std::vector<bool>({false, false, false, true})};
109
111
112 std::cout << "Test 2... ";
113 assert(!ans);
114 std::cout << "passed\n";
115}
◆ test3()
this test is testing if hamilton_cycle returns true for clique with 4 vertices
- Returns
- None
Definition at line 122 of file hamiltons_cycle.cpp.
122 {
123 std::vector<std::vector<bool>> arr{
124 std::vector<bool>({true, true, true, true}),
125 std::vector<bool>({true, true, true, true}),
126 std::vector<bool>({true, true, true, true}),
127 std::vector<bool>({true, true, true, true})};
128
130
131 std::cout << "Test 3... ";
132 assert(ans);
133 std::cout << "passed\n";
134}