Algorithm]Operations on Relations

  1. relation.cpp

//
//  Relation.cpp
//  C++
//
//  Created by 李天培 on 16/4/9.
//  Copyright © 2016年 lee. All rights reserved.
//


#include "Relation.hpp"


Relation::Relation(BooleanMatrix const & m): matrix(m) {
}


BooleanMatrix Relation::getBooleanMatrix() const {
    return matrix;
}




Relation Relation::complementary() const {
    Relation temp(matrix);
    for (int i = 1; i <= matrix.getRow(); i++) {
        for (int j = 1; j <= matrix.getColums(); j++) {
            temp.matrix.replace(!temp.matrix.getElement(i, j), i, j);
        }
    }
    return temp;
}


Relation Relation::inverse() const {
    Relation inverseR(matrix.transpose());
    return inverseR;
}


Relation Relation::operator&(const Relation &r) {
    Relation temp(matrix & r.matrix);
    return temp;
}


Relation Relation::operator|(const Relation &r) {
    Relation temp(matrix | r.matrix);
    return temp;
}


2.

  • BinaryRelation.cpp
  • main.cpp
  • Set.hpp
  • Set.cpp
  • Matrix.hpp
  • Matrix.cpp
  • BooleanMatrix.hpp
  • BooleanMatrix.cpp
  • Relation.hpp
  • BinaryRelation.hpp

//
//  BinaryRelation.cpp
//  C++
//
//  Created by 李天培 on 16/4/7.
//  Copyright © 2016年 lee. All rights reserved.
//


#include "BinaryRelation.hpp"


BinaryRelation::BinaryRelation(BooleanMatrix const &m, Set const &s)
: Relation(m), set(s) {
}




int BinaryRelation::inDegree(int x) {
    if (set.isInSet(x)) {
        int pos = getSetElePos(x);
        int sum = 0;
        for (int i = 1; i <= matrix.getRow(); i++) {
            sum += matrix.getElement(i, pos);
        }
        return sum;
    }
    return -1;
}


int BinaryRelation::outDegree(int x) {
    if (set.isInSet(x)) {
        int pos = getSetElePos(x);
        int sum = 0;
        for (int i = 1; i <= matrix.getColums(); i++) {
            sum += matrix.getElement(pos, i);
        }
        return sum;
    }
    return -1;
}


int BinaryRelation::getSetElePos(int x) {
    for (int i = 1; i <= set.getSize(); i++) {
        if (set.get(i) == x) return i;
    }
    return -1;
}


BinaryRelation BinaryRelation::pathOfLength(int n) {
    BooleanMatrix tempBM = matrix;
    for (int i = 1; i < n; i++) {
        tempBM = tempBM.BooleanProduct(matrix);
    }
    BinaryRelation temp(tempBM, set);
    return temp;
}


bool BinaryRelation::isReflexive() const {
    for (int i = 1; i <= matrix.getRow(); i++) {
        if (matrix.getElement(i, i) == 0) {
            return false;
        }
    }
    return true;
}


bool BinaryRelation::isIrreflexive() const {
    for (int i = 1; i <= matrix.getRow(); i++) {
        if (matrix.getElement(i, i) == 1) {
            return false;
        }
    }
    return true;
}


bool BinaryRelation::isSymmetric() const {
    for (int i = 1; i <= matrix.getRow(); i++) {
        for (int j = 1; j <= matrix.getColums(); j++) {
            if (matrix.getElement(i, j) == 1 &&
                matrix.getElement(j, i) == 0) {
                return false;
            }
        }
    }
    return true;
}


bool BinaryRelation::isAsymmetric() const {
    for (int i = 1; i <= matrix.getRow(); i++) {
        for (int j = 1; j <= matrix.getColums(); j++) {
            if (matrix.getElement(i, j) == 1 &&
                matrix.getElement(j, i) == 1) {
                return false;
            }
        }
    }
    return true;
}


bool BinaryRelation::isAntisymmetric() const {
    for (int i = 1; i <= matrix.getRow(); i++) {
        for (int j = 1; j <= matrix.getColums(); j++) {
            if (i != j &&
                matrix.getElement(i, j) == 1 &&
                matrix.getElement(j, i) == 1) {
                return false;
            }
        }
    }
    return true;
}


