src/work/jacint/edmonds.cc
author marci
Fri, 30 Apr 2004 17:10:01 +0000
changeset 499 767f3da8ce0e
permissions -rw-r--r--
A bipartite graph template can be used as BipartiteGraph<ListGraph>.
jacint@494
     1
#include <iostream>
jacint@494
     2
#include <vector>
jacint@494
     3
#include <string>
jacint@494
     4
jacint@494
     5
#include <list_graph.h>
jacint@494
     6
#include <edmonds.h>
jacint@494
     7
jacint@494
     8
using namespace hugo;
jacint@494
     9
jacint@494
    10
int main (int, char*[])
jacint@494
    11
{
jacint@494
    12
  typedef ListGraph::NodeIt NodeIt;
jacint@494
    13
  typedef ListGraph::EdgeIt EdgeIt;
jacint@494
    14
  typedef ListGraph::Node Node;
jacint@494
    15
  typedef ListGraph::Edge Edge;
jacint@494
    16
  typedef ListGraph::OutEdgeIt OutEdgeIt;
jacint@494
    17
  typedef ListGraph::InEdgeIt InEdgeIt;
jacint@494
    18
  
jacint@494
    19
  ListGraph flow_test;
jacint@494
    20
 
jacint@494
    21
  /*    //Ahuja könyv példája, maxflowvalue=13*/
jacint@494
    22
  Node s=flow_test.addNode();
jacint@494
    23
  Node v1=flow_test.addNode();
jacint@494
    24
  Node v2=flow_test.addNode();
jacint@494
    25
  Node v3=flow_test.addNode();
jacint@494
    26
  Node v4=flow_test.addNode();
jacint@494
    27
  Node v5=flow_test.addNode();
jacint@494
    28
  Node t=flow_test.addNode();
jacint@494
    29
  
jacint@494
    30
  ListGraph::NodeMap<std::string> Node_name(flow_test);
jacint@494
    31
  Node_name.set(s, "s");
jacint@494
    32
  Node_name.set(v1, "v1");
jacint@494
    33
  Node_name.set(v2, "v2");
jacint@494
    34
  Node_name.set(v3, "v3");
jacint@494
    35
  Node_name.set(v4, "v4");
jacint@494
    36
  Node_name.set(v5, "v5");
jacint@494
    37
  Node_name.set(t, "t");
jacint@494
    38
jacint@494
    39
  Edge s_v1=flow_test.addEdge(s, v1);
jacint@494
    40
  Edge s_v2=flow_test.addEdge(s, v2);
jacint@494
    41
  Edge s_v3=flow_test.addEdge(s, v3);
jacint@494
    42
  Edge v2_v4=flow_test.addEdge(v2, v4);
jacint@494
    43
  Edge v2_v5=flow_test.addEdge(v2, v5);
jacint@494
    44
  Edge v3_v5=flow_test.addEdge(v3, v5);
jacint@494
    45
  Edge v4_t=flow_test.addEdge(v4, t);
jacint@494
    46
  Edge v5_t=flow_test.addEdge(v5, t);
jacint@494
    47
  Edge v2_s=flow_test.addEdge(v2, s);
jacint@494
    48
  
jacint@494
    49
   ListGraph::EdgeMap<int> cap(flow_test);  
jacint@494
    50
  cap.set(s_v1, 0);
jacint@494
    51
  cap.set(s_v2, 10);
jacint@494
    52
  cap.set(s_v3, 10);
jacint@494
    53
 cap.set(v2_v4, 5);
jacint@494
    54
  cap.set(v2_v5, 8);
jacint@494
    55
  cap.set(v3_v5, 5);
jacint@494
    56
  cap.set(v4_t, 8);
jacint@494
    57
  cap.set(v5_t, 8);
jacint@494
    58
  cap.set(v2_s, 0);
jacint@494
    59
  
jacint@494
    60
jacint@494
    61
  Edmonds<ListGraph> edmonds(flow_test);
jacint@494
    62
  std::vector<Edge> matching(0);
jacint@494
    63
  std::cout<<  edmonds.run(matching);
jacint@494
    64
jacint@494
    65
  
jacint@494
    66
  /*  //Marci példája, maxflowvalue=23
jacint@494
    67
    Node s=flow_test.addNode();
jacint@494
    68
  Node v1=flow_test.addNode();
jacint@494
    69
  Node v2=flow_test.addNode();
jacint@494
    70
  Node v3=flow_test.addNode();
jacint@494
    71
  Node v4=flow_test.addNode();
jacint@494
    72
  Node t=flow_test.addNode();
jacint@494
    73
  Node z=flow_test.addNode();
jacint@494
    74
jacint@494
    75
  
jacint@494
    76
   ListGraph::NodeMap<std::string> Node_name(flow_test);
jacint@494
    77
  Node_name.set(s, "s");
jacint@494
    78
  Node_name.set(v1, "v1");
jacint@494
    79
  Node_name.set(v2, "v2");
jacint@494
    80
  Node_name.set(v3, "v3");
jacint@494
    81
  Node_name.set(v4, "v4");
jacint@494
    82
  Node_name.set(t, "t");
jacint@494
    83
  Node_name.set(z, "z");
jacint@494
    84
jacint@494
    85
  Edge s_v1=flow_test.addEdge(s, v1);
jacint@494
    86
  Edge s_v2=flow_test.addEdge(s, v2);
jacint@494
    87
  Edge v1_v2=flow_test.addEdge(v1, v2);
jacint@494
    88
  Edge v2_v1=flow_test.addEdge(v2, v1);
jacint@494
    89
  Edge v1_v3=flow_test.addEdge(v1, v3);
jacint@494
    90
  Edge v3_v2=flow_test.addEdge(v3, v2);
jacint@494
    91
  Edge v2_v4=flow_test.addEdge(v2, v4);
jacint@494
    92
  Edge v4_v3=flow_test.addEdge(v4, v3);
jacint@494
    93
  Edge v3_t=flow_test.addEdge(v3, t);
jacint@494
    94
  Edge v4_t=flow_test.addEdge(v4, t);
jacint@494
    95
  Edge v3_v3=flow_test.addEdge(v3, v3);
jacint@494
    96
  Edge s_z=flow_test.addEdge(s, z);
jacint@494
    97
  //  Edge v2_s=flow_test.addEdge(v2, s);
jacint@494
    98
  
jacint@494
    99
jacint@494
   100
jacint@494
   101
   ListGraph::EdgeMap<int> cap(flow_test);  
jacint@494
   102
  cap.set(s_v1, 16);
jacint@494
   103
  cap.set(s_v2, 13);
jacint@494
   104
  cap.set(v1_v2, 10);
jacint@494
   105
  cap.set(v2_v1, 4);
jacint@494
   106
  cap.set(v1_v3, 12);
jacint@494
   107
  cap.set(v3_v2, 9);
jacint@494
   108
  cap.set(v2_v4, 14);
jacint@494
   109
  cap.set(v4_v3, 7);
jacint@494
   110
  cap.set(v3_t, 20);
jacint@494
   111
  cap.set(v4_t, 4);
jacint@494
   112
  cap.set(v3_v3, 4);
jacint@494
   113
  cap.set(s_z, 4);
jacint@494
   114
  //  cap.set(v2_s, 0);
jacint@494
   115
jacint@494
   116
  */
jacint@494
   117
jacint@494
   118
  //pelda 3, maxflowvalue=4
jacint@494
   119
  /*      Node s=flow_test.addNode();
jacint@494
   120
  Node v1=flow_test.addNode();
jacint@494
   121
  Node v2=flow_test.addNode();
jacint@494
   122
  Node t=flow_test.addNode();
jacint@494
   123
  Node w=flow_test.addNode();
jacint@494
   124
  
jacint@494
   125
  NodeMap<ListGraph, std::string> Node_name(flow_test);
jacint@494
   126
  Node_name.set(s, "s");
jacint@494
   127
  Node_name.set(v1, "v1");
jacint@494
   128
  Node_name.set(v2, "v2");
jacint@494
   129
  Node_name.set(t, "t");
jacint@494
   130
  Node_name.set(w, "w");
jacint@494
   131
jacint@494
   132
  Edge s_v1=flow_test.addEdge(s, v1);
jacint@494
   133
  Edge v1_v2=flow_test.addEdge(v1, v2);
jacint@494
   134
  Edge v2_t=flow_test.addEdge(v2, t);
jacint@494
   135
  Edge v1_v1=flow_test.addEdge(v1, v1);
jacint@494
   136
  Edge s_w=flow_test.addEdge(s, w);
jacint@494
   137
jacint@494
   138
jacint@494
   139
  EdgeMap<ListGraph, int> cap(flow_test); 
jacint@494
   140
    
jacint@494
   141
  cap.set(s_v1, 16);
jacint@494
   142
  cap.set(v1_v2, 10);
jacint@494
   143
  cap.set(v2_t, 4);
jacint@494
   144
  cap.set(v1_v1, 3);
jacint@494
   145
  cap.set(s_w, 5);
jacint@494
   146
  */
jacint@494
   147
  
jacint@494
   148
jacint@494
   149
jacint@494
   150
  /*
jacint@494
   151
  std::cout << "Testing reverse_bfs..." << std::endl;
jacint@494
   152
  
jacint@494
   153
  reverse_bfs<ListGraph> bfs_test(flow_test, t);
jacint@494
   154
jacint@494
   155
  bfs_test.run();
jacint@494
   156
jacint@494
   157
  for (EachNodeIt w=flow_test.first_Node(); w.valid(); ++w) {
jacint@494
   158
    std::cout <<"The distance of " << w << " is " << bfs_test.dist(w) <<std::endl;
jacint@494
   159
    }
jacint@494
   160
jacint@494
   161
  */
jacint@494
   162
jacint@494
   163
jacint@494
   164
  /*
jacint@494
   165
  std::cout << "Testing preflow_push_hl..." << std::endl;
jacint@494
   166
  
jacint@494
   167
  preflow_push_hl<ListGraph, int> preflow_push_test(flow_test, s, t, cap);
jacint@494
   168
jacint@494
   169
  preflow_push_test.run();
jacint@494
   170
jacint@494
   171
  std::cout << "Maximum flow value is: " << preflow_push_test.maxflow() << "."<<std::endl;
jacint@494
   172
jacint@494
   173
  std::cout<< "The flow on Edge s-v1 is "<< preflow_push_test.flowonEdge(s_v1) << "."<<std::endl;
jacint@494
   174
jacint@494
   175
   ListGraph::EdgeMap<int> flow=preflow_push_test.allflow();  
jacint@494
   176
  for (EachEdgeIt e=flow_test.template first<EachEdgeIt>(); e.valid(); ++e) {
jacint@494
   177
    std::cout <<"Flow on Edge " << flow_test.tail(e) <<"-" << flow_test.head(e)<< " is " <<flow.get(e) <<std::endl;
jacint@494
   178
    }
jacint@494
   179
jacint@494
   180
  std::cout << "A minimum cut: " <<std::endl;  
jacint@494
   181
   ListGraph::NodeMap<bool> mincut=preflow_push_test.mincut();
jacint@494
   182
jacint@494
   183
  for (EachNodeIt v=flow_test.template first<EachNodeIt>(); v.valid(); ++v) {
jacint@494
   184
      if (mincut.get(v)) std::cout <<Node_name.get(v)<< " ";
jacint@494
   185
    }
jacint@494
   186
  
jacint@494
   187
  std::cout<<"\n\n"<<std::endl;
jacint@494
   188
jacint@494
   189
jacint@494
   190
jacint@494
   191
jacint@494
   192
  std::cout << "Testing preflow_push_max_flow..." << std::endl;
jacint@494
   193
 
jacint@494
   194
  preflow_push_max_flow<ListGraph, int> max_flow_test(flow_test, s, t, cap);
jacint@494
   195
jacint@494
   196
  max_flow_test.run();
jacint@494
   197
jacint@494
   198
  std::cout << "Maximum flow value is: " << max_flow_test.maxflow() << "."<< std::endl;
jacint@494
   199
jacint@494
   200
  std::cout << "A minimum cut: " <<std::endl;  
jacint@494
   201
   ListGraph::NodeMap<bool> mincut2=max_flow_test.mincut();
jacint@494
   202
jacint@494
   203
  for (EachNodeIt v=flow_test.template first<EachNodeIt>(); v.valid(); ++v) {
jacint@494
   204
    if (mincut2.get(v)) std::cout <<Node_name.get(v)<< " ";
jacint@494
   205
  }
jacint@494
   206
  
jacint@494
   207
  std::cout << std::endl <<std::endl;
jacint@494
   208
  */
jacint@494
   209
jacint@494
   210
  return 0;
jacint@494
   211
}
jacint@494
   212
jacint@494
   213
jacint@494
   214
jacint@494
   215
jacint@494
   216
jacint@494
   217
jacint@494
   218
jacint@494
   219
jacint@494
   220