通过与输入相同的增强图创建无向图

时间:2014-06-01 10:55:30

标签: c++ boost boost-graph

我想创建如下图形,第一列是顶点,其他是邻接顶点

  • 1 2 3 4 7
  • 2 1 3 4
  • 3 1 2 4
  • 4 1 2 3 5
  • 5 4 6 7 8
  • 6 5 7 8
  • 7 1 5 6 8
  • 8 5 6 7

我将边缘添加到图中

using MinCutG = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>;

MinCutG graph;
std::vector<std::vector<int> > input{ {1,2,3,4,7}, {2,1,3,4}, {3,1,2,4},
            {4,1,2,3,5}, {5,4,6,7,8}, {6,5,7,8}, {7,1,5,6,8}, {8,5,6,7}};
for(auto const &data : input){
    auto begin = std::begin(data);
    auto end = std::end(data);
    if(begin != end){
        auto const Vertex = *begin;
        ++begin;
        while(begin != end){
            boost::add_edge(Vertex, *begin, graph);        
            ++begin;
        }
    }
}

打印图表

template<typename Graph>
void print_adjacent_vertex(Graph const &g)
{
    for (auto vertex = vertices(g); vertex.first != vertex.second; ++vertex.first){
        std::cout << *vertex.first << " is connected with ";
        for (auto neighbour = adjacent_vertices(*vertex.first, g);
             neighbour.first != neighbour.second; ++neighbour.first){
            std::cout << *neighbour.first << " ";
        }
        std::cout<<std::endl;
    }
}

我希望输出与输入相同,但事实并非如此 结果是

  • 1与2 3 4 7 2 3 4 7
  • 连接
  • 2与1 1 3 4 3 4
  • 连接
  • 3与1 2 1 2 4 4
  • 连接
  • 4与1 2 3 1 2 3 5 5
  • 连接
  • 5与4 4 6 7 8 6 7 8
  • 连接
  • 6与5 5 7 8 7 8
  • 连接
  • 7与1 5 6 1 5 6 8 8
  • 连接
  • 8与5 6 7 5 6 7
  • 连接

我的预期结果

  • 1与2 3 4 7
  • 连接
  • 2与1 3 4
  • 连接
  • 3与1 2 4
  • 连接
  • 4与1 2 3 5
  • 连接
  • 5与4 6 7 8
  • 连接
  • 6与6 5 7 8
  • 连接
  • 7与7 1 5 6 8
  • 连接
  • 8与8 5 6 7
  • 连接

2 个答案:

答案 0 :(得分:3)

简而言之,对setS模板参数使用OutEdgeList将禁用平行边缘,从而产生所需的输出。

boost::adjacency_list的第一个模板参数是OutEdgeList,它控制某些图形行为,例如允许或禁止平行边缘。对于无向MinCutG图形,vecS用作OutEdgeList,它将启用平行边。例如,如果无向图支持平行边,则使用:

add_edge(1, 2, graph); // Vertex 1 and 2 are adjacent to one another via edge A.
add_edge(2, 1, graph); // Vertex 1 and 2 are adjacent to one another via edge B,
                       // which is parallel to edge A.

顶点1的{​​{3}}将包含顶点2两次,每个边缘一次(AB)。

adjacent_vertices()所述,可以使用setS的{​​{1}}或hash_setS选择器禁用并行边。例如,更改:

OutEdgeList

为:

using MinCutG = boost::adjacency_list<
  boost::vecS, // OutEdgeList with parallel edges
  boost::vecS,
  boost::undirectedS>;

以下是documentation使用原始代码,仅将using MinCutG = boost::adjacency_list< boost::setS, // OutEdgeList with no parallel edges boost::vecS, boost::undirectedS>; OutEdgeList更改为vecS

setS

产生以下输出:

#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>

template<typename Graph>
void print_adjacent_vertex(Graph const &g)
{
    for (auto vertex = vertices(g); vertex.first != vertex.second; 
         ++vertex.first){
        std::cout << *vertex.first << " is connected with ";
        for (auto neighbour = adjacent_vertices(*vertex.first, g);
             neighbour.first != neighbour.second; ++neighbour.first){
            std::cout << *neighbour.first << " ";
        }
        std::cout<<std::endl;
    }
}

int main()
{
    using MinCutG = boost::adjacency_list<
        boost::setS, boost::vecS, boost::undirectedS>;

    MinCutG graph;
    std::vector<std::vector<int> > input
    {
        {1,2,3,4,7},
        {2,1,3,4},
        {3,1,2,4},
        {4,1,2,3,5},
        {5,4,6,7,8},
        {6,5,7,8},
        {7,1,5,6,8},
        {8,5,6,7}
    };

    for(auto const &data : input){
        auto begin = std::begin(data);
        auto end = std::end(data);
        if(begin != end){
            auto const Vertex = *begin;
            ++begin;
            while(begin != end){
                boost::add_edge(Vertex, *begin, graph);        
                ++begin;
            }
        }
    }
    print_adjacent_vertex(graph);
}

答案 1 :(得分:0)

正如Tanner所解释的那样,你基本上将每个边加两次,首先是(a,b),后来是(b,a)。避免这种情况的一种但相当昂贵的方法是使用setS作为控制边缘容器的模板参数。这就是坦纳在回答中提出的建议。

替代方法是在添加边缘之前检查边缘是否存在。这意味着行

boost::add_edge(Vertex, *begin, graph);   

应替换为

if (! edge(Vertex, *begin, graph).second ) //edge does not exist yet
    boost::add_edge(Vertex, *begin, graph); 

在大多数情况下,此代码可能更快并且消耗更少的内存。