Implementation of Dijkstra termination detection algorithm