簡體   English   中英

將有向無環圖轉換為序列的算法

[英]Algorithm to convert directed acyclic graphs to sequences

如果D依賴於B和C,而B和C都依賴於A,則我想要ABCD(或ACBD)作為結果; 即從圖生成平面序列,以使所有節點都出現在其任何后代之前。 例如,在安裝X之前,我們可能需要為X安裝依賴項。

有什么好的算法?

在這樣的問題中,術語對於找到正確的鏈接至關重要。

您描述的依賴項形成了部分排序的集合 (姿勢)。 簡而言之,這是一個帶有訂單運算符的集合,對於這些集合,某些對的比較可能不確定。 在您的示例中BC是不可比的( B都不依賴C ,或者C都不依賴B )。

訂單運算符的擴展是一種尊重原始部分訂單並為以前不可比擬的元素添加一些額外比較的擴展。 在極端情況下: 線性擴展會導致整體排序。 對於每個部分排序,都存在這種擴展

從位姿獲得線性擴展的算法稱為拓撲排序 Wikipedia提供以下非常簡單的算法

L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
    remove a node n from S
    add n to tail of L
    for each node m with an edge e from n to m do
        remove edge e from the graph
        if m has no other incoming edges then
            insert m into S
if graph has edges then
    return error (graph has at least one cycle)
else 
    return L (a topologically sorted order)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM