【离散数学】【改进版】实验二 集合上二元关系性质判定的实现

性质判断原理和编码思路

         关于自反性、对称性、传递性、反自反性和反对称性的定义不在此赘述。自反性对称性和反自反反对称比较简单,关于传递性的判断,我们使用Warshall算法计算传递闭包,当传递闭包对应的关系矩阵与原关系矩阵一致时,我们认为它是满足传递性的。

关于编码思路,做个提纲: 

         一共6个函数,前5个函数分别表示对5个性质的判断,第6个是Warshall算法函数,实现封装机制,在第3个判断传递性的函数中直接调用函数6即可。

关于输入输出的说明:第一次输入的是集合元素个数,第二个输入的是关系个数,然后接着输入关系,输出结果判断,我将在下面以例子说明。

实现代码:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string.h>
#include <cstring>
using namespace std;

const int LEN = 100;
bool  Reflexivity();   //自反性
bool  Symmetry();      //对称性
bool  Transmission();  //传递性
bool  Irreflexivity();  //反自反性
bool  Irsymmetry();    //反对称性
void  Warshall();       //Warshall算法

int num;
int relation_num;
int relation[LEN][LEN];
int A[LEN][LEN];

int main()
{
    while(cin >> num && cin >> relation_num)
    {
        int tmp1, tmp2;
        memset(relation, 0, sizeof(relation));
        memset(A, 0, sizeof(A));

        for(int i = 1; i <= relation_num; i++)
        {
            cin >> tmp1 >> tmp2;
            relation[tmp1][tmp2] = 1;
        }

        if(Reflexivity())
        {
            cout << "Meet the reflexive..." ;
            }
        else
        {
            cout << "Not meet the reflexive...";
        }
        cout << endl;

        if(Symmetry())
        {
            cout << "Meet the Symmetry...";
        }
        else
        {
            cout << "Not meet the Symmetry...";
        }
        cout << endl;

        if(Transmission())
        {
            cout << "Meet the Transmission...";
        }
        else
        {
            cout << "Not meet the Transmission...";
        }
        cout << endl;

        if(Irreflexivity())
        {
            cout << "Meet the Irreflexivity...";
        }
        else
        {
            cout << "Not meet the Irreflexivity...";
        }
        cout << endl;

        if(Irsymmetry())
        {
            cout << "Meet the Irsymmetry..";
        }
        else
        {
            cout << "Not meet the Irsymmetry...";
        }
        cout << endl;

    }
    return 0;
}

bool  Reflexivity()  //自反性
{
  //  bool flag = false;
    for(int i = 1; i <= num; i++)
    {
        if(relation[i][i] != 1)
        {
            return false;
        }
    }
    return true;
}

bool  Symmetry()     //对称性
{
    for(int i = 1; i <= num; i++)
    {
        for(int j = 1; j <= num; j++)
        {
            if(relation[i][j] != relation[j][i])
            {
                return false;
            }
        }
    }
    return true;
}

bool  Transmission()  //传递性
{
    Warshall();
    for(int i = 1; i <= num; i++)
    {
        for(int j = 1; j <= num; j++)
        {
            if(A[i][j] != relation[i][j])
            {
                return false;
            }
        }
    }
    return true;
}

bool  Irreflexivity()  //反自反性
{
     for(int i = 1; i <= num; i++)
     {
         if(relation[i][i] == 1)
         {
             return false;
         }
     }
     return true;
}

bool  Irsymmetry()    //反对称性
{
     for(int i = 1; i <= num - 1; i++)
     {
         for(int j = i + 1; j <= num; j++)
         {
              if(relation[i][j] == 1 && relation[j][i] == 1)
              {
                  if(i != j)
                  {
                      return false;
                  }
              }
         }
     }
     return true;
}

void  Warshall()       //Warshall算法
{
    for(int i = 1; i <= num; i++)
    {
        for(int j = 1; j <= num; j++)
        {
            A[i][j] = relation[i][j];
        }
    }
    for(int i = 1; i <= num; i++)
    {
        for(int j = 1; j <= num; j++)
        {
            if(A[j][i] == 1)
            {
                for(int k = 1; k <= num; k++)
                {
                    A[j][k] = A[j][k] + A[i][k];
                    if(A[j][k] >= 1)
                    {
                        A[j][k] =  1;
                    }
                }
            }
        }
    }

}

假设我们有一个集合A={1,2,3,4},A上的关系为{<1,1>,<1,3>,<2,2>,<3,3>,<3,1>,<3,4>,<4,3>,<4,4>},接下来我们判断该关系的性质。

因为集合元素个数有4个,所以输入4

因为关系个数共8个,所以接着输入8

接着输入

1   1 

1   3

2   2

....

等,一共8组数据。

运行示例如下:


版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/Tobyuyu/p/4965340.html