如何测试集合是自反,对称,反对称和/或传递?

时间:2011-11-16 17:18:55

标签: c++

我在尝试编写这些函数时遇到了困难。他们工作不正常,不知道我做错了什么。至于Transitive,我甚至无法开始,并希望你能给予任何帮助,以及我在功能上做错了什么。谢谢。
样本输入:

0 1 2 3 //elements (A)
0 0     //relations (B)
1 1
2 2
3 3

x y z //elements (A)
x y   //relations (B)
y z
y y
z z

x y z //elements (A)
x x   //relations (B)
y z
x y
z y
x z
y y
z x
y x
z z

1 2 3 4 5 6 7 8 //elements (A)
1 4             //relations (B)
1 7
2 5
2 8
3 6
4 7
5 8
6 6
1 1
2 2

代码:

bool reflexive(int a[], int sizeOfA, int b[], int sizeOfB) 
{
  bool hold = true;
  for(int i=0; i+1<sizeOfB; i+=2) 
  {
    int e = b[i];
    int e1 = b[i];
    if(pair_is_in_relation(e1, e, b, sizeOfB) == false) 
    {
        if (hold) 
    {
            return false;
            break;
         }
    }
  }
  if (hold)
    cout << "Reflexive - Yes" << endl; 
  else
    cout << "Reflexive - No" << endl; 
return hold;
}

bool symmetric(int a[], int sizeOfA, int b[], int sizeOfB)
{
bool hold = true; // set hold to true
for(int i=0; i+1<sizeOfB; i+=2) // for each pair (e,f) in b
{
    int e = b[i];
    int f = b[i+1];
    if(is_in_relation(f, e, b, sizeOfB)) // if pair(e,f) is not in b
    {
        if(hold) // set hold to false
        {
            return false;
            break;
        }
    }
}
if(hold) // if hold return true
    cout << "Symmetric - Yes" << endl;
else // if hold is false return false
    cout << "Symmetric - No" << endl;
}

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;

}

void transitive(int a[], int sizeOfA, int b[], int sizeOfB)
{

}

int main()
{
char keepGoing = 'y';
    while (keepGoing=='y') {

int set1[4] = {0, 1, 2, 3};
int rel1[8] = {0, 0, 1, 1, 2, 2, 3, 3};
cout << "Set 1: " << endl;
reflexive(set1, 3, rel1, 4);
symmetric(set1, 3, rel1, 4);
antiSymmetric(set1, 3, rel1, 4);

cout << endl;
char set2[4] = {'x', 'y', 'z'};
char rel2[8] = {'x', 'y', 'y', 'z', 'y', 'y', 'z', 'z'};
cout << "Set 2: " << endl;
charReflexive(set2, 4, rel2, 8);
charSymmetric(set2, 4, rel2, 8);
charAntiSymmetric(set2, 4, rel2, 8);

cout << endl;
char set3[3] = {'x', 'y', 'z'};
char rel3[18] = {'x', 'x', 'y', 'z', 'x', 'y', 'z', 'y', 'x', 
                 'z', 'y', 'y', 'z', 'x', 'y', 'x', 'z', 'z'};
cout << "Set 3: " << endl;
charReflexive(set3, 3, rel3, 18);
charSymmetric(set3, 3, rel3, 18);
charAntiSymmetric(set3, 3, rel3, 18);

cout << endl;
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;
reflexive(set4, 8, rel4, 20);
symmetric(set4, 8, rel4, 20);
antiSymmetric(set4, 8, rel4, 20);

cout << endl << "Would you like to test it again? (y/n): ";
    cin >> keepGoing;
}

return 0;
}

4 个答案:

答案 0 :(得分:2)

对于初学者来说,这个for循环的内容是什么,你永远不会增加被迭代的变量?

for(j = 0; j < sizeOfB;)
{
    ...
}

如果你在第一次迭代中突破它,为什么会有这样一个过于复杂的for无限循环呢?

如果您需要迭代两个集合的交叉产品,可以使用以下代码段作为开头:

for( int i = 0; i < sizeOfA; ++i )
{
    for( int j = 0; j < sizeOfB; ++j )
    {
        int elemA = a[i];
        int elemB = b[j];

        ... do your magic ...
    }
}

答案 1 :(得分:2)

