简体   繁体   English

使用BGL(Boost图形库)查找DAG中的所有拓扑排序

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

How can use the Boost Graph Library to find all the topological sorts of a given DAG G ? 如何使用Boost Graph Library查找给定DAG G所有拓扑类型?

I have found some theoretical references explaining that is possible, there's also this code where there's an implementation. 我找到了一些理论上的解释来解释这是可能的,还有一些实现的代码 But I don't actually want to start from scratch, and I want to use BGL to compute them. 但是我实际上并不想从头开始,我想使用BGL来计算它们。

One strategy would be to get all vertices without predecessors (no directed edges to it). 一种策略是获取所有没有前任顶点的顶点(无顶点)。 And multiplying vectors based on vertices without predecessors. 并在没有前任的情况下基于顶点相乘向量。 If you have a C++ to do it please share. 如果您有C ++,请分享。

The code to get depth_first topological sort: 获得depth_first拓扑排序的代码:

Given a DAG graph with vertex type vertex_t : 给定一个顶点类型为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;
} 

Without custom vertex: 没有自定义顶点:

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