Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
MatchingTest.cpp
Go to the documentation of this file.
1#include "storm-config.h"
2#include "test/storm_gtest.h"
3
4#include <algorithm>
5#include <cstdint>
6#include <set>
7#include <vector>
8
10
11namespace {
12
16auto edges(std::vector<std::vector<uint64_t>> const& adjacency) {
17 return [&adjacency](uint64_t const v, uint64_t const u) { return std::find(adjacency[v].begin(), adjacency[v].end(), u) != adjacency[v].end(); };
18}
19
20} // namespace
21
22TEST(MatchingTest, PerfectMatchingIsFoundIfOneExists) {
23 // Every vertex has two options here, so the (unique) bijection cannot be found by matching every domain vertex with its first option.
24 std::vector<std::vector<uint64_t>> const adjacency{{0, 1}, {1, 2}, {2, 0}};
25 auto const matching = storm::utility::findPerfectMatching(3, edges(adjacency));
26 ASSERT_TRUE(matching.has_value());
27 ASSERT_EQ(3ull, matching->size());
28 EXPECT_EQ(std::set<uint64_t>({0, 1, 2}), std::set<uint64_t>(matching->begin(), matching->end())) << "not a bijection";
29 for (uint64_t v = 0; v < 3; ++v) {
30 EXPECT_TRUE(edges(adjacency)(v, (*matching)[v])) << "domain vertex " << v << " is not adjacent to the image vertex " << (*matching)[v];
31 }
32}
33
34TEST(MatchingTest, NoPerfectMatchingIfTwoVerticesShareTheirOnlyOption) {
35 // The domain vertices 0 and 1 both only fit the image vertex 0, so there is no bijection, although every vertex has an option.
36 std::vector<std::vector<uint64_t>> const adjacency{{0}, {0}, {1, 2}};
37 EXPECT_FALSE(storm::utility::findPerfectMatching(3, edges(adjacency)).has_value());
38}
39
40TEST(MatchingTest, EmptyGraphHasPerfectMatching) {
41 auto const matching = storm::utility::findPerfectMatching(0, [](uint64_t, uint64_t) { return false; });
42 ASSERT_TRUE(matching.has_value());
43 EXPECT_TRUE(matching->empty());
44}
TEST(MatchingTest, PerfectMatchingIsFoundIfOneExists)
std::optional< std::vector< uint64_t > > findPerfectMatching(uint64_t const n, EdgePredicate &&hasEdge)
Definition matching.h:18