Revert "Remove networkx dependency"

This reverts commit a3e2ce9fec2504c0f07dcc399ccf7aa409117201.
Revert "Revert "Remove networkx dependency""

This reverts commit c458f85813ebbea79c5abc0fe6443ffa1f693d7a.
changed add_edge to take distinct nodes instead of tuple