Implementing factor graph.