7 /* a generic graph library. */
9 /* Copyright (c) 2006 Darina Dimitrova, Arnaud Legrand.
10 All rights reserved. */
12 /* This program is free software; you can redistribute it and/or modify it
13 * under the terms of the license (GNU LGPL) which comes with this package. */
16 #include "xbt/sysdep.h"
18 #include "xbt/graph.h"
19 #include "graph_private.h"
20 #include "xbt/graphxml_parse.h"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(graph, xbt, "Graph");
30 xbt_graph_t xbt_graph_new_graph(unsigned short int directed, void *data)
32 xbt_graph_t graph = NULL;
33 graph = xbt_new0(struct xbt_graph, 1);
34 graph->directed = directed;
36 graph->nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
37 graph->edges = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
42 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
44 xbt_node_t node = NULL;
45 node = xbt_new0(struct xbt_node, 1);
47 node->in = xbt_dynar_new(sizeof(xbt_node_t), NULL);
48 node->out = xbt_dynar_new(sizeof(xbt_node_t), NULL);
49 xbt_dynar_push(g->nodes, &node);
55 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g,
56 xbt_node_t src, xbt_node_t dst, void *data)
58 xbt_edge_t edge = NULL;
61 edge = xbt_new0(struct xbt_edge, 1);
62 xbt_dynar_push(src->out, &edge);
63 xbt_dynar_push(dst->in, &edge);
70 xbt_dynar_push(src->in, &edge);
71 xbt_dynar_push(dst->out, &edge);
74 xbt_dynar_push(g->edges, &edge);
81 * \param l poor victim
83 * Free the graph structure.
85 void xbt_graph_free_graph(xbt_graph_t g,
86 void node_free_function(void *ptr),
87 void edge_free_function(void *ptr),
88 void graph_free_function(void *ptr))
91 xbt_node_t node = NULL;
92 xbt_edge_t edge = NULL;
95 xbt_dynar_foreach(g->nodes, cursor, node)
97 xbt_dynar_free(&(node->out));
98 xbt_dynar_free(&(node->in));
99 if(node_free_function)
100 node_free_function(node->data);
103 xbt_dynar_foreach(g->edges, cursor, edge)
105 if(edge_free_function)
106 edge_free_function(edge->data);
109 xbt_dynar_foreach(g->nodes, cursor, node)
111 xbt_dynar_free(&(g->nodes));
113 xbt_dynar_foreach(g->edges, cursor, edge)
115 xbt_dynar_free(&(g->edges));
124 void xbt_graph_free_node(xbt_graph_t g, xbt_node_t n,
125 void_f_pvoid_t * node_free_function,
126 void_f_pvoid_t * edge_free_function)
131 xbt_node_t node = NULL;
132 xbt_edge_t edge = NULL;
134 nbr = xbt_dynar_length(g->edges);
136 for (i = 0; i < nbr; i++)
138 xbt_dynar_cursor_get(g->edges, &cursor, &edge);
140 if ((edge->dst == n) || (edge->src == n))
142 xbt_graph_free_edge(g, edge, edge_free_function);
144 else xbt_dynar_cursor_step( g->edges, &cursor);
148 if ((node_free_function) && (n->data))
149 node_free_function(n->data);
152 xbt_dynar_foreach(g->nodes, cursor, node)
155 xbt_dynar_cursor_rm(g->nodes, &cursor);
162 void xbt_graph_free_edge(xbt_graph_t g, xbt_edge_t e,
163 void free_function(void *ptr))
167 xbt_edge_t edge = NULL;
169 if ((free_function) && (e->data))
170 free_function(e->data);
172 xbt_dynar_foreach(g->edges, cursor, edge)
176 idx = __xbt_find_in_dynar(edge->dst->in,edge);
177 xbt_dynar_remove_at(edge->dst->in, idx,NULL);
178 idx = __xbt_find_in_dynar(edge->src->out,edge);
179 xbt_dynar_remove_at(edge->src->out,idx,NULL);
182 idx = __xbt_find_in_dynar(edge->src->in,edge);
183 xbt_dynar_remove_at(edge->src->in,idx,NULL);
184 idx = __xbt_find_in_dynar(edge->dst->out,edge);
185 xbt_dynar_remove_at(edge->dst->out,idx,NULL);
187 xbt_dynar_cursor_rm(g->edges, &cursor);
194 int __xbt_find_in_dynar(xbt_dynar_t dynar, void *p)
200 xbt_dynar_foreach(dynar, cursor, tmp)
208 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
213 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
218 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
224 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
230 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
236 double xbt_graph_edge_get_length(xbt_edge_t e)
242 /*construct the adjacency matrix corresponding to a graph,
243 the weights are the distances between nodes
245 double *xbt_graph_get_length_matrix(xbt_graph_t g)
251 xbt_edge_t edge = NULL;
252 xbt_node_t node=NULL;
255 # define D(u,v) d[(u)*n+(v)]
256 n = xbt_dynar_length(g->nodes);
258 d = (double *) xbt_new0(double, n*n);
260 for (i = 0; i < n * n; i++)
265 xbt_dynar_foreach(g->nodes, cursor, node)
268 D(cursor, cursor) = 0;
270 xbt_dynar_foreach(node->out, in_cursor, edge)
273 idx= __xbt_find_in_dynar(g->nodes, edge->src);
274 else /*case of undirected graphs*/
275 idx = __xbt_find_in_dynar(g->nodes, edge->dst);
276 D( cursor,idx) = edge->length;
286 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
291 n = xbt_dynar_length(g->nodes);
293 # define D(u,v) d[(u)*n+(v)]
294 # define P(u,v) p[(u)*n+(v)]
296 for (i = 0; i < n * n; i++)
302 for (i = 0; i < n; i++)
304 for (j = 0; j < n; j++)
308 P(i,j) =*((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, i));
313 for (k = 0; k < n; k++)
315 for (i = 0; i < n; i++)
317 for (j = 0; j < n; j++)
319 if ((D(i, k) != -1) && (D(k, j) != -1))
321 if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j)))
323 D(i, j) = D(i, k) + D(k, j);
337 /*computes all-pairs shortest paths*/
338 xbt_node_t *xbt_graph_shortest_paths(xbt_graph_t g)
348 # define P(u,v) p[(u)*n+(v)]
349 # define R(u,v) r[(u)*n+(v)]
351 n = xbt_dynar_length(g->nodes);
352 adj = xbt_graph_get_length_matrix(g);
353 d = xbt_new0(double,n*n);
354 p = xbt_new0(xbt_node_t,n*n);
355 r = xbt_new0(xbt_node_t,n*n);
357 xbt_floyd_algorithm(g, adj, d, p);
359 for (i = 0; i < n; i++)
361 for (j = 0; j < n; j++)
365 while ((P(i, k)) && (__xbt_find_in_dynar(g->nodes, P(i, k)) != i))
367 k = __xbt_find_in_dynar(g->nodes, P(i, k));
372 R(i, j) = *((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, k));
386 static xbt_graph_t parsed_graph = NULL;
387 static xbt_dict_t parsed_nodes = NULL;
390 static void __parse_graph_begin(void)
394 static void __parse_graph_end(void)
399 static void __parse_node(void)
402 xbt_graph_new_node(parsed_graph, (void *) A_graphxml_node_name);
404 xbt_dict_set(parsed_nodes, A_graphxml_node_name, (void *) node, NULL);
406 DEBUG1("<node label=\"%s\"/>", (char *) (node->data));
408 static void __parse_edge(void)
410 xbt_edge_t edge = xbt_graph_new_edge(parsed_graph,
411 xbt_dict_get(parsed_nodes,
412 A_graphxml_edge_source),
413 xbt_dict_get(parsed_nodes,
414 A_graphxml_edge_target),
415 (void *) A_graphxml_edge_name);
417 xbt_graph_edge_set_length(edge, atof(A_graphxml_edge_length));
419 DEBUG4("<edge name=\"%s\" source=\"%s\" target=\"%s\" length=\"%f\"/>",
421 (char *) (edge->src)->data,
422 (char *) (edge->dst)->data,
423 xbt_graph_edge_get_length(edge));
426 xbt_graph_t xbt_graph_read(const char *filename)
428 xbt_graph_t graph = xbt_graph_new_graph(1, NULL);
430 parsed_graph = graph;
431 parsed_nodes = xbt_dict_new();
434 xbt_graph_parse_reset_parser();
436 STag_graphxml_graph_fun = __parse_graph_begin;
437 ETag_graphxml_graph_fun = __parse_graph_end;
438 ETag_graphxml_node_fun = __parse_node;
439 ETag_graphxml_edge_fun = __parse_edge;
442 xbt_graph_parse_open(filename);
443 xbt_assert1((!xbt_graph_parse()), "Parse error in %s", filename);
444 xbt_graph_parse_close();
446 xbt_dict_free(&parsed_nodes);
452 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename,
453 const char *(node_name) (xbt_node_t),
454 const char *(edge_name) (xbt_edge_t))
457 xbt_node_t node = NULL;
458 xbt_edge_t edge = NULL;
460 const char *name=NULL;
462 file=fopen(filename,"w");
463 xbt_assert1(file, "Failed to open %s \n",filename);
465 if(g->directed) fprintf(file,"digraph test {\n");
466 else fprintf(file,"graph test {\n");
468 fprintf(file," graph [overlap=scale]\n");
470 fprintf(file," node [shape=box, style=filled]\n");
471 fprintf(file," node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
473 xbt_dynar_foreach(g->nodes, cursor, node) {
474 fprintf(file," \"%p\" ", node);
475 if((node_name)&&((name=node_name(node)))) fprintf(file,"[label=\"%s\"]",name);
478 xbt_dynar_foreach(g->edges, cursor, edge) {
480 fprintf(file," \"%p\" -> \"%p\"",edge->src, edge->dst);
482 fprintf(file," \"%p\" -- \"%p\"",edge->src, edge->dst);
483 if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"[label=\"%s\"]",name);
490 void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
491 const char *(node_name)(xbt_node_t),
492 const char *(edge_name)(xbt_edge_t))
495 xbt_node_t node = NULL;
496 xbt_edge_t edge = NULL;
498 const char *name = NULL;
500 file=fopen(filename,"w");
501 xbt_assert1(file, "Failed to open %s \n",filename);
503 fprintf(file,"<?xml version='1.0'?>\n");
504 fprintf(file,"<!DOCTYPE graph SYSTEM \"graphxml.dtd\">\n");
505 fprintf(file,"<graph>\n");
506 xbt_dynar_foreach(g->nodes, cursor, node) {
507 fprintf(file," <node name=\"%p\" ", node);
508 if((node_name)&&((name=node_name(node)))) fprintf(file,"label=\"%s\" ",name);
511 xbt_dynar_foreach(g->edges, cursor, edge) {
512 fprintf(file," <edge source=\"%p\" target =\"%p\" ",
513 edge->src, edge->dst );
514 if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"label=\"%s\" ",name);
517 fprintf(file,"</graph>\n");