Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <algorithm>
- using namespace std;
- template <typename A>
- class Relation
- {
- vector<pair<A, A>> relations;
- public:
- Relation() {}
- bool addPair(const A& a1, const A& a2) {
- auto p = make_pair(a1, a2);
- if(count(relations.begin(), relations.end(), p) == 0)
- relations.push_back(p);
- return true;
- }
- bool hasPair(const A& a1, const A& a2) {
- auto p = make_pair(a1, a2);
- return count(relations.begin(), relations.end(), p) != 0;
- }
- bool isReflexive() {
- for(auto i = 0; i < relations.size(); ++i) {
- if(!hasPair(relations[i].first, relations[i].first) ||
- !hasPair(relations[i].second, relations[i].second))
- return false;
- }
- return true;
- }
- bool isSymmetric() {
- for(auto i = 0; i < relations.size(); ++i) {
- if(!hasPair(relations[i].second, relations[i].first))
- return false;
- }
- return true;
- }
- bool isTransitive() {
- for(auto i = 0; i < relations.size() - 1; ++i) {
- for(auto j = i + 1; j < relations.size(); ++j) {
- if(!hasPair(relations[i].first, relations[j].second))
- return false;
- }
- }
- return true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement