How To Test Whether a Set is Reflexive, Symmetric, Anti-Symmetric and/or Transitive using 2darray?
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;
}
}
Comments
Leave a comment