Question: Implement the following method. In this lab, a binary relation is represented using a boolean matrix (double array of booleans) as follows. Recall that A={a1,an}
Implement the following method.
In this lab, a binary relation is represented using a boolean matrix (double array of booleans) as follows.
Recall that A={a1,an} and RAA is a relation on A.
We represent a binary relation R using a matrix MR, where MR[i][j]=true means that (ai+1,aj+1)R, and MR[i][j]=false means that (ai+1,aj+1)R. (Note there is an off by one thing going on here. Our set starts couning elements at 1 but double arrays in Java are indexed starting at 0.)
| public class Relations { | |
| /** | |
| * Returns composition of R and S. Return relation T such that if (i, j) in R and (j, k) in S then (i, k) in T. | |
| * @param S relation, represented as a matrix (double array) | |
| * @param R relation, represented as a matrix (double array) | |
| * @return S composed with R | |
| */ | |
| public static boolean[][] compose(boolean[][] R, boolean[][] S) { | |
| // compare matrix sizes and make sure they agree: if R is n1 x n2 then S should be n2 x n3. | |
| int n1 = R.length; | |
| int n3 = S.length; | |
| if (n1 == 0 || n3 == 0) throw new UnsupportedOperationException("expecting non-empty arrays!"); | |
| int n2 = R[0].length; | |
| int n4 = S[0].length; | |
| if (n2 == 0 || n4 == 0) throw new UnsupportedOperationException("expecting non-empty arrays!"); | |
| if (n2 != n3) throw new UnsupportedOperationException("Number of columns of R must match number of rows of S"); | |
| throw new UnsupportedOperationException("implement me!"); | |
| } | |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