bool BinaryRelation::isTransitive() const {
    for (int i = 1; i <= matrix.getRow(); i++) {
        for (int j = 1; j <= matrix.getColums(); j++) {
            for (int k = 1; k <= matrix.getRow(); k++) {
                if (matrix.getElement(i, k) == 1 &&
                    matrix.getElement(k, j) == 1 &&
                    matrix.getElement(i, j) == 0) {
                    return false;
                }
            }
        }
    }
    return true;
}


bool BinaryRelation::isEquivalence() const {
    return ((*this).isReflexive() &&
            (*this).isSymmetric() &&
            (*this).isTransitive());
}


BinaryRelation BinaryRelation::composition(const BinaryRelation & br) {
    BooleanMatrix m = matrix.BooleanProduct(br.getBooleanMatrix());
    return BinaryRelation(m, set);
}


BinaryRelation BinaryRelation::reflexiveClosure() const {
    BooleanMatrix identify(matrix.getRow(), matrix.getColums());
    for (int i = 1; i <= identify.getRow(); i++) {
        identify.replace(1, i, i);
    }
    return BinaryRelation(identify | matrix, set);
}


BinaryRelation BinaryRelation::symmetricClosure() const {
    return BinaryRelation(inverse().getBooleanMatrix() | matrix, set);
}


BinaryRelation BinaryRelation::transitiveClosure() const {
    int n = matrix.getRow();
    BooleanMatrix temp(matrix);
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                temp.replace(temp.getElement(i, j) |
                             (temp.getElement(i, k) &
                              temp.getElement(k, j)),
                             i, j);
            }
        }
    }
    return BinaryRelation(temp, set);
}

3.

//
//  main.cpp
//  C++
//

//  Created by 李天培 on 16/2/25.


//  Copyright © 2016年 lee. All rights reserved.
//


#include "Set.hpp"
#include "BooleanMatrix.hpp"
#include "Matrix.hpp"
#include "BinaryRelation.hpp"
#include "Relation.hpp"


using std::cin;
using std::cout;
using std::endl;
using std::string;


void displaySet(int* members, int size) {
    std::cout << "{";
    for (int i = 0; i < size; i++) {
        if (i < size - 1) std::cout << members[i] << ", ";
        else std::cout << members[i];
    }
    std::cout << "}" << std::endl;
}


void testBooleanMatrix() {
    cout << "TEST BOOLEAN MATRIX" << endl;
    int r = 0, c = 0;
    cin >> r >> c;
    BooleanMatrix m1(r, r);
    BooleanMatrix m2(r, r);
    BooleanMatrix m3(r, c);


    for (int i = 1; i <= r; i++) {
        for (int j = 1; j <= r; j++) {
            int t = 0;
            cin >> t;
            m1.replace(t, i, j);
        }
    }
    for (int i = 1; i <= r; i++) {
        for (int j = 1; j <= r; j++) {
            int t = 0;
            cin >> t;
            m2.replace(t, i, j);
        }
    }




    for (int i = 1; i <= r; i++) {
        for (int j = 1; j <= c; j++) {
            int t = 0;
            cin >> t;
            m3.replace(t, i, j);
        }
    }


    m1.display();
    cout << endl;
    m2.display();
    cout << endl;
    m3.display();
    cout << endl;
    (m1 | m2).display();
    cout << endl;
    (m1 & m2).display();
    cout << endl;
    (m1.BooleanProduct(m3)).display();
}


