src/work/marci/oldies/marci_max_flow.hh
author jacint
Thu, 13 May 2004 10:29:13 +0000
changeset 629 6620dfc606af
permissions -rw-r--r--
max_flow interface changes
marci@280
     1
#ifndef MARCI_MAX_FLOW_HH
marci@280
     2
#define MARCI_MAX_FLOW_HH
marci@280
     3
marci@280
     4
#include <algorithm>
marci@280
     5
marci@280
     6
#include <marci_property_vector.hh>
marci@280
     7
#include <marci_bfs.hh>
marci@280
     8
marci@280
     9
namespace hugo {
marci@280
    10
marci@280
    11
  template<typename graph_type, typename T>
marci@280
    12
  class res_graph_type { 
marci@280
    13
    typedef typename graph_type::node_iterator node_iterator;
marci@280
    14
    typedef typename graph_type::each_node_iterator each_node_iterator;
marci@280
    15
    typedef typename graph_type::sym_edge_iterator old_sym_edge_iterator;
marci@280
    16
    graph_type& G;
marci@280
    17
    edge_property_vector<graph_type, T>& flow;
marci@280
    18
    edge_property_vector<graph_type, T>& capacity;
marci@280
    19
  public:
marci@280
    20
    res_graph_type(graph_type& _G, edge_property_vector<graph_type, T>& _flow, edge_property_vector<graph_type, T>& _capacity) : G(_G), flow(_flow), capacity(_capacity) { }
marci@280
    21
marci@280
    22
    class edge_iterator {
marci@280
    23
      friend class res_graph_type<graph_type, T>;
marci@280
    24
    protected:
marci@280
    25
      res_graph_type<graph_type, T>* resG;
marci@280
    26
      old_sym_edge_iterator sym;
marci@280
    27
    public:
marci@280
    28
      edge_iterator() { }
marci@280
    29
      //bool is_free() {  
marci@280
    30
      //if (resG->G.a_node(sym)==resG->G.tail(sym)) { 
marci@280
    31
      //  return (resG->flow.get(sym)<resG->capacity.get(sym)); 
marci@280
    32
      //} else { 
marci@280
    33
      //  return (resG->flow.get(sym)>0); 
marci@280
    34
      //}
marci@280
    35
      //}
marci@280
    36
      T free() { 
marci@280
    37
	if (resG->G.a_node(sym)==resG->G.tail(sym)) { 
marci@280
    38
	  return (resG->capacity.get(sym)-resG->flow.get(sym)); 
marci@280
    39
	} else { 
marci@280
    40
	  return (resG->flow.get(sym)); 
marci@280
    41
	}
marci@280
    42
      }
marci@280
    43
      bool valid() { return sym.valid(); }
marci@280
    44
      void make_invalid() { sym.make_invalid(); }
marci@280
    45
      void augment(T a) {
marci@280
    46
	if (resG->G.a_node(sym)==resG->G.tail(sym)) { 
marci@280
    47
	  resG->flow.put(sym, resG->flow.get(sym)+a);
marci@280
    48
	} else { 
marci@280
    49
	  resG->flow.put(sym, resG->flow.get(sym)-a);
marci@280
    50
	}
marci@280
    51
      }
marci@280
    52
    };
marci@280
    53
marci@280
    54
    class out_edge_iterator : public edge_iterator {
marci@280
    55
    public:
marci@280
    56
      out_edge_iterator() { }
marci@280
    57
      out_edge_iterator(res_graph_type<graph_type, T>& _resG, const node_iterator& v) { 
marci@280
    58
      	resG=&_resG;
marci@280
    59
	sym=resG->G.first_sym_edge(v);
marci@280
    60
	while( sym.valid() && !(free()>0) ) { ++sym; }
marci@280
    61
      }
marci@280
    62
      out_edge_iterator& operator++() { 
marci@280
    63
	++sym; 
marci@280
    64
	while( sym.valid() && !(free()>0) ) { ++sym; }
marci@280
    65
	return *this; 
marci@280
    66
      }
marci@280
    67
    };
marci@280
    68
marci@280
    69
    out_edge_iterator first_out_edge(const node_iterator& v) {
marci@280
    70
      return out_edge_iterator(*this, v);
marci@280
    71
    }
marci@280
    72
marci@280
    73
    each_node_iterator first_node() {
marci@280
    74
      return G.first_node();
marci@280
    75
    }
marci@280
    76
marci@280
    77
    node_iterator tail(const edge_iterator& e) { return G.a_node(e.sym); }
marci@280
    78
    node_iterator head(const edge_iterator& e) { return G.b_node(e.sym); }
marci@280
    79
marci@280
    80
    int id(const node_iterator& v) { return G.id(v); }
marci@280
    81
marci@280
    82
    //node_iterator invalid_node() { return G.invalid_node(); }
marci@280
    83
    //res_edge_it invalid_edge() { res_edge_it n; n.sym=G.invalid_sym_edge(); return n; } 
marci@280
    84
  };
marci@280
    85
marci@280
    86
  template <typename graph_type, typename T>
marci@280
    87
  struct max_flow_type {
marci@280
    88
    typedef typename graph_type::node_iterator node_iterator;
marci@280
    89
    typedef typename graph_type::edge_iterator edge_iterator;
marci@280
    90
    typedef typename graph_type::each_node_iterator each_node_iterator;
marci@280
    91
    typedef typename graph_type::out_edge_iterator out_edge_iterator;
marci@280
    92
    typedef typename graph_type::in_edge_iterator in_edge_iterator;
marci@280
    93
    graph_type& G;
marci@280
    94
    node_iterator s;
marci@280
    95
    node_iterator t;
marci@280
    96
    edge_property_vector<graph_type, T> flow;
marci@280
    97
    edge_property_vector<graph_type, T>& capacity;
marci@280
    98
marci@280
    99
    max_flow_type(graph_type& _G, node_iterator _s, node_iterator _t, edge_property_vector<graph_type, T>& _capacity) : G(_G), s(_s), t(_t), flow(_G), capacity(_capacity) { 
marci@280
   100
      for(each_node_iterator i=G.first_node(); i.valid(); ++i) 
marci@280
   101
	for(out_edge_iterator j=G.first_out_edge(i); j.valid(); ++j) 
marci@280
   102
	  flow.put(j, 0);
marci@280
   103
    }
marci@280
   104
    void run() {
marci@280
   105
      typedef res_graph_type<graph_type, T> aug_graph_type;
marci@280
   106
      aug_graph_type res_graph(G, flow, capacity);
marci@280
   107
marci@280
   108
      bool augment;
marci@280
   109
      do {
marci@280
   110
	augment=false;
marci@280
   111
marci@280
   112
	typedef std::queue<aug_graph_type::out_edge_iterator> bfs_queue_type;
marci@280
   113
	bfs_queue_type bfs_queue;
marci@280
   114
	bfs_queue.push(res_graph.first_out_edge(s));
marci@280
   115
marci@280
   116
	typedef node_property_vector<aug_graph_type, bool> reached_type;
marci@280
   117
	reached_type reached(res_graph, false);
marci@280
   118
	reached.put(s, true); 
marci@280
   119
	
marci@280
   120
	bfs_iterator1< aug_graph_type, reached_type > 
marci@280
   121
	res_bfs(res_graph, bfs_queue, reached);
marci@280
   122
marci@280
   123
	typedef node_property_vector<aug_graph_type, aug_graph_type::edge_iterator> pred_type;
marci@280
   124
	pred_type pred(res_graph);
marci@280
   125
	aug_graph_type::edge_iterator a; 
marci@280
   126
	a.make_invalid();
marci@280
   127
	pred.put(s, a);
marci@280
   128
marci@280
   129
	typedef node_property_vector<aug_graph_type, int> free_type;
marci@280
   130
	free_type free(res_graph);
marci@280
   131
	
marci@280
   132
	//searching for augmenting path
marci@280
   133
	while ( res_bfs.valid() ) { 
marci@280
   134
	  //std::cout<<"KULSO ciklus itt jar: "<<G.id(res_graph.tail(res_bfs))<<"->"<<G.id(res_graph.head(res_bfs))<<std::endl;
marci@280
   135
	  if (res_bfs.newly_reached()) {
marci@280
   136
	    aug_graph_type::edge_iterator e;
marci@280
   137
	    e=res_bfs;
marci@280
   138
	    node_iterator v=res_graph.tail(e);
marci@280
   139
	    node_iterator w=res_graph.head(e);
marci@280
   140
	    //std::cout<<G.id(v)<<"->"<<G.id(w)<<", "<<G.id(w)<<" is newly reached";
marci@280
   141
	    pred.put(w, e);
marci@280
   142
	    if (pred.get(v).valid()) {
marci@280
   143
	      free.put(w, std::min(free.get(v), e.free()));
marci@280
   144
	      //std::cout <<" nem elso csucs: ";
marci@280
   145
	      //std::cout <<"szabad kap eddig: "<< free.get(w) << " ";
marci@280
   146
	    } else {
marci@280
   147
	      free.put(w, e.free()); 
marci@280
   148
	      //std::cout <<" elso csucs: ";
marci@280
   149
	      //std::cout <<"szabad kap eddig: "<< free.get(w) << " ";
marci@280
   150
	    }
marci@280
   151
	    //std::cout<<std::endl;
marci@280
   152
	  }
marci@280
   153
	
marci@280
   154
	  if (res_graph.head(res_bfs)==t) break;
marci@280
   155
	  ++res_bfs;
marci@280
   156
	}
marci@280
   157
	if (reached.get(t)) {
marci@280
   158
	  augment=true;
marci@280
   159
	  node_iterator n=t;
marci@280
   160
	  T augment_value=free.get(t);
marci@280
   161
	  std::cout<<"augmentation: ";
marci@280
   162
	  while (pred.get(n).valid()) { 
marci@280
   163
	    aug_graph_type::edge_iterator e=pred.get(n);
marci@280
   164
	    e.augment(augment_value); 
marci@280
   165
	    std::cout<<"("<<res_graph.tail(e)<< "->"<<res_graph.head(e)<<") ";
marci@280
   166
	    n=res_graph.tail(e);
marci@280
   167
	  }
marci@280
   168
	  std::cout<<std::endl;
marci@280
   169
	}
marci@280
   170
marci@280
   171
	std::cout << "actual flow: "<< std::endl;
marci@280
   172
	for(typename graph_type::each_edge_iterator e=G.first_edge(); e.valid(); ++e) { 
marci@280
   173
	  std::cout<<"("<<G.tail(e)<< "-"<<flow.get(e)<<"->"<<G.head(e)<<") ";
marci@280
   174
	}
marci@280
   175
	std::cout<<std::endl;
marci@280
   176
marci@280
   177
      } while (augment);
marci@280
   178
    }
marci@280
   179
  };
marci@280
   180
marci@280
   181
} // namespace hugo
marci@280
   182
marci@280
   183
#endif //MARCI_MAX_FLOW_HH