欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

离散数学—群的判定,java代码实现

程序员文章站 2022-12-20 10:34:48
实验目的:群的判定实验内容:输入代数系统的集合A和运算的运算表,判定是否是群。实验要求:提供输入接口;提供输出结果;元素集合中的元素至少有3个;上传程序源代码文件和运行效果截图。设*是定义在集合A上的二元运算:集合A非空、封闭即是广群;在广群的基础上对任意X,Y,Z∈A 有(X*Y)*Z=X*(Y*Z),可结合的,则它是半群;在半群的基础上含有幺元(X*幺元=X,幺元*X=X,X∈A),则它是独异点(含幺半群);在独异点基础上其内部每个....
实验目的:
群的判定
实验内容:
输入代数系统<A, >的集合A和运算的运算表,判定<A,*>是否是群。
实验要求:
提供输入接口;
提供输出结果;
元素集合中的元素至少有3个;
上传程序源代码文件和运行效果截图。

设*是定义在集合A上的二元运算:

集合A非空、封闭即是广群

在广群的基础上对任意X,Y,Z∈A 有(X*Y)*Z=X*(Y*Z),可结合的,则它是半群

在半群的基础上含有幺元(X*幺元=X,幺元*X=X,X∈A),则它是独异点(含幺半群)

在独异点基础上其内部每个元素都可逆(每个元素总能和另一个元素进行*运算等于幺元)则成为

实现思想:
是否封闭;
是否可结合(随机选3个元素进行运算,选择方法使用1/0背包方法);
是否含有幺元(判断每行列是否有与表头匹配的行或列);
是否可逆(判断每行每列是否有幺元);

下面是实现代码

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

/**
 * 输入代数系统<A,*>的集合A和*运算的运算表,判定<A,*>是否是群。
 * @author Datums
 */
public class Group {

    public static boolean flagClose;
    //封闭
    public static boolean flagAssociative = true;
    //可结合
    public static boolean flagIdentityElement;
    //含幺元
    public static boolean flagInverse;
    //可逆
    public static String identityElement = null;
    //幺元

    public static void getTable(String[] aggregate, String[][] table) {
        System.out.print("*    ");
        for (int i = 0; i < aggregate.length; i++) {
            System.out.print(aggregate[i]+"   ");
        }
        System.out.println();
        for (int i = 0; i < table.length; i++) {
            System.out.print(aggregate[i]+"    ");
            for (int j = 0; j < table[i].length; j++) {
                System.out.print(table[i][j]+"   ");
            }
            System.out.println();
        }

    }
    //打印

    public static String[][] textInTable(String[] aggregate, Scanner scanner) {

        String[][] table = new String[aggregate.length][aggregate.length];
        for (int i = 0; i < aggregate.length; i++) {
            for (int j = 0; j < aggregate.length; j++) {
                System.out.printf("Text in %s * %s=", aggregate[i], aggregate[j]);
                table[i][j] = scanner.next();
            }
        }
        return table;
    }
    //输入数值表

