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