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(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);
48 node->in = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
49 node->out = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
50 node->position_x = -1.0;
51 node->position_y = -1.0;
53 xbt_dynar_push(g->nodes, &node);
58 /** @brief add an edge to the given graph */
59 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g,
60 xbt_node_t src, xbt_node_t dst, void *data)
62 xbt_edge_t edge = NULL;
65 edge = xbt_new0(struct xbt_edge, 1);
66 xbt_dynar_push(src->out, &edge);
68 xbt_dynar_push(dst->in, &edge);
69 else /* only the "out" field is used */
70 xbt_dynar_push(dst->out, &edge);
77 xbt_dynar_push(src->in, &edge);
78 xbt_dynar_push(dst->out, &edge);
82 xbt_dynar_push(g->edges, &edge);
87 void *xbt_graph_node_get_data(xbt_node_t node)
92 void *xbt_graph_edge_get_data(xbt_edge_t edge)
98 * @param l: poor victim
100 * Free the graph structure.
102 void xbt_graph_free_graph(xbt_graph_t g,
103 void node_free_function(void *ptr),
104 void edge_free_function(void *ptr),
105 void graph_free_function(void *ptr))
108 xbt_node_t node = NULL;
109 xbt_edge_t edge = NULL;
112 xbt_dynar_foreach(g->nodes, cursor, node)
114 xbt_dynar_free(&(node->out));
115 xbt_dynar_free(&(node->in));
116 if(node_free_function)
117 node_free_function(node->data);
120 xbt_dynar_foreach(g->edges, cursor, edge)
122 if(edge_free_function)
123 edge_free_function(edge->data);
126 xbt_dynar_foreach(g->nodes, cursor, node)
128 xbt_dynar_free(&(g->nodes));
130 xbt_dynar_foreach(g->edges, cursor, edge)
132 xbt_dynar_free(&(g->edges));
140 /** @brief remove the given node from the given graph */
141 void xbt_graph_free_node(xbt_graph_t g, xbt_node_t n,
142 void_f_pvoid_t * node_free_function,
143 void_f_pvoid_t * edge_free_function)
148 xbt_node_t node = NULL;
149 xbt_edge_t edge = NULL;
151 nbr = xbt_dynar_length(g->edges);
153 for (i = 0; i < nbr; i++)
155 xbt_dynar_cursor_get(g->edges, &cursor, &edge);
157 if ((edge->dst == n) || (edge->src == n))
159 xbt_graph_free_edge(g, edge, edge_free_function);
161 else xbt_dynar_cursor_step( g->edges, &cursor);
164 if ((node_free_function) && (n->data))
165 node_free_function(n->data);
168 xbt_dynar_foreach(g->nodes, cursor, node)
170 xbt_dynar_cursor_rm(g->nodes, &cursor);
172 xbt_dynar_free(&(n->in));
173 xbt_dynar_free(&(n->out));
180 /** @brief remove the given edge from the given graph */
181 void xbt_graph_free_edge(xbt_graph_t g, xbt_edge_t e,
182 void free_function(void *ptr))
186 xbt_edge_t edge = NULL;
188 if ((free_function) && (e->data))
189 free_function(e->data);
191 xbt_dynar_foreach(g->edges, cursor, edge)
196 idx = __xbt_find_in_dynar(edge->dst->in,edge);
197 xbt_dynar_remove_at(edge->dst->in, idx,NULL);
198 } else { /* only the out field is used */
199 idx = __xbt_find_in_dynar(edge->dst->out,edge);
200 xbt_dynar_remove_at(edge->dst->out, idx,NULL);
203 idx = __xbt_find_in_dynar(edge->src->out,edge);
204 xbt_dynar_remove_at(edge->src->out,idx,NULL);
206 xbt_dynar_cursor_rm(g->edges, &cursor);
213 int __xbt_find_in_dynar(xbt_dynar_t dynar, void *p)
219 xbt_dynar_foreach(dynar, cursor, tmp)
227 /** @brief Retrieve the graph's nodes as a dynar */
228 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
233 /** @brief Retrieve the graph's edges as a dynar */
234 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
239 /** @brief Retrieve the node at the source of the given edge */
240 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
246 /** @brief Retrieve the node being the target of the given edge */
247 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
253 /** @brief Set the weight of the given edge */
254 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
260 double xbt_graph_edge_get_length(xbt_edge_t e)
266 /** @brief construct the adjacency matrix corresponding to the given graph
268 * The weights are the distances between nodes
270 double *xbt_graph_get_length_matrix(xbt_graph_t g)
276 xbt_edge_t edge = NULL;
277 xbt_node_t node=NULL;
280 # define D(u,v) d[(u)*n+(v)]
281 n = xbt_dynar_length(g->nodes);
283 d = (double *) xbt_new0(double, n*n);
285 for (i = 0; i < n * n; i++)
290 xbt_dynar_foreach(g->nodes, cursor, node)
293 D(cursor, cursor) = 0;
295 xbt_dynar_foreach(node->out, in_cursor, edge)
298 idx= __xbt_find_in_dynar(g->nodes, edge->src);
299 else /*case of undirected graphs*/
300 idx = __xbt_find_in_dynar(g->nodes, edge->dst);
301 D( cursor,idx) = edge->length;
310 /** @brief Floyd-Warshall algorithm for shortest path finding
314 * The Floyd–Warshall algorithm takes as input an adjacency matrix
315 * representation of a weighted, directed graph (V, E). The weight of a
316 * path between two vertices is the sum of the weights of the edges along
317 * that path. The edges E of the graph may have negative weights, but the
318 * graph must not have any negative weight cycles. The algorithm computes,
319 * for each pair of vertices, the minimum weight among all paths between
320 * the two vertices. The running time complexity is Θ(|V|3).
322 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
327 n = xbt_dynar_length(g->nodes);
329 # define D(u,v) d[(u)*n+(v)]
330 # define P(u,v) p[(u)*n+(v)]
332 for (i = 0; i < n * n; i++)
338 for (i = 0; i < n; i++)
340 for (j = 0; j < n; j++)
344 P(i,j) =*((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, i));
349 for (k = 0; k < n; k++)
351 for (i = 0; i < n; i++)
353 for (j = 0; j < n; j++)
355 if ((D(i, k) != -1) && (D(k, j) != -1))
357 if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j)))
359 D(i, j) = D(i, k) + D(k, j);
373 /** @brief computes all-pairs shortest paths */
374 xbt_node_t *xbt_graph_shortest_paths(xbt_graph_t g)
384 # define P(u,v) p[(u)*n+(v)]
385 # define R(u,v) r[(u)*n+(v)]
387 n = xbt_dynar_length(g->nodes);
388 adj = xbt_graph_get_length_matrix(g);
389 d = xbt_new0(double,n*n);
390 p = xbt_new0(xbt_node_t,n*n);
391 r = xbt_new0(xbt_node_t,n*n);
393 xbt_floyd_algorithm(g, adj, d, p);
395 for (i = 0; i < n; i++)
397 for (j = 0; j < n; j++)
401 while ((P(i, k)) && (__xbt_find_in_dynar(g->nodes, P(i, k)) != i))
403 k = __xbt_find_in_dynar(g->nodes, P(i, k));
408 R(i, j) = *((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, k));
421 /** @brief Extract a spanning tree of the given graph */
422 xbt_edge_t* xbt_graph_spanning_tree_prim(xbt_graph_t g)
425 int tree_size_max=xbt_dynar_length(g->nodes)-1;
426 xbt_edge_t *tree = xbt_new0(xbt_edge_t,tree_size_max);
428 xbt_node_t node = NULL;
429 xbt_dynar_t edge_list = NULL;
430 xbt_heap_t heap = xbt_heap_new(10,NULL);
433 xbt_assert0(!(g->directed),
434 "Spanning trees do not make sense on directed graphs");
436 xbt_dynar_foreach(g->nodes, cursor, node) {
437 node->xbtdata = NULL;
440 node = xbt_dynar_getfirst_as(g->nodes,xbt_node_t);
441 node->xbtdata = (void*) 1;
442 edge_list = node->out;
443 xbt_dynar_foreach(edge_list, cursor, e)
444 xbt_heap_push(heap,e, -(e->length));
446 while((edge=xbt_heap_pop(heap))) {
447 if((edge->src->xbtdata) && (edge->dst->xbtdata)) continue;
448 tree[tree_size++]=edge;
449 if(!(edge->src->xbtdata)) {
450 edge->src->xbtdata = (void*) 1;
451 edge_list = edge->src->out;
452 xbt_dynar_foreach(edge_list, cursor, e) {
453 xbt_heap_push(heap,e, -(e->length));
456 edge->dst->xbtdata = (void*) 1;
457 edge_list = edge->dst->out;
458 xbt_dynar_foreach(edge_list, cursor, e) {
459 xbt_heap_push(heap,e, -(e->length));
462 if(tree_size==tree_size_max) break;
470 /** @brief Topological sort on the given graph
474 * In graph theory, a topological sort of a directed acyclic graph (DAG) is
475 * a linear ordering of its nodes which is compatible with the partial
476 * order R induced on the nodes where x comes before y (xRy) if there's a
477 * directed path from x to y in the DAG. An equivalent definition is that
478 * each node comes before all nodes to which it has edges. Every DAG has at
479 * least one topological sort, and may have many.
481 xbt_node_t* xbt_graph_topo_sort(xbt_graph_t g)
489 n= xbt_dynar_length(g->nodes);
492 sorted=xbt_malloc(n*sizeof(xbt_node_t));
494 xbt_dynar_foreach(g->nodes,cursor , node)
495 node->xbtdata=xbt_new0(int,1);
497 xbt_dynar_foreach(g->nodes,cursor , node)
498 xbt_graph_depth_visit(g,node,sorted,&idx);
500 xbt_dynar_foreach(g->nodes, cursor, node) {
502 node->xbtdata = NULL;
508 /** @brief First-depth graph traversal */
509 void xbt_graph_depth_visit(xbt_graph_t g,xbt_node_t n,xbt_node_t* sorted,int* idx )
514 if (*((int*)(n->xbtdata))==ALREADY_EXPLORED)
516 else if (*((int*)(n->xbtdata))==CURRENTLY_EXPLORING)
517 THROW0(0,0,"There is a cycle");
520 *((int*)(n->xbtdata))=CURRENTLY_EXPLORING;
522 xbt_dynar_foreach(n->out,cursor, edge)
524 xbt_graph_depth_visit(g,edge->dst,sorted,idx);
527 *((int*)(n->xbtdata))=ALREADY_EXPLORED;
532 /********************* Import and Export ******************/
533 static xbt_graph_t parsed_graph = NULL;
534 static xbt_dict_t parsed_nodes = NULL;
536 static void *(*__parse_node_label_and_data)(xbt_node_t, const char*, const char*) = NULL;
537 static void *(*__parse_edge_label_and_data)(xbt_edge_t, const char*, const char*) = NULL;
539 static void __parse_graph_begin(void)
542 if(A_graphxml_graph_isDirected == A_graphxml_graph_isDirected_true)
543 parsed_graph = xbt_graph_new_graph(1, NULL);
544 else parsed_graph = xbt_graph_new_graph(0, NULL);
546 parsed_nodes = xbt_dict_new();
549 static void __parse_graph_end(void)
551 xbt_dict_free(&parsed_nodes);
555 static void __parse_node(void)
558 xbt_graph_new_node(parsed_graph, NULL);
560 DEBUG1("<node name=\"%s\"/>", A_graphxml_node_name);
561 if(__parse_node_label_and_data)
562 node->data = __parse_node_label_and_data(node,A_graphxml_node_label,
563 A_graphxml_node_data);
564 xbt_graph_parse_get_double(&(node->position_x),A_graphxml_node_position_x);
565 xbt_graph_parse_get_double(&(node->position_y),A_graphxml_node_position_y);
567 xbt_dict_set(parsed_nodes, A_graphxml_node_name, (void *) node, NULL);
570 static void __parse_edge(void)
573 xbt_graph_new_edge(parsed_graph,
574 xbt_dict_get(parsed_nodes,A_graphxml_edge_source),
575 xbt_dict_get(parsed_nodes,A_graphxml_edge_target),
578 if(__parse_edge_label_and_data)
579 edge->data = __parse_edge_label_and_data(edge,A_graphxml_edge_label,
580 A_graphxml_edge_data);
582 xbt_graph_parse_get_double(&(edge->length),A_graphxml_edge_length);
584 DEBUG3("<edge source=\"%s\" target=\"%s\" length=\"%f\"/>",
585 (char *) (edge->src)->data,
586 (char *) (edge->dst)->data,
587 xbt_graph_edge_get_length(edge));
590 /** @brief Import a graph from a file following the GraphXML format */
591 xbt_graph_t xbt_graph_read(const char *filename,
592 void *(node_label_and_data)(xbt_node_t, const char*, const char*),
593 void *(edge_label_and_data)(xbt_edge_t, const char*, const char*))
596 xbt_graph_t graph = NULL;
598 __parse_node_label_and_data = node_label_and_data;
599 __parse_edge_label_and_data = edge_label_and_data;
601 xbt_graph_parse_reset_parser();
603 STag_graphxml_graph_fun = __parse_graph_begin;
604 ETag_graphxml_graph_fun = __parse_graph_end;
605 ETag_graphxml_node_fun = __parse_node;
606 ETag_graphxml_edge_fun = __parse_edge;
608 xbt_graph_parse_open(filename);
609 xbt_assert1((!xbt_graph_parse()), "Parse error in %s", filename);
610 xbt_graph_parse_close();
612 graph = parsed_graph;
618 /** @brief Export the given graph in the GraphViz formatting for visualization */
619 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename,
620 const char *(node_name) (xbt_node_t),
621 const char *(edge_name) (xbt_edge_t))
624 xbt_node_t node = NULL;
625 xbt_edge_t edge = NULL;
627 const char *name=NULL;
629 file=fopen(filename,"w");
630 xbt_assert1(file, "Failed to open %s \n",filename);
632 if(g->directed) fprintf(file,"digraph test {\n");
633 else fprintf(file,"graph test {\n");
635 fprintf(file," graph [overlap=scale]\n");
637 fprintf(file," node [shape=box, style=filled]\n");
638 fprintf(file," node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
640 xbt_dynar_foreach(g->nodes, cursor, node) {
641 fprintf(file," \"%p\" ", node);
642 if((node_name)&&((name=node_name(node)))) fprintf(file,"[label=\"%s\"]",name);
645 xbt_dynar_foreach(g->edges, cursor, edge) {
647 fprintf(file," \"%p\" -> \"%p\"",edge->src, edge->dst);
649 fprintf(file," \"%p\" -- \"%p\"",edge->src, edge->dst);
650 if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"[label=\"%s\"]",name);
657 /** @brief Export the given graph in the GraphXML format */
658 void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
659 const char *(node_name)(xbt_node_t),
660 const char *(edge_name)(xbt_edge_t),
661 const char *(node_data_print)(void *),
662 const char *(edge_data_print)(void *))
665 xbt_node_t node = NULL;
666 xbt_edge_t edge = NULL;
668 const char *name = NULL;
670 file=fopen(filename,"w");
671 xbt_assert1(file, "Failed to open %s \n",filename);
673 fprintf(file,"<?xml version='1.0'?>\n");
674 fprintf(file,"<!DOCTYPE graph SYSTEM \"graphxml.dtd\">\n");
675 if(g->directed) fprintf(file,"<graph isDirected=\"true\">\n");
676 else fprintf(file,"<graph isDirected=\"false\">\n");
677 xbt_dynar_foreach(g->nodes, cursor, node) {
678 fprintf(file," <node name=\"%p\" ", node);
679 if((node_name)&&((name=node_name(node)))) fprintf(file,"label=\"%s\" ",name);
680 if((node_data_print)&&((name=node_data_print(node->data))))
681 fprintf(file,"data=\"%s\" ",name);
684 xbt_dynar_foreach(g->edges, cursor, edge) {
685 fprintf(file," <edge source=\"%p\" target =\"%p\" ",
686 edge->src, edge->dst );
687 if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"label=\"%s\" ",name);
688 if(edge->length>=0.0) fprintf(file,"length=\"%g\" ",edge->length);
689 if((edge_data_print)&&((name=edge_data_print(edge->data))))
690 fprintf(file,"data=\"%s\" ",name);
693 fprintf(file,"</graph>\n");