File: graph.c

package info (click to toggle)
swig 1.1p5-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 9,448 kB
  • ctags: 5,025
  • sloc: cpp: 21,599; ansic: 13,333; yacc: 3,297; python: 2,794; makefile: 2,197; perl: 1,984; tcl: 1,583; sh: 736; lisp: 201; objc: 143
file content (411 lines) | stat: -rw-r--r-- 8,872 bytes parent folder | download | duplicates (8)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* SWIG Graph Example
 *
 * Dave Beazley
 *
 * This code implements a few simple graph algorithms.
 * Our data structures are basically as follows :
 *
 *   1.  A linked list of nodes (containing all of the current
 *       nodes).
 *   2.  Each node has an adjacency list which is a linked list
 *       of all of the edges to other nodes.
 */

#include <stdio.h>
#include "graph.h"

int   V = 0;           /* Number of nodes */

Node    *head;         /* Linked list of nodes */
Node    *z; 
AdjList *az;           /* Terminator of the adjaceny lists */


/* ------------------------------------------------------------
  void init_graph()
 
  Initializes the graph code by clearing the linked list of nodes
  ------------------------------------------------------------ */
void init_graph() {

  Node *n, *n1;
  AdjList *l, *l1;
  int i;

  V = 0;
  if (head) {
    n = head->next_node;
    while (n != z) {
      l = n->adj;
      while (l != az) {
	l1 = l->next;
	free(l);
	l = l1;
      }
      n1 = n->next_node;
      free(n);
      n = n1;
    }
  }
    
  head = (Node *) malloc(sizeof(Node));
  z = (Node *) malloc(sizeof(Node));
  z->next_node = z;
  head->next_node = z;
  az = (AdjList *) malloc(sizeof(AdjList));
  az->next = az;
  az->v = 0;
}

/* --------------------------------------------------------
  Node *new_node()
 
  Creates a new node.   This node is added onto the linked
  list of nodes.   Returns a pointer to the new node.
  -------------------------------------------------------- */
                 
Node *new_node() {

  Node *t;
  t = (Node *) malloc(sizeof(Node));
  t->num = V++;
  t->adj = az;
  t->oldadj = az;
  t->next_node = head->next_node;
  head->next_node = t;
  return t;
}

/* --------------------------------------------------------
  void AddLink(Node *v1, Node *v2)
 
  Adds a directional link between node v1 and v2.  This
  just adds node v2 to node v1's adjacency list. Also checks
  for duplication.
  --------------------------------------------------------- */

void AddLink(Node *v1, Node *v2) {

  AdjList *l;

  /* First make sure we don't already have this link */

  l = v1->adj;
  while (l != az) {
    if ((Node *) l->v == v2) return;
    l = l->next;
  }
  if (v1 == v2) return;

  /* Create a new adjacency entry */

  l = (AdjList *) malloc(sizeof(AdjList));
  l->v = (Node *) v2;
  l->next = v1->adj;
  v1->adj = l;

}

/* -----------------------------------------------------------
   void FreeAdjList(AdjList *l)

   Frees the memory used by a adjacency list
   ----------------------------------------------------------- */

void FreeAdjList(AdjList *l) {

  AdjList *l1;
  while (l != az) {
    l1 = l->next;
    free(l);
    l = l1;
  }

}

/* -----------------------------------------------------------
   PrintNodes()

   A debugging function for printing out all of the nodes and
   and their adjacency lists.
   ------------------------------------------------------------ */

void PrintNodes() {

  Node *n, *v;
  AdjList *l;

  /* Print out all of the nodes */

  n = head->next_node;
  while (n != z) {
    printf("Node %d : ", n->num);
    l = n->adj;
    while (l != az) {
      v = (Node *) l->v;
      printf("%d ", v->num);
      l = l->next;
    }
    printf("\n");
    n = n->next_node;
  }

}

/* -------------------------------------------------------------
   find_Connected(Node *n)

   A function that finds all of the nodes connected to node N.
   Sets the node->visit flag to 1 for each node that is connected
   to n.   You'll probably want to set the node->visit flags to 0
   before calling this the first time.
   ------------------------------------------------------------- */

void find_Connected(Node *n) {
  AdjList *l;
  Node *v;

  if (n->visit) return;    /* Return if already seen this node */
  n->visit = 1;

  /* Walk down my adjacency list and look at those nodes */

  l = n->adj;
  while (l != az) {
    v = (Node *) l->v;
    find_Connected(v);
    l = l->next;
  }
}

/* --------------------------------------------------------------
   Connected(Node *n)

   Finds all of the nodes connected to node n.   Basically this is
   just the initialization code that sets all of the n->visit bytes
   to 0 and calls find_Connected.
   --------------------------------------------------------------- */
   
void Connected(Node *n) {

  Node *v;
  v = head->next_node;
  while (v != z) {
    v->visit = 0;
    v = v->next_node;
  }
  find_Connected(n);
}

