1 /* a generic graph library. */
3 /* Copyright (c) 2006-2017. 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"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_graph, xbt, "Graph");
22 /** @brief Constructor
25 xbt_graph_t xbt_graph_new_graph(unsigned short int directed, void *data)
27 xbt_graph_t graph = xbt_new0(struct xbt_graph, 1);
28 graph->directed = directed;
30 graph->nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
31 graph->edges = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
36 /** @brief add a node to the given graph */
37 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
39 xbt_node_t node= xbt_new0(struct xbt_node, 1);
42 /* only the "out" field is used */
43 node->in = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
45 node->out = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
46 node->position_x = -1.0;
47 node->position_y = -1.0;
49 xbt_dynar_push(g->nodes, &node);
54 /** @brief add an edge to the given graph */
55 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g, xbt_node_t src, xbt_node_t dst, void *data)
57 xbt_edge_t edge = xbt_new0(struct xbt_edge, 1);
58 xbt_dynar_push(src->out, &edge);
60 xbt_dynar_push(dst->in, &edge);
61 else /* only the "out" field is used */
62 xbt_dynar_push(dst->out, &edge);
68 xbt_dynar_push(g->edges, &edge);
73 /** @brief Get the edge connecting src and dst */
74 xbt_edge_t xbt_graph_get_edge(xbt_graph_t g, xbt_node_t src, xbt_node_t dst)
79 xbt_dynar_foreach(src->out, cursor, edge) {
80 XBT_DEBUG("%p = %p--%p", edge, edge->src, edge->dst);
81 if ((edge->src == src) && (edge->dst == dst))
85 xbt_dynar_foreach(src->out, cursor, edge) {
86 XBT_DEBUG("%p = %p--%p", edge, edge->src, edge->dst);
87 if ((edge->dst == src) && (edge->src == dst))
94 /** @brief Get the user data associated to a node */
95 void *xbt_graph_node_get_data(xbt_node_t node)
100 /** @brief Set the user data associated to a node */
101 void xbt_graph_node_set_data(xbt_node_t node, void *data)
106 /** @brief Get the user data associated to a edge */
107 void *xbt_graph_edge_get_data(xbt_edge_t edge)
112 /** @brief Set the user data associated to a edge */
113 void xbt_graph_edge_set_data(xbt_edge_t edge, void *data)
118 /** @brief Destructor
119 * @param g: poor victim
120 * @param node_free_function: function to use to free data associated to each node
121 * @param edge_free_function: function to use to free data associated to each edge
122 * @param graph_free_function: function to use to free data associated to g
124 * Free the graph structure.
126 void xbt_graph_free_graph(xbt_graph_t g, void_f_pvoid_t node_free_function, void_f_pvoid_t edge_free_function,
127 void_f_pvoid_t graph_free_function)
133 xbt_dynar_foreach(g->edges, cursor, edge) {
134 if (edge_free_function)
135 edge_free_function(edge->data);
138 xbt_dynar_free(&(g->edges));
140 xbt_dynar_foreach(g->nodes, cursor, node) {
141 xbt_dynar_free(&(node->out));
142 xbt_dynar_free(&(node->in));
143 if (node_free_function)
144 node_free_function(node->data);
147 xbt_dynar_free(&(g->nodes));
149 if (graph_free_function)
150 graph_free_function(g->data);
154 /** @brief Retrieve the graph's nodes as a dynar */
155 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
160 /** @brief Retrieve the graph's edges as a dynar */
161 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
166 /** @brief Retrieve the node at the source of the given edge */
167 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
172 /** @brief Retrieve the node being the target of the given edge */
173 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
178 /** @brief Retrieve the outgoing edges of the given node */
179 xbt_dynar_t xbt_graph_node_get_outedges(xbt_node_t n)
184 /** @brief Set the weight of the given edge */
185 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
191 /** @brief Get the length of a edge */
192 double xbt_graph_edge_get_length(xbt_edge_t edge)
197 /** @brief Floyd-Warshall algorithm for shortest path finding
201 * The Floyd-Warshall algorithm takes as input an adjacency matrix representation of a weighted, directed graph (V, E).
202 * 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
203 * graph may have negative weights, but the graph must not have any negative weight cycles. The algorithm computes, for
204 * each pair of vertices, the minimum weight among all paths between the two vertices. The running time complexity is
207 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d, xbt_node_t * p)
212 unsigned long n = xbt_dynar_length(g->nodes);
214 for (i = 0; i < n * n; i++) {
218 for (i = 0; i < n; i++) {
219 for (j = 0; j < n; j++) {
221 p[i*n+j] = *((xbt_node_t *) xbt_dynar_get_ptr(g->nodes, i));
226 for (k = 0; k < n; k++) {
227 for (i = 0; i < n; i++) {
228 for (j = 0; j < n; j++) {
229 if (d[i * n + k] > -1 && d[k * n + j] > -1 &&
230 (d[i * n + j] < 0 || d[i * n + j] > d[i * n + k] + d[k * n + j])) {
231 d[i * n + j] = d[i * n + k] + d[k * n + j];
232 p[i * n + j] = p[k * n + j];
239 /** @brief Export the given graph in the GraphViz formatting for visualization */
240 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename, const char *(node_name) (xbt_node_t),
241 const char *(edge_name) (xbt_edge_t))
243 unsigned int cursor = 0;
244 xbt_node_t node = NULL;
245 xbt_edge_t edge = NULL;
246 const char *name = NULL;
248 FILE *file = fopen(filename, "w");
249 xbt_assert(file, "Failed to open %s \n", filename);
252 fprintf(file, "digraph test {\n");
254 fprintf(file, "graph test {\n");
256 fprintf(file, " graph [overlap=scale]\n");
258 fprintf(file, " node [shape=box, style=filled]\n");
259 fprintf(file, " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
261 xbt_dynar_foreach(g->nodes, cursor, node) {
263 fprintf(file, " \"%s\";\n", node_name(node));
265 fprintf(file, " \"%p\";\n", node);
268 xbt_dynar_foreach(g->edges, cursor, edge) {
270 const char *c_dir = "->";
271 const char *c_ndir = "--";
278 const char *src_name = node_name(edge->src);
279 const char *dst_name = node_name(edge->dst);
280 fprintf(file, " \"%s\" %s \"%s\"", src_name, c, dst_name);
282 fprintf(file, " \"%p\" %s \"%p\"", edge->src, c, edge->dst);
286 name = edge_name(edge);
288 fprintf(file, "[label=\"%s\"]", name);
290 fprintf(file, ";\n");
292 fprintf(file, "}\n");