Seeking algorithm Closure transfer warshall

 ————————————————————————————

Question: R is a binary relation defined on the set S, R closure request is transmitted.

Input:relation R,set A

Output:t(R),which is the transitive closure of R 

Solution:Warshall algorithm

————————————————————————————

Transitive closure (transitive closure) R & lt * = R & lt . 1  ∪ R & lt ∪ R & lt . 3  ∪ ∪ R & lt ...... n-  

Explain transfer relationship R closure is a relationship that can be represented by R * is represented by t (R).

Warshall algorithm works like this:

First, we represented the R matrix,

Then begins to traverse from the first column by column, for the i-th column, from top to bottom through each element,

If the i-th row j-th column element is 1, corresponds to the i-th row j-th row added.

 

python simulation code:

 

# Assumes that the first two-dimensional array index for the row index R, and the second subscript is the column index. 
Warshall DEF (R & lt):
  n-size = (R & lt)
  for I in Range (0,. 1-n-):
    for J in Range (0,. 1-n-):
      IF R & lt [J] [I] ==. 1:
        R & lt [ J] [:] = R & lt + [I] [:]
  return R & lt

Guess you like

Origin www.cnblogs.com/dynmi/p/12122937.html