#NEWLINE# def transitive_closure( implications ) : #NEWLINE# #INDENT# "\n    Computes the transitive closure of a list of implications\n\n    Uses Warshall's algorithm, as described at\n    http://www.cs.hope.edu/~cusack/Notes/Notes/DiscreteMath/Warshall.pdf.\n    " #NEWLINE# full_implications = set ( implications ) #NEWLINE# literals = set ( ) . union ( * map ( set , full_implications ) ) #NEWLINE# for k in literals : #NEWLINE# #INDENT# for i in literals : #NEWLINE# #INDENT# if ( ( i , k ) in full_implications ) : #NEWLINE# #INDENT# for j in i : #NEWLINE# #INDENT# if ( ( k , j ) in full_implications ) : #NEWLINE# #INDENT# full_implications . add ( ( i , j ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return full_implications