コード例 #1
0
ファイル: test_graph_layout.c プロジェクト: brunokim/cgraph
void test_circle_layout(){
	int n = 64, k = 4;
	int radius = 5, width=1;
	graph_t *g = new_watts_strogatz(n, k, 0.25);
	
	color_t solid_red = {255, 0, 0, 255};
	color_t black     = {0,   0, 0, 255};
	
	coord_t *p = malloc(n * sizeof(*p));
	double size = graph_layout_circle(width+radius, p, n);
	
	int m = graph_num_edges(g);
	int *es = malloc(m * sizeof(*es));
	path_style_t edge_style[2];
	
	graph_layout_circle_edges(g, size, width, black, es, &edge_style[0]);
	
	int *ps = malloc(n * sizeof(*ps));
	memset(ps, 0, n*sizeof(*ps));
	
	circle_style_t point_style;
	point_style.radius = radius;
	point_style.width = width;
	color_copy(point_style.fill, solid_red);
	color_copy(point_style.stroke, black);
	
	graph_print_svg_some_styles("test/test_circle_layout.svg", 0, 0, g, p, 
	                            ps, &point_style, 1, 
	                            es, edge_style, 2);
	free(ps); free(es);
	free(p);
	delete_graph(g);
}
コード例 #2
0
ファイル: stats_degree.c プロジェクト: pauldmccarthy/ccnet
double stats_avg_degree(graph_t *g) {

  uint32_t nnodes;
  uint32_t nedges;

  nnodes = graph_num_nodes(g);
  nedges = graph_num_edges(g);

  return (2.0*nedges) / nnodes;
}
コード例 #3
0
ファイル: stats_edges.c プロジェクト: pauldmccarthy/ccnet
double stats_num_intra_edges(graph_t *g, double *inter_out) {

  uint64_t       i;
  uint64_t       j;
  uint32_t       nnodes;
  uint32_t       nnbrs;
  uint32_t      *nbrs;
  graph_label_t *ilbl;
  graph_label_t *jlbl;
  double         intra;
  double         inter;

  if (graph_num_labelvals(g) <= 1) {
    if (inter_out != NULL) *inter_out = 0;
    return graph_num_edges(g);
  }

  intra  = 0;
  inter  = 0;
  nnodes = graph_num_nodes(g);

  for (i = 0; i < nnodes; i++) {

    nnbrs = graph_num_neighbours(g, i);
    nbrs  = graph_get_neighbours(g, i);
    ilbl  = graph_get_nodelabel( g, i);
    
    for (j = 0; j < nnbrs; j++) {

      if (i > nbrs[j]) continue;

      jlbl = graph_get_nodelabel(g, nbrs[j]);

      if (ilbl->labelval == jlbl->labelval) intra ++;
      else                                  inter ++;
    }
  }

  stats_cache_add(g,
                  STATS_CACHE_INTRA_EDGES,
                  STATS_CACHE_TYPE_GRAPH,
                  sizeof(double));
  stats_cache_add(g,
                  STATS_CACHE_INTER_EDGES,
                  STATS_CACHE_TYPE_GRAPH,
                  sizeof(double));
  stats_cache_update(g, STATS_CACHE_INTRA_EDGES, 0, -1, &intra);
  stats_cache_update(g, STATS_CACHE_INTER_EDGES, 0, -1, &inter);

  if (inter_out != NULL) *inter_out = inter;
  return intra;
}
コード例 #4
0
ファイル: dumpngdb.c プロジェクト: pauldmccarthy/ccnet
void _meta(graph_t *g) {

  uint32_t i;
  uint16_t nmsgs;
  char    *msg;

  nmsgs = graph_log_num_msgs(g);

  printf("num nodes: %u\n", graph_num_nodes(g));
  printf("num edges: %u\n", graph_num_edges(g));
  printf("directed:  %u\n", graph_is_directed(g));


  printf("log messages:\n");
  for (i = 0; i < nmsgs; i++) {
    
    msg = graph_log_get_msg(g, i);
    printf("  %3u: %s\n", i, msg);
  }

}
コード例 #5
0
ファイル: cnet.c プロジェクト: pauldmccarthy/ccnet
void print_stats(graph_t *g, struct args *args) {

  uint64_t       i;
  uint64_t       j;
  uint32_t       nodestart;
  uint32_t       nodeend;
  uint32_t       numnodes;
  uint32_t       numcmps;
  double         degree;
  double         degcent;
  double         swidx;
  double         pathlength;
  double         locefficiency;
  uint32_t       connected;
  double         clustering;
  double         closeness;
  double         betweenness;
  
  uint32_t      *components;
  array_t        cmpsizes;
  graph_label_t *label;
  double         tmp;
  uint32_t       nlblvals;
  uint32_t      *lblvals;

  degree         = 0;
  degcent        = 0;
  swidx          = 0;
  pathlength     = 0;
  connected      = 0;
  locefficiency  = 0;
  clustering     = 0;
  closeness      = 0;
  betweenness    = 0;
  nlblvals       = 0;
  
  components     = NULL;
  array_create(&cmpsizes, sizeof(uint32_t), 10);

  numnodes  = graph_num_nodes(g);
  connected = stats_cache_connected(g);

  if (args->nodestart == -1) nodestart = 0;
  else                       nodestart = args->nodestart;
  if (args->nodeend   == -1) nodeend   = numnodes;
  else                       nodeend   = args->nodeend; 

  components = calloc(numnodes, sizeof(uint32_t));

  if (args->nodelabel) {

    for (i = nodestart; i < nodeend; i++) {
      label = graph_get_nodelabel(g,i);
      printf("label %" PRIu64 ":\t%u,%f,%f,%f\n", i, 
        label->labelval, 
        label->xval, 
        label->yval, 
        label->zval);
    }
    printf("\n");
  }

  if (args->degree) {

    for (i = nodestart; i < nodeend; i++) {
      tmp = stats_degree(g, i);
      degree += tmp;

      printf("degree %" PRIu64 ":\t%f\n", i, tmp);
    }
    printf("\n");
  }

  if (args->degcent) {

    for (i = nodestart; i < nodeend; i++) {
      tmp = stats_degree_centrality(g, i);
      degcent += tmp;

      printf("degree centraliy %" PRIu64 ":\t%f\n", i, tmp);
    }
    printf("\n");
  } 

  if (args->ersmallworld) {
    swidx = stats_smallworld_index(g);
  }

  if (args->clustering) {

    for (i = nodestart; i < nodeend; i++) {
      stats_cache_node_clustering(g, i, &tmp);
      clustering += tmp;

      printf("clustering %" PRIu64 ":\t%f\n", i, tmp);
    }
    printf("\n");
  }

  if (args->pathlength) {

    for (i = nodestart; i < nodeend; i++) {
      stats_cache_node_pathlength(g, i, &tmp);
      pathlength += tmp;

      printf("pathlength %" PRIu64 ":\t%f\n", i, tmp);
    }
    printf("\n");
  }

  if (args->closeness) {

    for (i = nodestart; i < nodeend; i++) {
      tmp = stats_closeness_centrality(g, i);
      closeness += tmp;
      
      printf("closeness %" PRIu64 ":\t%f\n", i, tmp);
    }
    printf("\n");
  }

  if (args->betweenness) {

    for (i = nodestart; i < nodeend; i++) {
      stats_cache_betweenness_centrality(g, i, &tmp);
      betweenness += tmp;
      
      printf("betweenness %" PRIu64 ":\t%f\n", i, tmp);
    }
    printf("\n");
  }

  if (args->lefficiency) {

    for (i = nodestart; i < nodeend; i++) {

      stats_cache_node_local_efficiency(g, i, &tmp);
      locefficiency += tmp;

      printf("efficiency %" PRIu64 ":\t%f\n", i, tmp);
    }
    printf("\n");
  }

  if (args->numpaths) {

    for (i = nodestart; i < nodeend; i++) {

      stats_cache_node_numpaths(g, i, &tmp);
      printf("numpaths %" PRIu64 ":\t%f\n", i, tmp);
    }
    printf("\n");
  }

  if (args->components) {
    stats_num_components(g, 1, &cmpsizes, components);
    for (i = nodestart; i < nodeend; i++) {
      printf("component %" PRIu64 ":\t%u\n", i, components[i]);
    }
    for (i = 0; i < cmpsizes.size; i++) {
      printf("component %" PRIu64 " size:\t%u\n", i,
             ((uint32_t *)(cmpsizes.data))[i]);
    }
    printf("\n");
  }

  if (args->comppops) {
    stats_num_components(g, 1, &cmpsizes, components);

    for (i = 0; i < cmpsizes.size; i++) {
      printf("component %" PRIu64 " population: ", i);

      for (j = 0; j < numnodes; j++) {
        if (components[j] == i) printf("%" PRIu64 " ", (j+1));
      }
      printf("\n");
    }
  }

  if (args->labelvals) {
    nlblvals = graph_num_labelvals(g);
    lblvals  = graph_get_labelvals(g);
    

    for (i = 0; i < nlblvals; i++) {
      tmp = stats_num_labelled_nodes(g, lblvals[i]);
      printf("label value %" PRIu64 ": %u (%0.0f)\n", i, lblvals[i], tmp);
    }
  }

  if (args->compspan) {
    numcmps = stats_cache_num_components(g);

    for (i = 0; i < numcmps; i++) {

      printf("component %" PRIu64 " span: %0.6f\n",
             i, stats_component_span(g, i));
    }
  }

  if (args->avgedist) {

    for (i = 0; i < numnodes; i++) {

      printf("avg edge distance %" PRIu64 ": %0.6f\n",
             i, stats_avg_edge_distance(g, i));
    }
  }

  degree         /= (nodeend - nodestart);
  degcent        /= (nodeend - nodestart);
  clustering     /= (nodeend - nodestart);
  pathlength     /= connected;
  locefficiency  /= connected;
  closeness      /= (nodeend - nodestart);
  betweenness    /= (nodeend - nodestart);
  
  if (args->nodes)
    printf("nodes:                 %u\n",    numnodes);
  if (args->edges)
    printf("edges:                 %u\n",    graph_num_edges(g));
  if (args->connected)
    printf("dis/connected:         %u/%u\n", numnodes-connected, connected);
  if (args->density)
    printf("density:               %f\n",    stats_density(g));
  if (args->degree)
    printf("avg degree:            %f\n",    degree);
  if (args->degcent)
    printf("avg degree centrality: %f\n",    degcent); 
  if (args->components) {
    printf("components:            %u\n",    cmpsizes.size);
  }
  if (args->clustering)
    printf("avg clustering:        %f\n",    clustering);
  if (args->approxclust) {
    if (args->approxclust < 0) args->approxclust = numnodes/10;
    printf("approx. clustering:    %f\n",    stats_cache_approx_clustering(
                                               g,args->approxclust));
  }
  if (args->pathlength)
    printf("avg pathlength:        %f\n",    pathlength);
  if (args->gefficiency)
    printf("global efficiency:     %f\n",    stats_cache_global_efficiency(g));
  if (args->lefficiency)
    printf("avg local efficiency:  %f\n",    locefficiency);
  if (args->ersmallworld) {
    printf("er clustering:         %f\n",    stats_er_clustering(g));
    printf("er pathlength:         %f\n",    stats_er_pathlength(g));
    printf("small-world index:     %f\n",    swidx);
  }
  if (args->assortativity)
    printf("assortativity:         %f\n",    stats_cache_assortativity(g));
  if (args->closeness)
    printf("closeness:             %f\n",    closeness);
  if (args->betweenness)
    printf("betweenness:           %f\n",    betweenness);
  if (args->modularity)
    printf("modularity:            %f\n",    stats_cache_modularity(g));
  if (args->chira)
    printf("chira fitness:         %f\n",    stats_cache_chira(g)); 
  if (args->nintra)
    printf("intra-cluster edges:   %0.0f\n", stats_cache_intra_edges(g));
  if (args->ninter)
    printf("inter-cluster edges:   %0.0f\n", stats_cache_inter_edges(g));
  if (args->labelvals)
    printf("num label vals:        %u\n",    nlblvals);
  if (args->newmanerror)
    printf("newman error:          %f\n",    stats_newman_error(g));
  if (args->mutualinfo) {
    printf("mutual info:           %f\n",    stats_graph_mutual_information(g));
  }

  if (args->ebmatrix) print_matrix(   g, &stats_cache_edge_betweenness);
  if (args->psmatrix)
    print_edge_vals(g, &stats_edge_pathsharing, "path-sharing");
  if (args->edgedist)
    print_edge_vals(g, &stats_edge_distance, "distance");
  if (args->alledges)
    print_edge_vals(g, &graph_get_weight, "edge");
}
コード例 #6
0
ファイル: ctrim.c プロジェクト: pauldmccarthy/ccnet
static uint8_t _trim(graph_t *gin, graph_t *gout, args_t *a) {

  uint64_t  i;
  uint32_t  val;
  uint32_t  flags;
  uint32_t  oldcmp;
  void     *opt;
  mod_opt_t modopt;
  uint8_t (*tfunc)(
    graph_t  *gin, graph_t *gout, uint32_t cmplimit, 
    uint32_t igndis, void *opt,
    uint8_t (*init)(graph_t *g),
    uint8_t (*remove)(
      graph_t *g, double *space, array_t *edges, graph_edge_t *edge),
    uint8_t (*recalc)(graph_t *g, graph_edge_t *edge));

  flags = 0;

  if (a->modularity) {
    tfunc = &graph_threshold_modularity;
    val   = a->nedges;
    opt   = &modopt;
    if (val == 0) 
      val = graph_num_edges(gin);
  }
  else if (a->chira) {
    tfunc = &graph_threshold_chira;
    val   = a->nedges;
    if (val == 0) 
      val = graph_num_edges(gin);
  } 
  else if (a->nedges   != 0) {
    tfunc = &graph_threshold_edges;
    val   = a->nedges;
    if (val == 0) 
      val = graph_num_edges(gin);
  }
  else if (a->cmplimit != 0) {
    tfunc = &graph_threshold_components;
    val   = a->cmplimit;
    flags = a->igndis;
  }
  else goto fail;

  switch (a->criteria) {

    case C_PATHSHARING:
      if (tfunc(gin,
                gout,
                val,
                flags,
                opt,
                &graph_init_pathsharing,
                &graph_remove_pathsharing,
                &graph_recalculate_pathsharing))
        goto fail;
      break;

    case C_EDGEBETWEENNESS:
      if (tfunc(gin,
                gout,
                val,
                flags,
                opt,
                &graph_init_edge_betweenness,
                &graph_remove_edge_betweenness,
                &graph_recalculate_edge_betweenness))
        goto fail;
      break;
  }

  if (a->modularity && a->printmod) {

    oldcmp = 0xFFFFFFFF;

    for (i = 0; i < modopt.nvals; i++) {

      if (modopt.ncmps[i] != oldcmp) {

        printf("%05" PRIu64 ", %04u, %0.6f\n", 
               i, modopt.ncmps[i], modopt.modularity[i]);
        oldcmp = modopt.ncmps[i];
      }
    }
  }

  return 0;
  
fail:
  return 1;
}