This is copied verbatim from `bellman_ford`. Both needs to be fixed.