1 /* a generic graph library. */
3 /* Copyright (c) 2006-2014. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/sysdep.h"
11 #include "xbt/graph.h"
12 #include "graph_private.h"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_graph, xbt, "Graph");
26 /** @brief Constructor
29 xbt_graph_t xbt_graph_new_graph(unsigned short int directed, void *data)
31 xbt_graph_t graph = NULL;
32 graph = xbt_new0(struct xbt_graph, 1);
33 graph->directed = directed;
35 graph->nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
36 graph->edges = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
41 /** @brief add a node to the given graph */
42 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
44 xbt_node_t node = NULL;
45 node = xbt_new0(struct xbt_node, 1);
48 /* only the "out" field is used */
49 node->in = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
51 node->out = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
52 node->position_x = -1.0;
53 node->position_y = -1.0;
55 xbt_dynar_push(g->nodes, &node);
60 /** @brief add an edge to the given graph */
61 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g, xbt_node_t src, xbt_node_t dst, void *data)
63 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);
76 xbt_dynar_push(g->edges, &edge);
81 /** @brief Get the edge connecting src and dst */
82 xbt_edge_t xbt_graph_get_edge(xbt_graph_t g, xbt_node_t src, xbt_node_t dst)
87 xbt_dynar_foreach(src->out, cursor, edge) {
88 XBT_DEBUG("%p = %p--%p", edge, edge->src, edge->dst);
89 if ((edge->src == src) && (edge->dst == dst))
93 xbt_dynar_foreach(src->out, cursor, edge) {
94 XBT_DEBUG("%p = %p--%p", edge, edge->src, edge->dst);
95 if ((edge->dst == src) && (edge->src == dst))
102 /** @brief Get the user data associated to a node */
103 void *xbt_graph_node_get_data(xbt_node_t node)
108 /** @brief Set the user data associated to a node */
109 void xbt_graph_node_set_data(xbt_node_t node, void *data)
114 /** @brief Get the user data associated to a edge */
115 void *xbt_graph_edge_get_data(xbt_edge_t edge)
120 /** @brief Set the user data associated to a edge */
121 void xbt_graph_edge_set_data(xbt_edge_t edge, void *data)
126 /** @brief Destructor
127 * @param g: poor victim
128 * @param node_free_function: function to use to free data associated to each node
129 * @param edge_free_function: function to use to free data associated to each edge
130 * @param graph_free_function: function to use to free data associated to g
132 * Free the graph structure.
134 void xbt_graph_free_graph(xbt_graph_t g,
135 void_f_pvoid_t node_free_function,
136 void_f_pvoid_t edge_free_function,
137 void_f_pvoid_t graph_free_function)
143 xbt_dynar_foreach(g->edges, cursor, edge) {
144 if (edge_free_function)
145 edge_free_function(edge->data);
148 xbt_dynar_free(&(g->edges));
150 xbt_dynar_foreach(g->nodes, cursor, node) {
151 xbt_dynar_free(&(node->out));
152 xbt_dynar_free(&(node->in));
153 if (node_free_function)
154 node_free_function(node->data);
157 xbt_dynar_free(&(g->nodes));
159 if (graph_free_function)
160 graph_free_function(g->data);
165 /** @brief Retrieve the graph's nodes as a dynar */
166 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
171 /** @brief Retrieve the graph's edges as a dynar */
172 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
177 /** @brief Retrieve the node at the source of the given edge */
178 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
183 /** @brief Retrieve the node being the target of the given edge */
184 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
189 /** @brief Retrieve the outgoing edges of the given node */
190 xbt_dynar_t xbt_graph_node_get_outedges(xbt_node_t n)
195 /** @brief Set the weight of the given edge */
196 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
202 /** @brief Get the length of a edge */
203 double xbt_graph_edge_get_length(xbt_edge_t edge)
208 /** @brief Floyd-Warshall algorithm for shortest path finding
212 * The Floyd–Warshall algorithm takes as input an adjacency matrix
213 * representation of a weighted, directed graph (V, E). The weight of a
214 * path between two vertices is the sum of the weights of the edges along
215 * that path. The edges E of the graph may have negative weights, but the
216 * graph must not have any negative weight cycles. The algorithm computes,
217 * for each pair of vertices, the minimum weight among all paths between
218 * the two vertices. The running time complexity is Θ(|V|3).
220 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
223 unsigned long i, j, k;
225 n = xbt_dynar_length(g->nodes);
227 # define D(u,v) d[(u)*n+(v)]
228 # define P(u,v) p[(u)*n+(v)]
230 for (i = 0; i < n * n; i++) {
235 for (i = 0; i < n; i++) {
236 for (j = 0; j < n; j++) {
238 P(i, j) = *((xbt_node_t *) xbt_dynar_get_ptr(g->nodes, i));
243 for (k = 0; k < n; k++) {
244 for (i = 0; i < n; i++) {
245 for (j = 0; j < n; j++) {
246 if ((D(i, k) != -1) && (D(k, j) != -1)) {
247 if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j))) {
248 D(i, j) = D(i, k) + D(k, j);
259 /** @brief Export the given graph in the GraphViz formatting for visualization */
260 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename,
261 const char *(node_name) (xbt_node_t),
262 const char *(edge_name) (xbt_edge_t))
264 unsigned int cursor = 0;
265 xbt_node_t node = NULL;
266 xbt_edge_t edge = NULL;
268 const char *name = NULL;
270 file = fopen(filename, "w");
271 xbt_assert(file, "Failed to open %s \n", filename);
274 fprintf(file, "digraph test {\n");
276 fprintf(file, "graph test {\n");
278 fprintf(file, " graph [overlap=scale]\n");
280 fprintf(file, " node [shape=box, style=filled]\n");
282 " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
284 xbt_dynar_foreach(g->nodes, cursor, node) {
286 fprintf(file, " \"%s\";\n", node_name(node));
288 fprintf(file, " \"%p\";\n", node);
291 xbt_dynar_foreach(g->edges, cursor, edge) {
293 const char *c_dir = "->";
294 const char *c_ndir = "--";
300 const char *src_name, *dst_name;
302 src_name = node_name(edge->src);
303 dst_name = node_name(edge->dst);
304 fprintf(file, " \"%s\" %s \"%s\"", src_name, c, dst_name);
306 fprintf(file, " \"%p\" %s \"%p\"", edge->src, c, edge->dst);
309 if ((edge_name) && ((name = edge_name(edge))))
310 fprintf(file, "[label=\"%s\"]", name);
311 fprintf(file, ";\n");
313 fprintf(file, "}\n");