2021-06-16

蒟蒻湘潭打铁后第四篇博客
实验:
二元关系性质判断
【问题描述】
空集或非空的有序对集合是二元关系,记作R。二元关系的性质主要有:自反性、反自反性、对称性、反对称性和传递性。实验要求通过算法设计并编写程序实现对给定集合上关系性质的判断,加深对关系性质的理解,掌握用矩阵来判断关系性质的方法。
【基本要求】
判断自反性、反自反性、对称性、反对称性。
【测试数据】
二元关系R的关系矩阵如下所示:
1 0 0 1
0 1 0 0
0 0 1 0
1 0 0 1
测试该关系具有的性质。
【选作内容】
判断二元关系的传递性。

C++代码如下:

#include <bits/stdc++.h>

using namespace std;

const int N = 1000;
int n;
int a[N][N];
int b[N][N];

bool is_reflexive(int a[N][N])
{
	for(int i = 1; i <= n; i ++ )
	{
		for(int j = 1; j <= n; j ++ )
		{
			if(i == j)
			{
				if(!a[i][j]) return false;
			}
		}
	}
	return true;
}

bool is_antireflexive(int a[N][N])
{
	for(int i = 1; i <= n; i ++ )
	{
		for(int j = 1; j <= n; j ++ )
		{
			if(i == j)
			{
				if(a[i][j]) return false;
			}
		}
	}
	return true;
}

bool is_symmetry(int a[N][N])
{
	for(int i = 1; i <= n; i ++ )
	{
		for(int j = 1; j <= n; j ++ )
		{
			if(a[i][j] != a[j][i]) return false;
		}
	}
	return true;
}

bool is_antisymmetry(int a[N][N])
{
	for(int i = 1; i <= n; i ++ )
	{
		for(int j = 1; j <= n; j ++ )
		{
			if(a[i][j] == a[j][i] && a[i][j] == 1)
			{
				if(i != j) return false;
			} 
		}
	}
	return true;
}

bool is_transfer(int a[N][N])
{
	for(int i = 1; i <= n; i ++ )
	{
		for(int j = 1; j <= n; j ++ )
		{
			for(int k = 1; k <= n; k ++ )
			{
				if(a[i][j] == 1 && a[j][k] == 1)
				{
					if(a[i][k] == 1) continue;
					else return false;
				}
			}
		}
	}
	return true;
}

int main()
{
	ios::sync_with_stdio(false);
	cout << "Please input the row number of relation matrix of binary relation:";
	cin >> n;
	cout << "Please input relation matrix of binary relation:" << endl;
	for(int i = 1; i <= n; i ++ )
	{
		for(int j = 1; j <= n; j ++ )
		{
			cin >> a[i][j];
		}
	}
	if(is_reflexive(a)) cout << "The relation matrix of binary relation is reflexive" << endl;
	else cout << "The relation matrix of binary relation isn't reflexive" << endl;

	if(is_antireflexive(a)) cout << "The relation matrix of binary relation is anti-reflexive" << endl;
	else cout << "The relation matrix of binary relation isn't anti-reflexive" << endl;

	if(is_symmetry(a)) cout << "The relation matrix of binary relation is symmetry" << endl;
	else cout << "The relation matrix of binary relation isn't symmetry" << endl;

	if(is_antisymmetry(a)) cout << "The relation matrix of binary relation is anti-symmetry" << endl;
	else cout << "The relation matrix of binary relation isn't anti-symmetry" << endl;

	if(is_transfer(a)) cout << "The relation matrix of binary relation has tranfer" << endl;
	else cout << "The relation matrix of binary relation has no tranfer" << endl;
	return 0;
}


Java代码

import java.util.Scanner;

public class Binaryrelation {
    public static void main(String args[]) {
        System.out.print("Please input the row number of relation matrix of binary relation:");
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        System.out.println("Please input relation matrix of binary relation:");
        final int N = 1000;
        int a[][] = new int[N][N];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                a[i][j] = in.nextInt();
            }
        }
        if (is_reflexive(a , n)) System.out.println("The relation matrix of binary relation is reflexive.");
        else System.out.println("The relation matrix of binary relation isn't reflexive");

        if (is_antireflexive(a , n)) System.out.println("The relation matrix of binary relation is anti-reflexive");
        else System.out.println("The relation matrix of binary relation isn't anti-reflexive");

        if (is_symmetry(a , n)) System.out.println("The relation matrix of binary relation is symmetry");
        else System.out.println("The relation matrix of binary relation isn't symmetry");

        if (is_antisymmetry(a , n)) System.out.println("The relation matrix of binary relation is anti-symmetry");
        else System.out.println("The relation matrix of binary relation isn't anti-symmetry");

        if (is_transfer(a , n)) System.out.println("The relation matrix of binary relation has tranfer");
        else System.out.println("The relation matrix of binary relation has no tranfer");
    }
    public static boolean is_reflexive(int a[][] , int n){
        for(int i = 1; i <= n; i ++ )
        {
            for(int j = 1; j <= n; j ++ )
            {
                if(i == j)
                {
                    if(a[i][j] == 0) return false;
                }
            }
        }
        return true;
    }
    public static boolean is_antireflexive(int a[][] , int n){
        for(int i = 1; i <= n; i ++ )
        {
            for(int j = 1; j <= n; j ++ )
            {
                if(i == j)
                {
                    if(a[i][j] != 0) return false;
                }
            }
        }
        return true;
    }
    public static boolean is_symmetry(int a[][] ,int n){
        for(int i = 1; i <= n; i ++ )
        {
            for(int j = 1; j <= n; j ++ )
            {
                if(a[i][j] != a[j][i]) return false;
            }
        }
        return true;
    }
    public static boolean is_antisymmetry(int a[][] ,int n){
        for(int i = 1; i <= n; i ++ )
        {
            for(int j = 1; j <= n; j ++ )
            {
                if(a[i][j] == a[j][i] && a[i][j] == 1)
                {
                    if(i != j) return false;
                }
            }
        }
        return true;
    }
    public static boolean is_transfer(int a[][] , int n){
 	for(int i = 1; i <= n; i ++ )
	{
		for(int j = 1; j <= n; j ++ )
		{
			for(int k = 1; k <= n; k ++ )
			{
				if(a[i][j] == 1 && a[j][k] == 1)
				{
					if(a[i][k] == 1) continue;
					else return false;
				}
			}
		}
	}
	return true;
    }
}


传递性可以也可以这样判断

bool is_transfer(int a[N][N])
{
	for(int i = 1; i <= n; i ++ )
	{
		for(int j = 1; j <= n; j ++ )
		{
			for(int k = 1; k <= n; k ++ )
			{
				b[i][j] += a[i][k] * a[k][j];
			}
		}
	}

	for(int i = 1; i <= n; i ++ )
	{
		for(int j = 1; j <= n; j ++ )
		{
			if(a[i][j] == 1)
			{
				if(b[i][j] == 1) continue;
				else return false;
			}
		}
	}
	return true; 
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值