Determine whether a directed graph has a unique topological ordering?

扶醉桌前 提交于 2019-12-24 11:30:15

问题


I'm trying to create pseudo-code for an algorithm that will be able to determine whether a directed graph has a unique topological ordering. I've already come up with the following pseudo-code for a topological sort, but what would I have to add or edit in order to determine whether a digraph has a unique topological ordering?

Input: A graph G
Output: A list L that contain the sorted vertices

 define an empty list L;
 create a stack Stack;
 push all vertices with no incoming edges into Stack;
 while Stack is not empty do

v ← Stack.pop();
add v to the list L;

for all the vertices w with an edge e from v to w do
remove edge e from G;
 if w has no other incoming edges then
push w into Stack;
 if G has edges left then
 return error (graph G can’t be topological ordered);
 else
 return L;

回答1:


There's no particular reason to use a stack as opposed to some other kind of collection. If we pop elements nondeterministically, then we can realize all possible topological orders. Thus, the topological order is unique if and only if the collection contains one element each time we pop (except when it's empty at the end).



来源:https://stackoverflow.com/questions/27198355/determine-whether-a-directed-graph-has-a-unique-topological-ordering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!