コード例 #1
0
int main() {
  
  igraph_t g;
  igraph_vector_t v, weights;
  long int i;
  igraph_real_t value;
  igraph_arpack_options_t options;
  
  igraph_star(&g, 100, IGRAPH_STAR_UNDIRECTED, 0);

  igraph_arpack_options_init(&options);
  igraph_vector_init(&v, 0);
  igraph_eigenvector_centrality(&g, &v, &value, /*directed=*/ 0, 
				/*scale=*/0, /*weights=*/0, 
				&options);

  if (options.info != 0) {
    return 1;
  }

  for (i=0; i<igraph_vector_size(&v); i++) {
    printf(" %.3f", fabs(VECTOR(v)[i]));
  }
  printf("\n");
  
  igraph_destroy(&g);

  /* Special cases: check for empty graph */
  igraph_empty(&g, 10, 0);
  igraph_eigenvector_centrality(&g, &v, &value, 0, 0, 0, &options);
  if (value != 0.0) {
    return 1;
  }
  for (i=0; i<igraph_vector_size(&v); i++) {
    printf(" %.2f", fabs(VECTOR(v)[i]));
  }
  printf("\n");
  igraph_destroy(&g);

  /* Special cases: check for full graph, zero weights */
  igraph_full(&g, 10, 0, 0);
  igraph_vector_init(&weights, 45);
  igraph_vector_fill(&weights, 0);
  igraph_eigenvector_centrality(&g, &v, &value, 0, 0, &weights, &options);
  igraph_vector_destroy(&weights);
  if (value != 0.0) {
    return 2;
  }
  for (i=0; i<igraph_vector_size(&v); i++) {
    printf(" %.2f", fabs(VECTOR(v)[i]));
  }
  printf("\n");
  igraph_destroy(&g);

  igraph_vector_destroy(&v);

  return 0;
}
コード例 #2
0
ファイル: igraph_cliques.c プロジェクト: igraph/igraph
int main() {
  
  igraph_t g;
  igraph_vector_ptr_t result;
  igraph_es_t es;
  igraph_integer_t omega;
  long int i, j, n;
  const int params[] = {4, -1, 2, 2, 0, 0, -1, -1};
 
  igraph_set_warning_handler(warning_handler_ignore);

  igraph_vector_ptr_init(&result, 0);
  igraph_full(&g, 6, 0, 0);
  igraph_es_pairs_small(&es, 0, 0, 1, 0, 2, 3, 5, -1);
  igraph_delete_edges(&g, es);
  igraph_es_destroy(&es);
  
  for (j=0; j<sizeof(params)/(2*sizeof(params[0])); j++) {
    if (params[2*j+1] != 0) {
      igraph_cliques(&g, &result, params[2*j], params[2*j+1]);  
    } else {
      igraph_largest_cliques(&g, &result);
    }
    n = igraph_vector_ptr_size(&result);
    printf("%ld cliques found\n", (long)n);
    canonicalize_list(&result);
    for (i=0; i<n; i++) {
      igraph_vector_t* v = (igraph_vector_t*) igraph_vector_ptr_e(&result,i);
      print_vector(v);
      igraph_vector_destroy(v);
      free(v);
    }
  }
   
  igraph_clique_number(&g, &omega);
  printf("omega=%ld\n", (long)omega);

  test_callback(&g);

  igraph_destroy(&g);

  igraph_tree(&g, 5, 2, IGRAPH_TREE_OUT);
  igraph_cliques(&g, &result, 5, 5);
  if (igraph_vector_ptr_size(&result) != 0) return 1;

  igraph_destroy(&g);
  igraph_vector_ptr_destroy(&result);

  return 0;
}
コード例 #3
0
ファイル: igraph_hrg.c プロジェクト: AlessiaWent/igraph
int main() {
  igraph_t graph;
  igraph_t full, tree;
  igraph_hrg_t hrg;
  igraph_t dendrogram;
  // int i, j;
  // igraph_vector_t neis;

  igraph_rng_seed(igraph_rng_default(), 42);

  // We need attributes
  igraph_i_set_attribute_table(&igraph_cattribute_table);
  
  igraph_full(&full, 10, /*directed=*/ 0, /*loops=*/ 0);
  igraph_tree(&tree, 15, /*children=*/ 2, /*type=*/ IGRAPH_TREE_UNDIRECTED);
  igraph_disjoint_union(&graph, &full, &tree);
  igraph_add_edge(&graph, 0, 10);
  
  igraph_destroy(&full);
  igraph_destroy(&tree);

  // Fit
  igraph_hrg_init(&hrg, igraph_vcount(&graph));
  igraph_hrg_fit(&graph, &hrg, /*start=*/ 0, /*steps=*/ 0);

  // Create a graph from it
  igraph_hrg_dendrogram(&dendrogram, &hrg);

  // Print the tree, with labels
  // igraph_vector_init(&neis, 0);
  // for (i=0; i<igraph_vcount(&graph)-1; i++) {
  //   printf("Vertex # %2i, ", (int) (i+igraph_vcount(&graph)));
  //   igraph_neighbors(&dendrogram, &neis, i+igraph_vcount(&graph), IGRAPH_OUT);
  //   printf("left: # %2i, right: # %2i, ", (int) VECTOR(neis)[0], 
  // 	   (int) VECTOR(neis)[1]);
  //   printf("prob: %6.2g\n", 
  // 	   VAN(&dendrogram, "probability", i+igraph_vcount(&graph)));
  // }
  // igraph_vector_destroy(&neis);

  igraph_destroy(&dendrogram);
  igraph_hrg_destroy(&hrg);
  igraph_destroy(&graph);

  return 0;
}
コード例 #4
0
ファイル: igraph_pagerank.c プロジェクト: AlessiaWent/igraph
int main() {

  igraph_t g;
  igraph_vector_t v, res, reset, weights;
  igraph_arpack_options_t arpack_options;
  igraph_real_t value;
  int ret;
  igraph_pagerank_power_options_t power_options;

  /* Test graphs taken from http://www.iprcom.com/papers/pagerank/ */
  igraph_vector_init(&v, 10);
  VECTOR(v)[0]=0; VECTOR(v)[1]=1;
  VECTOR(v)[2]=1; VECTOR(v)[3]=2;
  VECTOR(v)[4]=2; VECTOR(v)[5]=0;
  VECTOR(v)[6]=3; VECTOR(v)[7]=2;
  VECTOR(v)[8]=0; VECTOR(v)[9]=2;
  igraph_create(&g, &v, 0, 1);

  igraph_vector_init(&res, 0);
  oldwarn=igraph_set_warning_handler(warning_handler_stdout);
  igraph_pagerank_old(&g, &res, igraph_vss_all(), 1, 1000, 0.001, 0.85, 0);
  print_vector(&res, stdout);
  igraph_vector_destroy(&res);
  igraph_vector_destroy(&v);
  
  igraph_destroy(&g);

  igraph_vector_init(&v, 28);
  VECTOR(v)[ 0]=0; VECTOR(v)[ 1]=1;
  VECTOR(v)[ 2]=0; VECTOR(v)[ 3]=2;
  VECTOR(v)[ 4]=0; VECTOR(v)[ 5]=3;
  VECTOR(v)[ 6]=1; VECTOR(v)[ 7]=0;
  VECTOR(v)[ 8]=2; VECTOR(v)[ 9]=0;
  VECTOR(v)[10]=3; VECTOR(v)[11]=0;
  VECTOR(v)[12]=3; VECTOR(v)[13]=4;
  VECTOR(v)[14]=3; VECTOR(v)[15]=5;
  VECTOR(v)[16]=3; VECTOR(v)[17]=6;
  VECTOR(v)[18]=3; VECTOR(v)[19]=7;
  VECTOR(v)[20]=4; VECTOR(v)[21]=0;
  VECTOR(v)[22]=5; VECTOR(v)[23]=0;
  VECTOR(v)[24]=6; VECTOR(v)[25]=0;
  VECTOR(v)[26]=7; VECTOR(v)[27]=0;
  igraph_create(&g, &v, 0, 1);

  igraph_vector_init(&res, 0);
  igraph_pagerank_old(&g, &res, igraph_vss_all(), 1, 10000, 0.0001, 0.85, 0);
  print_vector(&res, stdout);
  igraph_vector_destroy(&res);
  igraph_vector_destroy(&v);
  igraph_destroy(&g);

  igraph_set_warning_handler(oldwarn);

  /* New PageRank */
  igraph_star(&g, 11, IGRAPH_STAR_UNDIRECTED, 0);
  igraph_vector_init(&res, 0);
  igraph_arpack_options_init(&arpack_options);
  igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0,
		  igraph_vss_all(), 0, 0.85, 0, &arpack_options);
  print_vector(&res, stdout);
  igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0,
		  igraph_vss_all(), 0, 0.85, 0, 0);
  print_vector(&res, stdout);
  /* Check twice more for consistency */
  igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0,
		  igraph_vss_all(), 0, 0.85, 0, &arpack_options);
  print_vector(&res, stdout);
  igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0,
		  igraph_vss_all(), 0, 0.85, 0, 0);
  print_vector(&res, stdout);

  igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0,
		  igraph_vss_all(), 0, 0.85, 0, &arpack_options);
  print_vector(&res, stdout);
  igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0,
		  igraph_vss_all(), 0, 0.85, 0, 0);
  print_vector(&res, stdout);

  /* Check personalized PageRank */
  igraph_personalized_pagerank_vs(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0,
				  igraph_vss_all(), 0, 0.5,
				  igraph_vss_1(1), 0, &arpack_options);
  print_vector(&res, stdout);
  igraph_personalized_pagerank_vs(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0,
				  igraph_vss_all(), 0, 0.5,
				  igraph_vss_1(1), 0, 0);
  print_vector(&res, stdout);

  /* Errors */
  power_options.niter = -1; power_options.eps=0.0001;
  igraph_set_error_handler(igraph_error_handler_ignore);
  igraph_set_warning_handler(igraph_warning_handler_ignore);
  ret=igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_POWER, &res,
		      /*value=*/ 0, igraph_vss_all(), 1, 0.85,
		      /*weights=*/ 0, &power_options);
  if (ret != IGRAPH_EINVAL) {
    return 1;
  }
  
  power_options.niter=10000; power_options.eps=-1;
  ret=igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_POWER, &res,
		      /*value=*/ 0, igraph_vss_all(), 1, 0.85,
		      /*weights=*/ 0, &power_options);
  if (ret != IGRAPH_EINVAL) {
    return 2;
  }

  power_options.niter=10000; power_options.eps=0.0001;
  ret=igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_POWER, &res,
		      /*value=*/ 0, igraph_vss_all(), 1, 1.2,
		      /*weights=*/ 0, &power_options);
  if (ret != IGRAPH_EINVAL) {
    return 3;
  }

  igraph_vector_init(&reset, 2);
  ret=igraph_personalized_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0,
				   igraph_vss_all(), 0, 0.85, &reset, 0,
				   &arpack_options);
  if (ret != IGRAPH_EINVAL) {
    return 4;
  }
  ret=igraph_personalized_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0,
				   igraph_vss_all(), 0, 0.85, &reset, 0, 0);
  if (ret != IGRAPH_EINVAL) {
    return 4;
  }
  igraph_vector_resize(&reset, 10);
  igraph_vector_fill(&reset, 0);
  ret=igraph_personalized_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK,
				   &res, 0, igraph_vss_all(), 0, 0.85,
				   &reset, 0, &arpack_options);
  if (ret != IGRAPH_EINVAL) {
    return 5;
  }
  ret=igraph_personalized_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK,
				   &res, 0, igraph_vss_all(), 0, 0.85,
				   &reset, 0, 0);
  if (ret != IGRAPH_EINVAL) {
    return 5;
  }
  igraph_vector_destroy(&reset);
  igraph_destroy(&g);
  igraph_set_error_handler(igraph_error_handler_abort);

  /* Special cases: check for empty graph */
  igraph_empty(&g, 10, 0);
  igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, &value,
		  igraph_vss_all(), 1, 0.85, 0, &arpack_options);
  if (value != 1.0) {
    return 6;
  }
  igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, &value,
		  igraph_vss_all(), 1, 0.85, 0, 0);
  if (value != 1.0) {
    return 6;
  }
  print_vector(&res, stdout);
  igraph_destroy(&g);

  /* Special cases: check for full graph, zero weights */
  igraph_full(&g, 10, 0, 0);
  igraph_vector_init(&v, 45);
  igraph_vector_fill(&v, 0);
  igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, &value,
		  igraph_vss_all(), 1, 0.85, &v, &arpack_options);
  if (value != 1.0) {
    return 7;
  }
  igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, &value,
		  igraph_vss_all(), 1, 0.85, &v, 0);
  if (value != 1.0) {
    return 7;
  }
  igraph_vector_destroy(&v);
  print_vector(&res, stdout);
  igraph_destroy(&g);

  /* Another test case for PageRank (bug #792352) */
  igraph_small(&g, 9, 1, 0, 5, 1, 5, 2, 0, 3, 1, 5, 4, 5, 7, 6, 0, 8, 0, 8, 1, -1);
  igraph_vector_init(&weights, 9);
  VECTOR(weights)[0] = 4; VECTOR(weights)[1] = 5; VECTOR(weights)[2] = 5;
  VECTOR(weights)[3] = 4; VECTOR(weights)[4] = 4; VECTOR(weights)[5] = 4;
  VECTOR(weights)[6] = 3; VECTOR(weights)[7] = 4; VECTOR(weights)[8] = 4;
  igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_ARPACK, &res, 0,
		  igraph_vss_all(), 1, 0.85, &weights, &arpack_options);
  print_vector(&res, stdout);
  igraph_pagerank(&g, IGRAPH_PAGERANK_ALGO_PRPACK, &res, 0,
		  igraph_vss_all(), 1, 0.85, &weights, 0);
  print_vector(&res, stdout);
  igraph_vector_destroy(&weights);
  igraph_destroy(&g);

  igraph_vector_destroy(&res);
  return 0;
}
コード例 #5
0
ファイル: main.cpp プロジェクト: hindersin/efficientFixation
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";

}
コード例 #6
0
ファイル: Graph.cpp プロジェクト: rcalsaverini/AuthCPP
Graph::Graph(int size) : size(size) {
  graph = new igraph_t;
  igraph_full(graph, size, 0, 0);
}
コード例 #7
0
int main() {
  igraph_t g, g2;
  igraph_vector_ptr_t sep;
  igraph_vs_t vs;
  
  igraph_small(&g, 7, IGRAPH_UNDIRECTED,
	       1,0, 2,0, 3,0, 4,0, 5,0, 6,0,
	       -1);
  igraph_vector_ptr_init(&sep, 0);
  igraph_minimum_size_separators(&g, &sep);
  print_and_destroy(&sep);
  igraph_destroy(&g);

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

  igraph_small(&g, 5, IGRAPH_UNDIRECTED,
	       0,3, 1,3, 2,3,
	       0,4, 1,4, 2,4,
	       -1);
  igraph_vector_ptr_init(&sep, 0);
  igraph_minimum_size_separators(&g, &sep);
  print_and_destroy(&sep);
  igraph_destroy(&g);

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

  igraph_small(&g, 5, IGRAPH_UNDIRECTED,
	       2,0, 3,0, 4,0,
	       2,1, 3,1, 4,1,
	       -1);
  igraph_vector_ptr_init(&sep, 0);
  igraph_minimum_size_separators(&g, &sep);
  print_and_destroy(&sep);
  igraph_destroy(&g);

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

  igraph_small(&g, 10, IGRAPH_UNDIRECTED,
	       0,2, 0,3, 1,2, 1,3, 5,2, 5,3, 6,2, 6,3, 
	       7,2, 7,3, 8,2, 8,3, 9,2, 9,3,
	       2,4, 4,3,
	       -1);
  igraph_vector_ptr_init(&sep, 0);
  igraph_minimum_size_separators(&g, &sep);
  print_and_destroy(&sep);
  igraph_destroy(&g);  

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

  igraph_full(&g, 4, IGRAPH_UNDIRECTED, /*loops=*/ 0);
  igraph_vector_ptr_init(&sep, 0);
  igraph_minimum_size_separators(&g, &sep);
  print_and_destroy(&sep);
  igraph_destroy(&g);  

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

  igraph_small(&g, 23, IGRAPH_UNDIRECTED,
	       0,1, 0,2, 0,3, 0,4, 0,5,
	       1,2, 1,3, 1,4, 1,6,
	       2,3, 2,5, 2,6,
	       3,4, 3,5, 3,6,
	       4,5, 4,6, 4,20,
	       5,6, 
	       6,7, 6,10, 6,13, 6,18,
	       7,8, 7,10, 7,13,
	       8,9,
	       9,11, 9,12,
	       10,11, 10,13,
	       11,15,
	       12,15,
	       13,14,
	       14,15,
	       16,17, 16,18, 16,19,
	       17,19, 17,20,
	       18,19, 18,21, 18,22,
	       19,20,
	       20,21, 20,22,
	       21,22,
	       -1);

  igraph_vector_ptr_init(&sep, 0);
  igraph_minimum_size_separators(&g, &sep);
  printf("Orig:\n"); print_and_destroy(&sep);

  igraph_vector_ptr_init(&sep, 0);
  igraph_vs_vector_small(&vs, 0,1,2,3,4,5,6, 16,17,18,19,20,21,22, -1);
  igraph_induced_subgraph(&g, &g2, vs, IGRAPH_SUBGRAPH_AUTO);
  igraph_minimum_size_separators(&g2, &sep);
  printf("1-7,17-23:\n"); print_and_destroy(&sep);
  igraph_vs_destroy(&vs);
  igraph_destroy(&g2);
  
  igraph_vector_ptr_init(&sep, 0);
  igraph_vs_vector_small(&vs, 6,7,8,9,10,11,12,13,14,15, -1);
  igraph_induced_subgraph(&g, &g2, vs, IGRAPH_SUBGRAPH_AUTO);
  igraph_minimum_size_separators(&g2, &sep);
  printf("7-16:\n"); print_and_destroy(&sep);
  igraph_vs_destroy(&vs);
  igraph_destroy(&g2);
  
  igraph_vector_ptr_init(&sep, 0);
  igraph_vs_vector_small(&vs, 16,17,18,19,20,21,22, -1);
  igraph_induced_subgraph(&g, &g2, vs, IGRAPH_SUBGRAPH_AUTO);
  igraph_minimum_size_separators(&g2, &sep);
  printf("17-23:\n"); print_and_destroy(&sep);
  igraph_vs_destroy(&vs);
  igraph_destroy(&g2);
  
  igraph_vector_ptr_init(&sep, 0);
  igraph_vs_vector_small(&vs, 6,7,10,13, -1);
  igraph_induced_subgraph(&g, &g2, vs, IGRAPH_SUBGRAPH_AUTO);
  igraph_minimum_size_separators(&g2, &sep);
  printf("7,8,11,14:\n"); print_and_destroy(&sep);
  igraph_vs_destroy(&vs);
  igraph_destroy(&g2);

  igraph_vector_ptr_init(&sep, 0);
  igraph_vs_vector_small(&vs, 0,1,2,3,4,5,6, -1);
  igraph_induced_subgraph(&g, &g2, vs, IGRAPH_SUBGRAPH_AUTO);
  igraph_minimum_size_separators(&g2, &sep);
  printf("1-7:\n"); print_and_destroy(&sep);
  igraph_vs_destroy(&vs);
  igraph_destroy(&g2);  

  igraph_destroy(&g);  

  return 0;
}
コード例 #8
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;
}
コード例 #9
0
ファイル: rvine.c プロジェクト: yasserglez/dml
static void
fit_rvine_trees(igraph_t **trees,
                const gsl_matrix *data,
                const dml_vine_weight_t weight,
                const dml_vine_trunc_t trunc,
                const dml_copula_indeptest_t indeptest,
                const double indeptest_level,
                const dml_copula_type_t *types,
                const size_t types_size,
                const dml_copula_select_t select,
                const gsl_rng *rng)
{
    size_t m, n;
    igraph_t *graph;
    igraph_vector_t *graph_weight;
    dml_copula_t *copula;
    gsl_vector *x;
    igraph_integer_t e; // Edge id.
    igraph_integer_t a, aa, ab, b, ba, bb; // Vertex id.
    gsl_vector *u = NULL, *v = NULL;
    igraph_integer_t Cea, Ceb;
    gsl_vector_short *Ue, *Ua, *Ub;
    size_t k;
    dml_measure_t *measure;
    double tree_aic, copula_aic;
    gsl_permutation *perm, *rank, *u_rank = NULL, *v_rank = NULL;

    igraph_i_set_attribute_table(&igraph_cattribute_table);

    m = data->size1;
    n = data->size2;
    graph = g_malloc(sizeof(igraph_t));
    graph_weight = g_malloc(sizeof(igraph_vector_t));
    perm = gsl_permutation_alloc(m);

    for (k = 0; k < n - 1; k++) { // Tree index.
        if (k == 0) {
            igraph_full(graph, n, IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS);

            // Assign the observations to the nodes.
            for (size_t i = 0; i < n; i++) { // Variable and node index.
                x = gsl_vector_alloc(m);
                gsl_matrix_get_col(x, data, i);

                // Results of the h-function of the copula assigned to the
                // edge that corresponds to this vertex in the previous tree.
                // h for the h-function with its arguments in order and
                // hrev for the h-function with its arguments reversed. In the
                // first tree both are equal to the observations of the
                // corresponding variable, in the rest of the trees they differ.
                SETVAP(graph, "h", i, x);
                SETVAP(graph, "hrev", i, x);
                gsl_sort_vector_index(perm, x);
                rank = gsl_permutation_alloc(m);
                gsl_permutation_inverse(rank, perm);
                // Ranks of the h and hrev vectors.
                SETVAP(graph, "hrank", i, rank);
                SETVAP(graph, "hrevrank", i, rank);
            }

            for (e = 0; e < igraph_ecount(graph); e++) {
                igraph_edge(graph, e, &a, &b);

                // Variables "connected" by this edge.
                Ue = gsl_vector_short_calloc(n);
                gsl_vector_short_set(Ue, a, 1);
                gsl_vector_short_set(Ue, b, 1);
                SETEAP(graph, "Ue", e, Ue);

                // Conditioned set.
                SETEAN(graph, "Cea", e, a + 1);
                SETEAN(graph, "Ceb", e, b + 1);
                Cea = EAN(graph, "Cea", e);
                Ceb = EAN(graph, "Ceb", e);

                // Calculate the weight of the edge.
                u = VAP(graph, "h", a);
                v = VAP(graph, "h", b);
                u_rank = VAP(graph, "hrank", a);
                v_rank = VAP(graph, "hrank", b);
                // The conditioned set is ordered to make the order of the
                // arguments in the bivariate copulas unique as suggested in
                // Czado, C. (2010) Pair-Copula Constructions of Multivariate
                // Copulas. In Jaworski, P. and Durante, F. and Hardle, W. K.
                // and Rychlik, T. (eds.) Copula Theory and Its Applications,
                // Springer-Verlag, 93-109.
                if (Cea < Ceb) {
                    rvine_set_weight(graph, weight, e, u, v, u_rank, v_rank);
                } else {
                    rvine_set_weight(graph, weight, e, v, u, v_rank, u_rank);
                }
            }
        } else {
            igraph_empty(graph, n - k, IGRAPH_UNDIRECTED);

            // Adding all "possible" edges.
            for (a = 0; a < igraph_vcount(graph) - 1; a++) {
                for (b = a + 1; b < igraph_vcount(graph); b++) {
                    igraph_edge(trees[k - 1], a, &aa, &ab);
                    igraph_edge(trees[k - 1], b, &ba, &bb);

                    // Checking the proximity condition.
                    if (aa == ba || aa == bb || ab == ba || ab == bb) {
                        igraph_add_edge(graph, a, b);
                        igraph_get_eid(graph, &e, a, b, IGRAPH_UNDIRECTED, 1);

                        // Variables "connected" by this edge and conditioned set.
                        Ua = EAP(trees[k - 1], "Ue", a);
                        Ub = EAP(trees[k - 1], "Ue", b);
                        Ue = gsl_vector_short_calloc(n);
                        for (size_t i = 0; i < n; i++) {
                            gsl_vector_short_set(Ue, i,
                                    gsl_vector_short_get(Ua, i)
                                            | gsl_vector_short_get(Ub, i));
                            if (gsl_vector_short_get(Ua, i)
                                    && !gsl_vector_short_get(Ub, i)) {
                                SETEAN(graph, "Cea", e, i + 1);
                            }
                            if (gsl_vector_short_get(Ub, i)
                                    && !gsl_vector_short_get(Ua, i)) {
                                SETEAN(graph, "Ceb", e, i + 1);
                            }
                        }
                        SETEAP(graph, "Ue", e, Ue);
                    }
                }
            }

            // Compute pseudo-observations and edge weights.
            for (a = 0; a < igraph_vcount(graph); a++) {
                // See the comment in the code for the first tree.
                SETVAP(graph, "h", a, NULL);
                SETVAP(graph, "hrev", a, NULL);
                SETVAP(graph, "hrank", a, NULL);
                SETVAP(graph, "hrevrank", a, NULL);
            }
            for (e = 0; e < igraph_ecount(graph); e++) {
                igraph_edge(graph, e, &a, &b);
                Cea = EAN(graph, "Cea", e);
                Ceb = EAN(graph, "Ceb", e);

                // Assign u and u_rank.
                if ((Cea == EAN(trees[k - 1], "Cea", a)
                        && (EAN(trees[k - 1], "Cea", a)
                                < EAN(trees[k - 1], "Ceb", a)))
                        || (Cea != EAN(trees[k - 1], "Cea", a)
                                && (EAN(trees[k - 1], "Cea", a)
                                        > EAN(trees[k - 1], "Ceb", a)))) {
                    u = VAP(graph, "h", a);
                    if (u == NULL) {
                        copula = EAP(trees[k - 1], "copula", a);
                        measure = EAP(trees[k - 1], "measure", a);
                        u = gsl_vector_alloc(m);
                        dml_copula_h(copula, measure->x, measure->y, u);
                        SETVAP(graph, "h", a, u);
                        gsl_sort_vector_index(perm, u);
                        rank = gsl_permutation_alloc(m);
                        gsl_permutation_inverse(rank, perm);
                        SETVAP(graph, "hrank", a, rank);
                    }
                    u_rank = VAP(graph, "hrank", a);
                }
                if ((Cea == EAN(trees[k - 1], "Cea", a)
                        && (EAN(trees[k - 1], "Cea", a)
                                > EAN(trees[k - 1], "Ceb", a)))
                        || (Cea != EAN(trees[k - 1], "Cea", a)
                                && (EAN(trees[k - 1], "Cea", a)
                                        < EAN(trees[k - 1], "Ceb", a)))) {
                    u = VAP(graph, "hrev", a);
                    if (u == NULL) {
                        copula = EAP(trees[k - 1], "copula", a);
                        measure = EAP(trees[k - 1], "measure", a);
                        u = gsl_vector_alloc(m);
                        dml_copula_h(copula, measure->y, measure->x, u);
                        SETVAP(graph, "hrev", a, u);
                        gsl_sort_vector_index(perm, u);
                        rank = gsl_permutation_alloc(m);
                        gsl_permutation_inverse(rank, perm);
                        SETVAP(graph, "hrevrank", a, rank);
                    }
                    u_rank = VAP(graph, "hrevrank", a);
                }

                // Assign v and v_rank.
                if ((Ceb == EAN(trees[k - 1], "Cea", b)
                        && (EAN(trees[k - 1], "Cea", b)
                                < EAN(trees[k - 1], "Ceb", b)))
                        || (Ceb != EAN(trees[k - 1], "Cea", b)
                                && (EAN(trees[k - 1], "Cea", b)
                                        > EAN(trees[k - 1], "Ceb", b)))) {
                    v = VAP(graph, "h", b);
                    if (v == NULL) {
                        copula = EAP(trees[k - 1], "copula", b);
                        measure = EAP(trees[k - 1], "measure", b);
                        v = gsl_vector_alloc(m);
                        dml_copula_h(copula, measure->x, measure->y, v);
                        SETVAP(graph, "h", b, v);
                        gsl_sort_vector_index(perm, v);
                        rank = gsl_permutation_alloc(m);
                        gsl_permutation_inverse(rank, perm);
                        SETVAP(graph, "hrank", b, rank);
                    }
                    v_rank = VAP(graph, "hrank", b);

                }
                if ((Ceb == EAN(trees[k - 1], "Cea", b)
                        && (EAN(trees[k - 1], "Cea", b)
                                > EAN(trees[k - 1], "Ceb", b)))
                        || (Ceb != EAN(trees[k - 1], "Cea", b)
                                && (EAN(trees[k - 1], "Cea", b)
                                        < EAN(trees[k - 1], "Ceb", b)))) {
                    v = VAP(graph, "hrev", b);
                    if (v == NULL) {
                        copula = EAP(trees[k - 1], "copula", b);
                        measure = EAP(trees[k - 1], "measure", b);
                        v = gsl_vector_alloc(m);
                        dml_copula_h(copula, measure->y, measure->x, v);
                        SETVAP(graph, "hrev", b, v);
                        gsl_sort_vector_index(perm, v);
                        rank = gsl_permutation_alloc(m);
                        gsl_permutation_inverse(rank, perm);
                        SETVAP(graph, "hrevrank", b, rank);
                    }
                    v_rank = VAP(graph, "hrevrank", b);
                }

                // Set the weight of the edge. The arguments are ordered here.
                // The order determines the x and y fields of measure.
                if (Cea < Ceb) {
                    rvine_set_weight(graph, weight, e, u, v, u_rank, v_rank);
                } else {
                    rvine_set_weight(graph, weight, e, v, u, v_rank, u_rank);
                }
            }
        }

        // Compute the minimum weight spanning tree.
        trees[k] = g_malloc(sizeof(igraph_t));
        igraph_vector_init(graph_weight, igraph_ecount(graph));
        EANV(graph, "weight", graph_weight);
        igraph_minimum_spanning_tree_prim(graph, trees[k], graph_weight);
        igraph_vector_destroy(graph_weight);

        tree_aic = 0;
        for (e = 0; e < igraph_ecount(trees[k]); e++) {
            igraph_edge(trees[k], e, &a, &b);
            Cea = EAN(trees[k], "Cea", e);
            Ceb = EAN(trees[k], "Ceb", e);
            measure = EAP(trees[k], "measure", e);

            // Assign a bivariate copula to the edge.
            if (Cea < Ceb) {
                copula = dml_copula_select(measure->x, measure->y, measure,
                                           indeptest, indeptest_level, types,
                                           types_size, select, rng);
                // Get information for the truncation of the vine.
                if (trunc == DML_VINE_TRUNC_AIC) {
                    dml_copula_aic(copula, measure->x, measure->y, &copula_aic);
                    tree_aic += copula_aic;
                }
            } else {
                copula = dml_copula_select(measure->y, measure->x, measure,
                                           indeptest, indeptest_level, types,
                                           types_size, select, rng);
                // Get information for the truncation of the vine.
                if (trunc == DML_VINE_TRUNC_AIC) {
                    dml_copula_aic(copula, measure->y, measure->x, &copula_aic);
                    tree_aic += copula_aic;
                }
            }
            SETEAP(trees[k], "copula", e, copula);
        }

        igraph_destroy(graph);

        // Check if the vine should be truncated.
        if (trunc == DML_VINE_TRUNC_AIC && tree_aic >= 0) {
            // Free the memory used for the last tree.
            rvine_tree_cleanup(trees[k]);
            for (e = 0; e < igraph_ecount(trees[k]); e++) {
                copula = EAP(trees[k], "copula", e);
                dml_copula_free(copula);
            }
            igraph_destroy(trees[k]);
            g_free(trees[k]);
            trees[k] = NULL;
            break;
        }

        if (k > 0) rvine_tree_cleanup(trees[k - 1]);
    }

    // Cleanup the last tree if the vine was completely estimated.
    // If the vine was truncated, the last tree will be freed in
    // the function vine_fit_rvine, because the rvine_trees_to_vine
    // function needs some attributes of its edges.
    if (k == n - 1) {
        rvine_tree_cleanup(trees[n - 2]);
    }

    g_free(graph_weight);
    g_free(graph);
    gsl_permutation_free(perm);
}
コード例 #10
0
int main() {
  float dist[8][8] = {
	  {0.00, 4.69, 6.79, 3.50, 3.11, 4.46, 5.57, 3.00},
	  {4.69, 0.00, 2.10, 2.27, 2.65, 2.36, 1.99, 1.74},
	  {6.79, 2.10, 0.00, 3.78, 4.53, 2.83, 2.44, 3.79},
	  {3.50, 2.27, 3.78, 0.00, 1.98, 4.35, 2.07, 0.53},
	  {3.11, 2.65, 4.53, 1.98, 0.00, 3.80, 3.31, 1.47},
	  {4.46, 2.36, 2.83, 4.35, 3.80, 0.00, 4.35, 3.82},
	  {5.57, 1.99, 2.44, 2.07, 3.31, 4.35, 0.00, 2.57},
	  {3.00, 1.74, 3.79, 0.53, 1.47, 3.82, 2.57, 0.00},
  };
  igraph_t g;
  igraph_matrix_t coords, dist_mat;
  igraph_real_t vc;
  igraph_arpack_options_t options;
  int i, j;
  srand(time(0));

  igraph_arpack_options_init(&options);

  igraph_tree(&g, 10, 2, IGRAPH_TREE_UNDIRECTED);
  igraph_matrix_init(&coords, 0, 0);
  igraph_layout_mds(&g, &coords, 0, 2, &options);
  if (MATRIX(coords, 0, 0) > 0) {
    for (i = 0; i < igraph_matrix_nrow(&coords); i++)
      MATRIX(coords, i, 0) *= -1;
  }
  if (MATRIX(coords, 0, 1) < 0) {
    for (i = 0; i < igraph_matrix_nrow(&coords); i++)
      MATRIX(coords, i, 1) *= -1;
  }
  igraph_matrix_print(&coords);
  igraph_matrix_destroy(&coords);
  igraph_destroy(&g);

  igraph_full(&g, 8, IGRAPH_UNDIRECTED, 0);
  igraph_matrix_init(&coords, 8, 2);
  igraph_matrix_init(&dist_mat, 8, 8);
  for (i = 0; i < 8; i++)
    for (j = 0; j < 2; j++)
      MATRIX(coords, i, j) = rand() % 1000;
  for (i = 0; i < 8; i++)
    for (j = i+1; j < 8; j++) {
      double dist_sq = 0.0;
      dist_sq += sqr(MATRIX(coords, i, 0)-MATRIX(coords, j, 0));
      dist_sq += sqr(MATRIX(coords, i, 1)-MATRIX(coords, j, 1));
      MATRIX(dist_mat, i, j) = sqrt(dist_sq);
      MATRIX(dist_mat, j, i) = sqrt(dist_sq);
	}
  igraph_layout_mds(&g, &coords, &dist_mat, 2, &options);
  for (i = 0; i < 8; i++)
    for (j = i+1; j < 8; j++) {
      double dist_sq = 0.0;
      dist_sq += sqr(MATRIX(coords, i, 0)-MATRIX(coords, j, 0));
      dist_sq += sqr(MATRIX(coords, i, 1)-MATRIX(coords, j, 1));
      if (fabs(sqrt(dist_sq) - MATRIX(dist_mat, i, j)) > 1e-2) {
        printf("dist(%d,%d) should be %.4f, but it is %.4f\n",
				i, j, MATRIX(dist_mat, i, j), sqrt(dist_sq));
        return 1;
      }
    }
  igraph_matrix_destroy(&dist_mat);
  igraph_matrix_destroy(&coords);
  igraph_destroy(&g);

  return 0;
}