gkgraph.c 10.1 KB
Newer Older
lisj's avatar
lisj committed
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*!
\file  
\brief A simple frequent itemset discovery program to test GKlib's routines

\date 6/12/2008
\author George
\version \verbatim $Id: gkgraph.c 17700 2014-09-27 18:10:02Z karypis $ \endverbatim
*/

#include <GKlib.h>

/*************************************************************************/
/*! Data structures for the code */
/*************************************************************************/
typedef struct {
  int type;
  int niter;
  float eps;
  float lamda;

  char *infile;
  char *outfile;
} params_t;

/*************************************************************************/
/*! Constants */
/*************************************************************************/
#define CMD_NITER       1
#define CMD_EPS         2
#define CMD_LAMDA       3
#define CMD_TYPE        4
#define CMD_HELP        10


/*************************************************************************/
/*! Local variables */
/*************************************************************************/
static struct gk_option long_options[] = {
  {"type",       1,      0,      CMD_TYPE},
  {"niter",      1,      0,      CMD_NITER},
  {"lamda",      1,      0,      CMD_LAMDA},
  {"eps",        1,      0,      CMD_EPS},
  {"help",       0,      0,      CMD_HELP},
  {0,            0,      0,      0}
};


/*-------------------------------------------------------------------*/
/* Mini help  */
/*-------------------------------------------------------------------*/
static char helpstr[][100] = {
" ",
"Usage: gkgraph [options] <graph-file> [<out-file>]",
" ",
" Required parameters",
"  graph-file",
"     The name of the file storing the graph. The file is in ",
"     Metis' graph format.",
" ",
" Optional parameters",
"  -niter=int",
"     Specifies the maximum number of iterations. [default: 100]",
" ",
"  -lamda=float",
"     Specifies the follow-the-adjacent-links probability. [default: 0.80]",
" ",
"  -eps=float",
"     Specifies the error tollerance. [default: 1e-10]",
" ",
"  -help",
"     Prints this message.",
""
};

static char shorthelpstr[][100] = {
" ",
"   Usage: gkgraph [options] <graph-file> [<out-file>]",
"          use 'gkgraph -help' for a summary of the options.",
""
};
 


/*************************************************************************/
/*! Function prototypes */
/*************************************************************************/
double compute_compactness(params_t *params, gk_graph_t *graph, int32_t *perm);
void reorder_centroid(params_t *params, gk_graph_t *graph, int32_t *perm);
void print_init_info(params_t *params, gk_graph_t *graph);
void print_final_info(params_t *params);
params_t *parse_cmdline(int argc, char *argv[]);


/*************************************************************************/
/*! the entry point */
/**************************************************************************/
int main(int argc, char *argv[])
{
  ssize_t i, j, v;
  params_t *params;
  gk_graph_t *graph, *pgraph;
  int32_t *perm;
 
  /* get command-line options */
  params = parse_cmdline(argc, argv);

  /* read the data */
  graph = gk_graph_Read(params->infile, GK_GRAPH_FMT_METIS, -1, -1, 0, 0, 0);

  /* display some basic stats */
  print_init_info(params, graph);


  /* determine the initial compactness of the graph */
  printf("Initial compactness: %le\n", compute_compactness(params, graph, NULL));

  /* compute the BFS ordering and re-order the graph */
  //for (i=0; i<params->niter; i++) {
  for (i=0; i<1; i++) {
    v = RandomInRange(graph->nvtxs);
    gk_graph_ComputeBFSOrdering(graph, v, &perm, NULL);
    printf("BFS from %8d. Compactness: %le\n", 
        (int) v, compute_compactness(params, graph, perm));

    pgraph = gk_graph_Reorder(graph, perm, NULL);
    gk_graph_Write(pgraph, "bfs.metis", GK_GRAPH_FMT_METIS);
    gk_graph_Free(&pgraph);

    gk_graph_ComputeBestFOrdering(graph, v, params->type, &perm, NULL);
    printf("BestF from %8d. Compactness: %le\n", 
        (int) v, compute_compactness(params, graph, perm));

    pgraph = gk_graph_Reorder(graph, perm, NULL);
    gk_graph_Write(pgraph, "bestf.metis", GK_GRAPH_FMT_METIS);
    gk_graph_Free(&pgraph);

#ifdef XXX
    for (j=0; j<params->niter; j++) {
      reorder_centroid(params, graph, perm);
      printf("\tAfter centroid; Compactness: %le\n", 
          compute_compactness(params, graph, perm));
    }

    pgraph = gk_graph_Reorder(graph, perm, NULL);
    gk_graph_Write(pgraph, "centroid.metis", GK_GRAPH_FMT_METIS);
    gk_graph_Free(&pgraph);
#endif
    gk_free((void **)&perm, LTERM);
  }

  gk_graph_Free(&graph);
  //gk_graph_Free(&pgraph);

  print_final_info(params);
}




