示例#1
0
文件: network.c 项目: gyc2015/GYC
int main(int argc, char *argv[])
{
    /* setup */
    char *path = "./data/ct/";
	char *path2 = "/home/gyc/Sources/linux.doc/kernel/";
	vector_char str;
	vector_char_init(&str,100);
	VECTOR(str)[0] = '\0';

    SetupLexer();
    dic_setup();
    struct dictionary *dict = new_dictionary(10000);

    graph_t lkn;
    vector_int edges;
    catch_function_call_dir(path, dict, &edges);
    printf("capacity=%d,size=%d\n",dict_capacity(dict), dict_size(dict));
    new_graph(&lkn, &edges, 0, GRAPH_DIRECTED);

	struct dictionary *filedict = new_dictionary(4);
	vector_funcP flist;
	vector_funcP_init_(&flist, dict_size(dict));
	get_function_filename(path2, dict, filedict, &flist);
    printf("filedict: capacity=%d,size=%d\n",dict_capacity(filedict), dict_size(filedict));

	/* reciprocal */
	printf("reciprocal = %f \n", graph_reciprocal(&lkn));
	vector_double res;
	vector_double_init(&res, 0);
	graph_betweenness(&lkn, &res, graph_vss_all(), GRAPH_DIRECTED);
	printf("betweenness directed:"); print_vector_double(&(res),stdout);
	vector_double_destroy(&res);

	/* degree */
    graph_degree(&lkn, &edges, graph_vss_all(), GRAPH_OUT, GRAPH_NO_LOOPS);
    printf(">>>out, no loops");
	int min, max, sum;
	double ave;
	graph_degree_minmax_avesum(&lkn, &min, &max, &ave, &sum, GRAPH_OUT, GRAPH_NO_LOOPS);
	printf("minout=%d\nmaxout=%d\nsumout=%d\naveout=%f\n",min,max,sum,ave);
	graph_degree_minmax_avesum(&lkn, &min, &max, &ave, &sum, GRAPH_IN, GRAPH_NO_LOOPS);
	printf("minin=%d\nmaxin=%d\nsumin=%d\navein=%f\n",min,max,sum,ave);

	/* fast community */
	graph_reverse(&lkn);
	vector_int v1;
	vector_int_init(&v1,0);
	int ncom = 0;
	double modularity = graph_community_fastgreedy(&lkn, &v1, &ncom);
	
	printf("modularity = %f,ncom = %d\n",modularity,ncom);
	FILE *f = fopen("funccom.fc.xlsx","w");
	fprintf(f, "comID\tname\n");
	for (int i = 0; i < dict_size(dict);i++) {
		fprintf(f, "%d\t", VECTOR(v1)[i]);
		struct rb_node* e = dict_ele(dict, i);
		dic_traceback_string(e, &str);
		fprintf(f, "%s\n",VECTOR(str));
	}
	fclose(f);
	f = fopen("comID.fc.xlsx","w");
	output_com_filename(&flist, &v1, graph_vertices_count(&lkn), ncom, filedict, f);
	fclose(f);

	//print_vector_int(&v1, stdout);

	print_communities(&lkn, &v1, "community.fc.xlsx", "comedge.fc.xlsx");

	vector_funcP_destroy(&flist);
	vector_int_destroy(&v1);
	vector_char_destroy(&str);
    vector_int_destroy(&edges);
    graph_destroy(&lkn);
    return 0;
}
示例#2
0
void find_comm(graph *g, unsigned int nev, unsigned int evmode) {

  int k, uev, umax, ni, nf, nn;
  int n = g->n;
  int *kmax;
  double *maxq;
  double *eval;
  double *evec;
  int **e1,**e2;

  int evinfo = 0;
  int maxitr = 5000;
  int ncv = 30;

  eval = malloc(nev*sizeof(double));
  evec = malloc(n*nev*sizeof(double));

  /* calculating eigenvalues and eigenvectors */
  gr_eig(g, &nev, 1, 1, eval, evec, ncv, maxitr, evinfo, evmode);

  /* write EV in logfile */
  fprintf(logfile, "number of converged eigenvalues: %d\n", nev);
  for (k=0; k<nev; k++)
    fprintf(logfile, "%f\n",eval[k]);

  /* angular distance needs at least two coordinates */
  ni = 2;
  nf = nev-1;
  nn = nf-ni+1;

  kmax = malloc(nn*sizeof(int));
  maxq = malloc(nn*sizeof(double));
  e1 = malloc(nn*sizeof(int*));
  e2 = malloc(nn*sizeof(int*));
  for (k=0;k<nn; k++) {
    e1[k] = malloc((n-1)*sizeof(int));
    e2[k] = malloc((n-1)*sizeof(int));
  }
  
  /* loop on the number of used eigenvectors */
  umax = 0;
  for (uev=ni; uev<=nf; uev++) {
    comm_findmaxq(n, g, evec, uev, e1[uev-ni],e2[uev-ni], &(maxq[uev-ni]),
		  &(kmax[uev-ni]));
    fprintf(logfile, "D= %d\tQmax = %g\n", uev, maxq[uev-ni]);
    /* find max modularity */
    if (umax>0) {
      if (maxq[uev-ni] > maxq[umax-ni])
	umax = uev;
    } else
      umax = uev;
  }

  uev = umax;

  fprintf(logfile, "using %d eigenvalues: Qmax found = %f\n", 
	  uev, maxq[uev-ni]);
  print_communities(g, e1[uev-ni], e2[uev-ni], kmax[uev-ni]);

  for (k=0;k<nn; k++) {
    free(e1[k]);
    free(e2[k]);
  }
  free(e1);
  free(e2);
  free(kmax);
  free(maxq);
  free(eval);
  free(evec);
}