Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Document Arnaud's last changes
[simgrid.git] / src / xbt / graph.c
1 /*      $Id$     */
2
3 /* a generic graph library.                                                 */
4
5 /* Copyright (c) 2006 Darina Dimitrova, Arnaud Legrand.                     */
6 /* All rights reserved.                                                     */
7
8 /* This program is free software; you can redistribute it and/or modify it
9  * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <stdlib.h>
12 #include "xbt/sysdep.h"
13 #include "xbt/log.h"
14 #include "xbt/graph.h"
15 #include "graph_private.h"
16 #include "xbt/graphxml_parse.h"
17 #include "xbt/dict.h"
18 #include "xbt/heap.h"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(graph, xbt, "Graph");
21
22
23
24 /** Constructor
25  * \return a new graph
26  */
27 xbt_graph_t xbt_graph_new_graph(unsigned short int directed, void *data)
28 {
29   xbt_graph_t graph = NULL;
30   graph = xbt_new0(struct xbt_graph, 1);
31   graph->directed = directed;
32   graph->data = data;
33   graph->nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
34   graph->edges = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
35
36   return graph;
37 }
38
39 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
40 {
41   xbt_node_t node = NULL;
42   node = xbt_new0(struct xbt_node, 1);
43   node->data = data;
44   node->in = xbt_dynar_new(sizeof(xbt_node_t), NULL);
45   node->out = xbt_dynar_new(sizeof(xbt_node_t), NULL);
46   node->position_x = -1.0;
47   node->position_y = -1.0;
48   xbt_dynar_push(g->nodes, &node);
49
50   return node;
51 }
52
53
54 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g,
55                               xbt_node_t src, xbt_node_t dst, void *data)
56 {
57   xbt_edge_t edge = NULL;
58  
59
60   edge = xbt_new0(struct xbt_edge, 1);
61   xbt_dynar_push(src->out, &edge);
62   if (g->directed) 
63     xbt_dynar_push(dst->in, &edge);
64   else /* only the "out" field is used */
65     xbt_dynar_push(dst->out, &edge);
66
67   edge->data = data;
68   edge->src = src;
69   edge->dst = dst;
70
71   xbt_dynar_push(g->edges, &edge);
72
73   return edge;
74 }
75
76
77 /** Destructor
78  * \param l poor victim
79  *
80  * Free the graph structure. 
81  */
82 void xbt_graph_free_graph(xbt_graph_t g,
83                           void node_free_function(void *ptr),
84                           void edge_free_function(void *ptr),
85                           void graph_free_function(void *ptr))
86 {
87   int cursor = 0;
88   xbt_node_t node = NULL;
89   xbt_edge_t edge = NULL;
90
91
92   xbt_dynar_foreach(g->nodes, cursor, node)
93     {
94       xbt_dynar_free(&(node->out));
95       xbt_dynar_free(&(node->in));
96       if(node_free_function)
97         node_free_function(node->data);
98     }
99
100   xbt_dynar_foreach(g->edges, cursor, edge) 
101     {
102       if(edge_free_function)
103         edge_free_function(edge->data);
104     }
105
106   xbt_dynar_foreach(g->nodes, cursor, node)
107     free(node);
108   xbt_dynar_free(&(g->nodes));
109
110   xbt_dynar_foreach(g->edges, cursor, edge)
111     free(edge);
112   xbt_dynar_free(&(g->edges));
113
114   free(g);
115
116   return;
117 }
118
119
120
121 void xbt_graph_free_node(xbt_graph_t g, xbt_node_t n,
122                          void_f_pvoid_t * node_free_function,
123                          void_f_pvoid_t * edge_free_function)
124 {
125   unsigned long nbr;
126   int i;
127   int cursor = 0;
128   xbt_node_t node = NULL;
129   xbt_edge_t edge = NULL;
130
131   nbr = xbt_dynar_length(g->edges);
132   cursor=0;
133   for (i = 0; i < nbr; i++)
134     {
135       xbt_dynar_cursor_get(g->edges, &cursor, &edge);
136         
137       if ((edge->dst == n) || (edge->src == n))
138         {
139           xbt_graph_free_edge(g, edge, edge_free_function);
140         } 
141       else xbt_dynar_cursor_step( g->edges, &cursor);   
142     }
143
144
145  if ((node_free_function) && (n->data))
146     node_free_function(n->data);
147
148   cursor = 0;
149   xbt_dynar_foreach(g->nodes, cursor, node)
150     {
151       if (node == n)
152         xbt_dynar_cursor_rm(g->nodes, &cursor);
153
154     }
155
156   return;
157 }
158
159 void xbt_graph_free_edge(xbt_graph_t g, xbt_edge_t e,
160                          void free_function(void *ptr))
161 {
162   int idx;
163   int cursor = 0;
164   xbt_edge_t edge = NULL; 
165
166   if ((free_function) && (e->data))
167     free_function(e->data);
168
169   xbt_dynar_foreach(g->edges, cursor, edge) 
170     {
171     if (edge == e)
172       {
173         if (g->directed) {
174           idx = __xbt_find_in_dynar(edge->dst->in,edge); 
175           xbt_dynar_remove_at(edge->dst->in, idx,NULL);
176         } else { /* only the out field is used */
177           idx = __xbt_find_in_dynar(edge->dst->out,edge); 
178           xbt_dynar_remove_at(edge->dst->out, idx,NULL);
179         }
180
181         idx = __xbt_find_in_dynar(edge->src->out,edge);
182         xbt_dynar_remove_at(edge->src->out,idx,NULL);   
183
184         xbt_dynar_cursor_rm(g->edges, &cursor); 
185         free(edge);
186         break;
187       }
188     }
189 }
190
191 int __xbt_find_in_dynar(xbt_dynar_t dynar, void *p)
192 {
193
194   int cursor = 0;
195   void *tmp=NULL;
196
197   xbt_dynar_foreach(dynar, cursor, tmp)
198     {
199       if (tmp == p)
200         return cursor;
201     }
202   return -1;
203 }
204
205 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
206 {
207   return g->nodes;
208 }
209
210 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
211 {
212   return g->edges;
213 }
214
215 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
216 {
217
218   return e->src;
219 }
220
221 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
222 {
223   return e->dst;
224 }
225
226
227 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
228 {
229   e->length = length;
230
231 }
232
233 double xbt_graph_edge_get_length(xbt_edge_t e)
234 {
235   return e->length;
236 }
237
238
239 /*construct the adjacency matrix corresponding to a graph,
240   the weights are the distances between nodes
241  */
242 double *xbt_graph_get_length_matrix(xbt_graph_t g)
243 {
244   int cursor = 0;
245   int in_cursor = 0;
246   int  idx,i;
247   unsigned long n;
248   xbt_edge_t edge = NULL;
249   xbt_node_t node=NULL;
250   double *d = NULL;
251
252 # define D(u,v) d[(u)*n+(v)]
253   n = xbt_dynar_length(g->nodes);
254
255   d = (double *) xbt_new0(double, n*n);
256
257   for (i = 0; i < n * n; i++)
258     {
259       d[i] = -1.0;
260     }
261  
262   xbt_dynar_foreach(g->nodes, cursor, node) 
263     {
264       in_cursor = 0;
265       D(cursor, cursor) = 0;
266     
267       xbt_dynar_foreach(node->out, in_cursor, edge)
268         {
269           if (edge->dst==node) 
270             idx= __xbt_find_in_dynar(g->nodes, edge->src);
271           else /*case of  undirected graphs*/ 
272              idx = __xbt_find_in_dynar(g->nodes, edge->dst);
273           D( cursor,idx) = edge->length;
274         }
275     }
276
277 # undef D
278
279   return d;
280 }
281
282
283 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
284                          xbt_node_t * p)
285 {
286   int i, j, k;
287   unsigned long n;
288   n = xbt_dynar_length(g->nodes);
289
290 # define D(u,v) d[(u)*n+(v)]
291 # define P(u,v) p[(u)*n+(v)]
292
293   for (i = 0; i < n * n; i++)
294     {
295       d[i] = adj[i];
296     }
297
298
299   for (i = 0; i < n; i++)
300     {
301       for (j = 0; j < n; j++)
302         {
303           if (D(i, j) != -1)
304             {
305               P(i,j) =*((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, i));       
306             }
307         }
308     }
309  
310   for (k = 0; k < n; k++)
311     {
312       for (i = 0; i < n; i++)
313         {
314           for (j = 0; j < n; j++)
315             {
316               if ((D(i, k) != -1) && (D(k, j) != -1))
317                 {
318                   if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j)))
319                     {
320                       D(i, j) = D(i, k) + D(k, j);
321                       P(i, j) = P(k, j);
322                     }
323                 }
324             }
325         }
326     }
327
328
329
330 # undef P
331 # undef D
332 }
333
334 /*computes all-pairs shortest paths*/
335 xbt_node_t *xbt_graph_shortest_paths(xbt_graph_t g)
336 {
337   xbt_node_t *p;
338   xbt_node_t *r;
339   int i, j, k;
340   unsigned long n;
341
342   double *adj = NULL;
343   double *d = NULL;
344
345 # define P(u,v) p[(u)*n+(v)]
346 # define R(u,v) r[(u)*n+(v)]
347
348   n = xbt_dynar_length(g->nodes);
349   adj = xbt_graph_get_length_matrix(g);
350   d = xbt_new0(double,n*n);
351   p = xbt_new0(xbt_node_t,n*n);
352   r = xbt_new0(xbt_node_t,n*n);
353  
354   xbt_floyd_algorithm(g, adj, d, p);
355  
356   for (i = 0; i < n; i++)
357     {
358       for (j = 0; j < n; j++)
359         {
360           k = j;
361
362           while ((P(i, k)) && (__xbt_find_in_dynar(g->nodes, P(i, k)) != i))
363             {
364               k = __xbt_find_in_dynar(g->nodes, P(i, k));
365             }
366
367           if (P(i, j))
368             {
369               R(i, j) = *((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, k));
370             }
371         }
372     }
373 # undef R
374 # undef P
375
376   free(d);
377   free(p);
378   free(adj);
379   return r;
380 }
381
382
383 xbt_edge_t* xbt_graph_spanning_tree_prim(xbt_graph_t g)
384 {
385   int tree_size=0;
386   int tree_size_max=xbt_dynar_length(g->nodes)-1;
387   xbt_edge_t *tree = xbt_new0(xbt_edge_t,tree_size_max);
388   xbt_edge_t e,edge;
389   xbt_node_t node = NULL;
390   xbt_dynar_t edge_list = NULL;
391   xbt_heap_t heap = xbt_heap_new(10,NULL);
392   int cursor;
393
394   xbt_assert0(!(g->directed),
395               "Spanning trees do not make sense on directed graphs");
396
397   node = xbt_dynar_getfirst_as(g->nodes,xbt_node_t);
398   node->xbtdata = (void*) 1;
399   edge_list = node->out;
400   xbt_dynar_foreach(edge_list, cursor, e)
401     xbt_heap_push(heap,e, -(e->length));
402   
403   while((edge=xbt_heap_pop(heap))) {
404     if((edge->src->xbtdata) && (edge->dst->xbtdata)) continue;
405     tree[tree_size++]=edge;
406     if(!(edge->src->xbtdata)) {
407       edge->src->xbtdata = (void*) 1;
408       edge_list = edge->src->out;
409       xbt_dynar_foreach(edge_list, cursor, e) {
410         xbt_heap_push(heap,e, -(e->length));
411       }
412     } else {
413       edge->dst->xbtdata = (void*) 1;
414       edge_list = edge->dst->out;
415       xbt_dynar_foreach(edge_list, cursor, e) {
416         xbt_heap_push(heap,e, -(e->length));
417       }
418     }
419     if(tree_size==tree_size_max) break;
420   }
421   
422   xbt_heap_free(heap);
423
424   xbt_dynar_foreach(g->nodes, cursor, node) {
425     node->xbtdata = NULL;
426   }
427   return tree;
428 }
429
430 /********************* Import and Export ******************/
431 static xbt_graph_t parsed_graph = NULL;
432 static xbt_dict_t parsed_nodes = NULL;
433
434 static void *(*__parse_node_label_and_data)(xbt_node_t, const char*, const char*) = NULL;
435 static void *(*__parse_edge_label_and_data)(xbt_edge_t, const char*, const char*) = NULL;
436
437 static void __parse_graph_begin(void)
438 {
439
440   DEBUG0("<graph>");
441   if(A_graphxml_graph_isDirected == A_graphxml_graph_isDirected_true)
442     parsed_graph = xbt_graph_new_graph(1, NULL);
443   else parsed_graph = xbt_graph_new_graph(0, NULL);
444
445   parsed_nodes = xbt_dict_new();
446 }
447 static void __parse_graph_end(void)
448 {
449   xbt_dict_free(&parsed_nodes);
450   DEBUG0("</graph>");
451 }
452
453 static void __parse_node(void)
454 {
455   xbt_node_t node =
456       xbt_graph_new_node(parsed_graph, NULL);
457
458   DEBUG1("<node name=\"%s\"/>", A_graphxml_node_name);
459   if(__parse_node_label_and_data)
460     node->data = __parse_node_label_and_data(node,A_graphxml_node_label,
461                                              A_graphxml_node_data);
462   xbt_graph_parse_get_double(&(node->position_x),A_graphxml_node_position_x);
463   xbt_graph_parse_get_double(&(node->position_y),A_graphxml_node_position_y);
464
465   xbt_dict_set(parsed_nodes, A_graphxml_node_name, (void *) node, NULL);
466 }
467
468 static void __parse_edge(void)
469 {
470   xbt_edge_t edge =
471     xbt_graph_new_edge(parsed_graph,
472                        xbt_dict_get(parsed_nodes,A_graphxml_edge_source),
473                        xbt_dict_get(parsed_nodes,A_graphxml_edge_target),
474                        NULL);
475
476   if(__parse_edge_label_and_data)
477     edge->data = __parse_edge_label_and_data(edge,A_graphxml_edge_label,
478                                              A_graphxml_edge_data);
479
480   xbt_graph_parse_get_double(&(edge->length),A_graphxml_edge_length);
481
482   DEBUG4("<edge name=\"%s\"  source=\"%s\" target=\"%s\" length=\"%f\"/>",
483          (char *) edge->data,
484          (char *) (edge->src)->data,
485          (char *) (edge->dst)->data,
486          xbt_graph_edge_get_length(edge));
487 }
488
489 xbt_graph_t xbt_graph_read(const char *filename,
490                            void *(node_label_and_data)(xbt_node_t, const char*, const char*),
491                            void *(edge_label_and_data)(xbt_edge_t, const char*, const char*))
492 {
493
494   xbt_graph_t graph = NULL;
495
496   __parse_node_label_and_data = node_label_and_data;
497   __parse_edge_label_and_data = edge_label_and_data;
498
499   xbt_graph_parse_reset_parser();
500
501   STag_graphxml_graph_fun = __parse_graph_begin;
502   ETag_graphxml_graph_fun = __parse_graph_end;
503   ETag_graphxml_node_fun = __parse_node;
504   ETag_graphxml_edge_fun = __parse_edge;
505
506   xbt_graph_parse_open(filename);
507   xbt_assert1((!xbt_graph_parse()), "Parse error in %s", filename);
508   xbt_graph_parse_close();
509
510   graph = parsed_graph;
511   parsed_graph = NULL;
512
513   return graph;
514 }
515
516 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename,
517                                const char *(node_name) (xbt_node_t),
518                                const char *(edge_name) (xbt_edge_t))
519 {
520   int cursor = 0;
521   xbt_node_t node = NULL;
522   xbt_edge_t edge = NULL;
523   FILE *file = NULL;
524   const char *name=NULL;
525
526   file=fopen(filename,"w");
527   xbt_assert1(file, "Failed to open %s \n",filename);
528
529   if(g->directed) fprintf(file,"digraph test {\n");
530   else fprintf(file,"graph test {\n");
531
532   fprintf(file,"  graph [overlap=scale]\n");
533
534   fprintf(file,"  node [shape=box, style=filled]\n");
535   fprintf(file,"  node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
536   
537   xbt_dynar_foreach(g->nodes, cursor, node) {
538     fprintf(file,"  \"%p\" ", node);
539     if((node_name)&&((name=node_name(node)))) fprintf(file,"[label=\"%s\"]",name);
540     fprintf(file,";\n");
541   }
542   xbt_dynar_foreach(g->edges, cursor, edge) {
543     if(g->directed)
544       fprintf(file,"  \"%p\" -> \"%p\"",edge->src, edge->dst);
545     else
546       fprintf(file,"  \"%p\" -- \"%p\"",edge->src, edge->dst);
547     if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"[label=\"%s\"]",name);
548     fprintf(file,";\n");
549   }
550   fprintf(file,"}\n");
551   fclose(file);
552 }
553
554 void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
555                                const char *(node_name)(xbt_node_t),
556                                const char *(edge_name)(xbt_edge_t),
557                                const char *(node_data_print)(void *),
558                                const char *(edge_data_print)(void *))
559 {
560   int cursor = 0;
561   xbt_node_t node = NULL;
562   xbt_edge_t edge = NULL;
563   FILE *file = NULL;
564   const char *name = NULL;
565
566   file=fopen(filename,"w");
567   xbt_assert1(file, "Failed to open %s \n",filename);
568
569   fprintf(file,"<?xml version='1.0'?>\n");
570   fprintf(file,"<!DOCTYPE graph SYSTEM \"graphxml.dtd\">\n");
571   if(g->directed) fprintf(file,"<graph isDirected=\"true\">\n");
572   else fprintf(file,"<graph isDirected=\"false\">\n");
573   xbt_dynar_foreach(g->nodes, cursor, node) {
574     fprintf(file,"  <node name=\"%p\" ", node);
575     if((node_name)&&((name=node_name(node)))) fprintf(file,"label=\"%s\" ",name);
576     if((node_data_print)&&((name=node_data_print(node->data)))) 
577       fprintf(file,"data=\"%s\" ",name);
578     fprintf(file,">\n");
579   }
580   xbt_dynar_foreach(g->edges, cursor, edge) {
581     fprintf(file,"  <edge source=\"%p\" target =\"%p\" ",
582             edge->src, edge->dst );
583     if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"label=\"%s\" ",name);
584     if(edge->length>=0.0) fprintf(file,"length=\"%g\" ",edge->length);
585     if((edge_data_print)&&((name=edge_data_print(edge->data)))) 
586       fprintf(file,"data=\"%s\" ",name);
587     fprintf(file,">\n");
588   }
589   fprintf(file,"</graph>\n");
590   fclose(file);
591 }
592