inline typename property_traits<Components>::value_type connected_components(Graph& G, DFSVisitor v, Components c, undirected_tag) { return connected_components(G, v, c, get(vertex_color, G), undirected_tag()); }
void is_analysis_fun::run() { obtain_mask(); connected_components(); stat_generate(); visualize_image(); create_proofread_panel(); }
main() { graph g; read_graph(&g,FALSE); print_graph(&g); connected_components(&g); }
int numberOfConnectedComponents(Graph *G) { typedef std::map<graph_traits<MyGraphType>::vertex_descriptor, graph_traits<MyGraphType>::vertices_size_type> component_type; component_type component; boost::associative_property_map< component_type > component_map(component); int num_components = connected_components(*G, component_map); return num_components; }
inline typename property_traits<Components>::value_type connected_components(Graph& G, DFSVisitor v, Components c, Color color, directed_tag) { typedef typename graph_traits<Graph>::vertex_descriptor Vertex; return connected_components(G, v, c, get(vertex_discover_time, G), get(vertex_finish_time, G), color, directed_tag()); }
//process the templates into images with same color features of processed very original image with real objects. void normalize_template(IplImage** templates) { IplImage* selected_image = NULL; IplImage* temp_image = NULL; IplImage* red_point_image = NULL; IplImage* connected_reds_image = NULL; IplImage* connected_background_image = NULL; IplImage* result_image = NULL; CvSeq* red_components = NULL; CvSeq* background_components = NULL; for(int i = 0; i < TEMPLATES_NUM; i++) { if (red_point_image != NULL) { cvReleaseImage( &red_point_image ); cvReleaseImage( &temp_image ); cvReleaseImage( &connected_reds_image ); cvReleaseImage( &connected_background_image ); cvReleaseImage( &result_image ); } selected_image = templates[i]; red_point_image = cvCloneImage( selected_image ); result_image = cvCloneImage( selected_image ); temp_image = cvCloneImage( selected_image ); connected_reds_image = cvCloneImage( selected_image ); connected_background_image = cvCloneImage( selected_image ); //the same algorithm as process the image with real objects is used. find_red_points( selected_image, red_point_image, temp_image ); red_components = connected_components( red_point_image, connected_reds_image ); invert_image( red_point_image, temp_image ); background_components = connected_components( temp_image, connected_background_image ); determine_optimal_sign_classification( selected_image, red_point_image, red_components, background_components, result_image ); cvCopy(result_image, templates[i]); } }
void cut_and_report(MyFloat dW, Graph& g, string fname, bool full_verbose=false) { if(full_verbose) scoped_timer timemme("Get Components:....."); std::vector<int> component(num_vertices(g)); int num = connected_components(g, &component[0]); std::vector<int>::size_type i; cout << "Total number of components: " << num <<" Wcut="<< dW<< endl; if(full_verbose) { for (i = 0; i != component.size(); ++i) cout << "Vertex " << i <<" is in component " << component[i] << endl; } cout << endl; }
void CSimpleUGraph<ObjT, Compare>::GetConnectedComponents( vector< vector< ObjT > > &vObjTComponents ) { // This is not pretty, but it works vector< VertexIndexT > vComponents( num_vertices( oBoostGraph ) ); Int nNumComponents = connected_components( oBoostGraph, &vComponents[0] ); vObjTComponents.clear(); vObjTComponents.resize( nNumComponents ); for ( Size_Type nVertexIndex = 0; nVertexIndex < vComponents.size(); nVertexIndex ++ ) { VertexIndexT nComponentIndex; nComponentIndex = vComponents[ nVertexIndex ]; vObjTComponents[ nComponentIndex ].push_back( oVertexToDataMap[ nVertexIndex ] ); } }
void make_connected(Graph& g, VertexIndexMap vm, AddEdgeVisitor& vis) { typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator_t; typedef typename graph_traits<Graph>::vertex_descriptor vertex_t; typedef typename graph_traits<Graph>::vertices_size_type v_size_t; typedef iterator_property_map< typename std::vector<v_size_t>::iterator, VertexIndexMap > vertex_to_v_size_map_t; std::vector<v_size_t> component_vector(num_vertices(g)); vertex_to_v_size_map_t component(component_vector.begin(), vm); std::vector<vertex_t> vertices_by_component(num_vertices(g)); v_size_t num_components = connected_components(g, component); if (num_components < 2) return; vertex_iterator_t vi, vi_end; tie(vi,vi_end) = vertices(g); std::copy(vi, vi_end, vertices_by_component.begin()); bucket_sort(vertices_by_component.begin(), vertices_by_component.end(), component, num_components ); typedef typename std::vector<vertex_t>::iterator vec_of_vertices_itr_t; vec_of_vertices_itr_t ci_end = vertices_by_component.end(); vec_of_vertices_itr_t ci_prev = vertices_by_component.begin(); if (ci_prev == ci_end) return; for(vec_of_vertices_itr_t ci = boost::next(ci_prev); ci != ci_end; ci_prev = ci, ++ci ) { if (component[*ci_prev] != component[*ci]) vis.visit_vertex_pair(*ci_prev, *ci, g); } }
int myGraph::findConectedComponnets() { typedef boost::adjacency_list <boost::vecS, boost::vecS, boost::undirectedS> Graph; Graph G; add_edge(0, 1, G); add_edge(1, 4, G); add_edge(4, 0, G); add_edge(2, 5, G); std::vector<int> component(num_vertices(G)); int num = connected_components(G, &component[0]); std::vector<int>::size_type i; cout << "Total number of components: " << num << endl; for (i = 0; i != component.size(); ++i) cout << "Vertex " << i <<" is in component " << component[i] << endl; cout << endl; return num; }
int main(int argc, char const *argv[]) { // The following undirected graph has 3 connected components // 0 2 6 7 // |\ / | // | 4 | // |/ | // 1---3 5 int graph[][GRAPH_SIZE] = { {0,1,0,0,1,0,0,0}, {1,0,0,1,1,0,0,0}, {0,0,0,0,1,0,0,0}, {0,1,0,0,0,0,0,0}, {1,1,1,0,0,0,0,0}, {0,0,0,0,0,0,1,0}, {0,0,0,0,0,1,0,0}, {0,0,0,0,0,0,0,0} }; std::cout << "The graph has " << connected_components(graph) << " connected components" << std::endl; return 0; }
int main(int argc, char *argv[]) { FILE *f; int i; configure_logmsg(MSG_DEBUG1); parse_arguments(argc, argv); n_seq = 0; if (database_name) load_seqnames(database_name); load_scores(stdin); CA(chimeric, n_seq, sizeof(int)); if (chimera_file) load_chimeras(chimera_file); connected_components(); f = fopen("articulations.txt","w"); for(i=0;i<n_seq;i++) { if (arti_points[i]) { fprintf(f,"%d\n",i); } } fclose(f); return 0; }
void SemiSupervisedKernel::connectedComponent( std::vector<std::vector<int> >* pComponentArray) { std::vector<int> component(num_vertices(mMustLinkGraph)); int num = connected_components(mMustLinkGraph, &component[0]); pComponentArray->resize(num); std::vector<int> _dummy; pComponentArray->assign(num, _dummy); std::vector<std::vector<int> > _components(num, _dummy); VertexIter vi, vi_end; for (tie(vi, vi_end) = vertices(mMustLinkGraph); vi != vi_end; vi++) { int ccIndex = component[*vi]; _components[ccIndex].push_back((int) (*vi)); } std::vector<RankItem<int, int> > rankList; for (size_t i = 0; i < _components.size(); ++i) { RankItem<int, int> item((int) i, -1 * (int) _components[i].size()); rankList.push_back(item); } std::sort(rankList.begin(), rankList.end()); for (size_t i = 0; i < _components.size(); ++i) { int index = rankList[i].index; pComponentArray->at(i) = _components[index]; } }
void reduceTransitEdges(const std::vector<Primitive*>& vecPrimitive, std::vector<RelationEdge>& vecRelationEdge, RelationEdge::RelationEdgeType relationType, Graph& g) { std::sort(vecRelationEdge.begin(), vecRelationEdge.end()); std::map<GraphVertex, RelationVertex> mapVertex; for (size_t i = 0, iEnd = vecRelationEdge.size(); i < iEnd; ++ i) { GraphVertex u = vecRelationEdge[i].getTarget().getIdx(); GraphVertex v = vecRelationEdge[i].getSource().getIdx(); mapVertex[u] = vecRelationEdge[i].getTarget(); mapVertex[v] = vecRelationEdge[i].getSource(); add_edge(u, v, EdgeProp(relationType), g); } std::vector<GraphVertex> component(num_vertices(g)); size_t numComponent = connected_components(g, &component[0]); std::map<size_t, std::vector<size_t> > mapComponent; for (size_t i = 0, iEnd = component.size(); i < iEnd; ++ i) { mapComponent[component[i]].push_back(i); } vecRelationEdge.clear(); for (std::map<size_t, std::vector<size_t> >::const_iterator it = mapComponent.begin(); it != mapComponent.end(); ++ it) { const std::vector<size_t>& vecComponent = it->second; if (vecComponent.size() < 2) { continue; } GraphVertex u = vecComponent[0]; for (size_t i = 1, iEnd = vecComponent.size(); i < iEnd; ++ i) { GraphVertex v = vecComponent[i]; RelationEdge relationEdge(relationType, mapVertex[v], mapVertex[u], 1.0); GlobFit::computeEdgeScore(relationEdge, vecPrimitive); vecRelationEdge.push_back(relationEdge); } } std::sort(vecRelationEdge.begin(), vecRelationEdge.end()); if (vecRelationEdge.size() == 0) { return; } if (vecRelationEdge[0].getType() != RelationEdge::RET_EQUAL_ANGLE && vecRelationEdge[0].getType() != RelationEdge::RET_EQUAL_LENGTH) { return; } g.clear(); for (size_t i = 0, iEnd = vecPrimitive.size(); i < iEnd; ++ i) { RelationVertex relationVertex(i, vecPrimitive[i]->getIdx()); relationVertex.setParent(i); add_vertex(relationVertex, g); } std::vector<RelationEdge> vecRemainingEdge; std::vector<GraphVertex> vecPredecessor(vecPrimitive.size()); for (size_t i = 0, iEnd = vecRelationEdge.size(); i < iEnd; ++ i) { const RelationVertex& source = vecRelationEdge[i].getSource(); const RelationVertex& target = vecRelationEdge[i].getTarget(); bool sourceProduceLoop = willProduceLoop(vecPredecessor, source.getPrimitiveIdx1(), source.getPrimitiveIdx2(), g); bool targetProduceLoop = willProduceLoop(vecPredecessor, target.getPrimitiveIdx1(), target.getPrimitiveIdx2(), g); // this is too strong for avoiding conflicts, some compatible cases may be removed // TODO: deduce the right rules for edges with 4 primitives involved if (sourceProduceLoop || targetProduceLoop) { continue; } add_edge(source.getPrimitiveIdx1(), source.getPrimitiveIdx2(), g); add_edge(target.getPrimitiveIdx1(), target.getPrimitiveIdx2(), g); vecRemainingEdge.push_back(vecRelationEdge[i]); } vecRelationEdge = vecRemainingEdge; return; }
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; }
inline typename property_traits<Components>::value_type connected_components(const Graph& g, Components c, ColorMap color) { return connected_components(g, c, color, dfs_visitor<>()); }
int main( int argc, char** argv ) { int selected_image_num = 1; char show_ch = 's'; IplImage* images[NUM_IMAGES]; IplImage* selected_image = NULL; IplImage* temp_image = NULL; IplImage* red_point_image = NULL; IplImage* connected_reds_image = NULL; IplImage* connected_background_image = NULL; IplImage* result_image = NULL; CvSeq* red_components = NULL; CvSeq* background_components = NULL; // Load all the images. for (int file_num=1; (file_num <= NUM_IMAGES); file_num++) { if( (images[0] = cvLoadImage("./RealRoadSigns.jpg",-1)) == 0 ) return 0; if( (images[1] = cvLoadImage("./RealRoadSigns2.jpg",-1)) == 0 ) return 0; if( (images[2] = cvLoadImage("./ExampleRoadSigns.jpg",-1)) == 0 ) return 0; if( (images[3] = cvLoadImage("./Parking.jpg",-1)) == 0 ) return 0; if( (images[4] = cvLoadImage("./NoParking.jpg",-1)) == 0 ) return 0; } //load the template images and do normalization. IplImage* templates[TEMPLATES_NUM]; load_templates(templates); normalize_template(templates); // Explain the User Interface printf( "Hot keys: \n" "\tESC - quit the program\n" "\t1 - Real Road Signs (image 1)\n" "\t2 - Real Road Signs (image 2)\n" "\t3 - Synthetic Road Signs\n" "\t4 - Synthetic Parking Road Sign\n" "\t5 - Synthetic No Parking Road Sign\n" "\tr - Show red points\n" "\tc - Show connected red points\n" "\th - Show connected holes (non-red points)\n" "\ts - Show optimal signs\n" ); // Create display windows for images //cvNamedWindow( "Original", 1 ); cvNamedWindow( "Processed Image", 1 ); // Setup mouse callback on the original image so that the user can see image values as they move the // cursor over the image. cvSetMouseCallback( "Original", on_mouse_show_values, 0 ); window_name_for_on_mouse_show_values="Original"; image_for_on_mouse_show_values=selected_image; int user_clicked_key = 0; do { // Create images to do the processing in. if (red_point_image != NULL) { cvReleaseImage( &red_point_image ); cvReleaseImage( &temp_image ); cvReleaseImage( &connected_reds_image ); cvReleaseImage( &connected_background_image ); cvReleaseImage( &result_image ); } selected_image = images[selected_image_num-1]; red_point_image = cvCloneImage( selected_image ); result_image = cvCloneImage( selected_image ); temp_image = cvCloneImage( selected_image ); connected_reds_image = cvCloneImage( selected_image ); connected_background_image = cvCloneImage( selected_image ); // Process image image_for_on_mouse_show_values = selected_image; find_red_points( selected_image, red_point_image, temp_image ); red_components = connected_components( red_point_image, connected_reds_image ); invert_image( red_point_image, temp_image ); background_components = connected_components( temp_image, connected_background_image ); determine_optimal_sign_classification( selected_image, red_point_image, red_components, background_components, result_image ); //recognize the result_image(with white/black/red only) with the processed templates. recognize(selected_image,result_image,templates); // Show the original & result //cvShowImage( "Original", selected_image ); do { if ((user_clicked_key == 'r') || (user_clicked_key == 'c') || (user_clicked_key == 'h') || (user_clicked_key == 's')) show_ch = user_clicked_key; switch (show_ch) { case 'c': cvShowImage( "Processed Image", connected_reds_image ); break; case 'h': cvShowImage( "Processed Image", connected_background_image ); break; case 'r': cvShowImage( "Processed Image", red_point_image ); break; case 's': default: cvShowImage( "Processed Image", result_image ); break; } user_clicked_key = cvWaitKey(0); } while ((!((user_clicked_key >= '1') && (user_clicked_key <= '0'+NUM_IMAGES))) && ( user_clicked_key != ESC )); if ((user_clicked_key >= '1') && (user_clicked_key <= '0'+NUM_IMAGES)) { selected_image_num = user_clicked_key-'0'; } } while ( user_clicked_key != ESC ); return 1; }
//extract the sub image from the whole image. //The first node of a linkedlist with images and coordinates in the original image is returned. sub_image* extract_sub_image(IplImage* original_image, int min_width, int min_height) { int left = 0; int top = 0; int right = 0; int buttom = 0; int temp_x = 0; int temp_y = 0; int contour_flag = 0; int flag = 0; sub_image* temp_result = (sub_image*)malloc(sizeof(sub_image));; sub_image* result = NULL; sub_image* prev = NULL; IplImage* temp_original_image = cvCloneImage(original_image); IplImage* temp = cvCloneImage(original_image); //search the connected components(here search the connected area) CvSeq* contours = connected_components(temp_original_image, temp); for (CvSeq* c=contours ;c!=NULL; c=c->h_next) { for (int i = 0; i < c->total; i ++) { CvPoint* p = (CvPoint*)cvGetSeqElem(c, i); temp_x = p->x; temp_y = p->y; //printf("x = %d, y = %d\n",temp_x,temp_y); //do the approximation if(i == 0) { left = temp_x; right = temp_x; top = temp_y; buttom = temp_y; } else { if(temp_x <= left) left = temp_x; if(temp_x >= right) right = temp_x; if(temp_y <= top) top = temp_y; if(temp_y >= buttom) buttom = temp_y; } } //Not a correct area.To define the value of min_width and min_height I estimate the smallest scale of sub image. if(right-left < min_width || buttom - top < min_height) { continue; } else { //printf("find subimage %d\n",flag++); cvSetImageROI(temp_original_image, cvRect(left,top,right - left,buttom - top)); if(contour_flag++ == 0) { result = temp_result; } temp_result->image = cvCreateImage(cvSize(right - left, buttom - top),temp_original_image->depth,temp_original_image-> nChannels); temp_result->image_left = left; temp_result->image_top = top; temp_result->image_right = right; temp_result->image_buttom = buttom; //copy the region of interesting(the sub image) to the destination image. cvCopy(temp_original_image, temp_result->image, NULL); //cvShowImage(string[flag++],temp_result->image); //organize the linkedlist. temp_result->next_image = (sub_image*)malloc(sizeof(sub_image)); prev = temp_result; temp_result = temp_result->next_image; cvResetImageROI(temp_original_image); } } temp_result = prev; temp_result->next_image= NULL; //the head node of the linkedlist is returned. return result; }
void map_integration::cnstrct_lg_clstrs(){ // const individual_mapping_ppl* crt_map_ptr; int total_number_of_lgs = 0 ; for (int ii = 0; ii < number_of_maps; ii++) { total_number_of_lgs = total_number_of_lgs + p_to_array_maps[ii].get_number_of_lgs(); } /*define a temporary vertex type*/ struct graph_node_str { const linkage_group_bin* p_to_lg ; int ppl_id ; int lg_id ; }; graph_node_str vertices[total_number_of_lgs]; int counter = 0; for (int ii = 0 ; ii < number_of_maps ; ii++) { for (int jj = 0 ; jj < p_to_array_maps[ii].get_number_of_lgs(); jj++) { vertices[counter].p_to_lg = &((p_to_array_maps[ii].get_lgs())[jj]); vertices[counter].ppl_id = ii; vertices[counter].lg_id = jj; counter = counter + 1; } } /*0. construct a boost graph object*/ typedef adjacency_list <vecS, vecS, undirectedS> Graph; Graph lgs_graph(total_number_of_lgs); for (int ii = 0 ; ii < total_number_of_lgs; ii++) { for (int jj = ii+1 ; jj < total_number_of_lgs; jj++ ) { bool overlapping = lgs_intersect(*(vertices[ii].p_to_lg), *(vertices[jj].p_to_lg)); if (overlapping) { add_edge(ii,jj,lgs_graph); } } } /*1. Run the connected components algorithm to partition the lgs into clusters*/ vector<int> cc_ids(total_number_of_lgs, -1); int num = connected_components(lgs_graph, &cc_ids[0]); /*2. Now construct the lg_clusters object*/ vector<vector<linkage_group_bin*> > lg_groups(num); for (int ii = 0 ; ii < total_number_of_lgs; ii++) { if (cc_ids[ii] >= num) { cout << "ERROR!, the component id is invalid" << endl; assert(cc_ids[ii] >= num); // to crash the program if fail the assert } lg_groups[cc_ids[ii]].push_back(new linkage_group_bin(*(vertices[ii].p_to_lg))); } vector<LG_CLUSTER*> lgs(num); for (int i = 0; i < num; i++) { lgs[i] = new LG_CLUSTER(); lgs[i]->Initialize(lg_groups[i]); } lg_clusters.Initialize(lgs); // no need to delete those newed objects, since they are taken care of by the lg_clusters object };
Partitioner::RegionStats * Partitioner::region_statistics(const ExtentMap &addresses) { RegionStats *stats = new_region_stats(); assert(stats!=NULL); size_t nbytes = addresses.size(); if (0==nbytes) return stats; stats->add_sample(RegionStats::RA_NBYTES, nbytes); ExtentMap not_addresses = addresses.invert<ExtentMap>(); Disassembler::AddressSet worklist; // addresses waiting to be disassembled recursively InstructionMap insns_found; // all the instructions we found herein ExtentMap insns_extent; // memory used by the instructions we've found ExtentMap pending = addresses; // addresses we haven't looked at yet /* Undirected local control flow graph used to count connected components */ typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> CFG; typedef boost::graph_traits<CFG>::vertex_descriptor CFGVertex; typedef std::map<rose_addr_t, CFGVertex> Addr2Vertex; CFG cfg; Addr2Vertex va2id; /* Statistics */ size_t nstarts=0; // number of times the recursive disassembler was started size_t nfails=0; // number of disassembler failures size_t noverlaps=0; // instructions overlapping with a previously found instruction size_t nincomplete=0; // number of instructions with unknown successors size_t nfallthrough=0; // number of branches to fall-through address within our "addresses" size_t ncalls=0; // number of function calls outside our "addresses" size_t nnoncalls=0; // number of branches to non-functions outside our "addresses" size_t ninternal=0; // number of non-fallthrough internal branches while (!pending.empty()) { rose_addr_t start_va = pending.min(); worklist.insert(start_va); ++nstarts; while (!worklist.empty()) { rose_addr_t va = *worklist.begin(); worklist.erase(worklist.begin()); /* Obtain (disassemble) the instruction and make sure it falls entirely within the "addresses" */ Instruction *insn = find_instruction(va); if (!insn) { ++nfails; pending.erase(Extent(va)); continue; } Extent ie(va, insn->get_size()); if (not_addresses.overlaps(ie)) { ++nfails; pending.erase(Extent(va)); continue; } /* The disassembler can also return an "unknown" instruction when failing, depending on how it is invoked. */ if (insn->node->is_unknown()) { ++nfails; pending.erase(Extent(va, insn->get_size())); continue; } insns_found.insert(std::make_pair(va, insn)); rose_addr_t fall_through_va = va + insn->get_size(); /* Does this instruction overlap with any we've already found? */ if (insns_extent.overlaps(ie)) ++noverlaps; pending.erase(Extent(va, insn->get_size())); insns_extent.insert(ie); /* Find instruction successors by looking only at the instruction itself. This is simpler, but less rigorous * method than finding successors a basic block at a time. For instance, we'll find both sides of a branch * instruction even if the more rigorous method determined that one side or the other is always taken. But this is * probably what we want here anyway for determining whether something looks like code. */ bool complete; Disassembler::AddressSet succs = insn->get_successors(&complete); if (!complete) ++nincomplete; /* Add instruction as vertex to CFG */ std::pair<Addr2Vertex::iterator, bool> inserted = va2id.insert(std::make_pair(va, va2id.size())); if (inserted.second) { CFGVertex vertex __attribute__((unused)) = add_vertex(cfg); assert(vertex==inserted.first->second); } /* Classify the various successors. */ for (Disassembler::AddressSet::const_iterator si=succs.begin(); si!=succs.end(); ++si) { rose_addr_t succ_va = *si; if (succ_va==fall_through_va) { ++nfallthrough; if (pending.find(succ_va)!=pending.end()) worklist.insert(succ_va); /* Add edge to CFG graph */ va2id.insert(std::make_pair(succ_va, va2id.size())); add_edge(va2id[va], va2id[succ_va], cfg); } else if (addresses.find(succ_va)==addresses.end()) { /* A non-fallthrough branch to something outside this memory region */ if (functions.find(succ_va)!=functions.end()) { /* A branch to a function entry point we've previously discovered. */ ++ncalls; } else { ++nnoncalls; } } else { /* A non-fallthrough branch to something in our address range. */ ++ninternal; if (pending.find(succ_va)!=pending.end()) worklist.insert(succ_va); /* Add edge to CFG graph */ va2id.insert(std::make_pair(succ_va, va2id.size())); add_edge(va2id[va], va2id[succ_va], cfg); } } } } /* Statistics */ stats->add_sample(RegionStats::RA_NFAILS, nfails); stats->add_sample(RegionStats::RA_NINSNS, insns_found.size()); stats->add_sample(RegionStats::RA_NOVERLAPS, noverlaps); stats->add_sample(RegionStats::RA_NSTARTS, nstarts); stats->add_sample(RegionStats::RA_NCOVERAGE, insns_extent.size()); stats->add_sample(RegionStats::RA_NINCOMPLETE, nincomplete); stats->add_sample(RegionStats::RA_NBRANCHES, ncalls+nnoncalls+ninternal); stats->add_sample(RegionStats::RA_NCALLS, ncalls); stats->add_sample(RegionStats::RA_NNONCALLS, nnoncalls); stats->add_sample(RegionStats::RA_NINTERNAL, ninternal); stats->add_sample(RegionStats::RA_NICFGEDGES, ninternal + nfallthrough); stats->add_sample(RegionStats::RA_NIUNIQUE, count_kinds(insns_found)); stats->add_sample(RegionStats::RA_NPRIV, count_privileged(insns_found)); stats->add_sample(RegionStats::RA_NFLOAT, count_floating_point(insns_found)); double regsz, regvar; stats->add_sample(RegionStats::RA_NREGREFS, count_registers(insns_found, ®sz, ®var)); stats->add_sample(RegionStats::RA_REGSZ, regsz); stats->add_sample(RegionStats::RA_REGVAR, regvar); /* Count the number of connected components in the undirected CFG */ if (!va2id.empty()) { std::vector<int> component(num_vertices(cfg)); stats->add_sample(RegionStats::RA_NCOMPS, connected_components(cfg, &component[0])); } stats->compute_ratios(); return stats; }
pixelinfo_t * ri_sis(float *texels, int ngensamples, int width, int height) { int i; int maxlevel = 6; double m; double s; double v; double domega0; pixelinfo_t *info; pixelinfo_t **layer; pixelinfo_t *gensamples; FILE *fp; fp = fopen("gensamples.dat", "w"); if (!fp) exit(-1); printf("start sis\n"); gensamples = (pixelinfo_t *)malloc(sizeof(pixelinfo_t) * ngensamples); if (!gensamples) { printf("muda muda muda. gensamples = NULL\n"); exit(-1); } layer = (pixelinfo_t **)malloc(sizeof(pixelinfo_t *) * maxlevel); if (!layer) { printf("muda muda muda. layer = NULL\n"); exit(-1); } for (i = 0; i < maxlevel; i++) { layer[i] = (pixelinfo_t *)malloc(sizeof(pixelinfo_t) * width * height); } info = initpixelinfo(texels, width, height); mean(&m, info, width * height); sd (&s, info, width * height, m); assign_level(info, width * height, s, maxlevel); layerize(layer, maxlevel, info, width * height); //output_hdr(layer[1], 1, width, height); /* detect connected components each layer */ for (i = 0; i < maxlevel; i++) { connected_components(layer[i], width, height); //output_cc(layer[i], i, width, height); } domega0 = 0.01; gamma_4pi(&v, info, width * height, domega0); generate_sample(gensamples, ngensamples, v, layer, maxlevel, width, height); /* Output samples */ fprintf(fp, "%d\n", ngensamples); fprintf(fp, "%d %d\n", width, height); for (i = 0; i < ngensamples; i++) { fprintf(fp, "%d %d %f %f %f\n", gensamples[i].x, gensamples[i].y, gensamples[i].c[0], gensamples[i].c[1], gensamples[i].c[2]); } fclose(fp); return info; }
bool isConnected(const Graph & g) { std::vector<int> comps(order(g)); long num = connected_components(g, &comps[0]); return num < 2; }
vector<vector<int>> myGraph::getRank(vector<vector<int>> Edge, vector<vector<vector<int>>> Vertex, vector<vector<int>> VertexInfo, int roomID) { typedef boost::adjacency_list <boost::vecS, boost::vecS, boost::undirectedS> Graph; vector<vector<int>> Rank; Graph G; vector<Graph> subG; vector<int> nodeRanks(Vertex.size(),20000); float maxWeight=-1000000; //Edge to graph /*add_edge(0, 1, G); add_edge(1, 4, G); add_edge(4, 0, G); add_edge(2, 5, G);*/ if(roomID==0) roomID=roomID; int count=0; for(int k=0; k<Edge.size(); k++) { //if(Edge[k][0]==etemp[0] || Edge[k][1]==etemp[1]) if(roomID<0 || (VertexInfo[Edge[k][0]][3] == roomID && VertexInfo[Edge[k][1]][3] == roomID ) ) { if(Edge[k].size()>=4) { if(maxWeight<Edge[k][4]) maxWeight=Edge[k][4]; } else maxWeight=1; add_edge(Edge[k][0], Edge[k][1], G); count++; } } if(count==0) return Rank; vector<vector<int>> Edge_t;//=Edge; //change weight: //for each commpoent //find longestPath (reverse the weight) for all node //get connected components std::vector<int> component(num_vertices(G)); int num = connected_components(G, &component[0]); //subG.resize(num); vector<set<int>> edgeSet; vector<set<int>> nodeSet; edgeSet.resize(num); nodeSet.resize(num); std::vector<int>::size_type i; for (i = 0; i != component.size(); ++i) { for(int j=0; j<Vertex[i].size(); j++) for(int k=0; k<Vertex[i][j].size(); k++) { int eid = Vertex[i][j][k]; vector<int> edge=Edge[eid]; if(VertexInfo[edge[0]][3] == roomID && VertexInfo[edge[1]][3] == roomID) edgeSet[component[i]].insert(eid); } nodeSet[component[i]].insert(i); } for (int sid = 0; sid < edgeSet.size(); ++sid) { if(edgeSet[sid].size()>0) { Edge_t.clear(); for(set<int>::iterator it=edgeSet[sid].begin(); it!=edgeSet[sid].end(); it++) { int eid=*it; vector<int> edge = Edge[eid]; if(edge.size()<4) edge.push_back(1); else edge[4]= maxWeight+1-edge[4]; Edge_t.push_back(edge); } //init set<int> Node_t=nodeSet[sid]; set<int> preNodeSet; while(!Edge_t.empty() && !Node_t.empty()) { //get ranks for sub pathes subPathes(preNodeSet, Edge_t, Node_t, nodeRanks); } } } //put rank according the ditance to the start node on the path for (int nid = 0; nid < nodeRanks.size(); ++nid) { if(nodeRanks[nid]==20000) continue; int level=nodeRanks[nid]; if(Rank.size() <= level) { Rank.resize(level+1); } Rank[level].push_back(nid); } return Rank; }
inline typename property_traits<Components>::value_type connected_components(Graph& G, Components c) { return connected_components(G, c, dfs_visitor<>()); }