Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <vector>
  2. using namespace std;
  3.  
  4. template <typename A>
  5. class Relation
  6. {
  7.     vector<pair<A, A>> relations;
  8. public:
  9.     Relation() {}
  10.    
  11.     bool addPair(const A& a1, const A& a2) {
  12.         auto p = make_pair<A, A>(a1, a2);
  13.         if(count(relations.begin(), relations.end(), p) == 0)
  14.             relations.push_back(p);
  15.         return true;
  16.     }
  17.    
  18.     bool hasPair(const A& a1, const A& a2) {
  19.         auto p = make_pair<A, A>(a1, a2);
  20.         return count(relations.begin(), relations.end(), p) != 0;
  21.     }
  22.    
  23.     bool isReflexive() {
  24.         for(auto i = 0; i < relations.size(); ++i) {
  25.             if(!hasPair(relations[i].first, relations[i].first) ||
  26.             !hasPair(relations[i].second, relations[i].second))
  27.                 return false;
  28.         }
  29.         return true;
  30.     }
  31.    
  32.     bool isSymmetric() {
  33.         for(auto i = 0; i < relations.size(); ++i) {
  34.             if(!hasPair(relations[i].second, relations[i].first))
  35.                 return false;
  36.         }
  37.         return true;
  38.     }
  39.    
  40.     bool isTransitive() {
  41.         for(auto i = 0; i < relations.size() - 1; ++i) {
  42.             for(auto j = i + 1; j < relations.size(); ++j) {
  43.                 if(!hasPair(relations[i].first, relations[j].second))
  44.                     return false;
  45.             }
  46.         }
  47.         return true;
  48.     }
  49. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement