Exemplo n.º 1
0
graph_t *KCoreGraph::subgraph(IntStack *vset) {
	
	assert(!vset->empty());
	
	graph_t *g = graph_new(maxsize());
	IntStack *nb;
	
	int val,preval,val2,preval2;
	
	val = vset->head();
	preval = val-1;
	while (val != preval) {
		nb = neighbourhoods.at(val);
		if (!nb->empty()) {
			val2 = nb->head();
			preval2 = val2-1;
			while (val2 != preval2) {
				if ((val > val2) && vset->contain(val2)) GRAPH_ADD_EDGE(g,val,val2);
				preval2 = val2;
				val2 = nb->next(preval2);
			}
		}
		preval = val;
		val = vset->next(preval);
	}
	
	return g;
};
Exemplo n.º 2
0
graph_t* MatrixToGraph(double *adjac_mtx_ptr, int iGraphOrder)
{
    int i, j, idx;
    graph_t *ptrGraph;
    
    /* Initialize the graph that we want to return. */
    ptrGraph = graph_new(iGraphOrder);
    
    /* Loop through the adjacency matrix to create the graph.  We assume
     * that the adjacency matrix is symmetric and only consider the super-
     * diagonals of the matrix. */
    /* The indexing here seems flipped, but it's due to the translation
     * between MATLAB and C element ordering. */
    for (j = 0; j < iGraphOrder; j++)
        for (i = j + 1; i < iGraphOrder; i++)
        {
            /* The matrix adj_mtx is stored as a 1-dimensional array, so
             * we must convert the coordinate (i, j) to the corresponding
             * 1-dimensional index. */
            idx = j + i * iGraphOrder;
            
            /* If the entry of the adjacency matrix is a 1, we want to add
             * an edge to our graph. */
            if(adjac_mtx_ptr[idx] == 1)
                GRAPH_ADD_EDGE(ptrGraph, i, j);
        }
    
    /* Just to be cautios, ensure that we've produced a valid graph. */
    ASSERT(graph_test(ptrGraph, NULL));
    
    return ptrGraph;
}
Exemplo n.º 3
0
/* Convert an igraph graph to a Cliquer graph */
static void igraph_to_cliquer(const igraph_t *ig, graph_t **cg) {
    igraph_integer_t vcount, ecount;
    int i;

    if (igraph_is_directed(ig))
      IGRAPH_WARNING("Edge directions are ignored for clique calculations");

    vcount = igraph_vcount(ig);
    ecount = igraph_ecount(ig);

    *cg = graph_new(vcount);

    for (i=0; i < ecount; ++i) {
        long s, t;
        s = IGRAPH_FROM(ig, i);
        t = IGRAPH_TO(ig, i);
        if (s != t)
            GRAPH_ADD_EDGE(*cg, s, t);
    }
}
Exemplo n.º 4
0
/*
 * graph_read_dimacs_binary()
 *
 * Reads a dimacs-format binary file from file stream fp with the first
 * line being firstline.
 *
 * Returns the newly-allocated graph or NULL if an error occurred.
 *
 * TODO: This function leaks memory when reading erroneous files.
 */
