15# include <gtest/gtest.h>
40 for (
size_t i = 0; i < n; ++i)
45 for (
auto it_i =
nodes.get_it(); it_i.has_curr(); it_i.next_ne(), ++i)
48 for (
auto it_j =
nodes.get_it(); it_j.has_curr(); it_j.next_ne(), ++j)
50 g.
insert_arc(it_i.get_curr(), it_j.get_curr());
63 for (
size_t i = 0; i < n; ++i)
68 for (
auto it =
nodes.get_it(); it.has_curr(); it.next_ne())
90 for (
size_t i = 0; i < n; ++i)
92 auto *curr = g.
insert_node(
"v" + std::to_string(i));
106 if (n == 0)
return g;
108 for (
size_t i = 1; i < n; ++i)
110 auto *leaf = g.
insert_node(
"leaf" + std::to_string(i));
124 for (
int i = 0; i < 10; ++i)
128 for (
int i = 0; i < 5; ++i)
139 for (
int i = 0; i < 5; ++i)
155 for (
size_t i = 1; i < n; ++i)
164 for (
auto it = ring.
get_it(); it.has_curr(); it.next_ne())
166 auto *curr = it.get_curr();
185 for (
size_t i = 0; i <
m; ++i)
187 for (
size_t i = 0; i < n; ++i)
190 for (
auto li = left.
get_it(); li.has_curr(); li.next_ne())
191 for (
auto ri = right.
get_it(); ri.has_curr(); ri.next_ne())
243TEST(GraphColoring, IsolatedNodes)
246 for (
int i = 0; i < 10; ++i)
314TEST(GraphColoring, CompleteBipartiteK33)
392TEST(GraphColoring, PetersenGraph)
406TEST(GraphColoring, DisconnectedComponents)
431TEST(GraphColoring, DisconnectedMixed)
454TEST(GraphColoring, WheelW5EvenRing)
465TEST(GraphColoring, WheelW6OddRing)
480TEST(GraphColoring, AllAlgorithmsProduceValidColorings)
500TEST(GraphColoring, GreedyBoundDeltaPlusOne)
516TEST(GraphColoring, ValidationRejectsInvalidColoring)
522 for (
auto it = g.get_node_it(); it.has_curr(); it.next_ne())
523 colors.
insert(it.get_curr(), 0);
528TEST(GraphColoring, ValidationRejectsMissingNode)
545TEST(GraphColoring, ValidationRejectsForeignKeyedMap)
555 colors.
insert(foreign, 0);
568TEST(GraphColoring, ValidationRejectsIsolatedNodeMissingFromColors)
588TEST(GraphColoring, ValidationRejectsSelfLoop)
604TEST(GraphColoring, ValidationRejectsParallelEdge)
639TEST(GraphColoring, ChromaticNumberEmptyGraph)
647TEST(GraphColoring, ChromaticNumberIsolatedNodes)
650 for (
int i = 0; i < 5; ++i)
658TEST(GraphColoring, ChromaticNumberK4)
668TEST(GraphColoring, ChromaticNumberPetersen)
677TEST(GraphColoring, ChromaticNumberOddCycle)
686TEST(GraphColoring, ChromaticNumberEvenCycle)
695TEST(GraphColoring, ChromaticNumberBipartite)
704TEST(GraphColoring, ChromaticNumberTooManyNodes)
707 for (
int i = 0; i < 65; ++i)
714TEST(GraphColoring, ChromaticNumber64Nodes)
717 std::vector<Graph::Node *> ns;
718 for (
int i = 0; i < 64; ++i)
719 ns.push_back(g.
insert_node(
"v" + std::to_string(i)));
729 EXPECT_EQ(colors.
size(), 64u);
737TEST(GraphColoring, SGraphBasicColoring)
754TEST(GraphColoring, SGraphGreedy)
773TEST(GraphColoring, DigraphSingleArcUsesTwoColors)
776 auto *a = g.insert_node(
"a");
777 auto *b = g.insert_node(
"b");
795TEST(GraphColoring, DigraphReverseOrderStillUsesTwoColors)
798 auto *a = g.insert_node(
"a");
799 auto *b = g.insert_node(
"b");
820TEST(GraphColoring, AGraphTriangle)
842TEST(GraphColoring, FunctorWrappers)
848 size_t k1 = greedy(g, colors);
853 size_t k2 = wp(g, colors);
858 size_t k3 = dsat(g, colors);
867TEST(GraphColoring, CookiesPreservedAfterColoring)
892TEST(GraphColoring, SouthAmericaMapColoring)
948TEST(GraphColoring, ExamScheduling)
974 EXPECT_LE(num_slots, 3u);
981TEST(GraphColoring, ChromaticNumberWheelEvenRing)
991TEST(GraphColoring, ChromaticNumberWheelOddRing)
1005TEST(GraphColoring, RandomGraphsPropertyBased)
1008 std::mt19937
rng(42);
1012 std::vector<Graph::Node *>
nodes;
1017 std::uniform_real_distribution<> dist(0.0, 1.0);
1019 for (
size_t j = i + 1; j <
num_nodes; ++j)
1020 if (dist(
rng) < edge_prob)
1026 auto max_degree = [](
const Graph &g) ->
size_t {
1028 for (
auto it = g.get_node_it(); it.has_curr(); it.next_ne())
1029 delta = std::max(delta, g.get_num_arcs(it.get_curr()));
1037 std::string description;
1040 std::vector<TestCase> test_cases = {
1041 {10, 0.1,
"sparse"},
1042 {15, 0.3,
"medium"},
1044 {20, 0.05,
"disconnected components"},
1045 {8, 0.8,
"very dense"}
1048 for (
const auto &tc : test_cases)
1050 for (
int trial = 0; trial < 20; ++trial)
1057 size_t delta = max_degree(g);
1063 <<
"Greedy produced invalid coloring for " << tc.description
1064 <<
" graph (trial " << trial <<
")";
1065 EXPECT_LE(k_greedy, delta + 1)
1066 <<
"Greedy used " << k_greedy <<
" colors but max degree is " << delta
1067 <<
" for " << tc.description <<
" graph (trial " << trial <<
")";
1072 <<
"Welsh-Powell produced invalid coloring for " << tc.description
1073 <<
" graph (trial " << trial <<
")";
1074 EXPECT_LE(k_wp, delta + 1)
1075 <<
"Welsh-Powell used " << k_wp <<
" colors but max degree is " << delta
1076 <<
" for " << tc.description <<
" graph (trial " << trial <<
")";
1083 <<
"DSatur produced invalid coloring for " << tc.description
1084 <<
" graph (trial " << trial <<
")";
1087 EXPECT_LE(
k, delta + 1)
1088 <<
"DSatur used " <<
k <<
" colors but max degree is " << delta
1089 <<
" for " << tc.description <<
" graph (trial " << trial <<
")";
1098 <<
"chromatic_number produced invalid coloring for " << tc.description
1099 <<
" graph (trial " << trial <<
")";
1101 EXPECT_LE(chi, k_greedy)
1102 <<
"Exact chromatic number " << chi <<
" exceeds Greedy result " << k_greedy
1103 <<
" for " << tc.description <<
" graph (trial " << trial <<
")";
1104 EXPECT_LE(chi, k_wp)
1105 <<
"Exact chromatic number " << chi <<
" exceeds Welsh-Powell result " << k_wp
1106 <<
" for " << tc.description <<
" graph (trial " << trial <<
")";
1108 <<
"Exact chromatic number " << chi <<
" exceeds DSatur result " <<
k
1109 <<
" for " << tc.description <<
" graph (trial " << trial <<
")";
Graph coloring algorithms and heuristics.
virtual Node * insert_node(Node *p)
Inserts the node p, made with new, which the graph takes.
Arc * insert_arc(Node *src, Node *tgt, void *a)
Functor wrapper for dsatur_coloring.
Generic directed graph (digraph) wrapper template.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Generic key-value map implemented on top of a binary search tree.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Dynamic set backed by balanced binary search trees with automatic memory management.
const size_t & size() const
Returns the cardinality of the set.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Arc for graphs implemented with simple adjacency lists.
Functor wrapper for greedy_coloring.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Graph class implemented with singly-linked adjacency lists.
Arc * insert_arc(Node *src, Node *tgt, void *a)
virtual Node * insert_node(Node *p)
Insertion of a node whose memory has already been allocated.
A non-degenerate triangle defined by three points.
Functor wrapper for welsh_powell_coloring.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
CityGraph build_random_graph(int num_nodes, double edge_probability, double max_weight, unsigned seed=42)
Build a random graph for performance testing.
static Graph build_complete_bipartite(size_t m, size_t n)
static Graph build_star(size_t n)
static Graph build_petersen()
static Graph build_path(size_t n)
static Graph build_complete_graph(size_t n)
static Graph build_cycle(size_t n)
static Graph build_wheel(size_t n)
static size_t count_distinct_colors(const DynMapTree< Graph::Node *, size_t > &colors)
DynArray< Graph::Node * > nodes
size_t chromatic_number(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
Computes the exact chromatic number of a small graph.
size_t greedy_coloring(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
Greedy graph coloring in iteration order.
bool is_valid_coloring(const GT &g, const DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
Validates a graph coloring.
#define NODE_COOKIE(p)
Return the node cookie
size_t dsatur_coloring(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
DSatur graph coloring (saturation-degree heuristic).
size_t welsh_powell_coloring(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
Welsh-Powell graph coloring.
Main namespace for Aleph-w library functions.
Arc of graph implemented with double-linked adjacency lists.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Array-based graph implementation.
Simple graph implementation with adjacency lists.