/* --------------------------------------------------------------
   Closure_Step()
   
   This function computes one step of a transitive closure
   algorithm by adding more links.    Basically, whereever
   we have a link n1 --> n2 --> n3, this function will add
   the link n1 --> n3.    Call repeatedly to compute the
   transitive closure.

   Note : this is pretty ugly due to our choice of data structures
   --------------------------------------------------------------- */

void Closure_Step() {

  Node    *v,*v1,*n;
  AdjList *l, *l1, *l2;
  int  found;

  n = head->next_node;

  /* First save old adjacency lists and clear new ones */
  while (n != z) {
    n->oldadj = n->adj;
    n->adj = az;
    n = n->next_node;
  }

  /* Now go figure out who my new nodes ares */

  n = head->next_node;
  while (n != z) {

    /* Go down my adjacency list and add arcs */

    l = n->oldadj;
    while (l != az) {    /* Walk down my adjacency list */
      v = (Node *) l->v;
      l1 = v->oldadj;    /* Find out who's connected to my neighbors */
      while (l1 != az) { 
	v1 = (Node *) l1->v;
	l2 = n->oldadj;
	found = 0;
	while (l2 != az) {
	  if ((Node *) l2->v == v1) found = 1;
	  l2 = l2->next;
	}
	if ((!found) && (n != v1))
	  AddLink(n,v1);           /* Add an arc */
	l1 = l1->next;
      }
      l = l->next;
    }
    n = n->next_node;
  }

  /* Now connect everything back up again */

  n = head->next_node;
  while (n != z) {
    l = n->oldadj;
    if (l != az) {
      while (l->next != az) l = l->next;
      l->next = n->adj;  
      n->adj = n->oldadj;
      n->oldadj = az;
    }
    n = n->next_node;
  }
}

/* ----------------------------------------------------------------
   Shortest Path

   The following few functions compute the shortest path between
   two nodes.    This is done by using a breadth-first search
   algorithm.   Thus, I also need to implement some sort of queuing
   system as shown below 
   ---------------------------------------------------------------- */

typedef struct q {
  Node *n;
  struct q *next;
  struct q *prev;
} QNode;

QNode *qhead;
QNode *qtail;

/* Put something on the queue */

void queue_put(Node *n) {
  QNode *q;

  if (!qhead) {
    qhead = (QNode *) malloc(sizeof(QNode));
    qtail = (QNode *) malloc(sizeof(QNode));
    qhead->next = qtail;
    qtail->next = qtail;
    qhead->prev = qhead;
    qtail->prev = qhead;
  }

  q = (QNode *) malloc(sizeof(QNode));
  q->n = n;
  q->next = qtail;
  q->prev = qtail->prev;
  q->prev->next = q;
  qtail->prev = q;

}

/* Take something off the queue */

Node *queue_get() {

  QNode *q;
  Node *n;

  q = qhead->next;
  if (q == qtail) return NULL;
  qhead->next = q->next;
  q->next->prev = qhead;
  n = q->n;
  free(q);
  return n;
}

/* -----------------------------------------------------------
   AdjList *FindShort(Node *n1, Node *n2)

   Finds the shortest path between node n1 and node n2.
   Returns a linked list of nodes in the path.   This can easily
   be expressed using the AdjList structure already defined.
   ----------------------------------------------------------- */

AdjList *FindShort(Node *n1, Node *n2) {

  Node *v, *v1;
  AdjList *l;
  AdjList *path;
  
  v = head->next_node;
  while (v != z) {
    v->visit = 0;
    v->oldadj = az;
    v = v->next_node;
  }

  queue_put(n1);
  n1->visit = 1;
  while ((v = queue_get())) {
    if (v == n2) 
      break;
    else {
      l = v->adj;
      while (l != az) {
	v1 = (Node *) l->v;
	if (!v1->visit) {
	  queue_put(v1);
	  v1->visit = 1;
	  v1->oldadj = (AdjList *) malloc(sizeof(AdjList));
	  v1->oldadj->v = v;
	  v1->oldadj->next = az;
	}
	l = l->next;
      }
    }
  }

  /* Flush out queue */
  while ((v1 = queue_get()));

  /* If v is non-null, then we found something */

  if (v) {
    /* Backtrack through parents to trace out path */
    path = (AdjList *) malloc(sizeof(AdjList));
    path->next = az;
    path->v = n2;
    while (v != n1) {
      v->oldadj->next = path;
      path = v->oldadj;
      v->visit = 1;
      v1 = (Node *) (v->oldadj->v);
      v->oldadj = az;
      v = v1;
    }
    l = (AdjList *) malloc(sizeof(AdjList));
    l->v = n1;
    l->next = path;
  } else {
    l = 0;
  }

  /* Go through and clean up */
  
  v = head->next_node;
  while (v != z) {
    if (v->oldadj != az) free(v->oldadj);
    v = v->next_node;
  }

  /* Return the path */

  return l;

}