Use topological order instead of dijkstra