-
Notifications
You must be signed in to change notification settings - Fork 5
/
read_nwk.c
53 lines (38 loc) · 1.13 KB
/
read_nwk.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
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// code for SIR on networks by Petter Holme (2018)
#include "sir.h"
extern NODE *n;
extern GLOBALS g;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// reads the network, assumes an edge list with vertex label 0,N-1
// if your network has nodes with degree zero, make sure that none of them is
// the node with largest index
void read_data (FILE *fp) {
unsigned int i, me, you;
g.n = 0;
// scan the system size
while (2 == fscanf(fp, "%u %u\n", &me, &you)) {
if (g.n < me) g.n = me;
if (g.n < you) g.n = you;
}
g.n++;
n = calloc(g.n, sizeof(NODE));
rewind(fp);
// scan the degrees
while (2 == fscanf(fp, "%u %u\n", &me, &you)) {
n[me].deg++;
n[you].deg++;
}
// allocate adjacency lists
for (i = 0; i < g.n; i++) {
n[i].nb = malloc(n[i].deg * sizeof(unsigned int));
n[i].deg = 0;
}
rewind(fp);
// fill adjacency lists
while (2 == fscanf(fp, "%u %u\n", &me, &you)) {
n[me].nb[n[me].deg++] = you;
n[you].nb[n[you].deg++] = me;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -