fix bug in graph to avoid duplicating edges