Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add FindNS3 module to cmake.
[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 #include "xbt/str.h"
18
19
20
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_graph, xbt, "Graph");
23
24
25
26 /** @brief Constructor
27  *  @return a new graph
28  */
29 xbt_graph_t xbt_graph_new_graph(unsigned short int directed, void *data)
30 {
31   xbt_graph_t graph = NULL;
32   graph = xbt_new0(struct xbt_graph, 1);
33   graph->directed = directed;
34   graph->data = data;
35   graph->nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
36   graph->edges = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
37
38   return graph;
39 }
40
41 /** @brief add a node to the given graph */
42 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
43 {
44   xbt_node_t node = NULL;
45   node = xbt_new0(struct xbt_node, 1);
46   node->data = data;
47   if (g->directed)
48     /* only the "out" field is used */
49     node->in = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
50
51   node->out = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
52   node->position_x = -1.0;
53   node->position_y = -1.0;
54
55   xbt_dynar_push(g->nodes, &node);
56
57   return node;
58 }
59
60 /** @brief add an edge to the given graph */
61 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g,
62                               xbt_node_t src, xbt_node_t dst, void *data)
63 {
64   xbt_edge_t edge = NULL;
65
66   edge = xbt_new0(struct xbt_edge, 1);
67   xbt_dynar_push(src->out, &edge);
68   if (g->directed)
69     xbt_dynar_push(dst->in, &edge);
70   else                          /* only the "out" field is used */
71     xbt_dynar_push(dst->out, &edge);
72
73   edge->data = data;
74   edge->src = src;
75   edge->dst = dst;
76
77   xbt_dynar_push(g->edges, &edge);
78
79   return edge;
80 }
81
82 xbt_edge_t xbt_graph_get_edge(xbt_graph_t g, xbt_node_t src,
83                               xbt_node_t dst)
84 {
85   xbt_edge_t edge;
86   unsigned int cursor;
87
88   xbt_dynar_foreach(src->out, cursor, edge) {
89     XBT_DEBUG("%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       XBT_DEBUG("%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   xbt_graph_parse_lex_destroy();
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   return e->src;
266 }
267
268 /** @brief Retrieve the node being the target of the given edge */
269 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
270 {
271   return e->dst;
272 }
273
274 /** @brief Retrieve the outgoing edges of the given node */
275 xbt_dynar_t xbt_graph_node_get_outedges(xbt_node_t n)
276 {
277   return n->out;
278 }
279
280 /** @brief Set the weight of the given edge */
281 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
282 {
283   e->length = length;
284
285 }
286
287 double xbt_graph_edge_get_length(xbt_edge_t e)
288 {
289   return e->length;
290 }
291
292
293 /** @brief construct the adjacency matrix corresponding to the given graph
294  *
295  * The weights are the distances between nodes
296  */
297 double *xbt_graph_get_length_matrix(xbt_graph_t g)
298 {
299   unsigned int cursor = 0;
300   unsigned int in_cursor = 0;
301   unsigned long idx, i;
302   unsigned long n;
303   xbt_edge_t edge = NULL;
304   xbt_node_t node = NULL;
305   double *d = NULL;
306
307 # define D(u,v) d[(u)*n+(v)]
308   n = xbt_dynar_length(g->nodes);
309
310   d = (double *) xbt_new0(double, n * n);
311
312   for (i = 0; i < n * n; i++) {
313     d[i] = -1.0;
314   }
315
316   xbt_dynar_foreach(g->nodes, cursor, node) {
317     in_cursor = 0;
318     D(cursor, cursor) = 0;
319
320     xbt_dynar_foreach(node->out, in_cursor, edge) {
321       if (edge->dst == node)
322         idx = __xbt_find_in_dynar(g->nodes, edge->src);
323       else                      /*case of  undirected graphs */
324         idx = __xbt_find_in_dynar(g->nodes, edge->dst);
325       D(cursor, idx) = edge->length;
326     }
327   }
328
329 # undef D
330
331   return d;
332 }
333
334 /** @brief Floyd-Warshall algorithm for shortest path finding
335  *
336  * From wikipedia:
337  *
338  * The Floyd–Warshall algorithm takes as input an adjacency matrix
339  * representation of a weighted, directed graph (V, E). The weight of a
340  * path between two vertices is the sum of the weights of the edges along
341  * that path. The edges E of the graph may have negative weights, but the
342  * graph must not have any negative weight cycles. The algorithm computes,
343  * for each pair of vertices, the minimum weight among all paths between
344  * the two vertices. The running time complexity is Θ(|V|3).
345  */
346 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
347                          xbt_node_t * p)
348 {
349   unsigned long i, j, k;
350   unsigned long n;
351   n = xbt_dynar_length(g->nodes);
352
353 # define D(u,v) d[(u)*n+(v)]
354 # define P(u,v) p[(u)*n+(v)]
355
356   for (i = 0; i < n * n; i++) {
357     d[i] = adj[i];
358   }
359
360
361   for (i = 0; i < n; i++) {
362     for (j = 0; j < n; j++) {
363       if (D(i, j) != -1) {
364         P(i, j) = *((xbt_node_t *) xbt_dynar_get_ptr(g->nodes, i));
365       }
366     }
367   }
368
369   for (k = 0; k < n; k++) {
370     for (i = 0; i < n; i++) {
371       for (j = 0; j < n; j++) {
372         if ((D(i, k) != -1) && (D(k, j) != -1)) {
373           if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j))) {
374             D(i, j) = D(i, k) + D(k, j);
375             P(i, j) = P(k, j);
376           }
377         }
378       }
379     }
380   }
381
382
383
384 # undef P
385 # undef D
386 }
387
388 /** @brief computes all-pairs shortest paths */
389 xbt_node_t *xbt_graph_shortest_paths(xbt_graph_t g)
390 {
391   xbt_node_t *p;
392   xbt_node_t *r;
393   unsigned long i, j, k;
394   unsigned long n;
395
396   double *adj = NULL;
397   double *d = NULL;
398
399 # define P(u,v) p[(u)*n+(v)]
400 # define R(u,v) r[(u)*n+(v)]
401
402   n = xbt_dynar_length(g->nodes);
403   adj = xbt_graph_get_length_matrix(g);
404   d = xbt_new0(double, n * n);
405   p = xbt_new0(xbt_node_t, n * n);
406   r = xbt_new0(xbt_node_t, n * n);
407
408   xbt_floyd_algorithm(g, adj, d, p);
409
410   for (i = 0; i < n; i++) {
411     for (j = 0; j < n; j++) {
412       k = j;
413
414       while ((P(i, k)) && (__xbt_find_in_dynar(g->nodes, P(i, k)) != i)) {
415         k = __xbt_find_in_dynar(g->nodes, P(i, k));
416       }
417
418       if (P(i, j)) {
419         R(i, j) = *((xbt_node_t *) xbt_dynar_get_ptr(g->nodes, k));
420       }
421     }
422   }
423 # undef R
424 # undef P
425
426   free(d);
427   free(p);
428   free(adj);
429   return r;
430 }
431
432 /** @brief Extract a spanning tree of the given graph */
433 xbt_edge_t *xbt_graph_spanning_tree_prim(xbt_graph_t g)
434 {
435   int tree_size = 0;
436   int tree_size_max = xbt_dynar_length(g->nodes) - 1;
437   xbt_edge_t *tree = xbt_new0(xbt_edge_t, tree_size_max);
438   xbt_edge_t e, edge;
439   xbt_node_t node = NULL;
440   xbt_dynar_t edge_list = NULL;
441   xbt_heap_t heap = xbt_heap_new(10, NULL);
442   unsigned int cursor;
443
444   xbt_assert(!(g->directed),
445               "Spanning trees do not make sense on directed graphs");
446
447   xbt_dynar_foreach(g->nodes, cursor, node) {
448     node->xbtdata = NULL;
449   }
450
451   node = xbt_dynar_getfirst_as(g->nodes, xbt_node_t);
452   node->xbtdata = (void *) 1;
453   edge_list = node->out;
454   xbt_dynar_foreach(edge_list, cursor, e)
455       xbt_heap_push(heap, e, -(e->length));
456
457   while ((edge = xbt_heap_pop(heap))) {
458     if ((edge->src->xbtdata) && (edge->dst->xbtdata))
459       continue;
460     tree[tree_size++] = edge;
461     if (!(edge->src->xbtdata)) {
462       edge->src->xbtdata = (void *) 1;
463       edge_list = edge->src->out;
464       xbt_dynar_foreach(edge_list, cursor, e) {
465         xbt_heap_push(heap, e, -(e->length));
466       }
467     } else {
468       edge->dst->xbtdata = (void *) 1;
469       edge_list = edge->dst->out;
470       xbt_dynar_foreach(edge_list, cursor, e) {
471         xbt_heap_push(heap, e, -(e->length));
472       }
473     }
474     if (tree_size == tree_size_max)
475       break;
476   }
477
478   xbt_heap_free(heap);
479
480   return tree;
481 }
482
483 /** @brief Topological sort on the given graph
484  *
485  *  From wikipedia:
486  *
487  * In graph theory, a topological sort of a directed acyclic graph (DAG) is
488  * a linear ordering of its nodes which is compatible with the partial
489  * order R induced on the nodes where x comes before y (xRy) if there's a
490  * directed path from x to y in the DAG. An equivalent definition is that
491  * each node comes before all nodes to which it has edges. Every DAG has at
492  * least one topological sort, and may have many.
493  */
494 xbt_node_t *xbt_graph_topo_sort(xbt_graph_t g)
495 {
496
497   xbt_node_t *sorted;
498   unsigned int cursor;
499   int idx;
500   xbt_node_t node;
501   unsigned long n;
502
503   n = xbt_dynar_length(g->nodes);
504   idx = n - 1;
505
506   sorted = xbt_malloc(n * sizeof(xbt_node_t));
507
508   xbt_dynar_foreach(g->nodes, cursor, node)
509       node->xbtdata = xbt_new0(int, 1);
510
511   xbt_dynar_foreach(g->nodes, cursor, node)
512       xbt_graph_depth_visit(g, node, sorted, &idx);
513
514   xbt_dynar_foreach(g->nodes, cursor, node) {
515     free(node->xbtdata);
516     node->xbtdata = NULL;
517   }
518
519   return sorted;
520 }
521
522 /** @brief First-depth graph traversal */
523 void xbt_graph_depth_visit(xbt_graph_t g, xbt_node_t n,
524                            xbt_node_t * sorted, int *idx)
525 {
526   unsigned int cursor;
527   xbt_edge_t edge;
528
529   if (*((int *) (n->xbtdata)) == ALREADY_EXPLORED)
530     return;
531   else if (*((int *) (n->xbtdata)) == CURRENTLY_EXPLORING)
532     THROWF(0, 0, "There is a cycle");
533   else {
534     *((int *) (n->xbtdata)) = CURRENTLY_EXPLORING;
535
536     xbt_dynar_foreach(n->out, cursor, edge) {
537       xbt_graph_depth_visit(g, edge->dst, sorted, idx);
538     }
539
540     *((int *) (n->xbtdata)) = ALREADY_EXPLORED;
541     sorted[(*idx)--] = n;
542   }
543 }
544
545 /********************* Import and Export ******************/
546 static xbt_graph_t parsed_graph = NULL;
547 static xbt_dict_t parsed_nodes = NULL;
548
549 static void *(*__parse_node_label_and_data) (xbt_node_t, const char *,
550                                              const char *) = NULL;
551 static void *(*__parse_edge_label_and_data) (xbt_edge_t, const char *,
552                                              const char *) = NULL;
553
554 static void __parse_graph_begin(void)
555 {
556   XBT_DEBUG("<graph>");
557   if (A_graphxml_graph_isDirected == A_graphxml_graph_isDirected_true)
558     parsed_graph = xbt_graph_new_graph(1, NULL);
559   else
560     parsed_graph = xbt_graph_new_graph(0, NULL);
561
562   parsed_nodes = xbt_dict_new();
563 }
564
565 static void __parse_graph_end(void)
566 {
567   xbt_dict_free(&parsed_nodes);
568   XBT_DEBUG("</graph>");
569 }
570
571 static void __parse_node(void)
572 {
573   xbt_node_t node = xbt_graph_new_node(parsed_graph, NULL);
574
575   XBT_DEBUG("<node name=\"%s\"/>", A_graphxml_node_name);
576   if (__parse_node_label_and_data)
577     node->data = __parse_node_label_and_data(node, A_graphxml_node_label,
578                                              A_graphxml_node_data);
579   xbt_graph_parse_get_double(&(node->position_x),
580                              A_graphxml_node_position_x);
581   xbt_graph_parse_get_double(&(node->position_y),
582                              A_graphxml_node_position_y);
583
584   xbt_dict_set(parsed_nodes, A_graphxml_node_name, (void *) node, NULL);
585 }
586
587 static void __parse_edge(void)
588 {
589   xbt_edge_t edge = xbt_graph_new_edge(parsed_graph,
590                                        xbt_dict_get(parsed_nodes,
591                                                     A_graphxml_edge_source),
592                                        xbt_dict_get(parsed_nodes,
593                                                     A_graphxml_edge_target),
594                                        NULL);
595
596   if (__parse_edge_label_and_data)
597     edge->data = __parse_edge_label_and_data(edge, A_graphxml_edge_label,
598                                              A_graphxml_edge_data);
599
600   xbt_graph_parse_get_double(&(edge->length), A_graphxml_edge_length);
601
602   XBT_DEBUG("<edge  source=\"%s\" target=\"%s\" length=\"%f\"/>",
603          (char *) (edge->src)->data,
604          (char *) (edge->dst)->data, xbt_graph_edge_get_length(edge));
605 }
606
607 /** @brief Import a graph from a file following the GraphXML format */
608 xbt_graph_t xbt_graph_read(const char *filename,
609                            void *(*node_label_and_data) (xbt_node_t,
610                                                          const char *,
611                                                          const char *),
612                            void *(*edge_label_and_data) (xbt_edge_t,
613                                                          const char *,
614                                                          const char *))
615 {
616
617   xbt_graph_t graph = NULL;
618
619   __parse_node_label_and_data = node_label_and_data;
620   __parse_edge_label_and_data = edge_label_and_data;
621
622   xbt_graph_parse_reset_parser();
623
624   STag_graphxml_graph_fun = __parse_graph_begin;
625   ETag_graphxml_graph_fun = __parse_graph_end;
626   ETag_graphxml_node_fun = __parse_node;
627   ETag_graphxml_edge_fun = __parse_edge;
628
629   xbt_graph_parse_open(filename);
630   int res;
631   res = (*xbt_graph_parse) ();
632   xbt_assert(!res, "Parse error in %s", filename);
633   xbt_graph_parse_close();
634
635   graph = parsed_graph;
636   parsed_graph = NULL;
637
638   return graph;
639 }
640
641 /** @brief Export the given graph in the GraphViz formatting for visualization */
642 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename,
643                                const char *(node_name) (xbt_node_t),
644                                const char *(edge_name) (xbt_edge_t))
645 {
646   unsigned int cursor = 0;
647   xbt_node_t node = NULL;
648   xbt_edge_t edge = NULL;
649   FILE *file = NULL;
650   const char *name = NULL;
651
652   file = fopen(filename, "w");
653   xbt_assert(file, "Failed to open %s \n", filename);
654
655   if (g->directed)
656     fprintf(file, "digraph test {\n");
657   else
658     fprintf(file, "graph test {\n");
659
660   fprintf(file, "  graph [overlap=scale]\n");
661
662   fprintf(file, "  node [shape=box, style=filled]\n");
663   fprintf(file,
664           "  node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
665
666   xbt_dynar_foreach(g->nodes, cursor, node) {
667     fprintf(file, "  \"%p\" ", node);
668     if ((node_name) && ((name = node_name(node))))
669       fprintf(file, "[label=\"%s\"]", name);
670     fprintf(file, ";\n");
671   }
672   xbt_dynar_foreach(g->edges, cursor, edge) {
673     if (g->directed)
674       fprintf(file, "  \"%p\" -> \"%p\"", edge->src, edge->dst);
675     else
676       fprintf(file, "  \"%p\" -- \"%p\"", edge->src, edge->dst);
677     if ((edge_name) && ((name = edge_name(edge))))
678       fprintf(file, "[label=\"%s\"]", name);
679     fprintf(file, ";\n");
680   }
681   fprintf(file, "}\n");
682   fclose(file);
683 }
684
685 /** @brief Export the given graph in the GraphXML format */
686 void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
687                                const char *(node_name) (xbt_node_t),
688                                const char *(edge_name) (xbt_edge_t),
689                                const char *(node_data_print) (void *),
690                                const char *(edge_data_print) (void *))
691 {
692   unsigned int cursor = 0;
693   xbt_node_t node = NULL;
694   xbt_edge_t edge = NULL;
695   FILE *file = NULL;
696   const char *name = NULL;
697
698   file = fopen(filename, "w");
699   xbt_assert(file, "Failed to open %s \n", filename);
700
701   fprintf(file, "<?xml version='1.0'?>\n");
702   fprintf(file, "<!DOCTYPE graph SYSTEM \"graphxml.dtd\">\n");
703   if (g->directed)
704     fprintf(file, "<graph isDirected=\"true\">\n");
705   else
706     fprintf(file, "<graph isDirected=\"false\">\n");
707   xbt_dynar_foreach(g->nodes, cursor, node) {
708     fprintf(file, "  <node name=\"%p\" ", node);
709     if ((node_name) && ((name = node_name(node))))
710       fprintf(file, "label=\"%s\" ", name);
711     if ((node_data_print) && ((name = node_data_print(node->data))))
712       fprintf(file, "data=\"%s\" ", name);
713     fprintf(file, ">\n");
714   }
715   xbt_dynar_foreach(g->edges, cursor, edge) {
716     fprintf(file, "  <edge source=\"%p\" target =\"%p\" ",
717             edge->src, edge->dst);
718     if ((edge_name) && ((name = edge_name(edge))))
719       fprintf(file, "label=\"%s\" ", name);
720     if (edge->length >= 0.0)
721       fprintf(file, "length=\"%g\" ", edge->length);
722     if ((edge_data_print) && ((name = edge_data_print(edge->data))))
723       fprintf(file, "data=\"%s\" ", name);
724     fprintf(file, ">\n");
725   }
726   fprintf(file, "</graph>\n");
727   fclose(file);
728 }
729
730 /** @brief Load a graph from a file (in the SimGrid Graph format) */
731 xbt_graph_t xbt_graph_load (const char *filename)
732 {
733   FILE *file = NULL;
734   file = fopen (filename, "r");
735   xbt_assert(file, "Failed to open %s \n", filename);
736
737   xbt_dict_t nodes_dict = xbt_dict_new ();
738   xbt_graph_t ret = xbt_graph_new_graph (0, NULL);
739
740   //read the number of nodes
741   size_t size;
742   char *nnodes_str = NULL;
743   getline (&nnodes_str, &size, file);
744   int i, nnodes = atoi (nnodes_str);
745   free (nnodes_str);
746
747   //read all nodes
748   for (i = 0; i < nnodes; i++){
749     char *node_str = NULL;
750     getline (&node_str, &size, file);
751     xbt_node_t n;
752     char *name = xbt_strdup (node_str);
753     xbt_str_subst (name, '\n', '\0', 0);
754     n = xbt_graph_new_node (ret, name);
755     xbt_dict_set (nodes_dict, name, n, NULL);
756     free (node_str);
757   }
758
759   //read the number of edges
760   char *nedges_str = NULL;
761   getline (&nedges_str, &size, file);
762   int nedges = atoi (nedges_str);
763   free (nedges_str);
764
765   //read all edges
766   for (i = 0; i < nedges; i++){
767     char *edge_str = NULL, edge_id[200], node_source[200], node_target[200];
768     getline (&edge_str, &size, file);
769     sscanf (edge_str, "%s %s %s", edge_id, node_source, node_target);
770     free (edge_str);
771     xbt_str_subst (edge_id, '\n', '\0', 0);
772     xbt_str_subst (node_source, '\n', '\0', 0);
773     xbt_str_subst (node_target, '\n', '\0', 0);
774
775     xbt_node_t source = xbt_dict_get (nodes_dict, node_source);
776     xbt_node_t target = xbt_dict_get (nodes_dict, node_target);
777     xbt_edge_t e;
778     e = xbt_graph_new_edge (ret, source, target, xbt_strdup(edge_id));
779   }
780   xbt_dict_free (&nodes_dict);
781   return ret;
782 }
783
784 /** @brief Save a graph from a file (in the SimGrid Graph format) */
785 void xbt_graph_save (xbt_graph_t span,
786                      const char *filename,
787                      const char *(nname) (xbt_node_t),
788                      const char *(ename) (xbt_edge_t))
789 {
790   FILE *file = NULL;
791   file = fopen(filename, "w");
792   xbt_assert(file, "Failed to open %s \n", filename);
793
794   xbt_dynar_t nodes = xbt_graph_get_nodes (span);
795   xbt_dynar_t edges = xbt_graph_get_edges (span);
796   unsigned int cpt;
797   xbt_node_t node;
798   fprintf (file, "%ld\n", xbt_dynar_length (nodes));
799   xbt_dynar_foreach (nodes, cpt, node) {
800     if (nname){
801       fprintf (file, "%s\n", nname(node));
802     }else{
803       fprintf (file, "%p\n", node);
804     }
805   }
806   fprintf (file, "%ld\n", xbt_dynar_length (edges));
807   xbt_edge_t edge;
808   xbt_dynar_foreach (edges, cpt, edge) {
809     xbt_node_t source = xbt_graph_edge_get_source (edge);
810     xbt_node_t target = xbt_graph_edge_get_target (edge);
811     if (ename){
812       if (nname){
813         fprintf (file, "%s %s %s\n", ename(edge), nname(source), nname(target));
814       }else{
815         fprintf (file, "%s %p %p\n", ename(edge), source, target);
816       }
817     }else{
818       if (nname){
819         fprintf (file, "%p %s %s\n", edge, nname(source), nname(target));
820       }else{
821         fprintf (file, "%p %p %p\n", edge, source, target);
822       }
823     }
824   }
825   fclose (file);
826 }