/*************************************************************************/
/*! This function computes the compactness of the graph's adjacency list */
/*************************************************************************/
double compute_compactness(params_t *params, gk_graph_t *graph, int32_t *perm)
{
  int i, v, u, nvtxs;
  ssize_t j, *xadj; 
  int32_t *adjncy;
  double compactness=0.0;
  int *freq;

  nvtxs  = graph->nvtxs;
  xadj   = graph->xadj;
  adjncy = graph->adjncy;

  freq = gk_ismalloc(nvtxs, 0, "compute_compactness: freq");

  for (i=0; i<nvtxs; i++) {
    v = (perm == NULL ? i : perm[i]);
    for (j=xadj[i]; j<xadj[i+1]; j++) {
      u = (perm == NULL ? adjncy[j] : perm[adjncy[j]]);
      compactness += abs(v-u);
      freq[gk_abs(v-u)]++;
    }
  }

  /*
  for (i=0; i<nvtxs; i++) {
    if (freq[i] > 0) 
      printf("%7d %6d\n", i, freq[i]);
  }
  */
  printf("\tnsmall: %d\n", freq[1]+freq[2]+freq[3]);

  return compactness/xadj[nvtxs];
}


/*************************************************************************/
/*! This function uses a centroid-based approach to refine the ordering */
/*************************************************************************/
void reorder_centroid(params_t *params, gk_graph_t *graph, int32_t *perm)
{
  int i, v, u, nvtxs;
  ssize_t j, *xadj; 
  int32_t *adjncy;
  gk_fkv_t *cand;
  double displacement;

  nvtxs  = graph->nvtxs;
  xadj   = graph->xadj;
  adjncy = graph->adjncy;

  cand = gk_fkvmalloc(nvtxs, "reorder_centroid: cand");

  for (i=0; i<nvtxs; i++) {
    v = perm[i];
    displacement = 0.0;

    for (j=xadj[i]; j<xadj[i+1]; j++) {
      u = perm[adjncy[j]];
      displacement += u-v;
      //displacement += sign(u-v, sqrt(abs(u-v)));
    }

    cand[i].val = i;
    cand[i].key = v + displacement*params->lamda/(xadj[i+1]-xadj[i]);
  }

  /* sort them based on the target position in increasing order */
  gk_fkvsorti(nvtxs, cand);


  /* derive the permutation from the ordered list */
  gk_i32set(nvtxs, -1, perm);
  for (i=0; i<nvtxs; i++) {
    if (perm[cand[i].val] != -1)
      errexit("Resetting perm[%d] = %d\n", cand[i].val, perm[cand[i].val]);
    perm[cand[i].val] = i;
  }

  gk_free((void **)&cand, LTERM);
}


/*************************************************************************/
/*! This function prints run parameters */
/*************************************************************************/
void print_init_info(params_t *params, gk_graph_t *graph)
{
  printf("*******************************************************************************\n");
  printf(" gkgraph\n\n");
  printf("Graph Information ----------------------------------------------------------\n");
  printf(" input file=%s, [%d, %zd]\n", 
      params->infile, graph->nvtxs, graph->xadj[graph->nvtxs]);

  printf("\n");
  printf("Options --------------------------------------------------------------------\n");
  printf(" type=%d, niter=%d, lamda=%f, eps=%e\n",
      params->type, params->niter, params->lamda, params->eps);

  printf("\n");
  printf("Working... -----------------------------------------------------------------\n");
}


/*************************************************************************/
/*! This function prints final statistics */
/*************************************************************************/
void print_final_info(params_t *params)
{
  printf("\n");
  printf("Memory Usage Information -----------------------------------------------------\n");
  printf("   Maximum memory used:              %10zd bytes\n", (ssize_t) gk_GetMaxMemoryUsed());
  printf("   Current memory used:              %10zd bytes\n", (ssize_t) gk_GetCurMemoryUsed());
  printf("********************************************************************************\n");
}


/*************************************************************************/
/*! This is the entry point of the command-line argument parser */
/*************************************************************************/
params_t *parse_cmdline(int argc, char *argv[])
{
  int i;
  int c, option_index;
  params_t *params;

  params = (params_t *)gk_malloc(sizeof(params_t), "parse_cmdline: params");

  /* initialize the params data structure */
  params->type      = 1;
  params->niter     = 1;
  params->eps       = 1e-10;
  params->lamda     = 0.20;
  params->infile    = NULL;


  /* Parse the command line arguments  */
  while ((c = gk_getopt_long_only(argc, argv, "", long_options, &option_index)) != -1) {
    switch (c) {
      case CMD_TYPE:
        if (gk_optarg) params->type = atoi(gk_optarg);
        break;
      case CMD_NITER:
        if (gk_optarg) params->niter = atoi(gk_optarg);
        break;
      case CMD_EPS:
        if (gk_optarg) params->eps = atof(gk_optarg);
        break;
      case CMD_LAMDA:
        if (gk_optarg) params->lamda = atof(gk_optarg);
        break;

      case CMD_HELP:
        for (i=0; strlen(helpstr[i]) > 0; i++)
          printf("%s\n", helpstr[i]);
        exit(0);
        break;
      case '?':
      default:
        printf("Illegal command-line option(s)\nUse %s -help for a summary of the options.\n", argv[0]);
        exit(0);
    }
  }

  if (argc-gk_optind != 1) {
    printf("Unrecognized parameters.");
    for (i=0; strlen(shorthelpstr[i]) > 0; i++)
      printf("%s\n", shorthelpstr[i]);
    exit(0);
  }

  params->infile  = gk_strdup(argv[gk_optind++]);

  if (argc-gk_optind > 0) 
    params->outfile = gk_strdup(argv[gk_optind++]);
  else
    params->outfile   = gk_strdup("gkgraph.out");

  if (!gk_fexists(params->infile))
    errexit("input file %s does not exist.\n", params->infile);

  return params;
}