Logo AND Algorithmique Numérique Distribuée

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