微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

提升迭代地滤除图形的边缘

如何解决提升迭代地滤除图形的边缘

我有一个连通的图A(级别0),并且我想通过反复在A中一次删除一个边来用图B,C,D ...填充下一级别。这是我的代码开始的方式: / p>

struct VertexData
{
    long ID;
};

typedef boost::adjacency_list<boost::listS,boost::listS,boost::undirectedS,VertexData,property< edge_index_t,int > >> simplegraph; 
typedef boost::graph_traits<simplegraph>::vertex_descriptor vertex_t;
typedef boost::graph_traits<simplegraph>::edge_descriptor edge_t;

int main(){
    
    simplegraph A;   
}

我尝试遍历A的边缘并在复制图形A之后一次移除边缘:

        
simplegraph tempStructure;
vector<simplegraph> newStructures;
vector<long> parentList;

// loop over the edges
auto es = boost::edges(A);
pred = A.ID;

for (auto eit = es.first; eit != es.second; ++eit){
            
      // 1. copy structure
      cout << "copy structure." <<endl;
      boost::copy_graph(A,tempStructure);
            
      // 2. remove a particular edge 
      cout << "Remove edge." << endl;
      boost::remove_edge(*eit,tempStructure);
            
      // 3. save structure
      cout << "Saving structure." << endl;
      newStructures.push_back(tempStructure);
            
      // 4. save parent of old structure]
      cout << "Saving parent." << endl;
      parentList.push_back(pred);

}

但是,我读过here,后,我知道这是非常幼稚的尝试。我找到了以下解决方案(Boost filtered graph with blacklisted edges),并且看到过滤后的图形似乎是行之有效的方法。但是,我在执行它时遇到了麻烦...

是否可以一次在父图中过滤掉一条边,以使子图是A中没有一条边的过滤后的A版本?

解决方法

由于对这个问题(here)的讨论,我得以提出解决方案。我采用了与线程中相同的方法,并决定将每个边缘“黑名单”:

typedef std::pair <int,int> Edge;
typedef boost::adjacency_list<boost::vecS,boost::vecS,boost::undirectedS,boost::no_property,boost::no_property> SimpleGraph;

struct BlackListEdgeConstraint{
    
private:
    Edge blackList;
    SimpleGraph* g;
    
public:
    BlackListEdgeConstraint() : blackList(),g(NULL) {}; // constructor with initialization
    BlackListEdgeConstraint(Edge& list,SimpleGraph* gM) : blackList(list),g(gM){}
    
    // the actual predicate function
    bool operator()(const boost::graph_traits<SimpleGraph>::edge_descriptor& e) const {
        
        Edge edge(source(e,*g),target(e,*g));
        
        if (edge == blackList){
            return false;
        }
        else{
            return true;
        }
    }
};

然后,我使用此谓词如下:

auto es = boost::edges(parentStructure);
        
for (auto eit = es.first; eit != es.second; ++eit){
                        
    // we filter the current edge -- blacklist it
    Edge edge(source(*eit,parentStructure),target(*eit,parentStructure));
    BlackListEdgeConstraint filter(edge,&parentStructure);
            
    // get filtered graph where this connection is missing
    boost::filtered_graph<SimpleGraph,BlackListEdgeConstraint> tempStructureFILT(parentStructure,filter);
}

我确信这可以用一种更优雅-更少耗时的方式来完成,但是目前,此解决方案符合我的目的。我很高兴听到任何有关如何改进它的反馈意见:)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?