-
-/** @brief Set the weight of the given edge */
-void xbt_graph_edge_set_length(xbt_edge_t e, double length)
-{
- e->length = length;
-
-}
-
-/** @brief Get the length of a edge */
-double xbt_graph_edge_get_length(xbt_edge_t edge)
-{
- return edge->length;
-}
-
-/** @brief Floyd-Warshall algorithm for shortest path finding
- *
- * From wikipedia:
- *
- * The Floyd–Warshall algorithm takes as input an adjacency matrix
- * representation of a weighted, directed graph (V, E). 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 graph may have negative weights, but the
- * graph must not have any negative weight cycles. The algorithm computes,
- * for each pair of vertices, the minimum weight among all paths between
- * the two vertices. The running time complexity is Θ(|V|3).
- */
-void xbt_floyd_algorithm(xbt_graph_t g, double *adj, double *d,
- xbt_node_t * p)
-{
- unsigned long i, j, k;
- unsigned long n;
- n = xbt_dynar_length(g->nodes);
-
-# define D(u,v) d[(u)*n+(v)]
-# define P(u,v) p[(u)*n+(v)]
-
- for (i = 0; i < n * n; i++) {
- d[i] = adj[i];
- }
-
-
- for (i = 0; i < n; i++) {
- for (j = 0; j < n; j++) {
- if (D(i, j) != -1) {
- P(i, j) = *((xbt_node_t *) xbt_dynar_get_ptr(g->nodes, i));
- }
- }
- }
-
- for (k = 0; k < n; k++) {
- for (i = 0; i < n; i++) {
- for (j = 0; j < n; j++) {
- if ((D(i, k) != -1) && (D(k, j) != -1)) {
- if ((D(i, j) == -1) || (D(i, j) > D(i, k) + D(k, j))) {
- D(i, j) = D(i, k) + D(k, j);
- P(i, j) = P(k, j);
- }
- }
- }
- }
- }
-# undef P
-# undef D
-}
-
-/** @brief Export the given graph in the GraphViz formatting for visualization */
-void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename,
- const char *(node_name) (xbt_node_t),
- const char *(edge_name) (xbt_edge_t))
-{
- unsigned int cursor = 0;
- xbt_node_t node = NULL;
- xbt_edge_t edge = NULL;
- FILE *file = NULL;
- const char *name = NULL;
-
- file = fopen(filename, "w");
- xbt_assert(file, "Failed to open %s \n", filename);
-
- if (g->directed)
- fprintf(file, "digraph test {\n");
- else
- fprintf(file, "graph test {\n");
-
- fprintf(file, " graph [overlap=scale]\n");
-
- fprintf(file, " node [shape=box, style=filled]\n");
- fprintf(file,
- " node [width=.3, height=.3, style=filled, color=skyblue]\n\n");
-
- xbt_dynar_foreach(g->nodes, cursor, node) {
- if (node_name){
- fprintf(file, " \"%s\";\n", node_name(node));
- }else{
- fprintf(file, " \"%p\";\n", node);
- }
- }
- xbt_dynar_foreach(g->edges, cursor, edge) {
- const char *c;
- const char *c_dir = "->";
- const char *c_ndir = "--";
- if (g->directed){
- c = c_dir;
- }else{
- c = c_ndir;
- }
- const char *src_name, *dst_name;
- if (node_name){
- src_name = node_name(edge->src);
- dst_name = node_name(edge->dst);
- fprintf(file, " \"%s\" %s \"%s\"", src_name, c, dst_name);
- }else{
- fprintf(file, " \"%p\" %s \"%p\"", edge->src, c, edge->dst);
- }
-
- if ((edge_name) && ((name = edge_name(edge))))
- fprintf(file, "[label=\"%s\"]", name);
- fprintf(file, ";\n");
- }
- fprintf(file, "}\n");
- fclose(file);
-}