3 #ifndef EDMONDS_KARP_HH
4 #define EDMONDS_KARP_HH
8 //#include <marci_property_vector.hh>
12 //#include <bfs_iterator.hh>
15 template <typename Graph, typename FlowMap, typename CapacityMap>
16 typename FlowMap::ValueType maxFlow(Graph &G,
19 typename Graph::NodeIt s,
20 typename Graph::NodeIt t)
22 typedef typename Graph::NodeIt NodeIt;
23 typedef typename Graph::EdgeIt EdgeIt;
24 typedef typename Graph::EachEdgeIt EachEdgeIt;
25 typedef typename Graph::OutEdgeIt OutEdgeIt;
26 typedef typename Graph::InEdgeIt InEdgeIt;
27 typedef typename FlowMap::ValueType T;
32 for(EachEdgeIt e(G);G.valid(e);G.next(e))
35 std::queue<NodeIt> bfs_queue;
36 typename Graph::NodeMap<int> visited(G); //0: unvisited,
37 //1: reached by a forward edge
38 //2: reached by a backward edge
40 typename Graph::NodeMap<EdgeIt> tree(G);
42 NodeIt gn; //FIXME: it might be too global for some people...
46 for(NodeIt n(G);G.valid(n);G.next(n))
51 //There must be a better way to do this:
52 while(!bfs_queue.empty()) bfs_queue.pop();
56 while(!bfs_queue.empty() && !visited.get(t))
58 NodeIt n(bfs_queue.front());
59 for(OutEdgeIt e(G,n);G.valid(e);G.next(e))
60 if(f.get(e)<c.get(e) && //FIXME: <
61 !visited.get(G.bNode(e)))
63 tree.set(G.bNode(e),e);
64 visited.set(G.bNode(e),1);
66 for(InEdgeIt e(G,n);G.valid(e);G.next(e))
67 if(f.get(e)>0 && //FIXME: >
68 !visited.get(G.bNode(e)))
70 tree.set(G.bNode(e),e);
71 visited.set(G.bNode(e),2);
75 if(!visited.get(t)) return flow_val;
77 // Augmenting value computation
78 aug_val = visited.get(t)==1 ?
79 c.get(tree.get(t))-f.get(tree.get(t)) : f.get(tree.get(t));
80 //FIXME: I would need 'G.opposite(e,n)'
81 gn = visited.get(t)==1 ? G.tail(tree.get(t)) : G.head(tree.get(t));
82 while(gn!=s) if(visited.get(gn)==1)
84 //FIXME: nonstandars. gcc extension!
85 aug_val <?= c.get(tree.get(gn))-f.get(tree.get(gn));
86 gn=G.tail(tree.get(gn));
89 //FIXME: nonstandars. gcc extension!
90 aug_val <?= f.get(tree.get(gn));
91 gn=G.head(tree.get(gn));
94 // The augmentation itself
96 while(gn!=s) if(visited.get(gn)==1)
98 f.set(tree.get(gn),f.get(tree.get(gn))+aug_val);
99 gn=G.tail(tree.get(gn));
102 f.set(tree.get(gn),f.get(tree.get(gn))-aug_val);
103 gn=G.head(tree.get(gn));
108 goto augment; // Vivat goto forever!
113 #endif //EDMONDS_KARP_HH