问题
I'm trying to struct an algorithm that uses DFS for the purpose of deciding whether a given directed graph has unique topological sort or not.
My approach to the problem is that only a specific graph has a unique topological sort. And that graph is a chain like graph, in which all of the vertices are connected to each other in one line.
My dilemma is how to do an efficient DFS algorithm, and what exactly should I check.
回答1:
From here
a digraph has a unique topological ordering if and only if there is a directed edge between each pair of consecutive vertices in the topological order (i.e., the digraph has a Hamiltonian path).
So, you run DFS and you need to check that the longest path you went from start vertex has length |V|, then you have unique topological order. As MattTimmermans pointed out such graph can be reduced to a "chain graph".
来源:https://stackoverflow.com/questions/48030136/dfs-algorithm-that-decides-if-a-directed-graph-has-a-unique-topological-sort