1 Matrix Multiplication to Test for Transitive Relations

advertisement
CS 241, Prof. D. Nassimi
1
Spring 2015
Matrix Multiplication to Test for Transitive Relations
These notes supplement our class discussion on matrices and test of transitivty. First, we discuss how to
use the matrix of a relation directly to test if the relation is transitive. Then, we will see an alternative
method of using matrix multiplication to test for transitivity.
1.1
Direct Observation of Matrix
A relation R on a set {1, 2, 3, · · · , n} may be represented by an n × n Boolean matrix A, where
(
A[i, j] =
1, if (i, j) ∈ R,
0, otherwise.
(1)
We can test if a relation R is transitive by directly examining its matrix. A relation is transitive if and
only if the following condition is true.
∀i∀j∀k, if (A[i, k] = 1 ∧ A[k, j] = 1) then A[i, j] = 1.
(2)
As an equivalent formulation, a relation is transitive if and only if:
∀i∀j, if ∃k (A[i, k] = 1 ∧ A[k, j] = 1) then A[i, j] = 1.
(3)
To see that the above two propositions are equivalent, we note that negating either one results in the
following proposition, which says a relation is not transitive if and only if:
∃i∃j∃k,
(A[i, k] = 1) ∧ (A[k, j] = 1) ∧ (A[i, j] = 0)
(4)
As an example, consider the relation
R = {(1, 1), (1, 2), (2, 1), (2, 2), (2, 3), (3, 3)}
The matrix of this relation is shown below. By directly examining the matrix, we note the relation is not
transitive because A[1, 2] = 1 and A[2, 3] = 1 but A[1, 3] = 0. (These entries of the matrix are marked as
circled.)


1 0
1 
1 
A= 1 1 
0 0 1
The following program tests if a relation is transitive, by basically enumerating all possible values of i, j, k.
This program runs in O(n3 ) time.
1
for i = 1 to n
for j = 1 to n
for k = 1 to n
if (A[i, k] = 1 and A[k, j] = 1 and A[i, j] = 0)
return (FALSE);
return(TRUE);
Next, we will discuss how to use matrix multiplication to test transitivity.
2
Review of Matrix Multiplication
Given two n × n matrices A and B, their product matrix C = A × B is defined as:
C[i, j] =
n
X
(A[i, k] ∗ B[k, j]).
k=1
That is, to compute the term C[i, j], we take row i of matrix A, column j of matrix B, and compute their
inner product.
If we use this formula in a staightforward way to compute the product, each product term C[i, j] takes O(n)
time, thus the entire product matrix C takes O(n3 ) time. However, there are more advanced algorithms that
run more efficiently. For example, Strassen’s matrix multiplication algorithm runs in O(nlog2 7 ) ≈ O(n2.8 ).
2.1
Example of Matrix Multiplication






1 0 1
1
2
0 1 1 1  
 

0 = 2 1 1 
1 1 × 1 1  0
.
1
1 0 0
1 0 0 1 1
For example, C[1, 3] = (1 ∗ 1) + (0 ∗ 0) + (1 ∗ 1) = 2. (The terms are circled.)
2.2
Boolean Matrix Multiplication
When we multiply two Boolean matrices, their product is normally expresed as a Boolen matrix. One way
to do this is to first find the product in the regular way, as in the above example, then convert all product
terms to Boolean. (That is, all terms greater than 1 are converted to 1.)
Alternatively, we can compute the Boolean product directly as follows:
C[i, j] = ∨nk=1 (A[i, k] ∧ B[k, j]).
Observe from this formula that C[i, j] = 1 if and only if:
∃k, (A[i, k] = 1 and B[k, j] = 1).
This observation will be useful later in the proof of the test for transitivity.
2
(5)
2.3
Example of Boolean Matrix Multiplication
Let us multiply the above two Boolean matrices to find their Boolean product:






1
1
1 0 1
1 1 0 1 
 
 

0
1
1
1 .
1
1
0
1
1
=
×
 
 

1
0 1 1
1 0 1 0 0
For example, C[1, 3] = (1 ∧ 1) ∨ (0 ∧ 0) ∨ (1 ∧ 1) = 1. (The terms are circled.)
3
Use of Matrix Multiplication to Test for Transitivity
3.1
The Test
Let A be the matrix of a relation R.
1. Compute the square of the matrix (as a Boolean product), A2 = A × A.
2. Compare A with A2 term-by-term to decide if the relation is transitive. The relation is transitive if
and only if the following is true:
T :
∀i∀j,
if (A2 [i, j] = 1) then (A[i, j] = 1).
Equivalently, a relation is not transitive if and only if:
∃i∃j,
(A2 [i, j] = 1) ∧ (A[i, j] = 0).
Let us see why test T is correct. From our earlier discussion on matrix multiplication (see Proposition 5),
we know that A2 [i, j] = 1 if and only if
∃k,
(A[i, k] = 1) ∧ (A[k, j] = 1).
That is, the two conditions are equivalent. Therefore, we may replace the term (A2 [i, j] = 1) in test T
with its equivalent condition. So test T becomes:
∀i∀j, if ∃k (A[i, k] = 1 ∧ A[k, j] = 1) then A[i, j] = 1.
But this is exactly the definition of a transitive relation as stated earlier in Proposition (3). So, test T is
correct.
3.2
Example of the Test
Consider again the following matrix of the relation:
3


1 1 0


A= 1 1 1 
0 0 1
Earlier, by direct observation of the matrix, we decided the relation is not transitive because A[1, 2] = 1
and A[2, 3] = 1 but A[1, 3] = 0.
Now, let us use matrix multiplication to decide if the relation is transitive.

1 1

A2 = A × A =  1 1
0 0




1 1 0
1 1
0
 
 
1
1
1
=
×
1  
  1 1
0 0 1
1
0 0
We conclude the relation is not transitive because A2 [1, 3] = 1 but A[1, 3] = 0.
4

1

1 .
1
Download