Generate Random Topological Sort of Graph in Python?

As we all know, topological sorts are not unique. A graph can have many topological sorts. For example, the graph {A, B, C}, {(A,C)} has exactly three sorts: {A,B,C}, {B,A,C}, and {A,C,B}.

My problem is this, using the NetworkX library in Python, I am trying to select a random topological sort from a directed graph. NetworkX has two functions that deal with topological sorting: nx.topological_sort() and nx.all_topological_sorts(). all_topological_sorts() will return all possible topological sorts. For the graph I mentioned above, it returns all three possible sorts.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> import networkx as nx
nodes = ["A", "B", "C"]
G = nx.DiGraph()
G.add_nodes_from(nodes)
G.add_edges_from([("A","C")])
print(list(nx.all_topological_sorts(G)))
</code>
<code> import networkx as nx nodes = ["A", "B", "C"] G = nx.DiGraph() G.add_nodes_from(nodes) G.add_edges_from([("A","C")]) print(list(nx.all_topological_sorts(G))) </code>
    import networkx as nx
    nodes = ["A", "B", "C"]

    G = nx.DiGraph()
    G.add_nodes_from(nodes)
    G.add_edges_from([("A","C")])
    print(list(nx.all_topological_sorts(G)))

nx.topological_sort() will return only a single sort using Kahn’s Algorithm. nx.all_topological_sorts() does not use Kahn’s algorithm. It Just based on how Kahn is implemented, if I change the order the nodes are inserted in the DiGraph, I change the topological sort Kahn returns. However, if I generated all possible permutations of {A, B, C} and pass that into Kahn’s, it only ever returns the topological sort {A, B, C} and {B, A, C}. No permutation returns {A, C, B} despite it being a valid sort. I would like all the possible topological sorts to be equally weighted, but at minimum I do not want one to be completely overlooked. I did some playing around with larger graphs, and in that case thousands of possible topological sorts are completely overlooked.

Now, I could just generate all topological sorts and then select one randomly. That would be equal; however, I am dealing with graphs much larger than three vertices, and that is just practically infeasible for me. I was wondering if there is a more computationally efficient way of getting a random topological sort, or if there some hackery I can do to the Graph to equally weight the possible topological sorts from Kahn’s.

I read this answer, however the poster points out that these are not equally weighted. So I expect I will run into the same problem as above. There is also this answer, but many comments disagree that the proposed algorithm is equally weighted. Could someone confirm? If it at all possible topological sorts have a non-zero chance of being selected, that’s sufficient. However, I would like to stay within the NetworkX library if I can.

Any insight is greatly appreciated!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật