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"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_graph, xbt, "Graph");
27 /** @brief Constructor
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 /** @brief add a node to the given graph */
43 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
45 xbt_node_t node = NULL;
46 node = xbt_new0(struct xbt_node, 1);
49 /* only the "out" field is used */
50 node->in = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
52 node->out = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
53 node->position_x = -1.0;
54 node->position_y = -1.0;
56 xbt_dynar_push(g->nodes, &node);
61 /** @brief add an edge to the given graph */
62 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g,
63 xbt_node_t src, xbt_node_t dst, void *data)
65 xbt_edge_t edge = NULL;
67 edge = xbt_new0(struct xbt_edge, 1);
68 xbt_dynar_push(src->out, &edge);
70 xbt_dynar_push(dst->in, &edge);
71 else /* only the "out" field is used */
72 xbt_dynar_push(dst->out, &edge);
78 xbt_dynar_push(g->edges, &edge);
83 xbt_edge_t xbt_graph_get_edge(xbt_graph_t g, xbt_node_t src, xbt_node_t dst)
88 xbt_dynar_foreach(src->out, cursor, edge) {
89 DEBUG3("%p = %p--%p", edge, edge->src, edge->dst);
90 if ((edge->src == src) && (edge->dst == dst))
94 xbt_dynar_foreach(src->out, cursor, edge) {
95 DEBUG3("%p = %p--%p", edge, edge->src, edge->dst);
96 if ((edge->dst == src) && (edge->src == dst))
103 void *xbt_graph_node_get_data(xbt_node_t node)
108 void xbt_graph_node_set_data(xbt_node_t node, void *data)
113 void *xbt_graph_edge_get_data(xbt_edge_t edge)
118 void xbt_graph_edge_set_data(xbt_edge_t edge, void *data)
123 /** @brief Destructor
124 * @param g: poor victim
125 * @param node_free_function: function to use to free data associated to each node
126 * @param edge_free_function: function to use to free data associated to each edge
127 * @param graph_free_function: function to use to free data associated to g
129 * Free the graph structure.
131 void xbt_graph_free_graph(xbt_graph_t g,
132 void_f_pvoid_t node_free_function,
133 void_f_pvoid_t edge_free_function,
134 void_f_pvoid_t graph_free_function)
136 unsigned int cursor = 0;
137 xbt_node_t node = NULL;
138 xbt_edge_t edge = NULL;
141 xbt_dynar_foreach(g->nodes, cursor, node) {
142 xbt_dynar_free(&(node->out));
143 xbt_dynar_free(&(node->in));
144 if (node_free_function)
145 (*node_free_function) (node->data);
148 xbt_dynar_foreach(g->edges, cursor, edge) {
149 if (edge_free_function)
150 (*edge_free_function) (edge->data);
153 xbt_dynar_foreach(g->nodes, cursor, node)
155 xbt_dynar_free(&(g->nodes));
157 xbt_dynar_foreach(g->edges, cursor, edge)
159 xbt_dynar_free(&(g->edges));
160 if (graph_free_function)
161 (*graph_free_function) (g->data);
168 /** @brief remove the given node from the given graph */
169 void xbt_graph_free_node(xbt_graph_t g, xbt_node_t n,
170 void_f_pvoid_t node_free_function,
171 void_f_pvoid_t edge_free_function)
175 unsigned int cursor = 0;
176 xbt_node_t node = NULL;
177 xbt_edge_t edge = NULL;
179 nbr = xbt_dynar_length(g->edges);
181 for (i = 0; i < nbr; i++) {
182 xbt_dynar_get_cpy(g->edges, cursor, &edge);
184 if ((edge->dst == n) || (edge->src == n)) {
185 xbt_graph_free_edge(g, edge, edge_free_function);
190 if ((node_free_function) && (n->data))
191 (*node_free_function) (n->data);
194 xbt_dynar_foreach(g->nodes, cursor, node)
196 xbt_dynar_cursor_rm(g->nodes, &cursor);
198 xbt_dynar_free(&(n->in));
199 xbt_dynar_free(&(n->out));
206 /** @brief remove the given edge from the given graph */
207 void xbt_graph_free_edge(xbt_graph_t g, xbt_edge_t e,
208 void_f_pvoid_t free_function)
211 unsigned int cursor = 0;
212 xbt_edge_t edge = NULL;
214 if ((free_function) && (e->data))
215 (*free_function) (e->data);
217 xbt_dynar_foreach(g->edges, cursor, edge) {
220 idx = __xbt_find_in_dynar(edge->dst->in, edge);
221 xbt_dynar_remove_at(edge->dst->in, idx, NULL);
222 } else { /* only the out field is used */
223 idx = __xbt_find_in_dynar(edge->dst->out, edge);
224 xbt_dynar_remove_at(edge->dst->out, idx, NULL);
227 idx = __xbt_find_in_dynar(edge->src->out, edge);
228 xbt_dynar_remove_at(edge->src->out, idx, NULL);
230 xbt_dynar_cursor_rm(g->edges, &cursor);
237 int __xbt_find_in_dynar(xbt_dynar_t dynar, void *p)
240 unsigned int cursor = 0;
243 xbt_dynar_foreach(dynar, cursor, tmp) {
250 /** @brief Retrieve the graph's nodes as a dynar */
251 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
256 /** @brief Retrieve the graph's edges as a dynar */
257 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
262 /** @brief Retrieve the node at the source of the given edge */
263 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
268 /** @brief Retrieve the node being the target of the given edge */
269 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
274 /** @brief Retrieve the outgoing edges of the given node */
275 xbt_dynar_t xbt_graph_node_get_outedges(xbt_node_t n)
280 /** @brief Set the weight of the given edge */
281 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
287 double xbt_graph_edge_get_length(xbt_edge_t e)
293 /** @brief construct the adjacency matrix corresponding to the given graph
295 * The weights are the distances between nodes
297 double *xbt_graph_get_length_matrix(xbt_graph_t g)
299 unsigned int cursor = 0;
300 unsigned int in_cursor = 0;
301 unsigned long idx, i;
303 xbt_edge_t edge = NULL;
304 xbt_node_t node = NULL;
307 # define D(u,v) d[(u)*n+(v)]
308 n = xbt_dynar_length(g->nodes);
310 d = (double *) xbt_new0(double, n * n);
312 for (i = 0; i < n * n; i++) {
316 xbt_dynar_foreach(g->nodes, cursor, node) {
318 D(cursor, cursor) = 0;
320 xbt_dynar_foreach(node->out, in_cursor, edge) {
321 if (edge->dst == node)
322 idx = __xbt_find_in_dynar(g->nodes, edge->src);
323 else /*case of undirected graphs */
324 idx = __xbt_find_in_dynar(g->nodes, edge->dst);
325 D(cursor, idx) = edge->length;
334 /** @brief Floyd-Warshall algorithm for shortest path finding
338 * The Floyd–Warshall algorithm takes as input an adjacency matrix
339 * representation of a weighted, directed graph (V, E). The weight of a
340 * path between two vertices is the sum of the weights of the edges along
341 * that path. The edges E of the graph may have negative weights, but the
342 * graph must not have any negative weight cycles. The algorithm computes,
343 * for each pair of vertices, the minimum weight among all paths between
344 * the two vertices. The running time complexity is Θ(|V|3).
346 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
349 unsigned long i, j, k;
351 n = xbt_dynar_length(g->nodes);
353 # define D(u,v) d[(u)*n+(v)]
354 # define P(u,v) p[(u)*n+(v)]
356 for (i = 0; i < n * n; i++) {
361 for (i = 0; i < n; i++) {
362 for (j = 0; j < n; j++) {
364 P(i, j) = *((xbt_node_t *) xbt_dynar_get_ptr(g->nodes, i));
369 for (k = 0; k < n; k++) {
370 for (i = 0; i < n; i++) {
371 for (j = 0; j < n; j++) {
372 if ((D(i, k) != -1) && (D(k, j) != -1)) {
373 if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j))) {
374 D(i, j) = D(i, k) + D(k, j);
388 /** @brief computes all-pairs shortest paths */
389 xbt_node_t *xbt_graph_shortest_paths(xbt_graph_t g)
393 unsigned long i, j, k;
399 # define P(u,v) p[(u)*n+(v)]
400 # define R(u,v) r[(u)*n+(v)]
402 n = xbt_dynar_length(g->nodes);
403 adj = xbt_graph_get_length_matrix(g);
404 d = xbt_new0(double, n * n);
405 p = xbt_new0(xbt_node_t, n * n);
406 r = xbt_new0(xbt_node_t, n * n);
408 xbt_floyd_algorithm(g, adj, d, p);
410 for (i = 0; i < n; i++) {
411 for (j = 0; j < n; j++) {
414 while ((P(i, k)) && (__xbt_find_in_dynar(g->nodes, P(i, k)) != i)) {
415 k = __xbt_find_in_dynar(g->nodes, P(i, k));
419 R(i, j) = *((xbt_node_t *) xbt_dynar_get_ptr(g->nodes, k));
432 /** @brief Extract a spanning tree of the given graph */
433 xbt_edge_t *xbt_graph_spanning_tree_prim(xbt_graph_t g)
436 int tree_size_max = xbt_dynar_length(g->nodes) - 1;
437 xbt_edge_t *tree = xbt_new0(xbt_edge_t, tree_size_max);
439 xbt_node_t node = NULL;
440 xbt_dynar_t edge_list = NULL;
441 xbt_heap_t heap = xbt_heap_new(10, NULL);
444 xbt_assert0(!(g->directed),
445 "Spanning trees do not make sense on directed graphs");
447 xbt_dynar_foreach(g->nodes, cursor, node) {
448 node->xbtdata = NULL;
451 node = xbt_dynar_getfirst_as(g->nodes, xbt_node_t);
452 node->xbtdata = (void *) 1;
453 edge_list = node->out;
454 xbt_dynar_foreach(edge_list, cursor, e)
455 xbt_heap_push(heap, e, -(e->length));
457 while ((edge = xbt_heap_pop(heap))) {
458 if ((edge->src->xbtdata) && (edge->dst->xbtdata))
460 tree[tree_size++] = edge;
461 if (!(edge->src->xbtdata)) {
462 edge->src->xbtdata = (void *) 1;
463 edge_list = edge->src->out;
464 xbt_dynar_foreach(edge_list, cursor, e) {
465 xbt_heap_push(heap, e, -(e->length));
468 edge->dst->xbtdata = (void *) 1;
469 edge_list = edge->dst->out;
470 xbt_dynar_foreach(edge_list, cursor, e) {
471 xbt_heap_push(heap, e, -(e->length));
474 if (tree_size == tree_size_max)
483 /** @brief Topological sort on the given graph
487 * In graph theory, a topological sort of a directed acyclic graph (DAG) is
488 * a linear ordering of its nodes which is compatible with the partial
489 * order R induced on the nodes where x comes before y (xRy) if there's a
490 * directed path from x to y in the DAG. An equivalent definition is that
491 * each node comes before all nodes to which it has edges. Every DAG has at
492 * least one topological sort, and may have many.
494 xbt_node_t *xbt_graph_topo_sort(xbt_graph_t g)
503 n = xbt_dynar_length(g->nodes);
506 sorted = xbt_malloc(n * sizeof(xbt_node_t));
508 xbt_dynar_foreach(g->nodes, cursor, node)
509 node->xbtdata = xbt_new0(int, 1);
511 xbt_dynar_foreach(g->nodes, cursor, node)
512 xbt_graph_depth_visit(g, node, sorted, &idx);
514 xbt_dynar_foreach(g->nodes, cursor, node) {
516 node->xbtdata = NULL;
522 /** @brief First-depth graph traversal */
523 void xbt_graph_depth_visit(xbt_graph_t g, xbt_node_t n,
524 xbt_node_t * sorted, int *idx)
529 if (*((int *) (n->xbtdata)) == ALREADY_EXPLORED)
531 else if (*((int *) (n->xbtdata)) == CURRENTLY_EXPLORING)
532 THROW0(0, 0, "There is a cycle");
534 *((int *) (n->xbtdata)) = CURRENTLY_EXPLORING;
536 xbt_dynar_foreach(n->out, cursor, edge) {
537 xbt_graph_depth_visit(g, edge->dst, sorted, idx);
540 *((int *) (n->xbtdata)) = ALREADY_EXPLORED;
541 sorted[(*idx)--] = n;
545 /********************* Import and Export ******************/
546 static xbt_graph_t parsed_graph = NULL;
547 static xbt_dict_t parsed_nodes = NULL;
549 static void *(*__parse_node_label_and_data) (xbt_node_t, const char *,
550 const char *) = NULL;
551 static void *(*__parse_edge_label_and_data) (xbt_edge_t, const char *,
552 const char *) = NULL;
554 static void __parse_graph_begin(void)
557 if (A_graphxml_graph_isDirected == A_graphxml_graph_isDirected_true)
558 parsed_graph = xbt_graph_new_graph(1, NULL);
560 parsed_graph = xbt_graph_new_graph(0, NULL);
562 parsed_nodes = xbt_dict_new();
565 static void __parse_graph_end(void)
567 xbt_dict_free(&parsed_nodes);
571 static void __parse_node(void)
573 xbt_node_t node = xbt_graph_new_node(parsed_graph, NULL);
575 DEBUG1("<node name=\"%s\"/>", A_graphxml_node_name);
576 if (__parse_node_label_and_data)
577 node->data = __parse_node_label_and_data(node, A_graphxml_node_label,
578 A_graphxml_node_data);
579 xbt_graph_parse_get_double(&(node->position_x), A_graphxml_node_position_x);
580 xbt_graph_parse_get_double(&(node->position_y), A_graphxml_node_position_y);
582 xbt_dict_set(parsed_nodes, A_graphxml_node_name, (void *) node, NULL);
585 static void __parse_edge(void)
587 xbt_edge_t edge = xbt_graph_new_edge(parsed_graph,
588 xbt_dict_get(parsed_nodes,
589 A_graphxml_edge_source),
590 xbt_dict_get(parsed_nodes,
591 A_graphxml_edge_target),
594 if (__parse_edge_label_and_data)
595 edge->data = __parse_edge_label_and_data(edge, A_graphxml_edge_label,
596 A_graphxml_edge_data);
598 xbt_graph_parse_get_double(&(edge->length), A_graphxml_edge_length);
600 DEBUG3("<edge source=\"%s\" target=\"%s\" length=\"%f\"/>",
601 (char *) (edge->src)->data,
602 (char *) (edge->dst)->data, xbt_graph_edge_get_length(edge));
605 /** @brief Import a graph from a file following the GraphXML format */
606 xbt_graph_t xbt_graph_read(const char *filename,
607 void *(*node_label_and_data) (xbt_node_t,
610 void *(*edge_label_and_data) (xbt_edge_t,
615 xbt_graph_t graph = NULL;
617 __parse_node_label_and_data = node_label_and_data;
618 __parse_edge_label_and_data = edge_label_and_data;
620 xbt_graph_parse_reset_parser();
622 STag_graphxml_graph_fun = __parse_graph_begin;
623 ETag_graphxml_graph_fun = __parse_graph_end;
624 ETag_graphxml_node_fun = __parse_node;
625 ETag_graphxml_edge_fun = __parse_edge;
627 xbt_graph_parse_open(filename);
628 xbt_assert1((!(*xbt_graph_parse) ()), "Parse error in %s", filename);
629 xbt_graph_parse_close();
631 graph = parsed_graph;
637 /** @brief Export the given graph in the GraphViz formatting for visualization */
638 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename,
639 const char *(node_name) (xbt_node_t),
640 const char *(edge_name) (xbt_edge_t))
642 unsigned int cursor = 0;
643 xbt_node_t node = NULL;
644 xbt_edge_t edge = NULL;
646 const char *name = NULL;
648 file = fopen(filename, "w");
649 xbt_assert1(file, "Failed to open %s \n", filename);
652 fprintf(file, "digraph test {\n");
654 fprintf(file, "graph test {\n");
656 fprintf(file, " graph [overlap=scale]\n");
658 fprintf(file, " node [shape=box, style=filled]\n");
660 " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
662 xbt_dynar_foreach(g->nodes, cursor, node) {
663 fprintf(file, " \"%p\" ", node);
664 if ((node_name) && ((name = node_name(node))))
665 fprintf(file, "[label=\"%s\"]", name);
666 fprintf(file, ";\n");
668 xbt_dynar_foreach(g->edges, cursor, edge) {
670 fprintf(file, " \"%p\" -> \"%p\"", edge->src, edge->dst);
672 fprintf(file, " \"%p\" -- \"%p\"", edge->src, edge->dst);
673 if ((edge_name) && ((name = edge_name(edge))))
674 fprintf(file, "[label=\"%s\"]", name);
675 fprintf(file, ";\n");
677 fprintf(file, "}\n");
681 /** @brief Export the given graph in the GraphXML format */
682 void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
683 const char *(node_name) (xbt_node_t),
684 const char *(edge_name) (xbt_edge_t),
685 const char *(node_data_print) (void *),
686 const char *(edge_data_print) (void *))
688 unsigned int cursor = 0;
689 xbt_node_t node = NULL;
690 xbt_edge_t edge = NULL;
692 const char *name = NULL;
694 file = fopen(filename, "w");
695 xbt_assert1(file, "Failed to open %s \n", filename);
697 fprintf(file, "<?xml version='1.0'?>\n");
698 fprintf(file, "<!DOCTYPE graph SYSTEM \"graphxml.dtd\">\n");
700 fprintf(file, "<graph isDirected=\"true\">\n");
702 fprintf(file, "<graph isDirected=\"false\">\n");
703 xbt_dynar_foreach(g->nodes, cursor, node) {
704 fprintf(file, " <node name=\"%p\" ", node);
705 if ((node_name) && ((name = node_name(node))))
706 fprintf(file, "label=\"%s\" ", name);
707 if ((node_data_print) && ((name = node_data_print(node->data))))
708 fprintf(file, "data=\"%s\" ", name);
709 fprintf(file, ">\n");
711 xbt_dynar_foreach(g->edges, cursor, edge) {
712 fprintf(file, " <edge source=\"%p\" target =\"%p\" ",
713 edge->src, edge->dst);
714 if ((edge_name) && ((name = edge_name(edge))))
715 fprintf(file, "label=\"%s\" ", name);
716 if (edge->length >= 0.0)
717 fprintf(file, "length=\"%g\" ", edge->length);
718 if ((edge_data_print) && ((name = edge_data_print(edge->data))))
719 fprintf(file, "data=\"%s\" ", name);
720 fprintf(file, ">\n");
722 fprintf(file, "</graph>\n");