当前位置:网站首页>Njupt Nanyou Discrete Mathematics_ Experiment 3

Njupt Nanyou Discrete Mathematics_ Experiment 3

2022-06-11 00:10:00 Retreat drum ten level performer

Content :
Find the set A On the division relationship R The corresponding covering relation , And determine the poset <A,R> Whether it is a case , If case , Judge whether it is a complemented lattice .
requirement :
aggregate A It can be any set of positive integers given by the user .

package exp03;

import java.util.*;
import java.util.stream.Collectors;

class Poset {
    
    int A[];    //  aggregate 
    int R[][];  //  The relational matrix 

    public Poset(int[] a) {
    
        A = a;
        //  Initialize the relationship matrix 
        R=new int[A.length][A.length];
        for(int i = 0; i < A.length; i++){
    
            for(int j = 0; j < A.length; j++){
    
                if(A[j] % A[i] == 0){
      // If it satisfies the divisible relation , Set it to  1
                    R[i][j] = 1;
                }
            }
        }

    }

    //  Seek to cover the relationship 
    public void displayCOVER() {
    

        int matrixs[][]=R;  //  A binary array used to determine the covering relationship 
        for(int i = 0; i < A.length; i++){
    
            for(int j = 0; j <A.length; j++){
    
                for(int k = 0; k <A.length; k++){
    
                    matrixs[k][k] = 0;  //  Get rid of reflexivity 
                    if(matrixs[i][j]==1 && matrixs[j][k]==1){
    
                        matrixs[i][k] = 0;  //  Remove transitivity 
                    }
                }
            }
        }

        System.out.print("COV A={");
        for(int i = 0; i <A.length; i++){
    
            for(int j = 0; j <A.length; j++){
    
                if(matrixs[i][j]==1){
      // Remove what was removed before , Others are satisfied to cover the relationship 
                    System.out.print("<"+A[i]+","+A[j]+">,");
                }
            }
        }
        System.out.println("\b}");
    }

    //  Judge whether it is case 
    public boolean isLattice(){
    
        int Gcd=0;  //  greatest common divisor 
        int Lcm=0;  //  Minimum common multiple 
        boolean flag=true;
        List<Integer> list = Arrays.stream(A).boxed().collect(Collectors.toList()); //  Convert to a collection for easy query of elements 
        for(int i=0;i<A.length;i++)
        {
    
            for (int j=0;j<A.length;j++)
            {
    
                if(i==j)
                    continue;
                Gcd = gcd(A[i],A[j]); //  Find the maximum lower bound 
                Lcm = A[i] / Gcd * A[j]; //  Find the minimum upper bound 
                //  Traverse A, if Gcd and Lcm All in A in , For Grid 
                if(!(list.contains(Gcd)&&list.contains(Lcm)))
                {
    
                    return false;
                }
            }
        }
        return flag;
    }

    //  Find the greatest common divisor 
    private int gcd(int x, int y) {
    
        int m;
        // division 
        do{
    
            m = x % y;
            x = y;
            y = m;
        }while(m != 0);
        return x;
    }

    public boolean isComplementedLattice() {
    
        int Gcd=0;  //  greatest common divisor 
        int Lcm=0;  //  Minimum common multiple 
        boolean flag;
        List<Integer> list = Arrays.stream(A).boxed().collect(Collectors.toList()); //  Convert to a collection for easy query of elements 
        Collections.sort(list);
        for(int i=0;i<A.length;i++)
        {
    
            flag=false;
            for (int j=0;j<A.length;j++)
            {
    
                if(i==j)
                    continue;
                Gcd = gcd(A[i],A[j]); //  Find the maximum lower bound 
                Lcm = A[i] / Gcd * A[j]; //  Find the minimum upper bound 
                //  Bounded lattices 
                if( (Gcd==list.get(0)) && (Lcm==list.get(list.size()-1)))
                {
    
                    flag = true;
                    break;
                }
            }
            if(!flag){
    
                return false;
            }
        }
        return true;
    }
}

package exp03;

import java.util.Scanner;

public class Test03 {
    
    public static void main(String[] args) {
    
        System.out.println(" Please enter a positive integer set A:");
        //  Convert user input into an array 
        Scanner inA = new Scanner(System.in);
        String stringA[] = inA.nextLine().split(" ");
        int num[] = new int[stringA.length];
        for (int i = 0; i < stringA.length; i++) {
    
            num[i] = Integer.parseInt(stringA[i]);
            //System.out.println(stringA[i]);
        }
        //  Initialize poset 
        Poset poset=new Poset(num);
        poset.displayCOVER();
        if (poset.isLattice()){
    
            System.out.println("<A,R> It's Ge ");
            if(poset.isComplementedLattice())
                System.out.println("<A,R> There are complements ");
            else
                System.out.println("<A,R> There is no complement ");
        }else
            System.out.println("<A,R> Not a case ");
    }
}


原网站

版权声明
本文为[Retreat drum ten level performer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020629039934.html