Logo AND Algorithmique Numérique Distribuée

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