Graph: Make edge visiting code reusable