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"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_graph, xbt, "Graph");
21 /** @brief Constructor
24 xbt_graph_t xbt_graph_new_graph(unsigned short int directed, void *data)
26 xbt_graph_t graph = xbt_new0(struct xbt_graph, 1);
27 graph->directed = directed;
29 graph->nodes = xbt_dynar_new(sizeof(xbt_node_t), NULL);
30 graph->edges = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
35 /** @brief add a node to the given graph */
36 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
38 xbt_node_t node= xbt_new0(struct xbt_node, 1);
41 /* only the "out" field is used */
42 node->in = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
44 node->out = xbt_dynar_new(sizeof(xbt_edge_t), NULL);
45 node->position_x = -1.0;
46 node->position_y = -1.0;
48 xbt_dynar_push(g->nodes, &node);
53 /** @brief add an edge to the given graph */
54 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g, xbt_node_t src, xbt_node_t dst, void *data)
56 xbt_edge_t edge = xbt_new0(struct xbt_edge, 1);
57 xbt_dynar_push(src->out, &edge);
59 xbt_dynar_push(dst->in, &edge);
60 else /* only the "out" field is used */
61 xbt_dynar_push(dst->out, &edge);
67 xbt_dynar_push(g->edges, &edge);
72 /** @brief Get the edge connecting src and dst */
73 xbt_edge_t xbt_graph_get_edge(xbt_graph_t g, xbt_node_t src, xbt_node_t dst)
78 xbt_dynar_foreach(src->out, cursor, edge) {
79 XBT_DEBUG("%p = %p--%p", edge, edge->src, edge->dst);
80 if ((edge->src == src) && (edge->dst == dst))
84 xbt_dynar_foreach(src->out, cursor, edge) {
85 XBT_DEBUG("%p = %p--%p", edge, edge->src, edge->dst);
86 if ((edge->dst == src) && (edge->src == dst))
93 /** @brief Get the user data associated to a node */
94 void *xbt_graph_node_get_data(xbt_node_t node)
99 /** @brief Set the user data associated to a node */
100 void xbt_graph_node_set_data(xbt_node_t node, void *data)
105 /** @brief Get the user data associated to a edge */
106 void *xbt_graph_edge_get_data(xbt_edge_t edge)
111 /** @brief Set the user data associated to a edge */
112 void xbt_graph_edge_set_data(xbt_edge_t edge, void *data)
117 /** @brief Destructor
118 * @param g: poor victim
119 * @param node_free_function: function to use to free data associated to each node
120 * @param edge_free_function: function to use to free data associated to each edge
121 * @param graph_free_function: function to use to free data associated to g
123 * Free the graph structure.
125 void xbt_graph_free_graph(xbt_graph_t g, void_f_pvoid_t node_free_function, void_f_pvoid_t edge_free_function,
126 void_f_pvoid_t graph_free_function)
132 xbt_dynar_foreach(g->edges, cursor, edge) {
133 if (edge_free_function)
134 edge_free_function(edge->data);
137 xbt_dynar_free(&(g->edges));
139 xbt_dynar_foreach(g->nodes, cursor, node) {
140 xbt_dynar_free(&(node->out));
141 xbt_dynar_free(&(node->in));
142 if (node_free_function)
143 node_free_function(node->data);
146 xbt_dynar_free(&(g->nodes));
148 if (graph_free_function)
149 graph_free_function(g->data);
153 /** @brief Retrieve the graph's nodes as a dynar */
154 xbt_dynar_t xbt_graph_get_nodes(xbt_graph_t g)
159 /** @brief Retrieve the graph's edges as a dynar */
160 xbt_dynar_t xbt_graph_get_edges(xbt_graph_t g)
165 /** @brief Retrieve the node at the source of the given edge */
166 xbt_node_t xbt_graph_edge_get_source(xbt_edge_t e)
171 /** @brief Retrieve the node being the target of the given edge */
172 xbt_node_t xbt_graph_edge_get_target(xbt_edge_t e)
177 /** @brief Retrieve the outgoing edges of the given node */
178 xbt_dynar_t xbt_graph_node_get_outedges(xbt_node_t n)
183 /** @brief Set the weight of the given edge */
184 void xbt_graph_edge_set_length(xbt_edge_t e, double length)
190 /** @brief Get the length of a edge */
191 double xbt_graph_edge_get_length(xbt_edge_t edge)
196 /** @brief Floyd-Warshall algorithm for shortest path finding
200 * The Floyd-Warshall algorithm takes as input an adjacency matrix representation of a weighted, directed graph (V, E).
201 * 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
202 * graph may have negative weights, but the graph must not have any negative weight cycles. The algorithm computes, for
203 * each pair of vertices, the minimum weight among all paths between the two vertices. The running time complexity is
206 void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d, xbt_node_t * p)
211 unsigned long n = xbt_dynar_length(g->nodes);
213 for (i = 0; i < n * n; i++) {
217 for (i = 0; i < n; i++) {
218 for (j = 0; j < n; j++) {
220 p[i*n+j] = *((xbt_node_t *) xbt_dynar_get_ptr(g->nodes, i));
225 for (k = 0; k < n; k++) {
226 for (i = 0; i < n; i++) {
227 for (j = 0; j < n; j++) {
228 if (d[i * n + k] > -1 && d[k * n + j] > -1 &&
229 (d[i * n + j] < 0 || d[i * n + j] > d[i * n + k] + d[k * n + j])) {
230 d[i * n + j] = d[i * n + k] + d[k * n + j];
231 p[i * n + j] = p[k * n + j];
238 /** @brief Export the given graph in the GraphViz formatting for visualization */
239 void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename, const char *(node_name) (xbt_node_t),
240 const char *(edge_name) (xbt_edge_t))
242 unsigned int cursor = 0;
243 xbt_node_t node = NULL;
244 xbt_edge_t edge = NULL;
245 const char *name = NULL;
247 FILE *file = fopen(filename, "w");
248 xbt_assert(file, "Failed to open %s \n", filename);
251 fprintf(file, "digraph test {\n");
253 fprintf(file, "graph test {\n");
255 fprintf(file, " graph [overlap=scale]\n");
257 fprintf(file, " node [shape=box, style=filled]\n");
258 fprintf(file, " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
260 xbt_dynar_foreach(g->nodes, cursor, node) {
262 fprintf(file, " \"%s\";\n", node_name(node));
264 fprintf(file, " \"%p\";\n", node);
267 xbt_dynar_foreach(g->edges, cursor, edge) {
269 const char *c_dir = "->";
270 const char *c_ndir = "--";
277 const char *src_name = node_name(edge->src);
278 const char *dst_name = node_name(edge->dst);
279 fprintf(file, " \"%s\" %s \"%s\"", src_name, c, dst_name);
281 fprintf(file, " \"%p\" %s \"%p\"", edge->src, c, edge->dst);
285 name = edge_name(edge);
287 fprintf(file, "[label=\"%s\"]", name);
289 fprintf(file, ";\n");
291 fprintf(file, "}\n");