[C++]Operation on Relations

1452 ワード

Operation on Relations
Description:
This problem tries to put discrete mathematical structure in program. Using matrix, set to show relation. It is a bit hard but not severely difficult.
The demo link:
Demo Link
Analysis:
The answer, given by TA, is much easier than me. He use many method which has been given before, even if that method is also easy to write again, but it still save much time and decrese errors. It is a good tip whatever.
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);
}