c++11 - Populate a tree from vectors with BGL -
i have 2 vectors of objects need make tree structure them. let's assume have vector <obj> parents
, vector <obj> leaves
. therefore, each element of vector <obj> parents
has several leaves sits @ end of tree. doing defining vertex properties
, edges properties
below, , define bidirectional graph
:
struct vertexdata { std::string obj_name; // concatenation of labels std::string obj_class_num; int num; vector <int> segments_list; bool is_leaf=false; }; struct edgedata { std::string edge_name; double confidence; }; typedef boost::adjacency_list<boost::vecs, boost::vecs, boost::bidirectionals, vertexdata, boost::property<boost::edge_weight_t, double, edgedata> > graph; graph graph;
first approach: looping through
vector <obj> leaves
, each member, find parent , make edge. assign properties edge , vertices. next leaf, should check if has parent in tree or should add new vertex parent.second approach: thing tried, looping through
vector <obj> parents
, , each element try make leaves. not sure correct way this. here link: adding custom vertices boost graph try same iterations.
code added 1st approach:
vector <class1> parents; // has objects of type class1 vector <class2> leaves; // has objects of type class2 /// declare graph typedef boost::adjacency_list<boost::vecs, boost::vecs, boost::bidirectionals, vertexdata, boost::property<boost::edge_weight_t, double, edgedata> > graph; /// instantiate graph graph graph; typedef boost::graph_traits<graph>::vertex_descriptor vertex_t; typedef boost::graph_traits<graph>::edge_descriptor edge_t; vector<vertex_t> obj_vertices; vector<string> parents_labels_v; bool parent_exist=false; /// loop through leaves , make edges associated parent (auto leaf: leaves) { int leaf_nr = leaf.number; vertex_t v = boost::add_vertex(graph); // leaf vertex graph[v].num = leaf_nr; // leaf number graph[v].is_leaf = true; /// access parent label leaf number string label1 = parents[leaf_nr].label; /// check if parent exist, using label if(std::find(parents_labels_v.begin(), parents_labels_v.end(), label1) != parents_labels_v.end()){ parent_exist = true; }else{ parents_labels_v.push_back(label1); } if(parent_exist) { // find already_exist parent vertex make edge vertex_t u = ??? here have problem // create edge connecting 2 vertices edge_t e; bool b; boost::tie(e,b) = boost::add_edge(u,v,graph); } else{ // if parent-vertex there not, add graph vertex_t u = boost::add_vertex(graph); // parent vertex graph[u].obj_name = label1; graph[u].segments_list.push_back(leaf_nr); obj_vertices.push_back(u); // create edge connecting 2 vertices edge_t e; bool b; boost::tie(e,b) = boost::add_edge(u,v,graph); } }
Comments
Post a Comment