also works with graph with 2 nodes