demo/kruskal_demo.cc
changeset 1584 cf4bc8d477f4
parent 1583 2b329fd595ef
child 1641 77f6ab7ad66f
     1.1 --- a/demo/kruskal_demo.cc	Fri Jul 22 15:15:29 2005 +0000
     1.2 +++ b/demo/kruskal_demo.cc	Fri Jul 22 16:57:07 2005 +0000
     1.3 @@ -60,44 +60,71 @@
     1.4    Edge e9 = g.addEdge(v3, t);
     1.5    Edge e10 = g.addEdge(v4, t);
     1.6  
     1.7 -  //Make the input and output for the kruskal.
     1.8 +  //Make the input for the kruskal.
     1.9    typedef ListGraph::EdgeMap<int> ECostMap;
    1.10 +  ECostMap edge_cost_map(g);
    1.11 +
    1.12 +  // Fill the edge_cost_map. 
    1.13 +  edge_cost_map.set(e1, -10);
    1.14 +  edge_cost_map.set(e2, -9);
    1.15 +  edge_cost_map.set(e3, -8);
    1.16 +  edge_cost_map.set(e4, -7);
    1.17 +  edge_cost_map.set(e5, -6);
    1.18 +  edge_cost_map.set(e6, -5);
    1.19 +  edge_cost_map.set(e7, -4);
    1.20 +  edge_cost_map.set(e8, -3);
    1.21 +  edge_cost_map.set(e9, -2);
    1.22 +  edge_cost_map.set(e10, -1);
    1.23 +
    1.24 +  // Make the map or the vector, which will contain the edges of the minimum
    1.25 +  // spanning tree.
    1.26 +
    1.27    typedef ListGraph::EdgeMap<bool> EBoolMap;
    1.28 -
    1.29 -  ECostMap edge_cost_map(g, 2);
    1.30    EBoolMap tree_map(g);
    1.31  
    1.32 -  // Kruskal.
    1.33 -  std::cout << "The weight of the minimum spanning tree by using Kruskal algorithm is " 
    1.34 -	    << kruskal(g, ConstMap<ListGraph::Edge,int>(2), tree_map)<<std::endl;
    1.35 -
    1.36 -  //Make another input (non-uniform costs) for the kruskal.
    1.37 -  ECostMap edge_cost_map_2(g);
    1.38 -  edge_cost_map_2.set(e1, -10);
    1.39 -  edge_cost_map_2.set(e2, -9);
    1.40 -  edge_cost_map_2.set(e3, -8);
    1.41 -  edge_cost_map_2.set(e4, -7);
    1.42 -  edge_cost_map_2.set(e5, -6);
    1.43 -  edge_cost_map_2.set(e6, -5);
    1.44 -  edge_cost_map_2.set(e7, -4);
    1.45 -  edge_cost_map_2.set(e8, -3);
    1.46 -  edge_cost_map_2.set(e9, -2);
    1.47 -  edge_cost_map_2.set(e10, -1);
    1.48 -
    1.49    vector<Edge> tree_edge_vec;
    1.50  
    1.51 -  //Test with non uniform costs and inserter.
    1.52 -  std::cout << "The weight of the minimum spanning tree with non-uniform costs is " << 
    1.53 -    kruskal(g, edge_cost_map_2, std::back_inserter(tree_edge_vec)) <<std::endl;
    1.54  
    1.55 -  //The vector for the edges of the output tree.
    1.56 -  tree_edge_vec.clear();
    1.57 +  //Kruskal Algorithm.  
    1.58  
    1.59 -  //Test with makeKruskalMapInput and makeKruskalSequenceOutput.
    1.60 +  //Input: a graph (g); a costmap of the graph (edge_cost_map); a
    1.61 +  //boolmap (tree_map) or a vector (tree_edge_vec) to store the edges
    1.62 +  //of the output tree;
    1.63  
    1.64 +  //Output: it gives back the value of the minimum spanning tree, and
    1.65 +  //set true for the edges of the tree in the edgemap tree_map or
    1.66 +  //store the edges of the tree in the vector tree_edge_vec;
    1.67 +
    1.68 +
    1.69 +  // Kruskal with boolmap;
    1.70 +  std::cout << "The weight of the minimum spanning tree is " << 
    1.71 +    kruskal(g, edge_cost_map, tree_map)<<std::endl;
    1.72 +
    1.73 +  int k=0;
    1.74 +  std::cout << "The edges of the tree:" ;
    1.75 +  for(EdgeIt i(g); i!=INVALID; ++i){
    1.76 +
    1.77 +    if (tree_map[i]) { 
    1.78 +      std::cout << g.id(i) <<";";
    1.79 +      ++k;
    1.80 +    }
    1.81 +  }
    1.82 +  std::cout << std::endl;
    1.83 +  std::cout << "The size of the tree is: "<< k << std::endl;
    1.84 +
    1.85 +
    1.86 +  // Kruskal with vector;
    1.87    std::cout << "The weight of the minimum spanning tree again is " << 
    1.88 -   kruskal(g,makeKruskalMapInput(g,edge_cost_map_2),makeKruskalSequenceOutput(std::back_inserter(tree_edge_vec)))<< std::endl;
    1.89 +    kruskal(g, edge_cost_map, std::back_inserter(tree_edge_vec)) <<std::endl;
    1.90  
    1.91  
    1.92 +
    1.93 +  std::cout << "The edges of the tree again: " ;
    1.94 +  for(int i=tree_edge_vec.size()-1; i>=0;  i--)
    1.95 +    std::cout << g.id(tree_edge_vec[i]) << ";" ;
    1.96 +  std::cout << std::endl;
    1.97 +  std::cout << "The size of the tree again is: "<< tree_edge_vec.size()<< std::endl; 
    1.98 +
    1.99 +  
   1.100    return 0;
   1.101  }