summaryrefslogtreecommitdiff
path: root/src/johnson/johnson_cycles.c
blob: 5135b20848619a455c60895832cfd9a524e60ff5 (plain)
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
/**
 *  This program is free software: you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 *  (c) Vincenzo Nicosia 2009-2017 -- <v.nicosia@qmul.ac.uk>
 * 
 *  This file is part of NetBunch, a package for complex network
 *  analysis and modelling. For more information please visit:
 *
 *             http://www.complex-networks.net/
 *
 *  If you use this software, please add a reference to 
 *
 *               V. Latora, V. Nicosia, G. Russo             
 *   "Complex Networks: Principles, Methods and Applications"
 *              Cambridge University Press (2017) 
 *                ISBN: 9781107103184
 *
 ***********************************************************************
 *
 *  Compute all the simple cycles of an undirected graph provided as
 *  input (optionally, only up to a certain length) using the Johnson
 *  algorithm.
 *
 *  References:
 *
 * [1] D. B. Johnson. "Finding All the Elementary Circuits of a
 *     Directed Graph". SIAM J. Comput. 4 (1975), 77-84.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "utils.h"
#include "stack.h"


#define FALSE 0
#define TRUE  1


void usage(char *argv[]){

  printf("********************************************************************\n"
         "**                                                                **\n"
         "**                    -*- johnson_cycles -*-                      **\n"
         "**                                                                **\n"
         "**  Enumerates all the elementary cycles of a graph on input.     **\n"
         "**                                                                **\n"
         "**  If 'graph_in' is equal to '-' (dash), read the edge list      **\n"
         "**  from standard input (STDIN).                                  **\n"
         "**                                                                **\n"
         "**  If 'max_length' is provided, ignore all cycles whose length   **\n"
         "**  is larger than 'max_length'                                   **\n"
         "**                                                                **\n"
         "**  The program prints on output a row for each cycle length,     **\n"
         "**  in the format:                                                **\n"
         "**                                                                **\n"
         "**        2 N_2                                                   **\n"
         "**        3 N_3                                                   **\n"
         "**        4 N_4                                                   **\n"
         "**        5 N_5                                                   **\n"
         "**        ....                                                    **\n"
         "**                                                                **\n"
         "**  where 2, 3, 4, 5, ... is the length of the cycle, and N_2,    **\n"
         "**  N_3, N_4, N_5, ... is the number of cycles of that length.    **\n"
         "**  Notice that in undirected graphs each cycle is reported       **\n"
         "**  (and counted) twice, once for each possible direction in      **\n"
         "**  which the cycle can be traversed.                             **\n"
         "**                                                                **\n"         
         "**  If the third parameter is equal to 'SHOW', the program will   **\n"         
         "**  dump all the found cycles on STDERR, using the format:        **\n"
         "**                                                                **\n"
         "**           N_(l-1) N_(l-2)... N_0                               **\n"
         "**                                                                **\n"
         "**  where N_(l-i) is the i-th node in the cycle starting at node  **\n"
         "**  N_0. Notice that according to this notation we report only    **\n"
         "**  'l' nodes belonging to the cycle, in reversed traversal       **\n"
         "**  order, avoiding to report the starting node (N_0) twice.      **\n"
         "**                                                                **\n"
         "**  The program shows also  cycles of length '2', that in         **\n"
         "**  undirected graphs correspond to the degenerate cycle obtained **\n"
         "**  by traversing the same edge in the two directions.            **\n"
         "**                                                                **\n"
         "********************************************************************\n"
         " This is Free Software - You can use and distribute it under \n"
         " the terms of the GNU General Public License, version 3 or later\n\n"
         " Please visit http://www.complex-networks.net for more information\n\n"
         " (c) Vincenzo Nicosia 2009-2017 (v.nicosia@qmul.ac.uk)\n"
         "********************************************************************\n\n"
         );
  printf("Usage: %s <graph_in> [<max_length> [SHOW]]\n", argv[0]);

}


typedef struct{
  unsigned int num;
  double *c;
} count_t;


/**
 *
 * This structure contains all the information needed by the algorithm
 * to run:
 *
 * - N is the number of nodes
 *
 * - K is twice the number of edges
 *
 * - J_slap and r_slap are the usual SLAP representation of the
 *    adjacency matrix
 *
 * - B_len is an array with the length of the set B(u) for each node u
 *
 * - B is a vector of character of the same size of J_slap, which
 *   stores the values of the indicator vector B(u) for each node u
 * 
 * - blocked is an indicator vector of size N
 *
 * - v is the stack of nodes
 *
 * - stats maintains the counts for all the cycle lengths
 * 
 */
