当前位置:网站首页>Realization of binary relation of discrete mathematics with C language and its properties

Realization of binary relation of discrete mathematics with C language and its properties

2022-07-05 07:41:00 LCH Nan'an

One 、 The experiment purpose

Master the representation of binary relationship on computer , And it can be realized by computer to determine the nature of the relationship .

Two 、 Experimental content

Program to judge whether a binary relationship is equivalent , If it is , Find its quotient set .( You can use C or Python To write ).

Equivalence relation : aggregate A The binary relationship on R At the same time, it is reflexive 、 Symmetry and transitivity , said R yes A The equivalence relationship on .

3、 ... and 、 Program design flow chart ( Or experimental principles )

  Result chart :

Code :


#include <stdio.h>

#define MaxSize 10
char a[MaxSize][MaxSize];
int i, j, n;

// Initialization matrix 
void Init()
{
    printf(" Enter the total number of fields of binary relationship :\n");
    scanf("%d", &n);
    printf(" Please enter the relationship matrix .\n");
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
}

// reflexivity 
int Reflexivity()
{ // The diagonal element is  0: That is, not satisfied 
    for (int i = 0; i < n; ++i)
    {
        if (a[i][i] == 1)
        {
            printf("1. Have reflexivity \n");
            return 1;
        }
        else if (a[i][i] != 1)
        {
            printf("1. Not reflexive \n");
            return 0;
        }
    }
    return 1;
}

// symmetry 
int Symmetry()
{
    for (int i = 0; i < n; ++i)
    {
        for (int j = 1; j < n; ++j)
        {
            if (a[i][j] == a[j][i]) // Symmetrical elements are not equal : That is, the symmetry is not satisfied 
            {
                printf("2. Have symmetry \n");
                return 1;
            }
            else if (a[i][j] != a[j][i])
            {
                printf("2. Without symmetry \n");
                return 0;
            }
            break;
        }
    }
    return 1;
}

// Transitivity 
int Transitivity()
{
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            for (int k = 0; k < n; ++k)
            {
                if (a[i][j] && a[j][k] && !a[i][k])
                {
                    printf("3. It's not transitive \n");
                    return 0;
                }
                else
                {
                    printf("3. Transitivity \n");
                    return 1;
                }
            }
        }
    }
    return 1;
}
// Quotient set 
void Quotient()
{
    int c[MaxSize];
    for (int i = 0; i < n; i++)
        c[i] = i + 1; // i  On behalf of the  i  Elements 

    printf("\n Quotient set is :\n");
    printf("{ ");
    for (int i = 0; i < n; i++) // Loop all elements 
    {
        if (c[i])
        {
            printf("{ ");
            for (int j = 0; j < n; j++)
            {
                if (a[i][j] && c[j])
                {
                    printf("%d ", c[j]);
                    c[j] = 0;
                }
            }
            printf("} ");
        }
    }
    printf("}\n");
}

int main()
{

    Init(); //  Initialization matrix 
    if (Reflexivity() && Symmetry() && Transitivity())
    {
        printf(" Therefore, there is an equivalence relationship \n");
        //  Print quotient set 
        Quotient();
    }
    else
    {
        printf(" Therefore, there is no equivalence relationship \n");
    }
}

原网站

版权声明
本文为[LCH Nan'an]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140551381665.html