Primitive Dijkstra with stl priority queue. flow_test.cc is for testing flows and Dijkstra.
authorjacint
Fri, 23 Jan 2004 22:26:13 +0000
changeset 33f505c414feb9
parent 32 82f6987d6d2e
child 34 2b607665c9e2
Primitive Dijkstra with stl priority queue. flow_test.cc is for testing flows and Dijkstra.
src/work/dijkstra.hh
src/work/flow_test.cc
src/work/jacint_makefile
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/dijkstra.hh	Fri Jan 23 22:26:13 2004 +0000
     1.3 @@ -0,0 +1,192 @@
     1.4 +/*
     1.5 + *dijkstra
     1.6 + *by jacint
     1.7 + *Performs Dijkstra's algorithm from node s. 
     1.8 + *
     1.9 + *Constructor: 
    1.10 + *
    1.11 + *dijkstra(graph_type& G, node_iterator s, edge_property_vector& distance)
    1.12 + *
    1.13 + *
    1.14 + *
    1.15 + *Member functions:
    1.16 + *
    1.17 + *void run()
    1.18 + *
    1.19 + *  The following function should be used after run() was already run.
    1.20 + *
    1.21 + *
    1.22 + *T dist(node_iterator v) : returns the distance from s to v. 
    1.23 + *   It is 0 if v is not reachable from s.
    1.24 + *
    1.25 + *
    1.26 + *edge_iterator pred(node_iterator v)
    1.27 + *   Returns the last edge of a shortest s-v path. 
    1.28 + *   Returns an invalid iterator if v=s or v is not
    1.29 + *   reachable from s.
    1.30 + *
    1.31 + *
    1.32 + *bool reach(node_iterator v) : true if v is reachable from s
    1.33 + *
    1.34 + *
    1.35 + *
    1.36 + *
    1.37 + *
    1.38 + *Problems: 
    1.39 + * 
    1.40 + *Heap implementation is needed, because the priority queue of stl
    1.41 + *does not have a mathod for key-decrease, so we had to use here a 
    1.42 + *g\'any solution.
    1.43 + * 
    1.44 + *The implementation of infinity would be desirable, see after line 100. 
    1.45 + */
    1.46 +
    1.47 +#ifndef DIJKSTRA_HH
    1.48 +#define DIJKSTRA_HH
    1.49 +
    1.50 +#include <queue>
    1.51 +#include <algorithm>
    1.52 +
    1.53 +#include <marci_graph_traits.hh>
    1.54 +#include <marci_property_vector.hh>
    1.55 +
    1.56 +
    1.57 +namespace std {
    1.58 +  namespace marci {
    1.59 +
    1.60 +
    1.61 +
    1.62 +
    1.63 +
    1.64 +    template <typename graph_type, typename T>
    1.65 +    class dijkstra{
    1.66 +      typedef typename graph_traits<graph_type>::node_iterator node_iterator;
    1.67 +      typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
    1.68 +      typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
    1.69 +      typedef typename graph_traits<graph_type>::in_edge_iterator in_edge_iterator;
    1.70 +      typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
    1.71 +      
    1.72 +      
    1.73 +      graph_type& G;
    1.74 +      node_iterator s;
    1.75 +      node_property_vector<graph_type, edge_iterator> predecessor;
    1.76 +      node_property_vector<graph_type, T> distance;
    1.77 +      edge_property_vector<graph_type, T> length;
    1.78 +      node_property_vector<graph_type, bool> reached;
    1.79 +          
    1.80 +  public :
    1.81 +
    1.82 +    /*
    1.83 +      The distance of all the nodes is 0.
    1.84 +    */
    1.85 +    dijkstra(graph_type& _G, node_iterator _s, edge_property_vector<graph_type, T>& _length) : 
    1.86 +      G(_G), s(_s), predecessor(G, 0), distance(G, 0), length(_length), reached(G, false) { }
    1.87 +    
    1.88 +
    1.89 +      
    1.90 +      /*By Misi.*/
    1.91 +      struct node_dist_comp
    1.92 +      {
    1.93 +	node_property_vector<graph_type, T> &d;
    1.94 +	node_dist_comp(node_property_vector<graph_type, T> &_d) : d(_d) {} 
    1.95 +	
    1.96 +	bool operator()(const node_iterator& u, const node_iterator& v) const 
    1.97 +	{ return d.get(u) < d.get(v); }
    1.98 +      };
    1.99 +
   1.100 +
   1.101 +      
   1.102 +      void run() {
   1.103 +	
   1.104 +	node_property_vector<graph_type, bool> scanned(G, false);
   1.105 +	std::priority_queue<node_iterator, vector<node_iterator>, node_dist_comp> 
   1.106 +	  heap(( node_dist_comp(distance) ));
   1.107 +      
   1.108 +	heap.push(s);
   1.109 +	reached.put(s, true);
   1.110 +
   1.111 +	while (!heap.empty()) {
   1.112 +
   1.113 +	  node_iterator v=heap.top();	
   1.114 +	  heap.pop();
   1.115 +
   1.116 +
   1.117 +	  if (!scanned.get(v)) {
   1.118 +	
   1.119 +	    for(out_edge_iterator e=G.first_out_edge(v); e.valid(); ++e) {
   1.120 +	      node_iterator w=G.head(e);
   1.121 +
   1.122 +	      if (!scanned.get(w)) {
   1.123 +		if (!reached.get(w)) {
   1.124 +		  reached.put(w,true);
   1.125 +		  distance.put(w, distance.get(v)-length.get(e));
   1.126 +		  predecessor.put(w,e);
   1.127 +		} else if (distance.get(v)-length.get(e)>distance.get(w)) {
   1.128 +		  distance.put(w, distance.get(v)-length.get(e));
   1.129 +		  predecessor.put(w,e);
   1.130 +		}
   1.131 +		
   1.132 +		heap.push(w);
   1.133 +	      
   1.134 +	      } 
   1.135 +
   1.136 +	    } 
   1.137 +	    scanned.put(v,true);
   1.138 +	    
   1.139 +	  } // if (!scanned.get(v))
   1.140 +	  
   1.141 +	  
   1.142 +	  
   1.143 +	} // while (!heap.empty())
   1.144 +
   1.145 +	
   1.146 +      } //void run()
   1.147 +      
   1.148 +      
   1.149 +      
   1.150 +
   1.151 +
   1.152 +      /*
   1.153 +       *Returns the distance of the node v.
   1.154 +       *It is 0 for the root and for the nodes not
   1.155 +       *reachable form the root.
   1.156 +       */      
   1.157 +      T dist(node_iterator v) {
   1.158 +	return -distance.get(v);
   1.159 +      }
   1.160 +
   1.161 +
   1.162 +
   1.163 +      /*
   1.164 +       *  Returns the last edge of a shortest s-v path. 
   1.165 +       *  Returns an invalid iterator if v=root or v is not
   1.166 +       *  reachable from the root.
   1.167 +       */      
   1.168 +      edge_iterator pred(node_iterator v) {
   1.169 +	if (v!=s) { return predecessor.get(v);}
   1.170 +	else {return edge_iterator();}
   1.171 +      }
   1.172 +     
   1.173 +
   1.174 +      
   1.175 +      bool reach(node_iterator v) {
   1.176 +	return reached.get(v);
   1.177 +      }
   1.178 +
   1.179 +
   1.180 +
   1.181 +
   1.182 +
   1.183 +
   1.184 +
   1.185 +
   1.186 +
   1.187 +    };// class dijkstra
   1.188 +
   1.189 +
   1.190 +
   1.191 +  } // namespace marci
   1.192 +}
   1.193 +#endif //DIJKSTRA_HH
   1.194 +
   1.195 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/work/flow_test.cc	Fri Jan 23 22:26:13 2004 +0000
     2.3 @@ -0,0 +1,247 @@
     2.4 +#include <iostream>
     2.5 +#include <vector>
     2.6 +#include <string>
     2.7 +
     2.8 +#include <marci_list_graph.hh>
     2.9 +#include <marci_graph_traits.hh>
    2.10 +#include <marci_property_vector.hh>
    2.11 +#include <preflow_push_hl.hh>
    2.12 +#include <preflow_push_max_flow.hh>
    2.13 +#include <reverse_bfs.hh>
    2.14 +#include <dijkstra.hh>
    2.15 +
    2.16 +using namespace marci;
    2.17 +
    2.18 +
    2.19 +int main (int, char*[])
    2.20 +{
    2.21 +  typedef graph_traits<list_graph>::node_iterator node_iterator;
    2.22 +  typedef graph_traits<list_graph>::edge_iterator edge_iterator;
    2.23 +  typedef graph_traits<list_graph>::each_node_iterator each_node_iterator;
    2.24 +  typedef graph_traits<list_graph>::each_edge_iterator each_edge_iterator;
    2.25 +  typedef graph_traits<list_graph>::out_edge_iterator out_edge_iterator;
    2.26 +  typedef graph_traits<list_graph>::in_edge_iterator in_edge_iterator;
    2.27 +  typedef graph_traits<list_graph>::sym_edge_iterator sym_edge_iterator;
    2.28 +
    2.29 +  list_graph flow_test;
    2.30 + 
    2.31 +    //Ahuja könyv példája, maxflowvalue=13
    2.32 +  node_iterator s=flow_test.add_node();
    2.33 +  node_iterator v1=flow_test.add_node();
    2.34 +  node_iterator v2=flow_test.add_node();
    2.35 +  node_iterator v3=flow_test.add_node();
    2.36 +  node_iterator v4=flow_test.add_node();
    2.37 +  node_iterator v5=flow_test.add_node();
    2.38 +  node_iterator t=flow_test.add_node();
    2.39 +  
    2.40 +  node_property_vector<list_graph, std::string> node_name(flow_test);
    2.41 +  node_name.put(s, "s");
    2.42 +  node_name.put(v1, "v1");
    2.43 +  node_name.put(v2, "v2");
    2.44 +  node_name.put(v3, "v3");
    2.45 +  node_name.put(v4, "v4");
    2.46 +  node_name.put(v5, "v5");
    2.47 +  node_name.put(t, "t");
    2.48 +
    2.49 +  edge_iterator s_v1=flow_test.add_edge(s, v1);
    2.50 +  edge_iterator s_v2=flow_test.add_edge(s, v2);
    2.51 +  edge_iterator s_v3=flow_test.add_edge(s, v3);
    2.52 +  edge_iterator v2_v4=flow_test.add_edge(v2, v4);
    2.53 +  edge_iterator v2_v5=flow_test.add_edge(v2, v5);
    2.54 +  edge_iterator v3_v5=flow_test.add_edge(v3, v5);
    2.55 +  edge_iterator v4_t=flow_test.add_edge(v4, t);
    2.56 +  edge_iterator v5_t=flow_test.add_edge(v5, t);
    2.57 +  edge_iterator v2_s=flow_test.add_edge(v2, s);
    2.58 +  
    2.59 +  edge_property_vector<list_graph, int> cap(flow_test);  
    2.60 +  cap.put(s_v1, 0);
    2.61 +  cap.put(s_v2, 10);
    2.62 +  cap.put(s_v3, 10);
    2.63 +  cap.put(v2_v4, 5);
    2.64 +  cap.put(v2_v5, 8);
    2.65 +  cap.put(v3_v5, 5);
    2.66 +  cap.put(v4_t, 8);
    2.67 +  cap.put(v5_t, 8);
    2.68 +  cap.put(v2_s, 0);
    2.69 +
    2.70 +
    2.71 +  
    2.72 +  //Marci példája, maxflowvalue=23
    2.73 +  /*  node_iterator s=flow_test.add_node();
    2.74 +  node_iterator v1=flow_test.add_node();
    2.75 +  node_iterator v2=flow_test.add_node();
    2.76 +  node_iterator v3=flow_test.add_node();
    2.77 +  node_iterator v4=flow_test.add_node();
    2.78 +  node_iterator t=flow_test.add_node();
    2.79 +  node_iterator w=flow_test.add_node();
    2.80 +
    2.81 +  
    2.82 +  node_property_vector<list_graph, std::string> node_name(flow_test);
    2.83 +  node_name.put(s, "s");
    2.84 +  node_name.put(v1, "v1");
    2.85 +  node_name.put(v2, "v2");
    2.86 +  node_name.put(v3, "v3");
    2.87 +  node_name.put(v4, "v4");
    2.88 +  node_name.put(t, "t");
    2.89 +  node_name.put(w, "w");
    2.90 +
    2.91 +  edge_iterator s_v1=flow_test.add_edge(s, v1);
    2.92 +  edge_iterator s_v2=flow_test.add_edge(s, v2);
    2.93 +  edge_iterator v1_v2=flow_test.add_edge(v1, v2);
    2.94 +  edge_iterator v2_v1=flow_test.add_edge(v2, v1);
    2.95 +  edge_iterator v1_v3=flow_test.add_edge(v1, v3);
    2.96 +  edge_iterator v3_v2=flow_test.add_edge(v3, v2);
    2.97 +  edge_iterator v2_v4=flow_test.add_edge(v2, v4);
    2.98 +  edge_iterator v4_v3=flow_test.add_edge(v4, v3);
    2.99 +  edge_iterator v3_t=flow_test.add_edge(v3, t);
   2.100 +  edge_iterator v4_t=flow_test.add_edge(v4, t);
   2.101 +  edge_iterator v3_v3=flow_test.add_edge(v3, v3);
   2.102 +  edge_iterator s_w=flow_test.add_edge(s, w);
   2.103 +  //  edge_iterator v2_s=flow_test.add_edge(v2, s);
   2.104 +  
   2.105 +
   2.106 +
   2.107 +  edge_property_vector<list_graph, int> cap(flow_test);  //serves as length in dijkstra
   2.108 +  cap.put(s_v1, 16);
   2.109 +  cap.put(s_v2, 13);
   2.110 +  cap.put(v1_v2, 10);
   2.111 +  cap.put(v2_v1, 4);
   2.112 +  cap.put(v1_v3, 12);
   2.113 +  cap.put(v3_v2, 9);
   2.114 +  cap.put(v2_v4, 14);
   2.115 +  cap.put(v4_v3, 7);
   2.116 +  cap.put(v3_t, 20);
   2.117 +  cap.put(v4_t, 4);
   2.118 +  cap.put(v3_v3, 4);
   2.119 +  cap.put(s_w, 4);
   2.120 +  //  cap.put(v2_s, 0);
   2.121 +
   2.122 +*/
   2.123 +
   2.124 +  //pelda 3, maxflowvalue=4
   2.125 +  /*      node_iterator s=flow_test.add_node();
   2.126 +  node_iterator v1=flow_test.add_node();
   2.127 +  node_iterator v2=flow_test.add_node();
   2.128 +  node_iterator t=flow_test.add_node();
   2.129 +  node_iterator w=flow_test.add_node();
   2.130 +  
   2.131 +  node_property_vector<list_graph, std::string> node_name(flow_test);
   2.132 +  node_name.put(s, "s");
   2.133 +  node_name.put(v1, "v1");
   2.134 +  node_name.put(v2, "v2");
   2.135 +  node_name.put(t, "t");
   2.136 +  node_name.put(w, "w");
   2.137 +
   2.138 +  edge_iterator s_v1=flow_test.add_edge(s, v1);
   2.139 +  edge_iterator v1_v2=flow_test.add_edge(v1, v2);
   2.140 +  edge_iterator v2_t=flow_test.add_edge(v2, t);
   2.141 +  edge_iterator v1_v1=flow_test.add_edge(v1, v1);
   2.142 +  edge_iterator s_w=flow_test.add_edge(s, w);
   2.143 +
   2.144 +
   2.145 +  edge_property_vector<list_graph, int> cap(flow_test); 
   2.146 +    
   2.147 +  cap.put(s_v1, 16);
   2.148 +  cap.put(v1_v2, 10);
   2.149 +  cap.put(v2_t, 4);
   2.150 +  cap.put(v1_v1, 3);
   2.151 +  cap.put(s_w, 5);
   2.152 +  */
   2.153 +  
   2.154 +
   2.155 +
   2.156 +
   2.157 +  std::cout << "Testing reverse_bfs..." << std::endl;
   2.158 +  
   2.159 +  reverse_bfs<list_graph> bfs_test(flow_test, t);
   2.160 +
   2.161 +  bfs_test.run();
   2.162 +
   2.163 +  for (each_node_iterator w=flow_test.first_node(); w.valid(); ++w) {
   2.164 +    std::cout <<"The distance of " << w << " is " << bfs_test.dist(w) <<std::endl;
   2.165 +    }
   2.166 +
   2.167 +
   2.168 +
   2.169 +
   2.170 +
   2.171 +  std::cout << "Testing preflow_push_hl..." << std::endl;
   2.172 +  
   2.173 +  preflow_push_hl<list_graph, int> preflow_push_test(flow_test, s, t, cap);
   2.174 +
   2.175 +  preflow_push_test.run();
   2.176 +
   2.177 +  std::cout << "Maximum flow value is: " << preflow_push_test.maxflow() << "."<<std::endl;
   2.178 +
   2.179 +  std::cout<< "The flow on edge s-v1 is "<< preflow_push_test.flowonedge(s_v1) << "."<<std::endl;
   2.180 +
   2.181 +  edge_property_vector<list_graph, int> flow=preflow_push_test.allflow();  
   2.182 +  for (each_edge_iterator e=flow_test.first_edge(); e.valid(); ++e) {
   2.183 +    std::cout <<"Flow on edge " << flow_test.tail(e) <<"-" << flow_test.head(e)<< " is " <<flow.get(e) <<std::endl;
   2.184 +    }
   2.185 +
   2.186 +  std::cout << "A minimum cut: " <<std::endl;  
   2.187 +  node_property_vector<list_graph, bool> mincut=preflow_push_test.mincut();
   2.188 +
   2.189 +  for (each_node_iterator v=flow_test.first_node(); v.valid(); ++v) {
   2.190 +      if (mincut.get(v)) std::cout <<node_name.get(v)<< " ";
   2.191 +    }
   2.192 +  
   2.193 +  std::cout<<"\n\n"<<std::endl;
   2.194 +
   2.195 +
   2.196 +
   2.197 +
   2.198 +  std::cout << "Testing preflow_push_max_flow..." << std::endl;
   2.199 + 
   2.200 +  preflow_push_max_flow<list_graph, int> max_flow_test(flow_test, s, t, cap);
   2.201 +
   2.202 +  max_flow_test.run();
   2.203 +
   2.204 +  std::cout << "Maximum flow value is: " << max_flow_test.maxflow() << "."<< std::endl;
   2.205 +
   2.206 +  std::cout << "A minimum cut: " <<std::endl;  
   2.207 +  node_property_vector<list_graph, bool> mincut2=max_flow_test.mincut();
   2.208 +
   2.209 +  for (each_node_iterator v=flow_test.first_node(); v.valid(); ++v) {
   2.210 +    if (mincut2.get(v)) std::cout <<node_name.get(v)<< " ";
   2.211 +  }
   2.212 +  
   2.213 +  std::cout << std::endl <<std::endl;
   2.214 +
   2.215 +
   2.216 +
   2.217 +    std::cout << "Testing dijkstra..." << std::endl;
   2.218 +  
   2.219 +    node_iterator root=v2;
   2.220 +
   2.221 +    dijkstra<list_graph, int> dijkstra_test(flow_test, root, cap);
   2.222 +
   2.223 +    dijkstra_test.run();
   2.224 +
   2.225 +    for (each_node_iterator w=flow_test.first_node(); w.valid(); ++w) {
   2.226 +      if (dijkstra_test.reach(w)) {
   2.227 +      std::cout <<"The distance of " << w << " is " << dijkstra_test.dist(w);
   2.228 +      if (dijkstra_test.pred(w).valid()) {
   2.229 +      std::cout <<", a shortest path from the root ends with edge " << dijkstra_test.pred(w) <<std::endl; 
   2.230 +      } else {
   2.231 +       std::cout <<", this is the root."<<std::endl; }
   2.232 +      
   2.233 +      } else {
   2.234 +	cout << w << " is not reachable from " << root <<std::endl;
   2.235 +      }
   2.236 +    }
   2.237 +
   2.238 +
   2.239 +
   2.240 +  return 0;
   2.241 +}
   2.242 +
   2.243 +
   2.244 +
   2.245 +
   2.246 +
   2.247 +
   2.248 +
   2.249 +
   2.250 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/work/jacint_makefile	Fri Jan 23 22:26:13 2004 +0000
     3.3 @@ -0,0 +1,9 @@
     3.4 +CCFLAGS = -Wall -ansi
     3.5 +CXXFLAGS = -Wall -ansi -I. -g
     3.6 +CC = g++-3.0
     3.7 +
     3.8 +flow_test: flow_test.cc marci_list_graph.hh reverse_bfs.hh preflow_push_hl.hh marci_graph_traits.hh marci_property_vector.hh preflow_push_max_flow.hh dijkstra.hh
     3.9 +	$(CXX) $(CXXFLAGS) -o flow_test flow_test.cc
    3.10 +
    3.11 +marci_graph_demo: marci_graph_demo.cc marci_graph_traits.hh marci_list_graph.hh marci_property_vector.hh marci_bfs.hh marci_max_flow.hh
    3.12 +	$(CC) $(CCFLAGS) -I. marci_graph_demo.cc -o marci_graph_demo