int main(){
    Graph G = udgraph_toy_ex();
    // biconnected component is a partition of *edges*, so we need a property_map (for edges) to store the component id for each edge
    WeightMap wm = get(edge_weight, G); 
    /* we just use the WeightMap (which is just a property_map for edges) to store the components
    * else we can use the edge_component_t as above... ( need to redefine the `Graph` type, see: http://www.boost.org/doc/libs/1_59_0/libs/graph/example/biconnected_components.cpp )
    */
    int ncomp = biconnected_components(G, wm); 
    cout << ncomp << " biconnected components" << endl;
    EdgeIt ei, ei_end;
    for (tie(ei, ei_end) = edges(G); ei != ei_end; ++ei)
    cout << source(*ei, G) << "-" << target(*ei, G)
              << ", belongs to biconnected-component-" << wm[*ei] << endl;
    cout << endl;
    
    // get articulation points
    vector<Vertex> art_pts;
    articulation_points(G, back_inserter(art_pts));
    cout << art_pts.size() << " articulation points, they are: " << endl;
    for(vector<Vertex>::iterator it=art_pts.begin(); it!=art_pts.end(); it++)
        cout << *it << ", ";
    cout << endl;
}
Esempio n. 2
0
static void detectArticulationPoints(std::vector<RelationEdge>& vecRelationEdge, Graph& g)
{
  for (size_t i = 0, iEnd = vecRelationEdge.size(); i < iEnd; ++ i) {
    RelationEdge& relationEdge = vecRelationEdge[i];

    GraphVertex u = relationEdge.getSource().getIdx();
    GraphVertex v = relationEdge.getTarget().getIdx();

    add_edge(u, v, EdgeProp(relationEdge.getType(), relationEdge.getScore()), g);
  }

  removeIsolatedEdges(g);

  std::vector<GraphVertex> vecArticulationPoint;
  articulation_points(g, std::back_inserter(vecArticulationPoint));
  while (!vecArticulationPoint.empty()) {
    GraphVertex nWeakestPoint = 0;
    double nMinWeight = std::numeric_limits<double>::max();
    for (GraphVertex i = 0; i < vecArticulationPoint.size(); ++ i) {
      double nWeight = 0;
      std::pair<GraphOutEdgeItr, GraphOutEdgeItr> edgeItr = out_edges(vecArticulationPoint[i], g);
      for (GraphOutEdgeItr it = edgeItr.first; it != edgeItr.second; it ++) {
        nWeight += g[*it].score;
      }
      if (nWeight < nMinWeight) {
        nMinWeight = nWeight;
        nWeakestPoint = vecArticulationPoint[i];
      }
    }
    std::map<GraphVertex, EdgeProp> mapVertex2Edge;
    std::pair<GraphOutEdgeItr, GraphOutEdgeItr> edgeItr = out_edges(nWeakestPoint, g);
    for (GraphOutEdgeItr it = edgeItr.first; it != edgeItr.second; it ++) {
      mapVertex2Edge[target(*it, g)] = g[*it];
    }
    clear_vertex(nWeakestPoint, g);
    std::vector<GraphVertex> component(num_vertices(g));
    size_t nComponentNum = connected_components(g, &component[0]);
    std::vector<double> vecWeight(nComponentNum, 0.0);
    std::vector<int> vecCount(nComponentNum, 0);
    for (std::map<GraphVertex, EdgeProp>::iterator it = mapVertex2Edge.begin(); it != mapVertex2Edge.end(); it ++) {
      vecWeight[component[it->first]] += it->second.score;
      vecCount[component[it->first]] ++;
    }
    for (size_t i = 0; i < nComponentNum; ++ i) {
      if (vecCount[i] != 0) {
        vecWeight[i] /= vecCount[i];
      }
    }
    size_t nStrongestComponent = std::distance(vecWeight.begin(), std::max_element(vecWeight.begin(), vecWeight.end()));
    for (std::map<GraphVertex, EdgeProp>::iterator it = mapVertex2Edge.begin(); it != mapVertex2Edge.end(); it ++) {
      GraphVertex v = it->first;
      if (component[v] == nStrongestComponent) {
        add_edge(nWeakestPoint, v, mapVertex2Edge[v], g);
      }
    }

    removeIsolatedEdges(g);

    vecArticulationPoint.clear();
    articulation_points(g, std::back_inserter(vecArticulationPoint));
  }

  return;
}