Logo AND Algorithmique Numérique Distribuée

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