Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Doc + use all arguments in free function.
[simgrid.git] / src / xbt / graph.c
1 /*      $Id$     */
2
3 /* a generic graph library.                                                 */
4
5 /* Copyright (c) 2006 Darina Dimitrova, Arnaud Legrand.                     */
6 /* All rights reserved.                                                     */
7
8 /* This program is free software; you can redistribute it and/or modify it
9  * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <stdlib.h>
12 #include "xbt/sysdep.h"
13 #include "xbt/log.h"
14 #include "xbt/graph.h"
15 #include "graph_private.h"
16 #include "xbt/graphxml_parse.h"
17 #include "xbt/dict.h"
18 #include "xbt/heap.h"
19
20
21
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_graph, xbt, "Graph");
24
25
26
27 /** @brief Constructor
28  *  @return a new graph
29  */
30 xbt_graph_t xbt_graph_new_graph(unsigned short int directed, void *data)
31 {
32   xbt_graph_t graph = NULL;
33   graph = xbt_new0(struct xbt_graph, 1);
34   graph->directed = directed;
35   graph->data = data;
36   graph->nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
37   graph->edges = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
38
39   return graph;
40 }
41
42 /** @brief add a node to the given graph */
43 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
44 {
45   xbt_node_t node = NULL;
46   node = xbt_new0(struct xbt_node, 1);
47   node->data = data;
48   if (g->directed)
49     /* only the "out" field is used */
50     node->in = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
51
52   node->out = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
53   node->position_x = -1.0;
54   node->position_y = -1.0;
55
56   xbt_dynar_push(g->nodes, &node);
57
58   return node;
59 }
60
61 /** @brief add an edge to the given graph */
62 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g,
63                               xbt_node_t src, xbt_node_t dst, void *data)
64 {
65   xbt_edge_t edge = NULL;
66
67   edge = xbt_new0(struct xbt_edge, 1);
68   xbt_dynar_push(src->out, &edge);
69   if (g->directed)
70     xbt_dynar_push(dst->in, &edge);
71   else                          /* only the "out" field is used */
72     xbt_dynar_push(dst->out, &edge);
73
74   edge->data = data;
75   edge->src = src;
76   edge->dst = dst;
77
78   xbt_dynar_push(g->edges, &edge);
79
80   return edge;
81 }
82
83 xbt_edge_t xbt_graph_get_edge(xbt_graph_t g, xbt_node_t src, xbt_node_t dst)
84 {
85   xbt_edge_t edge;
86   int cursor;
87
88   xbt_dynar_foreach(src->out, cursor, edge) {
89     DEBUG3("%p = %p--%p",edge,edge->src,edge->dst);
90     if((edge->src==src) && (edge->dst==dst)) return edge;
91   }
92   if(!g->directed) {
93     xbt_dynar_foreach(src->out, cursor, edge) {
94       DEBUG3("%p = %p--%p",edge,edge->src,edge->dst);
95       if((edge->dst==src) && (edge->src==dst)) return edge;
96     }
97   }
98   return NULL;
99 }
100
101 void *xbt_graph_node_get_data(xbt_node_t node)
102 {
103   return node->data;
104 }
105
106 void xbt_graph_node_set_data(xbt_node_t node, void *data)
107 {
108   node->data = data;
109 }
110
111 void *xbt_graph_edge_get_data(xbt_edge_t edge)
112 {
113   return edge->data;
114 }
115
116 void xbt_graph_edge_set_data(xbt_edge_t edge, void *data)
117 {
118   edge->data = data;
119 }
120
121 /** @brief Destructor
122  *  @param g: poor victim
123  *  @param node_free_function: function to use to free data associated to each node
124  *  @param edge_free_function: function to use to free data associated to each edge
125  *  @param graph_free_function: function to use to free data associated to g
126  *
127  * Free the graph structure. 
128  */
129 void xbt_graph_free_graph(xbt_graph_t g,
130                           void_f_pvoid_t * node_free_function,
131                           void_f_pvoid_t * edge_free_function,
132                           void_f_pvoid_t * graph_free_function)
133 {
134   int cursor = 0;
135   xbt_node_t node = NULL;
136   xbt_edge_t edge = NULL;
137
138
139   xbt_dynar_foreach(g->nodes, cursor, node) {
140     xbt_dynar_free(&(node->out));
141     xbt_dynar_free(&(node->in));
142     if (node_free_function)
143       (*node_free_function)(node->data);
144   }
145
146   xbt_dynar_foreach(g->edges, cursor, edge) {
147     if (edge_free_function)
148       (*edge_free_function)(edge->data);
149   }
150
151   xbt_dynar_foreach(g->nodes, cursor, node)
152       free(node);
153   xbt_dynar_free(&(g->nodes));
154
155   xbt_dynar_foreach(g->edges, cursor, edge)
156       free(edge);
157   xbt_dynar_free(&(g->edges));
158   if(graph_free_function) (*graph_free_function)(g->data);
159   free(g);
160
161   return;
162 }
163
164
165 /** @brief remove the given node from the given graph */
166 void xbt_graph_free_node(xbt_graph_t g, xbt_node_t n,
167                          void_f_pvoid_t * node_free_function,
168                          void_f_pvoid_t * edge_free_function)
169 {
170   unsigned long nbr;
171   int i;
172   int cursor = 0;
173   xbt_node_t node = NULL;
174   xbt_edge_t edge = NULL;
175
176   nbr = xbt_dynar_length(g->edges);
177   cursor = 0;
178   for (i = 0; i < nbr; i++) {
179     xbt_dynar_get_cpy(g->edges, cursor, &edge);
180
181     if ((edge->dst == n) || (edge->src == n)) {
182       xbt_graph_free_edge(g, edge, edge_free_function);
183     } else
184       cursor ++;
185   }
186
187   if ((node_free_function) && (n->data))
188     node_free_function(n->data);
189
190   cursor = 0;
191   xbt_dynar_foreach(g->nodes, cursor, node)
192     if (node == n)
193       xbt_dynar_cursor_rm(g->nodes, &cursor);
194
195   xbt_dynar_free(&(n->in));
196   xbt_dynar_free(&(n->out));
197
198   free(n);
199
200   return;
201 }
202
203 /** @brief remove the given edge from the given graph */
204 void xbt_graph_free_edge(xbt_graph_t g, xbt_edge_t e,
205                          void_f_pvoid_t *free_function)
206 {
207   int idx;
208   int cursor = 0;
209   xbt_edge_t edge = NULL;
210
211   if ((free_function) && (e->data))
212     (*free_function)(e->data);
213
214   xbt_dynar_foreach(g->edges, cursor, edge) {
215     if (edge == e) {
216       if (g->directed) {
217         idx = __xbt_find_in_dynar(edge->dst->in, edge);
218         xbt_dynar_remove_at(edge->dst->in, idx, NULL);
219       } else {                  /* only the out field is used */
220         idx = __xbt_find_in_dynar(edge->dst->out, edge);
221         xbt_dynar_remove_at(edge->dst->out, idx, NULL);
222       }
223
224       idx = __xbt_find_in_dynar(edge->src->out, edge);
225       xbt_dynar_remove_at(edge->src->out, idx, NULL);
226
227       xbt_dynar_cursor_rm(g->edges, &cursor);
228       free(edge);
229       break;
230     }
231   }
232 }
233
234 int __xbt_find_in_dynar(xbt_dynar_t dynar, void *p)
235 {
236
237   int cursor = 0;
238   void *tmp = NULL;
239
240   xbt_dynar_foreach(dynar, cursor, tmp) {
241     if (tmp == p)
242       return cursor;
243   }
244   return -1;
245 }
246
247 /** @brief Retrieve the graph's nodes as a dynar */
248 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
249 {
250   return g->nodes;
251 }
252
253 /** @brief Retrieve the graph's edges as a dynar */
254 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
255 {
256   return g->edges;
257 }
258
259 /** @brief Retrieve the node at the source of the given edge */
260 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
261 {
262
263   return e->src;
264 }
265
266 /** @brief Retrieve the node being the target of the given edge */
267 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
268 {
269   return e->dst;
270 }
271
272
273 /** @brief Set the weight of the given edge */
274 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
275 {
276   e->length = length;
277
278 }
279
280 double xbt_graph_edge_get_length(xbt_edge_t e)
281 {
282   return e->length;
283 }
284
285
286 /** @brief construct the adjacency matrix corresponding to the given graph
287  * 
288  * The weights are the distances between nodes
289  */
290 double *xbt_graph_get_length_matrix(xbt_graph_t g)
291 {
292   int cursor = 0;
293   int in_cursor = 0;
294   int idx, i;
295   unsigned long n;
296   xbt_edge_t edge = NULL;
297   xbt_node_t node = NULL;
298   double *d = NULL;
299
300 # define D(u,v) d[(u)*n+(v)]
301   n = xbt_dynar_length(g->nodes);
302
303   d = (double *) xbt_new0(double, n * n);
304
305   for (i = 0; i < n * n; i++) {
306     d[i] = -1.0;
307   }
308
309   xbt_dynar_foreach(g->nodes, cursor, node) {
310     in_cursor = 0;
311     D(cursor, cursor) = 0;
312
313     xbt_dynar_foreach(node->out, in_cursor, edge) {
314       if (edge->dst == node)
315         idx = __xbt_find_in_dynar(g->nodes, edge->src);
316       else                      /*case of  undirected graphs */
317         idx = __xbt_find_in_dynar(g->nodes, edge->dst);
318       D(cursor, idx) = edge->length;
319     }
320   }
321
322 # undef D
323
324   return d;
325 }
326
327 /** @brief Floyd-Warshall algorithm for shortest path finding
328  * 
329  * From wikipedia: 
330  * 
331  * The Floyd–Warshall algorithm takes as input an adjacency matrix
332  * representation of a weighted, directed graph (V, E). The weight of a
333  * path between two vertices is the sum of the weights of the edges along
334  * that path. The edges E of the graph may have negative weights, but the
335  * graph must not have any negative weight cycles. The algorithm computes,
336  * for each pair of vertices, the minimum weight among all paths between
337  * the two vertices. The running time complexity is Θ(|V|3).
338  */
339 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
340                          xbt_node_t * p)
341 {
342   int i, j, k;
343   unsigned long n;
344   n = xbt_dynar_length(g->nodes);
345
346 # define D(u,v) d[(u)*n+(v)]
347 # define P(u,v) p[(u)*n+(v)]
348
349   for (i = 0; i < n * n; i++) {
350     d[i] = adj[i];
351   }
352
353
354   for (i = 0; i < n; i++) {
355     for (j = 0; j < n; j++) {
356       if (D(i, j) != -1) {
357         P(i, j) = *((xbt_node_t *) xbt_dynar_get_ptr(g->nodes, i));
358       }
359     }
360   }
361
362   for (k = 0; k < n; k++) {
363     for (i = 0; i < n; i++) {
364       for (j = 0; j < n; j++) {
365         if ((D(i, k) != -1) && (D(k, j) != -1)) {
366           if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j))) {
367             D(i, j) = D(i, k) + D(k, j);
368             P(i, j) = P(k, j);
369           }
370         }
371       }
372     }
373   }
374
375
376
377 # undef P
378 # undef D
379 }
380
381 /** @brief computes all-pairs shortest paths */
382 xbt_node_t *xbt_graph_shortest_paths(xbt_graph_t g)
383 {
384   xbt_node_t *p;
385   xbt_node_t *r;
386   int i, j, k;
387   unsigned long n;
388
389   double *adj = NULL;
390   double *d = NULL;
391
392 # define P(u,v) p[(u)*n+(v)]
393 # define R(u,v) r[(u)*n+(v)]
394
395   n = xbt_dynar_length(g->nodes);
396   adj = xbt_graph_get_length_matrix(g);
397   d = xbt_new0(double, n * n);
398   p = xbt_new0(xbt_node_t, n * n);
399   r = xbt_new0(xbt_node_t, n * n);
400
401   xbt_floyd_algorithm(g, adj, d, p);
402
403   for (i = 0; i < n; i++) {
404     for (j = 0; j < n; j++) {
405       k = j;
406
407       while ((P(i, k)) && (__xbt_find_in_dynar(g->nodes, P(i, k)) != i)) {
408         k = __xbt_find_in_dynar(g->nodes, P(i, k));
409       }
410
411       if (P(i, j)) {
412         R(i, j) = *((xbt_node_t *) xbt_dynar_get_ptr(g->nodes, k));
413       }
414     }
415   }
416 # undef R
417 # undef P
418
419   free(d);
420   free(p);
421   free(adj);
422   return r;
423 }
424
425 /** @brief Extract a spanning tree of the given graph */
426 xbt_edge_t *xbt_graph_spanning_tree_prim(xbt_graph_t g)
427 {
428   int tree_size = 0;
429   int tree_size_max = xbt_dynar_length(g->nodes) - 1;
430   xbt_edge_t *tree = xbt_new0(xbt_edge_t, tree_size_max);
431   xbt_edge_t e, edge;
432   xbt_node_t node = NULL;
433   xbt_dynar_t edge_list = NULL;
434   xbt_heap_t heap = xbt_heap_new(10, NULL);
435   int cursor;
436
437   xbt_assert0(!(g->directed),
438               "Spanning trees do not make sense on directed graphs");
439
440   xbt_dynar_foreach(g->nodes, cursor, node) {
441     node->xbtdata = NULL;
442   }
443
444   node = xbt_dynar_getfirst_as(g->nodes, xbt_node_t);
445   node->xbtdata = (void *) 1;
446   edge_list = node->out;
447   xbt_dynar_foreach(edge_list, cursor, e)
448       xbt_heap_push(heap, e, -(e->length));
449
450   while ((edge = xbt_heap_pop(heap))) {
451     if ((edge->src->xbtdata) && (edge->dst->xbtdata))
452       continue;
453     tree[tree_size++] = edge;
454     if (!(edge->src->xbtdata)) {
455       edge->src->xbtdata = (void *) 1;
456       edge_list = edge->src->out;
457       xbt_dynar_foreach(edge_list, cursor, e) {
458         xbt_heap_push(heap, e, -(e->length));
459       }
460     } else {
461       edge->dst->xbtdata = (void *) 1;
462       edge_list = edge->dst->out;
463       xbt_dynar_foreach(edge_list, cursor, e) {
464         xbt_heap_push(heap, e, -(e->length));
465       }
466     }
467     if (tree_size == tree_size_max)
468       break;
469   }
470
471   xbt_heap_free(heap);
472
473   return tree;
474 }
475
476 /** @brief Topological sort on the given graph 
477  *
478  *  From wikipedia:
479  * 
480  * In graph theory, a topological sort of a directed acyclic graph (DAG) is
481  * a linear ordering of its nodes which is compatible with the partial
482  * order R induced on the nodes where x comes before y (xRy) if there's a
483  * directed path from x to y in the DAG. An equivalent definition is that
484  * each node comes before all nodes to which it has edges. Every DAG has at
485  * least one topological sort, and may have many.
486  */
487 xbt_node_t *xbt_graph_topo_sort(xbt_graph_t g)
488 {
489
490   xbt_node_t *sorted;
491   int cursor, idx;
492   xbt_node_t node;
493   unsigned long n;
494
495   n = xbt_dynar_length(g->nodes);
496   idx = n - 1;
497
498   sorted = xbt_malloc(n * sizeof(xbt_node_t));
499
500   xbt_dynar_foreach(g->nodes, cursor, node)
501     node->xbtdata = xbt_new0(int, 1);
502
503   xbt_dynar_foreach(g->nodes, cursor, node)
504     xbt_graph_depth_visit(g, node, sorted, &idx);
505
506   xbt_dynar_foreach(g->nodes, cursor, node) {
507     free(node->xbtdata);
508     node->xbtdata = NULL;
509   }
510
511   return sorted;
512 }
513
514 /** @brief First-depth graph traversal */
515 void xbt_graph_depth_visit(xbt_graph_t g, xbt_node_t n,
516                            xbt_node_t * sorted, int *idx)
517 {
518   int cursor;
519   xbt_edge_t edge;
520
521   if (*((int *) (n->xbtdata)) == ALREADY_EXPLORED)
522     return;
523   else if (*((int *) (n->xbtdata)) == CURRENTLY_EXPLORING)
524     THROW0(0, 0, "There is a cycle");
525   else {
526     *((int *) (n->xbtdata)) = CURRENTLY_EXPLORING;
527
528     xbt_dynar_foreach(n->out, cursor, edge) {
529       xbt_graph_depth_visit(g, edge->dst, sorted, idx);
530     }
531
532     *((int *) (n->xbtdata)) = ALREADY_EXPLORED;
533     sorted[(*idx)--] = n;
534   }
535 }
536
537 /********************* Import and Export ******************/
538 static xbt_graph_t parsed_graph = NULL;
539 static xbt_dict_t parsed_nodes = NULL;
540
541 static void *(*__parse_node_label_and_data) (xbt_node_t, const char *,
542                                              const char *) = NULL;
543 static void *(*__parse_edge_label_and_data) (xbt_edge_t, const char *,
544                                              const char *) = NULL;
545
546 static void __parse_graph_begin(void)
547 {
548   DEBUG0("<graph>");
549   if (A_graphxml_graph_isDirected == A_graphxml_graph_isDirected_true)
550     parsed_graph = xbt_graph_new_graph(1, NULL);
551   else
552     parsed_graph = xbt_graph_new_graph(0, NULL);
553
554   parsed_nodes = xbt_dict_new();
555 }
556
557 static void __parse_graph_end(void)
558 {
559   xbt_dict_free(&parsed_nodes);
560   DEBUG0("</graph>");
561 }
562
563 static void __parse_node(void)
564 {
565   xbt_node_t node = xbt_graph_new_node(parsed_graph, NULL);
566
567   DEBUG1("<node name=\"%s\"/>", A_graphxml_node_name);
568   if (__parse_node_label_and_data)
569     node->data = __parse_node_label_and_data(node, A_graphxml_node_label,
570                                              A_graphxml_node_data);
571   xbt_graph_parse_get_double(&(node->position_x),
572                              A_graphxml_node_position_x);
573   xbt_graph_parse_get_double(&(node->position_y),
574                              A_graphxml_node_position_y);
575
576   xbt_dict_set(parsed_nodes, A_graphxml_node_name, (void *) node, NULL);
577 }
578
579 static void __parse_edge(void)
580 {
581   xbt_edge_t edge = xbt_graph_new_edge(parsed_graph,
582                                        xbt_dict_get(parsed_nodes,
583                                                     A_graphxml_edge_source),
584                                        xbt_dict_get(parsed_nodes,
585                                                     A_graphxml_edge_target),
586                                        NULL);
587
588   if (__parse_edge_label_and_data)
589     edge->data = __parse_edge_label_and_data(edge, A_graphxml_edge_label,
590                                              A_graphxml_edge_data);
591
592   xbt_graph_parse_get_double(&(edge->length), A_graphxml_edge_length);
593
594   DEBUG3("<edge  source=\"%s\" target=\"%s\" length=\"%f\"/>",
595          (char *) (edge->src)->data,
596          (char *) (edge->dst)->data, xbt_graph_edge_get_length(edge));
597 }
598
599 /** @brief Import a graph from a file following the GraphXML format */
600 xbt_graph_t xbt_graph_read(const char *filename,
601                            void *(node_label_and_data) (xbt_node_t,
602                                                         const char *,
603                                                         const char *),
604                            void *(edge_label_and_data) (xbt_edge_t,
605                                                         const char *,
606                                                         const char *))
607 {
608
609   xbt_graph_t graph = NULL;
610
611   __parse_node_label_and_data = node_label_and_data;
612   __parse_edge_label_and_data = edge_label_and_data;
613
614   xbt_graph_parse_reset_parser();
615
616   STag_graphxml_graph_fun = __parse_graph_begin;
617   ETag_graphxml_graph_fun = __parse_graph_end;
618   ETag_graphxml_node_fun = __parse_node;
619   ETag_graphxml_edge_fun = __parse_edge;
620
621   xbt_graph_parse_open(filename);
622   xbt_assert1((!xbt_graph_parse()), "Parse error in %s", filename);
623   xbt_graph_parse_close();
624
625   graph = parsed_graph;
626   parsed_graph = NULL;
627
628   return graph;
629 }
630
631 /** @brief Export the given graph in the GraphViz formatting for visualization */
632 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename,
633                                const char *(node_name) (xbt_node_t),
634                                const char *(edge_name) (xbt_edge_t))
635 {
636   int cursor = 0;
637   xbt_node_t node = NULL;
638   xbt_edge_t edge = NULL;
639   FILE *file = NULL;
640   const char *name = NULL;
641
642   file = fopen(filename, "w");
643   xbt_assert1(file, "Failed to open %s \n", filename);
644
645   if (g->directed)
646     fprintf(file, "digraph test {\n");
647   else
648     fprintf(file, "graph test {\n");
649
650   fprintf(file, "  graph [overlap=scale]\n");
651
652   fprintf(file, "  node [shape=box, style=filled]\n");
653   fprintf(file,
654           "  node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
655
656   xbt_dynar_foreach(g->nodes, cursor, node) {
657     fprintf(file, "  \"%p\" ", node);
658     if ((node_name) && ((name = node_name(node))))
659       fprintf(file, "[label=\"%s\"]", name);
660     fprintf(file, ";\n");
661   }
662   xbt_dynar_foreach(g->edges, cursor, edge) {
663     if (g->directed)
664       fprintf(file, "  \"%p\" -> \"%p\"", edge->src, edge->dst);
665     else
666       fprintf(file, "  \"%p\" -- \"%p\"", edge->src, edge->dst);
667     if ((edge_name) && ((name = edge_name(edge))))
668       fprintf(file, "[label=\"%s\"]", name);
669     fprintf(file, ";\n");
670   }
671   fprintf(file, "}\n");
672   fclose(file);
673 }
674
675 /** @brief Export the given graph in the GraphXML format */
676 void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
677                                const char *(node_name) (xbt_node_t),
678                                const char *(edge_name) (xbt_edge_t),
679                                const char *(node_data_print) (void *),
680                                const char *(edge_data_print) (void *))
681 {
682   int cursor = 0;
683   xbt_node_t node = NULL;
684   xbt_edge_t edge = NULL;
685   FILE *file = NULL;
686   const char *name = NULL;
687
688   file = fopen(filename, "w");
689   xbt_assert1(file, "Failed to open %s \n", filename);
690
691   fprintf(file, "<?xml version='1.0'?>\n");
692   fprintf(file, "<!DOCTYPE graph SYSTEM \"graphxml.dtd\">\n");
693   if (g->directed)
694     fprintf(file, "<graph isDirected=\"true\">\n");
695   else
696     fprintf(file, "<graph isDirected=\"false\">\n");
697   xbt_dynar_foreach(g->nodes, cursor, node) {
698     fprintf(file, "  <node name=\"%p\" ", node);
699     if ((node_name) && ((name = node_name(node))))
700       fprintf(file, "label=\"%s\" ", name);
701     if ((node_data_print) && ((name = node_data_print(node->data))))
702       fprintf(file, "data=\"%s\" ", name);
703     fprintf(file, ">\n");
704   }
705   xbt_dynar_foreach(g->edges, cursor, edge) {
706     fprintf(file, "  <edge source=\"%p\" target =\"%p\" ",
707             edge->src, edge->dst);
708     if ((edge_name) && ((name = edge_name(edge))))
709       fprintf(file, "label=\"%s\" ", name);
710     if (edge->length >= 0.0)
711       fprintf(file, "length=\"%g\" ", edge->length);
712     if ((edge_data_print) && ((name = edge_data_print(edge->data))))
713       fprintf(file, "data=\"%s\" ", name);
714     fprintf(file, ">\n");
715   }
716   fprintf(file, "</graph>\n");
717   fclose(file);
718 }