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"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_graph, xbt, "Graph");
23 /** @brief Constructor
26 xbt_graph_t xbt_graph_new_graph(unsigned short int directed, void *data)
28 xbt_graph_t graph = NULL;
29 graph = xbt_new0(struct xbt_graph, 1);
30 graph->directed = directed;
32 graph->nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
33 graph->edges = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
38 /** @brief add a node to the given graph */
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);
45 /* only the "out" field is used */
46 node->in = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
48 node->out = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
49 node->position_x = -1.0;
50 node->position_y = -1.0;
52 xbt_dynar_push(g->nodes, &node);
57 /** @brief add an edge to the given graph */
58 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g, xbt_node_t src, xbt_node_t dst, void *data)
60 xbt_edge_t edge = NULL;
62 edge = xbt_new0(struct xbt_edge, 1);
63 xbt_dynar_push(src->out, &edge);
65 xbt_dynar_push(dst->in, &edge);
66 else /* only the "out" field is used */
67 xbt_dynar_push(dst->out, &edge);
73 xbt_dynar_push(g->edges, &edge);
78 /** @brief Get the edge connecting src and dst */
79 xbt_edge_t xbt_graph_get_edge(xbt_graph_t g, xbt_node_t src, xbt_node_t dst)
84 xbt_dynar_foreach(src->out, cursor, edge) {
85 XBT_DEBUG("%p = %p--%p", edge, edge->src, edge->dst);
86 if ((edge->src == src) && (edge->dst == dst))
90 xbt_dynar_foreach(src->out, cursor, edge) {
91 XBT_DEBUG("%p = %p--%p", edge, edge->src, edge->dst);
92 if ((edge->dst == src) && (edge->src == dst))
99 /** @brief Get the user data associated to a node */
100 void *xbt_graph_node_get_data(xbt_node_t node)
105 /** @brief Set the user data associated to a node */
106 void xbt_graph_node_set_data(xbt_node_t node, void *data)
111 /** @brief Get the user data associated to a edge */
112 void *xbt_graph_edge_get_data(xbt_edge_t edge)
117 /** @brief Set the user data associated to a 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, void_f_pvoid_t node_free_function, void_f_pvoid_t edge_free_function,
132 void_f_pvoid_t graph_free_function)
138 xbt_dynar_foreach(g->edges, cursor, edge) {
139 if (edge_free_function)
140 edge_free_function(edge->data);
143 xbt_dynar_free(&(g->edges));
145 xbt_dynar_foreach(g->nodes, cursor, node) {
146 xbt_dynar_free(&(node->out));
147 xbt_dynar_free(&(node->in));
148 if (node_free_function)
149 node_free_function(node->data);
152 xbt_dynar_free(&(g->nodes));
154 if (graph_free_function)
155 graph_free_function(g->data);
159 /** @brief Retrieve the graph's nodes as a dynar */
160 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
165 /** @brief Retrieve the graph's edges as a dynar */
166 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
171 /** @brief Retrieve the node at the source of the given edge */
172 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
177 /** @brief Retrieve the node being the target of the given edge */
178 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
183 /** @brief Retrieve the outgoing edges of the given node */
184 xbt_dynar_t xbt_graph_node_get_outedges(xbt_node_t n)
189 /** @brief Set the weight of the given edge */
190 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
196 /** @brief Get the length of a edge */
197 double xbt_graph_edge_get_length(xbt_edge_t edge)
202 /** @brief Floyd-Warshall algorithm for shortest path finding
206 * The Floyd-Warshall algorithm takes as input an adjacency matrix representation of a weighted, directed graph (V, E).
207 * The weight of a path between two vertices is the sum of the weights of the edges along that path. The edges E of the
208 * graph may have negative weights, but the graph must not have any negative weight cycles. The algorithm computes, for
209 * each pair of vertices, the minimum weight among all paths between the two vertices. The running time complexity is
212 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d, xbt_node_t * p)
214 unsigned long i, j, k;
216 n = xbt_dynar_length(g->nodes);
218 # define D(u,v) d[(u)*n+(v)]
219 # define P(u,v) p[(u)*n+(v)]
221 for (i = 0; i < n * n; i++) {
225 for (i = 0; i < n; i++) {
226 for (j = 0; j < n; j++) {
228 P(i, j) = *((xbt_node_t *) xbt_dynar_get_ptr(g->nodes, i));
233 for (k = 0; k < n; k++) {
234 for (i = 0; i < n; i++) {
235 for (j = 0; j < n; j++) {
236 if ((D(i, k) != -1) && (D(k, j) != -1)) {
237 if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j))) {
238 D(i, j) = D(i, k) + D(k, j);
249 /** @brief Export the given graph in the GraphViz formatting for visualization */
250 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename, const char *(node_name) (xbt_node_t),
251 const char *(edge_name) (xbt_edge_t))
253 unsigned int cursor = 0;
254 xbt_node_t node = NULL;
255 xbt_edge_t edge = NULL;
257 const char *name = NULL;
259 file = fopen(filename, "w");
260 xbt_assert(file, "Failed to open %s \n", filename);
263 fprintf(file, "digraph test {\n");
265 fprintf(file, "graph test {\n");
267 fprintf(file, " graph [overlap=scale]\n");
269 fprintf(file, " node [shape=box, style=filled]\n");
270 fprintf(file, " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
272 xbt_dynar_foreach(g->nodes, cursor, node) {
274 fprintf(file, " \"%s\";\n", node_name(node));
276 fprintf(file, " \"%p\";\n", node);
279 xbt_dynar_foreach(g->edges, cursor, edge) {
281 const char *c_dir = "->";
282 const char *c_ndir = "--";
288 const char *src_name, *dst_name;
290 src_name = node_name(edge->src);
291 dst_name = node_name(edge->dst);
292 fprintf(file, " \"%s\" %s \"%s\"", src_name, c, dst_name);
294 fprintf(file, " \"%p\" %s \"%p\"", edge->src, c, edge->dst);
297 if ((edge_name) && ((name = edge_name(edge))))
298 fprintf(file, "[label=\"%s\"]", name);
299 fprintf(file, ";\n");
301 fprintf(file, "}\n");