-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_io.c
133 lines (107 loc) · 2.64 KB
/
graph_io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "graph_io.h"
void print_node(graph* g, int node_id, int show_neighbours, int show_bc) {
vertex *v;
neighbour *n;
edge *e;
v = &g->vertices[node_id];
printf("Node: [%d]\tColor: [%d]\tID Tree: [%d]\tDegree: [%d]\t", v->id, v->color, v->id_tree, v->degree);
if (show_neighbours) {
char c = show_bc ? '\n' : '\t';
printf("%cNeighbours:%c", c, c);
n = v->first_neighbour;
while (n != NULL)
{
e = &g->edges[n->edge_id];
printf("%d [E: %d, F: %d]\t", n->id, e->is_edge, e->is_frond);
if (show_bc)
{
printf("BC ID: [%d]\n", e->color);
}
n = n->next;
}
if (show_bc)
{
print_list(v->bc_ids);
}
else
{
printf("\n");
}
}
printf("\n");
}
void print_graph(graph *g, int show_not_colored, int show_neighbours, int show_bc)
{
if (g == NULL || g->vertices == NULL || g->num_vertices == 0)
{
printf("Empty graph\n");
}
printf("\n");
for (int i = 1; i <= g->num_vertices; i++)
{
if (!show_not_colored || !g->vertices[i].color) {
print_node(g, i, show_neighbours, show_bc);
}
}
}
void print_k_bc_stats(graph* g, int num_bc) {
int num_cores = color_graph_rec(g, 0);
printf("# of Cores: %d\n\n", num_cores);
int *cores_size = (int *)calloc(num_cores, sizeof(int));
int core_id;
for (int i = 1; i <= g->num_vertices; i++) {
core_id = g->vertices[i].color;
if (core_id <= num_cores) {
cores_size[core_id - 1]++;
}
}
int *bcs_size = (int *)calloc(num_bc, sizeof(int));
int *bc_to_core = (int *)calloc(num_bc, sizeof(int));
vertex* v;
Node* n;
for (int i = 1; i <= g->num_vertices; i++) {
v = &g->vertices[i];
core_id = v->color;
if (core_id <= num_cores && v->bc_ids->size > 0) {
n = v->bc_ids->head;
while (n != NULL) {
bcs_size[n->item - 1]++;
bc_to_core[n->item - 1] = core_id;
n = n->next;
}
}
}
printf("Cores Info:\n");
for (int i = 0; i < num_cores; i++) {
printf("Core ID: %d\tSize: [%d]\n", i + 1, cores_size[i]);
}
printf("\n\nCommunities:\n");
for (int i = 0; i < num_bc; i++) {
printf("Com. ID: %d\tSize: [%d]\tCore ID: %d\n", i + 1, bcs_size[i], bc_to_core[i]);
}
free(bcs_size);
free(bc_to_core);
free(cores_size);
}
void write_graph_communities(graph* g, char* filename) {
FILE *file = fopen(filename, "w");
if (file == NULL) {
perror("Error opening file");
return;
}
vertex *v;
neighbour* n;
for(int i = 1; i <= g->num_vertices; i++) {
v = &g->vertices[i];
if (!v->color) {
n = v->first_neighbour;
while (n != NULL) {
if (!g->vertices[n->id].color && i < n->id) {
fprintf(file, "%d\t%d\t%d\n", i, n->id, g->edges[n->edge_id].color);
}
n = n->next;
}
}
}
fclose(file);
}