static graph_t *graph_read_dimacs_binary(FILE *fp,char *firstline) {
	int length=0;
	graph_t *g;
	int i,j;
	char *buffer;
	char *start;
	char *end;
	char **buf;
	char tmp[10];

	if (sscanf(firstline," %d %2s",&length,tmp)!=1)
		return NULL;
	if (length<=0) {
		fprintf(stderr,"Malformed preamble: preamble size < 0.\n");
		return NULL;
	}
	buffer=malloc(length+2);
	if (fread(buffer,1,length,fp)<length) {
		fprintf(stderr,"Malformed preamble: unexpected "
			"end of file.\n");
		free(buffer);
		return NULL;
	}

	g=calloc(1,sizeof(graph_t));
	start=buffer;
	while (start < buffer+length) {
		end=strchr(start,'\n');
		if (end==NULL)
			end=buffer+length;
		end[0]=0;
		if (!parse_input(start,g)) {
			fprintf(stderr,"Malformed preamble: %s\n",start);
			free (buffer);
			return NULL;
		}
		start=end+1;
	}

	free(buffer);
	if (g->n <= 0) {
		fprintf(stderr,"Malformed preamble: number of "
			"vertices <= 0\n");
		free(g);
		return NULL;
	}

	/* Binary part. */
	buf=calloc(g->n,sizeof(char*));
	for (i=0; i < g->n; i++) {
		buf[i]=calloc(g->n,1);
		if (fread(buf[i],1,i/8+1,fp) < (i/8+1)) {
			fprintf(stderr,"Unexpected end of file when "
				"reading graph.\n");
			return NULL;
		}
	}

	for (i=0; i < g->n; i++) {
		for (j=0; j<i; j++) {
			if (buf[i][j/8]&(1<<(7-(j%8)))) {
				GRAPH_ADD_EDGE(g,i,j);
			}
		}
		free(buf[i]);
	}
	free(buf);

	return g;
}
Exemplo n.º 5
0
/*
 * parse_input()
 *
 * Parses the string str for ASCII-format dimacs commands, and modifies
 * the graph g accordingly.
 *
 * Returns TRUE if successful, FALSE if a bad command was encountered.
 *
 * Note: Ignores all unknown commands.  The 'd', 'v' and 'x' commands
 *       (mainly generator-specific information) are ignored silently,
 *       for all others a warning message is printed to stderr.
 */
