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 = xbt_new0(struct xbt_graph, 1);
29 graph->directed = directed;
31 graph->nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
32 graph->edges = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
37 /** @brief add a node to the given graph */
38 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
40 xbt_node_t node= xbt_new0(struct xbt_node, 1);
43 /* only the "out" field is used */
44 node->in = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
46 node->out = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
47 node->position_x = -1.0;
48 node->position_y = -1.0;
50 xbt_dynar_push(g->nodes, &node);
55 /** @brief add an edge to the given graph */
56 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g, xbt_node_t src, xbt_node_t dst, void *data)
58 xbt_edge_t edge = xbt_new0(struct xbt_edge, 1);
59 xbt_dynar_push(src->out, &edge);
61 xbt_dynar_push(dst->in, &edge);
62 else /* only the "out" field is used */
63 xbt_dynar_push(dst->out, &edge);
69 xbt_dynar_push(g->edges, &edge);
74 /** @brief Get the edge connecting src and dst */
75 xbt_edge_t xbt_graph_get_edge(xbt_graph_t g, xbt_node_t src, xbt_node_t dst)
80 xbt_dynar_foreach(src->out, cursor, edge) {
81 XBT_DEBUG("%p = %p--%p", edge, edge->src, edge->dst);
82 if ((edge->src == src) && (edge->dst == dst))
86 xbt_dynar_foreach(src->out, cursor, edge) {
87 XBT_DEBUG("%p = %p--%p", edge, edge->src, edge->dst);
88 if ((edge->dst == src) && (edge->src == dst))
95 /** @brief Get the user data associated to a node */
96 void *xbt_graph_node_get_data(xbt_node_t node)
101 /** @brief Set the user data associated to a node */
102 void xbt_graph_node_set_data(xbt_node_t node, void *data)
107 /** @brief Get the user data associated to a edge */
108 void *xbt_graph_edge_get_data(xbt_edge_t edge)
113 /** @brief Set the user data associated to a edge */
114 void xbt_graph_edge_set_data(xbt_edge_t edge, void *data)
119 /** @brief Destructor
120 * @param g: poor victim
121 * @param node_free_function: function to use to free data associated to each node
122 * @param edge_free_function: function to use to free data associated to each edge
123 * @param graph_free_function: function to use to free data associated to g
125 * Free the graph structure.
127 void xbt_graph_free_graph(xbt_graph_t g, void_f_pvoid_t node_free_function, void_f_pvoid_t edge_free_function,
128 void_f_pvoid_t graph_free_function)
134 xbt_dynar_foreach(g->edges, cursor, edge) {
135 if (edge_free_function)
136 edge_free_function(edge->data);
139 xbt_dynar_free(&(g->edges));
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_free(&(g->nodes));
150 if (graph_free_function)
151 graph_free_function(g->data);
155 /** @brief Retrieve the graph's nodes as a dynar */
156 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
161 /** @brief Retrieve the graph's edges as a dynar */
162 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
167 /** @brief Retrieve the node at the source of the given edge */
168 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
173 /** @brief Retrieve the node being the target of the given edge */
174 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
179 /** @brief Retrieve the outgoing edges of the given node */
180 xbt_dynar_t xbt_graph_node_get_outedges(xbt_node_t n)
185 /** @brief Set the weight of the given edge */
186 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
192 /** @brief Get the length of a edge */
193 double xbt_graph_edge_get_length(xbt_edge_t edge)
198 /** @brief Floyd-Warshall algorithm for shortest path finding
202 * The Floyd-Warshall algorithm takes as input an adjacency matrix representation of a weighted, directed graph (V, E).
203 * 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
204 * graph may have negative weights, but the graph must not have any negative weight cycles. The algorithm computes, for
205 * each pair of vertices, the minimum weight among all paths between the two vertices. The running time complexity is
208 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d, xbt_node_t * p)
213 unsigned long n = xbt_dynar_length(g->nodes);
215 for (i = 0; i < n * n; i++) {
219 for (i = 0; i < n; i++) {
220 for (j = 0; j < n; j++) {
222 p[i*n+j] = *((xbt_node_t *) xbt_dynar_get_ptr(g->nodes, i));
227 for (k = 0; k < n; k++) {
228 for (i = 0; i < n; i++) {
229 for (j = 0; j < n; j++) {
230 if ((d[i*n+k] > -1) && (d[k*n+j] > -1)) {
231 if ((d[i*n+j] < 0) || (d[i*n+j] > d[i*n+k] + d[k*n+j])) {
232 d[i*n+j] = d[i*n+k] + d[k*n+j];
241 /** @brief Export the given graph in the GraphViz formatting for visualization */
242 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename, const char *(node_name) (xbt_node_t),
243 const char *(edge_name) (xbt_edge_t))
245 unsigned int cursor = 0;
246 xbt_node_t node = NULL;
247 xbt_edge_t edge = NULL;
248 const char *name = NULL;
250 FILE *file = fopen(filename, "w");
251 xbt_assert(file, "Failed to open %s \n", filename);
254 fprintf(file, "digraph test {\n");
256 fprintf(file, "graph test {\n");
258 fprintf(file, " graph [overlap=scale]\n");
260 fprintf(file, " node [shape=box, style=filled]\n");
261 fprintf(file, " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
263 xbt_dynar_foreach(g->nodes, cursor, node) {
265 fprintf(file, " \"%s\";\n", node_name(node));
267 fprintf(file, " \"%p\";\n", node);
270 xbt_dynar_foreach(g->edges, cursor, edge) {
272 const char *c_dir = "->";
273 const char *c_ndir = "--";
280 const char *src_name = node_name(edge->src);
281 const char *dst_name = node_name(edge->dst);
282 fprintf(file, " \"%s\" %s \"%s\"", src_name, c, dst_name);
284 fprintf(file, " \"%p\" %s \"%p\"", edge->src, c, edge->dst);
288 name = edge_name(edge);
290 fprintf(file, "[label=\"%s\"]", name);
292 fprintf(file, ";\n");
294 fprintf(file, "}\n");