void testDigraph() {
    cout << "TEST BinaryRelation" << endl;
    unsigned int r = 1;
    cin >> r;


    BooleanMatrix matrix(r, r);
    for (int i = 1; i <= r; i++) {
        for (int j = 1; j <= r; j++) {
            bool t = true;
            int x;
            cin >> x;
            t = (x == 1) ? 1 : 0;
            matrix.replace(t, i, j);
        }
    }
    cout << "matrix:" << endl;
    matrix.display();
    cout << endl;


    Set set;
    int *test;
    test = new int[r];
    for (int i = 0; i < r; i++) {
        cin >> test[i];
        while (set.isInSet(test[i])) {
            test[i]++;
        }
        set.append(test[i]);
    }
    cout << "set:" << endl;
    displaySet(set.getMembers(), set.getSize());
    cout << endl;


    BinaryRelation d(matrix, set);


    int x;
    cin >> x;
    cout << test[x] << "'s in degree: " << d.inDegree(test[x]) << endl;


    cin >> x;
    cout << test[x] << "'s out degree: " << d.outDegree(test[x]) << endl;


    cin >> x;
    BinaryRelation pathD = d.pathOfLength(x);
    pathD.getBooleanMatrix().display();
    cout << endl;


    cout << "reflexvie: " << d.isReflexive() << endl;
    cout << "reflexvie: " << pathD.isReflexive() << endl;


    cout << "irreflexvie: " << d.isIrreflexive() << endl;
    cout << "irreflexvie: " << pathD.isIrreflexive() << endl;


    cout << "symmetric: " << d.isSymmetric() << endl;
    cout << "symmetric: " << pathD.isSymmetric() << endl;


    cout << "Asymmetric: " << d.isAsymmetric() << endl;
    cout << "Asymmetric: " << pathD.isAsymmetric() << endl;


    cout << "Antisymmetric: " << d.isAntisymmetric() << endl;
    cout << "Antisymmetric: " << pathD.isAntisymmetric() << endl;


    cout << "transitive: " << d.isTransitive() << endl;
    cout << "transitive: " << pathD.isTransitive() << endl;


    cout << "Equivalence: " << d.isEquivalence() << endl;
    cout << "Equivalence: " << pathD.isEquivalence() << endl;


    cout << "reflexive closure: " << endl;
    d.reflexiveClosure().getBooleanMatrix().display();
    cout << "reflexvie: " << d.reflexiveClosure().isReflexive() << endl;


    cout << "symmetric closure: " << endl;
    d.symmetricClosure().getBooleanMatrix().display();
    cout << "symmetric: " << d.symmetricClosure().isSymmetric() << endl;


    cout << "transitive closure: " << endl;
    d.transitiveClosure().getBooleanMatrix().display();
    cout << "transitive: " << d.transitiveClosure().isTransitive() << endl;


    cout << "composition: " << endl;
    d.composition(pathD).getBooleanMatrix().display();




    delete [] test;
}






void testRelation() {
    cout << "test relation" << endl;
    unsigned int r = 1, c = 1;
    cin >> r >> c;


    BooleanMatrix matrix1(r, c);
    for (int i = 1; i <= r; i++) {
        for (int j = 1; j <= c; j++) {
            bool t = true;
            int x;
            cin >> x;
            t = (x == 1) ? 1 : 0;
            matrix1.replace(t, i, j);
        }
    }
    cout << "matrix1:" << endl;
    matrix1.display();
    cout << endl;


    BooleanMatrix matrix2(r, c);
    for (int i = 1; i <= r; i++) {
        for (int j = 1; j <= c; j++) {
            bool t = true;
            int x;
            cin >> x;
            t = (x == 1) ? 1 : 0;
            matrix2.replace(t, i, j);
        }
    }
    cout << "matrix2:" << endl;
    matrix2.display();
    cout << endl;


    Relation r1(matrix1);
    r1.getBooleanMatrix().display();
    cout << endl;


    Relation r2(matrix2);
    r2.getBooleanMatrix().display();
    cout << endl;


    r1.complementary().getBooleanMatrix().display();
    cout << endl;
    r1.inverse().getBooleanMatrix().display();
    cout << endl;


    (r1 & r2).getBooleanMatrix().display();
    cout << endl;
    (r1 | r2).getBooleanMatrix().display();
    cout << endl;
}


int main() {
    testBooleanMatrix();
    testDigraph();
    testRelation();
}


4.

//
//  Set.hpp
//  C++
//
//  Created by 李天培 on 16/2/25.
//  Copyright © 2016年 lee. All rights reserved.
//


#ifndef Set_hpp
#define Set_hpp


#include <stdio.h>


#define MAX_MEMBERS 100


class Set {
private:
    int members[MAX_MEMBERS];
    int size;
public:
    // Create an empty set.
    Set();
    // Create an set with some element.
    Set(int* m, int s);
    // Copy Constructor
    Set(Set const &s);


    // append a element to set.
    // If element in the set, return false.
    // Or insert in set and return true.
    bool append(int e);


    // remove a element by its value from set.
    // If element in the set, then remove it and return true.
    // Or return false.
    bool remove(int e);


    // return true if the set is empty, or return false.
    bool isEmptySet();


    // return true if the element e is in the set, or return false.
    bool isInSet(int e);


    // & is intersection of two set
    Set operator&(const Set &s);
    // | is union of two set
    Set operator|(const Set &s);
    // A - B is the complement of B with respect to A
    Set operator-(const Set &s);
    // A + B is their symmetric difference. A + B = (A - B) | (B - A)
    Set operator+(const Set &s);


    // return set.
    int* getMembers();


    // return size of set.
    int getSize();


    int get(int pos);
};


#endif /* Set_hpp */







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值