-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag.c
45 lines (34 loc) · 1.03 KB
/
dag.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
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MIN_PER_RANK 2 /* Nodes/Rank: How 'fat' the DAG should be. */
#define MAX_PER_RANK 7
#define PERCENT 10 /* Chance of having an Edge. */
int main(int argc, char *argv[])
{
int i, j, k, nodes = 0, edges = 0;
int ranks = atoi(argv[1]);
char command[256];
FILE *outfile;
srand(time(NULL));
outfile = fopen("dag.txt", "w");
for (i = 1; i <= ranks; i++) {
/* New nodes of 'higher' rank than all nodes generated till now. */
int new_nodes = MIN_PER_RANK + (rand () % (MAX_PER_RANK - MIN_PER_RANK + 1));
/* Edges from old nodes ('nodes') to new ones ('new_nodes'). */
for (j = 1; j <= nodes; j++) {
for (k = 1; k <= new_nodes; k++) {
if ((rand() % 100 + 1) < PERCENT) {
fprintf(outfile, "%d %d\n", j, k + nodes);
edges++;
}
}
}
nodes += new_nodes; /* Accumulate into old node set. */
}
fclose(outfile);
sprintf(command, "sed -i '1s/^/%d %d %d\\n/' dag.txt", nodes, nodes, edges);
system(command);
return 0;
}