3 /* a generic graph library. */
5 /* Copyright (c) 2006 Darina Dimitrova, Arnaud Legrand. */
6 * All rights reserved. */
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. */
12 #include "xbt/sysdep.h"
14 #include "xbt/graph.h"
15 #include "graph_private.h"
16 #include "xbt/graphxml_parse.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(graph, xbt, "Graph");
26 xbt_graph_t xbt_graph_new_graph(unsigned short int directed, void *data)
28 xbt_graph_t graph = NULL;
29 graph = xbt_new0(struct xbt_graph, 1);
30 graph->directed = directed;
32 graph->nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
33 graph->edges = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
38 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
40 xbt_node_t node = NULL;
41 node = xbt_new0(struct xbt_node, 1);
43 node->in = xbt_dynar_new(sizeof(xbt_node_t), NULL);
44 node->out = xbt_dynar_new(sizeof(xbt_node_t), NULL);
45 xbt_dynar_push(g->nodes, &node);
51 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g,
52 xbt_node_t src, xbt_node_t dst, void *data)
54 xbt_edge_t edge = NULL;
57 edge = xbt_new0(struct xbt_edge, 1);
58 xbt_dynar_push(src->out, &edge);
59 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)
127 xbt_node_t node = NULL;
128 xbt_edge_t edge = NULL;
130 nbr = xbt_dynar_length(g->edges);
132 for (i = 0; i < nbr; i++)
134 xbt_dynar_cursor_get(g->edges, &cursor, &edge);
136 if ((edge->dst == n) || (edge->src == n))
138 xbt_graph_free_edge(g, edge, edge_free_function);
140 else xbt_dynar_cursor_step( g->edges, &cursor);
144 if ((node_free_function) && (n->data))
145 node_free_function(n->data);
148 xbt_dynar_foreach(g->nodes, cursor, node)
151 xbt_dynar_cursor_rm(g->nodes, &cursor);
158 void xbt_graph_free_edge(xbt_graph_t g, xbt_edge_t e,
159 void free_function(void *ptr))
163 xbt_edge_t edge = NULL;
165 if ((free_function) && (e->data))
166 free_function(e->data);
168 xbt_dynar_foreach(g->edges, cursor, edge)
172 idx = __xbt_find_in_dynar(edge->dst->in,edge);
173 xbt_dynar_remove_at(edge->dst->in, idx,NULL);
174 idx = __xbt_find_in_dynar(edge->src->out,edge);
175 xbt_dynar_remove_at(edge->src->out,idx,NULL);
178 idx = __xbt_find_in_dynar(edge->src->in,edge);
179 xbt_dynar_remove_at(edge->src->in,idx,NULL);
180 idx = __xbt_find_in_dynar(edge->dst->out,edge);
181 xbt_dynar_remove_at(edge->dst->out,idx,NULL);
183 xbt_dynar_cursor_rm(g->edges, &cursor);
190 int __xbt_find_in_dynar(xbt_dynar_t dynar, void *p)
196 xbt_dynar_foreach(dynar, cursor, tmp)
204 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
209 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
214 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
220 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
226 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
232 double xbt_graph_edge_get_length(xbt_edge_t e)
238 /*construct the adjacency matrix corresponding to a graph,
239 the weights are the distances between nodes
241 double *xbt_graph_get_length_matrix(xbt_graph_t g)
247 xbt_edge_t edge = NULL;
248 xbt_node_t node=NULL;
251 # define D(u,v) d[(u)*n+(v)]
252 n = xbt_dynar_length(g->nodes);
254 d = (double *) xbt_new0(double, n*n);
256 for (i = 0; i < n * n; i++)
261 xbt_dynar_foreach(g->nodes, cursor, node)
264 D(cursor, cursor) = 0;
266 xbt_dynar_foreach(node->out, in_cursor, edge)
269 idx= __xbt_find_in_dynar(g->nodes, edge->src);
270 else /*case of undirected graphs*/
271 idx = __xbt_find_in_dynar(g->nodes, edge->dst);
272 D( cursor,idx) = edge->length;
282 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
287 n = xbt_dynar_length(g->nodes);
289 # define D(u,v) d[(u)*n+(v)]
290 # define P(u,v) p[(u)*n+(v)]
292 for (i = 0; i < n * n; i++)
298 for (i = 0; i < n; i++)
300 for (j = 0; j < n; j++)
304 P(i,j) =*((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, i));
309 for (k = 0; k < n; k++)
311 for (i = 0; i < n; i++)
313 for (j = 0; j < n; j++)
315 if ((D(i, k) != -1) && (D(k, j) != -1))
317 if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j)))
319 D(i, j) = D(i, k) + D(k, j);
333 /*computes all-pairs shortest paths*/
334 xbt_node_t *xbt_graph_shortest_paths(xbt_graph_t g)
344 # define P(u,v) p[(u)*n+(v)]
345 # define R(u,v) r[(u)*n+(v)]
347 n = xbt_dynar_length(g->nodes);
348 adj = xbt_graph_get_length_matrix(g);
349 d = xbt_new0(double,n*n);
350 p = xbt_new0(xbt_node_t,n*n);
351 r = xbt_new0(xbt_node_t,n*n);
353 xbt_floyd_algorithm(g, adj, d, p);
355 for (i = 0; i < n; i++)
357 for (j = 0; j < n; j++)
361 while ((P(i, k)) && (__xbt_find_in_dynar(g->nodes, P(i, k)) != i))
363 k = __xbt_find_in_dynar(g->nodes, P(i, k));
368 R(i, j) = *((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, k));
381 static xbt_graph_t parsed_graph = NULL;
382 static xbt_dict_t parsed_nodes = NULL;
385 static void __parse_graph_begin(void)
389 static void __parse_graph_end(void)
394 static void __parse_node(void)
397 xbt_graph_new_node(parsed_graph, NULL);
399 xbt_dict_set(parsed_nodes, A_graphxml_node_name, (void *) node, NULL);
401 DEBUG1("<node label=\"%s\"/>", (char *) (node->data));
403 static void __parse_edge(void)
406 xbt_graph_new_edge(parsed_graph,
407 xbt_dict_get(parsed_nodes,A_graphxml_edge_source),
408 xbt_dict_get(parsed_nodes,A_graphxml_edge_target),
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,
417 xbt_graph_edge_get_length(edge));
420 xbt_graph_t xbt_graph_read(const char *filename)
422 xbt_graph_t graph = xbt_graph_new_graph(1, NULL);
424 parsed_graph = graph;
425 parsed_nodes = 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);
446 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename,
447 const char *(node_name) (xbt_node_t),
448 const char *(edge_name) (xbt_edge_t))
451 xbt_node_t node = NULL;
452 xbt_edge_t edge = NULL;
454 const char *name=NULL;
456 file=fopen(filename,"w");
457 xbt_assert1(file, "Failed to open %s \n",filename);
459 if(g->directed) fprintf(file,"digraph test {\n");
460 else fprintf(file,"graph test {\n");
462 fprintf(file," graph [overlap=scale]\n");
464 fprintf(file," node [shape=box, style=filled]\n");
465 fprintf(file," node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
467 xbt_dynar_foreach(g->nodes, cursor, node) {
468 fprintf(file," \"%p\" ", node);
469 if((node_name)&&((name=node_name(node)))) fprintf(file,"[label=\"%s\"]",name);
472 xbt_dynar_foreach(g->edges, cursor, edge) {
474 fprintf(file," \"%p\" -> \"%p\"",edge->src, edge->dst);
476 fprintf(file," \"%p\" -- \"%p\"",edge->src, edge->dst);
477 if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"[label=\"%s\"]",name);
484 void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
485 const char *(node_name)(xbt_node_t),
486 const char *(edge_name)(xbt_edge_t))
489 xbt_node_t node = NULL;
490 xbt_edge_t edge = NULL;
492 const char *name = NULL;
494 file=fopen(filename,"w");
495 xbt_assert1(file, "Failed to open %s \n",filename);
497 fprintf(file,"<?xml version='1.0'?>\n");
498 fprintf(file,"<!DOCTYPE graph SYSTEM \"graphxml.dtd\">\n");
499 fprintf(file,"<graph>\n");
500 xbt_dynar_foreach(g->nodes, cursor, node) {
501 fprintf(file," <node name=\"%p\" ", node);
502 if((node_name)&&((name=node_name(node)))) fprintf(file,"label=\"%s\" ",name);
505 xbt_dynar_foreach(g->edges, cursor, edge) {
506 fprintf(file," <edge source=\"%p\" target =\"%p\" ",
507 edge->src, edge->dst );
508 if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"label=\"%s\" ",name);
511 fprintf(file,"</graph>\n");