Implementation of 1 pass Bellman using toposort