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"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(graph, xbt, "Graph");
27 xbt_graph_t xbt_graph_new_graph(unsigned short int directed, void *data)
29 xbt_graph_t graph = NULL;
30 graph = xbt_new0(struct xbt_graph, 1);
31 graph->directed = directed;
33 graph->nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
34 graph->edges = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
39 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
41 xbt_node_t node = NULL;
42 node = xbt_new0(struct xbt_node, 1);
44 node->in = xbt_dynar_new(sizeof(xbt_node_t), NULL);
45 node->out = xbt_dynar_new(sizeof(xbt_node_t), NULL);
46 xbt_dynar_push(g->nodes, &node);
52 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g,
53 xbt_node_t src, xbt_node_t dst, void *data)
55 xbt_edge_t edge = NULL;
58 edge = xbt_new0(struct xbt_edge, 1);
59 xbt_dynar_push(src->out, &edge);
61 xbt_dynar_push(dst->in, &edge);
62 else /* only the "out" field is used */
63 xbt_dynar_push(dst->out, &edge);
69 xbt_dynar_push(g->edges, &edge);
76 * \param l poor victim
78 * Free the graph structure.
80 void xbt_graph_free_graph(xbt_graph_t g,
81 void node_free_function(void *ptr),
82 void edge_free_function(void *ptr),
83 void graph_free_function(void *ptr))
86 xbt_node_t node = NULL;
87 xbt_edge_t edge = NULL;
90 xbt_dynar_foreach(g->nodes, cursor, node)
92 xbt_dynar_free(&(node->out));
93 xbt_dynar_free(&(node->in));
94 if(node_free_function)
95 node_free_function(node->data);
98 xbt_dynar_foreach(g->edges, cursor, edge)
100 if(edge_free_function)
101 edge_free_function(edge->data);
104 xbt_dynar_foreach(g->nodes, cursor, node)
106 xbt_dynar_free(&(g->nodes));
108 xbt_dynar_foreach(g->edges, cursor, edge)
110 xbt_dynar_free(&(g->edges));
119 void xbt_graph_free_node(xbt_graph_t g, xbt_node_t n,
120 void_f_pvoid_t * node_free_function,
121 void_f_pvoid_t * edge_free_function)
126 xbt_node_t node = NULL;
127 xbt_edge_t edge = NULL;
129 nbr = xbt_dynar_length(g->edges);
131 for (i = 0; i < nbr; i++)
133 xbt_dynar_cursor_get(g->edges, &cursor, &edge);
135 if ((edge->dst == n) || (edge->src == n))
137 xbt_graph_free_edge(g, edge, edge_free_function);
139 else xbt_dynar_cursor_step( g->edges, &cursor);
143 if ((node_free_function) && (n->data))
144 node_free_function(n->data);
147 xbt_dynar_foreach(g->nodes, cursor, node)
150 xbt_dynar_cursor_rm(g->nodes, &cursor);
157 void xbt_graph_free_edge(xbt_graph_t g, xbt_edge_t e,
158 void free_function(void *ptr))
162 xbt_edge_t edge = NULL;
164 if ((free_function) && (e->data))
165 free_function(e->data);
167 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 } else { /* only the out field is used */
175 idx = __xbt_find_in_dynar(edge->dst->out,edge);
176 xbt_dynar_remove_at(edge->dst->out, idx,NULL);
179 idx = __xbt_find_in_dynar(edge->src->out,edge);
180 xbt_dynar_remove_at(edge->src->out,idx,NULL);
182 xbt_dynar_cursor_rm(g->edges, &cursor);
189 int __xbt_find_in_dynar(xbt_dynar_t dynar, void *p)
195 xbt_dynar_foreach(dynar, cursor, tmp)
203 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
208 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
213 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
219 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
225 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
231 double xbt_graph_edge_get_length(xbt_edge_t e)
237 /*construct the adjacency matrix corresponding to a graph,
238 the weights are the distances between nodes
240 double *xbt_graph_get_length_matrix(xbt_graph_t g)
246 xbt_edge_t edge = NULL;
247 xbt_node_t node=NULL;
250 # define D(u,v) d[(u)*n+(v)]
251 n = xbt_dynar_length(g->nodes);
253 d = (double *) xbt_new0(double, n*n);
255 for (i = 0; i < n * n; i++)
260 xbt_dynar_foreach(g->nodes, cursor, node)
263 D(cursor, cursor) = 0;
265 xbt_dynar_foreach(node->out, in_cursor, edge)
268 idx= __xbt_find_in_dynar(g->nodes, edge->src);
269 else /*case of undirected graphs*/
270 idx = __xbt_find_in_dynar(g->nodes, edge->dst);
271 D( cursor,idx) = edge->length;
281 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
286 n = xbt_dynar_length(g->nodes);
288 # define D(u,v) d[(u)*n+(v)]
289 # define P(u,v) p[(u)*n+(v)]
291 for (i = 0; i < n * n; i++)
297 for (i = 0; i < n; i++)
299 for (j = 0; j < n; j++)
303 P(i,j) =*((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, i));
308 for (k = 0; k < n; k++)
310 for (i = 0; i < n; i++)
312 for (j = 0; j < n; j++)
314 if ((D(i, k) != -1) && (D(k, j) != -1))
316 if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j)))
318 D(i, j) = D(i, k) + D(k, j);
332 /*computes all-pairs shortest paths*/
333 xbt_node_t *xbt_graph_shortest_paths(xbt_graph_t g)
343 # define P(u,v) p[(u)*n+(v)]
344 # define R(u,v) r[(u)*n+(v)]
346 n = xbt_dynar_length(g->nodes);
347 adj = xbt_graph_get_length_matrix(g);
348 d = xbt_new0(double,n*n);
349 p = xbt_new0(xbt_node_t,n*n);
350 r = xbt_new0(xbt_node_t,n*n);
352 xbt_floyd_algorithm(g, adj, d, p);
354 for (i = 0; i < n; i++)
356 for (j = 0; j < n; j++)
360 while ((P(i, k)) && (__xbt_find_in_dynar(g->nodes, P(i, k)) != i))
362 k = __xbt_find_in_dynar(g->nodes, P(i, k));
367 R(i, j) = *((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, k));
381 xbt_edge_t* xbt_graph_spanning_tree_prim(xbt_graph_t g)
384 int tree_size_max=xbt_dynar_length(g->nodes)-1;
385 xbt_edge_t *tree = xbt_new0(xbt_edge_t,tree_size_max);
387 xbt_node_t node = NULL;
388 xbt_dynar_t edge_list = NULL;
389 xbt_heap_t heap = xbt_heap_new(10,NULL);
392 xbt_assert0(!(g->directed),
393 "Spanning trees do not make sense on directed graphs");
395 node = xbt_dynar_getfirst_as(g->nodes,xbt_node_t);
396 node->xbtdata = (void*) 1;
397 edge_list = node->out;
398 xbt_dynar_foreach(edge_list, cursor, e)
399 xbt_heap_push(heap,e, -(e->length));
401 while((edge=xbt_heap_pop(heap))) {
402 if((edge->src->xbtdata) && (edge->dst->xbtdata)) continue;
403 tree[tree_size++]=edge;
404 if(!(edge->src->xbtdata)) {
405 edge->src->xbtdata = (void*) 1;
406 edge_list = edge->src->out;
407 xbt_dynar_foreach(edge_list, cursor, e) {
408 xbt_heap_push(heap,e, -(e->length));
411 edge->dst->xbtdata = (void*) 1;
412 edge_list = edge->dst->out;
413 xbt_dynar_foreach(edge_list, cursor, e) {
414 xbt_heap_push(heap,e, -(e->length));
417 if(tree_size==tree_size_max) break;
422 xbt_dynar_foreach(g->nodes, cursor, node) {
423 node->xbtdata = NULL;
428 /********************* Import and Export ******************/
429 static xbt_graph_t parsed_graph = NULL;
430 static xbt_dict_t parsed_nodes = NULL;
432 static void __parse_graph_begin(void)
436 static void __parse_graph_end(void)
441 static void __parse_node(void)
444 xbt_graph_new_node(parsed_graph, NULL);
446 xbt_dict_set(parsed_nodes, A_graphxml_node_name, (void *) node, NULL);
448 DEBUG1("<node label=\"%s\"/>", (char *) (node->data));
450 static void __parse_edge(void)
453 xbt_graph_new_edge(parsed_graph,
454 xbt_dict_get(parsed_nodes,A_graphxml_edge_source),
455 xbt_dict_get(parsed_nodes,A_graphxml_edge_target),
458 xbt_graph_edge_set_length(edge, atof(A_graphxml_edge_length));
460 DEBUG4("<edge name=\"%s\" source=\"%s\" target=\"%s\" length=\"%f\"/>",
462 (char *) (edge->src)->data,
463 (char *) (edge->dst)->data,
464 xbt_graph_edge_get_length(edge));
467 xbt_graph_t xbt_graph_read(const char *filename)
469 xbt_graph_t graph = xbt_graph_new_graph(1, NULL);
471 parsed_graph = graph;
472 parsed_nodes = xbt_dict_new();
475 xbt_graph_parse_reset_parser();
477 STag_graphxml_graph_fun = __parse_graph_begin;
478 ETag_graphxml_graph_fun = __parse_graph_end;
479 ETag_graphxml_node_fun = __parse_node;
480 ETag_graphxml_edge_fun = __parse_edge;
483 xbt_graph_parse_open(filename);
484 xbt_assert1((!xbt_graph_parse()), "Parse error in %s", filename);
485 xbt_graph_parse_close();
487 xbt_dict_free(&parsed_nodes);
493 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename,
494 const char *(node_name) (xbt_node_t),
495 const char *(edge_name) (xbt_edge_t))
498 xbt_node_t node = NULL;
499 xbt_edge_t edge = NULL;
501 const char *name=NULL;
503 file=fopen(filename,"w");
504 xbt_assert1(file, "Failed to open %s \n",filename);
506 if(g->directed) fprintf(file,"digraph test {\n");
507 else fprintf(file,"graph test {\n");
509 fprintf(file," graph [overlap=scale]\n");
511 fprintf(file," node [shape=box, style=filled]\n");
512 fprintf(file," node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
514 xbt_dynar_foreach(g->nodes, cursor, node) {
515 fprintf(file," \"%p\" ", node);
516 if((node_name)&&((name=node_name(node)))) fprintf(file,"[label=\"%s\"]",name);
519 xbt_dynar_foreach(g->edges, cursor, edge) {
521 fprintf(file," \"%p\" -> \"%p\"",edge->src, edge->dst);
523 fprintf(file," \"%p\" -- \"%p\"",edge->src, edge->dst);
524 if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"[label=\"%s\"]",name);
531 void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
532 const char *(node_name)(xbt_node_t),
533 const char *(edge_name)(xbt_edge_t))
536 xbt_node_t node = NULL;
537 xbt_edge_t edge = NULL;
539 const char *name = NULL;
541 file=fopen(filename,"w");
542 xbt_assert1(file, "Failed to open %s \n",filename);
544 fprintf(file,"<?xml version='1.0'?>\n");
545 fprintf(file,"<!DOCTYPE graph SYSTEM \"graphxml.dtd\">\n");
546 fprintf(file,"<graph>\n");
547 xbt_dynar_foreach(g->nodes, cursor, node) {
548 fprintf(file," <node name=\"%p\" ", node);
549 if((node_name)&&((name=node_name(node)))) fprintf(file,"label=\"%s\" ",name);
552 xbt_dynar_foreach(g->edges, cursor, edge) {
553 fprintf(file," <edge source=\"%p\" target =\"%p\" ",
554 edge->src, edge->dst );
555 if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"label=\"%s\" ",name);
558 fprintf(file,"</graph>\n");