簡體   English   中英

使用BGL(Boost圖形庫)查找DAG中的所有拓撲排序

[英]Find all topological sorts in a DAG using BGL (Boost graph library)

如何使用Boost Graph Library查找給定DAG G所有拓撲類型?

我找到了一些理論上的解釋來解釋這是可能的,還有一些實現的代碼 但是我實際上並不想從頭開始,我想使用BGL來計算它們。

一種策略是獲取所有沒有前任頂點的頂點(無頂點)。 並在沒有前任的情況下基於頂點相乘向量。 如果您有C ++,請分享。

獲得depth_first拓撲排序的代碼:

給定一個頂點類型為vertex_t的DAG graph

deque<vertex_t> topologicalSorted;

//perform topological sort
if (topologicalSort(graph, topologicalSorted)) {
    cout << "The graph is directed acyclic!" << endl;
    cout << "Topological order: ";
    for (int i = 0; i < topologicalSorted.size(); i++) {
        cout << graph[topologicalSorted.at(i)].label << " ";
    }
    cout << endl;
} 


bool topologicalSort()
{
    try
    {
        boost::topological_sort(graph, front_inserter(topologicalSorted));
    }
    catch (boost::not_a_dag)
    {
        cout << "not a dag\n";
        return false;
    }
    cout << "top sort OK\n";

    return true;
} 

沒有自定義頂點:

deque<int> topologicalSorted;

if (topologicalSort()) {
        // Print the results.
        for (int i = 0; i < topologicalSorted.size(); i++) {
            cout << topologicalSorted.at(i) << ",";
        }
        cout << endl;
    } 

暫無
暫無

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

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