src/work/johanna/kruskal_test.cc
author klao
Sat, 17 Apr 2004 01:57:48 +0000
changeset 347 e4ab32225f1c
parent 218 5964f1c64ca1
child 349 42c660f58702
permissions -rw-r--r--
A generic map with value type [0, N) where N is a small integer.
Can enumerate keys with a given value.
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@246
    74
       << kruskal1(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@246
    92
       << kruskal2(G, edge_cost_map, back_inserter(tree_edge_vec))
beckerjc@246
    93
       << endl;
beckerjc@246
    94
beckerjc@246
    95
  int i = 1;
beckerjc@246
    96
  for(vector<Edge>::iterator e = tree_edge_vec.begin();
beckerjc@246
    97
      e != tree_edge_vec.end(); ++e, ++i) {
beckerjc@246
    98
    cout << i << ". el: " << *e << endl;
beckerjc@246
    99
  }
beckerjc@246
   100
beckerjc@246
   101
beckerjc@246
   102
//   typedef MinCostTreeKruskal<ListGraph, ECostMap, EBoolMap> MCTK;
beckerjc@246
   103
beckerjc@246
   104
//   MCTK mctk(G, edge_cost_map, tree_map);
beckerjc@246
   105
//   double k0lts = mctk.run();
beckerjc@246
   106
beckerjc@246
   107
//   cout << "Uniform 2-es koltseggel: " << k0lts << endl;
beckerjc@246
   108
beckerjc@246
   109
//   // Max koltsegu fa szamitasa elore megrendezett koltseg vektorbol:
beckerjc@246
   110
//   typedef MinCostTreeKruskal<ListGraph, DummyMap<Edge,int>, EBoolMap> MCTK2;
beckerjc@246
   111
//   MCTK2 mctk2(G, DummyMap<Edge,int>(), tree_map);
beckerjc@246
   112
//   MCTK2::EdgeCostVector ecv;
beckerjc@246
   113
//   ecv.push_back(make_pair(e1, 10));
beckerjc@246
   114
//   ecv.push_back(make_pair(e2, 9));
beckerjc@246
   115
//   ecv.push_back(make_pair(e3, 8));
beckerjc@246
   116
//   ecv.push_back(make_pair(e4, 7));
beckerjc@246
   117
//   ecv.push_back(make_pair(e5, 6));
beckerjc@246
   118
//   ecv.push_back(make_pair(e6, 5));
beckerjc@246
   119
//   ecv.push_back(make_pair(e7, 4));
beckerjc@246
   120
//   ecv.push_back(make_pair(e8, 3));
beckerjc@246
   121
//   ecv.push_back(make_pair(e9, 2));
beckerjc@246
   122
//   ecv.push_back(make_pair(e10, 1));
beckerjc@246
   123
beckerjc@246
   124
//   k0lts = mctk2.run(ecv);
beckerjc@246
   125
//   cout << "Max koltsegu fa elore megrendezett koltseg vektorbol: 31 = "
beckerjc@246
   126
//        << k0lts << endl;
beckerjc@218
   127
beckerjc@150
   128
beckerjc@150
   129
  return 0;
beckerjc@150
   130
}