Efficient Detection of Commutative Factors in Factor Graphs