src/work/johanna/kruskal_test.cc
author deba
Wed, 14 Jul 2004 10:06:27 +0000
changeset 701 c03e073b8394
parent 349 42c660f58702
child 737 2d867176d10e
permissions -rw-r--r--
(none)
beckerjc@150
     1
#include <string>
beckerjc@150
     2
#include <iostream>
beckerjc@150
     3
#include <map>
beckerjc@246
     4
#include <vector>
beckerjc@150
     5
beckerjc@150
     6
#include <kruskal.h>
beckerjc@218
     7
#include <list_graph.h>
beckerjc@150
     8
beckerjc@150
     9
beckerjc@150
    10
using namespace std;
beckerjc@150
    11
using namespace hugo;
beckerjc@150
    12
beckerjc@150
    13
class string_int_map : public map<string,int> {
beckerjc@150
    14
public:
beckerjc@150
    15
  int get(const string &s) {
beckerjc@150
    16
    // Bocs, ez igy gaaaany, de nem volt kedvem utananezni, hogy
beckerjc@150
    17
    // hogy is mukodik ez a map :)
beckerjc@150
    18
    if( count(s) == 0 ) {
beckerjc@150
    19
      operator[](s) = -1;
beckerjc@150
    20
    }
beckerjc@150
    21
    return operator[](s);
beckerjc@150
    22
  }
beckerjc@150
    23
  void set(const string &s, int i) {
beckerjc@150
    24
      operator[](s) = i;
beckerjc@150
    25
  }
beckerjc@150
    26
};
beckerjc@150
    27
beckerjc@150
    28
beckerjc@218
    29
// Egy olyan "map", ami nem tud semmit, csak a typedef-eket.
beckerjc@218
    30
// Valami elegansabb megoldas kene a Kruskalban...
beckerjc@218
    31
beckerjc@218
    32
template <typename K, typename V>
beckerjc@218
    33
class DummyMap {
beckerjc@218
    34
public:
beckerjc@218
    35
  typedef K KeyType;
beckerjc@218
    36
  typedef V ValueType;
beckerjc@218
    37
};
beckerjc@218
    38
beckerjc@150
    39
