gen2.cpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 /* This gnerator generates $G_{-1/1,dens}$ :
4  A graph with density $dens$ and the existing edges have weights uniformly
5  from {-1,1}.
6 */
7 
8 #include <cstdio>
9 #include <cstdlib>
10 #include <algorithm>
11 
12 int
13 main(int argc, char* argv[])
14 {
15  if (argc != 4) {
16  printf("Usage: gen2 <nodenum> <density> <seed>\n");
17  return -1;
18  }
19 
20  const int num = atoi(argv[1]);
21  const double dens = atof(argv[2]);
22  const int seed = atoi(argv[3]);
23 
24  srand48(seed);
25 
26  const int maxedgenum = (num*(num-1)) / 2;
27  int * pick = new int[maxedgenum];
28  int k = 0;
29  for (int i = 1; i < num; ++i) {
30  for (int j = i + 1; j <= num; ++j) {
31  pick[k++] = (i << 16) + j;
32  }
33  }
34 
35  random_shuffle(pick, pick + maxedgenum);
36  const int edgenum = static_cast<int>(maxedgenum * dens);
37 
38  printf("%i %i\n", num, edgenum);
39 
40  for (k = 0; k < edgenum; ++k) {
41  const int i = pick[k] >> 16;
42  const int j = pick[k] & 0xffff;
43  if (drand48() >= .5)
44  printf("%3i %3i -1\n", i-1, j-1);
45  else
46  printf("%3i %3i 1\n", i-1, j-1);
47  }
48 
49  return 0;
50 }
int main(int argc, char *argv[])
Definition: BB_tm.cpp:32
static char * j
Definition: OSdtoa.cpp:3622
void fint fint * k