# HG changeset patch # User jacint # Date 1074896773 0 # Node ID f505c414feb9c509847a170716296673e01e29cc # Parent 82f6987d6d2e5bf995a5b9c6cbd56e5a2ecb71a9 Primitive Dijkstra with stl priority queue. flow_test.cc is for testing flows and Dijkstra. diff -r 82f6987d6d2e -r f505c414feb9 src/work/dijkstra.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/dijkstra.hh Fri Jan 23 22:26:13 2004 +0000 @@ -0,0 +1,192 @@ +/* + *dijkstra + *by jacint + *Performs Dijkstra's algorithm from node s. + * + *Constructor: + * + *dijkstra(graph_type& G, node_iterator s, edge_property_vector& distance) + * + * + * + *Member functions: + * + *void run() + * + * The following function should be used after run() was already run. + * + * + *T dist(node_iterator v) : returns the distance from s to v. + * It is 0 if v is not reachable from s. + * + * + *edge_iterator pred(node_iterator v) + * Returns the last edge of a shortest s-v path. + * Returns an invalid iterator if v=s or v is not + * reachable from s. + * + * + *bool reach(node_iterator v) : true if v is reachable from s + * + * + * + * + * + *Problems: + * + *Heap implementation is needed, because the priority queue of stl + *does not have a mathod for key-decrease, so we had to use here a + *g\'any solution. + * + *The implementation of infinity would be desirable, see after line 100. + */ + +#ifndef DIJKSTRA_HH +#define DIJKSTRA_HH + +#include +#include + +#include +#include + + +namespace std { + namespace marci { + + + + + + template + class dijkstra{ + typedef typename graph_traits::node_iterator node_iterator; + typedef typename graph_traits::edge_iterator edge_iterator; + typedef typename graph_traits::each_node_iterator each_node_iterator; + typedef typename graph_traits::in_edge_iterator in_edge_iterator; + typedef typename graph_traits::out_edge_iterator out_edge_iterator; + + + graph_type& G; + node_iterator s; + node_property_vector predecessor; + node_property_vector distance; + edge_property_vector length; + node_property_vector reached; + + public : + + /* + The distance of all the nodes is 0. + */ + dijkstra(graph_type& _G, node_iterator _s, edge_property_vector& _length) : + G(_G), s(_s), predecessor(G, 0), distance(G, 0), length(_length), reached(G, false) { } + + + + /*By Misi.*/ + struct node_dist_comp + { + node_property_vector &d; + node_dist_comp(node_property_vector &_d) : d(_d) {} + + bool operator()(const node_iterator& u, const node_iterator& v) const + { return d.get(u) < d.get(v); } + }; + + + + void run() { + + node_property_vector scanned(G, false); + std::priority_queue, node_dist_comp> + heap(( node_dist_comp(distance) )); + + heap.push(s); + reached.put(s, true); + + while (!heap.empty()) { + + node_iterator v=heap.top(); + heap.pop(); + + + if (!scanned.get(v)) { + + for(out_edge_iterator e=G.first_out_edge(v); e.valid(); ++e) { + node_iterator w=G.head(e); + + if (!scanned.get(w)) { + if (!reached.get(w)) { + reached.put(w,true); + distance.put(w, distance.get(v)-length.get(e)); + predecessor.put(w,e); + } else if (distance.get(v)-length.get(e)>distance.get(w)) { + distance.put(w, distance.get(v)-length.get(e)); + predecessor.put(w,e); + } + + heap.push(w); + + } + + } + scanned.put(v,true); + + } // if (!scanned.get(v)) + + + + } // while (!heap.empty()) + + + } //void run() + + + + + + /* + *Returns the distance of the node v. + *It is 0 for the root and for the nodes not + *reachable form the root. + */ + T dist(node_iterator v) { + return -distance.get(v); + } + + + + /* + * Returns the last edge of a shortest s-v path. + * Returns an invalid iterator if v=root or v is not + * reachable from the root. + */ + edge_iterator pred(node_iterator v) { + if (v!=s) { return predecessor.get(v);} + else {return edge_iterator();} + } + + + + bool reach(node_iterator v) { + return reached.get(v); + } + + + + + + + + + + };// class dijkstra + + + + } // namespace marci +} +#endif //DIJKSTRA_HH + + diff -r 82f6987d6d2e -r f505c414feb9 src/work/flow_test.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/flow_test.cc Fri Jan 23 22:26:13 2004 +0000 @@ -0,0 +1,247 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +using namespace marci; + + +int main (int, char*[]) +{ + typedef graph_traits::node_iterator node_iterator; + typedef graph_traits::edge_iterator edge_iterator; + typedef graph_traits::each_node_iterator each_node_iterator; + typedef graph_traits::each_edge_iterator each_edge_iterator; + typedef graph_traits::out_edge_iterator out_edge_iterator; + typedef graph_traits::in_edge_iterator in_edge_iterator; + typedef graph_traits::sym_edge_iterator sym_edge_iterator; + + list_graph flow_test; + + //Ahuja könyv példája, maxflowvalue=13 + node_iterator s=flow_test.add_node(); + node_iterator v1=flow_test.add_node(); + node_iterator v2=flow_test.add_node(); + node_iterator v3=flow_test.add_node(); + node_iterator v4=flow_test.add_node(); + node_iterator v5=flow_test.add_node(); + node_iterator t=flow_test.add_node(); + + node_property_vector node_name(flow_test); + node_name.put(s, "s"); + node_name.put(v1, "v1"); + node_name.put(v2, "v2"); + node_name.put(v3, "v3"); + node_name.put(v4, "v4"); + node_name.put(v5, "v5"); + node_name.put(t, "t"); + + edge_iterator s_v1=flow_test.add_edge(s, v1); + edge_iterator s_v2=flow_test.add_edge(s, v2); + edge_iterator s_v3=flow_test.add_edge(s, v3); + edge_iterator v2_v4=flow_test.add_edge(v2, v4); + edge_iterator v2_v5=flow_test.add_edge(v2, v5); + edge_iterator v3_v5=flow_test.add_edge(v3, v5); + edge_iterator v4_t=flow_test.add_edge(v4, t); + edge_iterator v5_t=flow_test.add_edge(v5, t); + edge_iterator v2_s=flow_test.add_edge(v2, s); + + edge_property_vector cap(flow_test); + cap.put(s_v1, 0); + cap.put(s_v2, 10); + cap.put(s_v3, 10); + cap.put(v2_v4, 5); + cap.put(v2_v5, 8); + cap.put(v3_v5, 5); + cap.put(v4_t, 8); + cap.put(v5_t, 8); + cap.put(v2_s, 0); + + + + //Marci példája, maxflowvalue=23 + /* node_iterator s=flow_test.add_node(); + node_iterator v1=flow_test.add_node(); + node_iterator v2=flow_test.add_node(); + node_iterator v3=flow_test.add_node(); + node_iterator v4=flow_test.add_node(); + node_iterator t=flow_test.add_node(); + node_iterator w=flow_test.add_node(); + + + node_property_vector node_name(flow_test); + node_name.put(s, "s"); + node_name.put(v1, "v1"); + node_name.put(v2, "v2"); + node_name.put(v3, "v3"); + node_name.put(v4, "v4"); + node_name.put(t, "t"); + node_name.put(w, "w"); + + edge_iterator s_v1=flow_test.add_edge(s, v1); + edge_iterator s_v2=flow_test.add_edge(s, v2); + edge_iterator v1_v2=flow_test.add_edge(v1, v2); + edge_iterator v2_v1=flow_test.add_edge(v2, v1); + edge_iterator v1_v3=flow_test.add_edge(v1, v3); + edge_iterator v3_v2=flow_test.add_edge(v3, v2); + edge_iterator v2_v4=flow_test.add_edge(v2, v4); + edge_iterator v4_v3=flow_test.add_edge(v4, v3); + edge_iterator v3_t=flow_test.add_edge(v3, t); + edge_iterator v4_t=flow_test.add_edge(v4, t); + edge_iterator v3_v3=flow_test.add_edge(v3, v3); + edge_iterator s_w=flow_test.add_edge(s, w); + // edge_iterator v2_s=flow_test.add_edge(v2, s); + + + + edge_property_vector cap(flow_test); //serves as length in dijkstra + cap.put(s_v1, 16); + cap.put(s_v2, 13); + cap.put(v1_v2, 10); + cap.put(v2_v1, 4); + cap.put(v1_v3, 12); + cap.put(v3_v2, 9); + cap.put(v2_v4, 14); + cap.put(v4_v3, 7); + cap.put(v3_t, 20); + cap.put(v4_t, 4); + cap.put(v3_v3, 4); + cap.put(s_w, 4); + // cap.put(v2_s, 0); + +*/ + + //pelda 3, maxflowvalue=4 + /* node_iterator s=flow_test.add_node(); + node_iterator v1=flow_test.add_node(); + node_iterator v2=flow_test.add_node(); + node_iterator t=flow_test.add_node(); + node_iterator w=flow_test.add_node(); + + node_property_vector node_name(flow_test); + node_name.put(s, "s"); + node_name.put(v1, "v1"); + node_name.put(v2, "v2"); + node_name.put(t, "t"); + node_name.put(w, "w"); + + edge_iterator s_v1=flow_test.add_edge(s, v1); + edge_iterator v1_v2=flow_test.add_edge(v1, v2); + edge_iterator v2_t=flow_test.add_edge(v2, t); + edge_iterator v1_v1=flow_test.add_edge(v1, v1); + edge_iterator s_w=flow_test.add_edge(s, w); + + + edge_property_vector cap(flow_test); + + cap.put(s_v1, 16); + cap.put(v1_v2, 10); + cap.put(v2_t, 4); + cap.put(v1_v1, 3); + cap.put(s_w, 5); + */ + + + + + std::cout << "Testing reverse_bfs..." << std::endl; + + reverse_bfs bfs_test(flow_test, t); + + bfs_test.run(); + + for (each_node_iterator w=flow_test.first_node(); w.valid(); ++w) { + std::cout <<"The distance of " << w << " is " << bfs_test.dist(w) < preflow_push_test(flow_test, s, t, cap); + + preflow_push_test.run(); + + std::cout << "Maximum flow value is: " << preflow_push_test.maxflow() << "."< flow=preflow_push_test.allflow(); + for (each_edge_iterator e=flow_test.first_edge(); e.valid(); ++e) { + std::cout <<"Flow on edge " << flow_test.tail(e) <<"-" << flow_test.head(e)<< " is " < mincut=preflow_push_test.mincut(); + + for (each_node_iterator v=flow_test.first_node(); v.valid(); ++v) { + if (mincut.get(v)) std::cout < max_flow_test(flow_test, s, t, cap); + + max_flow_test.run(); + + std::cout << "Maximum flow value is: " << max_flow_test.maxflow() << "."<< std::endl; + + std::cout << "A minimum cut: " < mincut2=max_flow_test.mincut(); + + for (each_node_iterator v=flow_test.first_node(); v.valid(); ++v) { + if (mincut2.get(v)) std::cout < dijkstra_test(flow_test, root, cap); + + dijkstra_test.run(); + + for (each_node_iterator w=flow_test.first_node(); w.valid(); ++w) { + if (dijkstra_test.reach(w)) { + std::cout <<"The distance of " << w << " is " << dijkstra_test.dist(w); + if (dijkstra_test.pred(w).valid()) { + std::cout <<", a shortest path from the root ends with edge " << dijkstra_test.pred(w) <