fix bug in graph to avoid duplicating edges

Also, add some comments and a function to print graphs.