void Graph::add_selfloops() { vector<unsigned long long> aux_deg; vector<int> aux_links; unsigned long long sum_d = 0ULL; for (int u=0 ; u < nb_nodes ; u++) { pair<vector<int>::iterator, vector<long double>::iterator> p = neighbors(u); int deg = nb_neighbors(u); for (int i=0 ; i < deg ; i++) { int neigh = *(p.first+i); aux_links.push_back(neigh); } sum_d += (unsigned long long)deg; if (nb_selfloops(u) == 0.0L) { aux_links.push_back(u); // add a selfloop sum_d += 1ULL; } aux_deg.push_back(sum_d); // add the (new) degree of vertex u } links = aux_links; degrees = aux_deg; nb_links += (unsigned long long)nb_nodes; }
void Graph::display() { /* for (unsigned int node=0 ; node<nb_nodes ; node++) { pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node); for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) { if (node<=*(p.first+i)) { if (weights.size()!=0) cout << node << " " << *(p.first+i) << " " << *(p.second+i) << endl; else cout << node << " " << *(p.first+i) << endl; } } }*/ for (unsigned int node=0 ; node<nb_nodes ; node++) { pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node); cout << node << ":" ; for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) { if (true) { if (weights.size()!=0) cout << " (" << *(p.first+i) << " " << *(p.second+i) << ")"; else cout << " " << *(p.first+i); } } cout << endl; } }
void Graph::init_layers_per_node() { map<int, deque<int> > layers_per_node; total_layer_per_node = 0; for (int node=0 ; node < nb_nodes ; node++) { for (int layer=0; layer < nb_layers; layer++) { double tot_degree = (double)nb_neighbors(node, layer, OUTGOING) + (double)nb_neighbors(node, layer, INCOMING) + self_weight(node, layer); if (tot_degree > 0) { layers_per_node[node].push_back(layer); total_layer_per_node++; } } } //Allocate memory for 'layer' degree and 'layers' per node if ( !(nb_nonnull_layers_per_node = (int*)malloc((long)nb_nodes*sizeof(int))) ) { cerr << "Could not allocated memory for nb nonnull layers." << endl; exit(-1); } if ( !(nonnull_layers_per_node = (int *)malloc((long)total_layer_per_node*sizeof(int))) ) { cerr << "Could not allocated memory for nonnull layers." << endl; exit(-1); } int prev = 0; int i = 0; for (int node=0; node < nb_nodes; node++) { int s = layers_per_node[node].size(); nb_nonnull_layers_per_node[node] = s + prev; prev = nb_nonnull_layers_per_node[node]; for (int layer_ind = 0; layer_ind < s; layer_ind++) { nonnull_layers_per_node[i++] = layers_per_node[node][layer_ind]; } } }
void Graph::display_reverse() { for (unsigned int node=0 ; node<nb_nodes ; node++) { pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node); for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) { if (node>*(p.first+i)) { if (weights.size()!=0) cout << *(p.first+i) << " " << node << " " << *(p.second+i) << endl; else cout << *(p.first+i) << " " << node << endl; } } } }
void Graph::display() { for (int node=0 ; node<nb_nodes ; node++) { pair<int *,int *> p = neighbors(node); for (int i=0 ; i<nb_neighbors(node) ; i++) { if (weights!=NULL) cout << node << " " << *(p.first+i) << " " << *(p.second+i) << endl; else { cout << (node+1) << " " << (*(p.first+i)+1) << endl; // cout << (node) << " " << (*(p.first+i)) << endl; } } } }
bool Graph::check_symmetry() { int error=0; for (unsigned int node=0 ; node<nb_nodes ; node++) { pair<vector<unsigned int>::iterator, vector<float>::iterator > p = neighbors(node); for (unsigned int i=0 ; i<nb_neighbors(node) ; i++) { unsigned int neigh = *(p.first+i); float weight = *(p.second+i); pair<vector<unsigned int>::iterator, vector<float>::iterator > p_neigh = neighbors(neigh); for (unsigned int j=0 ; j<nb_neighbors(neigh) ; j++) { unsigned int neigh_neigh = *(p_neigh.first+j); float neigh_weight = *(p_neigh.second+j); if (node==neigh_neigh && weight!=neigh_weight) { cout << node << " " << neigh << " " << weight << " " << neigh_weight << endl; if (error++==10) exit(0); } } } } return (error==0); }
void Graph::display() { for (int node=0 ; node<nb_nodes ; node++) { pair<vector<int>::iterator, vector<long double>::iterator > p = neighbors(node); cout << node << ":" ; for (int i=0 ; i<nb_neighbors(node) ; i++) { if (true) { if (weights.size()!=0) cout << " (" << *(p.first+i) << " " << *(p.second+i) << ")"; else cout << " " << *(p.first+i); } } cout << endl; } }
void Graph::display(char *outfile) { ofstream foutput; foutput.open(outfile ,fstream::out); for (int node=0 ; node<nb_nodes ; node++) { for (int layer=0; layer<nb_layers; layer++) { pair<int *,double *> p = neighbors(node, layer, OUTGOING); for (int i=0; i<nb_neighbors(node, layer, OUTGOING); i++) foutput << node << " " << *(p.first+i) << " " << *(p.second+i) << " " << layer << endl; } } foutput.close(); }
void Graph::init_self_weights() { // Initialize self weights if ( !(self_weights = (double*)malloc((long)nb_nodes*nb_layers*sizeof(double))) ) { cerr << "Could not allocated memory for self weights." << endl; exit(-1); } for (int node = 0; node < nb_nodes; node++) { for (int layer = 0; layer < nb_layers; layer++) { pair<int *,double *> p = neighbors(node, layer, OUTGOING); int deg = nb_neighbors(node, layer, OUTGOING); //cerr << "Node " << node << ", degree " << deg << ", mem " << p.first << endl; // By default no self_weight self_weights[node*nb_layers+layer] = 0; for (int i=0 ; i < deg; i++) { //cerr << " Address " << p.first + i << endl; if (*(p.first+i)==node) { if (weights!=NULL) { self_weights[node*nb_layers+layer] = *(p.second+i); } else { self_weights[node*nb_layers+layer] = 1; } } } } } }
/*=====================================================================================*/ list * MSF_Prim(MergeTree * MT) /*=====================================================================================*/ /*Segment a tree into two components. Returns a list of nodes correspunding to the Max Spanning Forest cut, computed using Prim's algorithm */ { int32_t i, j,u,v, x,y,z, x_1,y_1; int nb_markers; int nb_leafs; long N, M; // -------- Gathering usefull input graph (MT) informations ----------- float val=0; //weight parameter for leafs. mtree * T= MT->tree; // mergeTreePrint(T); float * W = MT->weights; JCctree *CT = T->CT; int root_node = CT->root; //M = nb nodes M = CT->nbnodes; //N = nb_edges nb_leafs = 0; for (i = 0; i < M; i++) if (CT->tabnodes[i].nbsons == 0) nb_leafs++; nb_markers = nb_leafs+1; N=M+nb_markers; M=N-1; //init Prim //Creates a Red-Black tree to sort edges Rbt *L; IndicsInit(M); L = mcrbt_CreeRbtVide(M); i=0; int sizeL = 0; // Set for already checked edges for(u = 0; u < M; u ++) Set(u, false); // marked nodes uint32_t * SeededNodes = (uint32_t*)malloc(nb_markers*sizeof(uint32_t)); if (SeededNodes == NULL) { fprintf(stderr, "prim : malloc failed\n"); exit(0);} // Resulting node labeling goes into G2 uint8_t * G2 = (uint8_t*)calloc(N ,sizeof(uint8_t)); // fill the array SeededNodes with marked index nodes // fill the tree L only with the marked edges SeededNodes[0]= M; j=1; for (i = 0; i < CT->nbnodes; i++) if (CT->tabnodes[i].nbsons == 0) { SeededNodes[j]= i+CT->nbnodes; G2[SeededNodes[j]] = 2; mcrbt_RbtInsert(&L, (TypRbtKey)(val), SeededNodes[j]); sizeL++; // fprintf(stderr,"L=%d", sizeL); Set(SeededNodes[j], true); j++; } G2[root_node]=1; mcrbt_RbtInsert(&L, (TypRbtKey)(1-W[root_node]), root_node); sizeL++; Set(root_node, true); // weights float * Weights = (float *)malloc(M*sizeof(float)); for(j=0;j<CT->nbnodes;j++) Weights[j]=W[j]; for(j=0;j<nb_leafs;j++) Weights[CT->nbnodes+j]=val; // While there exists unprocessed nodes while(sizeL != 0) { //Pick an edge u of min weight in the tree. u = RbtPopMin(L); // fprintf(stderr, "pop %d\n", u); sizeL--; // Find its extreme nodes (x,y) x = u; // x = G->Edges[0][u]; if (u<CT->nbnodes) y= CT->tabnodes[u].father; else if(u!=M) y= u-CT->nbnodes; else y=root_node; if (y==-1)y=M; //y = G->Edges[1][u]; // y must correspond to the marked node. if(G2[x] > G2[y]) {z=x; x=y; y=z;} // if one node is labeled if((minimum(G2[x],G2[y]) == 0) && (maximum(G2[x],G2[y]) > 0)) { // assign the same label to the other one G2[x] = G2[y]; //fprintf(stderr,"Map[%d]=Map[%d]\n",x,y); // select neighbors edges to place them in the tree j= nb_neighbors(u, CT, nb_leafs); //fprintf(stderr,"nb_neigbors= %d \n",j); for (i=0;i<j;i++) { v = neighbor(u, i, CT, nb_leafs, SeededNodes); if (v==-1)v=M; // fprintf(stderr," %d ",v); // if the edge v is not processed yet if(!IsSet(v, true)) { // Find its extreme nodes (x_1,y_1) x_1 = v; if (v<CT->nbnodes) y_1= CT->tabnodes[v].father; else if(v!=M) y_1= v-CT->nbnodes; else y_1 = root_node; if (y_1==-1)y_1=M; //fprintf(stderr," [%d %d] ",x_1, y_1); if((minimum(G2[x_1],G2[y_1]) == 0) && (maximum(G2[x_1],G2[y_1]) > 0)) { //fprintf(stderr,"( insert %d) ",v); mcrbt_RbtInsert(&L, (TypRbtKey)(1-Weights[v]), v); sizeL++; Set(v,true); } } } // fprintf(stderr," \n"); } UnSet(u,true); } /* for (i=0; i<N; i++) printf("Map[%d]=%d \n",i,G2[i]-1);*/ // Process the tree to find the cut list * cut = NULL; for (i = 0; i < CT->nbnodes; i++) { // nodes having a different value than their father are in the cut if ((CT->tabnodes[i].father != -1) && (G2[CT->tabnodes[i].father] != G2[i])) Insert3(&cut, i); // leafs having the same label as the root are in the cut if ((CT->tabnodes[i].nbsons == 0) && (G2[i]-1==0)) Insert3(&cut, i); } if (cut == NULL) Insert3(&cut, root_node); // PrintList(cut); IndicsTermine(); free(G2); mcrbt_RbtTermine(L); free(SeededNodes); free(Weights); return cut; }