    public static boolean isClosed(String[] aggregate, String[][] table) {

        boolean flagClose = false;

        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table[i].length; j++) {
                for (int k = 0; k < aggregate.length; k++) {
                    if (aggregate[k].equals(table[i][j])) {
                        flagClose = true;
                        break;
                    } else {
                        flagClose = false;
                    }
                }
                if (!flagClose) {
                    return false;
                }
            }
        }
        return flagClose;
    }
    //判断是否为封闭

    public static int indexOf(String[] aggregate, String target) {
        for (int i = 0; i < aggregate.length; i++) {
            if (aggregate[i].equals(target)) {
                return i;
            }
        }
        return -1;
    }
    //获取指定数值的坐标

    public static void associative(int[] option, int i, int hadChoose, String[][] table, String[] aggregate) {
        if (i > option.length-1) {
            if (hadChoose == 3) {
                int[] choice = new int[3];
                int turn = 0;
                for (int s = 0; s < option.length; s++) {
                    if (option[s] == 1) {
                        choice[turn] = s;
                        turn++;
                    }
                }

                String tempLa = table[choice[0]][choice[1]];
                String tempRa = table[choice[1]][choice[2]];
                int leftNum = indexOf(aggregate, tempLa);
                int rightNum = indexOf(aggregate, tempRa);
                if (!table[leftNum][choice[2]].equals(table[choice[0]][rightNum])) {
                    flagAssociative=false;
                }

//                System.out.print("1/0选择是"+Arrays.toString(option)+" 选择是"+Arrays.toString(choice)+" 集合是"+Arrays.toString(aggregate)+" ");
//                System.out.print(aggregate[choice[0]]);
//                System.out.print(aggregate[choice[1]]);
//                System.out.print(aggregate[choice[2]]);
//                System.out.println("  ("+aggregate[choice[0]]+"*"+aggregate[choice[1]]+")*"+aggregate[choice[2]]+
//                        flagAssociative+aggregate[choice[0]]+"*("+aggregate[choice[1]]+"*"+aggregate[choice[2]]+")");
            }
        } else {
            option[i] = 1;
            associative(option, i + 1, hadChoose + 1, table, aggregate);
            option[i] = 0;
            associative(option, i + 1, hadChoose, table, aggregate);
        }
    }
    //判断可结合,使用1/0背包问题解决元素选择

    public static boolean isIncludeIdentityElement(String[][] table, String[] aggregate) {
        boolean flag = false;
        int num;
        for (int i = 0; i < table.length; i++) {
            num = 0;
            for (int j = 0; j < table.length; j++) {
                if (!table[j][i].equals(aggregate[j])) {
                    break;
                }else {
                    num++;
                }
            }

            if (num==table.length) {
                num = 0;
                for (int j = 0; j < table[i].length; j++) {
                    if (!table[i][j].equals(aggregate[j])) {
                        break;
                    } else {
                        num++;
                    }
                }
                if (num == table[i].length) {
                    identityElement = table[i][i];
                    //将幺元保存
                    return true;
                }
            }
        }
        return false;
    }
    //判断是否存在幺元

    public static boolean isInversed(String[][] table) {
        int num = 0;
        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table[i].length; j++) {
                if (table[i][j].equals(identityElement)) {
                    num++;
                    break;
                }
            }
        }
        for (int i = 0; i < table.length; i++) {
            for (int j = 0; j < table[i].length; j++) {
                if (table[j][i].equals(identityElement)) {
                    num++;
                    break;
                }
            }
        }
        if (num / 2 == table.length) {
            return true;
        } else {
            return false;
        }
    }
    //判断是否可逆,通过每行每列是否有幺元来判断

    public static void main(String[] args) {
        String[] aggregate = new String[0];
        //定义集合
        Scanner scanner = new Scanner(System.in);


        while (aggregate.length<3) {
            System.out.println("Please text in your aggregate: (e.g. <1,2,3,4>)");
            String temp = scanner.next();
            //输入集合

            temp = temp.substring(1, temp.length() - 1);
            aggregate = temp.split(",");
            //处理集合并引用
        }

        String[][] table;
        table = textInTable(aggregate, scanner);
        //输入数值表

        getTable(aggregate, table);
        //打印数值表

        flagClose = isClosed(aggregate, table);
        System.out.println("*是一个定义在集合Aggregate上的二元运算。");
        //* is a binary operation defined on aggregate.
        if (flagClose) {
            System.out.println("该集合是封闭的");
            //The aggregate is close.

            int[] option = new int[aggregate.length];
            associative(option, 0, 0, table, aggregate);
            if (flagAssociative) {
                System.out.println("该集合是可结合的");
                //The aggregate is associative.
                flagIdentityElement = isIncludeIdentityElement(table, aggregate);
                if (flagIdentityElement) {
                    System.out.println("该集合含有幺元" + identityElement);
                    //The aggregate has Identity Element ->

                    flagInverse = isInversed(table);
                    if (flagInverse) {
                        System.out.println("所有元素都可逆");
                        //All element can be inverted.
                        System.out.println("该集合是一个群");
                        //The aggregate is a Group
                    } else {
                        System.out.println("该集合是一个含幺半群(独异点)");
                        //The aggregate is a Monoid Group
                    }
                } else {
                    System.out.println("该集合是个半群");
                    //The aggregate is a Semi Group
                }
            } else {
                System.out.println("该集合是个广群");
                //The aggregate is a Groupoid
            }
        } else {
            System.out.println("集合不是封闭的");
            //The aggregate is not close.
        }

    }

}

离散数学—群的判定,java代码实现

本文地址:https://blog.csdn.net/lickdog/article/details/109575029