Logo AND Algorithmique Numérique Distribuée

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