Advertisement
Guest User

Untitled

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