int main() {
beckerjc@150
    40
beckerjc@218
    41
  typedef ListGraph::Node Node;
beckerjc@218
    42
  typedef ListGraph::Edge Edge;
beckerjc@150
    43
  typedef ListGraph::NodeIt NodeIt;
beckerjc@150
    44
  typedef ListGraph::EdgeIt EdgeIt;
beckerjc@150
    45
beckerjc@150
    46
  ListGraph G;
beckerjc@150
    47
beckerjc@218
    48
  Node s=G.addNode();
beckerjc@218
    49
  Node v1=G.addNode();
beckerjc@218
    50
  Node v2=G.addNode();
beckerjc@218
    51
  Node v3=G.addNode();
beckerjc@218
    52
  Node v4=G.addNode();
beckerjc@218
    53
  Node t=G.addNode();
beckerjc@150
    54
  
beckerjc@218
    55
  Edge e1 = G.addEdge(s, v1);
beckerjc@218
    56
  Edge e2 = G.addEdge(s, v2);
beckerjc@218
    57
  Edge e3 = G.addEdge(v1, v2);
beckerjc@218
    58
  Edge e4 = G.addEdge(v2, v1);
beckerjc@218
    59
  Edge e5 = G.addEdge(v1, v3);
beckerjc@218
    60
  Edge e6 = G.addEdge(v3, v2);
beckerjc@218
    61
  Edge e7 = G.addEdge(v2, v4);
beckerjc@218
    62
  Edge e8 = G.addEdge(v4, v3);
beckerjc@218
    63
  Edge e9 = G.addEdge(v3, t);
beckerjc@218
    64
  Edge e10 = G.addEdge(v4, t);
beckerjc@150
    65
beckerjc@218
    66
  typedef ListGraph::EdgeMap<double> ECostMap;
beckerjc@218
    67
  typedef ListGraph::EdgeMap<bool> EBoolMap;
beckerjc@218
    68
beckerjc@218
    69
  ECostMap edge_cost_map(G, 2);
beckerjc@218
    70
  EBoolMap tree_map(G);
beckerjc@150
    71
  
beckerjc@150
    72
beckerjc@246
    73
  cout << "Uniform 2-es koltseggel: " 
beckerjc@349
    74
       << Kruskal_EdgeCostMapIn_BoolMapOut(G, edge_cost_map, tree_map)
beckerjc@246
    75
       << endl;
beckerjc@218
    76
beckerjc@218
    77
beckerjc@246
    78
  edge_cost_map.set(e1, -10);
beckerjc@246
    79
  edge_cost_map.set(e2, -9);
beckerjc@246
    80
  edge_cost_map.set(e3, -8);
beckerjc@246
    81
  edge_cost_map.set(e4, -7);
beckerjc@246
    82
  edge_cost_map.set(e5, -6);
beckerjc@246
    83
  edge_cost_map.set(e6, -5);
beckerjc@246
    84
  edge_cost_map.set(e7, -4);
beckerjc@246
    85
  edge_cost_map.set(e8, -3);
beckerjc@246
    86
  edge_cost_map.set(e9, -2);
beckerjc@246
    87
  edge_cost_map.set(e10, -1);
beckerjc@218
    88
beckerjc@246
    89
  vector<Edge> tree_edge_vec;
beckerjc@246
    90
beckerjc@246
    91
  cout << "Nemkonst koltseggel (-31): "
beckerjc@349
    92
       << Kruskal_EdgeCostMapIn_IteratorOut(G, edge_cost_map,
beckerjc@349
    93
					    back_inserter(tree_edge_vec))
beckerjc@246
    94
       << endl;
beckerjc@246
    95
beckerjc@246
    96
  int i = 1;
beckerjc@246
    97
  for(vector<Edge>::iterator e = tree_edge_vec.begin();
beckerjc@246
    98
      e != tree_edge_vec.end(); ++e, ++i) {
beckerjc@246
    99
    cout << i << ". el: " << *e << endl;
beckerjc@246
   100
  }
beckerjc@246
   101
beckerjc@349
   102
  tree_edge_vec.clear();
beckerjc@352
   103
//   SequenceOutput< back_insert_iterator< vector<Edge> > > 
beckerjc@352
   104
//     vec_filler(back_inserter(tree_edge_vec));
beckerjc@352
   105
//   cout << "Nemkonst koltseggel tarhatekonyabban: "
beckerjc@352
   106
//        << Kruskal(G,
beckerjc@352
   107
// 		  KruskalMapVec<ECostMap>(G, edge_cost_map),
beckerjc@352
   108
// 		  vec_filler)
beckerjc@352
   109
//        << endl;
beckerjc@349
   110
  cout << "Nemkonst koltseggel tarhatekonyabban: "
beckerjc@349
   111
       << Kruskal(G,
beckerjc@349
   112
		  KruskalMapVec<ECostMap>(G, edge_cost_map),
beckerjc@352
   113
		  makeSequenceOutput(back_inserter(tree_edge_vec))
beckerjc@352
   114
		  )
beckerjc@349
   115
       << endl;
beckerjc@349
   116
beckerjc@349
   117
  i = 1;
beckerjc@349
   118
  for(vector<Edge>::iterator e = tree_edge_vec.begin();
beckerjc@349
   119
      e != tree_edge_vec.end(); ++e, ++i) {
beckerjc@349
   120
    cout << i << ". el: " << *e << endl;
beckerjc@349
   121
  }
beckerjc@349
   122
beckerjc@246
   123
beckerjc@246
   124
//   typedef MinCostTreeKruskal<ListGraph, ECostMap, EBoolMap> MCTK;
beckerjc@246
   125
beckerjc@246
   126
//   MCTK mctk(G, edge_cost_map, tree_map);
beckerjc@246
   127
//   double k0lts = mctk.run();
beckerjc@246
   128
beckerjc@246
   129
//   cout << "Uniform 2-es koltseggel: " << k0lts << endl;
beckerjc@246
   130
beckerjc@246
   131
//   // Max koltsegu fa szamitasa elore megrendezett koltseg vektorbol:
beckerjc@246
   132
//   typedef MinCostTreeKruskal<ListGraph, DummyMap<Edge,int>, EBoolMap> MCTK2;
beckerjc@246
   133
//   MCTK2 mctk2(G, DummyMap<Edge,int>(), tree_map);
beckerjc@246
   134
//   MCTK2::EdgeCostVector ecv;
beckerjc@246
   135
//   ecv.push_back(make_pair(e1, 10));
beckerjc@246
   136
//   ecv.push_back(make_pair(e2, 9));
beckerjc@246
   137
//   ecv.push_back(make_pair(e3, 8));
beckerjc@246
   138
//   ecv.push_back(make_pair(e4, 7));
beckerjc@246
   139
//   ecv.push_back(make_pair(e5, 6));
beckerjc@246
   140
//   ecv.push_back(make_pair(e6, 5));
beckerjc@246
   141
//   ecv.push_back(make_pair(e7, 4));
beckerjc@246
   142
//   ecv.push_back(make_pair(e8, 3));
beckerjc@246
   143
//   ecv.push_back(make_pair(e9, 2));
beckerjc@246
   144
//   ecv.push_back(make_pair(e10, 1));
beckerjc@246
   145
beckerjc@246
   146
//   k0lts = mctk2.run(ecv);
beckerjc@246
   147
//   cout << "Max koltsegu fa elore megrendezett koltseg vektorbol: 31 = "
beckerjc@246
   148
//        << k0lts << endl;
beckerjc@218
   149
beckerjc@150
   150
beckerjc@150
   151
  return 0;
beckerjc@150
   152
}