Change the algorithm use directed graph.