用于选择连接到一个顶点的所有边和顶点的算法

时间:2011-02-20 10:47:59

标签: c++ boost graph

我正在使用Boost Graph来尝试理解我用Graphviz Dot格式生成的一些依赖图。

不幸的是,我对图理论知之甚少,所以我很难用图理论术语来构建我想知道的东西。

从有150个顶点的有向依赖图中,我想在一个特定的顶点V上“放大”,并构建一个包含V的子图,其所有传入边和它们的传入边,它的所有传出边和它们的传出边缘,有点像通过V的最长路径。

这些依赖图非常混乱,所以我想删除混乱,以便更清楚可能影响相关顶点的内容。

例如,给定;

     g
     |
     v
a -> b -> c -> d
|    |         |
v    v         |
e    f <-------+

如果我在c上运行算法,我想我想要;

     g
     |
     v
a -> b -> c -> d -> f

不确定b - &gt; f也应该包括在内......我认为它是因为所有顶点“之前”c应该包含它们的边缘,并且所有顶点“之后”c应该包含它们的外边缘,但在我看来,会失去一些信息。

感觉应该有一个算法可以做到这一点(或者更明智的东西,不确定我是否想要做一些愚蠢的事情,参见上面的b-&gt; f),但我不知道从哪里开始看着。

谢谢!

2 个答案:

答案 0 :(得分:41)

好的,所以我会根据您的具体问题翻译和调整我的教程。 文档总是假定大量“使用命名空间”;我不会使用任何,所以你知道什么是什么。 让我们开始吧:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/astar_search.hpp>

首先,定义顶点和边缘:

struct Vertex{
    string name; // or whatever, maybe nothing
};
struct Edge{
    // nothing, probably. Or a weight, a distance, a direction, ...
};

创建类型或图表:

typedef boost::adjacency_list<  // adjacency_list is a template depending on :
    boost::listS,               //  The container used for egdes : here, std::list.
    boost::vecS,                //  The container used for vertices: here, std::vector.
    boost::directedS,           //  directed or undirected edges ?.
    Vertex,                     //  The type that describes a Vertex.
    Edge                        //  The type that describes an Edge
> MyGraph;

现在,您可以使用Vertices和Edges的ID类型的快捷方式:

typedef MyGraph::vertex_descriptor VertexID;
typedef MyGraph::edge_descriptor   EdgeID;

实施图表:

MyGraph graph;

阅读Graphviz数据,并提供图表:

for (each Vertex V){
    VertexID vID = boost::add_vertex(graph); // vID is the index of a new Vertex
    graph[vID].name = whatever;
}

请注意,graph[ a VertexID ]给出了顶点,但graph[ an EdgeID ]给出了边缘。以下是添加一个的方法:

EdgeID edge;
bool ok;
boost::tie(edge, ok) = boost::add_edge(u,v, graphe); // boost::add_edge gives a std::pair<EdgeID,bool>. It's complicated to write, so boost::tie does it for us. 
if (ok)  // make sure there wasn't any error (duplicates, maybe)
    graph[edge].member = whatever you know about this edge

现在你有了你的图表。你想获得顶点“c”的VertexID。为了简单起见,我们使用线性搜索:

MyGraph::vertex_iterator vertexIt, vertexEnd;
boost::tie(vertexIt, vertexEnd) = vertices(graph);
for (; vertexIt != vertexEnd; ++vertexIt){
    VertexID vertexID = *vertexIt; // dereference vertexIt, get the ID
    Vertex & vertex = graph[vertexID];
    if (vertex.name == std::string("c")){} // Gotcha
}

最后,要获得顶点的邻居:

MyGraph::adjacency_iterator neighbourIt, neighbourEnd;
boost::tie(neighbourIt, neighbourEnd) = adjacent_vertices( vertexIdOfc, graph );
for(){you got it I guess}

您还可以使用

获得边缘
std::pair<out_edge_iterator, out_edge_iterator> out_edges(vertex_descriptor u, const adjacency_list& g)
std::pair<in_edge_iterator, in_edge_iterator> in_edges(vertex_descriptor v, const adjacency_list& g)
 // don't forget boost::tie !

所以,对于你真正的问题:

  • 找到Vertex“c”
  • 的ID
  • 递归查找in_edges
  • 递归查找out_edges

in_edges的例子(从未编译或尝试过,在我的脑海中):

void findParents(VertexID vID){
    MyGraph::inv_adjacency_iterator parentIt, ParentEnd;
    boost::tie(parentIt, ParentEnd) = inv_adjacent_vertices(vID, graph);
    for(;parentIt != parentEnd); ++parentIt){
        VertexID parentID = *parentIt;
        Vertex & parent = graph[parentID];
        add_edge_to_graphviz(vID, parentID); // or whatever
        findParents(parentID);
    }
}

换句话说,只需将Parent重命名为Children,并使用adjacency_iterator / adjacent_vertices。

希望这有帮助。

答案 1 :(得分:5)

以下是它的结果。我意识到我需要完全按照边缘和边缘工作:

// Graph-related types
typedef property < vertex_name_t, std::string > vertex_p;
typedef adjacency_list < vecS, vecS, bidirectionalS, vertex_p> graph_t;
typedef graph_t::vertex_descriptor vertex_t;
typedef std::set< graph_t::edge_descriptor > edge_set;

// Focussing algorithm
edge_set focus_on_vertex(graph_t& graph, const std::string& focus_vertex_name)
{
    const vertex_t focus_vertex = find_vertex_named(graph, focus_vertex_name);

    edge_set edges;
    collect_in_edges(graph, focus_vertex, edges);
    collect_out_edges(graph, focus_vertex, edges);

    return edges;
}

// Helpers
void collect_in_edges(const graph_t& graph, vertex_t vertex, edge_set& accumulator)
{
    typedef graph_t::in_edge_iterator edge_iterator;

    edge_iterator begin, end;
    boost::tie(begin, end) = in_edges(vertex, graph);
    for (edge_iterator i = begin; i != end; ++i)
    {
        if (accumulator.find(*i) == accumulator.end())
        {
            accumulator.insert(*i);
            collect_in_edges(graph, source(*i, graph), accumulator);
        }
    }
}

void collect_out_edges(const graph_t& graph, vertex_t vertex, edge_set& accumulator)
{
    typedef graph_t::out_edge_iterator edge_iterator;

    edge_iterator begin, end;
    boost::tie(begin, end) = out_edges(vertex, graph);
    for (edge_iterator i = begin; i != end; ++i)
    {
        if (accumulator.find(*i) == accumulator.end())
        {
            accumulator.insert(*i);
            collect_out_edges(graph, target(*i, graph), accumulator);
        }
    }
}

vertex_t find_vertex_named(const graph_t& graph, const std::string& name)
{
    graph_t::vertex_iterator begin, end;
    boost::tie(begin, end) = vertices(graph);
    for (graph_t::vertex_iterator i = begin; i != end; ++i)
    {
        if (get(vertex_name, graph, *i) == name)
            return *i;
    }

    return -1;
}

这也处理有问题的顶点之前或之后的循环。我的源依赖图有周期(颤抖)。

我尝试将collect _ * _ edges一般化为模板化的collect_edges,但是我没有足够的元编程调试能量来花费它。