Changeset 1025:3b1ad8bc21da in lemon-0.x for src
- Timestamp:
- 11/29/04 18:55:46 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1415
- Location:
- src/work/marci
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/work/marci/lp/max_flow_by_lp.cc
r1017 r1025 22 22 23 23 typedef ListGraph MutableGraph; 24 typedef SmartGraph Graph;24 typedef ListGraph Graph; 25 25 typedef Graph::Node Node; 26 26 typedef Graph::Edge Edge; … … 179 179 MinCostGenFlow<Graph, int, Excess, LCap> 180 180 min_cost(g, excess, lcap, cap, flow, cost); 181 min_cost.feasible(); 181 182 min_cost.run(); 182 183 -
src/work/marci/lp/min_cost_gen_flow.h
r1017 r1025 2 2 #ifndef LEMON_MIN_COST_GEN_FLOW_H 3 3 #define LEMON_MIN_COST_GEN_FLOW_H 4 //#include <iostream>4 #include <iostream> 5 5 //#include <fstream> 6 6 7 //#include <lemon/smart_graph.h>8 //#include <lemon/list_graph.h>7 #include <lemon/smart_graph.h> 8 #include <lemon/list_graph.h> 9 9 //#include <lemon/dimacs.h> 10 10 //#include <lemon/time_measure.h> 11 11 //#include <graph_wrapper.h> 12 //#include <lemon/preflow.h>12 #include <lemon/preflow.h> 13 13 //#include <augmenting_flow.h> 14 14 //#include <preflow_res.h> 15 #include <../merge_node_graph_wrapper.h> 15 16 #include <lemon/../work/marci/lp/lp_solver_wrapper.h> 16 17 … … 52 53 g(_g), excess(_excess), lcapacity(_lcapacity), 53 54 capacity(_capacity), flow(_flow), cost(_cost) { } 55 bool feasible() { 56 // std::cout << "making new vertices..." << std::endl; 57 typedef ListGraph Graph2; 58 Graph2 g2; 59 typedef MergeEdgeGraphWrapper<const Graph, Graph2> GW; 60 // std::cout << "merging..." << std::endl; 61 GW gw(g, g2); 62 typename GW::Node s(INVALID, g2.addNode(), true); 63 typename GW::Node t(INVALID, g2.addNode(), true); 64 typedef SmartGraph Graph3; 65 // std::cout << "making extender graph..." << std::endl; 66 typedef NewEdgeSetGraphWrapper2<GW, Graph3> GWW; 67 // { 68 // checkConcept<StaticGraph, GWW>(); 69 // } 70 GWW gww(gw); 71 typedef AugmentingGraphWrapper<GW, GWW> GWWW; 72 GWWW gwww(gw, gww); 73 74 // std::cout << "making new edges..." << std::endl; 75 typename GWWW::template EdgeMap<Num> translated_cap(gwww); 76 77 for (typename GW::EdgeIt e(gw); e!=INVALID; ++e) 78 translated_cap.set(typename GWWW::Edge(e,INVALID,false), 79 capacity[e]-lcapacity[e]); 80 81 Num expected=0; 82 83 // std::cout << "making new edges 2..." << std::endl; 84 for (typename Graph::NodeIt n(g); n!=INVALID; ++n) { 85 Num a=0; 86 for (typename Graph::InEdgeIt e(g, n); e!=INVALID; ++e) 87 a+=lcapacity[e]; 88 for (typename Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) 89 a-=lcapacity[e]; 90 if (excess[n]>a) { 91 typename GWW::Edge e= 92 gww.addEdge(typename GW::Node(n,INVALID,false), t); 93 translated_cap.set(typename GWWW::Edge(INVALID, e, true), 94 excess[n]-a); 95 } 96 if (excess[n]<a) { 97 typename GWW::Edge e= 98 gww.addEdge(s, typename GW::Node(n,INVALID,false)); 99 translated_cap.set(typename GWWW::Edge(INVALID, e, true), 100 a-excess[n]); 101 expected+=a-excess[n]; 102 } 103 } 104 105 // std::cout << "preflow..." << std::endl; 106 typename GWWW::template EdgeMap<Num> translated_flow(gwww, 0); 107 Preflow<GWWW, Num> preflow(gwww, s, t, 108 translated_cap, translated_flow); 109 preflow.run(); 110 // std::cout << "fv: " << preflow.flowValue() << std::endl; 111 // std::cout << "expected: " << expected << std::endl; 112 113 for (typename Graph::EdgeIt e(g); e!=INVALID; ++e) { 114 typename GW::Edge ew(e, INVALID, false); 115 typename GWWW::Edge ewww(ew, INVALID, false); 116 flow.set(e, translated_flow[ewww]+lcapacity[e]); 117 } 118 return (expected>=preflow.flowValue()); 119 } 54 120 void run() { 55 121 LPSolverWrapper lp; -
src/work/marci/merge_node_graph_wrapper.h
r1016 r1025 41 41 42 42 43 /*! A graph wrapper base class44 for merging the node-set of two node-disjoint graphs45 into the node-set of one graph.46 Generic implementation for unrelated _Graph1::Node and _Graph2::Node.47 */48 43 template <typename _Graph1, typename _Graph2, typename Enable=void> 49 class MergeNodeGraphWrapperBase :44 class MergeNodeGraphWrapperBaseBase : 50 45 public P1<_Graph1>, public P2<_Graph2> { 51 46 public: … … 58 53 typedef typename Parent2::Node Graph2Node; 59 54 protected: 60 MergeNodeGraphWrapperBase() { } 61 public: 62 template <typename _Value> class NodeMap; 55 MergeNodeGraphWrapperBaseBase() { } 56 public: 63 57 64 58 class Node : public Graph1Node, public Graph2Node { 65 friend class MergeNodeGraphWrapperBase<_Graph1, _Graph2>; 66 template <typename _Value> friend class NodeMap; 59 friend class MergeNodeGraphWrapperBaseBase<_Graph1, _Graph2>; 67 60 protected: 68 61 bool backward; //true, iff backward … … 89 82 }; 90 83 91 //typedef void Edge; 92 class Edge { }; 93 94 void first(Node& i) const { 95 Parent1::graph->first(*static_cast<Graph1Node*>(&i)); 96 i.backward=false; 97 if (*static_cast<Graph1Node*>(&i)==INVALID) { 98 Parent2::graph->first(*static_cast<Graph2Node*>(&i)); 99 i.backward=true; 100 } 101 } 102 void next(Node& i) const { 103 if (!(i.backward)) { 104 Parent1::graph->next(*static_cast<Graph1Node*>(&i)); 105 if (*static_cast<Graph1Node*>(&i)==INVALID) { 106 Parent2::graph->first(*static_cast<Graph2Node*>(&i)); 107 i.backward=true; 108 } 109 } else { 110 Parent2::graph->next(*static_cast<Graph2Node*>(&i)); 111 } 112 } 113 114 int id(const Node& n) const { 115 if (!n.backward) 116 return this->Parent1::graph->id(n); 117 else 118 return this->Parent2::graph->id(n); 119 } 120 121 template <typename _Value> 122 class NodeMap { 123 protected: 124 typedef typename _Graph1::template NodeMap<_Value> ParentMap1; 125 typedef typename _Graph2::template NodeMap<_Value> ParentMap2; 126 ParentMap1 forward_map; 127 ParentMap2 backward_map; 128 public: 129 typedef _Value Value; 130 typedef Node Key; 131 NodeMap(const MergeNodeGraphWrapperBase<_Graph1, _Graph2>& gw) : 132 forward_map(*(gw.Parent1::graph)), 133 backward_map(*(gw.Parent2::graph)) { } 134 NodeMap(const MergeNodeGraphWrapperBase<_Graph1, _Graph2>& gw, 135 const _Value& value) : 136 forward_map(*(gw.Parent1::graph), value), 137 backward_map(*(gw.Parent2::graph), value) { } 138 _Value operator[](const Node& n) const { 139 if (!n.backward) 140 return forward_map[n]; 141 else 142 return backward_map[n]; 143 } 144 void set(const Node& n, const _Value& value) { 145 if (!n.backward) 146 forward_map.set(n, value); 147 else 148 backward_map.set(n, value); 149 } 150 // using ParentMap1::operator[]; 151 // using ParentMap2::operator[]; 152 }; 153 154 }; 155 156 157 /*! A graph wrapper base class 158 for merging the node-set of two node-disjoint graphs 159 into the node-set of one graph. 160 Specialization for the case when _Graph1::Node are the same _Graph2::Node. 161 */ 84 static bool forward(const Node& n) { return !n.backward; } 85 static bool backward(const Node& n) { return n.backward; } 86 static void setForward(Node& n) { n.backward=false; } 87 static void setBackward(Node& n) { n.backward=true; } 88 }; 89 90 162 91 template <typename _Graph1, typename _Graph2> 163 class MergeNodeGraphWrapperBase <92 class MergeNodeGraphWrapperBaseBase< 164 93 _Graph1, _Graph2, typename boost::enable_if< 165 94 boost::is_same<typename _Graph1::Node, typename _Graph2::Node> >::type> : … … 174 103 typedef typename Parent2::Node Graph2Node; 175 104 protected: 176 MergeNodeGraphWrapperBase() { } 177 public: 178 template <typename _Value> class NodeMap; 105 MergeNodeGraphWrapperBaseBase() { } 106 public: 179 107 180 108 class Node : public Graph1Node { 181 friend class MergeNodeGraphWrapperBase<_Graph1, _Graph2>; 182 template <typename _Value> friend class NodeMap; 109 friend class MergeNodeGraphWrapperBaseBase<_Graph1, _Graph2>; 183 110 protected: 184 111 bool backward; //true, iff backward … … 190 117 Node(const Graph1Node& n1, 191 118 const Graph2Node& n2, bool _backward) : 192 Graph1Node(! backward ? n1 : n2), backward(_backward) { }119 Graph1Node(!_backward ? n1 : n2), backward(_backward) { } 193 120 Node(Invalid i) : Graph1Node(i), backward(true) { } 194 121 bool operator==(const Node& v) const { … … 201 128 }; 202 129 203 //typedef void Edge; 204 class Edge { }; 205 206 void first(Node& i) const { 207 Parent1::graph->first(*static_cast<Graph1Node*>(&i)); 208 i.backward=false; 209 if (*static_cast<Graph1Node*>(&i)==INVALID) { 210 Parent2::graph->first(*static_cast<Graph1Node*>(&i)); 211 i.backward=true; 212 } 213 } 214 void next(Node& i) const { 215 if (!(i.backward)) { 216 Parent1::graph->next(*static_cast<Graph1Node*>(&i)); 217 if (*static_cast<Graph1Node*>(&i)==INVALID) { 218 Parent2::graph->first(*static_cast<Graph1Node*>(&i)); 219 i.backward=true; 220 } 221 } else { 222 Parent2::graph->next(*static_cast<Graph1Node*>(&i)); 223 } 224 } 225 226 int id(const Node& n) const { 227 if (!n.backward) 228 return this->Parent1::graph->id(n); 229 else 230 return this->Parent2::graph->id(n); 231 } 232 233 template <typename _Value> 234 class NodeMap { 235 protected: 236 typedef typename _Graph1::template NodeMap<_Value> ParentMap1; 237 typedef typename _Graph2::template NodeMap<_Value> ParentMap2; 238 ParentMap1 forward_map; 239 ParentMap2 backward_map; 240 public: 241 typedef _Value Value; 242 typedef Node Key; 243 NodeMap(const MergeNodeGraphWrapperBase<_Graph1, _Graph2>& gw) : 244 forward_map(*(gw.Parent1::graph)), 245 backward_map(*(gw.Parent2::graph)) { } 246 NodeMap(const MergeNodeGraphWrapperBase<_Graph1, _Graph2>& gw, 247 const _Value& value) : 248 forward_map(*(gw.Parent1::graph), value), 249 backward_map(*(gw.Parent2::graph), value) { } 250 _Value operator[](const Node& n) const { 251 if (!n.backward) 252 return forward_map[n]; 253 else 254 return backward_map[n]; 255 } 256 void set(const Node& n, const _Value& value) { 257 if (!n.backward) 258 forward_map.set(n, value); 259 else 260 backward_map.set(n, value); 261 } 262 // using ParentMap1::operator[]; 263 // using ParentMap2::operator[]; 264 }; 265 266 }; 267 268 269 /*! A graph wrapper base class 270 for merging the node-set of two node-disjoint graphs 271 into the node-set of one graph. 272 Specialization for the case when 273 _Graph1::Node is a base class and _Graph2::Node is derived from it. 274 */ 130 static bool forward(const Node& n) { return !n.backward; } 131 static bool backward(const Node& n) { return n.backward; } 132 static void setForward(Node& n) { n.backward=false; } 133 static void setBackward(Node& n) { n.backward=true; } 134 }; 135 136 275 137 template <typename _Graph1, typename _Graph2> 276 class MergeNodeGraphWrapperBase <138 class MergeNodeGraphWrapperBaseBase< 277 139 _Graph1, _Graph2, typename boost::enable_if< 278 140 boost::is_base_and_derived<typename _Graph1::Node, typename _Graph2::Node> >::type> : … … 287 149 typedef typename Parent2::Node Graph2Node; 288 150 protected: 289 MergeNodeGraphWrapperBase() { } 290 public: 291 template <typename _Value> class NodeMap; 151 MergeNodeGraphWrapperBaseBase() { } 152 public: 292 153 293 154 class Node : public Graph2Node { 294 friend class MergeNodeGraphWrapperBase<_Graph1, _Graph2>; 295 template <typename _Value> friend class NodeMap; 155 friend class MergeNodeGraphWrapperBaseBase<_Graph1, _Graph2>; 296 156 protected: 297 157 bool backward; //true, iff backward … … 320 180 }; 321 181 322 //typedef void Edge; 323 class Edge { }; 324 325 void first(Node& i) const { 326 Parent1::graph->first(*static_cast<Graph1Node*>(&i)); 327 i.backward=false; 328 if (*static_cast<Graph1Node*>(&i)==INVALID) { 329 Parent2::graph->first(*static_cast<Graph2Node*>(&i)); 330 i.backward=true; 331 } 332 } 333 void next(Node& i) const { 334 if (!(i.backward)) { 335 Parent1::graph->next(*static_cast<Graph1Node*>(&i)); 336 if (*static_cast<Graph1Node*>(&i)==INVALID) { 337 Parent2::graph->first(*static_cast<Graph2Node*>(&i)); 338 i.backward=true; 339 } 340 } else { 341 Parent2::graph->next(*static_cast<Graph2Node*>(&i)); 342 } 343 } 344 345 int id(const Node& n) const { 346 if (!n.backward) 347 return this->Parent1::graph->id(n); 348 else 349 return this->Parent2::graph->id(n); 350 } 351 352 template <typename _Value> 353 class NodeMap { 354 protected: 355 typedef typename _Graph1::template NodeMap<_Value> ParentMap1; 356 typedef typename _Graph2::template NodeMap<_Value> ParentMap2; 357 ParentMap1 forward_map; 358 ParentMap2 backward_map; 359 public: 360 typedef _Value Value; 361 typedef Node Key; 362 NodeMap(const MergeNodeGraphWrapperBase<_Graph1, _Graph2>& gw) : 363 forward_map(*(gw.Parent1::graph)), 364 backward_map(*(gw.Parent2::graph)) { } 365 NodeMap(const MergeNodeGraphWrapperBase<_Graph1, _Graph2>& gw, 366 const _Value& value) : 367 forward_map(*(gw.Parent1::graph), value), 368 backward_map(*(gw.Parent2::graph), value) { } 369 _Value operator[](const Node& n) const { 370 if (!n.backward) 371 return forward_map[n]; 372 else 373 return backward_map[n]; 374 } 375 void set(const Node& n, const _Value& value) { 376 if (!n.backward) 377 forward_map.set(n, value); 378 else 379 backward_map.set(n, value); 380 } 381 // using ParentMap1::operator[]; 382 // using ParentMap2::operator[]; 383 }; 384 385 }; 386 387 388 /*! A graph wrapper base class 389 for merging the node-set of two node-disjoint graphs 390 into the node-set of one graph. 391 Specialized implementaton for the case when _Graph1::Node is derived 392 from _Graph2::Node. 393 */ 182 static bool forward(const Node& n) { return !n.backward; } 183 static bool backward(const Node& n) { return n.backward; } 184 static void setForward(Node& n) { n.backward=false; } 185 static void setBackward(Node& n) { n.backward=true; } 186 }; 187 188 394 189 template <typename _Graph1, typename _Graph2> 395 class MergeNodeGraphWrapperBase <190 class MergeNodeGraphWrapperBaseBase< 396 191 _Graph1, _Graph2, typename boost::enable_if< 397 192 boost::is_base_and_derived<typename _Graph2::Node, typename _Graph1::Node> >::type> : … … 406 201 typedef typename Parent2::Node Graph2Node; 407 202 protected: 408 MergeNodeGraphWrapperBase() { } 409 public: 410 template <typename _Value> class NodeMap; 203 MergeNodeGraphWrapperBaseBase() { } 204 public: 411 205 412 206 class Node : public Graph1Node { 413 friend class MergeNodeGraphWrapperBase<_Graph1, _Graph2>; 414 template <typename _Value> friend class NodeMap; 207 friend class MergeNodeGraphWrapperBaseBase<_Graph1, _Graph2>; 415 208 protected: 416 209 bool backward; //true, iff backward … … 439 232 }; 440 233 441 //typedef void Edge; 234 static bool forward(const Node& n) { return !n.backward; } 235 static bool backward(const Node& n) { return n.backward; } 236 static void setForward(Node& n) { n.backward=false; } 237 static void setBackward(Node& n) { n.backward=true; } 238 }; 239 240 241 template <typename _Graph1, typename _Graph2> 242 class MergeNodeGraphWrapperBase : 243 public MergeNodeGraphWrapperBaseBase<_Graph1, _Graph2> { 244 public: 245 typedef MergeNodeGraphWrapperBaseBase<_Graph1, _Graph2> Parent; 246 typedef _Graph1 Graph1; 247 typedef _Graph2 Graph2; 248 typedef P1<_Graph1> Parent1; 249 typedef P2<_Graph2> Parent2; 250 typedef typename Parent1::Node Graph1Node; 251 typedef typename Parent2::Node Graph2Node; 252 253 typedef typename Parent::Node Node; 442 254 class Edge { }; 443 255 444 256 void first(Node& i) const { 445 257 Parent1::graph->first(*static_cast<Graph1Node*>(&i)); 446 i.backward=false;258 this->setForward(i); 447 259 if (*static_cast<Graph1Node*>(&i)==INVALID) { 448 260 Parent2::graph->first(*static_cast<Graph2Node*>(&i)); 449 i.backward=true;261 this->setBackward(i); 450 262 } 451 263 } 452 264 void next(Node& i) const { 453 if ( !(i.backward)) {265 if (this->forward(i)) { 454 266 Parent1::graph->next(*static_cast<Graph1Node*>(&i)); 455 267 if (*static_cast<Graph1Node*>(&i)==INVALID) { 456 268 Parent2::graph->first(*static_cast<Graph2Node*>(&i)); 457 i.backward=true;269 this->setBackward(i); 458 270 } 459 271 } else { … … 463 275 464 276 int id(const Node& n) const { 465 if ( !n.backward)277 if (this->forward(n)) 466 278 return this->Parent1::graph->id(n); 467 279 else … … 487 299 backward_map(*(gw.Parent2::graph), value) { } 488 300 _Value operator[](const Node& n) const { 489 if ( !n.backward)301 if (Parent::forward(n)) 490 302 return forward_map[n]; 491 303 else … … 493 305 } 494 306 void set(const Node& n, const _Value& value) { 495 if ( !n.backward)307 if (Parent::forward(n)) 496 308 forward_map.set(n, value); 497 309 else … … 506 318 507 319 /*! A graph wrapper class 508 fors merging the node-set of two node-disjoint graphs 509 into one node-set. It does not satisfy 320 for merging the node-set of two node-disjoint graphs 321 into the node-set of one graph. 322 Different implementations are according to the relation of 323 _Graph1::Node and _Graph2::Node. 324 If _Graph1::Node and _Graph2::Node are unrelated, then 325 MergeNodeGraphWrapper<_Graph1, _Graph2>::Node 326 is derived from both. 327 If _Graph1::Node and _Graph2::Node are the same type, then 328 MergeNodeGraphWrapper<_Graph1, _Graph2>::Node 329 is derived from _Graph1::Node. 330 If one of _Graph1::Node and _Graph2::Node 331 is derived from the other one, then 332 MergeNodeGraphWrapper<_Graph1, _Graph2>::Node 333 is derived from the derived type. 334 It does not satisfy 510 335 StaticGraph concept as it has no edge-set which 511 336 works together with the node-set. 512 337 */ 513 338 template <typename _Graph1, typename _Graph2> 514 339 class MergeNodeGraphWrapper : public … … 583 408 }; 584 409 410 using Parent::forward; 411 using Parent::backward; 412 bool forward(const Edge& e) const { return !e.backward; } 413 bool backward(const Edge& e) const { return e.backward; } 414 585 415 using Parent::first; 586 416 void first(Edge& i) const { … … 593 423 } 594 424 void firstIn(Edge& i, const Node& n) const { 595 Parent1::graph->firstIn(*static_cast<Graph1Edge*>(&i), n); 596 i.backward=false; 597 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 425 if (!backward(n)) { 426 Parent1::graph->firstIn(*static_cast<Graph1Edge*>(&i), n); 427 if (*static_cast<Graph1Edge*>(&i)==INVALID) 428 i=INVALID; 429 else 430 i.backward=false; 431 } else { 598 432 Parent2::graph->firstIn(*static_cast<Graph2Edge*>(&i), n); 599 433 i.backward=true; … … 601 435 } 602 436 void firstOut(Edge& i, const Node& n) const { 603 Parent1::graph->firstOut(*static_cast<Graph1Edge*>(&i), n); 604 i.backward=false; 605 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 437 if (!backward(n)) { 438 Parent1::graph->firstOut(*static_cast<Graph1Edge*>(&i), n); 439 if (*static_cast<Graph1Edge*>(&i)==INVALID) 440 i=INVALID; 441 else 442 i.backward=false; 443 } else { 606 444 Parent2::graph->firstOut(*static_cast<Graph2Edge*>(&i), n); 607 445 i.backward=true; … … 624 462 if (!(i.backward)) { 625 463 Parent1::graph->nextIn(*static_cast<Graph1Edge*>(&i)); 626 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 627 Parent2::graph->first(*static_cast<Graph2Edge*>(&i)); 628 i.backward=true; 629 } 464 if (*static_cast<Graph1Edge*>(&i)==INVALID) i=INVALID; 630 465 } else { 631 466 Parent2::graph->nextIn(*static_cast<Graph2Edge*>(&i)); … … 635 470 if (!(i.backward)) { 636 471 Parent1::graph->nextOut(*static_cast<Graph1Edge*>(&i)); 637 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 638 Parent2::graph->first(*static_cast<Graph2Edge*>(&i)); 639 i.backward=true; 640 } 472 if (*static_cast<Graph1Edge*>(&i)==INVALID) i=INVALID; 641 473 } else { 642 474 Parent2::graph->nextOut(*static_cast<Graph2Edge*>(&i)); … … 750 582 Edge(const Graph1Edge& n1, 751 583 const Graph2Edge& n2, bool _backward) : 752 Graph1Edge(! backward ? n1 : n2), backward(_backward) { }584 Graph1Edge(!_backward ? n1 : n2), backward(_backward) { } 753 585 Edge(Invalid i) : Graph1Edge(i), backward(true) { } 754 586 bool operator==(const Edge& v) const { … … 760 592 } 761 593 }; 594 595 using Parent::forward; 596 using Parent::backward; 597 bool forward(const Edge& e) const { return !e.backward; } 598 bool backward(const Edge& e) const { return e.backward; } 762 599 763 600 using Parent::first; … … 771 608 } 772 609 void firstIn(Edge& i, const Node& n) const { 773 Parent1::graph->firstIn(*static_cast<Graph1Edge*>(&i), n); 774 i.backward=false; 775 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 610 if (!backward(n)) { 611 Parent1::graph->firstIn(*static_cast<Graph1Edge*>(&i), n); 612 if (*static_cast<Graph1Edge*>(&i)==INVALID) 613 i=INVALID; 614 else 615 i.backward=false; 616 } else { 776 617 Parent2::graph->firstIn(*static_cast<Graph1Edge*>(&i), n); 777 618 i.backward=true; … … 779 620 } 780 621 void firstOut(Edge& i, const Node& n) const { 781 Parent1::graph->firstOut(*static_cast<Graph1Edge*>(&i), n); 782 i.backward=false; 783 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 622 if (!backward(n)) { 623 Parent1::graph->firstOut(*static_cast<Graph1Edge*>(&i), n); 624 if (*static_cast<Graph1Edge*>(&i)==INVALID) 625 i=INVALID; 626 else 627 i.backward=false; 628 } else { 784 629 Parent2::graph->firstOut(*static_cast<Graph1Edge*>(&i), n); 785 630 i.backward=true; … … 802 647 if (!(i.backward)) { 803 648 Parent1::graph->nextIn(*static_cast<Graph1Edge*>(&i)); 804 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 805 Parent2::graph->first(*static_cast<Graph1Edge*>(&i)); 806 i.backward=true; 807 } 649 if (*static_cast<Graph1Edge*>(&i)==INVALID) i=INVALID; 808 650 } else { 809 651 Parent2::graph->nextIn(*static_cast<Graph1Edge*>(&i)); … … 813 655 if (!(i.backward)) { 814 656 Parent1::graph->nextOut(*static_cast<Graph1Edge*>(&i)); 815 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 816 Parent2::graph->first(*static_cast<Graph1Edge*>(&i)); 817 i.backward=true; 818 } 657 if (*static_cast<Graph1Edge*>(&i)==INVALID) i=INVALID; 819 658 } else { 820 659 Parent2::graph->nextOut(*static_cast<Graph1Edge*>(&i)); … … 946 785 }; 947 786 787 using Parent::forward; 788 using Parent::backward; 789 bool forward(const Edge& e) const { return !e.backward; } 790 bool backward(const Edge& e) const { return e.backward; } 791 948 792 using Parent::first; 949 793 void first(Edge& i) const { … … 956 800 } 957 801 void firstIn(Edge& i, const Node& n) const { 958 Parent1::graph->firstIn(*static_cast<Graph1Edge*>(&i), n); 959 i.backward=false; 960 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 802 if (!backward(n)) { 803 Parent1::graph->firstIn(*static_cast<Graph1Edge*>(&i), n); 804 if (*static_cast<Graph1Edge*>(&i)==INVALID) 805 i=INVALID; 806 else 807 i.backward=false; 808 } else { 961 809 Parent2::graph->firstIn(*static_cast<Graph2Edge*>(&i), n); 962 810 i.backward=true; … … 964 812 } 965 813 void firstOut(Edge& i, const Node& n) const { 966 Parent1::graph->firstOut(*static_cast<Graph1Edge*>(&i), n); 967 i.backward=false; 968 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 814 if (!backward(n)) { 815 Parent1::graph->firstOut(*static_cast<Graph1Edge*>(&i), n); 816 if (*static_cast<Graph1Edge*>(&i)==INVALID) 817 i=INVALID; 818 else 819 i.backward=false; 820 } else { 969 821 Parent2::graph->firstOut(*static_cast<Graph2Edge*>(&i), n); 970 822 i.backward=true; … … 987 839 if (!(i.backward)) { 988 840 Parent1::graph->nextIn(*static_cast<Graph1Edge*>(&i)); 989 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 990 Parent2::graph->first(*static_cast<Graph2Edge*>(&i)); 991 i.backward=true; 992 } 841 if (*static_cast<Graph1Edge*>(&i)==INVALID) i=INVALID; 993 842 } else { 994 843 Parent2::graph->nextIn(*static_cast<Graph2Edge*>(&i)); … … 998 847 if (!(i.backward)) { 999 848 Parent1::graph->nextOut(*static_cast<Graph1Edge*>(&i)); 1000 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 1001 Parent2::graph->first(*static_cast<Graph2Edge*>(&i)); 1002 i.backward=true; 1003 } 849 if (*static_cast<Graph1Edge*>(&i)==INVALID) i=INVALID; 1004 850 } else { 1005 851 Parent2::graph->nextOut(*static_cast<Graph2Edge*>(&i)); … … 1130 976 }; 1131 977 978 using Parent::forward; 979 using Parent::backward; 980 bool forward(const Edge& e) const { return !e.backward; } 981 bool backward(const Edge& e) const { return e.backward; } 982 1132 983 using Parent::first; 1133 984 void first(Edge& i) const { … … 1140 991 } 1141 992 void firstIn(Edge& i, const Node& n) const { 1142 Parent1::graph->firstIn(*static_cast<Graph1Edge*>(&i), n); 1143 i.backward=false; 1144 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 993 if (!backward(n)) { 994 Parent1::graph->firstIn(*static_cast<Graph1Edge*>(&i), n); 995 if (*static_cast<Graph1Edge*>(&i)==INVALID) 996 i=INVALID; 997 else 998 i.backward=false; 999 } else { 1145 1000 Parent2::graph->firstIn(*static_cast<Graph2Edge*>(&i), n); 1146 1001 i.backward=true; … … 1148 1003 } 1149 1004 void firstOut(Edge& i, const Node& n) const { 1150 Parent1::graph->firstOut(*static_cast<Graph1Edge*>(&i), n); 1151 i.backward=false; 1152 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 1005 if (!backward(n)) { 1006 Parent1::graph->firstOut(*static_cast<Graph1Edge*>(&i), n); 1007 if (*static_cast<Graph1Edge*>(&i)==INVALID) 1008 i=INVALID; 1009 else 1010 i.backward=false; 1011 } else { 1153 1012 Parent2::graph->firstOut(*static_cast<Graph2Edge*>(&i), n); 1154 1013 i.backward=true; … … 1171 1030 if (!(i.backward)) { 1172 1031 Parent1::graph->nextIn(*static_cast<Graph1Edge*>(&i)); 1173 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 1174 Parent2::graph->first(*static_cast<Graph2Edge*>(&i)); 1175 i.backward=true; 1176 } 1032 if (*static_cast<Graph1Edge*>(&i)==INVALID) i=INVALID; 1177 1033 } else { 1178 1034 Parent2::graph->nextIn(*static_cast<Graph2Edge*>(&i)); … … 1182 1038 if (!(i.backward)) { 1183 1039 Parent1::graph->nextOut(*static_cast<Graph1Edge*>(&i)); 1184 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 1185 Parent2::graph->first(*static_cast<Graph2Edge*>(&i)); 1186 i.backward=true; 1187 } 1040 if (*static_cast<Graph1Edge*>(&i)==INVALID) i=INVALID; 1188 1041 } else { 1189 1042 Parent2::graph->nextOut(*static_cast<Graph2Edge*>(&i)); … … 1348 1201 int edgeNum() const { return edge_set_graph->edgeNum(); } 1349 1202 1203 // NNode addOldNode() { 1204 // return Parent::addNode(); 1205 // } 1206 1207 // ENode addNewNode() { 1208 // return edge_set_graph->addNode(); 1209 // } 1210 1350 1211 Edge addEdge(const Node& u, const Node& v) { 1351 1212 return edge_set_graph->addEdge((*e_node)[u], (*e_node)[v]); … … 1393 1254 typedef _EdgeSetGraph EdgeSetGraph; 1394 1255 typedef IterableGraphExtender< 1395 NewEdgeSetGraphWrapper <_Graph, _EdgeSetGraph> > Parent;1256 NewEdgeSetGraphWrapperBase<_Graph, _EdgeSetGraph> > Parent; 1396 1257 protected: 1397 1258 NewEdgeSetGraphWrapper() { } … … 1410 1271 }; 1411 1272 1273 /*! A graph wrapper class for the following functionality. 1274 The same as NewEdgeSetGrapWrapper, but the bijection and the graph of 1275 new edges is andled inthe class. 1276 */ 1277 template <typename _Graph, typename _EdgeSetGraph> 1278 class NewEdgeSetGraphWrapper2 : 1279 public IterableGraphExtender< 1280 NewEdgeSetGraphWrapperBase<_Graph, _EdgeSetGraph> > { 1281 public: 1282 typedef _Graph Graph; 1283 typedef _EdgeSetGraph EdgeSetGraph; 1284 typedef IterableGraphExtender< 1285 NewEdgeSetGraphWrapperBase<_Graph, _EdgeSetGraph> > Parent; 1286 protected: 1287 _EdgeSetGraph _edge_set_graph; 1288 typename Graph::template NodeMap<typename EdgeSetGraph::Node> _e_node; 1289 typename EdgeSetGraph::template NodeMap<typename Graph::Node> _n_node; 1290 NewEdgeSetGraphWrapper2() { } 1291 public: 1292 typedef typename Graph::Node Node; 1293 // typedef typename Parent::Edge Edge; 1294 1295 NewEdgeSetGraphWrapper2(_Graph& _graph) : 1296 _edge_set_graph(), 1297 _e_node(_graph), _n_node(_edge_set_graph) { 1298 setGraph(_graph); 1299 setEdgeSetGraph(_edge_set_graph); 1300 setNodeMap(_n_node); setENodeMap(_e_node); 1301 Node n; 1302 for (this->first(n); n!=INVALID; this->next(n)) { 1303 typename EdgeSetGraph::Node e=_edge_set_graph.addNode(); 1304 _e_node.set(n, e); 1305 _n_node.set(e, n); 1306 } 1307 } 1308 1309 // Node addNode() { 1310 // Node n=(*this).Parent::addNode(); 1311 // typename EdgeSetGraph::Node e=_edge_set_graph.addNode(); 1312 // _e_node.set(n, e); 1313 // _n_node.set(e, n); 1314 // return n; 1315 // } 1316 1317 }; 1412 1318 1413 1319 /*! A graph wrapper base class … … 1418 1324 In an other point of view, _Graph1 is extended with 1419 1325 the edge-set of _Graph2. 1326 \warning we need specialize dimplementations 1327 \todo we need specialize dimplementations 1328 \bug we need specialize dimplementations 1420 1329 */ 1421 1330 template <typename _Graph1, typename _Graph2, typename Enable=void> … … 1507 1416 void nextIn(Edge& i) const { 1508 1417 if (!(i.backward)) { 1418 Node n=target(i); 1509 1419 Parent1::graph->nextIn(*static_cast<Graph1Edge*>(&i)); 1510 1420 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 1511 graph2->first (*static_cast<Graph2Edge*>(&i));1421 graph2->firstIn(*static_cast<Graph2Edge*>(&i), n); 1512 1422 i.backward=true; 1513 1423 } … … 1518 1428 void nextOut(Edge& i) const { 1519 1429 if (!(i.backward)) { 1430 Node n=source(i); 1520 1431 Parent1::graph->nextOut(*static_cast<Graph1Edge*>(&i)); 1521 1432 if (*static_cast<Graph1Edge*>(&i)==INVALID) { 1522 graph2->first (*static_cast<Graph2Edge*>(&i));1433 graph2->firstOut(*static_cast<Graph2Edge*>(&i), n); 1523 1434 i.backward=true; 1524 1435 } -
src/work/marci/merge_node_graph_wrapper_test.cc
r1016 r1025 5 5 #include <lemon/smart_graph.h> 6 6 #include <lemon/dimacs.h> 7 #include <lemon/preflow.h> 7 8 #include <lemon/full_graph.h> 8 9 #include <merge_node_graph_wrapper.h> … … 27 28 cout << " nodes:" << endl; 28 29 for (typename Graph::NodeIt n(g); n!=INVALID; ++n) { 29 cout << " " << g.id(n) << endl; 30 cout << " " << g.id(n) << ": out edges:" << endl; 31 for (typename Graph::OutEdgeIt e(g, n); e!=INVALID; ++e) 32 cout << " " << g.id(g.source(e)) << "->" << g.id(g.target(e)) << endl; 30 33 } 31 34 cout << " edges:" << endl; 32 for (typename Graph::EdgeIt n(g); n!=INVALID; ++n) {33 cout << " " << g.id(n) << ": "34 << g.id(g.source( n)) << "->" << g.id(g.target(n)) << endl;35 for (typename Graph::EdgeIt e(g); e!=INVALID; ++e) { 36 cout << " " << g.id(e) << ": " 37 << g.id(g.source(e)) << "->" << g.id(g.target(e)) << endl; 35 38 } 36 39 } … … 39 42 { 40 43 cout << "FIRST TEST" << endl; 41 typedef SmartGraph Graph1; 44 //typedef SmartGraph Graph1; 45 typedef ListGraph Graph1; 42 46 typedef ListGraph Graph2; 47 typedef SmartGraph Graph3; 43 48 44 {45 checkConcept<StaticGraph, MergeEdgeGraphWrapper<Graph1, Graph2> >();46 MergeEdgeGraphWrapper<Graph1, Graph2>::printNode();47 MergeEdgeGraphWrapper<Graph1, Graph2>::printEdge();48 checkConcept<StaticGraph, MergeEdgeGraphWrapper<Graph1, Graph1> >();49 MergeEdgeGraphWrapper<Graph1, Graph1>::printNode();50 MergeEdgeGraphWrapper<Graph1, Graph1>::printEdge();51 typedef ResGraphWrapper<Graph1, int,52 ConstMap<Graph1, int>, ConstMap<Graph1, int> > Graph4;53 checkConcept<StaticGraph, MergeEdgeGraphWrapper<Graph1, Graph4> >();54 MergeEdgeGraphWrapper<Graph1, Graph4>::printNode();55 MergeEdgeGraphWrapper<Graph1, Graph4>::printEdge();56 checkConcept<StaticGraph, MergeEdgeGraphWrapper<Graph4, Graph1> >();57 MergeEdgeGraphWrapper<Graph4, Graph1>::printNode();58 MergeEdgeGraphWrapper<Graph4, Graph1>::printEdge();59 }49 // { 50 // checkConcept<StaticGraph, MergeEdgeGraphWrapper<Graph1, Graph2> >(); 51 // MergeEdgeGraphWrapper<Graph1, Graph2>::printNode(); 52 // MergeEdgeGraphWrapper<Graph1, Graph2>::printEdge(); 53 // checkConcept<StaticGraph, MergeEdgeGraphWrapper<Graph1, Graph1> >(); 54 // MergeEdgeGraphWrapper<Graph1, Graph1>::printNode(); 55 // MergeEdgeGraphWrapper<Graph1, Graph1>::printEdge(); 56 // typedef ResGraphWrapper<Graph1, int, 57 // ConstMap<Graph1, int>, ConstMap<Graph1, int> > Graph4; 58 // checkConcept<StaticGraph, MergeEdgeGraphWrapper<Graph1, Graph4> >(); 59 // MergeEdgeGraphWrapper<Graph1, Graph4>::printNode(); 60 // MergeEdgeGraphWrapper<Graph1, Graph4>::printEdge(); 61 // checkConcept<StaticGraph, MergeEdgeGraphWrapper<Graph4, Graph1> >(); 62 // MergeEdgeGraphWrapper<Graph4, Graph1>::printNode(); 63 // MergeEdgeGraphWrapper<Graph4, Graph1>::printEdge(); 64 // } 60 65 61 66 Graph1 g1; … … 63 68 typedef MergeEdgeGraphWrapper<Graph1, Graph2> GW; 64 69 GW gw(g1, g2); 70 Graph1::Node s1, t1; 71 Graph2::Node s2, t2; 72 Graph1::EdgeMap<int> cap1(g1); 73 Graph2::EdgeMap<int> cap2(g2); 65 74 66 75 std::ifstream f1("graph1.dim"); … … 68 77 readDimacs(f1, g1); 69 78 readDimacs(f2, g2); 79 80 // GW::NodeMap<int> ize(gw, 8); 81 // for (GW::NodeIt n(gw); n!=INVALID; ++n) ize.set(n, 9); 82 // GW::EdgeMap<int> mize(gw, 8); 83 // for (GW::EdgeIt n(gw); n!=INVALID; ++n) mize.set(n, 7); 84 85 // std::ifstream f1("flow-1.dim"); 86 // std::ifstream f2("flow2.dim"); 87 // readDimacs(f1, g1, cap1, s1, t1); 88 // readDimacs(f2, g2, cap2, s2, t2); 70 89 71 cout << "1st graph" << endl; 72 printGraph(g1); 73 74 cout << "2nd graph" << endl; 75 printGraph(g2); 76 77 cout << "merged graph" << endl; 78 printGraph(gw); 79 90 // GW::EdgeMap<int> cap(gw); 91 // for (GW::EdgeIt e(gw); e!=INVALID; ++e) { 92 // if (gw.forward(e)) cap.set(e, cap1[e]); 93 // if (gw.backward(e)) cap.set(e, cap2[e]); 94 // } 95 96 // { 97 // GW::EdgeMap<int> flow(gw, 0); 98 // Preflow<GW, int> preflow(gw, GW::Node(s1, INVALID, false), 99 // GW::Node(t1, INVALID, false), cap, flow); 100 // preflow.run(); 101 // std::cout << "s1->t1: " << preflow.flowValue() << std::endl; 102 // } 103 // { 104 // GW::EdgeMap<int> flow(gw, 0); 105 // Preflow<GW, int> preflow(gw, GW::Node(INVALID, s2, true), 106 // GW::Node(INVALID, t2, true), cap, flow); 107 // preflow.run(); 108 // std::cout << "s2->t2: " << preflow.flowValue() << std::endl; 109 // } 110 // { 111 // GW::EdgeMap<int> flow(gw, 0); 112 // Preflow<GW, int> preflow(gw, GW::Node(s1, INVALID, false), 113 // GW::Node(INVALID, t2, true), cap, flow); 114 // preflow.run(); 115 // std::cout << "s1->t2: " << preflow.flowValue() << std::endl; 116 // } 117 // { 118 // GW::EdgeMap<int> flow(gw, 0); 119 // Preflow<GW, int> preflow(gw, GW::Node(INVALID, s2, true), 120 // GW::Node(s1, INVALID, false), cap, flow); 121 // preflow.run(); 122 // std::cout << "s2->t1: " << preflow.flowValue() << std::endl; 123 // } 124 cout << "1st graph" << endl; 125 printGraph(g1); 126 127 cout << "2nd graph" << endl; 128 printGraph(g2); 129 130 cout << "merged graph" << endl; 131 printGraph(gw); 132 133 // for (GW::NodeIt n(gw); n!=INVALID; ++n) 134 // std::cout << ize[n] << std::endl; 135 // for (GW::EdgeIt n(gw); n!=INVALID; ++n) 136 // std::cout << mize[n] << std::endl; 137 138 typedef NewEdgeSetGraphWrapper2<GW, Graph3> GWW; 139 // { 140 // checkConcept<StaticGraph, GWW>(); 141 // } 142 143 GWW gww(gw); 144 145 cout << "new edges graph" << endl; 146 printGraph(gww); 147 148 GWW::NodeIt n(gww); 149 GWW::Node n1=n; 150 ++n; 151 GWW::Node n2=n; 152 gww.addEdge(n1, n2); 153 // gww.addNode(); 154 // gww.addNode(); 155 156 cout << "new edges graph" << endl; 157 printGraph(gww); 158 159 typedef AugmentingGraphWrapper<GW, GWW> GWWW; 160 // { 161 // checkConcept<StaticGraph, GWWW>(); 162 // } 163 GWWW gwww(gw, gww); 164 165 cout << "fully merged graph" << endl; 166 printGraph(gwww); 80 167 } 81 168 … … 84 171 cout << "SECOND TEST" << endl; 85 172 typedef SmartGraph Graph1; 86 {87 checkConcept<StaticGraph, Graph1>();88 }173 // { 174 // checkConcept<StaticGraph, Graph1>(); 175 // } 89 176 90 177 Graph1 g1; … … 94 181 typedef EdgeSubGraphWrapper<FullGraph, ConstMap<FullGraph::Edge, bool> > 95 182 Graph2; 96 {97 checkConcept<StaticGraph, Graph2>();98 }183 // { 184 // checkConcept<StaticGraph, Graph2>(); 185 // } 99 186 100 187 Graph2 g2(pre_g2, const_false_map); 101 188 102 189 typedef MergeEdgeGraphWrapper<Graph1, Graph2> GW; 103 {104 checkConcept<StaticGraph, GW>();105 }190 // { 191 // checkConcept<StaticGraph, GW>(); 192 // } 106 193 GW gw(g1, g2); 107 194 GW::Node sw; … … 138 225 139 226 typedef NewEdgeSetGraphWrapper<GW, Graph3> GWW; 140 {141 checkConcept<StaticGraph, GWW>();142 }227 // { 228 // checkConcept<StaticGraph, GWW>(); 229 // } 143 230 144 231 GWW gww(gw, g3, gwn, g3n); … … 159 246 160 247 typedef AugmentingGraphWrapper<GW, GWW> GWWW; 161 {162 checkConcept<StaticGraph, GWWW>();163 }248 // { 249 // checkConcept<StaticGraph, GWWW>(); 250 // } 164 251 GWWW gwww(gw, gww); 165 252
Note: See TracChangeset
for help on using the changeset viewer.