int main() { igraph_t g; igraph_vs_t vertices; igraph_vector_t result1, result2; igraph_rng_seed(igraph_rng_default(), 42); igraph_vector_init(&result1, 0); igraph_vector_init(&result2, 0); igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNP, 100, .1, IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS); igraph_vs_seq(&vertices, 0, 99); igraph_transitivity_local_undirected(&g, &result1, igraph_vss_all(), IGRAPH_TRANSITIVITY_NAN); igraph_transitivity_local_undirected(&g, &result2, vertices, IGRAPH_TRANSITIVITY_NAN); if (!igraph_vector_all_e(&result1, &result2)) { igraph_vector_print(&result1); igraph_vector_print(&result2); return 1; } igraph_vector_destroy(&result1); igraph_vector_destroy(&result2); igraph_vs_destroy(&vertices); igraph_destroy(&g); return 0; }
int main(void) { igraph_integer_t diameter; igraph_t graph; igraph_rng_seed(igraph_rng_default(), 42); igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP, 1000, 5.0/1000, IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS); igraph_diameter(&graph, &diameter, 0, 0, 0, IGRAPH_UNDIRECTED, 1); printf("Diameter of a random graph with average degree 5: %d\n", (int) diameter); igraph_destroy(&graph); return 0; }
int main() { igraph_t g; igraph_vector_ptr_t res; igraph_integer_t i, n; igraph_rng_seed(igraph_rng_default(), 42); igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNM, 100, 3000, /* directed = */ 0, /* loops= */ 0); igraph_vector_ptr_init(&res, 0); BENCH("1 Cliques in random graph with 100 vertices and 3000 edges", igraph_cliques(&g, &res, /* min_size= */ 0, /* max_size= */ 0); );
/* call-seq: * IGraph::GenerateRandom.erdos_renyi_game(type,n,p_or_m,directed,loops) -> IGraph * * Generates a random (Erdos-Renyi) graph. * * type: The type of the random graph, possible values: * * IGraph::ERDOS_RENYI_GNM - G(n,m) graph, m edges are selected uniformly * randomly in a graph with n vertices. * * IGraph::ERDOS_RENYI_GNP - G(n,p) graph, every possible edge is included in * the graph with probability p. * * n: The number of vertices in the graph. * * p_or_m: This is the p parameter for G(n,p) graphs and the m parameter * for G(n,m) graphs. * * directed: Logical, whether to generate a directed graph. * * loops: Logical, whether to generate loops (self) edges. */ VALUE cIGraph_erdos_renyi_game(VALUE self, VALUE type, VALUE nodes, VALUE mp, VALUE directed, VALUE loops) { igraph_t *graph; VALUE new_graph; new_graph = cIGraph_alloc(cIGraph); Data_Get_Struct(new_graph, igraph_t, graph); igraph_destroy(graph); igraph_erdos_renyi_game(graph, NUM2INT(type), NUM2INT(nodes), NUM2DBL(mp), directed == Qtrue ? 1: 0, loops == Qtrue ? 1: 0); return new_graph; }
int main() { igraph_t graph; igraph_matrix_t coords; int i; igraph_matrix_init(&coords, 0, 0); for (i=0; i<10; i++) { igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP, /*n=*/ 100, /*p=*/ 2.0/100, IGRAPH_UNDIRECTED, /*loops=*/ 0); igraph_layout_mds(&graph, &coords, /*dist=*/ 0, /*dim=*/ 2, /*options=*/ 0); igraph_destroy(&graph); } igraph_matrix_destroy(&coords); return 0; }
int main() { igraph_t graph; igraph_vector_ptr_t cliques; igraph_rng_t rng; igraph_rng_init(&rng, &igraph_rngtype_mt19937); igraph_rng_seed(&rng, 42); igraph_rng_seed(igraph_rng_default(), 42); igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP, /*n=*/ 100, /*p=*/ 0.7, /*directed=*/ 0, /*loops=*/ 0, &rng); igraph_vector_ptr_init(&cliques, 0); igraph_maximal_cliques(&graph, &cliques, /*min_size=*/ 15, /*max_size=*/ 0); print_and_destroy(&cliques); igraph_destroy(&graph); igraph_rng_destroy(&rng); return 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"; }
Graph::Graph(int size, double p) : size(size){ graph = new igraph_t; igraph_erdos_renyi_game(graph, IGRAPH_ERDOS_RENYI_GNP, size, p, 0, 0); }
int main() { igraph_t g, g2, cli; igraph_vector_t perm; igraph_vector_ptr_t cliques; igraph_integer_t no; int i; igraph_rng_seed(igraph_rng_default(), 42); /* Create a graph that has a random component, plus a number of relatively small cliques */ igraph_vector_init_seq(&perm, 0, NODES-1); igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNM, NODES, NODES, /*directed=*/ 0, /*loops=*/ 0, igraph_rng_default()); igraph_full(&cli, CLIQUE_SIZE, /*directed=*/ 0, /*loops=*/ 0); for (i=0; i<NO_CLIQUES; i++) { /* Permute vertices of g */ permutation(&perm); igraph_permute_vertices(&g, &g2, &perm); igraph_destroy(&g); g=g2; /* Add a clique */ igraph_union(&g2, &g, &cli, /*edge_map1=*/ 0, /*edge_map2=*/ 0); igraph_destroy(&g); g=g2; } igraph_simplify(&g, /*multiple=*/ 1, /*loop=*/ 0, /*edge_comb=*/ 0); igraph_vector_destroy(&perm); igraph_destroy(&cli); /* Find the maximal cliques */ igraph_vector_ptr_init(&cliques, 0); igraph_maximal_cliques(&g, &cliques, /*min_size=*/ 3, /*max_size=*/ 0 /*no limit*/); igraph_maximal_cliques_count(&g, &no, /*min_size=*/ 3, /*max_size=*/ 0 /*no limit*/); if (no != igraph_vector_ptr_size(&cliques)) { return 1; } /* Print and destroy them */ print_and_destroy_cliques(&cliques); /* Clean up */ igraph_vector_ptr_destroy(&cliques); igraph_destroy(&g); /* Build a triangle with a loop (thanks to Emmanuel Navarro) */ igraph_small(&g, 3, IGRAPH_UNDIRECTED, 0, 1, 1, 2, 2, 0, 0, 0, -1); /* Find the maximal cliques */ igraph_vector_ptr_init(&cliques, 0); igraph_maximal_cliques(&g, &cliques, /*min_size=*/ 3, /*max_size=*/ 0 /*no limit*/); igraph_maximal_cliques_count(&g, &no, /*min_size=*/ 3, /*max_size=*/ 0 /*no limit*/); if (no != igraph_vector_ptr_size(&cliques)) { return 2; } /* Print and destroy them */ print_and_destroy_cliques(&cliques); /* Clean up */ igraph_vector_ptr_destroy(&cliques); igraph_destroy(&g); return 0; }
int check_simple() { igraph_t g; long int nodes=100; long int edges=1000; igraph_real_t p=3.0/nodes; long int runs=10; long int r, e, ecount; igraph_vector_t eids, pairs, path; srand(time(0)); igraph_vector_init(&pairs, edges*2); igraph_vector_init(&path, 0); igraph_vector_init(&eids, 0); for (r=0; r<runs; r++) { igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNP, nodes, p, /*directed=*/ 0, /*loops=*/ 0); ecount=igraph_ecount(&g); for (e=0; e<edges; e++) { long int edge=RNG_INTEGER(0, ecount-1); VECTOR(pairs)[2*e] = IGRAPH_FROM(&g, edge); VECTOR(pairs)[2*e+1] = IGRAPH_TO(&g, edge); } igraph_get_eids(&g, &eids, &pairs, /*path=*/ 0, 0, /*error=*/ 1); for (e=0; e<edges; e++) { long int edge=VECTOR(eids)[e]; long int from1=VECTOR(pairs)[2*e]; long int to1=VECTOR(pairs)[2*e+1]; long int from2=IGRAPH_FROM(&g, edge); long int to2=IGRAPH_TO(&g, edge); long int min1= from1 < to1 ? from1 : to1; long int max1= from1 < to1 ? to1 : from1; long int min2= from2 < to2 ? from2 : to2; long int max2= from2 < to2 ? to2 : from2; if (min1 != min2 || max1 != max2) { return 11; } } igraph_diameter(&g, /*res=*/ 0, /*from=*/ 0, /*to=*/ 0, &path, IGRAPH_UNDIRECTED, /*unconn=*/ 1); igraph_get_eids(&g, &eids, /*pairs=*/ 0, &path, 0, /*error=*/ 1); for (e=0; e<igraph_vector_size(&path)-1; e++) { long int edge=VECTOR(eids)[e]; long int from1=VECTOR(path)[e]; long int to1=VECTOR(path)[e+1]; long int from2=IGRAPH_FROM(&g, edge); long int to2=IGRAPH_TO(&g, edge); long int min1= from1 < to1 ? from1 : to1; long int max1= from1 < to1 ? to1 : from1; long int min2= from2 < to2 ? from2 : to2; long int max2= from2 < to2 ? to2 : from2; if (min1 != min2 || max1 != max2) { return 12; } } igraph_destroy(&g); } igraph_vector_destroy(&path); igraph_vector_destroy(&pairs); igraph_vector_destroy(&eids); return 0; }