static boolean parse_input(char *str,graph_t *g) {
	int i,j,w;
	char tmp[16];

	for (i=0; i<strlen(str); i++) {
		if (!isspace((int)str[i]))
			break;
	}
	if (i>=strlen(str))  /* blank line */
		return TRUE;
	if (str[i+1]!=0 && !isspace(str[i+1]))  /* not 1-char field */
		return FALSE;

	switch (str[i]) {
	case 'c':
		return TRUE;
	case 'p':
		if (g->n != 0)
			return FALSE;
		if (sscanf(str," p %15s %d %d %2s",tmp,&(g->n),&i,tmp)!=3)
			return FALSE;
		if (g->n <= 0)
			return FALSE;
		g->edges=calloc(g->n,sizeof(set_t));
		for (i=0; i<g->n; i++)
			g->edges[i]=set_new(g->n);
		g->weights=calloc(g->n,sizeof(int));
		for (i=0; i<g->n; i++)
			g->weights[i]=1;
		return TRUE;
	case 'n':
		if ((g->n <= 0) || (g->weights == NULL))
			return FALSE;
		if (sscanf(str," n %d %d %2s",&i,&w,tmp)!=2)
			return FALSE;
		if (i<1 || i>g->n)
			return FALSE;
		if (w<=0)
			return FALSE;
		g->weights[i-1]=w;
		return TRUE;
	case 'e':
		if ((g->n <= 0) || (g->edges == NULL))
			return FALSE;
		if (sscanf(str," e %d %d %2s",&i,&j,tmp)!=2)
			return FALSE;
		if (i<1 || j<1 || i>g->n || j>g->n)
			return FALSE;
		if (i==j)   /* We want antireflexive graphs. */
			return TRUE;
		GRAPH_ADD_EDGE(g,i-1,j-1);
		return TRUE;
	case 'd':
	case 'v':
	case 'x':
		return TRUE;
	default:
		fprintf(stderr,"Warning: ignoring field '%c' in "
			"input.\n",str[i]);
		return TRUE;
	}
}
Exemplo n.º 6
0
  void
  multibinpacking(Home home, 
         int n, int m, int k,
         const IntVarArgs& y, 
         const IntVarArgs& x, 
         const IntSharedArray& D,
         const IntSharedArray& B,
         IntConLevel) 
   {
      /// Check input sizes
      if (n*k != D.size() )
         throw ArgumentSizeMismatch("Int::multibinpacking");      

      if (k != B.size() )
         throw ArgumentSizeMismatch("Int::multibinpacking");      
    
      if (n != x.size() )
         throw ArgumentSizeMismatch("Int::multibinpacking");      
      
      if (m*k != y.size() )
         throw ArgumentSizeMismatch("Int::multibinpacking");      
      
      for (int i=B.size(); i--; )
         Limits::nonnegative(B[i],"Int::multibinpacking");

      if (home.failed()) return;

      /// Post first each single binpacking constraint
      /// Capacity constraint for each dimension
      for ( int j = 0; j < m; ++j )
         for ( int l = 0; l < k; ++l ) {
            IntView yi(y[j*k+l]);
            GECODE_ME_FAIL(yi.lq(home,B[l]));
         }

      /// Post a binpacking constraints for each dimension
      for ( int l = 0; l < k; ++l ) {
         ViewArray<OffsetView> yv(home,m);
         for (int j=m; j--; )
            yv[j] = OffsetView(y[j*k+l],0);

         ViewArray<BinPacking::Item> xs(home,x.size());
         for (int i=xs.size(); i--; )
            xs[i] = BinPacking::Item(x[i],D[i*k+l]);

         Support::quicksort(&xs[0], xs.size());

         GECODE_ES_FAIL(Int::BinPacking::Pack::post(home,yv,xs));
      }

      /// Clique Finding and Alldifferent posting
      {
         /// Firt construct the conflict graph
         graph_t* g = graph_new(n);
         for ( int a = 0; a < n-1; ++a ) {
            for ( int b = a+1; b < n; ++b ) {
               int v = a;  /// The variable with smaller domain
               int w = b;
               unsigned int nl = 0;
               if ( x[a].size() > x[b].size() ) {
                  v = b;
                  w = a;
               }
               IntVarValues i(x[v]);
               IntVarValues j(x[w]);
               while ( i() ) {
                  if ( i.val() != j.val() ) {
                     if ( i.val() < j.val() )
                        break;
                     else
                        ++i;
                  } else {
                     for ( int l = 0; l < k; ++l )
                        if ( D[a*k+l] + D[b*k+l] > B[l] ) {
                           nl++;
                           break;
                        }
                     ++i; ++j;
                  }
               }
               if ( nl >= x[v].size() )
                  GRAPH_ADD_EDGE(g,a,b);
            }
         }
         /// Consitency cheking: look for the maximum clique
         clique_options* opts;
         opts = (clique_options*) malloc (sizeof(clique_options));
         opts->time_function=NULL;
         opts->reorder_function=reorder_by_default;
         opts->reorder_map=NULL;
         opts->user_function=NULL;
         opts->user_data=NULL;
         opts->clique_list=NULL;
         opts->clique_list_length=0;
         set_t s;
         s = clique_find_single ( g, 0, 0, TRUE, opts);
         if ( s != NULL ) {
            if ( set_size(s) > m ) {
                set_free(s);
                free(opts);
                graph_free(g);
                GECODE_ES_FAIL(ES_FAILED);
            }
            if ( true ) { //option == 1 ) {  FIXING
               for ( int a = 0, j = 0; a < n; ++a ) {
                  if ( SET_CONTAINS_FAST(s,a) ) {
                     assert( x[a].in(j) );
                     IntView xi(x[a]);
                     GECODE_ME_FAIL(xi.eq(home,j++));
                  }
               }
            }
         }
         if ( s!=NULL )
            set_free(s);
         /// List every maximal clique in the conflict graph
         opts->user_function=record_clique_func;
         clique_find_all ( g, 2, 0, TRUE, opts);
         for ( int c = 0; c < clique_count; c++ ) {
            ViewArray<IntView> xv(home, set_size(clique_list[c]));
            for ( int a = 0, idx = 0; a < n; ++a )
               if ( SET_CONTAINS_FAST(clique_list[c],a) )
                  xv[idx++] = x[a];
            GECODE_ES_FAIL(Distinct::Dom<IntView>::post(home,xv));
            set_free(clique_list[c]);
         }
         free(opts);
         graph_free(g);
      }
   }
