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 node->position_x = -1.0;
47 node->position_y = -1.0;
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;
60 edge = xbt_new0(struct xbt_edge, 1);
61 xbt_dynar_push(src->out, &edge);
63 xbt_dynar_push(dst->in, &edge);
64 else /* only the "out" field is used */
65 xbt_dynar_push(dst->out, &edge);
71 xbt_dynar_push(g->edges, &edge);
78 * \param l poor victim
80 * Free the graph structure.
82 void xbt_graph_free_graph(xbt_graph_t g,
83 void node_free_function(void *ptr),
84 void edge_free_function(void *ptr),
85 void graph_free_function(void *ptr))
88 xbt_node_t node = NULL;
89 xbt_edge_t edge = NULL;
92 xbt_dynar_foreach(g->nodes, cursor, node)
94 xbt_dynar_free(&(node->out));
95 xbt_dynar_free(&(node->in));
96 if(node_free_function)
97 node_free_function(node->data);
100 xbt_dynar_foreach(g->edges, cursor, edge)
102 if(edge_free_function)
103 edge_free_function(edge->data);
106 xbt_dynar_foreach(g->nodes, cursor, node)
108 xbt_dynar_free(&(g->nodes));
110 xbt_dynar_foreach(g->edges, cursor, edge)
112 xbt_dynar_free(&(g->edges));
121 void xbt_graph_free_node(xbt_graph_t g, xbt_node_t n,
122 void_f_pvoid_t * node_free_function,
123 void_f_pvoid_t * edge_free_function)
128 xbt_node_t node = NULL;
129 xbt_edge_t edge = NULL;
131 nbr = xbt_dynar_length(g->edges);
133 for (i = 0; i < nbr; i++)
135 xbt_dynar_cursor_get(g->edges, &cursor, &edge);
137 if ((edge->dst == n) || (edge->src == n))
139 xbt_graph_free_edge(g, edge, edge_free_function);
141 else xbt_dynar_cursor_step( g->edges, &cursor);
145 if ((node_free_function) && (n->data))
146 node_free_function(n->data);
149 xbt_dynar_foreach(g->nodes, cursor, node)
152 xbt_dynar_cursor_rm(g->nodes, &cursor);
159 void xbt_graph_free_edge(xbt_graph_t g, xbt_edge_t e,
160 void free_function(void *ptr))
164 xbt_edge_t edge = NULL;
166 if ((free_function) && (e->data))
167 free_function(e->data);
169 xbt_dynar_foreach(g->edges, cursor, edge)
174 idx = __xbt_find_in_dynar(edge->dst->in,edge);
175 xbt_dynar_remove_at(edge->dst->in, idx,NULL);
176 } else { /* only the out field is used */
177 idx = __xbt_find_in_dynar(edge->dst->out,edge);
178 xbt_dynar_remove_at(edge->dst->out, idx,NULL);
181 idx = __xbt_find_in_dynar(edge->src->out,edge);
182 xbt_dynar_remove_at(edge->src->out,idx,NULL);
184 xbt_dynar_cursor_rm(g->edges, &cursor);
191 int __xbt_find_in_dynar(xbt_dynar_t dynar, void *p)
197 xbt_dynar_foreach(dynar, cursor, tmp)
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)
227 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
233 double xbt_graph_edge_get_length(xbt_edge_t e)
239 /*construct the adjacency matrix corresponding to a graph,
240 the weights are the distances between nodes
242 double *xbt_graph_get_length_matrix(xbt_graph_t g)
248 xbt_edge_t edge = NULL;
249 xbt_node_t node=NULL;
252 # define D(u,v) d[(u)*n+(v)]
253 n = xbt_dynar_length(g->nodes);
255 d = (double *) xbt_new0(double, n*n);
257 for (i = 0; i < n * n; i++)
262 xbt_dynar_foreach(g->nodes, cursor, node)
265 D(cursor, cursor) = 0;
267 xbt_dynar_foreach(node->out, in_cursor, edge)
270 idx= __xbt_find_in_dynar(g->nodes, edge->src);
271 else /*case of undirected graphs*/
272 idx = __xbt_find_in_dynar(g->nodes, edge->dst);
273 D( cursor,idx) = edge->length;
283 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
288 n = xbt_dynar_length(g->nodes);
290 # define D(u,v) d[(u)*n+(v)]
291 # define P(u,v) p[(u)*n+(v)]
293 for (i = 0; i < n * n; i++)
299 for (i = 0; i < n; i++)
301 for (j = 0; j < n; j++)
305 P(i,j) =*((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, i));
310 for (k = 0; k < n; k++)
312 for (i = 0; i < n; i++)
314 for (j = 0; j < n; j++)
316 if ((D(i, k) != -1) && (D(k, j) != -1))
318 if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j)))
320 D(i, j) = D(i, k) + D(k, j);
334 /*computes all-pairs shortest paths*/
335 xbt_node_t *xbt_graph_shortest_paths(xbt_graph_t g)
345 # define P(u,v) p[(u)*n+(v)]
346 # define R(u,v) r[(u)*n+(v)]
348 n = xbt_dynar_length(g->nodes);
349 adj = xbt_graph_get_length_matrix(g);
350 d = xbt_new0(double,n*n);
351 p = xbt_new0(xbt_node_t,n*n);
352 r = xbt_new0(xbt_node_t,n*n);
354 xbt_floyd_algorithm(g, adj, d, p);
356 for (i = 0; i < n; i++)
358 for (j = 0; j < n; j++)
362 while ((P(i, k)) && (__xbt_find_in_dynar(g->nodes, P(i, k)) != i))
364 k = __xbt_find_in_dynar(g->nodes, P(i, k));
369 R(i, j) = *((xbt_node_t*) xbt_dynar_get_ptr(g->nodes, k));
383 xbt_edge_t* xbt_graph_spanning_tree_prim(xbt_graph_t g)
386 int tree_size_max=xbt_dynar_length(g->nodes)-1;
387 xbt_edge_t *tree = xbt_new0(xbt_edge_t,tree_size_max);
389 xbt_node_t node = NULL;
390 xbt_dynar_t edge_list = NULL;
391 xbt_heap_t heap = xbt_heap_new(10,NULL);
394 xbt_assert0(!(g->directed),
395 "Spanning trees do not make sense on directed graphs");
397 node = xbt_dynar_getfirst_as(g->nodes,xbt_node_t);
398 node->xbtdata = (void*) 1;
399 edge_list = node->out;
400 xbt_dynar_foreach(edge_list, cursor, e)
401 xbt_heap_push(heap,e, -(e->length));
403 while((edge=xbt_heap_pop(heap))) {
404 if((edge->src->xbtdata) && (edge->dst->xbtdata)) continue;
405 tree[tree_size++]=edge;
406 if(!(edge->src->xbtdata)) {
407 edge->src->xbtdata = (void*) 1;
408 edge_list = edge->src->out;
409 xbt_dynar_foreach(edge_list, cursor, e) {
410 xbt_heap_push(heap,e, -(e->length));
413 edge->dst->xbtdata = (void*) 1;
414 edge_list = edge->dst->out;
415 xbt_dynar_foreach(edge_list, cursor, e) {
416 xbt_heap_push(heap,e, -(e->length));
419 if(tree_size==tree_size_max) break;
424 xbt_dynar_foreach(g->nodes, cursor, node) {
425 node->xbtdata = NULL;
430 /********************* Import and Export ******************/
431 static xbt_graph_t parsed_graph = NULL;
432 static xbt_dict_t parsed_nodes = NULL;
434 static void *(*__parse_node_label_and_data)(xbt_node_t, const char*, const char*) = NULL;
435 static void *(*__parse_edge_label_and_data)(xbt_edge_t, const char*, const char*) = NULL;
437 static void __parse_graph_begin(void)
441 if(A_graphxml_graph_isDirected == A_graphxml_graph_isDirected_true)
442 parsed_graph = xbt_graph_new_graph(1, NULL);
443 else parsed_graph = xbt_graph_new_graph(0, NULL);
445 parsed_nodes = xbt_dict_new();
447 static void __parse_graph_end(void)
449 xbt_dict_free(&parsed_nodes);
453 static void __parse_node(void)
456 xbt_graph_new_node(parsed_graph, NULL);
458 DEBUG1("<node name=\"%s\"/>", A_graphxml_node_name);
459 if(__parse_node_label_and_data)
460 node->data = __parse_node_label_and_data(node,A_graphxml_node_label,
461 A_graphxml_node_data);
462 xbt_graph_parse_get_double(&(node->position_x),A_graphxml_node_position_x);
463 xbt_graph_parse_get_double(&(node->position_y),A_graphxml_node_position_y);
465 xbt_dict_set(parsed_nodes, A_graphxml_node_name, (void *) node, NULL);
468 static void __parse_edge(void)
471 xbt_graph_new_edge(parsed_graph,
472 xbt_dict_get(parsed_nodes,A_graphxml_edge_source),
473 xbt_dict_get(parsed_nodes,A_graphxml_edge_target),
476 if(__parse_edge_label_and_data)
477 edge->data = __parse_edge_label_and_data(edge,A_graphxml_edge_label,
478 A_graphxml_edge_data);
480 xbt_graph_parse_get_double(&(edge->length),A_graphxml_edge_length);
482 DEBUG4("<edge name=\"%s\" source=\"%s\" target=\"%s\" length=\"%f\"/>",
484 (char *) (edge->src)->data,
485 (char *) (edge->dst)->data,
486 xbt_graph_edge_get_length(edge));
489 xbt_graph_t xbt_graph_read(const char *filename,
490 void *(node_label_and_data)(xbt_node_t, const char*, const char*),
491 void *(edge_label_and_data)(xbt_edge_t, const char*, const char*))
494 xbt_graph_t graph = NULL;
496 __parse_node_label_and_data = node_label_and_data;
497 __parse_edge_label_and_data = edge_label_and_data;
499 xbt_graph_parse_reset_parser();
501 STag_graphxml_graph_fun = __parse_graph_begin;
502 ETag_graphxml_graph_fun = __parse_graph_end;
503 ETag_graphxml_node_fun = __parse_node;
504 ETag_graphxml_edge_fun = __parse_edge;
506 xbt_graph_parse_open(filename);
507 xbt_assert1((!xbt_graph_parse()), "Parse error in %s", filename);
508 xbt_graph_parse_close();
510 graph = parsed_graph;
516 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename,
517 const char *(node_name) (xbt_node_t),
518 const char *(edge_name) (xbt_edge_t))
521 xbt_node_t node = NULL;
522 xbt_edge_t edge = NULL;
524 const char *name=NULL;
526 file=fopen(filename,"w");
527 xbt_assert1(file, "Failed to open %s \n",filename);
529 if(g->directed) fprintf(file,"digraph test {\n");
530 else fprintf(file,"graph test {\n");
532 fprintf(file," graph [overlap=scale]\n");
534 fprintf(file," node [shape=box, style=filled]\n");
535 fprintf(file," node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
537 xbt_dynar_foreach(g->nodes, cursor, node) {
538 fprintf(file," \"%p\" ", node);
539 if((node_name)&&((name=node_name(node)))) fprintf(file,"[label=\"%s\"]",name);
542 xbt_dynar_foreach(g->edges, cursor, edge) {
544 fprintf(file," \"%p\" -> \"%p\"",edge->src, edge->dst);
546 fprintf(file," \"%p\" -- \"%p\"",edge->src, edge->dst);
547 if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"[label=\"%s\"]",name);
554 void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
555 const char *(node_name)(xbt_node_t),
556 const char *(edge_name)(xbt_edge_t),
557 const char *(node_data_print)(void *),
558 const char *(edge_data_print)(void *))
561 xbt_node_t node = NULL;
562 xbt_edge_t edge = NULL;
564 const char *name = NULL;
566 file=fopen(filename,"w");
567 xbt_assert1(file, "Failed to open %s \n",filename);
569 fprintf(file,"<?xml version='1.0'?>\n");
570 fprintf(file,"<!DOCTYPE graph SYSTEM \"graphxml.dtd\">\n");
571 if(g->directed) fprintf(file,"<graph isDirected=\"true\">\n");
572 else fprintf(file,"<graph isDirected=\"false\">\n");
573 xbt_dynar_foreach(g->nodes, cursor, node) {
574 fprintf(file," <node name=\"%p\" ", node);
575 if((node_name)&&((name=node_name(node)))) fprintf(file,"label=\"%s\" ",name);
576 if((node_data_print)&&((name=node_data_print(node->data))))
577 fprintf(file,"data=\"%s\" ",name);
580 xbt_dynar_foreach(g->edges, cursor, edge) {
581 fprintf(file," <edge source=\"%p\" target =\"%p\" ",
582 edge->src, edge->dst );
583 if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"label=\"%s\" ",name);
584 if(edge->length>=0.0) fprintf(file,"length=\"%g\" ",edge->length);
585 if((edge_data_print)&&((name=edge_data_print(edge->data))))
586 fprintf(file,"data=\"%s\" ",name);
589 fprintf(file,"</graph>\n");