6 /* a generic graph library. */
8 /* Copyright (c) 2006 Darina Dimitrova, Arnaud Legrand.
9 All rights reserved. */
11 /* This program is free software; you can redistribute it and/or modify it
12 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "xbt/sysdep.h"
17 #include "xbt/graph.h"
18 #include "graph_private.h"
19 #include "xbt/graphxml_parse.h"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(graph, xbt, "Graph");
29 xbt_graph_t xbt_graph_new_graph(unsigned short int directed, void *data)
31 xbt_graph_t graph = NULL;
32 graph = xbt_new0(struct xbt_graph, 1);
33 graph->directed = directed;
35 graph->nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
36 graph->edges = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
41 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
43 xbt_node_t node = NULL;
44 node = xbt_new0(struct xbt_node, 1);
46 node->in = xbt_dynar_new(sizeof(xbt_node_t), NULL);
47 node->out = xbt_dynar_new(sizeof(xbt_node_t), NULL);
48 xbt_dynar_push(g->nodes, &node);
54 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g,
55 xbt_node_t src, xbt_node_t dst, void *data)
57 xbt_edge_t edge = NULL;
59 edge = xbt_new0(struct xbt_edge, 1);
60 xbt_dynar_push(src->out, &edge);
61 xbt_dynar_push(dst->in, &edge);
66 xbt_dynar_push(src->in, &edge);
67 xbt_dynar_push(dst->out, &edge);
70 xbt_dynar_push(g->edges, &edge);
77 * \param l poor victim
79 * Free the graph structure.
81 void xbt_graph_free_graph(xbt_graph_t g,
82 void node_free_function(void *ptr),
83 void edge_free_function(void *ptr),
84 void graph_free_function(void *ptr))
87 xbt_node_t node = NULL;
88 xbt_edge_t edge = NULL;
91 xbt_dynar_foreach(g->nodes, cursor, node)
93 xbt_dynar_free(&(node->out));
94 xbt_dynar_free(&(node->in));
95 if(node_free_function)
96 node_free_function(node->data);
99 xbt_dynar_foreach(g->edges, cursor, edge)
101 if(edge_free_function)
102 edge_free_function(edge->data);
105 xbt_dynar_foreach(g->nodes, cursor, node)
107 xbt_dynar_free(&(g->nodes));
109 xbt_dynar_foreach(g->edges, cursor, edge)
111 xbt_dynar_free(&(g->edges));
120 void xbt_graph_free_node(xbt_graph_t g, xbt_node_t n,
121 void_f_pvoid_t * node_free_function,
122 void_f_pvoid_t * edge_free_function)
128 xbt_node_t node = NULL;
129 xbt_edge_t edge = NULL;
131 if ((node_free_function) && (n->data))
132 node_free_function(n->data);
134 xbt_dynar_foreach(n->in,cursor,edge)
136 idx = __xbt_find_in_dynar(edge->src->out,edge);
137 xbt_dynar_remove_at(edge->src->out, idx,NULL);
140 xbt_dynar_foreach(n->out,cursor,edge)
142 idx = __xbt_find_in_dynar(edge->dst->in,edge);
143 xbt_dynar_remove_at(edge->dst->in, idx,NULL);
146 nbr = xbt_dynar_length(g->edges);
148 for (i = 0; i < nbr; i++)
150 xbt_dynar_cursor_get(g->edges, &cursor, &edge);
152 if ((edge->dst == n) || (edge->src == n))
154 xbt_graph_free_edge(g, edge, edge_free_function);
159 xbt_dynar_foreach(g->nodes, cursor, node)
162 xbt_dynar_cursor_rm(g->nodes, &cursor);
168 void xbt_graph_free_edge(xbt_graph_t g, xbt_edge_t e,
169 void free_function(void *ptr))
172 xbt_edge_t edge = NULL;
174 if ((free_function) && (e->data))
175 free_function(e->data);
177 xbt_dynar_foreach(g->edges, cursor, edge)
181 xbt_dynar_cursor_rm(g->edges, &cursor);
188 int __xbt_find_in_dynar(xbt_dynar_t dynar, void *p)
194 xbt_dynar_foreach(dynar, cursor, tmp)
199 /* FIXME : gerer le cas où n n'est pas dans le tableau, renvoyer
205 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
210 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
215 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
221 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
226 int xbt_get_node_index(xbt_graph_t g, xbt_node_t n)
231 xbt_dynar_foreach(g->nodes, cursor, tmp)
239 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
245 double xbt_graph_edge_get_length(xbt_edge_t e)
251 /*construct the adjacency matrix corresponding to a graph,
252 the weights are the distances between nodes
254 double *xbt_graph_get_length_matrix(xbt_graph_t g)
256 fprintf(stderr, "%s", "START GET LENGTHS\n");
261 xbt_edge_t edge = NULL;
265 # define D(u,v) d[(u)*n+(v)]
266 n = xbt_dynar_length(g->nodes);
268 d = (double *) xbt_malloc(n * n * (sizeof(double)));
270 for (i = 0; i < n * n; i++)
276 xbt_dynar_foreach(g->nodes, cursor, node) {
277 fprintf(stderr, "NODE NAME: %s\n", (char *) node->data);
278 /* fprintf(stderr,"CURSOR: %d\n",cursor ); */
280 D(cursor, cursor) = 0;
281 /* fprintf(stderr,"d[]= %le\n", D(cursor,cursor)); */
282 xbt_dynar_foreach(node->in, in_cursor, edge) {
283 fprintf(stderr, "EDGE IN: %s\n", (char *) edge->data);
284 fprintf(stderr, "EDGE DST: %s\n", (char *) edge->dst->data);
286 idx = xbt_get_node_index(g, edge->dst);
287 fprintf(stderr, "IDX: %d\n", idx);
288 /* fprintf(stderr,"EDGE ADR: %x\n",(int)edge ); */
289 /* fprintf(stderr,"EDGE LENGTH: %le\n", edge->length ); */
290 D(cursor, idx) = edge->length;
292 fprintf(stderr, "CURSOR END FOREACH: %d\n", cursor);
294 fprintf(stderr, "BEFORE RETURN\n");
301 /* calculate all-pairs shortest paths */
302 /* the shortest distance between node i and j are stocked in distances[i][j] */
303 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
308 n = xbt_dynar_length(g->nodes);
310 # define D(u,v) d[(u)*n+(v)]
311 # define P(u,v) p[(u)*n+(v)]
315 for (i = 0; i < n * n; i++) {
318 for (i = 0; i < n; i++) {
319 for (j = 0; j < n; j++) {
321 P(i, j) = xbt_dynar_get_ptr(g->nodes, i);
325 for (k = 0; k < n; k++) {
326 for (i = 0; i < n; i++) {
327 for (j = 0; j < n; j++) {
328 if ((D(i, k) != -1) && (D(k, j) != -1)) {
329 if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j))) {
330 D(i, j) = D(i, k) + D(k, j);
341 /*computes all-pairs shortest paths*/
342 xbt_node_t *xbt_graph_shortest_paths(xbt_graph_t g)
352 # define P(u,v) p[(u)*n+(v)]
353 # define R(u,v) r[(u)*n+(v)]
355 n = xbt_dynar_length(g->nodes);
356 adj = xbt_graph_get_length_matrix(g);
357 xbt_floyd_algorithm(g, adj, d, p);
359 for (i = 0; i < n; i++) {
360 for (j = 0; j < n; j++) {
362 while ((P(i, k)) && (xbt_get_node_index(g, P(i, k)) != i)) {
363 k = xbt_get_node_index(g, P(i, k));
366 R(i, j) = (xbt_node_t) xbt_dynar_get_ptr(g->nodes, k);
378 static xbt_graph_t parsed_graph = NULL;
379 static xbt_dict_t parsed_nodes = NULL;
380 static xbt_dict_t parsed_edges = NULL;
383 static void __parse_graph_begin(void)
387 static void __parse_graph_end(void)
392 static void __parse_node(void)
395 xbt_graph_new_node(parsed_graph, (void *) A_graphxml_node_name);
397 xbt_dict_set(parsed_nodes, A_graphxml_node_name, (void *) node, free);
399 DEBUG1("<node label=\"%s\"/>", (char *) (node->data));
401 static void __parse_edge(void)
403 xbt_edge_t edge = xbt_graph_new_edge(parsed_graph,
404 xbt_dict_get(parsed_nodes,
405 A_graphxml_edge_source),
406 xbt_dict_get(parsed_nodes,
407 A_graphxml_edge_target),
408 (void *) A_graphxml_edge_name);
410 xbt_dict_set(parsed_edges, A_graphxml_edge_name, (void *) edge, free);
411 xbt_graph_edge_set_length(edge, atof(A_graphxml_edge_length));
413 DEBUG4("<edge name=\"%s\" source=\"%s\" target=\"%s\" length=\"%f\"/>",
415 (char *) (edge->src)->data,
416 (char *) (edge->dst)->data, xbt_graph_edge_get_length(edge));
419 xbt_graph_t xbt_graph_read(const char *filename)
421 xbt_graph_t graph = xbt_graph_new_graph(1, NULL);
423 parsed_graph = graph;
424 parsed_nodes = xbt_dict_new();
425 parsed_edges = xbt_dict_new();
428 xbt_graph_parse_reset_parser();
430 STag_graphxml_graph_fun = __parse_graph_begin;
431 ETag_graphxml_graph_fun = __parse_graph_end;
432 ETag_graphxml_node_fun = __parse_node;
433 ETag_graphxml_edge_fun = __parse_edge;
436 xbt_graph_parse_open(filename);
437 xbt_assert1((!xbt_graph_parse()), "Parse error in %s", filename);
438 xbt_graph_parse_close();
440 xbt_dict_free(&parsed_nodes);
441 xbt_dict_free(&parsed_edges);
446 void xbt_graph_export_surfxml(xbt_graph_t g,
447 const char *filename,
448 const char *(node_name) (xbt_node_t),
449 const char *(edge_name) (xbt_edge_t)
455 /* ./xbt/graphxml_usage xbt/graph.xml --xbt-log=graph.thres=debug */