Modify algorithm to work with actual graph structure.