*** empty log message ***
authorjacint
Tue, 09 Mar 2004 15:53:19 +0000
changeset 160f1a7005e9dff
parent 159 0defa5aa1229
child 161 743fa50c442e
*** empty log message ***
src/work/jacint/dijkstra.cc
src/work/jacint/dijkstra.h
     1.1 --- a/src/work/jacint/dijkstra.cc	Tue Mar 09 15:32:40 2004 +0000
     1.2 +++ b/src/work/jacint/dijkstra.cc	Tue Mar 09 15:53:19 2004 +0000
     1.3 @@ -1,242 +1,37 @@
     1.4  #include <iostream>
     1.5 -#include <vector>
     1.6 -#include <string>
     1.7 +#include <fstream>
     1.8  
     1.9  #include <list_graph.hh>
    1.10 +#include <dimacs.hh>
    1.11  #include <dijkstra.h>
    1.12 +#include <time_measure.h>
    1.13  
    1.14  using namespace hugo;
    1.15  
    1.16 -int main (int, char*[])
    1.17 -{
    1.18 +int main(int, char **) {
    1.19    typedef ListGraph::NodeIt NodeIt;
    1.20 -  typedef ListGraph::EdgeIt EdgeIt;
    1.21 -  typedef ListGraph::EachNodeIt EachNodeIt;
    1.22    typedef ListGraph::EachEdgeIt EachEdgeIt;
    1.23 -  typedef ListGraph::OutEdgeIt OutEdgeIt;
    1.24 -  typedef ListGraph::InEdgeIt InEdgeIt;
    1.25 +
    1.26 +  ListGraph G;
    1.27 +  NodeIt s, t;
    1.28 +  ListGraph::EdgeMap<int> cap(G);
    1.29 +  readDimacsMaxFlow(std::cin, G, s, t, cap);
    1.30 +
    1.31 +  std::cout << "dijkstra demo ..." << std::endl;
    1.32    
    1.33 -  ListGraph flow_test;
    1.34 +  double pre_time=currTime();
    1.35 +    Dijkstra<ListGraph, int> dijkstra_test(G, s, cap);
    1.36 +  double post_time=currTime();
    1.37 +    
    1.38 +  std::cout << "running time: " << post_time-pre_time << " sec"<< std::endl; 
    1.39   
    1.40 -  /*    //Ahuja könyv példája, maxflowvalue=13
    1.41 -  NodeIt s=flow_test.addNode();
    1.42 -  NodeIt v1=flow_test.addNode();
    1.43 -  NodeIt v2=flow_test.addNode();
    1.44 -  NodeIt v3=flow_test.addNode();
    1.45 -  NodeIt v4=flow_test.addNode();
    1.46 -  NodeIt v5=flow_test.addNode();
    1.47 -  NodeIt t=flow_test.addNode();
    1.48 -  
    1.49 -  ListGraph::NodeMap<std::string> Node_name(flow_test);
    1.50 -  Node_name.set(s, "s");
    1.51 -  Node_name.set(v1, "v1");
    1.52 -  Node_name.set(v2, "v2");
    1.53 -  Node_name.set(v3, "v3");
    1.54 -  Node_name.set(v4, "v4");
    1.55 -  Node_name.set(v5, "v5");
    1.56 -  Node_name.set(t, "t");
    1.57 +  EachEdgeIt e;
    1.58  
    1.59 -  EdgeIt s_v1=flow_test.addEdge(s, v1);
    1.60 -  EdgeIt s_v2=flow_test.addEdge(s, v2);
    1.61 -  EdgeIt s_v3=flow_test.addEdge(s, v3);
    1.62 -  EdgeIt v2_v4=flow_test.addEdge(v2, v4);
    1.63 -  EdgeIt v2_v5=flow_test.addEdge(v2, v5);
    1.64 -  EdgeIt v3_v5=flow_test.addEdge(v3, v5);
    1.65 -  EdgeIt v4_t=flow_test.addEdge(v4, t);
    1.66 -  EdgeIt v5_t=flow_test.addEdge(v5, t);
    1.67 -  EdgeIt v2_s=flow_test.addEdge(v2, s);
    1.68 -  
    1.69 -   ListGraph::EdgeMap<int> cap(flow_test);  
    1.70 -  cap.set(s_v1, 0);
    1.71 -  cap.set(s_v2, 10);
    1.72 -  cap.set(s_v3, 10);
    1.73 -  cap.set(v2_v4, 5);
    1.74 -  cap.set(v2_v5, 8);
    1.75 -  cap.set(v3_v5, 5);
    1.76 -  cap.set(v4_t, 8);
    1.77 -  cap.set(v5_t, 8);
    1.78 -  cap.set(v2_s, 0);
    1.79 -  */
    1.80 -
    1.81 -  
    1.82 -  //Marci példája, maxflowvalue=23
    1.83 -    NodeIt s=flow_test.addNode();
    1.84 -  NodeIt v1=flow_test.addNode();
    1.85 -  NodeIt v2=flow_test.addNode();
    1.86 -  NodeIt v3=flow_test.addNode();
    1.87 -  NodeIt v4=flow_test.addNode();
    1.88 -  NodeIt t=flow_test.addNode();
    1.89 -  NodeIt z=flow_test.addNode();
    1.90 -
    1.91 -  
    1.92 -   ListGraph::NodeMap<std::string> Node_name(flow_test);
    1.93 -  Node_name.set(s, "s");
    1.94 -  Node_name.set(v1, "v1");
    1.95 -  Node_name.set(v2, "v2");
    1.96 -  Node_name.set(v3, "v3");
    1.97 -  Node_name.set(v4, "v4");
    1.98 -  Node_name.set(t, "t");
    1.99 -  Node_name.set(z, "z");
   1.100 -
   1.101 -  EdgeIt s_v1=flow_test.addEdge(s, v1);
   1.102 -  EdgeIt s_v2=flow_test.addEdge(s, v2);
   1.103 -  EdgeIt v1_v2=flow_test.addEdge(v1, v2);
   1.104 -  EdgeIt v2_v1=flow_test.addEdge(v2, v1);
   1.105 -  EdgeIt v1_v3=flow_test.addEdge(v1, v3);
   1.106 -  EdgeIt v3_v2=flow_test.addEdge(v3, v2);
   1.107 -  EdgeIt v2_v4=flow_test.addEdge(v2, v4);
   1.108 -  EdgeIt v4_v3=flow_test.addEdge(v4, v3);
   1.109 -  EdgeIt v3_t=flow_test.addEdge(v3, t);
   1.110 -  EdgeIt v4_t=flow_test.addEdge(v4, t);
   1.111 -  EdgeIt v3_v3=flow_test.addEdge(v3, v3);
   1.112 -  EdgeIt s_z=flow_test.addEdge(s, z);
   1.113 -  //  EdgeIt v2_s=flow_test.addEdge(v2, s);
   1.114 -  
   1.115 -
   1.116 -
   1.117 -   ListGraph::EdgeMap<int> cap(flow_test);  
   1.118 -  cap.set(s_v1, 16);
   1.119 -  cap.set(s_v2, 13);
   1.120 -  cap.set(v1_v2, 10);
   1.121 -  cap.set(v2_v1, 4);
   1.122 -  cap.set(v1_v3, 12);
   1.123 -  cap.set(v3_v2, 9);
   1.124 -  cap.set(v2_v4, 14);
   1.125 -  cap.set(v4_v3, 7);
   1.126 -  cap.set(v3_t, 20);
   1.127 -  cap.set(v4_t, 4);
   1.128 -  cap.set(v3_v3, 4);
   1.129 -  cap.set(s_z, 4);
   1.130 -  //  cap.set(v2_s, 0);
   1.131 -
   1.132 -
   1.133 -
   1.134 -  //pelda 3, maxflowvalue=4
   1.135 -  /*      NodeIt s=flow_test.addNode();
   1.136 -  NodeIt v1=flow_test.addNode();
   1.137 -  NodeIt v2=flow_test.addNode();
   1.138 -  NodeIt t=flow_test.addNode();
   1.139 -  NodeIt w=flow_test.addNode();
   1.140 -  
   1.141 -  NodeMap<ListGraph, std::string> Node_name(flow_test);
   1.142 -  Node_name.set(s, "s");
   1.143 -  Node_name.set(v1, "v1");
   1.144 -  Node_name.set(v2, "v2");
   1.145 -  Node_name.set(t, "t");
   1.146 -  Node_name.set(w, "w");
   1.147 -
   1.148 -  EdgeIt s_v1=flow_test.addEdge(s, v1);
   1.149 -  EdgeIt v1_v2=flow_test.addEdge(v1, v2);
   1.150 -  EdgeIt v2_t=flow_test.addEdge(v2, t);
   1.151 -  EdgeIt v1_v1=flow_test.addEdge(v1, v1);
   1.152 -  EdgeIt s_w=flow_test.addEdge(s, w);
   1.153 -
   1.154 -
   1.155 -  EdgeMap<ListGraph, int> cap(flow_test); 
   1.156 -    
   1.157 -  cap.set(s_v1, 16);
   1.158 -  cap.set(v1_v2, 10);
   1.159 -  cap.set(v2_t, 4);
   1.160 -  cap.set(v1_v1, 3);
   1.161 -  cap.set(s_w, 5);
   1.162 -  */
   1.163 -  
   1.164 -
   1.165 -
   1.166 -  /*
   1.167 -  std::cout << "Testing reverse_bfs..." << std::endl;
   1.168 -  
   1.169 -  reverse_bfs<ListGraph> bfs_test(flow_test, t);
   1.170 -
   1.171 -  bfs_test.run();
   1.172 -
   1.173 -  for (EachNodeIt w=flow_test.first_Node(); w.valid(); ++w) {
   1.174 -    std::cout <<"The distance of " << w << " is " << bfs_test.dist(w) <<std::endl;
   1.175 -    }
   1.176 -
   1.177 -  */
   1.178 -
   1.179 -
   1.180 -  /*
   1.181 -  std::cout << "Testing preflow_push_hl..." << std::endl;
   1.182 -  
   1.183 -  preflow_push_hl<ListGraph, int> preflow_push_test(flow_test, s, t, cap);
   1.184 -
   1.185 -  preflow_push_test.run();
   1.186 -
   1.187 -  std::cout << "Maximum flow value is: " << preflow_push_test.maxflow() << "."<<std::endl;
   1.188 -
   1.189 -  std::cout<< "The flow on Edge s-v1 is "<< preflow_push_test.flowonEdge(s_v1) << "."<<std::endl;
   1.190 -
   1.191 -   ListGraph::EdgeMap<int> flow=preflow_push_test.allflow();  
   1.192 -  for (EachEdgeIt e=flow_test.template first<EachEdgeIt>(); e.valid(); ++e) {
   1.193 -    std::cout <<"Flow on Edge " << flow_test.tail(e) <<"-" << flow_test.head(e)<< " is " <<flow.get(e) <<std::endl;
   1.194 -    }
   1.195 -
   1.196 -  std::cout << "A minimum cut: " <<std::endl;  
   1.197 -   ListGraph::NodeMap<bool> mincut=preflow_push_test.mincut();
   1.198 -
   1.199 -  for (EachNodeIt v=flow_test.template first<EachNodeIt>(); v.valid(); ++v) {
   1.200 -      if (mincut.get(v)) std::cout <<Node_name.get(v)<< " ";
   1.201 -    }
   1.202 -  
   1.203 -  std::cout<<"\n\n"<<std::endl;
   1.204 -
   1.205 -
   1.206 -
   1.207 -
   1.208 -  std::cout << "Testing preflow_push_max_flow..." << std::endl;
   1.209 - 
   1.210 -  preflow_push_max_flow<ListGraph, int> max_flow_test(flow_test, s, t, cap);
   1.211 -
   1.212 -  max_flow_test.run();
   1.213 -
   1.214 -  std::cout << "Maximum flow value is: " << max_flow_test.maxflow() << "."<< std::endl;
   1.215 -
   1.216 -  std::cout << "A minimum cut: " <<std::endl;  
   1.217 -   ListGraph::NodeMap<bool> mincut2=max_flow_test.mincut();
   1.218 -
   1.219 -  for (EachNodeIt v=flow_test.template first<EachNodeIt>(); v.valid(); ++v) {
   1.220 -    if (mincut2.get(v)) std::cout <<Node_name.get(v)<< " ";
   1.221 +  for ( G.getFirst(e) ; G.valid(e); G.next(e) ) {
   1.222 +    NodeIt u=G.tail(e);
   1.223 +    NodeIt v=G.head(e);
   1.224 +    assert ( dijkstra_test.dist(v) - dijkstra_test.dist(u) <= cap.get(e) );
   1.225    }
   1.226 -  
   1.227 -  std::cout << std::endl <<std::endl;
   1.228 -  */
   1.229 -
   1.230 -  
   1.231 -    std::cout << "Testing dijkstra..." << std::endl;
   1.232 -  
   1.233 -    NodeIt root=s;
   1.234 -
   1.235 -    Dijkstra<ListGraph, int> dijkstra_test(flow_test, root, cap);
   1.236 -
   1.237 -    dijkstra_test.run();
   1.238 -
   1.239 -    EachNodeIt w;
   1.240 -
   1.241 -    for ( flow_test.getFirst(w); flow_test.valid(w) ; flow_test.next(w) ) {
   1.242 -      if (dijkstra_test.reach(w)) {
   1.243 -      std::cout <<"The distance of " << w << " is " << dijkstra_test.dist(w);
   1.244 -      if (dijkstra_test.pred(w).valid()) {
   1.245 -      std::cout <<", a shortest path from the root ends with edge " << dijkstra_test.pred(w) <<std::endl; 
   1.246 -      } else {
   1.247 -       std::cout <<", this is the root."<<std::endl; }
   1.248 -      
   1.249 -      } else {
   1.250 -	std::cout << w << " is not reachable from " << root <<std::endl;
   1.251 -      }
   1.252 -    }
   1.253 -
   1.254 -
   1.255  
   1.256    return 0;
   1.257  }
   1.258 -
   1.259 -
   1.260 -
   1.261 -
   1.262 -
   1.263 -
   1.264 -
   1.265 -
   1.266 -
     2.1 --- a/src/work/jacint/dijkstra.h	Tue Mar 09 15:32:40 2004 +0000
     2.2 +++ b/src/work/jacint/dijkstra.h	Tue Mar 09 15:53:19 2004 +0000
     2.3 @@ -81,7 +81,7 @@
     2.4  	  
     2.5  	  OutEdgeIt e;
     2.6  	  for( G.getFirst(e,v); G.valid(e); G.next(e)) {
     2.7 -	    NodeIt w=G.head(e); 
     2.8 +	    NodeIt w=G.bNode(e); 
     2.9  	    
    2.10  	    if ( !scanned.get(w) ) {
    2.11  	      if ( !reached.get(w) ) {