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);
161 void xbt_graph_free_edge(xbt_graph_t g, xbt_edge_t e,
162 void free_function(void *ptr))
166 xbt_edge_t edge = NULL;
168 if ((free_function) && (e->data))
169 free_function(e->data);
171 xbt_dynar_foreach(g->edges, cursor, edge)
175 idx = __xbt_find_in_dynar(edge->dst->in,edge);
176 xbt_dynar_remove_at(edge->dst->in, idx,NULL);
177 idx = __xbt_find_in_dynar(edge->src->out,edge);
178 xbt_dynar_remove_at(edge->src->out,idx,NULL);
181 idx = __xbt_find_in_dynar(edge->src->in,edge);
182 xbt_dynar_remove_at(edge->src->in,idx,NULL);
183 idx = __xbt_find_in_dynar(edge->dst->out,edge);
184 xbt_dynar_remove_at(edge->dst->out,idx,NULL);
186 xbt_dynar_cursor_rm(g->edges, &cursor);
192 int __xbt_find_in_dynar(xbt_dynar_t dynar, void *p)
198 xbt_dynar_foreach(dynar, cursor, tmp)
206 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
211 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
216 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
222 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
228 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
234 double xbt_graph_edge_get_length(xbt_edge_t e)
240 /*construct the adjacency matrix corresponding to a graph,
241 the weights are the distances between nodes
243 double *xbt_graph_get_length_matrix(xbt_graph_t g)
249 xbt_edge_t edge = NULL;
250 xbt_node_t node=NULL;
253 # define D(u,v) d[(u)*n+(v)]
254 n = xbt_dynar_length(g->nodes);
256 d = (double *) xbt_malloc(n * n * (sizeof(double)));
258 for (i = 0; i < n * n; i++)
263 xbt_dynar_foreach(g->nodes, cursor, node)
266 D(cursor, cursor) = 0;
268 xbt_dynar_foreach(node->out, in_cursor, edge)
271 idx= __xbt_find_in_dynar(g->nodes, edge->src);
272 else /*case of undirected graphs*/
273 idx = __xbt_find_in_dynar(g->nodes, edge->dst);
274 D( cursor,idx) = edge->length;
284 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
289 n = xbt_dynar_length(g->nodes);
291 # define D(u,v) d[(u)*n+(v)]
292 # define P(u,v) p[(u)*n+(v)]
294 for (i = 0; i < n * n; i++)
300 for (i = 0; i < n; i++)
302 for (j = 0; j < n; j++)
306 P(i,j) =*((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, i));
311 for (k = 0; k < n; k++)
313 for (i = 0; i < n; i++)
315 for (j = 0; j < n; j++)
317 if ((D(i, k) != -1) && (D(k, j) != -1))
319 if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j)))
321 D(i, j) = D(i, k) + D(k, j);
335 /*computes all-pairs shortest paths*/
336 xbt_node_t *xbt_graph_shortest_paths(xbt_graph_t g)
346 # define P(u,v) p[(u)*n+(v)]
347 # define R(u,v) r[(u)*n+(v)]
349 n = xbt_dynar_length(g->nodes);
350 adj = xbt_graph_get_length_matrix(g);
351 d = xbt_malloc(n*n*sizeof(double));
352 p = xbt_malloc(n*n*sizeof(xbt_node_t));
353 r = xbt_malloc(n*n*sizeof(xbt_node_t));
355 xbt_floyd_algorithm(g, adj, d, p);
357 for (i = 0; i < n; i++)
359 for (j = 0; j < n; j++)
363 while ((P(i, k)) && (__xbt_find_in_dynar(g->nodes, P(i, k)) != i))
365 k = __xbt_find_in_dynar(g->nodes, P(i, k));
370 R(i, j) = *((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, k));
383 static xbt_graph_t parsed_graph = NULL;
384 static xbt_dict_t parsed_nodes = NULL;
387 static void __parse_graph_begin(void)
391 static void __parse_graph_end(void)
396 static void __parse_node(void)
399 xbt_graph_new_node(parsed_graph, (void *) A_graphxml_node_name);
401 xbt_dict_set(parsed_nodes, A_graphxml_node_name, (void *) node, NULL);
403 DEBUG1("<node label=\"%s\"/>", (char *) (node->data));
405 static void __parse_edge(void)
407 xbt_edge_t edge = xbt_graph_new_edge(parsed_graph,
408 xbt_dict_get(parsed_nodes,
409 A_graphxml_edge_source),
410 xbt_dict_get(parsed_nodes,
411 A_graphxml_edge_target),
412 (void *) A_graphxml_edge_name);
414 xbt_graph_edge_set_length(edge, atof(A_graphxml_edge_length));
416 DEBUG4("<edge name=\"%s\" source=\"%s\" target=\"%s\" length=\"%f\"/>",
418 (char *) (edge->src)->data,
419 (char *) (edge->dst)->data,
420 xbt_graph_edge_get_length(edge));
423 xbt_graph_t xbt_graph_read(const char *filename)
425 xbt_graph_t graph = xbt_graph_new_graph(1, NULL);
427 parsed_graph = graph;
428 parsed_nodes = xbt_dict_new();
431 xbt_graph_parse_reset_parser();
433 STag_graphxml_graph_fun = __parse_graph_begin;
434 ETag_graphxml_graph_fun = __parse_graph_end;
435 ETag_graphxml_node_fun = __parse_node;
436 ETag_graphxml_edge_fun = __parse_edge;
439 xbt_graph_parse_open(filename);
440 xbt_assert1((!xbt_graph_parse()), "Parse error in %s", filename);
441 xbt_graph_parse_close();
443 xbt_dict_free(&parsed_nodes);
448 void xbt_graph_export_surfxml(xbt_graph_t g,
449 const char *filename,
450 const char *(node_name) (xbt_node_t),
451 const char *(edge_name) (xbt_edge_t)