我只看了reflexive,但你需要重新考虑一下。通常,如果A中的第一个元素不等于B中的第一个元素,则会打印"Reflexive - No"并停止。我不认为你一直都这么认为。

[编辑]好吧,既然我们已经确定int a[]拥有什么,以及int b[]拥有什么,我必须重新开始。以前每个人都完全错了。 (尤其是,我的旧“反身”非常对称,并且错误地解释输入。)如果您已经了解了C ++类/容器,我强烈建议您更换int a[]int b[]类似的东西:

template <class T>
struct relation {
    typedef std::pair<T,T> single_relation;

    std::set<T> elements;
    std::set<single_relation> all_relations;
};

或类似的东西,但那只是我。

reflexive:
    set holds to true
    for each element e in a
        if pair(e,e) is not in b
            set holds to false
            break
symmetric:
    set holds to true
    for each pair(e,f) in b
        if pair(f,e) is not in b
            set holds to false
            break
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
transitive:
    set holds to true
    for each pair(e,f) in b
        for each pair(f,g) in b
            if pair(e,g) is not in b
                set holds to false
                break
        if holds is false
            break

请注意,只有反身实际上需要a[] 演示:

bool pair_is_in_relation(int left, int right, int b[], int sizeOfB)
{
    for(int i=0; i+1<sizeOfB; i+=2) {
        if (b[i]==left && b[i+1]==right)
            return true;
    }
    return false;
}
bool antiSymmetric(int b[], int sizeOfB) 
{
    bool holds = true;
    for(int i=0; i+1<sizeOfB; i+=2) {
        int e = b[i];
        int f = b[i+1];
        if(pair_is_in_relation(f, e, b, sizeOfB)) {
            if (e != f) {
                holds = false;
                break;
             }
        }
    }
    if (holds)
        std::cout << "AntiSymmetric - Yes" << endl; 
    else
        std::cout << "AntiSymmetric - No" << endl; 
    return holds;
}

答案 2 :(得分:1)

首先,你需要直截了当地说明术语:集合S不是自反,对称,传递或任何类型的东西。这就是为什么你很难想象传递(......)应该做什么。

特定集合S上只有特定的二元关系B可以是自反,对称和传递的。

现在,让我们从集合和关系的角度来考虑这一点。假设你有一个函数,方便地称为关系:

bool relation(int a, int b) 
{
    /* some code here that implements whatever 'relation' models. We will
     * pick some relation we know to be reflective, transitive and symmetric.
     * For example: 
     */
    return (a == b);
}

让我们添加一个糟糕的关系,只是为了好玩。你可以用它来测试:

bool relation_bad(int a, int b) 
{
    /* some code here that implements whatever 'relation' models. This is
     * a relation that isn't symmetric, but it is reflexive and transitive. 
     */
    return (a >= b);
}

现在,您想要编写'反身'代码。反身性意味着项目与自身相关:

bool reflexive(int *s, int items) 
{
    for(int i = 0; i != items; i++)
    {
        if(!relation(s[i], s[i]))
            return false;
    }

    return true;
}

现在,'对称'。对称性意味着如果a与be相关,则b必须与a:

相关
bool symmetric(int *s, int items)
{
    for(int i = 0; i != items; i++)
    { // for every item in the set:
        for(int j = 0; j != items; j++)
        { // check against every other items (including itself!)
            if(relation(s[i], s[j]) != relation(s[j], s[i])
                return false;
        }
    }

    return true;
}

我不会为你编码传递,但传递意味着如果a与b相关,而b与c相关,则a必须与c相关。

你可以看到这里需要三个循环和更复杂的检查。

答案 3 :(得分:0)

\\to find symetric relation
#include<iostream.h>
using namespace std;
main() {
    int a[5],b[5],c,d=0,e,f;
    cout<<"Enter 1st elemtnts: ";
    for(int i=0;i<5;i++){
        cin>>a[i];
    }
    cout<<"Enter second elemnt :";
    for(int j=0;j<5;j++){
        cin>>b[j];
    }
    for(c=0;c<5;c++){
        for(d=0;d<5;d++){
            if(a[c]==b[d]){
                cout<<"("<<a[c]<<",";
                cout<<b[d]<<")";
            }     
        }
    }   
    cout<<" Are the symetric \n\n";
    system("pause");
}