Exemplo n.º 7
0
int get_h(C_Graphe &G, char *name, int *UB){
	int *som = new int[G.nb_sommets];
	for(int i=0 ; i<G.nb_sommets ; i++){som[i] = 0;}
	int cpt=0;
	int h=0;
	int nb;
	*UB = 0;

	while (cpt <= G.nb_sommets-1){
		graph_t *g;
		g=(graph_t *)calloc(1,sizeof(graph_t));
		g->n = G.nb_sommets;
		g->edges=(set_t *)calloc(g->n,sizeof(set_t));
		for (int i=0; i<g->n; i++){
			g->edges[i]=set_new(g->n);
		}
		g->weights=(int *)calloc(g->n,sizeof(int));
		for(int i=0; i<g->n; i++){
			g->weights[i]=1;
		}
		for(int i=0; i<g->n; i++){
			for(int j=i+1; j<g->n; j++){
				if( G.matrice_adjacence[i][j] == 0 && i!=j && som[i]+som[j] == 0 ){
					GRAPH_ADD_EDGE(g,i,j);
//					cout<<"adding edge "<<i<<","<<j<<"\n";
				}
			}
		}
		set_t s;

		s = clique_find_single(g,0,0,FALSE,NULL);

		nb=0;
		for(int i=0 ; i<G.nb_sommets ; i++){
			if (SET_CONTAINS(s,i)){
				som[i] = 1;
				cpt++;
				nb++;
			}
		}
		h++;
		*UB=*UB+nb*h;
		for (int i=0; i<g->n; i++){
			set_free(g->edges[i]);
		}
		free(g->edges);
		free(g->weights);
		free(g);
		set_free(s);
	}
	for (int i=0; i<G.nb_sommets; i++){
		if(som[i] == 0){
			h++;
			break;
		}
	}

	ofstream fichier;
	ostringstream tmp;

	//Ouverture fichiers
	tmp.str("");tmp<<"h.col";
	fichier.open(tmp.str().c_str(), ios::out|ios::app);
	if (!fichier){
		cerr << "Erreur à l'ouverture du fichier\n";
		return 1;
	}
	fichier<<name<<" "<<h<<" "<<*UB<<"\n";
	fichier.close();

	delete [] som;

	return h;
}
Exemplo n.º 8
0
void STABLE_max_heur(C_Graphe &G, int *valeur){
	int *som = new int[G.nb_sommets];
	for(int i=0 ; i<G.nb_sommets ; i++){som[i] = 0;}
	int cpt=0;
	int h=0;
	int nb;
	*valeur = 0;

	while (cpt <= G.nb_sommets-1){
		graph_t *g;
		g=(graph_t *)calloc(1,sizeof(graph_t));
		g->n = G.nb_sommets;
		g->edges=(set_t *)calloc(g->n,sizeof(set_t));
		for (int i=0; i<g->n; i++){
			g->edges[i]=set_new(g->n);
		}
		g->weights=(int *)calloc(g->n,sizeof(int));
		for(int i=0; i<g->n; i++){
			g->weights[i]=1;
		}
		for(int i=0; i<g->n; i++){
			for(int j=i+1; j<g->n; j++){
				if( G.matrice_adjacence[i][j] == 0 && i!=j && som[i]+som[j] == 0 ){
					GRAPH_ADD_EDGE(g,i,j);
//					cout<<"adding edge "<<i<<","<<j<<"\n";
				}
			}
		}
		set_t s;

		s = clique_find_single(g,0,0,FALSE,NULL);

		nb=0;
		for(int i=0 ; i<G.nb_sommets ; i++){
			if (SET_CONTAINS(s,i)){
				som[i] = 1;
				cpt++;
				nb++;
			}
		}
		h++;
		*valeur=*valeur+nb*h;
		for (int i=0; i<g->n; i++){
			set_free(g->edges[i]);
		}
		free(g->edges);
		free(g->weights);
		free(g);
		set_free(s);
	}
	for (int i=0; i<G.nb_sommets; i++){
		if(som[i] == 0){
			h++;
			*valeur=*valeur+h;
			break;
		}
	}

	delete [] som;

	return ;
}