Ejemplo n.º 1
0
Archivo: graph.c Proyecto: AlgoLab/cppp
void
graph_pp(const graph_s* gp) {
#ifdef DEBUG
        assert(gp != NULL);
        log_debug("graph_pp");
        uint32_t n = gp->num_vertices;
        fprintf(stderr, "Graph %p has %d vertices\n", gp, n);

        fprintf(stderr, "Adjacency matrix\n");
        for (uint32_t v=0; v < n; v++) {
                fprintf(stderr, "Vertex %d (degree %d):", v, graph_degree(gp, v));
                for (uint32_t v2=0; v2 < n; v2++)
                        if (graph_get_edge(gp, v, v2))
                                fprintf(stderr, " %d", v2);
                fprintf(stderr, "\n");
        }

        fprintf(stderr, "Adjacency lists\n");
        for (uint32_t v=0; v < n; v++) {
                fprintf(stderr, "Vertex %d (degree %d):", v, graph_degree(gp, v));
                for (uint32_t p=0; p < graph_degree(gp, v); p++)
                        fprintf(stderr, " %d", graph_get_edge_pos(gp, v, p));
                fprintf(stderr, "\n");
        }
#endif
}
Ejemplo n.º 2
0
Archivo: graph.c Proyecto: AlgoLab/cppp
void
graph_del_edge(graph_s* gp, uint32_t v1, uint32_t v2) {
        log_debug("graph_del_edge %d %d", v1, v2);
        graph_check(gp);
        gp->adjacency[v1 * (gp->num_vertices) + v2] = false;
        gp->adjacency[v2 * (gp->num_vertices) + v1] = false;

        uint32_t pos = 0;
        while ((gp->adjacency_lists)[v1 * (gp->num_vertices) + pos] != v2)
                pos += 1;
        assert(pos < graph_degree(gp, v1));
        (gp->adjacency_lists)[v1 * (gp->num_vertices) + pos] = (gp->adjacency_lists)[v1 * (gp->num_vertices) + graph_degree(gp, v1) - 1];

        pos = 0;
        while ((gp->adjacency_lists)[v2 * (gp->num_vertices) + pos] != v1)
                pos += 1;
        assert(pos < graph_degree(gp, v2));
        (gp->adjacency_lists)[v2 * (gp->num_vertices) + pos] = (gp->adjacency_lists)[v2 * (gp->num_vertices) + graph_degree(gp ,v2) - 1];

        (gp->degrees)[v1] -= 1;
        (gp->degrees)[v2] -= 1;

        log_debug("graph_del_edge %d %d: completed", v1, v2);
        graph_pp(gp);
        graph_check(gp);
}
Ejemplo n.º 3
0
Archivo: graph.c Proyecto: AlgoLab/cppp
void
graph_check(const graph_s *gp) {
        assert(gp != NULL);
        unsigned int err = 0;
#ifdef DEBUG
        if (gp->degrees == NULL)
                err = 1;
        if (gp->adjacency == NULL)
                err = 2;
        uint32_t n = gp->num_vertices;

        for (uint32_t v=0; v < n; v++)
                if (gp->degrees[v] > n)
                        err = 3;

        if (err == 0)
                for (uint32_t v=0; v < n; v++) {
                        uint32_t deg = 0;
                        for (uint32_t v2=0; v2 < n; v2++) {
                                if (graph_get_edge(gp, v, v2) != graph_get_edge(gp, v, v2))
                                        err = 4;
                                if (graph_get_edge(gp, v, v2))
                                        deg += 1;
                        }
                        if (deg != graph_degree(gp, v))
                                err = 5;
                }

        /* Check if adjacency matrix and lists are the same
         */
        if (err == 0)
                for (uint32_t v=0; v < n; v++) {
                        bool from_matrix[n];
                        bool from_list[n];
                        memset(from_list, 0, n * sizeof(bool));
                        for (uint32_t v2=0; v2 < n; v2++)
                                from_matrix[v2] = graph_get_edge(gp, v, v2);
                        for (uint32_t pos=0; pos < graph_degree(gp, v); pos++)
                                from_list[graph_get_edge_pos(gp, v, pos)] = true;
                        for (uint32_t v2=0; v2 < n; v2++)
                                if (from_matrix[v2] != from_list[v2])
                                        err = 6;
                }


#endif
        if (err > 0) {
                graph_pp(gp);
                log_debug("check_graph code: %d", err);
        }
        assert(err == 0);
}
Ejemplo n.º 4
0
Archivo: graph.c Proyecto: AlgoLab/cppp
void
graph_add_edge(graph_s* gp, uint32_t v1, uint32_t v2) {
        log_debug("graph_add_edge %d %d", v1, v2);
        graph_check(gp);
        gp->adjacency[v1 * (gp->num_vertices) + v2] = true;
        gp->adjacency[v2 * (gp->num_vertices) + v1] = true;
        (gp->adjacency_lists)[v1 * (gp->num_vertices) + graph_degree(gp, v1)] = v2;
        (gp->adjacency_lists)[v2 * (gp->num_vertices) + graph_degree(gp, v2)] = v1;

        (gp->degrees)[v1] += 1;
        (gp->degrees)[v2] += 1;
        graph_check(gp);
}
Ejemplo n.º 5
0
Archivo: graph.c Proyecto: AlgoLab/cppp
void
graph_reachable(const graph_s* gp, uint32_t v, bool* reached) {
        assert(gp != NULL);
        assert(reached != NULL);
        graph_check(gp);
        log_debug("graph_reachable: graph_s=%p, v=%d, reached=%p", gp, v, reached);
        uint32_t n = gp->num_vertices;
        uint32_t border[n];
        uint32_t new_border[n];
        uint32_t border_size = 1;
        memset(reached, 0, n * sizeof(bool));
        border[0] = v;
        reached[v] = true;
        while (border_size > 0) {
                uint32_t new_border_size = 0;
                for (uint32_t vx = 0; vx < border_size; vx++) {
                        uint32_t v1 = border[vx];
                        for (uint32_t p = 0; p < graph_degree(gp, v1); p++) {
                                uint32_t w = graph_get_edge_pos(gp, v1, p);
                                if (!reached[w]) {
                                        new_border[new_border_size++] = w;
                                        reached[w] = true;
                                }
                        }
                }
                memcpy(border, new_border, new_border_size * sizeof(new_border[0]));
                border_size = new_border_size;
        }
        log_array_bool("reached: ", reached, gp->num_vertices);
        log_debug("graph_reachable: end");
}
Ejemplo n.º 6
0
Archivo: graph.c Proyecto: AlgoLab/cppp
void
connected_components(graph_s* gp, uint32_t* components) {
        assert(gp!=NULL);
        assert(components != NULL);
        log_debug("connected_components");
        log_debug("connected_components: graph_s=%p", gp);
        graph_check(gp);
        graph_pp(gp);
        memset(components, 0, (gp->num_vertices) * sizeof(uint32_t));
        bool visited[gp->num_vertices];
        memset(visited, 0, gp->num_vertices * sizeof(bool));

        uint32_t color = 0;
        for (uint32_t v = 0; v < gp->num_vertices; color++) {
                log_debug("Reaching from %d", v);
/*
  Check if v is an isolated vertex.
  In that case we do not call graph_reachable to find its connected
*/
                if (graph_degree(gp, v) == 0) {
                        components[v] = color;
                        visited[v] = true;
                } else {
                        bool current_component[gp->num_vertices];
                        graph_reachable(gp, v, current_component);
                        for (uint32_t w = 0; w < gp->num_vertices; w++)
                                if (current_component[w]) {
                                        components[w] = color;
                                        visited[w] = true;
                                }
                }
/*
  Find the next vertex in an unexplored component
*/
                uint32_t next = gp->num_vertices;
                for (uint32_t w = v + 1; w < gp->num_vertices; w++)
                        if (!visited[w]) {
                                next = w;
                                break;
                        }
                v = next;
        }
        log_array_uint32_t("component",components, gp->num_vertices);
        log_debug("connected_components: end");
}
Ejemplo n.º 7
0
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;
}
Ejemplo n.º 8
0
Archivo: graph.c Proyecto: AlgoLab/cppp
/**
   \brief returns the (pos+1)-th vertex that is adjacent to v1
*/
uint32_t
graph_get_edge_pos(const graph_s* gp, uint32_t v, uint32_t pos) {
        assert(pos < graph_degree(gp, v));
        return (gp->adjacency_lists)[v * (gp->num_vertices) + pos];
}
Ejemplo n.º 9
0
int main(void)
{
     int **graph_a = NULL;  //指向a图
     int **graph_b = NULL;  //指向b图
     int *ga_degree = NULL; //指向"存储a图的度的数组"
     int *gb_degree = NULL; //指向"存储b图的度的数组"
     int va_num = 0;        //a图的顶点个数
     int vb_num = 0;        //b图的顶点个数
     int ea_num = 0;        //a图的边数
     int eb_num = 0;        //b图的边数
     int ga_rand = 0;       //a图的秩
     int gb_rand = 0;       //b图的秩
     int i = 0;


     printf("-----本程序判断两个无向图是否同构-----\n");

     printf("请输入第一个图的顶点数: ");
     scanf("%d", &va_num);

     printf("请输入第二个图的顶点数: ");
     scanf("%d", &vb_num);

     if (va_num != vb_num)
     {
          printf("不同构! 顶点数不相等!\n");
          return 0;
     }
/*
     ////////////////////////试试放在函数里//////////////
     //分配内存
     //第一个图
     graph_a = (int **)malloc(va_num * sizeof(int *));
     for (i = 0; i < va_num; i++)
     {
          graph_a[i] = (int *) malloc(va_num * sizeof(int));
     }

     //第一个图的度
     ga_degree = (int *)malloc(va_num * sizeof(int));

     //分配第二个图
     graph_b = (int **)malloc(vb_num * sizeof(int *));
     for (i = 0; i < vb_num; i++)
     {
          graph_b[i] = (int *) malloc(vb_num * sizeof(int));
     }

     //第二图的度
     gb_degree = (int *) malloc(vb_num *sizeof(int));

*/
     graph_a = graph_malloc(va_num);
     ga_degree = degree_malloc(va_num);
     graph_b = graph_malloc(vb_num);
     gb_degree = degree_malloc(vb_num);

     //输入数据(邻接矩阵) & 检查是否输入有误
     printf("输入第一个图的邻接矩阵:\n");
     ea_num = graph_input(graph_a, va_num);

     printf("输入第二个图的邻接矩阵:\n");
     eb_num = graph_input(graph_b, vb_num);

     //判断边数是否相等
     if(ea_num != eb_num)
     {
         printf("不同构! 边数不相等!\n");
         return 0;
     }

     //求两图每个点的度, 排序后判断每个顶点的度
     //ga_degree 是值-结果参数
     graph_degree(graph_a, ga_degree, va_num);
     graph_degree(graph_b, gb_degree, vb_num);

     //判断
     for (i = 0; i < va_num; i++)
     {
         if (ga_degree[i] != gb_degree[i])
         {
             printf("不同构! 各个顶点的度不对应!\n");
             return 0;
         }
     }



     //求矩阵的秩
     ga_rand = graph_rand(graph_a, va_num);
     gb_rand = graph_rand(graph_b, vb_num);

     //判断矩阵的4是否相等
     if (ga_rand != gb_rand)
     {
//         printf("%d %d\n", ga_rand, gb_rand);
         printf("不同构! 秩不相等!\n");
     }
     else
     {
          printf("同构!\n");
     }

/*
     //释放内存
     for (i = 0; i < va_num; i++)
     {
          free(graph_a[i]);
     }
     free(graph_a);
     free(ga_degree);

     for (i = 0; i < vb_num; i++)
     {
         free(graph_b[i]);
     }
     free(graph_b);
     free(gb_degree);
*/
     graph_free(graph_a, ga_degree, va_num);
     graph_free(graph_b, gb_degree, vb_num);
     
     return 0;


}