Synthesizing patterns using factor graphs