Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Changed the convention for undirected graphs to decrease useless memory usage (I...
[simgrid.git] / src / xbt / graph.c
index 67e0049f42d3574c2fbb6ccb66f24788b14bc8fc..23f9ea0c8577ef3a0f76309c63a465817c3839cb 100644 (file)
@@ -1,13 +1,9 @@
-
-
-
 /*     $Id$     */
 
-
 /* a generic graph library.                                                 */
 
-/* Copyright (c) 2006 Darina Dimitrova, Arnaud Legrand. 
-   All rights reserved.                  */
+/* Copyright (c) 2006 Darina Dimitrova, Arnaud Legrand.                     */
+/* All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -19,6 +15,7 @@
 #include "graph_private.h"
 #include "xbt/graphxml_parse.h"
 #include "xbt/dict.h"
+#include "xbt/heap.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(graph, xbt, "Graph");
 
@@ -60,16 +57,14 @@ xbt_edge_t xbt_graph_new_edge(xbt_graph_t g,
 
   edge = xbt_new0(struct xbt_edge, 1);
   xbt_dynar_push(src->out, &edge);
-  xbt_dynar_push(dst->in, &edge);
+  if (g->directed) 
+    xbt_dynar_push(dst->in, &edge);
+  else /* only the "out" field is used */
+    xbt_dynar_push(dst->out, &edge);
 
   edge->data = data;
   edge->src = src;
   edge->dst = dst;
-  if (!g->directed) 
-    {
-    xbt_dynar_push(src->in, &edge);
-    xbt_dynar_push(dst->out, &edge);
-  }
 
   xbt_dynar_push(g->edges, &edge);
 
@@ -173,17 +168,17 @@ void xbt_graph_free_edge(xbt_graph_t g, xbt_edge_t e,
     {
     if (edge == e)
       {
-       idx = __xbt_find_in_dynar(edge->dst->in,edge); 
-       xbt_dynar_remove_at(edge->dst->in, idx,NULL);
+       if (g->directed) {
+         idx = __xbt_find_in_dynar(edge->dst->in,edge); 
+         xbt_dynar_remove_at(edge->dst->in, idx,NULL);
+       } else { /* only the out field is used */
+         idx = __xbt_find_in_dynar(edge->dst->out,edge); 
+         xbt_dynar_remove_at(edge->dst->out, idx,NULL);
+       }
+
        idx = __xbt_find_in_dynar(edge->src->out,edge);
        xbt_dynar_remove_at(edge->src->out,idx,NULL);   
-       if (!g->directed)
-         {
-           idx = __xbt_find_in_dynar(edge->src->in,edge); 
-           xbt_dynar_remove_at(edge->src->in,idx,NULL);
-           idx = __xbt_find_in_dynar(edge->dst->out,edge);
-           xbt_dynar_remove_at(edge->dst->out,idx,NULL);          
-         }
+
         xbt_dynar_cursor_rm(g->edges, &cursor); 
        free(edge);
        break;
@@ -383,10 +378,57 @@ xbt_node_t *xbt_graph_shortest_paths(xbt_graph_t g)
 }
 
 
+xbt_edge_t* xbt_graph_spanning_tree_prim(xbt_graph_t g)
+{
+  int tree_size=0;
+  int tree_size_max=xbt_dynar_length(g->nodes)-1;
+  xbt_edge_t *tree = xbt_new0(xbt_edge_t,tree_size_max);
+  xbt_edge_t e,edge;
+  xbt_node_t node = NULL;
+  xbt_dynar_t edge_list = NULL;
+  xbt_heap_t heap = xbt_heap_new(10,NULL);
+  int cursor;
+
+  xbt_assert0(!(g->directed),
+             "Spanning trees do not make sense on directed graphs");
+
+  node = xbt_dynar_getfirst_as(g->nodes,xbt_node_t);
+  node->xbtdata = (void*) 1;
+  edge_list = node->out;
+  xbt_dynar_foreach(edge_list, cursor, e)
+    xbt_heap_push(heap,e, -(e->length));
+  
+  while((edge=xbt_heap_pop(heap))) {
+    if((edge->src->xbtdata) && (edge->dst->xbtdata)) continue;
+    tree[tree_size++]=edge;
+    if(!(edge->src->xbtdata)) {
+      edge->src->xbtdata = (void*) 1;
+      edge_list = edge->src->out;
+      xbt_dynar_foreach(edge_list, cursor, e) {
+       xbt_heap_push(heap,e, -(e->length));
+      }
+    } else {
+      edge->dst->xbtdata = (void*) 1;
+      edge_list = edge->dst->out;
+      xbt_dynar_foreach(edge_list, cursor, e) {
+       xbt_heap_push(heap,e, -(e->length));
+      }
+    }
+    if(tree_size==tree_size_max) break;
+  }
+  
+  xbt_heap_free(heap);
+
+  xbt_dynar_foreach(g->nodes, cursor, node) {
+    node->xbtdata = NULL;
+  }
+  return tree;
+}
+
+/********************* Import and Export ******************/
 static xbt_graph_t parsed_graph = NULL;
 static xbt_dict_t parsed_nodes = NULL;
 
-
 static void __parse_graph_begin(void)
 {
   DEBUG0("<graph>");
@@ -399,7 +441,7 @@ static void __parse_graph_end(void)
 static void __parse_node(void)
 {
   xbt_node_t node =
-      xbt_graph_new_node(parsed_graph, (void *) A_graphxml_node_name);
+      xbt_graph_new_node(parsed_graph, NULL);
 
   xbt_dict_set(parsed_nodes, A_graphxml_node_name, (void *) node, NULL);
 
@@ -407,12 +449,11 @@ static void __parse_node(void)
 }
 static void __parse_edge(void)
 {
-  xbt_edge_t edge = xbt_graph_new_edge(parsed_graph,
-                                      xbt_dict_get(parsed_nodes,
-                                                   A_graphxml_edge_source),
-                                      xbt_dict_get(parsed_nodes,
-                                                   A_graphxml_edge_target),
-                                      (void *) A_graphxml_edge_name);
+  xbt_edge_t edge = 
+    xbt_graph_new_edge(parsed_graph,
+                      xbt_dict_get(parsed_nodes,A_graphxml_edge_source),
+                      xbt_dict_get(parsed_nodes,A_graphxml_edge_target),
+                      NULL);
 
   xbt_graph_edge_set_length(edge, atof(A_graphxml_edge_length));
 
@@ -449,7 +490,7 @@ xbt_graph_t xbt_graph_read(const char *filename)
   return graph;
 }
 
-void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
+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))
 {
@@ -462,19 +503,24 @@ void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
   file=fopen(filename,"w");
   xbt_assert1(file, "Failed to open %s \n",filename);
 
-  fprintf(file,"graph test {\n");
+  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) {
-    fprintf(file,"  %p ", node);
+    fprintf(file,"  \"%p\" ", node);
     if((node_name)&&((name=node_name(node)))) fprintf(file,"[label=\"%s\"]",name);
     fprintf(file,";\n");
   }
   xbt_dynar_foreach(g->edges, cursor, edge) {
-    fprintf(file,"  %p -- %p",edge->src, edge->dst);
+    if(g->directed)
+      fprintf(file,"  \"%p\" -> \"%p\"",edge->src, edge->dst);
+    else
+      fprintf(file,"  \"%p\" -- \"%p\"",edge->src, edge->dst);
     if((edge_name)&&((name=edge_name(edge)))) fprintf(file,"[label=\"%s\"]",name);
     fprintf(file,";\n");
   }
@@ -482,7 +528,7 @@ void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
   fclose(file);
 }
 
-void xbt_graph_export_graphviz(xbt_graph_t g, const char *filename,
+void xbt_graph_export_graphxml(xbt_graph_t g, const char *filename,
                               const char *(node_name)(xbt_node_t),
                               const char *(edge_name)(xbt_edge_t))
 {