typedef struct{
  unsigned int N;
  unsigned int K;
  unsigned int *J_slap;
  unsigned int *r_slap;
  my_stack_t *stack;
  int max_depth;
  count_t stats;
  unsigned int *B_len;
  unsigned int *B;
  char *blocked;
  char show;
} algo_info_t;




void unblock(algo_info_t *info, unsigned int u){
  
  unsigned int w, idx;
  
  info->blocked[u] = FALSE;
  
  while(info->B_len[u] >0){
    info->B_len[u] -= 1;
    idx = info->r_slap[u] + info->B_len[u];
    w = info->B[idx];
    if (info->blocked[w] == TRUE){
      unblock(info, w);
    }
  }
}




/* add v to B(w) */
void set_B(algo_info_t *info, unsigned int v, unsigned int w){
  
  unsigned int idx, i;

  idx = info->r_slap[w];
  
  
  for(i=0; i <info->B_len[w]; i ++){
    if (info->B[idx + i] == v)
      return;
  }
  info->B[idx + info->B_len[w]] = v;
  info->B_len[w] += 1;
  
}

char circuit(algo_info_t *info, unsigned int s, unsigned int v){

  unsigned int i, w;
  char f = FALSE;
  
  if (v < s)
    return FALSE;
  
  /* check if maximum depth has been reached */
  if(stack_size(info->stack) == info->max_depth){
    return TRUE;
  }
  
  stack_push(info->stack, v);
  
  info->blocked[v] = TRUE;
  for(i=info->r_slap[v]; i< info->r_slap[v+1]; i++){
    w = info->J_slap[i];
    if (w == s){
      /* output a cycle here */
      if (info->show)
        stack_dump(info->stack, stderr);
      info->stats.c[stack_size(info->stack)] += 1;
      f = TRUE;
    }
    if (w < s)
      continue; /* We only consider nodes with higher IDs */
    else
      if (! (info->blocked[w])){
        if (circuit(info, s, w) == TRUE)
          f = TRUE;
      }
  }
  if (f == TRUE) {
    unblock(info, v);
  }
  else{
    for(i=info->r_slap[v]; i < info->r_slap[v+1]; i++){
      w = info->J_slap[i];
      if (w > s)
        set_B(info, v, w); /* add v to B(w) only if v is not already in B(w) */
    }
  }
  stack_pop(info->stack, &w);
  return f;
}


void johnson_cycles(algo_info_t *info){
  
  unsigned int i, N;
  
  N = info->N;
  
  
  for(i=0; i<N; i++){
    /* clear the vector of blocked nodes */
    memset(info->blocked, 0, N * sizeof(char));
    
    /* clear the indicator vector B */
    
    /* we reset the length of each  set B*/
    memset(info->B_len, 0, N * sizeof(unsigned int)); 
    circuit(info, i, i);
  }
  
  
}


int main(int argc, char *argv[]){
  
  algo_info_t info;
  FILE *filein;
  unsigned int i;

  if (argc < 2){
    usage(argv);
    exit(1);
  }
  
  info.J_slap = NULL;
  info.r_slap = NULL;
  

  if(!strcmp(argv[1], "-")){
    filein = stdin;
  }
  else{
    filein = openfile_or_exit(argv[1], "r", 2);
  }
  
  read_slap(filein, &(info.K), &(info.N), &(info.J_slap), &(info.r_slap));

  fclose(filein);
  
  info.B = malloc(info.r_slap[info.N] * sizeof(unsigned int)); /* This is the indicator vector B(u) */
  info.B_len = malloc(info.N * sizeof(unsigned int)); /* lengths of  the B(u) */
  info.blocked = malloc(info.N * sizeof(char)); /* This is the indicator vector
                                                   of blocked nodes */
  
  info.stack = malloc(sizeof(my_stack_t)); 
  stack_create(info.stack);
  
  info.show = FALSE;
  if (argc > 2){
    info.max_depth = atoi(argv[2]);
    if (argc > 3){
      if (!my_strcasecmp(argv[3], "show")){
        info.show = TRUE;
      }
    }
  }
  else{
    info.max_depth = info.N;
  }
  
  /* Here we initialise the counters */
  info.stats.num = info.max_depth + 1;
  info.stats.c = malloc(info.stats.num * sizeof(double));
  /* Here we set the counters to zero */
  memset(info.stats.c, 0, info.stats.num * sizeof(double));
  johnson_cycles(&info);
  
  for(i=2; i<info.stats.num ; i ++){
    printf("%d %1.20g\n", i, info.stats.c[i]);
  }
  free(info.B);
  free(info.B_len);
  free(info.blocked);
  free(info.stack->v);
  free(info.stack);
  free(info.stats.c);
  free(info.r_slap);
  free(info.J_slap);
}