1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/*******************************************************************\
Module: Unit test for graph.h
Author: Diffblue Ltd
\*******************************************************************/
#include <testing-utils/use_catch.h>
#include <util/graph.h>
template<class E>
static inline bool operator==(
const graph_nodet<E> &gn1, const graph_nodet<E> &gn2)
{
return gn1.in == gn2.in && gn1.out == gn2.out;
}
template<class N>
static inline bool operator==(const grapht<N> &g1, const grapht<N> &g2)
{
if(g1.size() != g2.size())
return false;
for(typename grapht<N>::node_indext i = 0; i < g1.size(); ++i)
{
if(!(g1[i] == g2[i]))
return false;
}
return true;
}
/// To verify make_chordal is working as intended: naively search for a
/// hole (a chordless 4+-cycle)
template<class N>
static bool contains_hole(
const grapht<N> &g,
const std::vector<typename grapht<N>::node_indext> &cycle_so_far)
{
const auto &successors_map = g[cycle_so_far.back()].out;
// If this node has a triangular edge (one leading to cycle_so_far[-3]) then
// this isn't a hole:
if(cycle_so_far.size() >= 3 &&
successors_map.count(cycle_so_far[cycle_so_far.size() - 3]) != 0)
{
return false;
}
// If this node has an edge leading to any other cycle member (except our
// immediate predecessor) then we've found a hole:
if(cycle_so_far.size() >= 4)
{
for(std::size_t i = 0; i <= cycle_so_far.size() - 4; ++i)
{
if(successors_map.count(cycle_so_far[i]) != 0)
return true;
}
}
// Otherwise try to extend the cycle:
for(const auto &successor : successors_map)
{
// The input is undirected, so a 2-cycle is always present:
if(cycle_so_far.size() >= 2 &&
successor.first == cycle_so_far[cycle_so_far.size() - 2])
{
continue;
}
// Work around spurious GCC 14 warning about __builtin_memmove accessing
// elements out-of-bounds
#pragma GCC diagnostic push
#ifndef __clang__
# pragma GCC diagnostic ignored "-Warray-bounds"
# pragma GCC diagnostic ignored "-Wstringop-overflow"
#endif
std::vector<typename grapht<N>::node_indext> extended_cycle = cycle_so_far;
#pragma GCC diagnostic pop
extended_cycle.push_back(successor.first);
if(contains_hole(g, extended_cycle))
return true;
}
return false;
}
template<class N>
static bool contains_hole(const grapht<N> &g)
{
// For each node in the graph, check for cycles starting at that node.
// This is pretty dumb, but I figure this formulation is simple enough to
// check by hand and the complexity isn't too bad for small examples.
for(typename grapht<N>::node_indext i = 0; i < g.size(); ++i)
{
std::vector<typename grapht<N>::node_indext> start_node(1, i);
if(contains_hole(g, start_node))
return true;
}
return false;
}
typedef grapht<graph_nodet<empty_edget>> simple_grapht;
SCENARIO("graph-make-chordal",
"[core][util][graph]")
{
GIVEN("An acyclic graph")
{
simple_grapht graph;
simple_grapht::node_indext indices[5];
for(int i = 0; i < 5; ++i)
indices[i] = graph.add_node();
// Make a graph: 0 <-> 1 <-> 4
// \-> 2 <-/
// \-> 3
graph.add_undirected_edge(indices[0], indices[1]);
graph.add_undirected_edge(indices[0], indices[2]);
graph.add_undirected_edge(indices[0], indices[3]);
graph.add_undirected_edge(indices[1], indices[4]);
graph.add_undirected_edge(indices[2], indices[4]);
WHEN("The graph is made chordal")
{
simple_grapht chordal_graph = graph;
chordal_graph.make_chordal();
THEN("The graph should be unchanged")
{
// This doesn't pass, as make_chordal actually adds triangular edges to
// *all* common neighbours, even nodes that aren't part of a cycle.
// REQUIRE(graph == chordal_graph);
// At least it shouldn't be chordal!
REQUIRE(!contains_hole(chordal_graph));
}
}
}
GIVEN("A cyclic graph that is already chordal")
{
simple_grapht graph;
simple_grapht::node_indext indices[5];
for(int i = 0; i < 5; ++i)
indices[i] = graph.add_node();
// Make a graph: 0 <-> 1 <-> 2 <-> 0
// 3 <-> 1 <-> 2 <-> 3
// 4 <-> 1 <-> 2 <-> 4
graph.add_undirected_edge(indices[0], indices[1]);
graph.add_undirected_edge(indices[1], indices[2]);
graph.add_undirected_edge(indices[2], indices[0]);
graph.add_undirected_edge(indices[3], indices[1]);
graph.add_undirected_edge(indices[2], indices[3]);
graph.add_undirected_edge(indices[4], indices[1]);
graph.add_undirected_edge(indices[2], indices[4]);
WHEN("The graph is made chordal")
{
simple_grapht chordal_graph = graph;
chordal_graph.make_chordal();
THEN("The graph should be unchanged")
{
// This doesn't pass, as make_chordal actually adds triangular edges to
// *all* common neighbours, even cycles that are already chordal.
// REQUIRE(graph == chordal_graph);
// At least it shouldn't be chordal!
REQUIRE(!contains_hole(chordal_graph));
}
}
}
GIVEN("A simple 4-cycle")
{
simple_grapht graph;
simple_grapht::node_indext indices[4];
for(int i = 0; i < 4; ++i)
indices[i] = graph.add_node();
graph.add_undirected_edge(indices[0], indices[1]);
graph.add_undirected_edge(indices[1], indices[2]);
graph.add_undirected_edge(indices[2], indices[3]);
graph.add_undirected_edge(indices[3], indices[0]);
// Check the contains_hole predicate is working as intended:
REQUIRE(contains_hole(graph));
WHEN("The graph is made chordal")
{
simple_grapht chordal_graph = graph;
chordal_graph.make_chordal();
THEN("The graph should gain a chord")
{
REQUIRE(!contains_hole(chordal_graph));
}
}
}
GIVEN("A more complicated graph with a hole")
{
simple_grapht graph;
simple_grapht::node_indext indices[8];
for(int i = 0; i < 8; ++i)
indices[i] = graph.add_node();
// A 5-cycle:
graph.add_undirected_edge(indices[0], indices[1]);
graph.add_undirected_edge(indices[1], indices[2]);
graph.add_undirected_edge(indices[2], indices[3]);
graph.add_undirected_edge(indices[3], indices[4]);
graph.add_undirected_edge(indices[4], indices[0]);
// A 3-cycle connected to the 5:
graph.add_undirected_edge(indices[4], indices[5]);
graph.add_undirected_edge(indices[5], indices[6]);
graph.add_undirected_edge(indices[6], indices[4]);
// Another 3-cycle joined onto the 5:
graph.add_undirected_edge(indices[1], indices[7]);
graph.add_undirected_edge(indices[3], indices[7]);
// Check we've made the input correctly:
REQUIRE(contains_hole(graph));
WHEN("The graph is made chordal")
{
simple_grapht chordal_graph = graph;
chordal_graph.make_chordal();
THEN("The graph's 5-cycle should be completed with chords")
{
REQUIRE(!contains_hole(chordal_graph));
}
}
}
}
SCENARIO("graph-connected-subgraphs",
"[core][util][graph]")
{
GIVEN("A connected graph")
{
simple_grapht graph;
simple_grapht::node_indext indices[5];
for(int i = 0; i < 5; ++i)
indices[i] = graph.add_node();
// Make a graph: 0 <-> 1 <-> 4
// \-> 2 <-/
// \-> 3
graph.add_undirected_edge(indices[0], indices[1]);
graph.add_undirected_edge(indices[0], indices[2]);
graph.add_undirected_edge(indices[0], indices[3]);
graph.add_undirected_edge(indices[1], indices[4]);
graph.add_undirected_edge(indices[2], indices[4]);
WHEN("We take its connected subgraphs")
{
std::vector<simple_grapht::node_indext> subgraphs;
graph.connected_subgraphs(subgraphs);
REQUIRE(subgraphs.size() == graph.size());
simple_grapht::node_indext only_subgraph = subgraphs.at(0);
// Check everything is in one subgraph:
REQUIRE(
subgraphs ==
std::vector<simple_grapht::node_indext>(graph.size(), only_subgraph));
}
}
GIVEN("A graph with three unconnected subgraphs")
{
simple_grapht graph;
simple_grapht::node_indext indices[6];
for(int i = 0; i < 6; ++i)
indices[i] = graph.add_node();
graph.add_undirected_edge(indices[0], indices[1]);
graph.add_undirected_edge(indices[2], indices[3]);
graph.add_undirected_edge(indices[4], indices[5]);
WHEN("We take its connected subgraphs")
{
std::vector<simple_grapht::node_indext> subgraphs;
graph.connected_subgraphs(subgraphs);
REQUIRE(subgraphs.size() == graph.size());
simple_grapht::node_indext first_subgraph = subgraphs.at(0);
simple_grapht::node_indext second_subgraph = subgraphs.at(2);
simple_grapht::node_indext third_subgraph = subgraphs.at(4);
std::vector<simple_grapht::node_indext> expected_subgraphs
{
first_subgraph,
first_subgraph,
second_subgraph,
second_subgraph,
third_subgraph,
third_subgraph
};
REQUIRE(subgraphs == expected_subgraphs);
}
}
}
SCENARIO("predecessors-successors-graph", "[core][util][graph]")
{
GIVEN("A graph")
{
simple_grapht graph;
simple_grapht::node_indext indices[2];
for(int i = 0; i < 2; ++i)
indices[i] = graph.add_node();
graph.add_edge(indices[0], indices[1]);
THEN("Nodes should have correct successors and predecessors")
{
REQUIRE(graph.get_predecessors(indices[0]).size() == 0);
REQUIRE(graph.get_successors(indices[0]).size() == 1);
REQUIRE(graph.get_predecessors(indices[1]).size() == 1);
REQUIRE(graph.get_successors(indices[1]).size() == 0);
int count = 0;
graph.for_each_predecessor(
indices[1], [&](const simple_grapht::node_indext &n) { count++; });
REQUIRE(count == 1);
// Refresh counter.
count = 0;
graph.for_each_successor(
indices[1], [&](const simple_grapht::node_indext &n) { count++; });
REQUIRE(count == 0);
}
}
}
|