简体   繁体   中英

How to implement an Antisymmetric Function?

I am trying to test whether the relations (pairs) of the set are antisymmetric meaning: a binary relation R on a set X is antisymmetric if, for all a and b in X if R(a,b) and R(b,a), then a = b, or, equivalently, if R(a,b) with a ≠ b, then R(b,a) must not hold.

antisymetric: 
 set holds to true
 for each pair(e,f) in b
    if pair(f,e) is in b
        if f is not e
            set holds to false
            break

What am I doing wrong??

Here is my function along with a sample test in the main:

void antiSymmetric(int b[], int sizeOfB)
{
bool hold = true; // set hold to true
for(int i = 0; i < sizeOfB;) // for each pair (e,f) in b
{
    if(hold == false)
    {
        cout << "AntiSymmetric - No" << endl; 
        break; //Did not find (e,e) in b
    }
    for(int j = 0; j < sizeOfB;)
    {
        if(b[i] == b[j+1] && b[i+1] == b[j]) //If true, then pair(f,e) exists
        {
            if(b[i+1] != b[i]) //If true, relation is antisymmetric
            {
                hold = true;
                break;
            }
            else
            {
                hold = false;
                j = j + 2;
            }
        }
        else
        {
            hold = false;
            j = j + 2;
        }

    }
    i = i + 2;

}
if(hold == true)
    cout << "AntiSymmetric - Yes" << endl;

}

int main()
{
    int set4[8] = {1, 2, 3, 4, 5, 6, 7, 8};
    int rel4[20] = {1, 7, 2, 5, 2, 8, 3, 6, 4,
                    7, 5, 8, 6, 6, 1, 1, 2, 2);

    cout << "Set 4: " << endl;
    antiSymmetric(rel4, 20);

  return 0;
}

Suppose your b array is { 1, 2, 3, 4 } . When i == 0 and j == 2 , the condition b[i] == b[j+1] && b[i+1] == b[j] reduces to 1 == 4 && 2 == 3 , which is false. So you take the else branch and set hold = false . But that test did not disprove the antisymmetry of the set. You set hold incorrectly in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM