Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- void printMatrix(vector<auto> v) {
- cout << '\n';
- for (auto i : v) {
- for (auto j : i)
- cout << j << ' ';
- cout << '\n';
- }
- }
- void printAnswer(int ref, int sym, bool tran) {
- cout << "Finitary relation is: ";
- if (ref == 1) cout << "Reflexive, ";
- else if (ref == 0) cout << "AntiReflexive, ";
- else cout << "Not-Reflexive, ";
- if (sym == 1) cout << "Symmetric, ";
- else if (sym == 0 && ref == 0) cout << "Asymmetric, ";
- else if (sym == 0) cout << "AntiSymmetric, ";
- else cout << "Not-Symmetric, ";
- tran ? cout << "Transitive.\n" : cout << "Not-Transitive.\n";
- }
- vector<vector<bool>> transparency(vector<auto> v) {
- for (int i = 0; i < v.size(); i++) {
- for (int j = i; j < v.size(); j++)
- swap(v[i][j], v[j][i]);
- }
- return v;
- }
- int is_reflexsity(vector<auto>& v) {
- int cnt = 0;
- for (int i = 0; i < v.size(); i++)
- if (v[i][i] == 1) cnt++;
- return cnt == v.size() ? 1 : cnt == 0 ? 0 : -1; // 1 - Рефлексивность, 0 - Антиреф., -1 - Нерефлексивность
- }
- int is_symmetry(vector<auto>& v) {
- vector<vector<bool>> v2 = v;
- v2 = transparency(v2);
- //printMatrix(v2);
- int cnt = 0;
- for (int i = 0; i < v.size(); i++) {
- for (int j = 0; j < v.size(); j++) {
- if (i != j && (v[i][j] * v2[i][j])) cnt++;
- }
- }
- if (v == v2 && cnt != 0) return 1; // Симметрия
- if (cnt > 0) return -1; // Несимметрия
- return 0; // Антисимметрия
- }
- bool is_transit(vector<auto>& v) {
- bool ans = 1;
- for (int i = 0; i < v.size(); i++) {
- for (int j = 0; j < v.size(); j++) {
- for (int u = 0; u < v.size(); u++) {
- ans = v[i][j] >= min(v[i][u], v[u][j]);
- if (!ans) return ans; // Нетранзитивно
- }
- }
- }
- return ans; // Транзитивно
- }
- int main() {
- int n, dim, reflex, sym, transit;
- cout << "Input dimensions of set and count of pairs the finitary relation:\n";
- cin >> dim >> n;
- vector <pair<int,int>> vec(n);
- cout << "Input pairs:\n";
- for (int i = 0; i < n; i++)
- cin >> vec[i].first >> vec[i].second;
- vector <vector<bool>> matrix(dim, vector<bool>(dim));
- for (int i = 0; i < n; i++) {
- matrix[vec[i].first-1][vec[i].second-1] = 1;
- }
- printMatrix(matrix);
- reflex = is_reflexsity(matrix);
- sym = is_symmetry(matrix);
- transit = is_transit(matrix);
- printAnswer(reflex, sym, transit);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement