Gibbs sampler for factor graphs