Implement optimizations for belief propagation