Answer to Question #294182 in Java | JSP | JSF for mona

Question #294182

How To Test Whether a Set is Reflexive, Symmetric, Anti-Symmetric and/or Transitive using 2darray?




1
Expert's answer
2022-02-06T02:14:03-0500
public class Main {

    public static boolean isReflexive(int[] set, int[][] relations) {
        for (Integer element : set) {
            boolean exist = false;
            for (int[] relation : relations) {
                if (relation[0] == element && relation[1] == element) {
                    exist = true;
                    break;
                }
            }
            if (!exist) {
                return false;
            }
        }
        return true;
    }

    public static boolean isSymmetric(int[][] relations) {
        for (int[] relation : relations) {
            if (relation[0] != relation[1]) {
                boolean symmetric = false;
                for (int[] relationB : relations) {
                    if (relationB[0] == relation[1] && relationB[1] == relation[0]) {
                        symmetric = true;
                        break;
                    }
                }
                if (!symmetric) {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean isAntiSymmetric(int[][] relations) {
        for (int[] relation : relations) {
            if (relation[0] != relation[1]) {
                boolean antiSymmetric = true;
                for (int[] relationB : relations) {
                    if (relationB[0] == relation[1] && relationB[1] == relation[0]) {
                        antiSymmetric = false;
                        break;
                    }
                }
                if (!antiSymmetric) {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean isTransitive(int[][] relations) {
        for (int[] relationA : relations) {
            for (int[] relationB : relations) {
                if (relationA[1] == relationB[0]) {
                    boolean transitive = false;
                    for (int[] relationC : relations) {
                        if (relationC[0] == relationA[0] && relationC[1] == relationB[1]) {
                            transitive = true;
                            break;
                        }
                    }
                    if (!transitive) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS