Example #1
0
/* call-seq:
 *   graph.get_adjacency(type) -> Array
 *
 * Returns the adjacency matrix of a graph
 *
 */
VALUE cIGraph_get_adjacency(VALUE self, VALUE mode){

  igraph_t *graph;
  igraph_get_adjacency_t pmode = NUM2INT(mode);
  igraph_matrix_t res;
  int i;
  int j;
  VALUE row;
  VALUE path_length;
  VALUE matrix = rb_ary_new();
  int n;

  Data_Get_Struct(self, igraph_t, graph);

  n = igraph_vcount(graph);

  //matrix to hold the results of the calculations
  igraph_matrix_init(&res,n,n);

  igraph_get_adjacency(graph,&res,pmode);

  for(i=0; i<igraph_matrix_nrow(&res); i++){
    row = rb_ary_new();
    rb_ary_push(matrix,row);
    for(j=0; j<igraph_matrix_ncol(&res); j++){
      path_length = INT2NUM(MATRIX(res,i,j));
      rb_ary_push(row,path_length);
    }
  }

  igraph_matrix_destroy(&res);

  return matrix;

}
Example #2
0
symmetric_matrix<double, lower> Graph::adjacency() const {
  double x;
  int i,j;
  igraph_matrix_t m; 
  symmetric_matrix<double, lower> adjmatrix(size, size);
  igraph_matrix_init(&m, size, size);
  igraph_get_adjacency(graph, &m,IGRAPH_GET_ADJACENCY_LOWER);  /* IGRAPH_GET_ADJACENCY_BOTH);*/
  for(i = 0; i < size; i++){
    for(j = 0; j <=i; j++) {
      x = MATRIX(m,i,j) ? 1 : 0;
      adjmatrix(i,j) = x;
    }
  }
  igraph_matrix_destroy(&m);
  return adjmatrix;
}
Example #3
0
int* readGrafo(){

	int *grafo;
	igraph_matrix_t gMatrix;
	igraph_t g;
	igraph_i_set_attribute_table(&igraph_cattribute_table);
	FILE *ifile;
	ifile=fopen("/home/john/git/primAlgorithm/celegansneural.gml"/*"/home/john/git/primAlgorithm/grafo.gml"*/, "r");
	if (ifile==0) {
		printf("Problema abriendo archivo de grafo\n");
		return NULL;
	}
	igraph_read_graph_gml(&g, ifile);

	fclose(ifile);
	numNodos = igraph_vcount(&g);
	grafo = malloc(numNodos*numNodos*sizeof(int));
	igraph_matrix_init(&gMatrix,numNodos,numNodos);
	igraph_get_adjacency(&g,&gMatrix,IGRAPH_GET_ADJACENCY_BOTH,1);

	igraph_vector_t el;
	int ii, jj, n;
	igraph_vector_init(&el, 0);
	igraph_get_edgelist(&g, &el, 0);
	n = igraph_ecount(&g);

	memset(grafo,INT_MAX2,numNodos*numNodos*sizeof(int));

	  for (ii=0, jj=0; ii<n; ii++, jj+=2) {
	    grafo[((long)VECTOR(el)[jj])+numNodos*((long)VECTOR(el)[jj+1])] = (int)EAN(&g, "weight", ii);
	    grafo[((long)VECTOR(el)[jj+1])+numNodos*((long)VECTOR(el)[jj])] =  (int)EAN(&g, "weight", ii);
	  }

	printf("\nNumero de nodos %d",numNodos);

	igraph_vector_destroy(&el);
	igraph_destroy(&g);
	return grafo;

}
int main() {

  igraph_t g;
  igraph_matrix_t L, R;
  igraph_sparsemat_t Lsparse, Rsparse;
  igraph_matrix_t adj, V;
  igraph_vector_t groups;
  igraph_eigen_which_t which;

  igraph_matrix_init(&L, 0, 0);
  igraph_matrix_init(&R, 0, 0);
  igraph_matrix_init(&adj, 0, 0);
  igraph_matrix_init(&V, 0, 0);
  igraph_vector_init(&groups, 0);
    
  igraph_rng_seed(igraph_rng_default(), 42);
  
  igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED);
  
  igraph_get_adjacency(&g, &adj, IGRAPH_GET_ADJACENCY_BOTH, /*eids=*/ 0);

  which.pos=IGRAPH_EIGEN_LM;
  which.howmany=1;
  igraph_eigen_matrix_symmetric(&adj, /*sparsemat=*/ 0, /*fun=*/ 0,
				igraph_vcount(&g), /*extra=*/ 0, 
				/*algorithm=*/ IGRAPH_EIGEN_LAPACK,
				&which, /*options=*/ 0, /*storage=*/ 0, 
				/*values=*/ 0, &V);

#define SEMI()								\
  do {									\
    igraph_scg_semiprojectors(&groups, IGRAPH_SCG_SYMMETRIC, &L, &R,	\
			      &Lsparse, &Rsparse, /*p=*/ 0,		\
			      IGRAPH_SCG_NORM_ROW);			\
  } while(0)

#define PRINTRES()				\
  do {						\
    printf("----------------------\n");		\
    igraph_matrix_print(&L);			\
    printf("---\n");				\
    igraph_matrix_print(&R);			\
    printf("---\n");				\
  } while (0)

  /* -------------- */

  igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, 
		      /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC,
		      IGRAPH_SCG_OPTIMUM, /*p=*/ 0, /*maxiter=*/ 10000);
  SEMI();
  PRINTRES();

  /* -------------- */

  igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, 
		      /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC,
		      IGRAPH_SCG_INTERV_KM, /*p=*/ 0, /*maxiter=*/ 10000);
  SEMI();
  PRINTRES();

  /* -------------- */

  igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, 
		      /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC,
		      IGRAPH_SCG_INTERV, /*p=*/ 0, /*maxiter=*/ 10000);
  SEMI();
  PRINTRES();

  /* -------------- */

  igraph_scg_grouping(&V, &groups, /*(ignored) intervals=*/ 0, 
		      /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC,
		      IGRAPH_SCG_EXACT, /*p=*/ 0, /*maxiter=*/ 10000);
  SEMI();
  PRINTRES();

  /* -------------- */

  igraph_vector_destroy(&groups);
  igraph_matrix_destroy(&V);
  igraph_matrix_destroy(&adj);
  igraph_destroy(&g);
  
  return 0;
}
Example #5
0
int main(int argc, char* argv[])
{
    if (argc != 9)
    {
        cout << "Usage: ./release/fixating <Update Rule: \"Bd\", \"dB\"> <integer: population size> <\"directed\" or \"undirected\"> <double: fitness of mutant> <category of graph: \"complete\", \"ER\", \"BB\", \"WS\", \"geo\", or \"custom\" > <secondary parameter for the category of graph: \"GNM\" or \"GNP\" for Erdos Reny, double power of preference for Barabasi, int dimension for small world, bool periodic for geometric, , adjacency matrix for custom> <tertiary parameter for the category of graph: probability for every edge in Erdos-Reny GNP and geometric, number of edges for Erdos-Reny GNM, m for barabasi, probability of rewiring for small world, 0 for custom> <output: \"probability\", \"conditional\", \"unconditional\", or \"all\">" << endl;
        return -1;
    }
    //   ---------- If you want to stop time, uncomment all comments with //CLOCK//
    //CLOCK//
    std::clock_t start;
    //CLOCK//
    double bt = 0;
    //CLOCK//
    double st = 0;
    //counting variable for the graph generators
    int counts = 0;
    const unsigned int popSize = atoi(argv[2]);
    if (popSize > 23)
    {
        cout << "Code only possible for population size up to 23... aborting..." << endl;
        return -1;
    }
    const unsigned int numStates = 1 << popSize;

    string update = argv[1];
    if (update != "dB" && update != "Bd")
    {
        cout << "Only \"Bd\" or \"dB\" possible for update rule!... aborting..." << endl;
        return -1;
    }

    float fitnessMutants = atof(argv[4]);
    string direction = argv[3];
    string category = argv[5];
    igraph_t graph;
    int admat[popSize * popSize];

    string output = argv[8];
    if (output != "probability" && output != "conditional" && output != "unconditional" && output != "all")
    {
        cout << "Only \"probability\", \"unconditional\", \"conditional\" or \"all\" possible for output!... aborting..." << endl;
        return -1;
    }



    // ----------   Code snippet for fully connected graph   ----------
    if (category == "complete")
    {
        if (direction == "undirected")
        {
            igraph_full(&graph, popSize, false, false);
        }
        else if (direction == "directed")
        {
            igraph_full(&graph, popSize, true, false);
        }
        else
        {
            cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl;
            return -1;
        }
    }


    // ----------   Code snippet for random graph   ----------
    else if (category == "ER")
    {
        string gn = argv[6];

        igraph_rng_seed(igraph_rng_default(), std::clock());
        igraph_bool_t isConnected = 0;

        if (direction == "directed")
        {
            while ((isConnected == 0) & (counts < maxcount))
            {
                if (gn == "GNP")
                {
                    double edgeprob = atof(argv[7]);
                    if ((edgeprob > 1) || (edgeprob < 0))
                    {
                        cout << "probabilities larger than 1 or smaller than 0 ...aborting..." << endl;
                        return -1;
                    }
                    igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP,
                                            popSize, edgeprob,
                                            true, false);
                }
                else if (gn == "GNM")
                {
                    int edgenumber = atoi(argv[7]);
                    if ((edgenumber < 1) || (edgenumber > popSize*(popSize-1)))
                    {
                        cout << "number of edges must be greater than 1 and smaller than N*(N-1) ...aborting..." << endl;
                        return -1;
                    }

                    igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNM,
                                            popSize, edgenumber,
                                            true, false);
                }
                else
                {
                    cout << "Only \"GNM\" and \"GNP\" possible ... aborting..." << endl;
                }
                igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG);

                counts++;
            }
            if (counts == maxcount)
            {
                cout << "Probability or number of edges too low... Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl;
                return -1;
            }
        }
        else if (direction == "undirected")
        {
            int counts = 0;
            while ((isConnected == 0) & (counts < maxcount))
            {
                if (gn == "GNP")
                {
                    double edgeprob = atof(argv[7]);
                    if ((edgeprob > 1) || (edgeprob < 0))
                    {
                        cout << "probabilities larger than 1 or smaller than 0 ...aborting..." << endl;
                        return -1;
                    }
                    igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP,
                                            popSize, edgeprob,
                                            false, false);
                }
                else if (gn == "GNM")
                {
                    int edgenumber = atoi(argv[7]);
                    if ((edgenumber < 1) || (edgenumber > popSize*(popSize-1)/2))
                    {
                        cout << "number of edges must be greater than 1 and smaller than N*(N-1)/2 ...aborting..." << endl;
                        return -1;
                    }
                    igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNM,
                                            popSize, edgenumber,
                                            false, false);
                }
                else
                {
                    cout << "Only \"GNM\" and \"GNP\" possible ... aborting..." << endl;
                }
                igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG);
                counts++;
            }
            if (counts == maxcount)
            {
                cout << "Probability or number of edges too low... Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl;
                return -1;
            }
        }
        else
        {
            cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl;
            return -1;
        }
    }

//---------------------------- Code snippet for small world network --------------------------------//
    else if (category == "WS")
    {
        igraph_rng_seed(igraph_rng_default(), std::clock());
        igraph_bool_t isConnected = 0;

        double edgeprob = atof(argv[7]);
        if ((edgeprob > 1) || (edgeprob < 0))
        {
            cout << "probabilities larger than 1 or smaller than 0 ...aborting..." << endl;
            return -1;
        }

        int dim = atoi(argv[6]);
        int latSize = pow(popSize,1/double(dim));

        if (direction == "directed")
        {
            while ((isConnected == 0) & (counts < maxcount))
            {
                igraph_watts_strogatz_game(&graph, dim,
                                           latSize, 1,
                                           edgeprob, 0,
                                           0);
                igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG);
                counts++;
            }
        }
        else if (direction == "undirected")
        {
            while ((isConnected == 0) & (counts < maxcount))
            {
                igraph_watts_strogatz_game(&graph, dim,
                                           latSize, 1,
                                           edgeprob, 0,
                                           0);
                igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG);
                counts++;
            }
        }
        else
        {
            cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl;
            return -1;
        }
        if (counts == maxcount)
        {
            cout << "Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl;
            return -1;
        }
    }

//---------------------------- Code snippet for geometric generator --------------------------------//
    else if(category == "geo")
    {
        igraph_rng_seed(igraph_rng_default(), std::clock());
        igraph_bool_t isConnected = 0;
        double edgeprob = atof(argv[7]);
        if ((edgeprob > 1) || (edgeprob < 0))
        {
            cout << "probabilities larger than 1 or smaller than 0 ...aborting..." << endl;
            return -1;
        }
        bool torus = (atoi(argv[6]) == 1);
        double radius = sqrt(edgeprob/3.14);
        if (direction == "directed")
        {
            while ((isConnected == 0) & (counts < maxcount))
            {
                igraph_grg_game(&graph, popSize,
                                radius, torus,
                                0, 0);
                igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG);
                counts++;
            }
        }
        else if (direction == "undirected")
        {
            while ((isConnected == 0) & (counts < maxcount))
            {
                igraph_grg_game(&graph, popSize,
                                radius, torus,
                                0, 0);
                igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG);
                counts++;
            }
        }
        else
        {
            cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl;
            return -1;
        }
        if (counts == maxcount)
        {
            cout << "Probability or number of edges too low... Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl;
            return -1;
        }
    }
//---------------------------- Code snippet for barabasi generator --------------------------------//
    else if(category == "BB")
    {
        double power = atof(argv[6]);
        int m = atoi(argv[7]);

        igraph_rng_seed(igraph_rng_default(), std::clock());
        igraph_bool_t isConnected = 0;
        if (direction == "directed")
        {
            cout << "directed Barabasi-Albert never creates connected graphs, use undirected instead! aborting..." << endl;
            return -1;

        }
        else if (direction == "undirected")
        {
            while ((isConnected == 0) & (counts < maxcount))
            {
                igraph_barabasi_game(&graph, popSize,
                                     power,
                                     m,
                                     0,
                                     0,
                                     1.0,
                                     false,
                                     IGRAPH_BARABASI_PSUMTREE,
                                     0);
                igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG);
                counts++;
            }
        }
        else
        {
            cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl;
            return -1;
        }
        if (counts == maxcount)
        {
            cout << "Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl;
            return -1;
        }
    }
    // ----------   Code snippet for custom graph   ----------
    else if(category == "custom")
    {
        std::string admats = argv[6];
        if (admats.size() != popSize*popSize)
        {
            cout << "adjacency matrix has the wrong size... aborting..." << endl;
            return -1;
        }
        std::vector<int> ints;
        std::transform(std::begin(admats), std::end(admats), std::back_inserter(ints),
                       [](char c)
        {
            return c - '0';
        }
                      );
        std::copy(ints.begin(), ints.end(), admat);
    }

    else
    {
        cout << "Only \"complete\", \"ER\", \"BB\", \"WS\", or \"geo\" as categories... aborting..." << endl;
        return -1;
    }


    // ----------   Here the adjacency matrix gets copied into an array  ----------

    if(category!="custom")
    {
        igraph_matrix_t admatv;
        igraph_matrix_init(&admatv, 0,0);
        igraph_get_adjacency( &graph, &admatv,IGRAPH_GET_ADJACENCY_BOTH,false);
        for(unsigned int i = 0 ; i < popSize ; i++)
        {
            for(unsigned int k = 0 ; k < popSize ; k++)
            {
                admat[ i*popSize + k] = MATRIX(admatv,i,k );
            }
        }

        igraph_destroy(&graph);
        igraph_matrix_destroy(&admatv);
    }
    for (unsigned int i=0; i<popSize; i++) {

        for (unsigned int j=0; j<popSize; j++) {
            // If you want to print the adjacency matrix:
            cout<<admat[i * popSize + j]<<" ";
        }
    }
    cout<<endl;
    t_vectorFP data;
    t_vectorInt row;
    t_vectorInt col;
    data.reserve(popSize * numStates);
    row.reserve(popSize * numStates);
    col.reserve(popSize * numStates);

    //CLOCK//
    start = std::clock();
    createTransitionMatrix(popSize, numStates, fitnessMutants, update, admat, data, row, col);


    std::vector<T> tripletList;
    tripletList.reserve(popSize * numStates);

    for( unsigned int j = 0 ; j < data.size() ; j++)
    {
        tripletList.push_back(T(col.at(j),row.at(j),data.at(j)));
    }

    SpMat mat(numStates,numStates);
    mat.setFromTriplets(tripletList.begin(), tripletList.end());

    // Stopping time after creating transition matrix
    //CLOCK//
    bt = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;

    //for (int i = 0; i<data.size(); i++)
    //    cout<<"unconditional: transition prob from state "<<row[i]<<" to state "<<col[i]<<" is "<<data[i]<<endl;
    string s1;



    /*   ----------   No distinguishing between "probability", "unconditional" time, and "conditional" time   ----------    */

    float * fixProbAllStates = static_cast<float*> (malloc(numStates * sizeof(float)));
    fixProb(mat, popSize, numStates, fixProbAllStates);

    // Stopping time after solving fixation probabilities
    //CLOCK//
    st = ( std::clock() - start) / (double) CLOCKS_PER_SEC - bt;

    float probOne = 0.0;
    for(unsigned int i = 0; i < popSize; i++)
    {
        int j = 1 << i;

        probOne = probOne + fixProbAllStates[j];

    }
    probOne = probOne / (float)(popSize);

    cout << "fixation probability:" << probOne << endl;
    /*   ----------   Printing the fixation probability starting from all states   ----------    */
    /*
    for(unsigned int i = 0; i < numStates; i++)
    {
        bitset<23> b1(i);
        s1 =  b1.to_string();
        cout<<"fixation probability in state ";
        cout<< s1.substr(23-popSize,popSize);
        cout <<" is "<<fixProbAllStates[i]<<endl;
    }
    */
    if((output == "unconditional")||(output == "all"))
    {
        float * uncondFixTimeAllStates = static_cast<float*> (malloc(numStates * sizeof(float)));
        // Stopping the time for solving for unconditional fixation time
        //CLOCK// start = std::clock();
        //CLOCK// bt = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
        time(mat, popSize, numStates, uncondFixTimeAllStates);
        //CLOCK//

        float avUncondTime = 0.0;
        for(unsigned int i = 0 ; i < popSize ; i++)
        {
            int j = 1 << i;
            avUncondTime = avUncondTime + uncondFixTimeAllStates[j];
        }
        avUncondTime = avUncondTime / (float)(popSize);

        free(uncondFixTimeAllStates);

        cout<< "unconditional fixation time:" << avUncondTime << endl;
    }
    /*   ----------   Printing the average unconditional fixation time starting from all states   ----------    */

    //for(unsigned int i = 0; i < numStates; i++)
    //{
    //    bitset<23> b1(i);
    //    s1 =  b1.to_string();
    //cout<<"Unconditional fixation time in state ";
    //cout<< s1.substr (23-popSize,popSize);
    //cout <<" is "<<uncondFixTimeAllStates[i]<<endl;
    //}

    //float * fixProbAllStates = (float*) malloc(numStates * sizeof(float));
    //fixProb(mat, popSize, numStates, fixProbAllStates);
    if((output == "conditional")||(output == "all"))
    {
        createConditionalTransitionMatrix(popSize, numStates, fixProbAllStates, data, row, col);

        std::vector<T> tripletListCond;
        tripletListCond.reserve(popSize * numStates);

        for( unsigned int j = 0 ; j < data.size() ; j++)
        {
            tripletListCond.push_back(T(col.at(j),row.at(j),data.at(j)));
        }

        SpMat conditionalMatrix(numStates,numStates);
        conditionalMatrix.setFromTriplets(tripletListCond.begin(), tripletListCond.end());


        float * condFixTimeAllStates = static_cast<float*> (malloc(numStates * sizeof(float)));
        time(conditionalMatrix, popSize, numStates, condFixTimeAllStates);


        float avCondTime = 0.0;
        for(unsigned int i = 0 ; i < popSize ; i++)
        {
            int j = 1 << i;
            avCondTime = avCondTime + condFixTimeAllStates[j];
        }
        avCondTime = avCondTime / (float)(popSize);

        free(condFixTimeAllStates);
        cout << "conditional fixation time:" << avCondTime << endl;
    }

    free(fixProbAllStates);
    /*   ----------   Printing the average conditional fixation time starting from all states   ----------    */

    //for(unsigned int i = 0; i < numStates; i++)
    //{
    //bitset<23> b1(i);
    //s1 =  b1.to_string();
    //cout<<"Conditional fixation time in state ";
    //cout<< s1.substr (23-popSize,popSize);
    //cout <<" is "<<condFixTimeAllStates[i]<<endl;
    //}

    st = ( std::clock() - start) / (double) CLOCKS_PER_SEC - bt;
    //CLOCK//
    cout<<"building time: "<< bt <<'\n';
    //CLOCK//
    cout<<"solving time: "<< st << "\n\n";

}