Advertisement
kxcoze

check_finitary_relation(GCC)

Mar 29th, 2020
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include "bits/stdc++.h"    
  2.    
  3. using namespace std;    
  4.  
  5. void printMatrix(vector<auto> v) {
  6.     cout << '\n';
  7.     for (auto i : v) {
  8.         for (auto j : i)
  9.             cout << j << ' ';
  10.         cout << '\n';
  11.     }
  12. }
  13.  
  14. void printAnswer(int ref, int sym, bool tran) {
  15.     cout << "Finitary relation is: ";
  16.     if (ref == 1) cout << "Reflexive, ";
  17.     else if (ref == 0) cout << "AntiReflexive, ";
  18.     else cout << "Not-Reflexive, ";
  19.  
  20.     if (sym == 1) cout << "Symmetric, ";
  21.     else if (sym == 0 && ref == 0) cout << "Asymmetric, ";
  22.     else if (sym == 0) cout << "AntiSymmetric, ";
  23.     else cout << "Not-Symmetric, ";
  24.    
  25.     tran ? cout << "Transitive.\n" : cout << "Not-Transitive.\n";
  26. }
  27.  
  28. vector<vector<bool>> transparency(vector<auto> v) {
  29.     for (int i = 0; i < v.size(); i++) {
  30.         for (int j = i; j < v.size(); j++)
  31.             swap(v[i][j], v[j][i]);
  32.     }
  33.     return v;
  34. }
  35.  
  36. int is_reflexsity(vector<auto>& v) {
  37.     int cnt = 0;
  38.     for (int i = 0; i < v.size(); i++)
  39.         if (v[i][i] == 1) cnt++;
  40.     return cnt == v.size() ? 1 : cnt == 0 ? 0 : -1; // 1 - Рефлексивность, 0 - Антиреф., -1 - Нерефлексивность
  41. }
  42.  
  43. int is_symmetry(vector<auto>& v) {
  44.     vector<vector<bool>> v2 = v;
  45.     v2 = transparency(v2);
  46.     //printMatrix(v2);
  47.     int cnt = 0;
  48.     for (int i = 0; i < v.size(); i++) {
  49.         for (int j = 0; j < v.size(); j++) {
  50.             if (i != j && (v[i][j] * v2[i][j])) cnt++;  
  51.         }
  52.     }
  53.     if (v == v2 && cnt != 0) return 1; // Симметрия
  54.     if (cnt > 0) return -1; // Несимметрия
  55.     return 0; // Антисимметрия
  56. }
  57.  
  58. bool is_transit(vector<auto>& v) {
  59.     bool ans = 1;
  60.     for (int i = 0; i < v.size(); i++) {
  61.         for (int j = 0; j < v.size(); j++) {
  62.             for (int u = 0; u < v.size(); u++) {
  63.                 ans = v[i][j] >= min(v[i][u], v[u][j]);
  64.  
  65.                 if (!ans) return ans; // Нетранзитивно
  66.             }
  67.         }
  68.    
  69.     }
  70.     return ans; // Транзитивно
  71. }
  72.  
  73. int main() {    
  74.     int n, dim, reflex, sym, transit;
  75.     cout << "Input dimensions of set and count of pairs the finitary relation:\n";
  76.     cin >> dim >> n;
  77.     vector <pair<int,int>> vec(n);
  78.     cout << "Input pairs:\n";
  79.     for (int i = 0; i < n; i++)
  80.         cin >> vec[i].first >> vec[i].second;
  81.  
  82.     vector <vector<bool>> matrix(dim, vector<bool>(dim));
  83.     for (int i = 0; i < n; i++) {
  84.         matrix[vec[i].first-1][vec[i].second-1] = 1;
  85.     }
  86.  
  87.     printMatrix(matrix);
  88.     reflex = is_reflexsity(matrix);
  89.     sym = is_symmetry(matrix);
  90.     transit = is_transit(matrix);
  91.    
  92.     printAnswer(reflex, sym, transit);
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement