0
105
0
1
1
2
2
1
1
21
21
1
1
1
1
1
1
1
1
1
1
1
1
1
1
12
12
| 1 | 1 |
LEMON code without an explicit copyright is covered by the following |
| 2 | 2 |
copyright/license. |
| 3 | 3 |
|
| 4 |
Copyright (C) 2003- |
|
| 4 |
Copyright (C) 2003-2009 Egervary Jeno Kombinatorikus Optimalizalasi |
|
| 5 | 5 |
Kutatocsoport (Egervary Combinatorial Optimization Research Group, |
| 6 | 6 |
EGRES). |
| 7 | 7 |
|
| 8 | 8 |
Permission is hereby granted, free of charge, to any person or organization |
| 9 | 9 |
obtaining a copy of the software and accompanying documentation covered by |
| 10 | 10 |
this license (the "Software") to use, reproduce, display, distribute, |
| 11 | 11 |
execute, and transmit the Software, and to prepare derivative works of the |
| 12 | 12 |
Software, and to permit third-parties to whom the Software is furnished to |
| 13 | 13 |
do so, all subject to the following: |
| 14 | 14 |
|
| 15 | 15 |
The copyright notices in the Software and this entire statement, including |
| 16 | 16 |
the above license grant, this restriction and the following disclaimer, |
| 17 | 17 |
must be included in all copies of the Software, in whole or in part, and |
| 18 | 18 |
all derivative works of the Software, unless such copies or derivative |
| 19 | 19 |
works are solely in the form of machine-executable object code generated by |
| 20 | 20 |
a source language processor. |
| 21 | 21 |
|
| 22 | 22 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | 23 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | 24 |
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 25 | 25 |
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 26 | 26 |
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 27 | 27 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 28 | 28 |
DEALINGS IN THE SOFTWARE. |
| 29 | 29 |
|
| 30 | 30 |
=========================================================================== |
| 31 | 31 |
This license is a verbatim copy of the Boost Software License, Version 1.0. |
| 32 | 32 |
|
| 33 | 33 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup demos |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief Argument parser demo |
| 22 | 22 |
/// |
| 23 | 23 |
/// This example shows how the argument parser can be used. |
| 24 | 24 |
/// |
| 25 | 25 |
/// \include arg_parser_demo.cc |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/arg_parser.h> |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| 30 | 30 |
int main(int argc, char **argv) |
| 31 | 31 |
{
|
| 32 | 32 |
// Initialize the argument parser |
| 33 | 33 |
ArgParser ap(argc, argv); |
| 34 | 34 |
int i; |
| 35 | 35 |
std::string s; |
| 36 | 36 |
double d = 1.0; |
| 37 | 37 |
bool b, nh; |
| 38 | 38 |
bool g1, g2, g3; |
| 39 | 39 |
|
| 40 | 40 |
// Add a mandatory integer option with storage reference |
| 41 | 41 |
ap.refOption("n", "An integer input.", i, true);
|
| 42 | 42 |
// Add a double option with storage reference (the default value is 1.0) |
| 43 | 43 |
ap.refOption("val", "A double input.", d);
|
| 44 | 44 |
// Add a double option without storage reference (the default value is 3.14) |
| 45 | 45 |
ap.doubleOption("val2", "A double input.", 3.14);
|
| 46 | 46 |
// Set synonym for -val option |
| 47 | 47 |
ap.synonym("vals", "val");
|
| 48 | 48 |
// Add a string option |
| 49 | 49 |
ap.refOption("name", "A string input.", s);
|
| 50 | 50 |
// Add bool options |
| 51 | 51 |
ap.refOption("f", "A switch.", b)
|
| 52 | 52 |
.refOption("nohelp", "", nh)
|
| 53 | 53 |
.refOption("gra", "Choice A", g1)
|
| 54 | 54 |
.refOption("grb", "Choice B", g2)
|
| 55 | 55 |
.refOption("grc", "Choice C", g3);
|
| 56 | 56 |
// Bundle -gr* options into a group |
| 57 | 57 |
ap.optionGroup("gr", "gra")
|
| 58 | 58 |
.optionGroup("gr", "grb")
|
| 59 | 59 |
.optionGroup("gr", "grc");
|
| 60 | 60 |
// Set the group mandatory |
| 61 | 61 |
ap.mandatoryGroup("gr");
|
| 62 | 62 |
// Set the options of the group exclusive (only one option can be given) |
| 63 | 63 |
ap.onlyOneGroup("gr");
|
| 64 | 64 |
// Add non-parsed arguments (e.g. input files) |
| 65 | 65 |
ap.other("infile", "The input file.")
|
| 66 | 66 |
.other("...");
|
| 67 | 67 |
|
| 68 | 68 |
// Perform the parsing process |
| 69 | 69 |
// (in case of any error it terminates the program) |
| 70 | 70 |
ap.parse(); |
| 71 | 71 |
|
| 72 | 72 |
// Check each option if it has been given and print its value |
| 73 | 73 |
std::cout << "Parameters of '" << ap.commandName() << "':\n"; |
| 74 | 74 |
|
| 75 | 75 |
std::cout << " Value of -n: " << i << std::endl; |
| 76 | 76 |
if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl;
|
| 77 | 77 |
if(ap.given("val2")) {
|
| 78 | 78 |
d = ap["val2"]; |
| 79 | 79 |
std::cout << " Value of -val2: " << d << std::endl; |
| 80 | 80 |
} |
| 81 | 81 |
if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl;
|
| 82 | 82 |
if(ap.given("f")) std::cout << " -f is given\n";
|
| 83 | 83 |
if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << nh << std::endl;
|
| 84 | 84 |
if(ap.given("gra")) std::cout << " -gra is given\n";
|
| 85 | 85 |
if(ap.given("grb")) std::cout << " -grb is given\n";
|
| 86 | 86 |
if(ap.given("grc")) std::cout << " -grc is given\n";
|
| 87 | 87 |
|
| 88 | 88 |
switch(ap.files().size()) {
|
| 89 | 89 |
case 0: |
| 90 | 90 |
std::cout << " No file argument was given.\n"; |
| 91 | 91 |
break; |
| 92 | 92 |
case 1: |
| 93 | 93 |
std::cout << " 1 file argument was given. It is:\n"; |
| 94 | 94 |
break; |
| 95 | 95 |
default: |
| 96 | 96 |
std::cout << " " |
| 97 | 97 |
<< ap.files().size() << " file arguments were given. They are:\n"; |
| 98 | 98 |
} |
| 99 | 99 |
for(unsigned int i=0;i<ap.files().size();++i) |
| 100 | 100 |
std::cout << " '" << ap.files()[i] << "'\n"; |
| 101 | 101 |
|
| 102 | 102 |
return 0; |
| 103 | 103 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/// \ingroup demos |
| 20 | 20 |
/// \file |
| 21 | 21 |
/// \brief Demo of the graph drawing function \ref graphToEps() |
| 22 | 22 |
/// |
| 23 | 23 |
/// This demo program shows examples how to use the function \ref |
| 24 | 24 |
/// graphToEps(). It takes no input but simply creates seven |
| 25 | 25 |
/// <tt>.eps</tt> files demonstrating the capability of \ref |
| 26 | 26 |
/// graphToEps(), and showing how to draw directed graphs, |
| 27 | 27 |
/// how to handle parallel egdes, how to change the properties (like |
| 28 | 28 |
/// color, shape, size, title etc.) of nodes and arcs individually |
| 29 | 29 |
/// using appropriate graph maps. |
| 30 | 30 |
/// |
| 31 | 31 |
/// \include graph_to_eps_demo.cc |
| 32 | 32 |
|
| 33 | 33 |
#include<lemon/list_graph.h> |
| 34 | 34 |
#include<lemon/graph_to_eps.h> |
| 35 | 35 |
#include<lemon/math.h> |
| 36 | 36 |
|
| 37 | 37 |
using namespace std; |
| 38 | 38 |
using namespace lemon; |
| 39 | 39 |
|
| 40 | 40 |
int main() |
| 41 | 41 |
{
|
| 42 | 42 |
Palette palette; |
| 43 | 43 |
Palette paletteW(true); |
| 44 | 44 |
|
| 45 | 45 |
// Create a small digraph |
| 46 | 46 |
ListDigraph g; |
| 47 | 47 |
typedef ListDigraph::Node Node; |
| 48 | 48 |
typedef ListDigraph::NodeIt NodeIt; |
| 49 | 49 |
typedef ListDigraph::Arc Arc; |
| 50 | 50 |
typedef dim2::Point<int> Point; |
| 51 | 51 |
|
| 52 | 52 |
Node n1=g.addNode(); |
| 53 | 53 |
Node n2=g.addNode(); |
| 54 | 54 |
Node n3=g.addNode(); |
| 55 | 55 |
Node n4=g.addNode(); |
| 56 | 56 |
Node n5=g.addNode(); |
| 57 | 57 |
|
| 58 | 58 |
ListDigraph::NodeMap<Point> coords(g); |
| 59 | 59 |
ListDigraph::NodeMap<double> sizes(g); |
| 60 | 60 |
ListDigraph::NodeMap<int> colors(g); |
| 61 | 61 |
ListDigraph::NodeMap<int> shapes(g); |
| 62 | 62 |
ListDigraph::ArcMap<int> acolors(g); |
| 63 | 63 |
ListDigraph::ArcMap<int> widths(g); |
| 64 | 64 |
|
| 65 | 65 |
coords[n1]=Point(50,50); sizes[n1]=1; colors[n1]=1; shapes[n1]=0; |
| 66 | 66 |
coords[n2]=Point(50,70); sizes[n2]=2; colors[n2]=2; shapes[n2]=2; |
| 67 | 67 |
coords[n3]=Point(70,70); sizes[n3]=1; colors[n3]=3; shapes[n3]=0; |
| 68 | 68 |
coords[n4]=Point(70,50); sizes[n4]=2; colors[n4]=4; shapes[n4]=1; |
| 69 | 69 |
coords[n5]=Point(85,60); sizes[n5]=3; colors[n5]=5; shapes[n5]=2; |
| 70 | 70 |
|
| 71 | 71 |
Arc a; |
| 72 | 72 |
|
| 73 | 73 |
a=g.addArc(n1,n2); acolors[a]=0; widths[a]=1; |
| 74 | 74 |
a=g.addArc(n2,n3); acolors[a]=0; widths[a]=1; |
| 75 | 75 |
a=g.addArc(n3,n5); acolors[a]=0; widths[a]=3; |
| 76 | 76 |
a=g.addArc(n5,n4); acolors[a]=0; widths[a]=1; |
| 77 | 77 |
a=g.addArc(n4,n1); acolors[a]=0; widths[a]=1; |
| 78 | 78 |
a=g.addArc(n2,n4); acolors[a]=1; widths[a]=2; |
| 79 | 79 |
a=g.addArc(n3,n4); acolors[a]=2; widths[a]=1; |
| 80 | 80 |
|
| 81 | 81 |
IdMap<ListDigraph,Node> id(g); |
| 82 | 82 |
|
| 83 | 83 |
// Create .eps files showing the digraph with different options |
| 84 | 84 |
cout << "Create 'graph_to_eps_demo_out_1_pure.eps'" << endl; |
| 85 | 85 |
graphToEps(g,"graph_to_eps_demo_out_1_pure.eps"). |
| 86 | 86 |
coords(coords). |
| 87 | 87 |
title("Sample .eps figure").
|
| 88 |
copyright("(C) 2003-
|
|
| 88 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 89 | 89 |
run(); |
| 90 | 90 |
|
| 91 | 91 |
cout << "Create 'graph_to_eps_demo_out_2.eps'" << endl; |
| 92 | 92 |
graphToEps(g,"graph_to_eps_demo_out_2.eps"). |
| 93 | 93 |
coords(coords). |
| 94 | 94 |
title("Sample .eps figure").
|
| 95 |
copyright("(C) 2003-
|
|
| 95 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 96 | 96 |
absoluteNodeSizes().absoluteArcWidths(). |
| 97 | 97 |
nodeScale(2).nodeSizes(sizes). |
| 98 | 98 |
nodeShapes(shapes). |
| 99 | 99 |
nodeColors(composeMap(palette,colors)). |
| 100 | 100 |
arcColors(composeMap(palette,acolors)). |
| 101 | 101 |
arcWidthScale(.4).arcWidths(widths). |
| 102 | 102 |
nodeTexts(id).nodeTextSize(3). |
| 103 | 103 |
run(); |
| 104 | 104 |
|
| 105 | 105 |
cout << "Create 'graph_to_eps_demo_out_3_arr.eps'" << endl; |
| 106 | 106 |
graphToEps(g,"graph_to_eps_demo_out_3_arr.eps"). |
| 107 | 107 |
title("Sample .eps figure (with arrowheads)").
|
| 108 |
copyright("(C) 2003-
|
|
| 108 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 109 | 109 |
absoluteNodeSizes().absoluteArcWidths(). |
| 110 | 110 |
nodeColors(composeMap(palette,colors)). |
| 111 | 111 |
coords(coords). |
| 112 | 112 |
nodeScale(2).nodeSizes(sizes). |
| 113 | 113 |
nodeShapes(shapes). |
| 114 | 114 |
arcColors(composeMap(palette,acolors)). |
| 115 | 115 |
arcWidthScale(.4).arcWidths(widths). |
| 116 | 116 |
nodeTexts(id).nodeTextSize(3). |
| 117 | 117 |
drawArrows().arrowWidth(2).arrowLength(2). |
| 118 | 118 |
run(); |
| 119 | 119 |
|
| 120 | 120 |
// Add more arcs to the digraph |
| 121 | 121 |
a=g.addArc(n1,n4); acolors[a]=2; widths[a]=1; |
| 122 | 122 |
a=g.addArc(n4,n1); acolors[a]=1; widths[a]=2; |
| 123 | 123 |
|
| 124 | 124 |
a=g.addArc(n1,n2); acolors[a]=1; widths[a]=1; |
| 125 | 125 |
a=g.addArc(n1,n2); acolors[a]=2; widths[a]=1; |
| 126 | 126 |
a=g.addArc(n1,n2); acolors[a]=3; widths[a]=1; |
| 127 | 127 |
a=g.addArc(n1,n2); acolors[a]=4; widths[a]=1; |
| 128 | 128 |
a=g.addArc(n1,n2); acolors[a]=5; widths[a]=1; |
| 129 | 129 |
a=g.addArc(n1,n2); acolors[a]=6; widths[a]=1; |
| 130 | 130 |
a=g.addArc(n1,n2); acolors[a]=7; widths[a]=1; |
| 131 | 131 |
|
| 132 | 132 |
cout << "Create 'graph_to_eps_demo_out_4_par.eps'" << endl; |
| 133 | 133 |
graphToEps(g,"graph_to_eps_demo_out_4_par.eps"). |
| 134 | 134 |
title("Sample .eps figure (parallel arcs)").
|
| 135 |
copyright("(C) 2003-
|
|
| 135 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 136 | 136 |
absoluteNodeSizes().absoluteArcWidths(). |
| 137 | 137 |
nodeShapes(shapes). |
| 138 | 138 |
coords(coords). |
| 139 | 139 |
nodeScale(2).nodeSizes(sizes). |
| 140 | 140 |
nodeColors(composeMap(palette,colors)). |
| 141 | 141 |
arcColors(composeMap(palette,acolors)). |
| 142 | 142 |
arcWidthScale(.4).arcWidths(widths). |
| 143 | 143 |
nodeTexts(id).nodeTextSize(3). |
| 144 | 144 |
enableParallel().parArcDist(1.5). |
| 145 | 145 |
run(); |
| 146 | 146 |
|
| 147 | 147 |
cout << "Create 'graph_to_eps_demo_out_5_par_arr.eps'" << endl; |
| 148 | 148 |
graphToEps(g,"graph_to_eps_demo_out_5_par_arr.eps"). |
| 149 | 149 |
title("Sample .eps figure (parallel arcs and arrowheads)").
|
| 150 |
copyright("(C) 2003-
|
|
| 150 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 151 | 151 |
absoluteNodeSizes().absoluteArcWidths(). |
| 152 | 152 |
nodeScale(2).nodeSizes(sizes). |
| 153 | 153 |
coords(coords). |
| 154 | 154 |
nodeShapes(shapes). |
| 155 | 155 |
nodeColors(composeMap(palette,colors)). |
| 156 | 156 |
arcColors(composeMap(palette,acolors)). |
| 157 | 157 |
arcWidthScale(.3).arcWidths(widths). |
| 158 | 158 |
nodeTexts(id).nodeTextSize(3). |
| 159 | 159 |
enableParallel().parArcDist(1). |
| 160 | 160 |
drawArrows().arrowWidth(1).arrowLength(1). |
| 161 | 161 |
run(); |
| 162 | 162 |
|
| 163 | 163 |
cout << "Create 'graph_to_eps_demo_out_6_par_arr_a4.eps'" << endl; |
| 164 | 164 |
graphToEps(g,"graph_to_eps_demo_out_6_par_arr_a4.eps"). |
| 165 | 165 |
title("Sample .eps figure (fits to A4)").
|
| 166 |
copyright("(C) 2003-
|
|
| 166 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 167 | 167 |
scaleToA4(). |
| 168 | 168 |
absoluteNodeSizes().absoluteArcWidths(). |
| 169 | 169 |
nodeScale(2).nodeSizes(sizes). |
| 170 | 170 |
coords(coords). |
| 171 | 171 |
nodeShapes(shapes). |
| 172 | 172 |
nodeColors(composeMap(palette,colors)). |
| 173 | 173 |
arcColors(composeMap(palette,acolors)). |
| 174 | 174 |
arcWidthScale(.3).arcWidths(widths). |
| 175 | 175 |
nodeTexts(id).nodeTextSize(3). |
| 176 | 176 |
enableParallel().parArcDist(1). |
| 177 | 177 |
drawArrows().arrowWidth(1).arrowLength(1). |
| 178 | 178 |
run(); |
| 179 | 179 |
|
| 180 | 180 |
// Create an .eps file showing the colors of a default Palette |
| 181 | 181 |
ListDigraph h; |
| 182 | 182 |
ListDigraph::NodeMap<int> hcolors(h); |
| 183 | 183 |
ListDigraph::NodeMap<Point> hcoords(h); |
| 184 | 184 |
|
| 185 | 185 |
int cols=int(sqrt(double(palette.size()))); |
| 186 | 186 |
for(int i=0;i<int(paletteW.size());i++) {
|
| 187 | 187 |
Node n=h.addNode(); |
| 188 | 188 |
hcoords[n]=Point(1+i%cols,1+i/cols); |
| 189 | 189 |
hcolors[n]=i; |
| 190 | 190 |
} |
| 191 | 191 |
|
| 192 | 192 |
cout << "Create 'graph_to_eps_demo_out_7_colors.eps'" << endl; |
| 193 | 193 |
graphToEps(h,"graph_to_eps_demo_out_7_colors.eps"). |
| 194 | 194 |
scale(60). |
| 195 | 195 |
title("Sample .eps figure (Palette demo)").
|
| 196 |
copyright("(C) 2003-
|
|
| 196 |
copyright("(C) 2003-2009 LEMON Project").
|
|
| 197 | 197 |
coords(hcoords). |
| 198 | 198 |
absoluteNodeSizes().absoluteArcWidths(). |
| 199 | 199 |
nodeScale(.45). |
| 200 | 200 |
distantColorNodeTexts(). |
| 201 | 201 |
nodeTexts(hcolors).nodeTextSize(.6). |
| 202 | 202 |
nodeColors(composeMap(paletteW,hcolors)). |
| 203 | 203 |
run(); |
| 204 | 204 |
|
| 205 | 205 |
return 0; |
| 206 | 206 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup demos |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief Demonstrating graph input and output |
| 22 | 22 |
/// |
| 23 | 23 |
/// This program gives an example of how to read and write a digraph |
| 24 | 24 |
/// and additional maps from/to a stream or a file using the |
| 25 | 25 |
/// \ref lgf-format "LGF" format. |
| 26 | 26 |
/// |
| 27 | 27 |
/// The \c "digraph.lgf" file: |
| 28 | 28 |
/// \include digraph.lgf |
| 29 | 29 |
/// |
| 30 | 30 |
/// And the program which reads it and prints the digraph to the |
| 31 | 31 |
/// standard output: |
| 32 | 32 |
/// \include lgf_demo.cc |
| 33 | 33 |
|
| 34 | 34 |
#include <iostream> |
| 35 | 35 |
#include <lemon/smart_graph.h> |
| 36 | 36 |
#include <lemon/lgf_reader.h> |
| 37 | 37 |
#include <lemon/lgf_writer.h> |
| 38 | 38 |
|
| 39 | 39 |
using namespace lemon; |
| 40 | 40 |
|
| 41 | 41 |
int main() {
|
| 42 | 42 |
SmartDigraph g; |
| 43 | 43 |
SmartDigraph::ArcMap<int> cap(g); |
| 44 | 44 |
SmartDigraph::Node s, t; |
| 45 | 45 |
|
| 46 | 46 |
try {
|
| 47 | 47 |
digraphReader(g, "digraph.lgf"). // read the directed graph into g |
| 48 | 48 |
arcMap("capacity", cap). // read the 'capacity' arc map into cap
|
| 49 | 49 |
node("source", s). // read 'source' node to s
|
| 50 | 50 |
node("target", t). // read 'target' node to t
|
| 51 | 51 |
run(); |
| 52 | 52 |
} catch (Exception& error) { // check if there was any error
|
| 53 | 53 |
std::cerr << "Error: " << error.what() << std::endl; |
| 54 | 54 |
return -1; |
| 55 | 55 |
} |
| 56 | 56 |
|
| 57 | 57 |
std::cout << "A digraph is read from 'digraph.lgf'." << std::endl; |
| 58 | 58 |
std::cout << "Number of nodes: " << countNodes(g) << std::endl; |
| 59 | 59 |
std::cout << "Number of arcs: " << countArcs(g) << std::endl; |
| 60 | 60 |
|
| 61 | 61 |
std::cout << "We can write it to the standard output:" << std::endl; |
| 62 | 62 |
|
| 63 | 63 |
digraphWriter(g). // write g to the standard output |
| 64 | 64 |
arcMap("capacity", cap). // write cap into 'capacity'
|
| 65 | 65 |
node("source", s). // write s to 'source'
|
| 66 | 66 |
node("target", t). // write t to 'target'
|
| 67 | 67 |
run(); |
| 68 | 68 |
|
| 69 | 69 |
return 0; |
| 70 | 70 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/*! |
| 20 | 20 |
|
| 21 | 21 |
\page coding_style LEMON Coding Style |
| 22 | 22 |
|
| 23 | 23 |
\section naming_conv Naming Conventions |
| 24 | 24 |
|
| 25 | 25 |
In order to make development easier we have made some conventions |
| 26 | 26 |
according to coding style. These include names of types, classes, |
| 27 | 27 |
functions, variables, constants and exceptions. If these conventions |
| 28 | 28 |
are met in one's code then it is easier to read and maintain |
| 29 | 29 |
it. Please comply with these conventions if you want to contribute |
| 30 | 30 |
developing LEMON library. |
| 31 | 31 |
|
| 32 | 32 |
\note When the coding style requires the capitalization of an abbreviation, |
| 33 | 33 |
only the first letter should be upper case. |
| 34 | 34 |
|
| 35 | 35 |
\code |
| 36 | 36 |
XmlReader |
| 37 | 37 |
\endcode |
| 38 | 38 |
|
| 39 | 39 |
|
| 40 | 40 |
\warning In some cases we diverge from these rules. |
| 41 | 41 |
This is primary done because STL uses different naming convention and |
| 42 | 42 |
in certain cases |
| 43 | 43 |
it is beneficial to provide STL compatible interface. |
| 44 | 44 |
|
| 45 | 45 |
\subsection cs-files File Names |
| 46 | 46 |
|
| 47 | 47 |
The header file names should look like the following. |
| 48 | 48 |
|
| 49 | 49 |
\code |
| 50 | 50 |
header_file.h |
| 51 | 51 |
\endcode |
| 52 | 52 |
|
| 53 | 53 |
Note that all standard LEMON headers are located in the \c lemon subdirectory, |
| 54 | 54 |
so you should include them from C++ source like this: |
| 55 | 55 |
|
| 56 | 56 |
\code |
| 57 | 57 |
#include <lemon/header_file.h> |
| 58 | 58 |
\endcode |
| 59 | 59 |
|
| 60 | 60 |
The source code files use the same style and they have '.cc' extension. |
| 61 | 61 |
|
| 62 | 62 |
\code |
| 63 | 63 |
source_code.cc |
| 64 | 64 |
\endcode |
| 65 | 65 |
|
| 66 | 66 |
\subsection cs-class Classes and other types |
| 67 | 67 |
|
| 68 | 68 |
The name of a class or any type should look like the following. |
| 69 | 69 |
|
| 70 | 70 |
\code |
| 71 | 71 |
AllWordsCapitalizedWithoutUnderscores |
| 72 | 72 |
\endcode |
| 73 | 73 |
|
| 74 | 74 |
\subsection cs-func Methods and other functions |
| 75 | 75 |
|
| 76 | 76 |
The name of a function should look like the following. |
| 77 | 77 |
|
| 78 | 78 |
\code |
| 79 | 79 |
firstWordLowerCaseRestCapitalizedWithoutUnderscores |
| 80 | 80 |
\endcode |
| 81 | 81 |
|
| 82 | 82 |
\subsection cs-funcs Constants, Macros |
| 83 | 83 |
|
| 84 | 84 |
The names of constants and macros should look like the following. |
| 85 | 85 |
|
| 86 | 86 |
\code |
| 87 | 87 |
ALL_UPPER_CASE_WITH_UNDERSCORES |
| 88 | 88 |
\endcode |
| 89 | 89 |
|
| 90 | 90 |
\subsection cs-loc-var Class and instance member variables, auto variables |
| 91 | 91 |
|
| 92 | 92 |
The names of class and instance member variables and auto variables |
| 93 | 93 |
(=variables used locally in methods) should look like the following. |
| 94 | 94 |
|
| 95 | 95 |
\code |
| 96 | 96 |
all_lower_case_with_underscores |
| 97 | 97 |
\endcode |
| 98 | 98 |
|
| 99 | 99 |
\subsection pri-loc-var Private member variables |
| 100 | 100 |
|
| 101 | 101 |
Private member variables should start with underscore |
| 102 | 102 |
|
| 103 | 103 |
\code |
| 104 | 104 |
_start_with_underscores |
| 105 | 105 |
\endcode |
| 106 | 106 |
|
| 107 | 107 |
\subsection cs-excep Exceptions |
| 108 | 108 |
|
| 109 | 109 |
When writing exceptions please comply the following naming conventions. |
| 110 | 110 |
|
| 111 | 111 |
\code |
| 112 | 112 |
ClassNameEndsWithException |
| 113 | 113 |
\endcode |
| 114 | 114 |
|
| 115 | 115 |
or |
| 116 | 116 |
|
| 117 | 117 |
\code |
| 118 | 118 |
ClassNameEndsWithError |
| 119 | 119 |
\endcode |
| 120 | 120 |
|
| 121 | 121 |
\section header-template Template Header File |
| 122 | 122 |
|
| 123 | 123 |
Each LEMON header file should look like this: |
| 124 | 124 |
|
| 125 | 125 |
\include template.h |
| 126 | 126 |
|
| 127 | 127 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/** |
| 20 | 20 |
\dir demo |
| 21 | 21 |
\brief A collection of demo applications. |
| 22 | 22 |
|
| 23 | 23 |
This directory contains several simple demo applications, mainly |
| 24 | 24 |
for educational purposes. |
| 25 | 25 |
*/ |
| 26 | 26 |
|
| 27 | 27 |
/** |
| 28 | 28 |
\dir doc |
| 29 | 29 |
\brief Auxiliary (and the whole generated) documentation. |
| 30 | 30 |
|
| 31 | 31 |
This directory contains some auxiliary pages and the whole generated |
| 32 | 32 |
documentation. |
| 33 | 33 |
*/ |
| 34 | 34 |
|
| 35 | 35 |
/** |
| 36 | 36 |
\dir test |
| 37 | 37 |
\brief Test programs. |
| 38 | 38 |
|
| 39 | 39 |
This directory contains several test programs that check the consistency |
| 40 | 40 |
of the code. |
| 41 | 41 |
*/ |
| 42 | 42 |
|
| 43 | 43 |
/** |
| 44 | 44 |
\dir tools |
| 45 | 45 |
\brief Some useful executables. |
| 46 | 46 |
|
| 47 | 47 |
This directory contains the sources of some useful complete executables. |
| 48 | 48 |
*/ |
| 49 | 49 |
|
| 50 | 50 |
/** |
| 51 | 51 |
\dir lemon |
| 52 | 52 |
\brief Base include directory of LEMON. |
| 53 | 53 |
|
| 54 | 54 |
This is the base directory of LEMON includes, so each include file must be |
| 55 | 55 |
prefixed with this, e.g. |
| 56 | 56 |
\code |
| 57 | 57 |
#include<lemon/list_graph.h> |
| 58 | 58 |
#include<lemon/dijkstra.h> |
| 59 | 59 |
\endcode |
| 60 | 60 |
*/ |
| 61 | 61 |
|
| 62 | 62 |
/** |
| 63 | 63 |
\dir concepts |
| 64 | 64 |
\brief Concept descriptors and checking classes. |
| 65 | 65 |
|
| 66 | 66 |
This directory contains the concept descriptors and concept checking tools. |
| 67 | 67 |
For more information see the \ref concept "Concepts" module. |
| 68 | 68 |
*/ |
| 69 | 69 |
|
| 70 | 70 |
/** |
| 71 | 71 |
\dir bits |
| 72 | 72 |
\brief Auxiliary tools for implementation. |
| 73 | 73 |
|
| 74 |
This directory contains some auxiliary classes for implementing graphs, |
|
| 74 |
This directory contains some auxiliary classes for implementing graphs, |
|
| 75 | 75 |
maps and some other classes. |
| 76 | 76 |
As a user you typically don't have to deal with these files. |
| 77 | 77 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
namespace lemon {
|
| 20 | 20 |
|
| 21 | 21 |
/** |
| 22 | 22 |
@defgroup datas Data Structures |
| 23 | 23 |
This group describes the several data structures implemented in LEMON. |
| 24 | 24 |
*/ |
| 25 | 25 |
|
| 26 | 26 |
/** |
| 27 | 27 |
@defgroup graphs Graph Structures |
| 28 | 28 |
@ingroup datas |
| 29 | 29 |
\brief Graph structures implemented in LEMON. |
| 30 | 30 |
|
| 31 | 31 |
The implementation of combinatorial algorithms heavily relies on |
| 32 | 32 |
efficient graph implementations. LEMON offers data structures which are |
| 33 | 33 |
planned to be easily used in an experimental phase of implementation studies, |
| 34 | 34 |
and thereafter the program code can be made efficient by small modifications. |
| 35 | 35 |
|
| 36 | 36 |
The most efficient implementation of diverse applications require the |
| 37 | 37 |
usage of different physical graph implementations. These differences |
| 38 | 38 |
appear in the size of graph we require to handle, memory or time usage |
| 39 | 39 |
limitations or in the set of operations through which the graph can be |
| 40 | 40 |
accessed. LEMON provides several physical graph structures to meet |
| 41 | 41 |
the diverging requirements of the possible users. In order to save on |
| 42 | 42 |
running time or on memory usage, some structures may fail to provide |
| 43 | 43 |
some graph features like arc/edge or node deletion. |
| 44 | 44 |
|
| 45 | 45 |
Alteration of standard containers need a very limited number of |
| 46 | 46 |
operations, these together satisfy the everyday requirements. |
| 47 | 47 |
In the case of graph structures, different operations are needed which do |
| 48 | 48 |
not alter the physical graph, but gives another view. If some nodes or |
| 49 | 49 |
arcs have to be hidden or the reverse oriented graph have to be used, then |
| 50 | 50 |
this is the case. It also may happen that in a flow implementation |
| 51 | 51 |
the residual graph can be accessed by another algorithm, or a node-set |
| 52 | 52 |
is to be shrunk for another algorithm. |
| 53 | 53 |
LEMON also provides a variety of graphs for these requirements called |
| 54 | 54 |
\ref graph_adaptors "graph adaptors". Adaptors cannot be used alone but only |
| 55 | 55 |
in conjunction with other graph representations. |
| 56 | 56 |
|
| 57 | 57 |
You are free to use the graph structure that fit your requirements |
| 58 | 58 |
the best, most graph algorithms and auxiliary data structures can be used |
| 59 | 59 |
with any graph structure. |
| 60 | 60 |
|
| 61 | 61 |
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts". |
| 62 | 62 |
*/ |
| 63 | 63 |
|
| 64 | 64 |
/** |
| 65 | 65 |
@defgroup graph_adaptors Adaptor Classes for graphs |
| 66 | 66 |
@ingroup graphs |
| 67 | 67 |
\brief This group contains several adaptor classes for digraphs and graphs |
| 68 | 68 |
|
| 69 | 69 |
The main parts of LEMON are the different graph structures, generic |
| 70 | 70 |
graph algorithms, graph concepts which couple these, and graph |
| 71 | 71 |
adaptors. While the previous notions are more or less clear, the |
| 72 | 72 |
latter one needs further explanation. Graph adaptors are graph classes |
| 73 | 73 |
which serve for considering graph structures in different ways. |
| 74 | 74 |
|
| 75 | 75 |
A short example makes this much clearer. Suppose that we have an |
| 76 | 76 |
instance \c g of a directed graph type say ListDigraph and an algorithm |
| 77 | 77 |
\code |
| 78 | 78 |
template <typename Digraph> |
| 79 | 79 |
int algorithm(const Digraph&); |
| 80 | 80 |
\endcode |
| 81 | 81 |
is needed to run on the reverse oriented graph. It may be expensive |
| 82 | 82 |
(in time or in memory usage) to copy \c g with the reversed |
| 83 | 83 |
arcs. In this case, an adaptor class is used, which (according |
| 84 | 84 |
to LEMON digraph concepts) works as a digraph. The adaptor uses the |
| 85 | 85 |
original digraph structure and digraph operations when methods of the |
| 86 | 86 |
reversed oriented graph are called. This means that the adaptor have |
| 87 | 87 |
minor memory usage, and do not perform sophisticated algorithmic |
| 88 | 88 |
actions. The purpose of it is to give a tool for the cases when a |
| 89 | 89 |
graph have to be used in a specific alteration. If this alteration is |
| 90 | 90 |
obtained by a usual construction like filtering the arc-set or |
| 91 | 91 |
considering a new orientation, then an adaptor is worthwhile to use. |
| 92 | 92 |
To come back to the reverse oriented graph, in this situation |
| 93 | 93 |
\code |
| 94 | 94 |
template<typename Digraph> class ReverseDigraph; |
| 95 | 95 |
\endcode |
| 96 | 96 |
template class can be used. The code looks as follows |
| 97 | 97 |
\code |
| 98 | 98 |
ListDigraph g; |
| 99 | 99 |
ReverseDigraph<ListGraph> rg(g); |
| 100 | 100 |
int result = algorithm(rg); |
| 101 | 101 |
\endcode |
| 102 | 102 |
After running the algorithm, the original graph \c g is untouched. |
| 103 | 103 |
This techniques gives rise to an elegant code, and based on stable |
| 104 | 104 |
graph adaptors, complex algorithms can be implemented easily. |
| 105 | 105 |
|
| 106 | 106 |
In flow, circulation and bipartite matching problems, the residual |
| 107 | 107 |
graph is of particular importance. Combining an adaptor implementing |
| 108 | 108 |
this, shortest path algorithms and minimum mean cycle algorithms, |
| 109 | 109 |
a range of weighted and cardinality optimization algorithms can be |
| 110 | 110 |
obtained. For other examples, the interested user is referred to the |
| 111 | 111 |
detailed documentation of particular adaptors. |
| 112 | 112 |
|
| 113 | 113 |
The behavior of graph adaptors can be very different. Some of them keep |
| 114 | 114 |
capabilities of the original graph while in other cases this would be |
| 115 | 115 |
meaningless. This means that the concepts that they are models of depend |
| 116 | 116 |
on the graph adaptor, and the wrapped graph(s). |
| 117 | 117 |
If an arc of \c rg is deleted, this is carried out by deleting the |
| 118 | 118 |
corresponding arc of \c g, thus the adaptor modifies the original graph. |
| 119 | 119 |
|
| 120 | 120 |
But for a residual graph, this operation has no sense. |
| 121 | 121 |
Let us stand one more example here to simplify your work. |
| 122 | 122 |
RevGraphAdaptor has constructor |
| 123 | 123 |
\code |
| 124 | 124 |
ReverseDigraph(Digraph& digraph); |
| 125 | 125 |
\endcode |
| 126 | 126 |
This means that in a situation, when a <tt>const ListDigraph&</tt> |
| 127 | 127 |
reference to a graph is given, then it have to be instantiated with |
| 128 | 128 |
<tt>Digraph=const ListDigraph</tt>. |
| 129 | 129 |
\code |
| 130 | 130 |
int algorithm1(const ListDigraph& g) {
|
| 131 | 131 |
RevGraphAdaptor<const ListDigraph> rg(g); |
| 132 | 132 |
return algorithm2(rg); |
| 133 | 133 |
} |
| 134 | 134 |
\endcode |
| 135 | 135 |
*/ |
| 136 | 136 |
|
| 137 | 137 |
/** |
| 138 | 138 |
@defgroup semi_adaptors Semi-Adaptor Classes for Graphs |
| 139 | 139 |
@ingroup graphs |
| 140 | 140 |
\brief Graph types between real graphs and graph adaptors. |
| 141 | 141 |
|
| 142 | 142 |
This group describes some graph types between real graphs and graph adaptors. |
| 143 | 143 |
These classes wrap graphs to give new functionality as the adaptors do it. |
| 144 | 144 |
On the other hand they are not light-weight structures as the adaptors. |
| 145 | 145 |
*/ |
| 146 | 146 |
|
| 147 | 147 |
/** |
| 148 | 148 |
@defgroup maps Maps |
| 149 | 149 |
@ingroup datas |
| 150 | 150 |
\brief Map structures implemented in LEMON. |
| 151 | 151 |
|
| 152 | 152 |
This group describes the map structures implemented in LEMON. |
| 153 | 153 |
|
| 154 | 154 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
| 155 | 155 |
new maps from existing ones. |
| 156 | 156 |
|
| 157 | 157 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
| 158 | 158 |
*/ |
| 159 | 159 |
|
| 160 | 160 |
/** |
| 161 | 161 |
@defgroup graph_maps Graph Maps |
| 162 | 162 |
@ingroup maps |
| 163 | 163 |
\brief Special graph-related maps. |
| 164 | 164 |
|
| 165 | 165 |
This group describes maps that are specifically designed to assign |
| 166 | 166 |
values to the nodes and arcs/edges of graphs. |
| 167 | 167 |
|
| 168 | 168 |
If you are looking for the standard graph maps (\c NodeMap, \c ArcMap, |
| 169 | 169 |
\c EdgeMap), see the \ref graph_concepts "Graph Structure Concepts". |
| 170 | 170 |
*/ |
| 171 | 171 |
|
| 172 | 172 |
/** |
| 173 | 173 |
\defgroup map_adaptors Map Adaptors |
| 174 | 174 |
\ingroup maps |
| 175 | 175 |
\brief Tools to create new maps from existing ones |
| 176 | 176 |
|
| 177 | 177 |
This group describes map adaptors that are used to create "implicit" |
| 178 | 178 |
maps from other maps. |
| 179 | 179 |
|
| 180 | 180 |
Most of them are \ref concepts::ReadMap "read-only maps". |
| 181 | 181 |
They can make arithmetic and logical operations between one or two maps |
| 182 | 182 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
| 183 | 183 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
| 184 | 184 |
|
| 185 | 185 |
The typical usage of this classes is passing implicit maps to |
| 186 | 186 |
algorithms. If a function type algorithm is called then the function |
| 187 | 187 |
type map adaptors can be used comfortable. For example let's see the |
| 188 | 188 |
usage of map adaptors with the \c graphToEps() function. |
| 189 | 189 |
\code |
| 190 | 190 |
Color nodeColor(int deg) {
|
| 191 | 191 |
if (deg >= 2) {
|
| 192 | 192 |
return Color(0.5, 0.0, 0.5); |
| 193 | 193 |
} else if (deg == 1) {
|
| 194 | 194 |
return Color(1.0, 0.5, 1.0); |
| 195 | 195 |
} else {
|
| 196 | 196 |
return Color(0.0, 0.0, 0.0); |
| 197 | 197 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
namespace lemon {
|
| 20 | 20 |
/*! |
| 21 | 21 |
|
| 22 | 22 |
|
| 23 | 23 |
|
| 24 | 24 |
\page lgf-format LEMON Graph Format (LGF) |
| 25 | 25 |
|
| 26 | 26 |
The \e LGF is a <em>column oriented</em> |
| 27 | 27 |
file format for storing graphs and associated data like |
| 28 | 28 |
node and edge maps. |
| 29 | 29 |
|
| 30 | 30 |
Each line with \c '#' first non-whitespace |
| 31 | 31 |
character is considered as a comment line. |
| 32 | 32 |
|
| 33 | 33 |
Otherwise the file consists of sections starting with |
| 34 | 34 |
a header line. The header lines starts with an \c '@' character followed by the |
| 35 | 35 |
type of section. The standard section types are \c \@nodes, \c |
| 36 | 36 |
\@arcs and \c \@edges |
| 37 | 37 |
and \@attributes. Each header line may also have an optional |
| 38 | 38 |
\e name, which can be use to distinguish the sections of the same |
| 39 | 39 |
type. |
| 40 | 40 |
|
| 41 | 41 |
The standard sections are column oriented, each line consists of |
| 42 | 42 |
<em>token</em>s separated by whitespaces. A token can be \e plain or |
| 43 | 43 |
\e quoted. A plain token is just a sequence of non-whitespace characters, |
| 44 | 44 |
while a quoted token is a |
| 45 | 45 |
character sequence surrounded by double quotes, and it can also |
| 46 | 46 |
contain whitespaces and escape sequences. |
| 47 | 47 |
|
| 48 | 48 |
The \c \@nodes section describes a set of nodes and associated |
| 49 | 49 |
maps. The first is a header line, its columns are the names of the |
| 50 | 50 |
maps appearing in the following lines. |
| 51 | 51 |
One of the maps must be called \c |
| 52 | 52 |
"label", which plays special role in the file. |
| 53 | 53 |
The following |
| 54 | 54 |
non-empty lines until the next section describes nodes of the |
| 55 | 55 |
graph. Each line contains the values of the node maps |
| 56 | 56 |
associated to the current node. |
| 57 | 57 |
|
| 58 | 58 |
\code |
| 59 | 59 |
@nodes |
| 60 | 60 |
label coordinates size title |
| 61 | 61 |
1 (10,20) 10 "First node" |
| 62 | 62 |
2 (80,80) 8 "Second node" |
| 63 | 63 |
3 (40,10) 10 "Third node" |
| 64 | 64 |
\endcode |
| 65 | 65 |
|
| 66 | 66 |
The \c \@arcs section is very similar to the \c \@nodes section, |
| 67 | 67 |
it again starts with a header line describing the names of the maps, |
| 68 | 68 |
but the \c "label" map is not obligatory here. The following lines |
| 69 | 69 |
describe the arcs. The first two tokens of each line are |
| 70 | 70 |
the source and the target node of the arc, respectively, then come the map |
| 71 | 71 |
values. The source and target tokens must be node labels. |
| 72 | 72 |
|
| 73 | 73 |
\code |
| 74 | 74 |
@arcs |
| 75 | 75 |
capacity |
| 76 | 76 |
1 2 16 |
| 77 | 77 |
1 3 12 |
| 78 | 78 |
2 3 18 |
| 79 | 79 |
\endcode |
| 80 | 80 |
|
| 81 | 81 |
The \c \@edges is just a synonym of \c \@arcs. The \@arcs section can |
| 82 | 82 |
also store the edge set of an undirected graph. In such case there is |
| 83 | 83 |
a conventional method for store arc maps in the file, if two columns |
| 84 | 84 |
has the same caption with \c '+' and \c '-' prefix, then these columns |
| 85 | 85 |
can be regarded as the values of an arc map. |
| 86 | 86 |
|
| 87 | 87 |
The \c \@attributes section contains key-value pairs, each line |
| 88 | 88 |
consists of two tokens, an attribute name, and then an attribute |
| 89 | 89 |
value. The value of the attribute could be also a label value of a |
| 90 | 90 |
node or an edge, or even an edge label prefixed with \c '+' or \c '-', |
| 91 | 91 |
which regards to the forward or backward directed arc of the |
| 92 | 92 |
corresponding edge. |
| 93 | 93 |
|
| 94 | 94 |
\code |
| 95 | 95 |
@attributes |
| 96 | 96 |
source 1 |
| 97 | 97 |
target 3 |
| 98 | 98 |
caption "LEMON test digraph" |
| 99 | 99 |
\endcode |
| 100 | 100 |
|
| 101 | 101 |
The \e LGF can contain extra sections, but there is no restriction on |
| 102 | 102 |
the format of such sections. |
| 103 | 103 |
|
| 104 | 104 |
*/ |
| 105 | 105 |
} |
| 106 | 106 |
|
| 107 | 107 |
// LocalWords: whitespace whitespaces |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/** |
| 20 | 20 |
|
| 21 | 21 |
\page license License Terms |
| 22 | 22 |
|
| 23 | 23 |
\verbinclude LICENSE |
| 24 | 24 |
|
| 25 | 25 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/** |
| 20 | 20 |
\mainpage LEMON Documentation |
| 21 | 21 |
|
| 22 | 22 |
\section intro Introduction |
| 23 | 23 |
|
| 24 | 24 |
\subsection whatis What is LEMON |
| 25 | 25 |
|
| 26 | 26 |
LEMON stands for |
| 27 | 27 |
<b>L</b>ibrary of <b>E</b>fficient <b>M</b>odels |
| 28 | 28 |
and <b>O</b>ptimization in <b>N</b>etworks. |
| 29 | 29 |
It is a C++ template |
| 30 | 30 |
library aimed at combinatorial optimization tasks which |
| 31 | 31 |
often involve in working |
| 32 | 32 |
with graphs. |
| 33 | 33 |
|
| 34 | 34 |
<b> |
| 35 | 35 |
LEMON is an <a class="el" href="http://opensource.org/">open source</a> |
| 36 | 36 |
project. |
| 37 | 37 |
You are free to use it in your commercial or |
| 38 | 38 |
non-commercial applications under very permissive |
| 39 | 39 |
\ref license "license terms". |
| 40 | 40 |
</b> |
| 41 | 41 |
|
| 42 | 42 |
\subsection howtoread How to read the documentation |
| 43 | 43 |
|
| 44 | 44 |
If you want to get a quick start and see the most important features then |
| 45 | 45 |
take a look at our \ref quicktour |
| 46 | 46 |
"Quick Tour to LEMON" which will guide you along. |
| 47 | 47 |
|
| 48 | 48 |
If you already feel like using our library, see the page that tells you |
| 49 | 49 |
\ref getstart "How to start using LEMON". |
| 50 | 50 |
|
| 51 | 51 |
If you |
| 52 | 52 |
want to see how LEMON works, see |
| 53 | 53 |
some \ref demoprograms "demo programs". |
| 54 | 54 |
|
| 55 | 55 |
If you know what you are looking for then try to find it under the |
| 56 | 56 |
<a class="el" href="modules.html">Modules</a> |
| 57 | 57 |
section. |
| 58 | 58 |
|
| 59 | 59 |
If you are a user of the old (0.x) series of LEMON, please check out the |
| 60 | 60 |
\ref migration "Migration Guide" for the backward incompatibilities. |
| 61 | 61 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
namespace lemon {
|
| 20 | 20 |
/*! |
| 21 | 21 |
|
| 22 | 22 |
\page migration Migration from the 0.x Series |
| 23 | 23 |
|
| 24 | 24 |
This guide gives an in depth description on what has changed compared |
| 25 | 25 |
to the 0.x release series. |
| 26 | 26 |
|
| 27 | 27 |
Many of these changes adjusted automatically by the |
| 28 | 28 |
<tt>lemon-0.x-to-1.x.sh</tt> tool. Those requiring manual |
| 29 | 29 |
update are typeset <b>boldface</b>. |
| 30 | 30 |
|
| 31 | 31 |
\section migration-graph Graph Related Name Changes |
| 32 | 32 |
|
| 33 | 33 |
- \ref concepts::Digraph "Directed graphs" are called \c Digraph and |
| 34 | 34 |
they have <tt>Arc</tt>s (instead of <tt>Edge</tt>s), while |
| 35 | 35 |
\ref concepts::Graph "undirected graphs" are called \c Graph |
| 36 | 36 |
(instead of \c UGraph) and they have <tt>Edge</tt>s (instead of |
| 37 | 37 |
<tt>UEdge</tt>s). These changes reflected thoroughly everywhere in |
| 38 | 38 |
the library. Namely, |
| 39 | 39 |
- \c Graph -> \c Digraph |
| 40 | 40 |
- \c %ListGraph -> \c ListDigraph, \c %SmartGraph -> \c SmartDigraph etc. |
| 41 | 41 |
- \c UGraph -> \c Graph |
| 42 | 42 |
- \c ListUGraph -> \c ListGraph, \c SmartUGraph -> \c SmartGraph etc. |
| 43 | 43 |
- \c Edge -> \c Arc, \c UEdge -> \c Edge |
| 44 | 44 |
- \c EdgeMap -> \c ArcMap, \c UEdgeMap -> \c EdgeMap |
| 45 | 45 |
- \c EdgeIt -> \c ArcIt, \c UEdgeIt -> \c EdgeIt |
| 46 | 46 |
- Class names and function names containing the words \c graph, |
| 47 | 47 |
\c ugraph, \e edge or \e arc should also be updated. |
| 48 | 48 |
- <b>The two endpoints of an (\e undirected) \c Edge can be obtained by the |
| 49 | 49 |
<tt>u()</tt> and <tt>v()</tt> member function of the graph |
| 50 | 50 |
(instead of <tt>source()</tt> and <tt>target()</tt>). This change |
| 51 | 51 |
must be done by hand.</b> |
| 52 | 52 |
\n Of course, you can still use <tt>source()</tt> and <tt>target()</tt> |
| 53 | 53 |
for <tt>Arc</tt>s (directed edges). |
| 54 | 54 |
|
| 55 | 55 |
\warning |
| 56 | 56 |
<b>The <tt>lemon-0.x-to-1.x.sh</tt> script replaces the words \c graph, |
| 57 | 57 |
\c ugraph, \c edge and \c uedge in your own identifiers and in |
| 58 | 58 |
strings, comments etc. as well as in all LEMON specific identifiers. |
| 59 | 59 |
So use the script carefully and make a backup copy of your source files |
| 60 | 60 |
before applying the script to them.</b> |
| 61 | 61 |
|
| 62 | 62 |
\section migration-lgf LGF tools |
| 63 | 63 |
- The \ref lgf-format "LGF file format" has changed, |
| 64 | 64 |
<tt>\@nodeset</tt> has changed to <tt>\@nodes</tt>, |
| 65 | 65 |
<tt>\@edgeset</tt> and <tt>\@uedgeset</tt> to <tt>\@arcs</tt> or |
| 66 | 66 |
<tt>\@edges</tt>, which become completely equivalents. The |
| 67 | 67 |
<tt>\@nodes</tt>, <tt>\@edges</tt> and <tt>\@uedges</tt> sections are |
| 68 | 68 |
removed from the format, the content of them should be |
| 69 | 69 |
the part of <tt>\@attributes</tt> section. The data fields in |
| 70 | 70 |
the sections must follow a strict format, they must be either character |
| 71 | 71 |
sequences without whitespaces or quoted strings. |
| 72 | 72 |
- The <tt>LemonReader</tt> and <tt>LemonWriter</tt> core interfaces |
| 73 | 73 |
are no longer available. |
| 74 | 74 |
- The implementation of the general section readers and writers has changed |
| 75 | 75 |
they are simple functors now. Beside the old |
| 76 | 76 |
stream based section handling, currently line oriented section |
| 77 | 77 |
reading and writing are also supported. In the |
| 78 | 78 |
section readers the lines must be counted manually. The sections |
| 79 | 79 |
should be read and written with the SectionWriter and SectionReader |
| 80 | 80 |
classes. |
| 81 | 81 |
- Instead of the item readers and writers, item converters should be |
| 82 | 82 |
used. The converters are functors, which map the type to |
| 83 | 83 |
std::string or std::string to the type. The converters for standard |
| 84 | 84 |
containers hasn't yet been implemented in the new LEMON. The converters |
| 85 | 85 |
can return strings in any format, because if it is necessary, the LGF |
| 86 | 86 |
writer and reader will quote and unquote the given value. |
| 87 | 87 |
- The DigraphReader and DigraphWriter can used similarly to the |
| 88 | 88 |
0.x series, however the <tt>read</tt> or <tt>write</tt> prefix of |
| 89 | 89 |
the member functions are removed. |
| 90 | 90 |
- The new LEMON supports the function like interface, the \c |
| 91 | 91 |
digraphReader and \c digraphWriter functions are more convenient than |
| 92 | 92 |
using the classes directly. |
| 93 | 93 |
|
| 94 | 94 |
\section migration-search BFS, DFS and Dijkstra |
| 95 | 95 |
- <b>Using the function interface of BFS, DFS and %Dijkstra both source and |
| 96 | 96 |
target nodes can be given as parameters of the <tt>run()</tt> function |
| 97 | 97 |
(instead of \c bfs(), \c dfs() or \c dijkstra() itself).</b> |
| 98 | 98 |
- \ref named-templ-param "Named class template parameters" of \c Bfs, |
| 99 | 99 |
\c Dfs, \c Dijkstra, \c BfsVisit, \c DfsVisit are renamed to start |
| 100 | 100 |
with "Set" instead of "Def". Namely, |
| 101 | 101 |
- \c DefPredMap -> \c SetPredMap |
| 102 | 102 |
- \c DefDistMap -> \c SetDistMap |
| 103 | 103 |
- \c DefReachedMap -> \c SetReachedMap |
| 104 | 104 |
- \c DefProcessedMap -> \c SetProcessedMap |
| 105 | 105 |
- \c DefHeap -> \c SetHeap |
| 106 | 106 |
- \c DefStandardHeap -> \c SetStandardHeap |
| 107 | 107 |
- \c DefOperationTraits -> \c SetOperationTraits |
| 108 | 108 |
- \c DefProcessedMapToBeDefaultMap -> \c SetStandardProcessedMap |
| 109 | 109 |
|
| 110 | 110 |
\section migration-error Exceptions and Debug tools |
| 111 | 111 |
|
| 112 | 112 |
<b>The class hierarchy of exceptions has largely been simplified. Now, |
| 113 | 113 |
only the i/o related tools may throw exceptions. All other exceptions |
| 114 | 114 |
have been replaced with either the \c LEMON_ASSERT or the \c LEMON_DEBUG |
| 115 | 115 |
macros.</b> |
| 116 | 116 |
|
| 117 | 117 |
<b>On the other hand, the parameter order of constructors of the |
| 118 | 118 |
exceptions has been changed. See \ref IoError and \ref FormatError for |
| 119 | 119 |
more details.</b> |
| 120 | 120 |
|
| 121 | 121 |
\section migration-other Others |
| 122 | 122 |
- <b>The contents of <tt>graph_utils.h</tt> are moved to <tt>core.h</tt> |
| 123 | 123 |
and <tt>maps.h</tt>. <tt>core.h</tt> is included by all graph types, |
| 124 | 124 |
therefore it usually do not have to be included directly.</b> |
| 125 | 125 |
- <b><tt>path_utils.h</tt> is merged to \c path.h.</b> |
| 126 | 126 |
- <b>The semantic of the assignment operations and copy constructors of maps |
| 127 | 127 |
are still under discussion. So, you must copy them by hand (i.e. copy |
| 128 | 128 |
each entry one-by-one)</b> |
| 129 | 129 |
- <b>The parameters of the graph copying tools (i.e. \c GraphCopy, |
| 130 | 130 |
\c DigraphCopy) have to be given in the from-to order.</b> |
| 131 | 131 |
- \c copyDigraph() and \c copyGraph() are renamed to \c digraphCopy() |
| 132 | 132 |
and \c graphCopy(), respectively. |
| 133 | 133 |
- <b>The interface of \ref DynArcLookUp has changed. It is now the same as |
| 134 | 134 |
of \ref ArcLookUp and \ref AllArcLookUp</b> |
| 135 | 135 |
- Some map types should also been renamed. Namely, |
| 136 | 136 |
- \c IntegerMap -> \c RangeMap |
| 137 | 137 |
- \c StdMap -> \c SparseMap |
| 138 | 138 |
- \c FunctorMap -> \c FunctorToMap |
| 139 | 139 |
- \c MapFunctor -> \c MapToFunctor |
| 140 | 140 |
- \c ForkWriteMap -> \c ForkMap |
| 141 | 141 |
- \c StoreBoolMap -> \c LoggerBoolMap |
| 142 | 142 |
- \c dim2::BoundingBox -> \c dim2::Box |
| 143 | 143 |
|
| 144 | 144 |
*/ |
| 145 | 145 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/*! |
| 20 | 20 |
|
| 21 | 21 |
\page named-param Named Parameters |
| 22 | 22 |
|
| 23 | 23 |
\section named-func-param Named Function Parameters |
| 24 | 24 |
|
| 25 | 25 |
Several modern languages provide a convenient way to refer the |
| 26 | 26 |
function parameters by name also when you call the function. It is |
| 27 | 27 |
especially comfortable in case of a function having tons of parameters |
| 28 | 28 |
with natural default values. Sadly, C++ lack this amenity. |
| 29 | 29 |
|
| 30 | 30 |
However, with a crafty trick and with some little |
| 31 | 31 |
inconvenience, it is possible to emulate is. |
| 32 | 32 |
The example below shows how to do it. |
| 33 | 33 |
|
| 34 | 34 |
\code |
| 35 | 35 |
class namedFn |
| 36 | 36 |
{
|
| 37 | 37 |
int _id; |
| 38 | 38 |
double _val; |
| 39 | 39 |
int _dim; |
| 40 | 40 |
|
| 41 | 41 |
public: |
| 42 | 42 |
namedFn() : _id(0), _val(1), _dim(2) {}
|
| 43 | 43 |
namedFn& id(int p) { _id = p ; return *this; }
|
| 44 | 44 |
namedFn& val(double p) { _val = p ; return *this; }
|
| 45 | 45 |
namedFn& dim(int p) { _dim = p ; return *this; }
|
| 46 | 46 |
|
| 47 | 47 |
run() {
|
| 48 | 48 |
std::cout << "Here comes the function itself\n" << |
| 49 | 49 |
<< "With parameters " |
| 50 | 50 |
<< _id << ", " << _val << ", " << _dim << std::endl; |
| 51 | 51 |
} |
| 52 | 52 |
}; |
| 53 | 53 |
\endcode |
| 54 | 54 |
|
| 55 | 55 |
Then you can use it like this. |
| 56 | 56 |
|
| 57 | 57 |
\code |
| 58 | 58 |
namedFn().id(3).val(2).run(); |
| 59 | 59 |
\endcode |
| 60 | 60 |
|
| 61 | 61 |
The trick is obvious, each "named parameter" changes one component of |
| 62 | 62 |
the underlying class, then gives back a reference to it. Finally, |
| 63 | 63 |
<tt>run()</tt> executes the algorithm itself. |
| 64 | 64 |
|
| 65 | 65 |
\note Although it is a class, namedFn is used pretty much like as it were |
| 66 | 66 |
a function. That it why we called it namedFn instead of \c NamedFn. |
| 67 | 67 |
|
| 68 | 68 |
\note In fact, the final <tt>.run()</tt> could be made unnecessary, |
| 69 | 69 |
because the algorithm could also be implemented in the destructor of |
| 70 | 70 |
\c namedFn instead. This however would make it impossible to implement |
| 71 | 71 |
functions with return values, and would also cause serious problems when |
| 72 | 72 |
implementing \ref named-templ-func-param "named template parameters". |
| 73 | 73 |
<b>Therefore, by convention, <tt>.run()</tt> must be used |
| 74 | 74 |
explicitly to execute a function having named parameters |
| 75 | 75 |
everywhere in LEMON.</b> |
| 76 | 76 |
|
| 77 | 77 |
\section named-templ-func-param Named Function Template Parameters |
| 78 | 78 |
|
| 79 | 79 |
A named parameter can also be a template function. The usage is |
| 80 | 80 |
exactly the same, but the implementation behind is a kind of black |
| 81 | 81 |
magic and they are the dirtiest part of the LEMON code. |
| 82 | 82 |
|
| 83 | 83 |
You will probably never need to know how it works, but if you really |
| 84 | 84 |
committed, have a look at \ref lemon/graph_to_eps.h for an example. |
| 85 | 85 |
|
| 86 | 86 |
\section traits-classes Traits Classes |
| 87 | 87 |
|
| 88 | 88 |
A similar game can also be played when defining classes. In this case |
| 89 | 89 |
the type of the class attributes can be changed. Initially we have to |
| 90 | 90 |
define a special class called <em>Traits Class</em> defining the |
| 91 | 91 |
default type of the attributes. Then the types of these attributes can |
| 92 | 92 |
be changed in the same way as described in the next section. |
| 93 | 93 |
|
| 94 | 94 |
See \ref lemon::DijkstraDefaultTraits for an |
| 95 | 95 |
example how a traits class implementation looks like. |
| 96 | 96 |
|
| 97 | 97 |
\section named-templ-param Named Class Template Parameters |
| 98 | 98 |
|
| 99 | 99 |
If we would like to change the type of an attribute in a class that |
| 100 | 100 |
was instantiated by using a traits class as a template parameter, and |
| 101 | 101 |
the class contains named parameters, we do not have to instantiate again |
| 102 | 102 |
the class with new traits class, but instead adaptor classes can |
| 103 | 103 |
be used as shown in the following example. |
| 104 | 104 |
|
| 105 | 105 |
\code |
| 106 | 106 |
Dijkstra<>::SetPredMap<NullMap<Node,Arc> >::Create |
| 107 | 107 |
\endcode |
| 108 | 108 |
|
| 109 | 109 |
It can also be used in conjunction with other named template |
| 110 | 110 |
parameters in arbitrary order. |
| 111 | 111 |
|
| 112 | 112 |
\code |
| 113 | 113 |
Dijkstra<>::SetDistMap<MyMap>::SetPredMap<NullMap<Node,Arc> >::Create |
| 114 | 114 |
\endcode |
| 115 | 115 |
|
| 116 | 116 |
The result will be an instantiated Dijkstra class, in which the |
| 117 | 117 |
DistMap and the PredMap is modified. |
| 118 | 118 |
|
| 119 | 119 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/// The namespace of LEMON |
| 20 | 20 |
|
| 21 | 21 |
/// The namespace of LEMON |
| 22 | 22 |
/// |
| 23 | 23 |
namespace lemon {
|
| 24 | 24 |
|
| 25 | 25 |
/// The namespace of LEMON concepts and concept checking classes |
| 26 | 26 |
|
| 27 | 27 |
/// The namespace of LEMON concepts and concept checking classes |
| 28 | 28 |
/// |
| 29 | 29 |
namespace concepts {}
|
| 30 | 30 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_TEMPLATE_H |
| 20 | 20 |
#define LEMON_TEMPLATE_H |
| 21 | 21 |
|
| 22 | 22 |
#endif // LEMON_TEMPLATE_H |
| 1 | 1 |
EXTRA_DIST += \ |
| 2 | 2 |
lemon/lemon.pc.in \ |
| 3 | 3 |
lemon/CMakeLists.txt |
| 4 | 4 |
|
| 5 | 5 |
pkgconfig_DATA += lemon/lemon.pc |
| 6 | 6 |
|
| 7 | 7 |
lib_LTLIBRARIES += lemon/libemon.la |
| 8 | 8 |
|
| 9 | 9 |
lemon_libemon_la_SOURCES = \ |
| 10 |
lemon/arg_parser.cc \ |
|
| 11 |
lemon/base.cc \ |
|
| 12 |
lemon/color.cc \ |
|
| 13 |
lemon/random.cc |
|
| 10 |
lemon/arg_parser.cc \ |
|
| 11 |
lemon/base.cc \ |
|
| 12 |
lemon/color.cc \ |
|
| 13 |
lemon/random.cc |
|
| 14 | 14 |
|
| 15 | 15 |
#lemon_libemon_la_CXXFLAGS = $(GLPK_CFLAGS) $(CPLEX_CFLAGS) $(SOPLEX_CXXFLAGS) $(AM_CXXFLAGS) |
| 16 | 16 |
#lemon_libemon_la_LDFLAGS = $(GLPK_LIBS) $(CPLEX_LIBS) $(SOPLEX_LIBS) |
| 17 | 17 |
|
| 18 | 18 |
lemon_HEADERS += \ |
| 19 | 19 |
lemon/adaptors.h \ |
| 20 |
|
|
| 20 |
lemon/arg_parser.h \ |
|
| 21 | 21 |
lemon/assert.h \ |
| 22 |
lemon/bfs.h \ |
|
| 23 |
lemon/bin_heap.h \ |
|
| 24 |
lemon/circulation.h \ |
|
| 25 |
lemon/color.h \ |
|
| 22 |
lemon/bfs.h \ |
|
| 23 |
lemon/bin_heap.h \ |
|
| 24 |
lemon/circulation.h \ |
|
| 25 |
lemon/color.h \ |
|
| 26 | 26 |
lemon/concept_check.h \ |
| 27 |
|
|
| 27 |
lemon/counter.h \ |
|
| 28 | 28 |
lemon/core.h \ |
| 29 |
lemon/dfs.h \ |
|
| 30 |
lemon/dijkstra.h \ |
|
| 31 |
lemon/dim2.h \ |
|
| 32 |
lemon/dimacs.h \ |
|
| 29 |
lemon/dfs.h \ |
|
| 30 |
lemon/dijkstra.h \ |
|
| 31 |
lemon/dim2.h \ |
|
| 32 |
lemon/dimacs.h \ |
|
| 33 | 33 |
lemon/elevator.h \ |
| 34 | 34 |
lemon/error.h \ |
| 35 | 35 |
lemon/full_graph.h \ |
| 36 |
lemon/graph_to_eps.h \ |
|
| 37 |
lemon/grid_graph.h \ |
|
| 36 |
lemon/graph_to_eps.h \ |
|
| 37 |
lemon/grid_graph.h \ |
|
| 38 | 38 |
lemon/hypercube_graph.h \ |
| 39 | 39 |
lemon/kruskal.h \ |
| 40 | 40 |
lemon/hao_orlin.h \ |
| 41 | 41 |
lemon/lgf_reader.h \ |
| 42 | 42 |
lemon/lgf_writer.h \ |
| 43 | 43 |
lemon/list_graph.h \ |
| 44 | 44 |
lemon/maps.h \ |
| 45 | 45 |
lemon/math.h \ |
| 46 | 46 |
lemon/max_matching.h \ |
| 47 | 47 |
lemon/nauty_reader.h \ |
| 48 | 48 |
lemon/path.h \ |
| 49 | 49 |
lemon/preflow.h \ |
| 50 |
|
|
| 50 |
lemon/random.h \ |
|
| 51 | 51 |
lemon/smart_graph.h \ |
| 52 | 52 |
lemon/suurballe.h \ |
| 53 |
lemon/time_measure.h \ |
|
| 54 |
lemon/tolerance.h \ |
|
| 53 |
lemon/time_measure.h \ |
|
| 54 |
lemon/tolerance.h \ |
|
| 55 | 55 |
lemon/unionfind.h |
| 56 | 56 |
|
| 57 | 57 |
bits_HEADERS += \ |
| 58 | 58 |
lemon/bits/alteration_notifier.h \ |
| 59 | 59 |
lemon/bits/array_map.h \ |
| 60 | 60 |
lemon/bits/base_extender.h \ |
| 61 |
|
|
| 61 |
lemon/bits/bezier.h \ |
|
| 62 | 62 |
lemon/bits/default_map.h \ |
| 63 |
|
|
| 63 |
lemon/bits/enable_if.h \ |
|
| 64 | 64 |
lemon/bits/graph_adaptor_extender.h \ |
| 65 | 65 |
lemon/bits/graph_extender.h \ |
| 66 | 66 |
lemon/bits/map_extender.h \ |
| 67 | 67 |
lemon/bits/path_dump.h \ |
| 68 | 68 |
lemon/bits/traits.h \ |
| 69 | 69 |
lemon/bits/variant.h \ |
| 70 | 70 |
lemon/bits/vector_map.h |
| 71 | 71 |
|
| 72 | 72 |
concept_HEADERS += \ |
| 73 | 73 |
lemon/concepts/digraph.h \ |
| 74 | 74 |
lemon/concepts/graph.h \ |
| 75 | 75 |
lemon/concepts/graph_components.h \ |
| 76 | 76 |
lemon/concepts/heap.h \ |
| 77 | 77 |
lemon/concepts/maps.h \ |
| 78 | 78 |
lemon/concepts/path.h |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_ADAPTORS_H |
| 20 | 20 |
#define LEMON_ADAPTORS_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup graph_adaptors |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Several graph adaptors |
| 25 | 25 |
/// |
| 26 | 26 |
/// This file contains several useful adaptors for digraphs and graphs. |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/maps.h> |
| 30 | 30 |
#include <lemon/bits/variant.h> |
| 31 | 31 |
|
| 32 | 32 |
#include <lemon/bits/graph_adaptor_extender.h> |
| 33 | 33 |
#include <lemon/tolerance.h> |
| 34 | 34 |
|
| 35 | 35 |
#include <algorithm> |
| 36 | 36 |
|
| 37 | 37 |
namespace lemon {
|
| 38 | 38 |
|
| 39 | 39 |
template<typename _Digraph> |
| 40 | 40 |
class DigraphAdaptorBase {
|
| 41 | 41 |
public: |
| 42 | 42 |
typedef _Digraph Digraph; |
| 43 | 43 |
typedef DigraphAdaptorBase Adaptor; |
| 44 | 44 |
typedef Digraph ParentDigraph; |
| 45 | 45 |
|
| 46 | 46 |
protected: |
| 47 | 47 |
Digraph* _digraph; |
| 48 | 48 |
DigraphAdaptorBase() : _digraph(0) { }
|
| 49 | 49 |
void setDigraph(Digraph& digraph) { _digraph = &digraph; }
|
| 50 | 50 |
|
| 51 | 51 |
public: |
| 52 | 52 |
DigraphAdaptorBase(Digraph& digraph) : _digraph(&digraph) { }
|
| 53 | 53 |
|
| 54 | 54 |
typedef typename Digraph::Node Node; |
| 55 | 55 |
typedef typename Digraph::Arc Arc; |
| 56 | 56 |
|
| 57 | 57 |
void first(Node& i) const { _digraph->first(i); }
|
| 58 | 58 |
void first(Arc& i) const { _digraph->first(i); }
|
| 59 | 59 |
void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); }
|
| 60 | 60 |
void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); }
|
| 61 | 61 |
|
| 62 | 62 |
void next(Node& i) const { _digraph->next(i); }
|
| 63 | 63 |
void next(Arc& i) const { _digraph->next(i); }
|
| 64 | 64 |
void nextIn(Arc& i) const { _digraph->nextIn(i); }
|
| 65 | 65 |
void nextOut(Arc& i) const { _digraph->nextOut(i); }
|
| 66 | 66 |
|
| 67 | 67 |
Node source(const Arc& a) const { return _digraph->source(a); }
|
| 68 | 68 |
Node target(const Arc& a) const { return _digraph->target(a); }
|
| 69 | 69 |
|
| 70 | 70 |
typedef NodeNumTagIndicator<Digraph> NodeNumTag; |
| 71 | 71 |
int nodeNum() const { return _digraph->nodeNum(); }
|
| 72 | 72 |
|
| 73 | 73 |
typedef EdgeNumTagIndicator<Digraph> EdgeNumTag; |
| 74 | 74 |
int arcNum() const { return _digraph->arcNum(); }
|
| 75 | 75 |
|
| 76 | 76 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
| 77 | 77 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) {
|
| 78 | 78 |
return _digraph->findArc(u, v, prev); |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
Node addNode() { return _digraph->addNode(); }
|
| 82 | 82 |
Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); }
|
| 83 | 83 |
|
| 84 | 84 |
void erase(const Node& n) const { _digraph->erase(n); }
|
| 85 | 85 |
void erase(const Arc& a) const { _digraph->erase(a); }
|
| 86 | 86 |
|
| 87 | 87 |
void clear() const { _digraph->clear(); }
|
| 88 | 88 |
|
| 89 | 89 |
int id(const Node& n) const { return _digraph->id(n); }
|
| 90 | 90 |
int id(const Arc& a) const { return _digraph->id(a); }
|
| 91 | 91 |
|
| 92 | 92 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); }
|
| 93 | 93 |
Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); }
|
| 94 | 94 |
|
| 95 | 95 |
int maxNodeId() const { return _digraph->maxNodeId(); }
|
| 96 | 96 |
int maxArcId() const { return _digraph->maxArcId(); }
|
| 97 | 97 |
|
| 98 | 98 |
typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier; |
| 99 | 99 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
|
| 100 | 100 |
|
| 101 | 101 |
typedef typename ItemSetTraits<Digraph, Arc>::ItemNotifier ArcNotifier; |
| 102 | 102 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); }
|
| 103 | 103 |
|
| 104 | 104 |
template <typename _Value> |
| 105 | 105 |
class NodeMap : public Digraph::template NodeMap<_Value> {
|
| 106 | 106 |
public: |
| 107 | 107 |
|
| 108 | 108 |
typedef typename Digraph::template NodeMap<_Value> Parent; |
| 109 | 109 |
|
| 110 | 110 |
explicit NodeMap(const Adaptor& adaptor) |
| 111 | 111 |
: Parent(*adaptor._digraph) {}
|
| 112 | 112 |
|
| 113 | 113 |
NodeMap(const Adaptor& adaptor, const _Value& value) |
| 114 | 114 |
: Parent(*adaptor._digraph, value) { }
|
| 115 | 115 |
|
| 116 | 116 |
private: |
| 117 | 117 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 118 | 118 |
return operator=<NodeMap>(cmap); |
| 119 | 119 |
} |
| 120 | 120 |
|
| 121 | 121 |
template <typename CMap> |
| 122 | 122 |
NodeMap& operator=(const CMap& cmap) {
|
| 123 | 123 |
Parent::operator=(cmap); |
| 124 | 124 |
return *this; |
| 125 | 125 |
} |
| 126 | 126 |
|
| 127 | 127 |
}; |
| 128 | 128 |
|
| 129 | 129 |
template <typename _Value> |
| 130 | 130 |
class ArcMap : public Digraph::template ArcMap<_Value> {
|
| 131 | 131 |
public: |
| 132 | 132 |
|
| 133 | 133 |
typedef typename Digraph::template ArcMap<_Value> Parent; |
| 134 | 134 |
|
| 135 | 135 |
explicit ArcMap(const Adaptor& adaptor) |
| 136 | 136 |
: Parent(*adaptor._digraph) {}
|
| 137 | 137 |
|
| 138 | 138 |
ArcMap(const Adaptor& adaptor, const _Value& value) |
| 139 | 139 |
: Parent(*adaptor._digraph, value) {}
|
| 140 | 140 |
|
| 141 | 141 |
private: |
| 142 | 142 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 143 | 143 |
return operator=<ArcMap>(cmap); |
| 144 | 144 |
} |
| 145 | 145 |
|
| 146 | 146 |
template <typename CMap> |
| 147 | 147 |
ArcMap& operator=(const CMap& cmap) {
|
| 148 | 148 |
Parent::operator=(cmap); |
| 149 | 149 |
return *this; |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
}; |
| 153 | 153 |
|
| 154 | 154 |
}; |
| 155 | 155 |
|
| 156 | 156 |
template<typename _Graph> |
| 157 | 157 |
class GraphAdaptorBase {
|
| 158 | 158 |
public: |
| 159 | 159 |
typedef _Graph Graph; |
| 160 | 160 |
typedef Graph ParentGraph; |
| 161 | 161 |
|
| 162 | 162 |
protected: |
| 163 | 163 |
Graph* _graph; |
| 164 | 164 |
|
| 165 | 165 |
GraphAdaptorBase() : _graph(0) {}
|
| 166 | 166 |
|
| 167 | 167 |
void setGraph(Graph& graph) { _graph = &graph; }
|
| 168 | 168 |
|
| 169 | 169 |
public: |
| 170 | 170 |
GraphAdaptorBase(Graph& graph) : _graph(&graph) {}
|
| 171 | 171 |
|
| 172 | 172 |
typedef typename Graph::Node Node; |
| 173 | 173 |
typedef typename Graph::Arc Arc; |
| 174 | 174 |
typedef typename Graph::Edge Edge; |
| 175 | 175 |
|
| 176 | 176 |
void first(Node& i) const { _graph->first(i); }
|
| 177 | 177 |
void first(Arc& i) const { _graph->first(i); }
|
| 178 | 178 |
void first(Edge& i) const { _graph->first(i); }
|
| 179 | 179 |
void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); }
|
| 180 | 180 |
void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); }
|
| 181 | 181 |
void firstInc(Edge &i, bool &d, const Node &n) const {
|
| 182 | 182 |
_graph->firstInc(i, d, n); |
| 183 | 183 |
} |
| 184 | 184 |
|
| 185 | 185 |
void next(Node& i) const { _graph->next(i); }
|
| 186 | 186 |
void next(Arc& i) const { _graph->next(i); }
|
| 187 | 187 |
void next(Edge& i) const { _graph->next(i); }
|
| 188 | 188 |
void nextIn(Arc& i) const { _graph->nextIn(i); }
|
| 189 | 189 |
void nextOut(Arc& i) const { _graph->nextOut(i); }
|
| 190 | 190 |
void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); }
|
| 191 | 191 |
|
| 192 | 192 |
Node u(const Edge& e) const { return _graph->u(e); }
|
| 193 | 193 |
Node v(const Edge& e) const { return _graph->v(e); }
|
| 194 | 194 |
|
| 195 | 195 |
Node source(const Arc& a) const { return _graph->source(a); }
|
| 196 | 196 |
Node target(const Arc& a) const { return _graph->target(a); }
|
| 197 | 197 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/arg_parser.h> |
| 20 | 20 |
|
| 21 | 21 |
namespace lemon {
|
| 22 | 22 |
|
| 23 | 23 |
void ArgParser::_showHelp(void *p) |
| 24 | 24 |
{
|
| 25 | 25 |
(static_cast<ArgParser*>(p))->showHelp(); |
| 26 | 26 |
exit(1); |
| 27 | 27 |
} |
| 28 | 28 |
|
| 29 | 29 |
ArgParser::ArgParser(int argc, const char * const *argv) |
| 30 | 30 |
:_argc(argc), _argv(argv), _command_name(argv[0]) {
|
| 31 | 31 |
funcOption("-help","Print a short help message",_showHelp,this);
|
| 32 | 32 |
synonym("help","-help");
|
| 33 | 33 |
synonym("h","-help");
|
| 34 | 34 |
} |
| 35 | 35 |
|
| 36 | 36 |
ArgParser::~ArgParser() |
| 37 | 37 |
{
|
| 38 | 38 |
for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) |
| 39 | 39 |
if(i->second.self_delete) |
| 40 | 40 |
switch(i->second.type) {
|
| 41 | 41 |
case BOOL: |
| 42 | 42 |
delete i->second.bool_p; |
| 43 | 43 |
break; |
| 44 | 44 |
case STRING: |
| 45 | 45 |
delete i->second.string_p; |
| 46 | 46 |
break; |
| 47 | 47 |
case DOUBLE: |
| 48 | 48 |
delete i->second.double_p; |
| 49 | 49 |
break; |
| 50 | 50 |
case INTEGER: |
| 51 | 51 |
delete i->second.int_p; |
| 52 | 52 |
break; |
| 53 | 53 |
case UNKNOWN: |
| 54 | 54 |
break; |
| 55 | 55 |
case FUNC: |
| 56 | 56 |
break; |
| 57 | 57 |
} |
| 58 | 58 |
} |
| 59 | 59 |
|
| 60 | 60 |
|
| 61 | 61 |
ArgParser &ArgParser::intOption(const std::string &name, |
| 62 | 62 |
const std::string &help, |
| 63 | 63 |
int value, bool obl) |
| 64 | 64 |
{
|
| 65 | 65 |
ParData p; |
| 66 | 66 |
p.int_p=new int(value); |
| 67 | 67 |
p.self_delete=true; |
| 68 | 68 |
p.help=help; |
| 69 | 69 |
p.type=INTEGER; |
| 70 | 70 |
p.mandatory=obl; |
| 71 | 71 |
_opts[name]=p; |
| 72 | 72 |
return *this; |
| 73 | 73 |
} |
| 74 | 74 |
|
| 75 | 75 |
ArgParser &ArgParser::doubleOption(const std::string &name, |
| 76 | 76 |
const std::string &help, |
| 77 | 77 |
double value, bool obl) |
| 78 | 78 |
{
|
| 79 | 79 |
ParData p; |
| 80 | 80 |
p.double_p=new double(value); |
| 81 | 81 |
p.self_delete=true; |
| 82 | 82 |
p.help=help; |
| 83 | 83 |
p.type=DOUBLE; |
| 84 | 84 |
p.mandatory=obl; |
| 85 | 85 |
_opts[name]=p; |
| 86 | 86 |
return *this; |
| 87 | 87 |
} |
| 88 | 88 |
|
| 89 | 89 |
ArgParser &ArgParser::boolOption(const std::string &name, |
| 90 | 90 |
const std::string &help, |
| 91 | 91 |
bool value, bool obl) |
| 92 | 92 |
{
|
| 93 | 93 |
ParData p; |
| 94 | 94 |
p.bool_p=new bool(value); |
| 95 | 95 |
p.self_delete=true; |
| 96 | 96 |
p.help=help; |
| 97 | 97 |
p.type=BOOL; |
| 98 | 98 |
p.mandatory=obl; |
| 99 | 99 |
_opts[name]=p; |
| 100 | 100 |
return *this; |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 | 103 |
ArgParser &ArgParser::stringOption(const std::string &name, |
| 104 | 104 |
const std::string &help, |
| 105 | 105 |
std::string value, bool obl) |
| 106 | 106 |
{
|
| 107 | 107 |
ParData p; |
| 108 | 108 |
p.string_p=new std::string(value); |
| 109 | 109 |
p.self_delete=true; |
| 110 | 110 |
p.help=help; |
| 111 | 111 |
p.type=STRING; |
| 112 | 112 |
p.mandatory=obl; |
| 113 | 113 |
_opts[name]=p; |
| 114 | 114 |
return *this; |
| 115 | 115 |
} |
| 116 | 116 |
|
| 117 | 117 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 118 | 118 |
const std::string &help, |
| 119 | 119 |
int &ref, bool obl) |
| 120 | 120 |
{
|
| 121 | 121 |
ParData p; |
| 122 | 122 |
p.int_p=&ref; |
| 123 | 123 |
p.self_delete=false; |
| 124 | 124 |
p.help=help; |
| 125 | 125 |
p.type=INTEGER; |
| 126 | 126 |
p.mandatory=obl; |
| 127 | 127 |
_opts[name]=p; |
| 128 | 128 |
return *this; |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 132 | 132 |
const std::string &help, |
| 133 | 133 |
double &ref, bool obl) |
| 134 | 134 |
{
|
| 135 | 135 |
ParData p; |
| 136 | 136 |
p.double_p=&ref; |
| 137 | 137 |
p.self_delete=false; |
| 138 | 138 |
p.help=help; |
| 139 | 139 |
p.type=DOUBLE; |
| 140 | 140 |
p.mandatory=obl; |
| 141 | 141 |
_opts[name]=p; |
| 142 | 142 |
return *this; |
| 143 | 143 |
} |
| 144 | 144 |
|
| 145 | 145 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 146 | 146 |
const std::string &help, |
| 147 | 147 |
bool &ref, bool obl) |
| 148 | 148 |
{
|
| 149 | 149 |
ParData p; |
| 150 | 150 |
p.bool_p=&ref; |
| 151 | 151 |
p.self_delete=false; |
| 152 | 152 |
p.help=help; |
| 153 | 153 |
p.type=BOOL; |
| 154 | 154 |
p.mandatory=obl; |
| 155 | 155 |
_opts[name]=p; |
| 156 | 156 |
|
| 157 | 157 |
ref = false; |
| 158 | 158 |
|
| 159 | 159 |
return *this; |
| 160 | 160 |
} |
| 161 | 161 |
|
| 162 | 162 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 163 | 163 |
const std::string &help, |
| 164 | 164 |
std::string &ref, bool obl) |
| 165 | 165 |
{
|
| 166 | 166 |
ParData p; |
| 167 | 167 |
p.string_p=&ref; |
| 168 | 168 |
p.self_delete=false; |
| 169 | 169 |
p.help=help; |
| 170 | 170 |
p.type=STRING; |
| 171 | 171 |
p.mandatory=obl; |
| 172 | 172 |
_opts[name]=p; |
| 173 | 173 |
return *this; |
| 174 | 174 |
} |
| 175 | 175 |
|
| 176 | 176 |
ArgParser &ArgParser::funcOption(const std::string &name, |
| 177 | 177 |
const std::string &help, |
| 178 | 178 |
void (*func)(void *),void *data) |
| 179 | 179 |
{
|
| 180 | 180 |
ParData p; |
| 181 | 181 |
p.func_p.p=func; |
| 182 | 182 |
p.func_p.data=data; |
| 183 | 183 |
p.self_delete=false; |
| 184 | 184 |
p.help=help; |
| 185 | 185 |
p.type=FUNC; |
| 186 | 186 |
p.mandatory=false; |
| 187 | 187 |
_opts[name]=p; |
| 188 | 188 |
return *this; |
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 | 191 |
ArgParser &ArgParser::optionGroup(const std::string &group, |
| 192 | 192 |
const std::string &opt) |
| 193 | 193 |
{
|
| 194 | 194 |
Opts::iterator i = _opts.find(opt); |
| 195 | 195 |
LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'"); |
| 196 | 196 |
LEMON_ASSERT(!(i->second.ingroup), |
| 197 | 197 |
"Option already in option group: '"+opt+"'"); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_ARG_PARSER_H |
| 20 | 20 |
#define LEMON_ARG_PARSER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <map> |
| 24 | 24 |
#include <list> |
| 25 | 25 |
#include <string> |
| 26 | 26 |
#include <iostream> |
| 27 | 27 |
#include <sstream> |
| 28 | 28 |
#include <algorithm> |
| 29 | 29 |
#include <lemon/assert.h> |
| 30 | 30 |
|
| 31 | 31 |
///\ingroup misc |
| 32 | 32 |
///\file |
| 33 | 33 |
///\brief A tool to parse command line arguments. |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
///Command line arguments parser |
| 38 | 38 |
|
| 39 | 39 |
///\ingroup misc |
| 40 | 40 |
///Command line arguments parser. |
| 41 | 41 |
/// |
| 42 | 42 |
///For a complete example see the \ref arg_parser_demo.cc demo file. |
| 43 | 43 |
class ArgParser {
|
| 44 | 44 |
|
| 45 | 45 |
static void _showHelp(void *p); |
| 46 | 46 |
protected: |
| 47 | 47 |
|
| 48 | 48 |
int _argc; |
| 49 | 49 |
const char * const *_argv; |
| 50 | 50 |
|
| 51 | 51 |
enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
|
| 52 | 52 |
|
| 53 | 53 |
class ParData {
|
| 54 | 54 |
public: |
| 55 | 55 |
union {
|
| 56 | 56 |
bool *bool_p; |
| 57 | 57 |
int *int_p; |
| 58 | 58 |
double *double_p; |
| 59 | 59 |
std::string *string_p; |
| 60 | 60 |
struct {
|
| 61 | 61 |
void (*p)(void *); |
| 62 | 62 |
void *data; |
| 63 | 63 |
} func_p; |
| 64 | 64 |
|
| 65 | 65 |
}; |
| 66 | 66 |
std::string help; |
| 67 | 67 |
bool mandatory; |
| 68 | 68 |
OptType type; |
| 69 | 69 |
bool set; |
| 70 | 70 |
bool ingroup; |
| 71 | 71 |
bool has_syn; |
| 72 | 72 |
bool syn; |
| 73 | 73 |
bool self_delete; |
| 74 | 74 |
ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false), |
| 75 | 75 |
has_syn(false), syn(false), self_delete(false) {}
|
| 76 | 76 |
}; |
| 77 | 77 |
|
| 78 | 78 |
typedef std::map<std::string,ParData> Opts; |
| 79 | 79 |
Opts _opts; |
| 80 | 80 |
|
| 81 | 81 |
class GroupData |
| 82 | 82 |
{
|
| 83 | 83 |
public: |
| 84 | 84 |
typedef std::list<std::string> Opts; |
| 85 | 85 |
Opts opts; |
| 86 | 86 |
bool only_one; |
| 87 | 87 |
bool mandatory; |
| 88 | 88 |
GroupData() :only_one(false), mandatory(false) {}
|
| 89 | 89 |
}; |
| 90 | 90 |
|
| 91 | 91 |
typedef std::map<std::string,GroupData> Groups; |
| 92 | 92 |
Groups _groups; |
| 93 | 93 |
|
| 94 | 94 |
struct OtherArg |
| 95 | 95 |
{
|
| 96 | 96 |
std::string name; |
| 97 | 97 |
std::string help; |
| 98 | 98 |
OtherArg(std::string n, std::string h) :name(n), help(h) {}
|
| 99 | 99 |
|
| 100 | 100 |
}; |
| 101 | 101 |
|
| 102 | 102 |
std::vector<OtherArg> _others_help; |
| 103 | 103 |
std::vector<std::string> _file_args; |
| 104 | 104 |
std::string _command_name; |
| 105 | 105 |
|
| 106 | 106 |
|
| 107 | 107 |
private: |
| 108 | 108 |
//Bind a function to an option. |
| 109 | 109 |
|
| 110 | 110 |
//\param name The name of the option. The leading '-' must be omitted. |
| 111 | 111 |
//\param help A help string. |
| 112 | 112 |
//\retval func The function to be called when the option is given. It |
| 113 | 113 |
// must be of type "void f(void *)" |
| 114 | 114 |
//\param data Data to be passed to \c func |
| 115 | 115 |
ArgParser &funcOption(const std::string &name, |
| 116 | 116 |
const std::string &help, |
| 117 | 117 |
void (*func)(void *),void *data); |
| 118 | 118 |
|
| 119 | 119 |
public: |
| 120 | 120 |
|
| 121 | 121 |
///Constructor |
| 122 | 122 |
ArgParser(int argc, const char * const *argv); |
| 123 | 123 |
|
| 124 | 124 |
~ArgParser(); |
| 125 | 125 |
|
| 126 | 126 |
///\name Options |
| 127 | 127 |
/// |
| 128 | 128 |
|
| 129 | 129 |
///@{
|
| 130 | 130 |
|
| 131 | 131 |
///Add a new integer type option |
| 132 | 132 |
|
| 133 | 133 |
///Add a new integer type option. |
| 134 | 134 |
///\param name The name of the option. The leading '-' must be omitted. |
| 135 | 135 |
///\param help A help string. |
| 136 | 136 |
///\param value A default value for the option. |
| 137 | 137 |
///\param obl Indicate if the option is mandatory. |
| 138 | 138 |
ArgParser &intOption(const std::string &name, |
| 139 | 139 |
const std::string &help, |
| 140 | 140 |
int value=0, bool obl=false); |
| 141 | 141 |
|
| 142 | 142 |
///Add a new floating point type option |
| 143 | 143 |
|
| 144 | 144 |
///Add a new floating point type option. |
| 145 | 145 |
///\param name The name of the option. The leading '-' must be omitted. |
| 146 | 146 |
///\param help A help string. |
| 147 | 147 |
///\param value A default value for the option. |
| 148 | 148 |
///\param obl Indicate if the option is mandatory. |
| 149 | 149 |
ArgParser &doubleOption(const std::string &name, |
| 150 | 150 |
const std::string &help, |
| 151 | 151 |
double value=0, bool obl=false); |
| 152 | 152 |
|
| 153 | 153 |
///Add a new bool type option |
| 154 | 154 |
|
| 155 | 155 |
///Add a new bool type option. |
| 156 | 156 |
///\param name The name of the option. The leading '-' must be omitted. |
| 157 | 157 |
///\param help A help string. |
| 158 | 158 |
///\param value A default value for the option. |
| 159 | 159 |
///\param obl Indicate if the option is mandatory. |
| 160 | 160 |
///\note A mandatory bool obtion is of very little use. |
| 161 | 161 |
ArgParser &boolOption(const std::string &name, |
| 162 | 162 |
const std::string &help, |
| 163 | 163 |
bool value=false, bool obl=false); |
| 164 | 164 |
|
| 165 | 165 |
///Add a new string type option |
| 166 | 166 |
|
| 167 | 167 |
///Add a new string type option. |
| 168 | 168 |
///\param name The name of the option. The leading '-' must be omitted. |
| 169 | 169 |
///\param help A help string. |
| 170 | 170 |
///\param value A default value for the option. |
| 171 | 171 |
///\param obl Indicate if the option is mandatory. |
| 172 | 172 |
ArgParser &stringOption(const std::string &name, |
| 173 | 173 |
const std::string &help, |
| 174 | 174 |
std::string value="", bool obl=false); |
| 175 | 175 |
|
| 176 | 176 |
///Give help string for non-parsed arguments. |
| 177 | 177 |
|
| 178 | 178 |
///With this function you can give help string for non-parsed arguments. |
| 179 | 179 |
///The parameter \c name will be printed in the short usage line, while |
| 180 | 180 |
///\c help gives a more detailed description. |
| 181 | 181 |
ArgParser &other(const std::string &name, |
| 182 | 182 |
const std::string &help=""); |
| 183 | 183 |
|
| 184 | 184 |
///@} |
| 185 | 185 |
|
| 186 | 186 |
///\name Options with External Storage |
| 187 | 187 |
///Using this functions, the value of the option will be directly written |
| 188 | 188 |
///into a variable once the option appears in the command line. |
| 189 | 189 |
|
| 190 | 190 |
///@{
|
| 191 | 191 |
|
| 192 | 192 |
///Add a new integer type option with a storage reference |
| 193 | 193 |
|
| 194 | 194 |
///Add a new integer type option with a storage reference. |
| 195 | 195 |
///\param name The name of the option. The leading '-' must be omitted. |
| 196 | 196 |
///\param help A help string. |
| 197 | 197 |
///\param obl Indicate if the option is mandatory. |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_ASSERT_H |
| 20 | 20 |
#define LEMON_ASSERT_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup exceptions |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Extended assertion handling |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/error.h> |
| 27 | 27 |
|
| 28 | 28 |
namespace lemon {
|
| 29 | 29 |
|
| 30 | 30 |
inline void assert_fail_abort(const char *file, int line, |
| 31 | 31 |
const char *function, const char* message, |
| 32 | 32 |
const char *assertion) |
| 33 | 33 |
{
|
| 34 | 34 |
std::cerr << file << ":" << line << ": "; |
| 35 | 35 |
if (function) |
| 36 | 36 |
std::cerr << function << ": "; |
| 37 | 37 |
std::cerr << message; |
| 38 | 38 |
if (assertion) |
| 39 | 39 |
std::cerr << " (assertion '" << assertion << "' failed)"; |
| 40 | 40 |
std::cerr << std::endl; |
| 41 | 41 |
std::abort(); |
| 42 | 42 |
} |
| 43 | 43 |
|
| 44 | 44 |
namespace _assert_bits {
|
| 45 | 45 |
|
| 46 | 46 |
|
| 47 | 47 |
inline const char* cstringify(const std::string& str) {
|
| 48 | 48 |
return str.c_str(); |
| 49 | 49 |
} |
| 50 | 50 |
|
| 51 | 51 |
inline const char* cstringify(const char* str) {
|
| 52 | 52 |
return str; |
| 53 | 53 |
} |
| 54 | 54 |
} |
| 55 | 55 |
} |
| 56 | 56 |
|
| 57 | 57 |
#endif // LEMON_ASSERT_H |
| 58 | 58 |
|
| 59 | 59 |
#undef LEMON_ASSERT |
| 60 | 60 |
#undef LEMON_DEBUG |
| 61 | 61 |
|
| 62 | 62 |
#if (defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \ |
| 63 | 63 |
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) > 1 |
| 64 | 64 |
#error "LEMON assertion system is not set properly" |
| 65 | 65 |
#endif |
| 66 | 66 |
|
| 67 | 67 |
#if ((defined(LEMON_ASSERT_ABORT) ? 1 : 0) + \ |
| 68 | 68 |
(defined(LEMON_ASSERT_CUSTOM) ? 1 : 0) == 1 || \ |
| 69 | 69 |
defined(LEMON_ENABLE_ASSERTS)) && \ |
| 70 | 70 |
(defined(LEMON_DISABLE_ASSERTS) || \ |
| 71 | 71 |
defined(NDEBUG)) |
| 72 | 72 |
#error "LEMON assertion system is not set properly" |
| 73 | 73 |
#endif |
| 74 | 74 |
|
| 75 | 75 |
|
| 76 | 76 |
#if defined LEMON_ASSERT_ABORT |
| 77 | 77 |
# undef LEMON_ASSERT_HANDLER |
| 78 | 78 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort |
| 79 | 79 |
#elif defined LEMON_ASSERT_CUSTOM |
| 80 | 80 |
# undef LEMON_ASSERT_HANDLER |
| 81 | 81 |
# ifndef LEMON_CUSTOM_ASSERT_HANDLER |
| 82 | 82 |
# error "LEMON_CUSTOM_ASSERT_HANDLER is not set" |
| 83 | 83 |
# endif |
| 84 | 84 |
# define LEMON_ASSERT_HANDLER LEMON_CUSTOM_ASSERT_HANDLER |
| 85 | 85 |
#elif defined LEMON_DISABLE_ASSERTS |
| 86 | 86 |
# undef LEMON_ASSERT_HANDLER |
| 87 | 87 |
#elif defined NDEBUG |
| 88 | 88 |
# undef LEMON_ASSERT_HANDLER |
| 89 | 89 |
#else |
| 90 | 90 |
# define LEMON_ASSERT_HANDLER ::lemon::assert_fail_abort |
| 91 | 91 |
#endif |
| 92 | 92 |
|
| 93 | 93 |
#ifndef LEMON_FUNCTION_NAME |
| 94 | 94 |
# if defined __GNUC__ |
| 95 | 95 |
# define LEMON_FUNCTION_NAME (__PRETTY_FUNCTION__) |
| 96 | 96 |
# elif defined _MSC_VER |
| 97 | 97 |
# define LEMON_FUNCTION_NAME (__FUNCSIG__) |
| 98 | 98 |
# elif __STDC_VERSION__ >= 199901L |
| 99 | 99 |
# define LEMON_FUNCTION_NAME (__func__) |
| 100 | 100 |
# else |
| 101 | 101 |
# define LEMON_FUNCTION_NAME ("<unknown>")
|
| 102 | 102 |
# endif |
| 103 | 103 |
#endif |
| 104 | 104 |
|
| 105 | 105 |
#ifdef DOXYGEN |
| 106 | 106 |
|
| 107 | 107 |
/// \ingroup exceptions |
| 108 | 108 |
/// |
| 109 | 109 |
/// \brief Macro for assertion with customizable message |
| 110 | 110 |
/// |
| 111 | 111 |
/// Macro for assertion with customizable message. |
| 112 | 112 |
/// \param exp An expression that must be convertible to \c bool. If it is \c |
| 113 | 113 |
/// false, then an assertion is raised. The concrete behaviour depends on the |
| 114 | 114 |
/// settings of the assertion system. |
| 115 | 115 |
/// \param msg A <tt>const char*</tt> parameter, which can be used to provide |
| 116 | 116 |
/// information about the circumstances of the failed assertion. |
| 117 | 117 |
/// |
| 118 | 118 |
/// The assertions are enabled in the default behaviour. |
| 119 | 119 |
/// You can disable them with the following code: |
| 120 | 120 |
/// \code |
| 121 | 121 |
/// #define LEMON_DISABLE_ASSERTS |
| 122 | 122 |
/// \endcode |
| 123 | 123 |
/// or with compilation parameters: |
| 124 | 124 |
/// \code |
| 125 | 125 |
/// g++ -DLEMON_DISABLE_ASSERTS |
| 126 | 126 |
/// make CXXFLAGS='-DLEMON_DISABLE_ASSERTS' |
| 127 | 127 |
/// \endcode |
| 128 | 128 |
/// The checking is also disabled when the standard macro \c NDEBUG is defined. |
| 129 | 129 |
/// |
| 130 | 130 |
/// As a default behaviour the failed assertion prints a short log message to |
| 131 | 131 |
/// the standard error and aborts the execution. |
| 132 | 132 |
/// |
| 133 | 133 |
/// However, the following modes can be used in the assertion system: |
| 134 | 134 |
/// - \c LEMON_ASSERT_ABORT The failed assertion prints a short log message to |
| 135 | 135 |
/// the standard error and aborts the program. It is the default behaviour. |
| 136 | 136 |
/// - \c LEMON_ASSERT_CUSTOM The user can define own assertion handler |
| 137 | 137 |
/// function. |
| 138 | 138 |
/// \code |
| 139 | 139 |
/// void custom_assert_handler(const char* file, int line, |
| 140 | 140 |
/// const char* function, const char* message, |
| 141 | 141 |
/// const char* assertion); |
| 142 | 142 |
/// \endcode |
| 143 | 143 |
/// The name of the function should be defined as the \c |
| 144 | 144 |
/// LEMON_CUSTOM_ASSERT_HANDLER macro name. |
| 145 | 145 |
/// \code |
| 146 | 146 |
/// #define LEMON_CUSTOM_ASSERT_HANDLER custom_assert_handler |
| 147 | 147 |
/// \endcode |
| 148 | 148 |
/// Whenever an assertion is occured, the custom assertion |
| 149 | 149 |
/// handler is called with appropiate parameters. |
| 150 | 150 |
/// |
| 151 | 151 |
/// The assertion mode can also be changed within one compilation unit. |
| 152 | 152 |
/// If the macros are redefined with other settings and the |
| 153 | 153 |
/// \ref lemon/assert.h "assert.h" file is reincluded, then the |
| 154 | 154 |
/// behaviour is changed appropiately to the new settings. |
| 155 | 155 |
# define LEMON_ASSERT(exp, msg) \ |
| 156 | 156 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
| 157 | 157 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
| 158 | 158 |
LEMON_FUNCTION_NAME, \ |
| 159 | 159 |
::lemon::_assert_bits::cstringify(msg), #exp), 0))) |
| 160 | 160 |
|
| 161 | 161 |
/// \ingroup exceptions |
| 162 | 162 |
/// |
| 163 | 163 |
/// \brief Macro for internal assertions |
| 164 | 164 |
/// |
| 165 | 165 |
/// Macro for internal assertions, it is used in the library to check |
| 166 | 166 |
/// the consistency of results of algorithms, several pre- and |
| 167 | 167 |
/// postconditions and invariants. The checking is disabled by |
| 168 | 168 |
/// default, but it can be turned on with the macro \c |
| 169 | 169 |
/// LEMON_ENABLE_DEBUG. |
| 170 | 170 |
/// \code |
| 171 | 171 |
/// #define LEMON_ENABLE_DEBUG |
| 172 | 172 |
/// \endcode |
| 173 | 173 |
/// or with compilation parameters: |
| 174 | 174 |
/// \code |
| 175 | 175 |
/// g++ -DLEMON_ENABLE_DEBUG |
| 176 | 176 |
/// make CXXFLAGS='-DLEMON_ENABLE_DEBUG' |
| 177 | 177 |
/// \endcode |
| 178 | 178 |
/// |
| 179 | 179 |
/// This macro works like the \c LEMON_ASSERT macro, therefore the |
| 180 | 180 |
/// current behaviour depends on the settings of \c LEMON_ASSERT |
| 181 | 181 |
/// macro. |
| 182 | 182 |
/// |
| 183 | 183 |
/// \see LEMON_ASSERT |
| 184 | 184 |
# define LEMON_DEBUG(exp, msg) \ |
| 185 | 185 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
| 186 | 186 |
LEMON_ASSERT_HANDLER(__FILE__, __LINE__, \ |
| 187 | 187 |
LEMON_FUNCTION_NAME, \ |
| 188 | 188 |
::lemon::_assert_bits::cstringify(msg), #exp), 0))) |
| 189 | 189 |
|
| 190 | 190 |
#else |
| 191 | 191 |
|
| 192 | 192 |
# ifndef LEMON_ASSERT_HANDLER |
| 193 | 193 |
# define LEMON_ASSERT(exp, msg) (static_cast<void>(0)) |
| 194 | 194 |
# define LEMON_DEBUG(exp, msg) (static_cast<void>(0)) |
| 195 | 195 |
# else |
| 196 | 196 |
# define LEMON_ASSERT(exp, msg) \ |
| 197 | 197 |
(static_cast<void> (!!(exp) ? 0 : ( \ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\file |
| 20 | 20 |
///\brief Some basic non-inline functions and static global data. |
| 21 | 21 |
|
| 22 | 22 |
#include<lemon/tolerance.h> |
| 23 | 23 |
#include<lemon/core.h> |
| 24 | 24 |
namespace lemon {
|
| 25 | 25 |
|
| 26 | 26 |
float Tolerance<float>::def_epsilon = 1e-4; |
| 27 | 27 |
double Tolerance<double>::def_epsilon = 1e-10; |
| 28 | 28 |
long double Tolerance<long double>::def_epsilon = 1e-14; |
| 29 | 29 |
|
| 30 | 30 |
#ifndef LEMON_ONLY_TEMPLATES |
| 31 | 31 |
const Invalid INVALID = Invalid(); |
| 32 | 32 |
#endif |
| 33 | 33 |
|
| 34 | 34 |
} //namespace lemon |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BFS_H |
| 20 | 20 |
#define LEMON_BFS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup search |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief BFS algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
#include <lemon/path.h> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
///Default traits class of Bfs class. |
| 36 | 36 |
|
| 37 | 37 |
///Default traits class of Bfs class. |
| 38 | 38 |
///\tparam GR Digraph type. |
| 39 | 39 |
template<class GR> |
| 40 | 40 |
struct BfsDefaultTraits |
| 41 | 41 |
{
|
| 42 | 42 |
///The type of the digraph the algorithm runs on. |
| 43 | 43 |
typedef GR Digraph; |
| 44 | 44 |
|
| 45 | 45 |
///\brief The type of the map that stores the predecessor |
| 46 | 46 |
///arcs of the shortest paths. |
| 47 | 47 |
/// |
| 48 | 48 |
///The type of the map that stores the predecessor |
| 49 | 49 |
///arcs of the shortest paths. |
| 50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 52 | 52 |
///Instantiates a PredMap. |
| 53 | 53 |
|
| 54 | 54 |
///This function instantiates a PredMap. |
| 55 | 55 |
///\param g is the digraph, to which we would like to define the |
| 56 | 56 |
///PredMap. |
| 57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
| 58 | 58 |
{
|
| 59 | 59 |
return new PredMap(g); |
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 | 62 |
///The type of the map that indicates which nodes are processed. |
| 63 | 63 |
|
| 64 | 64 |
///The type of the map that indicates which nodes are processed. |
| 65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 67 | 67 |
///Instantiates a ProcessedMap. |
| 68 | 68 |
|
| 69 | 69 |
///This function instantiates a ProcessedMap. |
| 70 | 70 |
///\param g is the digraph, to which |
| 71 | 71 |
///we would like to define the ProcessedMap |
| 72 | 72 |
#ifdef DOXYGEN |
| 73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 74 | 74 |
#else |
| 75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 76 | 76 |
#endif |
| 77 | 77 |
{
|
| 78 | 78 |
return new ProcessedMap(); |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
///The type of the map that indicates which nodes are reached. |
| 82 | 82 |
|
| 83 | 83 |
///The type of the map that indicates which nodes are reached. |
| 84 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 85 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 86 | 86 |
///Instantiates a ReachedMap. |
| 87 | 87 |
|
| 88 | 88 |
///This function instantiates a ReachedMap. |
| 89 | 89 |
///\param g is the digraph, to which |
| 90 | 90 |
///we would like to define the ReachedMap. |
| 91 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 92 | 92 |
{
|
| 93 | 93 |
return new ReachedMap(g); |
| 94 | 94 |
} |
| 95 | 95 |
|
| 96 | 96 |
///The type of the map that stores the distances of the nodes. |
| 97 | 97 |
|
| 98 | 98 |
///The type of the map that stores the distances of the nodes. |
| 99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 101 | 101 |
///Instantiates a DistMap. |
| 102 | 102 |
|
| 103 | 103 |
///This function instantiates a DistMap. |
| 104 | 104 |
///\param g is the digraph, to which we would like to define the |
| 105 | 105 |
///DistMap. |
| 106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
| 107 | 107 |
{
|
| 108 | 108 |
return new DistMap(g); |
| 109 | 109 |
} |
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
///%BFS algorithm class. |
| 113 | 113 |
|
| 114 | 114 |
///\ingroup search |
| 115 | 115 |
///This class provides an efficient implementation of the %BFS algorithm. |
| 116 | 116 |
/// |
| 117 | 117 |
///There is also a \ref bfs() "function-type interface" for the BFS |
| 118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
| 119 | 119 |
///used easier. |
| 120 | 120 |
/// |
| 121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 122 | 122 |
///The default type is \ref ListDigraph. |
| 123 | 123 |
#ifdef DOXYGEN |
| 124 | 124 |
template <typename GR, |
| 125 | 125 |
typename TR> |
| 126 | 126 |
#else |
| 127 | 127 |
template <typename GR=ListDigraph, |
| 128 | 128 |
typename TR=BfsDefaultTraits<GR> > |
| 129 | 129 |
#endif |
| 130 | 130 |
class Bfs {
|
| 131 | 131 |
public: |
| 132 | 132 |
|
| 133 | 133 |
///The type of the digraph the algorithm runs on. |
| 134 | 134 |
typedef typename TR::Digraph Digraph; |
| 135 | 135 |
|
| 136 | 136 |
///\brief The type of the map that stores the predecessor arcs of the |
| 137 | 137 |
///shortest paths. |
| 138 | 138 |
typedef typename TR::PredMap PredMap; |
| 139 | 139 |
///The type of the map that stores the distances of the nodes. |
| 140 | 140 |
typedef typename TR::DistMap DistMap; |
| 141 | 141 |
///The type of the map that indicates which nodes are reached. |
| 142 | 142 |
typedef typename TR::ReachedMap ReachedMap; |
| 143 | 143 |
///The type of the map that indicates which nodes are processed. |
| 144 | 144 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 145 | 145 |
///The type of the paths. |
| 146 | 146 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 147 | 147 |
|
| 148 | 148 |
///The \ref BfsDefaultTraits "traits class" of the algorithm. |
| 149 | 149 |
typedef TR Traits; |
| 150 | 150 |
|
| 151 | 151 |
private: |
| 152 | 152 |
|
| 153 | 153 |
typedef typename Digraph::Node Node; |
| 154 | 154 |
typedef typename Digraph::NodeIt NodeIt; |
| 155 | 155 |
typedef typename Digraph::Arc Arc; |
| 156 | 156 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 157 | 157 |
|
| 158 | 158 |
//Pointer to the underlying digraph. |
| 159 | 159 |
const Digraph *G; |
| 160 | 160 |
//Pointer to the map of predecessor arcs. |
| 161 | 161 |
PredMap *_pred; |
| 162 | 162 |
//Indicates if _pred is locally allocated (true) or not. |
| 163 | 163 |
bool local_pred; |
| 164 | 164 |
//Pointer to the map of distances. |
| 165 | 165 |
DistMap *_dist; |
| 166 | 166 |
//Indicates if _dist is locally allocated (true) or not. |
| 167 | 167 |
bool local_dist; |
| 168 | 168 |
//Pointer to the map of reached status of the nodes. |
| 169 | 169 |
ReachedMap *_reached; |
| 170 | 170 |
//Indicates if _reached is locally allocated (true) or not. |
| 171 | 171 |
bool local_reached; |
| 172 | 172 |
//Pointer to the map of processed status of the nodes. |
| 173 | 173 |
ProcessedMap *_processed; |
| 174 | 174 |
//Indicates if _processed is locally allocated (true) or not. |
| 175 | 175 |
bool local_processed; |
| 176 | 176 |
|
| 177 | 177 |
std::vector<typename Digraph::Node> _queue; |
| 178 | 178 |
int _queue_head,_queue_tail,_queue_next_dist; |
| 179 | 179 |
int _curr_dist; |
| 180 | 180 |
|
| 181 | 181 |
//Creates the maps if necessary. |
| 182 | 182 |
void create_maps() |
| 183 | 183 |
{
|
| 184 | 184 |
if(!_pred) {
|
| 185 | 185 |
local_pred = true; |
| 186 | 186 |
_pred = Traits::createPredMap(*G); |
| 187 | 187 |
} |
| 188 | 188 |
if(!_dist) {
|
| 189 | 189 |
local_dist = true; |
| 190 | 190 |
_dist = Traits::createDistMap(*G); |
| 191 | 191 |
} |
| 192 | 192 |
if(!_reached) {
|
| 193 | 193 |
local_reached = true; |
| 194 | 194 |
_reached = Traits::createReachedMap(*G); |
| 195 | 195 |
} |
| 196 | 196 |
if(!_processed) {
|
| 197 | 197 |
local_processed = true; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BIN_HEAP_H |
| 20 | 20 |
#define LEMON_BIN_HEAP_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup auxdat |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Binary Heap implementation. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
#include <utility> |
| 28 | 28 |
#include <functional> |
| 29 | 29 |
|
| 30 | 30 |
namespace lemon {
|
| 31 | 31 |
|
| 32 | 32 |
///\ingroup auxdat |
| 33 | 33 |
/// |
| 34 | 34 |
///\brief A Binary Heap implementation. |
| 35 | 35 |
/// |
| 36 | 36 |
///This class implements the \e binary \e heap data structure. A \e heap |
| 37 | 37 |
///is a data structure for storing items with specified values called \e |
| 38 | 38 |
///priorities in such a way that finding the item with minimum priority is |
| 39 | 39 |
///efficient. \c Compare specifies the ordering of the priorities. In a heap |
| 40 | 40 |
///one can change the priority of an item, add or erase an item, etc. |
| 41 | 41 |
/// |
| 42 | 42 |
///\tparam _Prio Type of the priority of the items. |
| 43 | 43 |
///\tparam _ItemIntMap A read and writable Item int map, used internally |
| 44 | 44 |
///to handle the cross references. |
| 45 | 45 |
///\tparam _Compare A class for the ordering of the priorities. The |
| 46 | 46 |
///default is \c std::less<_Prio>. |
| 47 | 47 |
/// |
| 48 | 48 |
///\sa FibHeap |
| 49 | 49 |
///\sa Dijkstra |
| 50 | 50 |
template <typename _Prio, typename _ItemIntMap, |
| 51 | 51 |
typename _Compare = std::less<_Prio> > |
| 52 | 52 |
class BinHeap {
|
| 53 | 53 |
|
| 54 | 54 |
public: |
| 55 | 55 |
///\e |
| 56 | 56 |
typedef _ItemIntMap ItemIntMap; |
| 57 | 57 |
///\e |
| 58 | 58 |
typedef _Prio Prio; |
| 59 | 59 |
///\e |
| 60 | 60 |
typedef typename ItemIntMap::Key Item; |
| 61 | 61 |
///\e |
| 62 | 62 |
typedef std::pair<Item,Prio> Pair; |
| 63 | 63 |
///\e |
| 64 | 64 |
typedef _Compare Compare; |
| 65 | 65 |
|
| 66 | 66 |
/// \brief Type to represent the items states. |
| 67 | 67 |
/// |
| 68 | 68 |
/// Each Item element have a state associated to it. It may be "in heap", |
| 69 | 69 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
| 70 | 70 |
/// heap's point of view, but may be useful to the user. |
| 71 | 71 |
/// |
| 72 | 72 |
/// The ItemIntMap \e should be initialized in such way that it maps |
| 73 | 73 |
/// PRE_HEAP (-1) to any element to be put in the heap... |
| 74 | 74 |
enum State {
|
| 75 | 75 |
IN_HEAP = 0, |
| 76 | 76 |
PRE_HEAP = -1, |
| 77 | 77 |
POST_HEAP = -2 |
| 78 | 78 |
}; |
| 79 | 79 |
|
| 80 | 80 |
private: |
| 81 | 81 |
std::vector<Pair> data; |
| 82 | 82 |
Compare comp; |
| 83 | 83 |
ItemIntMap &iim; |
| 84 | 84 |
|
| 85 | 85 |
public: |
| 86 | 86 |
/// \brief The constructor. |
| 87 | 87 |
/// |
| 88 | 88 |
/// The constructor. |
| 89 | 89 |
/// \param _iim should be given to the constructor, since it is used |
| 90 | 90 |
/// internally to handle the cross references. The value of the map |
| 91 | 91 |
/// should be PRE_HEAP (-1) for each element. |
| 92 | 92 |
explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
|
| 93 | 93 |
|
| 94 | 94 |
/// \brief The constructor. |
| 95 | 95 |
/// |
| 96 | 96 |
/// The constructor. |
| 97 | 97 |
/// \param _iim should be given to the constructor, since it is used |
| 98 | 98 |
/// internally to handle the cross references. The value of the map |
| 99 | 99 |
/// should be PRE_HEAP (-1) for each element. |
| 100 | 100 |
/// |
| 101 | 101 |
/// \param _comp The comparator function object. |
| 102 | 102 |
BinHeap(ItemIntMap &_iim, const Compare &_comp) |
| 103 | 103 |
: iim(_iim), comp(_comp) {}
|
| 104 | 104 |
|
| 105 | 105 |
|
| 106 | 106 |
/// The number of items stored in the heap. |
| 107 | 107 |
/// |
| 108 | 108 |
/// \brief Returns the number of items stored in the heap. |
| 109 | 109 |
int size() const { return data.size(); }
|
| 110 | 110 |
|
| 111 | 111 |
/// \brief Checks if the heap stores no items. |
| 112 | 112 |
/// |
| 113 | 113 |
/// Returns \c true if and only if the heap stores no items. |
| 114 | 114 |
bool empty() const { return data.empty(); }
|
| 115 | 115 |
|
| 116 | 116 |
/// \brief Make empty this heap. |
| 117 | 117 |
/// |
| 118 | 118 |
/// Make empty this heap. It does not change the cross reference map. |
| 119 | 119 |
/// If you want to reuse what is not surely empty you should first clear |
| 120 | 120 |
/// the heap and after that you should set the cross reference map for |
| 121 | 121 |
/// each item to \c PRE_HEAP. |
| 122 | 122 |
void clear() {
|
| 123 | 123 |
data.clear(); |
| 124 | 124 |
} |
| 125 | 125 |
|
| 126 | 126 |
private: |
| 127 | 127 |
static int parent(int i) { return (i-1)/2; }
|
| 128 | 128 |
|
| 129 | 129 |
static int second_child(int i) { return 2*i+2; }
|
| 130 | 130 |
bool less(const Pair &p1, const Pair &p2) const {
|
| 131 | 131 |
return comp(p1.second, p2.second); |
| 132 | 132 |
} |
| 133 | 133 |
|
| 134 | 134 |
int bubble_up(int hole, Pair p) {
|
| 135 | 135 |
int par = parent(hole); |
| 136 | 136 |
while( hole>0 && less(p,data[par]) ) {
|
| 137 | 137 |
move(data[par],hole); |
| 138 | 138 |
hole = par; |
| 139 | 139 |
par = parent(hole); |
| 140 | 140 |
} |
| 141 | 141 |
move(p, hole); |
| 142 | 142 |
return hole; |
| 143 | 143 |
} |
| 144 | 144 |
|
| 145 | 145 |
int bubble_down(int hole, Pair p, int length) {
|
| 146 | 146 |
int child = second_child(hole); |
| 147 | 147 |
while(child < length) {
|
| 148 | 148 |
if( less(data[child-1], data[child]) ) {
|
| 149 | 149 |
--child; |
| 150 | 150 |
} |
| 151 | 151 |
if( !less(data[child], p) ) |
| 152 | 152 |
goto ok; |
| 153 | 153 |
move(data[child], hole); |
| 154 | 154 |
hole = child; |
| 155 | 155 |
child = second_child(hole); |
| 156 | 156 |
} |
| 157 | 157 |
child--; |
| 158 | 158 |
if( child<length && less(data[child], p) ) {
|
| 159 | 159 |
move(data[child], hole); |
| 160 | 160 |
hole=child; |
| 161 | 161 |
} |
| 162 | 162 |
ok: |
| 163 | 163 |
move(p, hole); |
| 164 | 164 |
return hole; |
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 | 167 |
void move(const Pair &p, int i) {
|
| 168 | 168 |
data[i] = p; |
| 169 | 169 |
iim.set(p.first, i); |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
public: |
| 173 | 173 |
/// \brief Insert a pair of item and priority into the heap. |
| 174 | 174 |
/// |
| 175 | 175 |
/// Adds \c p.first to the heap with priority \c p.second. |
| 176 | 176 |
/// \param p The pair to insert. |
| 177 | 177 |
void push(const Pair &p) {
|
| 178 | 178 |
int n = data.size(); |
| 179 | 179 |
data.resize(n+1); |
| 180 | 180 |
bubble_up(n, p); |
| 181 | 181 |
} |
| 182 | 182 |
|
| 183 | 183 |
/// \brief Insert an item into the heap with the given heap. |
| 184 | 184 |
/// |
| 185 | 185 |
/// Adds \c i to the heap with priority \c p. |
| 186 | 186 |
/// \param i The item to insert. |
| 187 | 187 |
/// \param p The priority of the item. |
| 188 | 188 |
void push(const Item &i, const Prio &p) { push(Pair(i,p)); }
|
| 189 | 189 |
|
| 190 | 190 |
/// \brief Returns the item with minimum priority relative to \c Compare. |
| 191 | 191 |
/// |
| 192 | 192 |
/// This method returns the item with minimum priority relative to \c |
| 193 | 193 |
/// Compare. |
| 194 | 194 |
/// \pre The heap must be nonempty. |
| 195 | 195 |
Item top() const {
|
| 196 | 196 |
return data[0].first; |
| 197 | 197 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_ALTERATION_NOTIFIER_H |
| 20 | 20 |
#define LEMON_BITS_ALTERATION_NOTIFIER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <list> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/core.h> |
| 26 | 26 |
|
| 27 | 27 |
//\ingroup graphbits |
| 28 | 28 |
//\file |
| 29 | 29 |
//\brief Observer notifier for graph alteration observers. |
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
|
| 33 | 33 |
// \ingroup graphbits |
| 34 | 34 |
// |
| 35 | 35 |
// \brief Notifier class to notify observes about alterations in |
| 36 | 36 |
// a container. |
| 37 | 37 |
// |
| 38 | 38 |
// The simple graphs can be refered as two containers: a node container |
| 39 | 39 |
// and an edge container. But they do not store values directly, they |
| 40 | 40 |
// are just key continars for more value containers, which are the |
| 41 | 41 |
// node and edge maps. |
| 42 | 42 |
// |
| 43 | 43 |
// The node and edge sets of the graphs can be changed as we add or erase |
| 44 | 44 |
// nodes and edges in the graph. LEMON would like to handle easily |
| 45 | 45 |
// that the node and edge maps should contain values for all nodes or |
| 46 | 46 |
// edges. If we want to check on every indicing if the map contains |
| 47 | 47 |
// the current indicing key that cause a drawback in the performance |
| 48 | 48 |
// in the library. We use another solution: we notify all maps about |
| 49 | 49 |
// an alteration in the graph, which cause only drawback on the |
| 50 | 50 |
// alteration of the graph. |
| 51 | 51 |
// |
| 52 | 52 |
// This class provides an interface to a node or edge container. |
| 53 | 53 |
// The first() and next() member functions make possible |
| 54 | 54 |
// to iterate on the keys of the container. |
| 55 | 55 |
// The id() function returns an integer id for each key. |
| 56 | 56 |
// The maxId() function gives back an upper bound of the ids. |
| 57 | 57 |
// |
| 58 | 58 |
// For the proper functonality of this class, we should notify it |
| 59 | 59 |
// about each alteration in the container. The alterations have four type: |
| 60 | 60 |
// add(), erase(), build() and clear(). The add() and |
| 61 | 61 |
// erase() signal that only one or few items added or erased to or |
| 62 | 62 |
// from the graph. If all items are erased from the graph or if a new graph |
| 63 | 63 |
// is built from an empty graph, then it can be signaled with the |
| 64 | 64 |
// clear() and build() members. Important rule that if we erase items |
| 65 | 65 |
// from graphs we should first signal the alteration and after that erase |
| 66 | 66 |
// them from the container, on the other way on item addition we should |
| 67 | 67 |
// first extend the container and just after that signal the alteration. |
| 68 | 68 |
// |
| 69 | 69 |
// The alteration can be observed with a class inherited from the |
| 70 | 70 |
// ObserverBase nested class. The signals can be handled with |
| 71 | 71 |
// overriding the virtual functions defined in the base class. The |
| 72 | 72 |
// observer base can be attached to the notifier with the |
| 73 | 73 |
// attach() member and can be detached with detach() function. The |
| 74 | 74 |
// alteration handlers should not call any function which signals |
| 75 | 75 |
// an other alteration in the same notifier and should not |
| 76 | 76 |
// detach any observer from the notifier. |
| 77 | 77 |
// |
| 78 | 78 |
// Alteration observers try to be exception safe. If an add() or |
| 79 | 79 |
// a clear() function throws an exception then the remaining |
| 80 | 80 |
// observeres will not be notified and the fulfilled additions will |
| 81 | 81 |
// be rolled back by calling the erase() or clear() functions. |
| 82 | 82 |
// Hence erase() and clear() should not throw exception. |
| 83 | 83 |
// Actullay, they can throw only \ref ImmediateDetach exception, |
| 84 | 84 |
// which detach the observer from the notifier. |
| 85 | 85 |
// |
| 86 | 86 |
// There are some cases, when the alteration observing is not completly |
| 87 | 87 |
// reliable. If we want to carry out the node degree in the graph |
| 88 | 88 |
// as in the \ref InDegMap and we use the reverseArc(), then it cause |
| 89 | 89 |
// unreliable functionality. Because the alteration observing signals |
| 90 | 90 |
// only erasing and adding but not the reversing, it will stores bad |
| 91 | 91 |
// degrees. Apart form that the subgraph adaptors cannot even signal |
| 92 | 92 |
// the alterations because just a setting in the filter map can modify |
| 93 | 93 |
// the graph and this cannot be watched in any way. |
| 94 | 94 |
// |
| 95 | 95 |
// \param _Container The container which is observed. |
| 96 | 96 |
// \param _Item The item type which is obserbved. |
| 97 | 97 |
|
| 98 | 98 |
template <typename _Container, typename _Item> |
| 99 | 99 |
class AlterationNotifier {
|
| 100 | 100 |
public: |
| 101 | 101 |
|
| 102 | 102 |
typedef True Notifier; |
| 103 | 103 |
|
| 104 | 104 |
typedef _Container Container; |
| 105 | 105 |
typedef _Item Item; |
| 106 | 106 |
|
| 107 | 107 |
// \brief Exception which can be called from clear() and |
| 108 | 108 |
// erase(). |
| 109 | 109 |
// |
| 110 | 110 |
// From the clear() and erase() function only this |
| 111 | 111 |
// exception is allowed to throw. The exception immediatly |
| 112 | 112 |
// detaches the current observer from the notifier. Because the |
| 113 | 113 |
// clear() and erase() should not throw other exceptions |
| 114 | 114 |
// it can be used to invalidate the observer. |
| 115 | 115 |
struct ImmediateDetach {};
|
| 116 | 116 |
|
| 117 | 117 |
// \brief ObserverBase is the base class for the observers. |
| 118 | 118 |
// |
| 119 | 119 |
// ObserverBase is the abstract base class for the observers. |
| 120 | 120 |
// It will be notified about an item was inserted into or |
| 121 | 121 |
// erased from the graph. |
| 122 | 122 |
// |
| 123 | 123 |
// The observer interface contains some pure virtual functions |
| 124 | 124 |
// to override. The add() and erase() functions are |
| 125 | 125 |
// to notify the oberver when one item is added or erased. |
| 126 | 126 |
// |
| 127 | 127 |
// The build() and clear() members are to notify the observer |
| 128 | 128 |
// about the container is built from an empty container or |
| 129 | 129 |
// is cleared to an empty container. |
| 130 | 130 |
class ObserverBase {
|
| 131 | 131 |
protected: |
| 132 | 132 |
typedef AlterationNotifier Notifier; |
| 133 | 133 |
|
| 134 | 134 |
friend class AlterationNotifier; |
| 135 | 135 |
|
| 136 | 136 |
// \brief Default constructor. |
| 137 | 137 |
// |
| 138 | 138 |
// Default constructor for ObserverBase. |
| 139 | 139 |
ObserverBase() : _notifier(0) {}
|
| 140 | 140 |
|
| 141 | 141 |
// \brief Constructor which attach the observer into notifier. |
| 142 | 142 |
// |
| 143 | 143 |
// Constructor which attach the observer into notifier. |
| 144 | 144 |
ObserverBase(AlterationNotifier& nf) {
|
| 145 | 145 |
attach(nf); |
| 146 | 146 |
} |
| 147 | 147 |
|
| 148 | 148 |
// \brief Constructor which attach the obserever to the same notifier. |
| 149 | 149 |
// |
| 150 | 150 |
// Constructor which attach the obserever to the same notifier as |
| 151 | 151 |
// the other observer is attached to. |
| 152 | 152 |
ObserverBase(const ObserverBase& copy) {
|
| 153 | 153 |
if (copy.attached()) {
|
| 154 | 154 |
attach(*copy.notifier()); |
| 155 | 155 |
} |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
// \brief Destructor |
| 159 | 159 |
virtual ~ObserverBase() {
|
| 160 | 160 |
if (attached()) {
|
| 161 | 161 |
detach(); |
| 162 | 162 |
} |
| 163 | 163 |
} |
| 164 | 164 |
|
| 165 | 165 |
// \brief Attaches the observer into an AlterationNotifier. |
| 166 | 166 |
// |
| 167 | 167 |
// This member attaches the observer into an AlterationNotifier. |
| 168 | 168 |
void attach(AlterationNotifier& nf) {
|
| 169 | 169 |
nf.attach(*this); |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
// \brief Detaches the observer into an AlterationNotifier. |
| 173 | 173 |
// |
| 174 | 174 |
// This member detaches the observer from an AlterationNotifier. |
| 175 | 175 |
void detach() {
|
| 176 | 176 |
_notifier->detach(*this); |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
// \brief Gives back a pointer to the notifier which the map |
| 180 | 180 |
// attached into. |
| 181 | 181 |
// |
| 182 | 182 |
// This function gives back a pointer to the notifier which the map |
| 183 | 183 |
// attached into. |
| 184 | 184 |
Notifier* notifier() const { return const_cast<Notifier*>(_notifier); }
|
| 185 | 185 |
|
| 186 | 186 |
// Gives back true when the observer is attached into a notifier. |
| 187 | 187 |
bool attached() const { return _notifier != 0; }
|
| 188 | 188 |
|
| 189 | 189 |
private: |
| 190 | 190 |
|
| 191 | 191 |
ObserverBase& operator=(const ObserverBase& copy); |
| 192 | 192 |
|
| 193 | 193 |
protected: |
| 194 | 194 |
|
| 195 | 195 |
Notifier* _notifier; |
| 196 | 196 |
typename std::list<ObserverBase*>::iterator _index; |
| 197 | 197 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_ARRAY_MAP_H |
| 20 | 20 |
#define LEMON_BITS_ARRAY_MAP_H |
| 21 | 21 |
|
| 22 | 22 |
#include <memory> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/bits/traits.h> |
| 25 | 25 |
#include <lemon/bits/alteration_notifier.h> |
| 26 | 26 |
#include <lemon/concept_check.h> |
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
|
| 29 | 29 |
// \ingroup graphbits |
| 30 | 30 |
// \file |
| 31 | 31 |
// \brief Graph map based on the array storage. |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
// \ingroup graphbits |
| 36 | 36 |
// |
| 37 | 37 |
// \brief Graph map based on the array storage. |
| 38 | 38 |
// |
| 39 | 39 |
// The ArrayMap template class is graph map structure that automatically |
| 40 | 40 |
// updates the map when a key is added to or erased from the graph. |
| 41 | 41 |
// This map uses the allocators to implement the container functionality. |
| 42 | 42 |
// |
| 43 | 43 |
// The template parameters are the Graph, the current Item type and |
| 44 | 44 |
// the Value type of the map. |
| 45 | 45 |
template <typename _Graph, typename _Item, typename _Value> |
| 46 | 46 |
class ArrayMap |
| 47 | 47 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
|
| 48 | 48 |
public: |
| 49 | 49 |
// The graph type. |
| 50 | 50 |
typedef _Graph Graph; |
| 51 | 51 |
// The item type. |
| 52 | 52 |
typedef _Item Item; |
| 53 | 53 |
// The reference map tag. |
| 54 | 54 |
typedef True ReferenceMapTag; |
| 55 | 55 |
|
| 56 | 56 |
// The key type of the map. |
| 57 | 57 |
typedef _Item Key; |
| 58 | 58 |
// The value type of the map. |
| 59 | 59 |
typedef _Value Value; |
| 60 | 60 |
|
| 61 | 61 |
// The const reference type of the map. |
| 62 | 62 |
typedef const _Value& ConstReference; |
| 63 | 63 |
// The reference type of the map. |
| 64 | 64 |
typedef _Value& Reference; |
| 65 | 65 |
|
| 66 | 66 |
// The notifier type. |
| 67 | 67 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
| 68 | 68 |
|
| 69 | 69 |
// The MapBase of the Map which imlements the core regisitry function. |
| 70 | 70 |
typedef typename Notifier::ObserverBase Parent; |
| 71 | 71 |
|
| 72 | 72 |
private: |
| 73 | 73 |
typedef std::allocator<Value> Allocator; |
| 74 | 74 |
|
| 75 | 75 |
public: |
| 76 | 76 |
|
| 77 | 77 |
// \brief Graph initialized map constructor. |
| 78 | 78 |
// |
| 79 | 79 |
// Graph initialized map constructor. |
| 80 | 80 |
explicit ArrayMap(const Graph& graph) {
|
| 81 | 81 |
Parent::attach(graph.notifier(Item())); |
| 82 | 82 |
allocate_memory(); |
| 83 | 83 |
Notifier* nf = Parent::notifier(); |
| 84 | 84 |
Item it; |
| 85 | 85 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 86 | 86 |
int id = nf->id(it);; |
| 87 | 87 |
allocator.construct(&(values[id]), Value()); |
| 88 | 88 |
} |
| 89 | 89 |
} |
| 90 | 90 |
|
| 91 | 91 |
// \brief Constructor to use default value to initialize the map. |
| 92 | 92 |
// |
| 93 | 93 |
// It constructs a map and initialize all of the the map. |
| 94 | 94 |
ArrayMap(const Graph& graph, const Value& value) {
|
| 95 | 95 |
Parent::attach(graph.notifier(Item())); |
| 96 | 96 |
allocate_memory(); |
| 97 | 97 |
Notifier* nf = Parent::notifier(); |
| 98 | 98 |
Item it; |
| 99 | 99 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 100 | 100 |
int id = nf->id(it);; |
| 101 | 101 |
allocator.construct(&(values[id]), value); |
| 102 | 102 |
} |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 | 105 |
private: |
| 106 | 106 |
// \brief Constructor to copy a map of the same map type. |
| 107 | 107 |
// |
| 108 | 108 |
// Constructor to copy a map of the same map type. |
| 109 | 109 |
ArrayMap(const ArrayMap& copy) : Parent() {
|
| 110 | 110 |
if (copy.attached()) {
|
| 111 | 111 |
attach(*copy.notifier()); |
| 112 | 112 |
} |
| 113 | 113 |
capacity = copy.capacity; |
| 114 | 114 |
if (capacity == 0) return; |
| 115 | 115 |
values = allocator.allocate(capacity); |
| 116 | 116 |
Notifier* nf = Parent::notifier(); |
| 117 | 117 |
Item it; |
| 118 | 118 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 119 | 119 |
int id = nf->id(it);; |
| 120 | 120 |
allocator.construct(&(values[id]), copy.values[id]); |
| 121 | 121 |
} |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
// \brief Assign operator. |
| 125 | 125 |
// |
| 126 | 126 |
// This operator assigns for each item in the map the |
| 127 | 127 |
// value mapped to the same item in the copied map. |
| 128 | 128 |
// The parameter map should be indiced with the same |
| 129 | 129 |
// itemset because this assign operator does not change |
| 130 | 130 |
// the container of the map. |
| 131 | 131 |
ArrayMap& operator=(const ArrayMap& cmap) {
|
| 132 | 132 |
return operator=<ArrayMap>(cmap); |
| 133 | 133 |
} |
| 134 | 134 |
|
| 135 | 135 |
|
| 136 | 136 |
// \brief Template assign operator. |
| 137 | 137 |
// |
| 138 | 138 |
// The given parameter should be conform to the ReadMap |
| 139 | 139 |
// concecpt and could be indiced by the current item set of |
| 140 | 140 |
// the NodeMap. In this case the value for each item |
| 141 | 141 |
// is assigned by the value of the given ReadMap. |
| 142 | 142 |
template <typename CMap> |
| 143 | 143 |
ArrayMap& operator=(const CMap& cmap) {
|
| 144 | 144 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
| 145 | 145 |
const typename Parent::Notifier* nf = Parent::notifier(); |
| 146 | 146 |
Item it; |
| 147 | 147 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 148 | 148 |
set(it, cmap[it]); |
| 149 | 149 |
} |
| 150 | 150 |
return *this; |
| 151 | 151 |
} |
| 152 | 152 |
|
| 153 | 153 |
public: |
| 154 | 154 |
// \brief The destructor of the map. |
| 155 | 155 |
// |
| 156 | 156 |
// The destructor of the map. |
| 157 | 157 |
virtual ~ArrayMap() {
|
| 158 | 158 |
if (attached()) {
|
| 159 | 159 |
clear(); |
| 160 | 160 |
detach(); |
| 161 | 161 |
} |
| 162 | 162 |
} |
| 163 | 163 |
|
| 164 | 164 |
protected: |
| 165 | 165 |
|
| 166 | 166 |
using Parent::attach; |
| 167 | 167 |
using Parent::detach; |
| 168 | 168 |
using Parent::attached; |
| 169 | 169 |
|
| 170 | 170 |
public: |
| 171 | 171 |
|
| 172 | 172 |
// \brief The subscript operator. |
| 173 | 173 |
// |
| 174 | 174 |
// The subscript operator. The map can be subscripted by the |
| 175 | 175 |
// actual keys of the graph. |
| 176 | 176 |
Value& operator[](const Key& key) {
|
| 177 | 177 |
int id = Parent::notifier()->id(key); |
| 178 | 178 |
return values[id]; |
| 179 | 179 |
} |
| 180 | 180 |
|
| 181 | 181 |
// \brief The const subscript operator. |
| 182 | 182 |
// |
| 183 | 183 |
// The const subscript operator. The map can be subscripted by the |
| 184 | 184 |
// actual keys of the graph. |
| 185 | 185 |
const Value& operator[](const Key& key) const {
|
| 186 | 186 |
int id = Parent::notifier()->id(key); |
| 187 | 187 |
return values[id]; |
| 188 | 188 |
} |
| 189 | 189 |
|
| 190 | 190 |
// \brief Setter function of the map. |
| 191 | 191 |
// |
| 192 | 192 |
// Setter function of the map. Equivalent with map[key] = val. |
| 193 | 193 |
// This is a compatibility feature with the not dereferable maps. |
| 194 | 194 |
void set(const Key& key, const Value& val) {
|
| 195 | 195 |
(*this)[key] = val; |
| 196 | 196 |
} |
| 197 | 197 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_BASE_EXTENDER_H |
| 20 | 20 |
#define LEMON_BITS_BASE_EXTENDER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/error.h> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/bits/map_extender.h> |
| 26 | 26 |
#include <lemon/bits/default_map.h> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/concept_check.h> |
| 29 | 29 |
#include <lemon/concepts/maps.h> |
| 30 | 30 |
|
| 31 | 31 |
//\ingroup digraphbits |
| 32 | 32 |
//\file |
| 33 | 33 |
//\brief Extenders for the graph types |
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
// \ingroup digraphbits |
| 37 | 37 |
// |
| 38 | 38 |
// \brief BaseDigraph to BaseGraph extender |
| 39 | 39 |
template <typename Base> |
| 40 | 40 |
class UndirDigraphExtender : public Base {
|
| 41 | 41 |
|
| 42 | 42 |
public: |
| 43 | 43 |
|
| 44 | 44 |
typedef Base Parent; |
| 45 | 45 |
typedef typename Parent::Arc Edge; |
| 46 | 46 |
typedef typename Parent::Node Node; |
| 47 | 47 |
|
| 48 | 48 |
typedef True UndirectedTag; |
| 49 | 49 |
|
| 50 | 50 |
class Arc : public Edge {
|
| 51 | 51 |
friend class UndirDigraphExtender; |
| 52 | 52 |
|
| 53 | 53 |
protected: |
| 54 | 54 |
bool forward; |
| 55 | 55 |
|
| 56 | 56 |
Arc(const Edge &ue, bool _forward) : |
| 57 | 57 |
Edge(ue), forward(_forward) {}
|
| 58 | 58 |
|
| 59 | 59 |
public: |
| 60 | 60 |
Arc() {}
|
| 61 | 61 |
|
| 62 | 62 |
// Invalid arc constructor |
| 63 | 63 |
Arc(Invalid i) : Edge(i), forward(true) {}
|
| 64 | 64 |
|
| 65 | 65 |
bool operator==(const Arc &that) const {
|
| 66 | 66 |
return forward==that.forward && Edge(*this)==Edge(that); |
| 67 | 67 |
} |
| 68 | 68 |
bool operator!=(const Arc &that) const {
|
| 69 | 69 |
return forward!=that.forward || Edge(*this)!=Edge(that); |
| 70 | 70 |
} |
| 71 | 71 |
bool operator<(const Arc &that) const {
|
| 72 | 72 |
return forward<that.forward || |
| 73 | 73 |
(!(that.forward<forward) && Edge(*this)<Edge(that)); |
| 74 | 74 |
} |
| 75 | 75 |
}; |
| 76 | 76 |
|
| 77 | 77 |
// First node of the edge |
| 78 | 78 |
Node u(const Edge &e) const {
|
| 79 | 79 |
return Parent::source(e); |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 | 82 |
// Source of the given arc |
| 83 | 83 |
Node source(const Arc &e) const {
|
| 84 | 84 |
return e.forward ? Parent::source(e) : Parent::target(e); |
| 85 | 85 |
} |
| 86 | 86 |
|
| 87 | 87 |
// Second node of the edge |
| 88 | 88 |
Node v(const Edge &e) const {
|
| 89 | 89 |
return Parent::target(e); |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 | 92 |
// Target of the given arc |
| 93 | 93 |
Node target(const Arc &e) const {
|
| 94 | 94 |
return e.forward ? Parent::target(e) : Parent::source(e); |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 | 97 |
// \brief Directed arc from an edge. |
| 98 | 98 |
// |
| 99 | 99 |
// Returns a directed arc corresponding to the specified edge. |
| 100 | 100 |
// If the given bool is true, the first node of the given edge and |
| 101 | 101 |
// the source node of the returned arc are the same. |
| 102 | 102 |
static Arc direct(const Edge &e, bool d) {
|
| 103 | 103 |
return Arc(e, d); |
| 104 | 104 |
} |
| 105 | 105 |
|
| 106 | 106 |
// Returns whether the given directed arc has the same orientation |
| 107 | 107 |
// as the corresponding edge. |
| 108 | 108 |
static bool direction(const Arc &a) { return a.forward; }
|
| 109 | 109 |
|
| 110 | 110 |
using Parent::first; |
| 111 | 111 |
using Parent::next; |
| 112 | 112 |
|
| 113 | 113 |
void first(Arc &e) const {
|
| 114 | 114 |
Parent::first(e); |
| 115 | 115 |
e.forward=true; |
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 | 118 |
void next(Arc &e) const {
|
| 119 | 119 |
if( e.forward ) {
|
| 120 | 120 |
e.forward = false; |
| 121 | 121 |
} |
| 122 | 122 |
else {
|
| 123 | 123 |
Parent::next(e); |
| 124 | 124 |
e.forward = true; |
| 125 | 125 |
} |
| 126 | 126 |
} |
| 127 | 127 |
|
| 128 | 128 |
void firstOut(Arc &e, const Node &n) const {
|
| 129 | 129 |
Parent::firstIn(e,n); |
| 130 | 130 |
if( Edge(e) != INVALID ) {
|
| 131 | 131 |
e.forward = false; |
| 132 | 132 |
} |
| 133 | 133 |
else {
|
| 134 | 134 |
Parent::firstOut(e,n); |
| 135 | 135 |
e.forward = true; |
| 136 | 136 |
} |
| 137 | 137 |
} |
| 138 | 138 |
void nextOut(Arc &e) const {
|
| 139 | 139 |
if( ! e.forward ) {
|
| 140 | 140 |
Node n = Parent::target(e); |
| 141 | 141 |
Parent::nextIn(e); |
| 142 | 142 |
if( Edge(e) == INVALID ) {
|
| 143 | 143 |
Parent::firstOut(e, n); |
| 144 | 144 |
e.forward = true; |
| 145 | 145 |
} |
| 146 | 146 |
} |
| 147 | 147 |
else {
|
| 148 | 148 |
Parent::nextOut(e); |
| 149 | 149 |
} |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
void firstIn(Arc &e, const Node &n) const {
|
| 153 | 153 |
Parent::firstOut(e,n); |
| 154 | 154 |
if( Edge(e) != INVALID ) {
|
| 155 | 155 |
e.forward = false; |
| 156 | 156 |
} |
| 157 | 157 |
else {
|
| 158 | 158 |
Parent::firstIn(e,n); |
| 159 | 159 |
e.forward = true; |
| 160 | 160 |
} |
| 161 | 161 |
} |
| 162 | 162 |
void nextIn(Arc &e) const {
|
| 163 | 163 |
if( ! e.forward ) {
|
| 164 | 164 |
Node n = Parent::source(e); |
| 165 | 165 |
Parent::nextOut(e); |
| 166 | 166 |
if( Edge(e) == INVALID ) {
|
| 167 | 167 |
Parent::firstIn(e, n); |
| 168 | 168 |
e.forward = true; |
| 169 | 169 |
} |
| 170 | 170 |
} |
| 171 | 171 |
else {
|
| 172 | 172 |
Parent::nextIn(e); |
| 173 | 173 |
} |
| 174 | 174 |
} |
| 175 | 175 |
|
| 176 | 176 |
void firstInc(Edge &e, bool &d, const Node &n) const {
|
| 177 | 177 |
d = true; |
| 178 | 178 |
Parent::firstOut(e, n); |
| 179 | 179 |
if (e != INVALID) return; |
| 180 | 180 |
d = false; |
| 181 | 181 |
Parent::firstIn(e, n); |
| 182 | 182 |
} |
| 183 | 183 |
|
| 184 | 184 |
void nextInc(Edge &e, bool &d) const {
|
| 185 | 185 |
if (d) {
|
| 186 | 186 |
Node s = Parent::source(e); |
| 187 | 187 |
Parent::nextOut(e); |
| 188 | 188 |
if (e != INVALID) return; |
| 189 | 189 |
d = false; |
| 190 | 190 |
Parent::firstIn(e, s); |
| 191 | 191 |
} else {
|
| 192 | 192 |
Parent::nextIn(e); |
| 193 | 193 |
} |
| 194 | 194 |
} |
| 195 | 195 |
|
| 196 | 196 |
Node nodeFromId(int ix) const {
|
| 197 | 197 |
return Parent::nodeFromId(ix); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BEZIER_H |
| 20 | 20 |
#define LEMON_BEZIER_H |
| 21 | 21 |
|
| 22 | 22 |
//\ingroup misc |
| 23 | 23 |
//\file |
| 24 | 24 |
//\brief Classes to compute with Bezier curves. |
| 25 | 25 |
// |
| 26 | 26 |
//Up to now this file is used internally by \ref graph_to_eps.h |
| 27 | 27 |
|
| 28 | 28 |
#include<lemon/dim2.h> |
| 29 | 29 |
|
| 30 | 30 |
namespace lemon {
|
| 31 | 31 |
namespace dim2 {
|
| 32 | 32 |
|
| 33 | 33 |
class BezierBase {
|
| 34 | 34 |
public: |
| 35 | 35 |
typedef lemon::dim2::Point<double> Point; |
| 36 | 36 |
protected: |
| 37 | 37 |
static Point conv(Point x,Point y,double t) {return (1-t)*x+t*y;}
|
| 38 | 38 |
}; |
| 39 | 39 |
|
| 40 | 40 |
class Bezier1 : public BezierBase |
| 41 | 41 |
{
|
| 42 | 42 |
public: |
| 43 | 43 |
Point p1,p2; |
| 44 | 44 |
|
| 45 | 45 |
Bezier1() {}
|
| 46 | 46 |
Bezier1(Point _p1, Point _p2) :p1(_p1), p2(_p2) {}
|
| 47 | 47 |
|
| 48 | 48 |
Point operator()(double t) const |
| 49 | 49 |
{
|
| 50 | 50 |
// return conv(conv(p1,p2,t),conv(p2,p3,t),t); |
| 51 | 51 |
return conv(p1,p2,t); |
| 52 | 52 |
} |
| 53 | 53 |
Bezier1 before(double t) const |
| 54 | 54 |
{
|
| 55 | 55 |
return Bezier1(p1,conv(p1,p2,t)); |
| 56 | 56 |
} |
| 57 | 57 |
|
| 58 | 58 |
Bezier1 after(double t) const |
| 59 | 59 |
{
|
| 60 | 60 |
return Bezier1(conv(p1,p2,t),p2); |
| 61 | 61 |
} |
| 62 | 62 |
|
| 63 | 63 |
Bezier1 revert() const { return Bezier1(p2,p1);}
|
| 64 | 64 |
Bezier1 operator()(double a,double b) const { return before(b).after(a/b); }
|
| 65 | 65 |
Point grad() const { return p2-p1; }
|
| 66 | 66 |
Point norm() const { return rot90(p2-p1); }
|
| 67 | 67 |
Point grad(double) const { return grad(); }
|
| 68 | 68 |
Point norm(double t) const { return rot90(grad(t)); }
|
| 69 | 69 |
}; |
| 70 | 70 |
|
| 71 | 71 |
class Bezier2 : public BezierBase |
| 72 | 72 |
{
|
| 73 | 73 |
public: |
| 74 | 74 |
Point p1,p2,p3; |
| 75 | 75 |
|
| 76 | 76 |
Bezier2() {}
|
| 77 | 77 |
Bezier2(Point _p1, Point _p2, Point _p3) :p1(_p1), p2(_p2), p3(_p3) {}
|
| 78 | 78 |
Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {}
|
| 79 | 79 |
Point operator()(double t) const |
| 80 | 80 |
{
|
| 81 | 81 |
// return conv(conv(p1,p2,t),conv(p2,p3,t),t); |
| 82 | 82 |
return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3; |
| 83 | 83 |
} |
| 84 | 84 |
Bezier2 before(double t) const |
| 85 | 85 |
{
|
| 86 | 86 |
Point q(conv(p1,p2,t)); |
| 87 | 87 |
Point r(conv(p2,p3,t)); |
| 88 | 88 |
return Bezier2(p1,q,conv(q,r,t)); |
| 89 | 89 |
} |
| 90 | 90 |
|
| 91 | 91 |
Bezier2 after(double t) const |
| 92 | 92 |
{
|
| 93 | 93 |
Point q(conv(p1,p2,t)); |
| 94 | 94 |
Point r(conv(p2,p3,t)); |
| 95 | 95 |
return Bezier2(conv(q,r,t),r,p3); |
| 96 | 96 |
} |
| 97 | 97 |
Bezier2 revert() const { return Bezier2(p3,p2,p1);}
|
| 98 | 98 |
Bezier2 operator()(double a,double b) const { return before(b).after(a/b); }
|
| 99 | 99 |
Bezier1 grad() const { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); }
|
| 100 | 100 |
Bezier1 norm() const { return Bezier1(2.0*rot90(p2-p1),2.0*rot90(p3-p2)); }
|
| 101 | 101 |
Point grad(double t) const { return grad()(t); }
|
| 102 | 102 |
Point norm(double t) const { return rot90(grad(t)); }
|
| 103 | 103 |
}; |
| 104 | 104 |
|
| 105 | 105 |
class Bezier3 : public BezierBase |
| 106 | 106 |
{
|
| 107 | 107 |
public: |
| 108 | 108 |
Point p1,p2,p3,p4; |
| 109 | 109 |
|
| 110 | 110 |
Bezier3() {}
|
| 111 | 111 |
Bezier3(Point _p1, Point _p2, Point _p3, Point _p4) |
| 112 | 112 |
: p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
|
| 113 | 113 |
Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)), |
| 114 | 114 |
p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {}
|
| 115 | 115 |
Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)), |
| 116 | 116 |
p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {}
|
| 117 | 117 |
|
| 118 | 118 |
Point operator()(double t) const |
| 119 | 119 |
{
|
| 120 | 120 |
// return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t); |
| 121 | 121 |
return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+ |
| 122 | 122 |
(3*t*t*(1-t))*p3+(t*t*t)*p4; |
| 123 | 123 |
} |
| 124 | 124 |
Bezier3 before(double t) const |
| 125 | 125 |
{
|
| 126 | 126 |
Point p(conv(p1,p2,t)); |
| 127 | 127 |
Point q(conv(p2,p3,t)); |
| 128 | 128 |
Point r(conv(p3,p4,t)); |
| 129 | 129 |
Point a(conv(p,q,t)); |
| 130 | 130 |
Point b(conv(q,r,t)); |
| 131 | 131 |
Point c(conv(a,b,t)); |
| 132 | 132 |
return Bezier3(p1,p,a,c); |
| 133 | 133 |
} |
| 134 | 134 |
|
| 135 | 135 |
Bezier3 after(double t) const |
| 136 | 136 |
{
|
| 137 | 137 |
Point p(conv(p1,p2,t)); |
| 138 | 138 |
Point q(conv(p2,p3,t)); |
| 139 | 139 |
Point r(conv(p3,p4,t)); |
| 140 | 140 |
Point a(conv(p,q,t)); |
| 141 | 141 |
Point b(conv(q,r,t)); |
| 142 | 142 |
Point c(conv(a,b,t)); |
| 143 | 143 |
return Bezier3(c,b,r,p4); |
| 144 | 144 |
} |
| 145 | 145 |
Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);}
|
| 146 | 146 |
Bezier3 operator()(double a,double b) const { return before(b).after(a/b); }
|
| 147 | 147 |
Bezier2 grad() const { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); }
|
| 148 | 148 |
Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1),
|
| 149 | 149 |
3.0*rot90(p3-p2), |
| 150 | 150 |
3.0*rot90(p4-p3)); } |
| 151 | 151 |
Point grad(double t) const { return grad()(t); }
|
| 152 | 152 |
Point norm(double t) const { return rot90(grad(t)); }
|
| 153 | 153 |
|
| 154 | 154 |
template<class R,class F,class S,class D> |
| 155 | 155 |
R recSplit(F &_f,const S &_s,D _d) const |
| 156 | 156 |
{
|
| 157 | 157 |
const Point a=(p1+p2)/2; |
| 158 | 158 |
const Point b=(p2+p3)/2; |
| 159 | 159 |
const Point c=(p3+p4)/2; |
| 160 | 160 |
const Point d=(a+b)/2; |
| 161 | 161 |
const Point e=(b+c)/2; |
| 162 | 162 |
const Point f=(d+e)/2; |
| 163 | 163 |
R f1=_f(Bezier3(p1,a,d,e),_d); |
| 164 | 164 |
R f2=_f(Bezier3(e,d,c,p4),_d); |
| 165 | 165 |
return _s(f1,f2); |
| 166 | 166 |
} |
| 167 | 167 |
|
| 168 | 168 |
}; |
| 169 | 169 |
|
| 170 | 170 |
|
| 171 | 171 |
} //END OF NAMESPACE dim2 |
| 172 | 172 |
} //END OF NAMESPACE lemon |
| 173 | 173 |
|
| 174 | 174 |
#endif // LEMON_BEZIER_H |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_DEFAULT_MAP_H |
| 20 | 20 |
#define LEMON_BITS_DEFAULT_MAP_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/bits/array_map.h> |
| 23 | 23 |
#include <lemon/bits/vector_map.h> |
| 24 | 24 |
//#include <lemon/bits/debug_map.h> |
| 25 | 25 |
|
| 26 | 26 |
//\ingroup graphbits |
| 27 | 27 |
//\file |
| 28 | 28 |
//\brief Graph maps that construct and destruct their elements dynamically. |
| 29 | 29 |
|
| 30 | 30 |
namespace lemon {
|
| 31 | 31 |
|
| 32 | 32 |
|
| 33 | 33 |
//#ifndef LEMON_USE_DEBUG_MAP |
| 34 | 34 |
|
| 35 | 35 |
template <typename _Graph, typename _Item, typename _Value> |
| 36 | 36 |
struct DefaultMapSelector {
|
| 37 | 37 |
typedef ArrayMap<_Graph, _Item, _Value> Map; |
| 38 | 38 |
}; |
| 39 | 39 |
|
| 40 | 40 |
// bool |
| 41 | 41 |
template <typename _Graph, typename _Item> |
| 42 | 42 |
struct DefaultMapSelector<_Graph, _Item, bool> {
|
| 43 | 43 |
typedef VectorMap<_Graph, _Item, bool> Map; |
| 44 | 44 |
}; |
| 45 | 45 |
|
| 46 | 46 |
// char |
| 47 | 47 |
template <typename _Graph, typename _Item> |
| 48 | 48 |
struct DefaultMapSelector<_Graph, _Item, char> {
|
| 49 | 49 |
typedef VectorMap<_Graph, _Item, char> Map; |
| 50 | 50 |
}; |
| 51 | 51 |
|
| 52 | 52 |
template <typename _Graph, typename _Item> |
| 53 | 53 |
struct DefaultMapSelector<_Graph, _Item, signed char> {
|
| 54 | 54 |
typedef VectorMap<_Graph, _Item, signed char> Map; |
| 55 | 55 |
}; |
| 56 | 56 |
|
| 57 | 57 |
template <typename _Graph, typename _Item> |
| 58 | 58 |
struct DefaultMapSelector<_Graph, _Item, unsigned char> {
|
| 59 | 59 |
typedef VectorMap<_Graph, _Item, unsigned char> Map; |
| 60 | 60 |
}; |
| 61 | 61 |
|
| 62 | 62 |
|
| 63 | 63 |
// int |
| 64 | 64 |
template <typename _Graph, typename _Item> |
| 65 | 65 |
struct DefaultMapSelector<_Graph, _Item, signed int> {
|
| 66 | 66 |
typedef VectorMap<_Graph, _Item, signed int> Map; |
| 67 | 67 |
}; |
| 68 | 68 |
|
| 69 | 69 |
template <typename _Graph, typename _Item> |
| 70 | 70 |
struct DefaultMapSelector<_Graph, _Item, unsigned int> {
|
| 71 | 71 |
typedef VectorMap<_Graph, _Item, unsigned int> Map; |
| 72 | 72 |
}; |
| 73 | 73 |
|
| 74 | 74 |
|
| 75 | 75 |
// short |
| 76 | 76 |
template <typename _Graph, typename _Item> |
| 77 | 77 |
struct DefaultMapSelector<_Graph, _Item, signed short> {
|
| 78 | 78 |
typedef VectorMap<_Graph, _Item, signed short> Map; |
| 79 | 79 |
}; |
| 80 | 80 |
|
| 81 | 81 |
template <typename _Graph, typename _Item> |
| 82 | 82 |
struct DefaultMapSelector<_Graph, _Item, unsigned short> {
|
| 83 | 83 |
typedef VectorMap<_Graph, _Item, unsigned short> Map; |
| 84 | 84 |
}; |
| 85 | 85 |
|
| 86 | 86 |
|
| 87 | 87 |
// long |
| 88 | 88 |
template <typename _Graph, typename _Item> |
| 89 | 89 |
struct DefaultMapSelector<_Graph, _Item, signed long> {
|
| 90 | 90 |
typedef VectorMap<_Graph, _Item, signed long> Map; |
| 91 | 91 |
}; |
| 92 | 92 |
|
| 93 | 93 |
template <typename _Graph, typename _Item> |
| 94 | 94 |
struct DefaultMapSelector<_Graph, _Item, unsigned long> {
|
| 95 | 95 |
typedef VectorMap<_Graph, _Item, unsigned long> Map; |
| 96 | 96 |
}; |
| 97 | 97 |
|
| 98 | 98 |
|
| 99 | 99 |
#if defined __GNUC__ && !defined __STRICT_ANSI__ |
| 100 | 100 |
|
| 101 | 101 |
// long long |
| 102 | 102 |
template <typename _Graph, typename _Item> |
| 103 | 103 |
struct DefaultMapSelector<_Graph, _Item, signed long long> {
|
| 104 | 104 |
typedef VectorMap<_Graph, _Item, signed long long> Map; |
| 105 | 105 |
}; |
| 106 | 106 |
|
| 107 | 107 |
template <typename _Graph, typename _Item> |
| 108 | 108 |
struct DefaultMapSelector<_Graph, _Item, unsigned long long> {
|
| 109 | 109 |
typedef VectorMap<_Graph, _Item, unsigned long long> Map; |
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
#endif |
| 113 | 113 |
|
| 114 | 114 |
|
| 115 | 115 |
// float |
| 116 | 116 |
template <typename _Graph, typename _Item> |
| 117 | 117 |
struct DefaultMapSelector<_Graph, _Item, float> {
|
| 118 | 118 |
typedef VectorMap<_Graph, _Item, float> Map; |
| 119 | 119 |
}; |
| 120 | 120 |
|
| 121 | 121 |
|
| 122 | 122 |
// double |
| 123 | 123 |
template <typename _Graph, typename _Item> |
| 124 | 124 |
struct DefaultMapSelector<_Graph, _Item, double> {
|
| 125 | 125 |
typedef VectorMap<_Graph, _Item, double> Map; |
| 126 | 126 |
}; |
| 127 | 127 |
|
| 128 | 128 |
|
| 129 | 129 |
// long double |
| 130 | 130 |
template <typename _Graph, typename _Item> |
| 131 | 131 |
struct DefaultMapSelector<_Graph, _Item, long double> {
|
| 132 | 132 |
typedef VectorMap<_Graph, _Item, long double> Map; |
| 133 | 133 |
}; |
| 134 | 134 |
|
| 135 | 135 |
|
| 136 | 136 |
// pointer |
| 137 | 137 |
template <typename _Graph, typename _Item, typename _Ptr> |
| 138 | 138 |
struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
|
| 139 | 139 |
typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
| 140 | 140 |
}; |
| 141 | 141 |
|
| 142 | 142 |
// #else |
| 143 | 143 |
|
| 144 | 144 |
// template <typename _Graph, typename _Item, typename _Value> |
| 145 | 145 |
// struct DefaultMapSelector {
|
| 146 | 146 |
// typedef DebugMap<_Graph, _Item, _Value> Map; |
| 147 | 147 |
// }; |
| 148 | 148 |
|
| 149 | 149 |
// #endif |
| 150 | 150 |
|
| 151 | 151 |
// DefaultMap class |
| 152 | 152 |
template <typename _Graph, typename _Item, typename _Value> |
| 153 | 153 |
class DefaultMap |
| 154 | 154 |
: public DefaultMapSelector<_Graph, _Item, _Value>::Map {
|
| 155 | 155 |
public: |
| 156 | 156 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
| 157 | 157 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
| 158 | 158 |
|
| 159 | 159 |
typedef typename Parent::Graph Graph; |
| 160 | 160 |
typedef typename Parent::Value Value; |
| 161 | 161 |
|
| 162 | 162 |
explicit DefaultMap(const Graph& graph) : Parent(graph) {}
|
| 163 | 163 |
DefaultMap(const Graph& graph, const Value& value) |
| 164 | 164 |
: Parent(graph, value) {}
|
| 165 | 165 |
|
| 166 | 166 |
DefaultMap& operator=(const DefaultMap& cmap) {
|
| 167 | 167 |
return operator=<DefaultMap>(cmap); |
| 168 | 168 |
} |
| 169 | 169 |
|
| 170 | 170 |
template <typename CMap> |
| 171 | 171 |
DefaultMap& operator=(const CMap& cmap) {
|
| 172 | 172 |
Parent::operator=(cmap); |
| 173 | 173 |
return *this; |
| 174 | 174 |
} |
| 175 | 175 |
|
| 176 | 176 |
}; |
| 177 | 177 |
|
| 178 | 178 |
} |
| 179 | 179 |
|
| 180 | 180 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
// This file contains a modified version of the enable_if library from BOOST. |
| 20 | 20 |
// See the appropriate copyright notice below. |
| 21 | 21 |
|
| 22 | 22 |
// Boost enable_if library |
| 23 | 23 |
|
| 24 | 24 |
// Copyright 2003 (c) The Trustees of Indiana University. |
| 25 | 25 |
|
| 26 | 26 |
// Use, modification, and distribution is subject to the Boost Software |
| 27 | 27 |
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 28 | 28 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 29 | 29 |
|
| 30 | 30 |
// Authors: Jaakko Jarvi (jajarvi at osl.iu.edu) |
| 31 | 31 |
// Jeremiah Willcock (jewillco at osl.iu.edu) |
| 32 | 32 |
// Andrew Lumsdaine (lums at osl.iu.edu) |
| 33 | 33 |
|
| 34 | 34 |
|
| 35 | 35 |
#ifndef LEMON_BITS_ENABLE_IF_H |
| 36 | 36 |
#define LEMON_BITS_ENABLE_IF_H |
| 37 | 37 |
|
| 38 | 38 |
//\file |
| 39 | 39 |
//\brief Miscellaneous basic utilities |
| 40 | 40 |
|
| 41 | 41 |
namespace lemon |
| 42 | 42 |
{
|
| 43 | 43 |
|
| 44 | 44 |
// Basic type for defining "tags". A "YES" condition for \c enable_if. |
| 45 | 45 |
|
| 46 | 46 |
// Basic type for defining "tags". A "YES" condition for \c enable_if. |
| 47 | 47 |
// |
| 48 | 48 |
//\sa False |
| 49 | 49 |
struct True {
|
| 50 | 50 |
//\e |
| 51 | 51 |
static const bool value = true; |
| 52 | 52 |
}; |
| 53 | 53 |
|
| 54 | 54 |
// Basic type for defining "tags". A "NO" condition for \c enable_if. |
| 55 | 55 |
|
| 56 | 56 |
// Basic type for defining "tags". A "NO" condition for \c enable_if. |
| 57 | 57 |
// |
| 58 | 58 |
//\sa True |
| 59 | 59 |
struct False {
|
| 60 | 60 |
//\e |
| 61 | 61 |
static const bool value = false; |
| 62 | 62 |
}; |
| 63 | 63 |
|
| 64 | 64 |
|
| 65 | 65 |
|
| 66 | 66 |
template <typename T> |
| 67 | 67 |
struct Wrap {
|
| 68 | 68 |
const T &value; |
| 69 | 69 |
Wrap(const T &t) : value(t) {}
|
| 70 | 70 |
}; |
| 71 | 71 |
|
| 72 | 72 |
/**************** dummy class to avoid ambiguity ****************/ |
| 73 | 73 |
|
| 74 | 74 |
template<int T> struct dummy { dummy(int) {} };
|
| 75 | 75 |
|
| 76 | 76 |
/**************** enable_if from BOOST ****************/ |
| 77 | 77 |
|
| 78 | 78 |
template <typename Type, typename T = void> |
| 79 | 79 |
struct exists {
|
| 80 | 80 |
typedef T type; |
| 81 | 81 |
}; |
| 82 | 82 |
|
| 83 | 83 |
|
| 84 | 84 |
template <bool B, class T = void> |
| 85 | 85 |
struct enable_if_c {
|
| 86 | 86 |
typedef T type; |
| 87 | 87 |
}; |
| 88 | 88 |
|
| 89 | 89 |
template <class T> |
| 90 | 90 |
struct enable_if_c<false, T> {};
|
| 91 | 91 |
|
| 92 | 92 |
template <class Cond, class T = void> |
| 93 | 93 |
struct enable_if : public enable_if_c<Cond::value, T> {};
|
| 94 | 94 |
|
| 95 | 95 |
template <bool B, class T> |
| 96 | 96 |
struct lazy_enable_if_c {
|
| 97 | 97 |
typedef typename T::type type; |
| 98 | 98 |
}; |
| 99 | 99 |
|
| 100 | 100 |
template <class T> |
| 101 | 101 |
struct lazy_enable_if_c<false, T> {};
|
| 102 | 102 |
|
| 103 | 103 |
template <class Cond, class T> |
| 104 | 104 |
struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {};
|
| 105 | 105 |
|
| 106 | 106 |
|
| 107 | 107 |
template <bool B, class T = void> |
| 108 | 108 |
struct disable_if_c {
|
| 109 | 109 |
typedef T type; |
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
template <class T> |
| 113 | 113 |
struct disable_if_c<true, T> {};
|
| 114 | 114 |
|
| 115 | 115 |
template <class Cond, class T = void> |
| 116 | 116 |
struct disable_if : public disable_if_c<Cond::value, T> {};
|
| 117 | 117 |
|
| 118 | 118 |
template <bool B, class T> |
| 119 | 119 |
struct lazy_disable_if_c {
|
| 120 | 120 |
typedef typename T::type type; |
| 121 | 121 |
}; |
| 122 | 122 |
|
| 123 | 123 |
template <class T> |
| 124 | 124 |
struct lazy_disable_if_c<true, T> {};
|
| 125 | 125 |
|
| 126 | 126 |
template <class Cond, class T> |
| 127 | 127 |
struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {};
|
| 128 | 128 |
|
| 129 | 129 |
} // namespace lemon |
| 130 | 130 |
|
| 131 | 131 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H |
| 20 | 20 |
#define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/error.h> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/bits/default_map.h> |
| 26 | 26 |
|
| 27 | 27 |
namespace lemon {
|
| 28 | 28 |
|
| 29 | 29 |
template <typename _Digraph> |
| 30 | 30 |
class DigraphAdaptorExtender : public _Digraph {
|
| 31 | 31 |
public: |
| 32 | 32 |
|
| 33 | 33 |
typedef _Digraph Parent; |
| 34 | 34 |
typedef _Digraph Digraph; |
| 35 | 35 |
typedef DigraphAdaptorExtender Adaptor; |
| 36 | 36 |
|
| 37 | 37 |
// Base extensions |
| 38 | 38 |
|
| 39 | 39 |
typedef typename Parent::Node Node; |
| 40 | 40 |
typedef typename Parent::Arc Arc; |
| 41 | 41 |
|
| 42 | 42 |
int maxId(Node) const {
|
| 43 | 43 |
return Parent::maxNodeId(); |
| 44 | 44 |
} |
| 45 | 45 |
|
| 46 | 46 |
int maxId(Arc) const {
|
| 47 | 47 |
return Parent::maxArcId(); |
| 48 | 48 |
} |
| 49 | 49 |
|
| 50 | 50 |
Node fromId(int id, Node) const {
|
| 51 | 51 |
return Parent::nodeFromId(id); |
| 52 | 52 |
} |
| 53 | 53 |
|
| 54 | 54 |
Arc fromId(int id, Arc) const {
|
| 55 | 55 |
return Parent::arcFromId(id); |
| 56 | 56 |
} |
| 57 | 57 |
|
| 58 | 58 |
Node oppositeNode(const Node &n, const Arc &e) const {
|
| 59 | 59 |
if (n == Parent::source(e)) |
| 60 | 60 |
return Parent::target(e); |
| 61 | 61 |
else if(n==Parent::target(e)) |
| 62 | 62 |
return Parent::source(e); |
| 63 | 63 |
else |
| 64 | 64 |
return INVALID; |
| 65 | 65 |
} |
| 66 | 66 |
|
| 67 | 67 |
class NodeIt : public Node {
|
| 68 | 68 |
const Adaptor* _adaptor; |
| 69 | 69 |
public: |
| 70 | 70 |
|
| 71 | 71 |
NodeIt() {}
|
| 72 | 72 |
|
| 73 | 73 |
NodeIt(Invalid i) : Node(i) { }
|
| 74 | 74 |
|
| 75 | 75 |
explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
|
| 76 | 76 |
_adaptor->first(static_cast<Node&>(*this)); |
| 77 | 77 |
} |
| 78 | 78 |
|
| 79 | 79 |
NodeIt(const Adaptor& adaptor, const Node& node) |
| 80 | 80 |
: Node(node), _adaptor(&adaptor) {}
|
| 81 | 81 |
|
| 82 | 82 |
NodeIt& operator++() {
|
| 83 | 83 |
_adaptor->next(*this); |
| 84 | 84 |
return *this; |
| 85 | 85 |
} |
| 86 | 86 |
|
| 87 | 87 |
}; |
| 88 | 88 |
|
| 89 | 89 |
|
| 90 | 90 |
class ArcIt : public Arc {
|
| 91 | 91 |
const Adaptor* _adaptor; |
| 92 | 92 |
public: |
| 93 | 93 |
|
| 94 | 94 |
ArcIt() { }
|
| 95 | 95 |
|
| 96 | 96 |
ArcIt(Invalid i) : Arc(i) { }
|
| 97 | 97 |
|
| 98 | 98 |
explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) {
|
| 99 | 99 |
_adaptor->first(static_cast<Arc&>(*this)); |
| 100 | 100 |
} |
| 101 | 101 |
|
| 102 | 102 |
ArcIt(const Adaptor& adaptor, const Arc& e) : |
| 103 | 103 |
Arc(e), _adaptor(&adaptor) { }
|
| 104 | 104 |
|
| 105 | 105 |
ArcIt& operator++() {
|
| 106 | 106 |
_adaptor->next(*this); |
| 107 | 107 |
return *this; |
| 108 | 108 |
} |
| 109 | 109 |
|
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
|
| 113 | 113 |
class OutArcIt : public Arc {
|
| 114 | 114 |
const Adaptor* _adaptor; |
| 115 | 115 |
public: |
| 116 | 116 |
|
| 117 | 117 |
OutArcIt() { }
|
| 118 | 118 |
|
| 119 | 119 |
OutArcIt(Invalid i) : Arc(i) { }
|
| 120 | 120 |
|
| 121 | 121 |
OutArcIt(const Adaptor& adaptor, const Node& node) |
| 122 | 122 |
: _adaptor(&adaptor) {
|
| 123 | 123 |
_adaptor->firstOut(*this, node); |
| 124 | 124 |
} |
| 125 | 125 |
|
| 126 | 126 |
OutArcIt(const Adaptor& adaptor, const Arc& arc) |
| 127 | 127 |
: Arc(arc), _adaptor(&adaptor) {}
|
| 128 | 128 |
|
| 129 | 129 |
OutArcIt& operator++() {
|
| 130 | 130 |
_adaptor->nextOut(*this); |
| 131 | 131 |
return *this; |
| 132 | 132 |
} |
| 133 | 133 |
|
| 134 | 134 |
}; |
| 135 | 135 |
|
| 136 | 136 |
|
| 137 | 137 |
class InArcIt : public Arc {
|
| 138 | 138 |
const Adaptor* _adaptor; |
| 139 | 139 |
public: |
| 140 | 140 |
|
| 141 | 141 |
InArcIt() { }
|
| 142 | 142 |
|
| 143 | 143 |
InArcIt(Invalid i) : Arc(i) { }
|
| 144 | 144 |
|
| 145 | 145 |
InArcIt(const Adaptor& adaptor, const Node& node) |
| 146 | 146 |
: _adaptor(&adaptor) {
|
| 147 | 147 |
_adaptor->firstIn(*this, node); |
| 148 | 148 |
} |
| 149 | 149 |
|
| 150 | 150 |
InArcIt(const Adaptor& adaptor, const Arc& arc) : |
| 151 | 151 |
Arc(arc), _adaptor(&adaptor) {}
|
| 152 | 152 |
|
| 153 | 153 |
InArcIt& operator++() {
|
| 154 | 154 |
_adaptor->nextIn(*this); |
| 155 | 155 |
return *this; |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
}; |
| 159 | 159 |
|
| 160 | 160 |
Node baseNode(const OutArcIt &e) const {
|
| 161 | 161 |
return Parent::source(e); |
| 162 | 162 |
} |
| 163 | 163 |
Node runningNode(const OutArcIt &e) const {
|
| 164 | 164 |
return Parent::target(e); |
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 | 167 |
Node baseNode(const InArcIt &e) const {
|
| 168 | 168 |
return Parent::target(e); |
| 169 | 169 |
} |
| 170 | 170 |
Node runningNode(const InArcIt &e) const {
|
| 171 | 171 |
return Parent::source(e); |
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
}; |
| 175 | 175 |
|
| 176 | 176 |
|
| 177 | 177 |
/// \ingroup digraphbits |
| 178 | 178 |
/// |
| 179 | 179 |
/// \brief Extender for the GraphAdaptors |
| 180 | 180 |
template <typename _Graph> |
| 181 | 181 |
class GraphAdaptorExtender : public _Graph {
|
| 182 | 182 |
public: |
| 183 | 183 |
|
| 184 | 184 |
typedef _Graph Parent; |
| 185 | 185 |
typedef _Graph Graph; |
| 186 | 186 |
typedef GraphAdaptorExtender Adaptor; |
| 187 | 187 |
|
| 188 | 188 |
typedef typename Parent::Node Node; |
| 189 | 189 |
typedef typename Parent::Arc Arc; |
| 190 | 190 |
typedef typename Parent::Edge Edge; |
| 191 | 191 |
|
| 192 | 192 |
// Graph extension |
| 193 | 193 |
|
| 194 | 194 |
int maxId(Node) const {
|
| 195 | 195 |
return Parent::maxNodeId(); |
| 196 | 196 |
} |
| 197 | 197 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_GRAPH_EXTENDER_H |
| 20 | 20 |
#define LEMON_BITS_GRAPH_EXTENDER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/bits/map_extender.h> |
| 25 | 25 |
#include <lemon/bits/default_map.h> |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/concept_check.h> |
| 28 | 28 |
#include <lemon/concepts/maps.h> |
| 29 | 29 |
|
| 30 | 30 |
//\ingroup graphbits |
| 31 | 31 |
//\file |
| 32 | 32 |
//\brief Extenders for the graph types |
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
// \ingroup graphbits |
| 36 | 36 |
// |
| 37 | 37 |
// \brief Extender for the digraph implementations |
| 38 | 38 |
template <typename Base> |
| 39 | 39 |
class DigraphExtender : public Base {
|
| 40 | 40 |
public: |
| 41 | 41 |
|
| 42 | 42 |
typedef Base Parent; |
| 43 | 43 |
typedef DigraphExtender Digraph; |
| 44 | 44 |
|
| 45 | 45 |
// Base extensions |
| 46 | 46 |
|
| 47 | 47 |
typedef typename Parent::Node Node; |
| 48 | 48 |
typedef typename Parent::Arc Arc; |
| 49 | 49 |
|
| 50 | 50 |
int maxId(Node) const {
|
| 51 | 51 |
return Parent::maxNodeId(); |
| 52 | 52 |
} |
| 53 | 53 |
|
| 54 | 54 |
int maxId(Arc) const {
|
| 55 | 55 |
return Parent::maxArcId(); |
| 56 | 56 |
} |
| 57 | 57 |
|
| 58 | 58 |
Node fromId(int id, Node) const {
|
| 59 | 59 |
return Parent::nodeFromId(id); |
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 | 62 |
Arc fromId(int id, Arc) const {
|
| 63 | 63 |
return Parent::arcFromId(id); |
| 64 | 64 |
} |
| 65 | 65 |
|
| 66 | 66 |
Node oppositeNode(const Node &node, const Arc &arc) const {
|
| 67 | 67 |
if (node == Parent::source(arc)) |
| 68 | 68 |
return Parent::target(arc); |
| 69 | 69 |
else if(node == Parent::target(arc)) |
| 70 | 70 |
return Parent::source(arc); |
| 71 | 71 |
else |
| 72 | 72 |
return INVALID; |
| 73 | 73 |
} |
| 74 | 74 |
|
| 75 | 75 |
// Alterable extension |
| 76 | 76 |
|
| 77 | 77 |
typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier; |
| 78 | 78 |
typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier; |
| 79 | 79 |
|
| 80 | 80 |
|
| 81 | 81 |
protected: |
| 82 | 82 |
|
| 83 | 83 |
mutable NodeNotifier node_notifier; |
| 84 | 84 |
mutable ArcNotifier arc_notifier; |
| 85 | 85 |
|
| 86 | 86 |
public: |
| 87 | 87 |
|
| 88 | 88 |
NodeNotifier& notifier(Node) const {
|
| 89 | 89 |
return node_notifier; |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 | 92 |
ArcNotifier& notifier(Arc) const {
|
| 93 | 93 |
return arc_notifier; |
| 94 | 94 |
} |
| 95 | 95 |
|
| 96 | 96 |
class NodeIt : public Node {
|
| 97 | 97 |
const Digraph* _digraph; |
| 98 | 98 |
public: |
| 99 | 99 |
|
| 100 | 100 |
NodeIt() {}
|
| 101 | 101 |
|
| 102 | 102 |
NodeIt(Invalid i) : Node(i) { }
|
| 103 | 103 |
|
| 104 | 104 |
explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) {
|
| 105 | 105 |
_digraph->first(static_cast<Node&>(*this)); |
| 106 | 106 |
} |
| 107 | 107 |
|
| 108 | 108 |
NodeIt(const Digraph& digraph, const Node& node) |
| 109 | 109 |
: Node(node), _digraph(&digraph) {}
|
| 110 | 110 |
|
| 111 | 111 |
NodeIt& operator++() {
|
| 112 | 112 |
_digraph->next(*this); |
| 113 | 113 |
return *this; |
| 114 | 114 |
} |
| 115 | 115 |
|
| 116 | 116 |
}; |
| 117 | 117 |
|
| 118 | 118 |
|
| 119 | 119 |
class ArcIt : public Arc {
|
| 120 | 120 |
const Digraph* _digraph; |
| 121 | 121 |
public: |
| 122 | 122 |
|
| 123 | 123 |
ArcIt() { }
|
| 124 | 124 |
|
| 125 | 125 |
ArcIt(Invalid i) : Arc(i) { }
|
| 126 | 126 |
|
| 127 | 127 |
explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) {
|
| 128 | 128 |
_digraph->first(static_cast<Arc&>(*this)); |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
ArcIt(const Digraph& digraph, const Arc& arc) : |
| 132 | 132 |
Arc(arc), _digraph(&digraph) { }
|
| 133 | 133 |
|
| 134 | 134 |
ArcIt& operator++() {
|
| 135 | 135 |
_digraph->next(*this); |
| 136 | 136 |
return *this; |
| 137 | 137 |
} |
| 138 | 138 |
|
| 139 | 139 |
}; |
| 140 | 140 |
|
| 141 | 141 |
|
| 142 | 142 |
class OutArcIt : public Arc {
|
| 143 | 143 |
const Digraph* _digraph; |
| 144 | 144 |
public: |
| 145 | 145 |
|
| 146 | 146 |
OutArcIt() { }
|
| 147 | 147 |
|
| 148 | 148 |
OutArcIt(Invalid i) : Arc(i) { }
|
| 149 | 149 |
|
| 150 | 150 |
OutArcIt(const Digraph& digraph, const Node& node) |
| 151 | 151 |
: _digraph(&digraph) {
|
| 152 | 152 |
_digraph->firstOut(*this, node); |
| 153 | 153 |
} |
| 154 | 154 |
|
| 155 | 155 |
OutArcIt(const Digraph& digraph, const Arc& arc) |
| 156 | 156 |
: Arc(arc), _digraph(&digraph) {}
|
| 157 | 157 |
|
| 158 | 158 |
OutArcIt& operator++() {
|
| 159 | 159 |
_digraph->nextOut(*this); |
| 160 | 160 |
return *this; |
| 161 | 161 |
} |
| 162 | 162 |
|
| 163 | 163 |
}; |
| 164 | 164 |
|
| 165 | 165 |
|
| 166 | 166 |
class InArcIt : public Arc {
|
| 167 | 167 |
const Digraph* _digraph; |
| 168 | 168 |
public: |
| 169 | 169 |
|
| 170 | 170 |
InArcIt() { }
|
| 171 | 171 |
|
| 172 | 172 |
InArcIt(Invalid i) : Arc(i) { }
|
| 173 | 173 |
|
| 174 | 174 |
InArcIt(const Digraph& digraph, const Node& node) |
| 175 | 175 |
: _digraph(&digraph) {
|
| 176 | 176 |
_digraph->firstIn(*this, node); |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
InArcIt(const Digraph& digraph, const Arc& arc) : |
| 180 | 180 |
Arc(arc), _digraph(&digraph) {}
|
| 181 | 181 |
|
| 182 | 182 |
InArcIt& operator++() {
|
| 183 | 183 |
_digraph->nextIn(*this); |
| 184 | 184 |
return *this; |
| 185 | 185 |
} |
| 186 | 186 |
|
| 187 | 187 |
}; |
| 188 | 188 |
|
| 189 | 189 |
// \brief Base node of the iterator |
| 190 | 190 |
// |
| 191 | 191 |
// Returns the base node (i.e. the source in this case) of the iterator |
| 192 | 192 |
Node baseNode(const OutArcIt &arc) const {
|
| 193 | 193 |
return Parent::source(arc); |
| 194 | 194 |
} |
| 195 | 195 |
// \brief Running node of the iterator |
| 196 | 196 |
// |
| 197 | 197 |
// Returns the running node (i.e. the target in this case) of the |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_MAP_EXTENDER_H |
| 20 | 20 |
#define LEMON_BITS_MAP_EXTENDER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iterator> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/bits/traits.h> |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/concept_check.h> |
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
|
| 29 | 29 |
//\file |
| 30 | 30 |
//\brief Extenders for iterable maps. |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
// \ingroup graphbits |
| 35 | 35 |
// |
| 36 | 36 |
// \brief Extender for maps |
| 37 | 37 |
template <typename _Map> |
| 38 | 38 |
class MapExtender : public _Map {
|
| 39 | 39 |
public: |
| 40 | 40 |
|
| 41 | 41 |
typedef _Map Parent; |
| 42 | 42 |
typedef MapExtender Map; |
| 43 | 43 |
|
| 44 | 44 |
|
| 45 | 45 |
typedef typename Parent::Graph Graph; |
| 46 | 46 |
typedef typename Parent::Key Item; |
| 47 | 47 |
|
| 48 | 48 |
typedef typename Parent::Key Key; |
| 49 | 49 |
typedef typename Parent::Value Value; |
| 50 | 50 |
|
| 51 | 51 |
class MapIt; |
| 52 | 52 |
class ConstMapIt; |
| 53 | 53 |
|
| 54 | 54 |
friend class MapIt; |
| 55 | 55 |
friend class ConstMapIt; |
| 56 | 56 |
|
| 57 | 57 |
public: |
| 58 | 58 |
|
| 59 | 59 |
MapExtender(const Graph& graph) |
| 60 | 60 |
: Parent(graph) {}
|
| 61 | 61 |
|
| 62 | 62 |
MapExtender(const Graph& graph, const Value& value) |
| 63 | 63 |
: Parent(graph, value) {}
|
| 64 | 64 |
|
| 65 | 65 |
private: |
| 66 | 66 |
MapExtender& operator=(const MapExtender& cmap) {
|
| 67 | 67 |
return operator=<MapExtender>(cmap); |
| 68 | 68 |
} |
| 69 | 69 |
|
| 70 | 70 |
template <typename CMap> |
| 71 | 71 |
MapExtender& operator=(const CMap& cmap) {
|
| 72 | 72 |
Parent::operator=(cmap); |
| 73 | 73 |
return *this; |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 | 76 |
public: |
| 77 | 77 |
class MapIt : public Item {
|
| 78 | 78 |
public: |
| 79 | 79 |
|
| 80 | 80 |
typedef Item Parent; |
| 81 | 81 |
typedef typename Map::Value Value; |
| 82 | 82 |
|
| 83 | 83 |
MapIt() {}
|
| 84 | 84 |
|
| 85 | 85 |
MapIt(Invalid i) : Parent(i) { }
|
| 86 | 86 |
|
| 87 | 87 |
explicit MapIt(Map& _map) : map(_map) {
|
| 88 | 88 |
map.notifier()->first(*this); |
| 89 | 89 |
} |
| 90 | 90 |
|
| 91 | 91 |
MapIt(const Map& _map, const Item& item) |
| 92 | 92 |
: Parent(item), map(_map) {}
|
| 93 | 93 |
|
| 94 | 94 |
MapIt& operator++() {
|
| 95 | 95 |
map.notifier()->next(*this); |
| 96 | 96 |
return *this; |
| 97 | 97 |
} |
| 98 | 98 |
|
| 99 | 99 |
typename MapTraits<Map>::ConstReturnValue operator*() const {
|
| 100 | 100 |
return map[*this]; |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 | 103 |
typename MapTraits<Map>::ReturnValue operator*() {
|
| 104 | 104 |
return map[*this]; |
| 105 | 105 |
} |
| 106 | 106 |
|
| 107 | 107 |
void set(const Value& value) {
|
| 108 | 108 |
map.set(*this, value); |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 | 111 |
protected: |
| 112 | 112 |
Map& map; |
| 113 | 113 |
|
| 114 | 114 |
}; |
| 115 | 115 |
|
| 116 | 116 |
class ConstMapIt : public Item {
|
| 117 | 117 |
public: |
| 118 | 118 |
|
| 119 | 119 |
typedef Item Parent; |
| 120 | 120 |
|
| 121 | 121 |
typedef typename Map::Value Value; |
| 122 | 122 |
|
| 123 | 123 |
ConstMapIt() {}
|
| 124 | 124 |
|
| 125 | 125 |
ConstMapIt(Invalid i) : Parent(i) { }
|
| 126 | 126 |
|
| 127 | 127 |
explicit ConstMapIt(Map& _map) : map(_map) {
|
| 128 | 128 |
map.notifier()->first(*this); |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
ConstMapIt(const Map& _map, const Item& item) |
| 132 | 132 |
: Parent(item), map(_map) {}
|
| 133 | 133 |
|
| 134 | 134 |
ConstMapIt& operator++() {
|
| 135 | 135 |
map.notifier()->next(*this); |
| 136 | 136 |
return *this; |
| 137 | 137 |
} |
| 138 | 138 |
|
| 139 | 139 |
typename MapTraits<Map>::ConstReturnValue operator*() const {
|
| 140 | 140 |
return map[*this]; |
| 141 | 141 |
} |
| 142 | 142 |
|
| 143 | 143 |
protected: |
| 144 | 144 |
const Map& map; |
| 145 | 145 |
}; |
| 146 | 146 |
|
| 147 | 147 |
class ItemIt : public Item {
|
| 148 | 148 |
public: |
| 149 | 149 |
|
| 150 | 150 |
typedef Item Parent; |
| 151 | 151 |
|
| 152 | 152 |
ItemIt() {}
|
| 153 | 153 |
|
| 154 | 154 |
ItemIt(Invalid i) : Parent(i) { }
|
| 155 | 155 |
|
| 156 | 156 |
explicit ItemIt(Map& _map) : map(_map) {
|
| 157 | 157 |
map.notifier()->first(*this); |
| 158 | 158 |
} |
| 159 | 159 |
|
| 160 | 160 |
ItemIt(const Map& _map, const Item& item) |
| 161 | 161 |
: Parent(item), map(_map) {}
|
| 162 | 162 |
|
| 163 | 163 |
ItemIt& operator++() {
|
| 164 | 164 |
map.notifier()->next(*this); |
| 165 | 165 |
return *this; |
| 166 | 166 |
} |
| 167 | 167 |
|
| 168 | 168 |
protected: |
| 169 | 169 |
const Map& map; |
| 170 | 170 |
|
| 171 | 171 |
}; |
| 172 | 172 |
}; |
| 173 | 173 |
|
| 174 | 174 |
// \ingroup graphbits |
| 175 | 175 |
// |
| 176 | 176 |
// \brief Extender for maps which use a subset of the items. |
| 177 | 177 |
template <typename _Graph, typename _Map> |
| 178 | 178 |
class SubMapExtender : public _Map {
|
| 179 | 179 |
public: |
| 180 | 180 |
|
| 181 | 181 |
typedef _Map Parent; |
| 182 | 182 |
typedef SubMapExtender Map; |
| 183 | 183 |
|
| 184 | 184 |
typedef _Graph Graph; |
| 185 | 185 |
|
| 186 | 186 |
typedef typename Parent::Key Item; |
| 187 | 187 |
|
| 188 | 188 |
typedef typename Parent::Key Key; |
| 189 | 189 |
typedef typename Parent::Value Value; |
| 190 | 190 |
|
| 191 | 191 |
class MapIt; |
| 192 | 192 |
class ConstMapIt; |
| 193 | 193 |
|
| 194 | 194 |
friend class MapIt; |
| 195 | 195 |
friend class ConstMapIt; |
| 196 | 196 |
|
| 197 | 197 |
public: |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_PRED_MAP_PATH_H |
| 20 | 20 |
#define LEMON_BITS_PRED_MAP_PATH_H |
| 21 | 21 |
|
| 22 | 22 |
namespace lemon {
|
| 23 | 23 |
|
| 24 | 24 |
template <typename _Digraph, typename _PredMap> |
| 25 | 25 |
class PredMapPath {
|
| 26 | 26 |
public: |
| 27 | 27 |
typedef True RevPathTag; |
| 28 | 28 |
|
| 29 | 29 |
typedef _Digraph Digraph; |
| 30 | 30 |
typedef typename Digraph::Arc Arc; |
| 31 | 31 |
typedef _PredMap PredMap; |
| 32 | 32 |
|
| 33 | 33 |
PredMapPath(const Digraph& _digraph, const PredMap& _predMap, |
| 34 | 34 |
typename Digraph::Node _target) |
| 35 | 35 |
: digraph(_digraph), predMap(_predMap), target(_target) {}
|
| 36 | 36 |
|
| 37 | 37 |
int length() const {
|
| 38 | 38 |
int len = 0; |
| 39 | 39 |
typename Digraph::Node node = target; |
| 40 | 40 |
typename Digraph::Arc arc; |
| 41 | 41 |
while ((arc = predMap[node]) != INVALID) {
|
| 42 | 42 |
node = digraph.source(arc); |
| 43 | 43 |
++len; |
| 44 | 44 |
} |
| 45 | 45 |
return len; |
| 46 | 46 |
} |
| 47 | 47 |
|
| 48 | 48 |
bool empty() const {
|
| 49 | 49 |
return predMap[target] != INVALID; |
| 50 | 50 |
} |
| 51 | 51 |
|
| 52 | 52 |
class RevArcIt {
|
| 53 | 53 |
public: |
| 54 | 54 |
RevArcIt() {}
|
| 55 | 55 |
RevArcIt(Invalid) : path(0), current(INVALID) {}
|
| 56 | 56 |
RevArcIt(const PredMapPath& _path) |
| 57 | 57 |
: path(&_path), current(_path.target) {
|
| 58 | 58 |
if (path->predMap[current] == INVALID) current = INVALID; |
| 59 | 59 |
} |
| 60 | 60 |
|
| 61 | 61 |
operator const typename Digraph::Arc() const {
|
| 62 | 62 |
return path->predMap[current]; |
| 63 | 63 |
} |
| 64 | 64 |
|
| 65 | 65 |
RevArcIt& operator++() {
|
| 66 | 66 |
current = path->digraph.source(path->predMap[current]); |
| 67 | 67 |
if (path->predMap[current] == INVALID) current = INVALID; |
| 68 | 68 |
return *this; |
| 69 | 69 |
} |
| 70 | 70 |
|
| 71 | 71 |
bool operator==(const RevArcIt& e) const {
|
| 72 | 72 |
return current == e.current; |
| 73 | 73 |
} |
| 74 | 74 |
|
| 75 | 75 |
bool operator!=(const RevArcIt& e) const {
|
| 76 | 76 |
return current != e.current; |
| 77 | 77 |
} |
| 78 | 78 |
|
| 79 | 79 |
bool operator<(const RevArcIt& e) const {
|
| 80 | 80 |
return current < e.current; |
| 81 | 81 |
} |
| 82 | 82 |
|
| 83 | 83 |
private: |
| 84 | 84 |
const PredMapPath* path; |
| 85 | 85 |
typename Digraph::Node current; |
| 86 | 86 |
}; |
| 87 | 87 |
|
| 88 | 88 |
private: |
| 89 | 89 |
const Digraph& digraph; |
| 90 | 90 |
const PredMap& predMap; |
| 91 | 91 |
typename Digraph::Node target; |
| 92 | 92 |
}; |
| 93 | 93 |
|
| 94 | 94 |
|
| 95 | 95 |
template <typename _Digraph, typename _PredMatrixMap> |
| 96 | 96 |
class PredMatrixMapPath {
|
| 97 | 97 |
public: |
| 98 | 98 |
typedef True RevPathTag; |
| 99 | 99 |
|
| 100 | 100 |
typedef _Digraph Digraph; |
| 101 | 101 |
typedef typename Digraph::Arc Arc; |
| 102 | 102 |
typedef _PredMatrixMap PredMatrixMap; |
| 103 | 103 |
|
| 104 | 104 |
PredMatrixMapPath(const Digraph& _digraph, |
| 105 | 105 |
const PredMatrixMap& _predMatrixMap, |
| 106 | 106 |
typename Digraph::Node _source, |
| 107 | 107 |
typename Digraph::Node _target) |
| 108 | 108 |
: digraph(_digraph), predMatrixMap(_predMatrixMap), |
| 109 | 109 |
source(_source), target(_target) {}
|
| 110 | 110 |
|
| 111 | 111 |
int length() const {
|
| 112 | 112 |
int len = 0; |
| 113 | 113 |
typename Digraph::Node node = target; |
| 114 | 114 |
typename Digraph::Arc arc; |
| 115 | 115 |
while ((arc = predMatrixMap(source, node)) != INVALID) {
|
| 116 | 116 |
node = digraph.source(arc); |
| 117 | 117 |
++len; |
| 118 | 118 |
} |
| 119 | 119 |
return len; |
| 120 | 120 |
} |
| 121 | 121 |
|
| 122 | 122 |
bool empty() const {
|
| 123 | 123 |
return source != target; |
| 124 | 124 |
} |
| 125 | 125 |
|
| 126 | 126 |
class RevArcIt {
|
| 127 | 127 |
public: |
| 128 | 128 |
RevArcIt() {}
|
| 129 | 129 |
RevArcIt(Invalid) : path(0), current(INVALID) {}
|
| 130 | 130 |
RevArcIt(const PredMatrixMapPath& _path) |
| 131 | 131 |
: path(&_path), current(_path.target) {
|
| 132 | 132 |
if (path->predMatrixMap(path->source, current) == INVALID) |
| 133 | 133 |
current = INVALID; |
| 134 | 134 |
} |
| 135 | 135 |
|
| 136 | 136 |
operator const typename Digraph::Arc() const {
|
| 137 | 137 |
return path->predMatrixMap(path->source, current); |
| 138 | 138 |
} |
| 139 | 139 |
|
| 140 | 140 |
RevArcIt& operator++() {
|
| 141 | 141 |
current = |
| 142 | 142 |
path->digraph.source(path->predMatrixMap(path->source, current)); |
| 143 | 143 |
if (path->predMatrixMap(path->source, current) == INVALID) |
| 144 | 144 |
current = INVALID; |
| 145 | 145 |
return *this; |
| 146 | 146 |
} |
| 147 | 147 |
|
| 148 | 148 |
bool operator==(const RevArcIt& e) const {
|
| 149 | 149 |
return current == e.current; |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
bool operator!=(const RevArcIt& e) const {
|
| 153 | 153 |
return current != e.current; |
| 154 | 154 |
} |
| 155 | 155 |
|
| 156 | 156 |
bool operator<(const RevArcIt& e) const {
|
| 157 | 157 |
return current < e.current; |
| 158 | 158 |
} |
| 159 | 159 |
|
| 160 | 160 |
private: |
| 161 | 161 |
const PredMatrixMapPath* path; |
| 162 | 162 |
typename Digraph::Node current; |
| 163 | 163 |
}; |
| 164 | 164 |
|
| 165 | 165 |
private: |
| 166 | 166 |
const Digraph& digraph; |
| 167 | 167 |
const PredMatrixMap& predMatrixMap; |
| 168 | 168 |
typename Digraph::Node source; |
| 169 | 169 |
typename Digraph::Node target; |
| 170 | 170 |
}; |
| 171 | 171 |
|
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_TRAITS_H |
| 20 | 20 |
#define LEMON_BITS_TRAITS_H |
| 21 | 21 |
|
| 22 | 22 |
//\file |
| 23 | 23 |
//\brief Traits for graphs and maps |
| 24 | 24 |
// |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/bits/enable_if.h> |
| 27 | 27 |
|
| 28 | 28 |
namespace lemon {
|
| 29 | 29 |
|
| 30 | 30 |
struct InvalidType {};
|
| 31 | 31 |
|
| 32 | 32 |
template <typename _Graph, typename _Item> |
| 33 | 33 |
class ItemSetTraits {};
|
| 34 | 34 |
|
| 35 | 35 |
|
| 36 | 36 |
template <typename Graph, typename Enable = void> |
| 37 | 37 |
struct NodeNotifierIndicator {
|
| 38 | 38 |
typedef InvalidType Type; |
| 39 | 39 |
}; |
| 40 | 40 |
template <typename Graph> |
| 41 | 41 |
struct NodeNotifierIndicator< |
| 42 | 42 |
Graph, |
| 43 | 43 |
typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type |
| 44 | 44 |
> {
|
| 45 | 45 |
typedef typename Graph::NodeNotifier Type; |
| 46 | 46 |
}; |
| 47 | 47 |
|
| 48 | 48 |
template <typename _Graph> |
| 49 | 49 |
class ItemSetTraits<_Graph, typename _Graph::Node> {
|
| 50 | 50 |
public: |
| 51 | 51 |
|
| 52 | 52 |
typedef _Graph Graph; |
| 53 | 53 |
|
| 54 | 54 |
typedef typename Graph::Node Item; |
| 55 | 55 |
typedef typename Graph::NodeIt ItemIt; |
| 56 | 56 |
|
| 57 | 57 |
typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier; |
| 58 | 58 |
|
| 59 | 59 |
template <typename _Value> |
| 60 | 60 |
class Map : public Graph::template NodeMap<_Value> {
|
| 61 | 61 |
public: |
| 62 | 62 |
typedef typename Graph::template NodeMap<_Value> Parent; |
| 63 | 63 |
typedef typename Graph::template NodeMap<_Value> Type; |
| 64 | 64 |
typedef typename Parent::Value Value; |
| 65 | 65 |
|
| 66 | 66 |
Map(const Graph& _digraph) : Parent(_digraph) {}
|
| 67 | 67 |
Map(const Graph& _digraph, const Value& _value) |
| 68 | 68 |
: Parent(_digraph, _value) {}
|
| 69 | 69 |
|
| 70 | 70 |
}; |
| 71 | 71 |
|
| 72 | 72 |
}; |
| 73 | 73 |
|
| 74 | 74 |
template <typename Graph, typename Enable = void> |
| 75 | 75 |
struct ArcNotifierIndicator {
|
| 76 | 76 |
typedef InvalidType Type; |
| 77 | 77 |
}; |
| 78 | 78 |
template <typename Graph> |
| 79 | 79 |
struct ArcNotifierIndicator< |
| 80 | 80 |
Graph, |
| 81 | 81 |
typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type |
| 82 | 82 |
> {
|
| 83 | 83 |
typedef typename Graph::ArcNotifier Type; |
| 84 | 84 |
}; |
| 85 | 85 |
|
| 86 | 86 |
template <typename _Graph> |
| 87 | 87 |
class ItemSetTraits<_Graph, typename _Graph::Arc> {
|
| 88 | 88 |
public: |
| 89 | 89 |
|
| 90 | 90 |
typedef _Graph Graph; |
| 91 | 91 |
|
| 92 | 92 |
typedef typename Graph::Arc Item; |
| 93 | 93 |
typedef typename Graph::ArcIt ItemIt; |
| 94 | 94 |
|
| 95 | 95 |
typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier; |
| 96 | 96 |
|
| 97 | 97 |
template <typename _Value> |
| 98 | 98 |
class Map : public Graph::template ArcMap<_Value> {
|
| 99 | 99 |
public: |
| 100 | 100 |
typedef typename Graph::template ArcMap<_Value> Parent; |
| 101 | 101 |
typedef typename Graph::template ArcMap<_Value> Type; |
| 102 | 102 |
typedef typename Parent::Value Value; |
| 103 | 103 |
|
| 104 | 104 |
Map(const Graph& _digraph) : Parent(_digraph) {}
|
| 105 | 105 |
Map(const Graph& _digraph, const Value& _value) |
| 106 | 106 |
: Parent(_digraph, _value) {}
|
| 107 | 107 |
}; |
| 108 | 108 |
|
| 109 | 109 |
}; |
| 110 | 110 |
|
| 111 | 111 |
template <typename Graph, typename Enable = void> |
| 112 | 112 |
struct EdgeNotifierIndicator {
|
| 113 | 113 |
typedef InvalidType Type; |
| 114 | 114 |
}; |
| 115 | 115 |
template <typename Graph> |
| 116 | 116 |
struct EdgeNotifierIndicator< |
| 117 | 117 |
Graph, |
| 118 | 118 |
typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type |
| 119 | 119 |
> {
|
| 120 | 120 |
typedef typename Graph::EdgeNotifier Type; |
| 121 | 121 |
}; |
| 122 | 122 |
|
| 123 | 123 |
template <typename _Graph> |
| 124 | 124 |
class ItemSetTraits<_Graph, typename _Graph::Edge> {
|
| 125 | 125 |
public: |
| 126 | 126 |
|
| 127 | 127 |
typedef _Graph Graph; |
| 128 | 128 |
|
| 129 | 129 |
typedef typename Graph::Edge Item; |
| 130 | 130 |
typedef typename Graph::EdgeIt ItemIt; |
| 131 | 131 |
|
| 132 | 132 |
typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier; |
| 133 | 133 |
|
| 134 | 134 |
template <typename _Value> |
| 135 | 135 |
class Map : public Graph::template EdgeMap<_Value> {
|
| 136 | 136 |
public: |
| 137 | 137 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
| 138 | 138 |
typedef typename Graph::template EdgeMap<_Value> Type; |
| 139 | 139 |
typedef typename Parent::Value Value; |
| 140 | 140 |
|
| 141 | 141 |
Map(const Graph& _digraph) : Parent(_digraph) {}
|
| 142 | 142 |
Map(const Graph& _digraph, const Value& _value) |
| 143 | 143 |
: Parent(_digraph, _value) {}
|
| 144 | 144 |
}; |
| 145 | 145 |
|
| 146 | 146 |
}; |
| 147 | 147 |
|
| 148 | 148 |
template <typename Map, typename Enable = void> |
| 149 | 149 |
struct MapTraits {
|
| 150 | 150 |
typedef False ReferenceMapTag; |
| 151 | 151 |
|
| 152 | 152 |
typedef typename Map::Key Key; |
| 153 | 153 |
typedef typename Map::Value Value; |
| 154 | 154 |
|
| 155 | 155 |
typedef Value ConstReturnValue; |
| 156 | 156 |
typedef Value ReturnValue; |
| 157 | 157 |
}; |
| 158 | 158 |
|
| 159 | 159 |
template <typename Map> |
| 160 | 160 |
struct MapTraits< |
| 161 | 161 |
Map, typename enable_if<typename Map::ReferenceMapTag, void>::type > |
| 162 | 162 |
{
|
| 163 | 163 |
typedef True ReferenceMapTag; |
| 164 | 164 |
|
| 165 | 165 |
typedef typename Map::Key Key; |
| 166 | 166 |
typedef typename Map::Value Value; |
| 167 | 167 |
|
| 168 | 168 |
typedef typename Map::ConstReference ConstReturnValue; |
| 169 | 169 |
typedef typename Map::Reference ReturnValue; |
| 170 | 170 |
|
| 171 | 171 |
typedef typename Map::ConstReference ConstReference; |
| 172 | 172 |
typedef typename Map::Reference Reference; |
| 173 | 173 |
}; |
| 174 | 174 |
|
| 175 | 175 |
template <typename MatrixMap, typename Enable = void> |
| 176 | 176 |
struct MatrixMapTraits {
|
| 177 | 177 |
typedef False ReferenceMapTag; |
| 178 | 178 |
|
| 179 | 179 |
typedef typename MatrixMap::FirstKey FirstKey; |
| 180 | 180 |
typedef typename MatrixMap::SecondKey SecondKey; |
| 181 | 181 |
typedef typename MatrixMap::Value Value; |
| 182 | 182 |
|
| 183 | 183 |
typedef Value ConstReturnValue; |
| 184 | 184 |
typedef Value ReturnValue; |
| 185 | 185 |
}; |
| 186 | 186 |
|
| 187 | 187 |
template <typename MatrixMap> |
| 188 | 188 |
struct MatrixMapTraits< |
| 189 | 189 |
MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag, |
| 190 | 190 |
void>::type > |
| 191 | 191 |
{
|
| 192 | 192 |
typedef True ReferenceMapTag; |
| 193 | 193 |
|
| 194 | 194 |
typedef typename MatrixMap::FirstKey FirstKey; |
| 195 | 195 |
typedef typename MatrixMap::SecondKey SecondKey; |
| 196 | 196 |
typedef typename MatrixMap::Value Value; |
| 197 | 197 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_VARIANT_H |
| 20 | 20 |
#define LEMON_BITS_VARIANT_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/assert.h> |
| 23 | 23 |
|
| 24 | 24 |
// \file |
| 25 | 25 |
// \brief Variant types |
| 26 | 26 |
|
| 27 | 27 |
namespace lemon {
|
| 28 | 28 |
|
| 29 | 29 |
namespace _variant_bits {
|
| 30 | 30 |
|
| 31 | 31 |
template <int left, int right> |
| 32 | 32 |
struct CTMax {
|
| 33 | 33 |
static const int value = left < right ? right : left; |
| 34 | 34 |
}; |
| 35 | 35 |
|
| 36 | 36 |
} |
| 37 | 37 |
|
| 38 | 38 |
|
| 39 | 39 |
// \brief Simple Variant type for two types |
| 40 | 40 |
// |
| 41 | 41 |
// Simple Variant type for two types. The Variant type is a type-safe |
| 42 | 42 |
// union. C++ has strong limitations for using unions, for |
| 43 | 43 |
// example you cannot store a type with non-default constructor or |
| 44 | 44 |
// destructor in a union. This class always knowns the current |
| 45 | 45 |
// state of the variant and it cares for the proper construction |
| 46 | 46 |
// and destruction. |
| 47 | 47 |
template <typename _First, typename _Second> |
| 48 | 48 |
class BiVariant {
|
| 49 | 49 |
public: |
| 50 | 50 |
|
| 51 | 51 |
// \brief The \c First type. |
| 52 | 52 |
typedef _First First; |
| 53 | 53 |
// \brief The \c Second type. |
| 54 | 54 |
typedef _Second Second; |
| 55 | 55 |
|
| 56 | 56 |
// \brief Constructor |
| 57 | 57 |
// |
| 58 | 58 |
// This constructor initalizes to the default value of the \c First |
| 59 | 59 |
// type. |
| 60 | 60 |
BiVariant() {
|
| 61 | 61 |
flag = true; |
| 62 | 62 |
new(reinterpret_cast<First*>(data)) First(); |
| 63 | 63 |
} |
| 64 | 64 |
|
| 65 | 65 |
// \brief Constructor |
| 66 | 66 |
// |
| 67 | 67 |
// This constructor initalizes to the given value of the \c First |
| 68 | 68 |
// type. |
| 69 | 69 |
BiVariant(const First& f) {
|
| 70 | 70 |
flag = true; |
| 71 | 71 |
new(reinterpret_cast<First*>(data)) First(f); |
| 72 | 72 |
} |
| 73 | 73 |
|
| 74 | 74 |
// \brief Constructor |
| 75 | 75 |
// |
| 76 | 76 |
// This constructor initalizes to the given value of the \c |
| 77 | 77 |
// Second type. |
| 78 | 78 |
BiVariant(const Second& s) {
|
| 79 | 79 |
flag = false; |
| 80 | 80 |
new(reinterpret_cast<Second*>(data)) Second(s); |
| 81 | 81 |
} |
| 82 | 82 |
|
| 83 | 83 |
// \brief Copy constructor |
| 84 | 84 |
// |
| 85 | 85 |
// Copy constructor |
| 86 | 86 |
BiVariant(const BiVariant& bivariant) {
|
| 87 | 87 |
flag = bivariant.flag; |
| 88 | 88 |
if (flag) {
|
| 89 | 89 |
new(reinterpret_cast<First*>(data)) First(bivariant.first()); |
| 90 | 90 |
} else {
|
| 91 | 91 |
new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); |
| 92 | 92 |
} |
| 93 | 93 |
} |
| 94 | 94 |
|
| 95 | 95 |
// \brief Destrcutor |
| 96 | 96 |
// |
| 97 | 97 |
// Destructor |
| 98 | 98 |
~BiVariant() {
|
| 99 | 99 |
destroy(); |
| 100 | 100 |
} |
| 101 | 101 |
|
| 102 | 102 |
// \brief Set to the default value of the \c First type. |
| 103 | 103 |
// |
| 104 | 104 |
// This function sets the variant to the default value of the \c |
| 105 | 105 |
// First type. |
| 106 | 106 |
BiVariant& setFirst() {
|
| 107 | 107 |
destroy(); |
| 108 | 108 |
flag = true; |
| 109 | 109 |
new(reinterpret_cast<First*>(data)) First(); |
| 110 | 110 |
return *this; |
| 111 | 111 |
} |
| 112 | 112 |
|
| 113 | 113 |
// \brief Set to the given value of the \c First type. |
| 114 | 114 |
// |
| 115 | 115 |
// This function sets the variant to the given value of the \c |
| 116 | 116 |
// First type. |
| 117 | 117 |
BiVariant& setFirst(const First& f) {
|
| 118 | 118 |
destroy(); |
| 119 | 119 |
flag = true; |
| 120 | 120 |
new(reinterpret_cast<First*>(data)) First(f); |
| 121 | 121 |
return *this; |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
// \brief Set to the default value of the \c Second type. |
| 125 | 125 |
// |
| 126 | 126 |
// This function sets the variant to the default value of the \c |
| 127 | 127 |
// Second type. |
| 128 | 128 |
BiVariant& setSecond() {
|
| 129 | 129 |
destroy(); |
| 130 | 130 |
flag = false; |
| 131 | 131 |
new(reinterpret_cast<Second*>(data)) Second(); |
| 132 | 132 |
return *this; |
| 133 | 133 |
} |
| 134 | 134 |
|
| 135 | 135 |
// \brief Set to the given value of the \c Second type. |
| 136 | 136 |
// |
| 137 | 137 |
// This function sets the variant to the given value of the \c |
| 138 | 138 |
// Second type. |
| 139 | 139 |
BiVariant& setSecond(const Second& s) {
|
| 140 | 140 |
destroy(); |
| 141 | 141 |
flag = false; |
| 142 | 142 |
new(reinterpret_cast<Second*>(data)) Second(s); |
| 143 | 143 |
return *this; |
| 144 | 144 |
} |
| 145 | 145 |
|
| 146 | 146 |
// \brief Operator form of the \c setFirst() |
| 147 | 147 |
BiVariant& operator=(const First& f) {
|
| 148 | 148 |
return setFirst(f); |
| 149 | 149 |
} |
| 150 | 150 |
|
| 151 | 151 |
// \brief Operator form of the \c setSecond() |
| 152 | 152 |
BiVariant& operator=(const Second& s) {
|
| 153 | 153 |
return setSecond(s); |
| 154 | 154 |
} |
| 155 | 155 |
|
| 156 | 156 |
// \brief Assign operator |
| 157 | 157 |
BiVariant& operator=(const BiVariant& bivariant) {
|
| 158 | 158 |
if (this == &bivariant) return *this; |
| 159 | 159 |
destroy(); |
| 160 | 160 |
flag = bivariant.flag; |
| 161 | 161 |
if (flag) {
|
| 162 | 162 |
new(reinterpret_cast<First*>(data)) First(bivariant.first()); |
| 163 | 163 |
} else {
|
| 164 | 164 |
new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); |
| 165 | 165 |
} |
| 166 | 166 |
return *this; |
| 167 | 167 |
} |
| 168 | 168 |
|
| 169 | 169 |
// \brief Reference to the value |
| 170 | 170 |
// |
| 171 | 171 |
// Reference to the value of the \c First type. |
| 172 | 172 |
// \pre The BiVariant should store value of \c First type. |
| 173 | 173 |
First& first() {
|
| 174 | 174 |
LEMON_DEBUG(flag, "Variant wrong state"); |
| 175 | 175 |
return *reinterpret_cast<First*>(data); |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
// \brief Const reference to the value |
| 179 | 179 |
// |
| 180 | 180 |
// Const reference to the value of the \c First type. |
| 181 | 181 |
// \pre The BiVariant should store value of \c First type. |
| 182 | 182 |
const First& first() const {
|
| 183 | 183 |
LEMON_DEBUG(flag, "Variant wrong state"); |
| 184 | 184 |
return *reinterpret_cast<const First*>(data); |
| 185 | 185 |
} |
| 186 | 186 |
|
| 187 | 187 |
// \brief Operator form of the \c first() |
| 188 | 188 |
operator First&() { return first(); }
|
| 189 | 189 |
// \brief Operator form of the const \c first() |
| 190 | 190 |
operator const First&() const { return first(); }
|
| 191 | 191 |
|
| 192 | 192 |
// \brief Reference to the value |
| 193 | 193 |
// |
| 194 | 194 |
// Reference to the value of the \c Second type. |
| 195 | 195 |
// \pre The BiVariant should store value of \c Second type. |
| 196 | 196 |
Second& second() {
|
| 197 | 197 |
LEMON_DEBUG(!flag, "Variant wrong state"); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_VECTOR_MAP_H |
| 20 | 20 |
#define LEMON_BITS_VECTOR_MAP_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <algorithm> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/core.h> |
| 26 | 26 |
#include <lemon/bits/alteration_notifier.h> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/concept_check.h> |
| 29 | 29 |
#include <lemon/concepts/maps.h> |
| 30 | 30 |
|
| 31 | 31 |
//\ingroup graphbits |
| 32 | 32 |
// |
| 33 | 33 |
//\file |
| 34 | 34 |
//\brief Vector based graph maps. |
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
// \ingroup graphbits |
| 38 | 38 |
// |
| 39 | 39 |
// \brief Graph map based on the std::vector storage. |
| 40 | 40 |
// |
| 41 | 41 |
// The VectorMap template class is graph map structure that automatically |
| 42 | 42 |
// updates the map when a key is added to or erased from the graph. |
| 43 | 43 |
// This map type uses std::vector to store the values. |
| 44 | 44 |
// |
| 45 | 45 |
// \tparam _Graph The graph this map is attached to. |
| 46 | 46 |
// \tparam _Item The item type of the graph items. |
| 47 | 47 |
// \tparam _Value The value type of the map. |
| 48 | 48 |
template <typename _Graph, typename _Item, typename _Value> |
| 49 | 49 |
class VectorMap |
| 50 | 50 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
|
| 51 | 51 |
private: |
| 52 | 52 |
|
| 53 | 53 |
// The container type of the map. |
| 54 | 54 |
typedef std::vector<_Value> Container; |
| 55 | 55 |
|
| 56 | 56 |
public: |
| 57 | 57 |
|
| 58 | 58 |
// The graph type of the map. |
| 59 | 59 |
typedef _Graph Graph; |
| 60 | 60 |
// The item type of the map. |
| 61 | 61 |
typedef _Item Item; |
| 62 | 62 |
// The reference map tag. |
| 63 | 63 |
typedef True ReferenceMapTag; |
| 64 | 64 |
|
| 65 | 65 |
// The key type of the map. |
| 66 | 66 |
typedef _Item Key; |
| 67 | 67 |
// The value type of the map. |
| 68 | 68 |
typedef _Value Value; |
| 69 | 69 |
|
| 70 | 70 |
// The notifier type. |
| 71 | 71 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
| 72 | 72 |
|
| 73 | 73 |
// The map type. |
| 74 | 74 |
typedef VectorMap Map; |
| 75 | 75 |
// The base class of the map. |
| 76 | 76 |
typedef typename Notifier::ObserverBase Parent; |
| 77 | 77 |
|
| 78 | 78 |
// The reference type of the map; |
| 79 | 79 |
typedef typename Container::reference Reference; |
| 80 | 80 |
// The const reference type of the map; |
| 81 | 81 |
typedef typename Container::const_reference ConstReference; |
| 82 | 82 |
|
| 83 | 83 |
|
| 84 | 84 |
// \brief Constructor to attach the new map into the notifier. |
| 85 | 85 |
// |
| 86 | 86 |
// It constructs a map and attachs it into the notifier. |
| 87 | 87 |
// It adds all the items of the graph to the map. |
| 88 | 88 |
VectorMap(const Graph& graph) {
|
| 89 | 89 |
Parent::attach(graph.notifier(Item())); |
| 90 | 90 |
container.resize(Parent::notifier()->maxId() + 1); |
| 91 | 91 |
} |
| 92 | 92 |
|
| 93 | 93 |
// \brief Constructor uses given value to initialize the map. |
| 94 | 94 |
// |
| 95 | 95 |
// It constructs a map uses a given value to initialize the map. |
| 96 | 96 |
// It adds all the items of the graph to the map. |
| 97 | 97 |
VectorMap(const Graph& graph, const Value& value) {
|
| 98 | 98 |
Parent::attach(graph.notifier(Item())); |
| 99 | 99 |
container.resize(Parent::notifier()->maxId() + 1, value); |
| 100 | 100 |
} |
| 101 | 101 |
|
| 102 | 102 |
private: |
| 103 | 103 |
// \brief Copy constructor |
| 104 | 104 |
// |
| 105 | 105 |
// Copy constructor. |
| 106 | 106 |
VectorMap(const VectorMap& _copy) : Parent() {
|
| 107 | 107 |
if (_copy.attached()) {
|
| 108 | 108 |
Parent::attach(*_copy.notifier()); |
| 109 | 109 |
container = _copy.container; |
| 110 | 110 |
} |
| 111 | 111 |
} |
| 112 | 112 |
|
| 113 | 113 |
// \brief Assign operator. |
| 114 | 114 |
// |
| 115 | 115 |
// This operator assigns for each item in the map the |
| 116 | 116 |
// value mapped to the same item in the copied map. |
| 117 | 117 |
// The parameter map should be indiced with the same |
| 118 | 118 |
// itemset because this assign operator does not change |
| 119 | 119 |
// the container of the map. |
| 120 | 120 |
VectorMap& operator=(const VectorMap& cmap) {
|
| 121 | 121 |
return operator=<VectorMap>(cmap); |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
|
| 125 | 125 |
// \brief Template assign operator. |
| 126 | 126 |
// |
| 127 | 127 |
// The given parameter should be conform to the ReadMap |
| 128 | 128 |
// concecpt and could be indiced by the current item set of |
| 129 | 129 |
// the NodeMap. In this case the value for each item |
| 130 | 130 |
// is assigned by the value of the given ReadMap. |
| 131 | 131 |
template <typename CMap> |
| 132 | 132 |
VectorMap& operator=(const CMap& cmap) {
|
| 133 | 133 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
| 134 | 134 |
const typename Parent::Notifier* nf = Parent::notifier(); |
| 135 | 135 |
Item it; |
| 136 | 136 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 137 | 137 |
set(it, cmap[it]); |
| 138 | 138 |
} |
| 139 | 139 |
return *this; |
| 140 | 140 |
} |
| 141 | 141 |
|
| 142 | 142 |
public: |
| 143 | 143 |
|
| 144 | 144 |
// \brief The subcript operator. |
| 145 | 145 |
// |
| 146 | 146 |
// The subscript operator. The map can be subscripted by the |
| 147 | 147 |
// actual items of the graph. |
| 148 | 148 |
Reference operator[](const Key& key) {
|
| 149 | 149 |
return container[Parent::notifier()->id(key)]; |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
// \brief The const subcript operator. |
| 153 | 153 |
// |
| 154 | 154 |
// The const subscript operator. The map can be subscripted by the |
| 155 | 155 |
// actual items of the graph. |
| 156 | 156 |
ConstReference operator[](const Key& key) const {
|
| 157 | 157 |
return container[Parent::notifier()->id(key)]; |
| 158 | 158 |
} |
| 159 | 159 |
|
| 160 | 160 |
|
| 161 | 161 |
// \brief The setter function of the map. |
| 162 | 162 |
// |
| 163 | 163 |
// It the same as operator[](key) = value expression. |
| 164 | 164 |
void set(const Key& key, const Value& value) {
|
| 165 | 165 |
(*this)[key] = value; |
| 166 | 166 |
} |
| 167 | 167 |
|
| 168 | 168 |
protected: |
| 169 | 169 |
|
| 170 | 170 |
// \brief Adds a new key to the map. |
| 171 | 171 |
// |
| 172 | 172 |
// It adds a new key to the map. It is called by the observer notifier |
| 173 | 173 |
// and it overrides the add() member function of the observer base. |
| 174 | 174 |
virtual void add(const Key& key) {
|
| 175 | 175 |
int id = Parent::notifier()->id(key); |
| 176 | 176 |
if (id >= int(container.size())) {
|
| 177 | 177 |
container.resize(id + 1); |
| 178 | 178 |
} |
| 179 | 179 |
} |
| 180 | 180 |
|
| 181 | 181 |
// \brief Adds more new keys to the map. |
| 182 | 182 |
// |
| 183 | 183 |
// It adds more new keys to the map. It is called by the observer notifier |
| 184 | 184 |
// and it overrides the add() member function of the observer base. |
| 185 | 185 |
virtual void add(const std::vector<Key>& keys) {
|
| 186 | 186 |
int max = container.size() - 1; |
| 187 | 187 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 188 | 188 |
int id = Parent::notifier()->id(keys[i]); |
| 189 | 189 |
if (id >= max) {
|
| 190 | 190 |
max = id; |
| 191 | 191 |
} |
| 192 | 192 |
} |
| 193 | 193 |
container.resize(max + 1); |
| 194 | 194 |
} |
| 195 | 195 |
|
| 196 | 196 |
// \brief Erase a key from the map. |
| 197 | 197 |
// |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CIRCULATION_H |
| 20 | 20 |
#define LEMON_CIRCULATION_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/tolerance.h> |
| 23 | 23 |
#include <lemon/elevator.h> |
| 24 | 24 |
|
| 25 | 25 |
///\ingroup max_flow |
| 26 | 26 |
///\file |
| 27 | 27 |
///\brief Push-relabel algorithm for finding a feasible circulation. |
| 28 | 28 |
/// |
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
/// \brief Default traits class of Circulation class. |
| 32 | 32 |
/// |
| 33 | 33 |
/// Default traits class of Circulation class. |
| 34 | 34 |
/// \tparam _Diraph Digraph type. |
| 35 | 35 |
/// \tparam _LCapMap Lower bound capacity map type. |
| 36 | 36 |
/// \tparam _UCapMap Upper bound capacity map type. |
| 37 | 37 |
/// \tparam _DeltaMap Delta map type. |
| 38 | 38 |
template <typename _Diraph, typename _LCapMap, |
| 39 | 39 |
typename _UCapMap, typename _DeltaMap> |
| 40 | 40 |
struct CirculationDefaultTraits {
|
| 41 | 41 |
|
| 42 | 42 |
/// \brief The type of the digraph the algorithm runs on. |
| 43 | 43 |
typedef _Diraph Digraph; |
| 44 | 44 |
|
| 45 | 45 |
/// \brief The type of the map that stores the circulation lower |
| 46 | 46 |
/// bound. |
| 47 | 47 |
/// |
| 48 | 48 |
/// The type of the map that stores the circulation lower bound. |
| 49 | 49 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 50 | 50 |
typedef _LCapMap LCapMap; |
| 51 | 51 |
|
| 52 | 52 |
/// \brief The type of the map that stores the circulation upper |
| 53 | 53 |
/// bound. |
| 54 | 54 |
/// |
| 55 | 55 |
/// The type of the map that stores the circulation upper bound. |
| 56 | 56 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 57 | 57 |
typedef _UCapMap UCapMap; |
| 58 | 58 |
|
| 59 | 59 |
/// \brief The type of the map that stores the lower bound for |
| 60 | 60 |
/// the supply of the nodes. |
| 61 | 61 |
/// |
| 62 | 62 |
/// The type of the map that stores the lower bound for the supply |
| 63 | 63 |
/// of the nodes. It must meet the \ref concepts::ReadMap "ReadMap" |
| 64 | 64 |
/// concept. |
| 65 | 65 |
typedef _DeltaMap DeltaMap; |
| 66 | 66 |
|
| 67 | 67 |
/// \brief The type of the flow values. |
| 68 | 68 |
typedef typename DeltaMap::Value Value; |
| 69 | 69 |
|
| 70 | 70 |
/// \brief The type of the map that stores the flow values. |
| 71 | 71 |
/// |
| 72 | 72 |
/// The type of the map that stores the flow values. |
| 73 | 73 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 74 | 74 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
| 75 | 75 |
|
| 76 | 76 |
/// \brief Instantiates a FlowMap. |
| 77 | 77 |
/// |
| 78 | 78 |
/// This function instantiates a \ref FlowMap. |
| 79 | 79 |
/// \param digraph The digraph, to which we would like to define |
| 80 | 80 |
/// the flow map. |
| 81 | 81 |
static FlowMap* createFlowMap(const Digraph& digraph) {
|
| 82 | 82 |
return new FlowMap(digraph); |
| 83 | 83 |
} |
| 84 | 84 |
|
| 85 | 85 |
/// \brief The elevator type used by the algorithm. |
| 86 | 86 |
/// |
| 87 | 87 |
/// The elevator type used by the algorithm. |
| 88 | 88 |
/// |
| 89 | 89 |
/// \sa Elevator |
| 90 | 90 |
/// \sa LinkedElevator |
| 91 | 91 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
| 92 | 92 |
|
| 93 | 93 |
/// \brief Instantiates an Elevator. |
| 94 | 94 |
/// |
| 95 | 95 |
/// This function instantiates an \ref Elevator. |
| 96 | 96 |
/// \param digraph The digraph, to which we would like to define |
| 97 | 97 |
/// the elevator. |
| 98 | 98 |
/// \param max_level The maximum level of the elevator. |
| 99 | 99 |
static Elevator* createElevator(const Digraph& digraph, int max_level) {
|
| 100 | 100 |
return new Elevator(digraph, max_level); |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 | 103 |
/// \brief The tolerance used by the algorithm |
| 104 | 104 |
/// |
| 105 | 105 |
/// The tolerance used by the algorithm to handle inexact computation. |
| 106 | 106 |
typedef lemon::Tolerance<Value> Tolerance; |
| 107 | 107 |
|
| 108 | 108 |
}; |
| 109 | 109 |
|
| 110 | 110 |
/** |
| 111 | 111 |
\brief Push-relabel algorithm for the network circulation problem. |
| 112 | 112 |
|
| 113 | 113 |
\ingroup max_flow |
| 114 | 114 |
This class implements a push-relabel algorithm for the network |
| 115 | 115 |
circulation problem. |
| 116 | 116 |
It is to find a feasible circulation when lower and upper bounds |
| 117 | 117 |
are given for the flow values on the arcs and lower bounds |
| 118 | 118 |
are given for the supply values of the nodes. |
| 119 | 119 |
|
| 120 | 120 |
The exact formulation of this problem is the following. |
| 121 | 121 |
Let \f$G=(V,A)\f$ be a digraph, |
| 122 | 122 |
\f$lower, upper: A\rightarrow\mathbf{R}^+_0\f$,
|
| 123 | 123 |
\f$delta: V\rightarrow\mathbf{R}\f$. Find a feasible circulation
|
| 124 | 124 |
\f$f: A\rightarrow\mathbf{R}^+_0\f$ so that
|
| 125 | 125 |
\f[ \sum_{a\in\delta_{out}(v)} f(a) - \sum_{a\in\delta_{in}(v)} f(a)
|
| 126 | 126 |
\geq delta(v) \quad \forall v\in V, \f] |
| 127 | 127 |
\f[ lower(a)\leq f(a) \leq upper(a) \quad \forall a\in A. \f] |
| 128 | 128 |
\note \f$delta(v)\f$ specifies a lower bound for the supply of node |
| 129 | 129 |
\f$v\f$. It can be either positive or negative, however note that |
| 130 | 130 |
\f$\sum_{v\in V}delta(v)\f$ should be zero or negative in order to
|
| 131 | 131 |
have a feasible solution. |
| 132 | 132 |
|
| 133 | 133 |
\note A special case of this problem is when |
| 134 | 134 |
\f$\sum_{v\in V}delta(v) = 0\f$. Then the supply of each node \f$v\f$
|
| 135 | 135 |
will be \e equal \e to \f$delta(v)\f$, if a circulation can be found. |
| 136 | 136 |
Thus a feasible solution for the |
| 137 | 137 |
\ref min_cost_flow "minimum cost flow" problem can be calculated |
| 138 | 138 |
in this way. |
| 139 | 139 |
|
| 140 | 140 |
\tparam _Digraph The type of the digraph the algorithm runs on. |
| 141 | 141 |
\tparam _LCapMap The type of the lower bound capacity map. The default |
| 142 | 142 |
map type is \ref concepts::Digraph::ArcMap "_Digraph::ArcMap<int>". |
| 143 | 143 |
\tparam _UCapMap The type of the upper bound capacity map. The default |
| 144 | 144 |
map type is \c _LCapMap. |
| 145 | 145 |
\tparam _DeltaMap The type of the map that stores the lower bound |
| 146 | 146 |
for the supply of the nodes. The default map type is |
| 147 | 147 |
\c _Digraph::ArcMap<_UCapMap::Value>. |
| 148 | 148 |
*/ |
| 149 | 149 |
#ifdef DOXYGEN |
| 150 | 150 |
template< typename _Digraph, |
| 151 | 151 |
typename _LCapMap, |
| 152 | 152 |
typename _UCapMap, |
| 153 | 153 |
typename _DeltaMap, |
| 154 | 154 |
typename _Traits > |
| 155 | 155 |
#else |
| 156 | 156 |
template< typename _Digraph, |
| 157 | 157 |
typename _LCapMap = typename _Digraph::template ArcMap<int>, |
| 158 | 158 |
typename _UCapMap = _LCapMap, |
| 159 | 159 |
typename _DeltaMap = typename _Digraph:: |
| 160 | 160 |
template NodeMap<typename _UCapMap::Value>, |
| 161 | 161 |
typename _Traits=CirculationDefaultTraits<_Digraph, _LCapMap, |
| 162 | 162 |
_UCapMap, _DeltaMap> > |
| 163 | 163 |
#endif |
| 164 | 164 |
class Circulation {
|
| 165 | 165 |
public: |
| 166 | 166 |
|
| 167 | 167 |
///The \ref CirculationDefaultTraits "traits class" of the algorithm. |
| 168 | 168 |
typedef _Traits Traits; |
| 169 | 169 |
///The type of the digraph the algorithm runs on. |
| 170 | 170 |
typedef typename Traits::Digraph Digraph; |
| 171 | 171 |
///The type of the flow values. |
| 172 | 172 |
typedef typename Traits::Value Value; |
| 173 | 173 |
|
| 174 | 174 |
/// The type of the lower bound capacity map. |
| 175 | 175 |
typedef typename Traits::LCapMap LCapMap; |
| 176 | 176 |
/// The type of the upper bound capacity map. |
| 177 | 177 |
typedef typename Traits::UCapMap UCapMap; |
| 178 | 178 |
/// \brief The type of the map that stores the lower bound for |
| 179 | 179 |
/// the supply of the nodes. |
| 180 | 180 |
typedef typename Traits::DeltaMap DeltaMap; |
| 181 | 181 |
///The type of the flow map. |
| 182 | 182 |
typedef typename Traits::FlowMap FlowMap; |
| 183 | 183 |
|
| 184 | 184 |
///The type of the elevator. |
| 185 | 185 |
typedef typename Traits::Elevator Elevator; |
| 186 | 186 |
///The type of the tolerance. |
| 187 | 187 |
typedef typename Traits::Tolerance Tolerance; |
| 188 | 188 |
|
| 189 | 189 |
private: |
| 190 | 190 |
|
| 191 | 191 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 192 | 192 |
|
| 193 | 193 |
const Digraph &_g; |
| 194 | 194 |
int _node_num; |
| 195 | 195 |
|
| 196 | 196 |
const LCapMap *_lo; |
| 197 | 197 |
const UCapMap *_up; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\file |
| 20 | 20 |
///\brief Color constants |
| 21 | 21 |
|
| 22 | 22 |
#include<lemon/color.h> |
| 23 | 23 |
|
| 24 | 24 |
namespace lemon {
|
| 25 | 25 |
|
| 26 | 26 |
const Color WHITE(1,1,1); |
| 27 | 27 |
|
| 28 | 28 |
const Color BLACK(0,0,0); |
| 29 | 29 |
const Color RED(1,0,0); |
| 30 | 30 |
const Color GREEN(0,1,0); |
| 31 | 31 |
const Color BLUE(0,0,1); |
| 32 | 32 |
const Color YELLOW(1,1,0); |
| 33 | 33 |
const Color MAGENTA(1,0,1); |
| 34 | 34 |
const Color CYAN(0,1,1); |
| 35 | 35 |
|
| 36 | 36 |
const Color GREY(0,0,0); |
| 37 | 37 |
const Color DARK_RED(.5,0,0); |
| 38 | 38 |
const Color DARK_GREEN(0,.5,0); |
| 39 | 39 |
const Color DARK_BLUE(0,0,.5); |
| 40 | 40 |
const Color DARK_YELLOW(.5,.5,0); |
| 41 | 41 |
const Color DARK_MAGENTA(.5,0,.5); |
| 42 | 42 |
const Color DARK_CYAN(0,.5,.5); |
| 43 | 43 |
|
| 44 | 44 |
} //namespace lemon |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_COLOR_H |
| 20 | 20 |
#define LEMON_COLOR_H |
| 21 | 21 |
|
| 22 | 22 |
#include<vector> |
| 23 | 23 |
#include<lemon/math.h> |
| 24 | 24 |
#include<lemon/maps.h> |
| 25 | 25 |
|
| 26 | 26 |
|
| 27 | 27 |
///\ingroup misc |
| 28 | 28 |
///\file |
| 29 | 29 |
///\brief Tools to manage RGB colors. |
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
|
| 33 | 33 |
|
| 34 | 34 |
/// \addtogroup misc |
| 35 | 35 |
/// @{
|
| 36 | 36 |
|
| 37 | 37 |
///Data structure representing RGB colors. |
| 38 | 38 |
|
| 39 | 39 |
///Data structure representing RGB colors. |
| 40 | 40 |
class Color |
| 41 | 41 |
{
|
| 42 | 42 |
double _r,_g,_b; |
| 43 | 43 |
public: |
| 44 | 44 |
///Default constructor |
| 45 | 45 |
Color() {}
|
| 46 | 46 |
///Constructor |
| 47 | 47 |
Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
|
| 48 | 48 |
///Set the red component |
| 49 | 49 |
double & red() {return _r;}
|
| 50 | 50 |
///Return the red component |
| 51 | 51 |
const double & red() const {return _r;}
|
| 52 | 52 |
///Set the green component |
| 53 | 53 |
double & green() {return _g;}
|
| 54 | 54 |
///Return the green component |
| 55 | 55 |
const double & green() const {return _g;}
|
| 56 | 56 |
///Set the blue component |
| 57 | 57 |
double & blue() {return _b;}
|
| 58 | 58 |
///Return the blue component |
| 59 | 59 |
const double & blue() const {return _b;}
|
| 60 | 60 |
///Set the color components |
| 61 | 61 |
void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
|
| 62 | 62 |
}; |
| 63 | 63 |
|
| 64 | 64 |
/// White color constant |
| 65 | 65 |
extern const Color WHITE; |
| 66 | 66 |
/// Black color constant |
| 67 | 67 |
extern const Color BLACK; |
| 68 | 68 |
/// Red color constant |
| 69 | 69 |
extern const Color RED; |
| 70 | 70 |
/// Green color constant |
| 71 | 71 |
extern const Color GREEN; |
| 72 | 72 |
/// Blue color constant |
| 73 | 73 |
extern const Color BLUE; |
| 74 | 74 |
/// Yellow color constant |
| 75 | 75 |
extern const Color YELLOW; |
| 76 | 76 |
/// Magenta color constant |
| 77 | 77 |
extern const Color MAGENTA; |
| 78 | 78 |
/// Cyan color constant |
| 79 | 79 |
extern const Color CYAN; |
| 80 | 80 |
/// Grey color constant |
| 81 | 81 |
extern const Color GREY; |
| 82 | 82 |
/// Dark red color constant |
| 83 | 83 |
extern const Color DARK_RED; |
| 84 | 84 |
/// Dark green color constant |
| 85 | 85 |
extern const Color DARK_GREEN; |
| 86 | 86 |
/// Drak blue color constant |
| 87 | 87 |
extern const Color DARK_BLUE; |
| 88 | 88 |
/// Dark yellow color constant |
| 89 | 89 |
extern const Color DARK_YELLOW; |
| 90 | 90 |
/// Dark magenta color constant |
| 91 | 91 |
extern const Color DARK_MAGENTA; |
| 92 | 92 |
/// Dark cyan color constant |
| 93 | 93 |
extern const Color DARK_CYAN; |
| 94 | 94 |
|
| 95 | 95 |
///Map <tt>int</tt>s to different <tt>Color</tt>s |
| 96 | 96 |
|
| 97 | 97 |
///This map assigns one of the predefined \ref Color "Color"s to |
| 98 | 98 |
///each <tt>int</tt>. It is possible to change the colors as well as |
| 99 | 99 |
///their number. The integer range is cyclically mapped to the |
| 100 | 100 |
///provided set of colors. |
| 101 | 101 |
/// |
| 102 | 102 |
///This is a true \ref concepts::ReferenceMap "reference map", so |
| 103 | 103 |
///you can also change the actual colors. |
| 104 | 104 |
|
| 105 | 105 |
class Palette : public MapBase<int,Color> |
| 106 | 106 |
{
|
| 107 | 107 |
std::vector<Color> colors; |
| 108 | 108 |
public: |
| 109 | 109 |
///Constructor |
| 110 | 110 |
|
| 111 | 111 |
///Constructor. |
| 112 | 112 |
///\param have_white Indicates whether white is among the |
| 113 | 113 |
///provided initial colors (\c true) or not (\c false). If it is true, |
| 114 | 114 |
///white will be assigned to \c 0. |
| 115 | 115 |
///\param num The number of the allocated colors. If it is \c -1, |
| 116 | 116 |
///the default color configuration is set up (26 color plus optionaly the |
| 117 | 117 |
///white). If \c num is less then 26/27 then the default color |
| 118 | 118 |
///list is cut. Otherwise the color list is filled repeatedly with |
| 119 | 119 |
///the default color list. (The colors can be changed later on.) |
| 120 | 120 |
Palette(bool have_white=false,int num=-1) |
| 121 | 121 |
{
|
| 122 | 122 |
if (num==0) return; |
| 123 | 123 |
do {
|
| 124 | 124 |
if(have_white) colors.push_back(Color(1,1,1)); |
| 125 | 125 |
|
| 126 | 126 |
colors.push_back(Color(0,0,0)); |
| 127 | 127 |
colors.push_back(Color(1,0,0)); |
| 128 | 128 |
colors.push_back(Color(0,1,0)); |
| 129 | 129 |
colors.push_back(Color(0,0,1)); |
| 130 | 130 |
colors.push_back(Color(1,1,0)); |
| 131 | 131 |
colors.push_back(Color(1,0,1)); |
| 132 | 132 |
colors.push_back(Color(0,1,1)); |
| 133 | 133 |
|
| 134 | 134 |
colors.push_back(Color(.5,0,0)); |
| 135 | 135 |
colors.push_back(Color(0,.5,0)); |
| 136 | 136 |
colors.push_back(Color(0,0,.5)); |
| 137 | 137 |
colors.push_back(Color(.5,.5,0)); |
| 138 | 138 |
colors.push_back(Color(.5,0,.5)); |
| 139 | 139 |
colors.push_back(Color(0,.5,.5)); |
| 140 | 140 |
|
| 141 | 141 |
colors.push_back(Color(.5,.5,.5)); |
| 142 | 142 |
colors.push_back(Color(1,.5,.5)); |
| 143 | 143 |
colors.push_back(Color(.5,1,.5)); |
| 144 | 144 |
colors.push_back(Color(.5,.5,1)); |
| 145 | 145 |
colors.push_back(Color(1,1,.5)); |
| 146 | 146 |
colors.push_back(Color(1,.5,1)); |
| 147 | 147 |
colors.push_back(Color(.5,1,1)); |
| 148 | 148 |
|
| 149 | 149 |
colors.push_back(Color(1,.5,0)); |
| 150 | 150 |
colors.push_back(Color(.5,1,0)); |
| 151 | 151 |
colors.push_back(Color(1,0,.5)); |
| 152 | 152 |
colors.push_back(Color(0,1,.5)); |
| 153 | 153 |
colors.push_back(Color(0,.5,1)); |
| 154 | 154 |
colors.push_back(Color(.5,0,1)); |
| 155 | 155 |
} while(int(colors.size())<num); |
| 156 | 156 |
if(num>=0) colors.resize(num); |
| 157 | 157 |
} |
| 158 | 158 |
///\e |
| 159 | 159 |
Color &operator[](int i) |
| 160 | 160 |
{
|
| 161 | 161 |
return colors[i%colors.size()]; |
| 162 | 162 |
} |
| 163 | 163 |
///\e |
| 164 | 164 |
const Color &operator[](int i) const |
| 165 | 165 |
{
|
| 166 | 166 |
return colors[i%colors.size()]; |
| 167 | 167 |
} |
| 168 | 168 |
///\e |
| 169 | 169 |
void set(int i,const Color &c) |
| 170 | 170 |
{
|
| 171 | 171 |
colors[i%colors.size()]=c; |
| 172 | 172 |
} |
| 173 | 173 |
///Adds a new color to the end of the color list. |
| 174 | 174 |
void add(const Color &c) |
| 175 | 175 |
{
|
| 176 | 176 |
colors.push_back(c); |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
///Sets the number of the existing colors. |
| 180 | 180 |
void resize(int s) { colors.resize(s);}
|
| 181 | 181 |
///Returns the number of the existing colors. |
| 182 | 182 |
int size() const { return int(colors.size());}
|
| 183 | 183 |
}; |
| 184 | 184 |
|
| 185 | 185 |
///Returns a visibly distinct \ref Color |
| 186 | 186 |
|
| 187 | 187 |
///Returns a \ref Color which is as different from the given parameter |
| 188 | 188 |
///as it is possible. |
| 189 | 189 |
inline Color distantColor(const Color &c) |
| 190 | 190 |
{
|
| 191 | 191 |
return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0); |
| 192 | 192 |
} |
| 193 | 193 |
///Returns black for light colors and white for the dark ones. |
| 194 | 194 |
|
| 195 | 195 |
///Returns black for light colors and white for the dark ones. |
| 196 | 196 |
inline Color distantBW(const Color &c){
|
| 197 | 197 |
return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
// The contents of this file was inspired by the concept checking |
| 20 | 20 |
// utility of the BOOST library (http://www.boost.org). |
| 21 | 21 |
|
| 22 | 22 |
///\file |
| 23 | 23 |
///\brief Basic utilities for concept checking. |
| 24 | 24 |
/// |
| 25 | 25 |
|
| 26 | 26 |
#ifndef LEMON_CONCEPT_CHECK_H |
| 27 | 27 |
#define LEMON_CONCEPT_CHECK_H |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
/* |
| 32 | 32 |
"inline" is used for ignore_unused_variable_warning() |
| 33 | 33 |
and function_requires() to make sure there is no |
| 34 | 34 |
overtarget with g++. |
| 35 | 35 |
*/ |
| 36 | 36 |
|
| 37 | 37 |
template <class T> inline void ignore_unused_variable_warning(const T&) { }
|
| 38 | 38 |
|
| 39 | 39 |
///\e |
| 40 | 40 |
template <class Concept> |
| 41 | 41 |
inline void function_requires() |
| 42 | 42 |
{
|
| 43 | 43 |
#if !defined(NDEBUG) |
| 44 | 44 |
void (Concept::*x)() = & Concept::constraints; |
| 45 | 45 |
ignore_unused_variable_warning(x); |
| 46 | 46 |
#endif |
| 47 | 47 |
} |
| 48 | 48 |
|
| 49 | 49 |
///\e |
| 50 | 50 |
template <typename Concept, typename Type> |
| 51 | 51 |
inline void checkConcept() {
|
| 52 | 52 |
#if !defined(NDEBUG) |
| 53 | 53 |
typedef typename Concept::template Constraints<Type> ConceptCheck; |
| 54 | 54 |
void (ConceptCheck::*x)() = & ConceptCheck::constraints; |
| 55 | 55 |
ignore_unused_variable_warning(x); |
| 56 | 56 |
#endif |
| 57 | 57 |
} |
| 58 | 58 |
|
| 59 | 59 |
} // namespace lemon |
| 60 | 60 |
|
| 61 | 61 |
#endif // LEMON_CONCEPT_CHECK_H |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CONCEPT_DIGRAPH_H |
| 20 | 20 |
#define LEMON_CONCEPT_DIGRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graph_concepts |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief The concept of directed graphs. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
#include <lemon/concept_check.h> |
| 29 | 29 |
#include <lemon/concepts/graph_components.h> |
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
namespace concepts {
|
| 33 | 33 |
|
| 34 | 34 |
/// \ingroup graph_concepts |
| 35 | 35 |
/// |
| 36 | 36 |
/// \brief Class describing the concept of directed graphs. |
| 37 | 37 |
/// |
| 38 | 38 |
/// This class describes the \ref concept "concept" of the |
| 39 | 39 |
/// immutable directed digraphs. |
| 40 | 40 |
/// |
| 41 | 41 |
/// Note that actual digraph implementation like @ref ListDigraph or |
| 42 | 42 |
/// @ref SmartDigraph may have several additional functionality. |
| 43 | 43 |
/// |
| 44 | 44 |
/// \sa concept |
| 45 | 45 |
class Digraph {
|
| 46 | 46 |
private: |
| 47 | 47 |
///Digraphs are \e not copy constructible. Use DigraphCopy() instead. |
| 48 | 48 |
|
| 49 | 49 |
///Digraphs are \e not copy constructible. Use DigraphCopy() instead. |
| 50 | 50 |
/// |
| 51 | 51 |
Digraph(const Digraph &) {};
|
| 52 | 52 |
///\brief Assignment of \ref Digraph "Digraph"s to another ones are |
| 53 | 53 |
///\e not allowed. Use DigraphCopy() instead. |
| 54 | 54 |
|
| 55 | 55 |
///Assignment of \ref Digraph "Digraph"s to another ones are |
| 56 | 56 |
///\e not allowed. Use DigraphCopy() instead. |
| 57 | 57 |
|
| 58 | 58 |
void operator=(const Digraph &) {}
|
| 59 | 59 |
public: |
| 60 | 60 |
///\e |
| 61 | 61 |
|
| 62 | 62 |
/// Defalult constructor. |
| 63 | 63 |
|
| 64 | 64 |
/// Defalult constructor. |
| 65 | 65 |
/// |
| 66 | 66 |
Digraph() { }
|
| 67 | 67 |
/// Class for identifying a node of the digraph |
| 68 | 68 |
|
| 69 | 69 |
/// This class identifies a node of the digraph. It also serves |
| 70 | 70 |
/// as a base class of the node iterators, |
| 71 | 71 |
/// thus they will convert to this type. |
| 72 | 72 |
class Node {
|
| 73 | 73 |
public: |
| 74 | 74 |
/// Default constructor |
| 75 | 75 |
|
| 76 | 76 |
/// @warning The default constructor sets the iterator |
| 77 | 77 |
/// to an undefined value. |
| 78 | 78 |
Node() { }
|
| 79 | 79 |
/// Copy constructor. |
| 80 | 80 |
|
| 81 | 81 |
/// Copy constructor. |
| 82 | 82 |
/// |
| 83 | 83 |
Node(const Node&) { }
|
| 84 | 84 |
|
| 85 | 85 |
/// Invalid constructor \& conversion. |
| 86 | 86 |
|
| 87 | 87 |
/// This constructor initializes the iterator to be invalid. |
| 88 | 88 |
/// \sa Invalid for more details. |
| 89 | 89 |
Node(Invalid) { }
|
| 90 | 90 |
/// Equality operator |
| 91 | 91 |
|
| 92 | 92 |
/// Two iterators are equal if and only if they point to the |
| 93 | 93 |
/// same object or both are invalid. |
| 94 | 94 |
bool operator==(Node) const { return true; }
|
| 95 | 95 |
|
| 96 | 96 |
/// Inequality operator |
| 97 | 97 |
|
| 98 | 98 |
/// \sa operator==(Node n) |
| 99 | 99 |
/// |
| 100 | 100 |
bool operator!=(Node) const { return true; }
|
| 101 | 101 |
|
| 102 | 102 |
/// Artificial ordering operator. |
| 103 | 103 |
|
| 104 | 104 |
/// To allow the use of digraph descriptors as key type in std::map or |
| 105 | 105 |
/// similar associative container we require this. |
| 106 | 106 |
/// |
| 107 | 107 |
/// \note This operator only have to define some strict ordering of |
| 108 | 108 |
/// the items; this order has nothing to do with the iteration |
| 109 | 109 |
/// ordering of the items. |
| 110 | 110 |
bool operator<(Node) const { return false; }
|
| 111 | 111 |
|
| 112 | 112 |
}; |
| 113 | 113 |
|
| 114 | 114 |
/// This iterator goes through each node. |
| 115 | 115 |
|
| 116 | 116 |
/// This iterator goes through each node. |
| 117 | 117 |
/// Its usage is quite simple, for example you can count the number |
| 118 | 118 |
/// of nodes in digraph \c g of type \c Digraph like this: |
| 119 | 119 |
///\code |
| 120 | 120 |
/// int count=0; |
| 121 | 121 |
/// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count; |
| 122 | 122 |
///\endcode |
| 123 | 123 |
class NodeIt : public Node {
|
| 124 | 124 |
public: |
| 125 | 125 |
/// Default constructor |
| 126 | 126 |
|
| 127 | 127 |
/// @warning The default constructor sets the iterator |
| 128 | 128 |
/// to an undefined value. |
| 129 | 129 |
NodeIt() { }
|
| 130 | 130 |
/// Copy constructor. |
| 131 | 131 |
|
| 132 | 132 |
/// Copy constructor. |
| 133 | 133 |
/// |
| 134 | 134 |
NodeIt(const NodeIt& n) : Node(n) { }
|
| 135 | 135 |
/// Invalid constructor \& conversion. |
| 136 | 136 |
|
| 137 | 137 |
/// Initialize the iterator to be invalid. |
| 138 | 138 |
/// \sa Invalid for more details. |
| 139 | 139 |
NodeIt(Invalid) { }
|
| 140 | 140 |
/// Sets the iterator to the first node. |
| 141 | 141 |
|
| 142 | 142 |
/// Sets the iterator to the first node of \c g. |
| 143 | 143 |
/// |
| 144 | 144 |
NodeIt(const Digraph&) { }
|
| 145 | 145 |
/// Node -> NodeIt conversion. |
| 146 | 146 |
|
| 147 | 147 |
/// Sets the iterator to the node of \c the digraph pointed by |
| 148 | 148 |
/// the trivial iterator. |
| 149 | 149 |
/// This feature necessitates that each time we |
| 150 | 150 |
/// iterate the arc-set, the iteration order is the same. |
| 151 | 151 |
NodeIt(const Digraph&, const Node&) { }
|
| 152 | 152 |
/// Next node. |
| 153 | 153 |
|
| 154 | 154 |
/// Assign the iterator to the next node. |
| 155 | 155 |
/// |
| 156 | 156 |
NodeIt& operator++() { return *this; }
|
| 157 | 157 |
}; |
| 158 | 158 |
|
| 159 | 159 |
|
| 160 | 160 |
/// Class for identifying an arc of the digraph |
| 161 | 161 |
|
| 162 | 162 |
/// This class identifies an arc of the digraph. It also serves |
| 163 | 163 |
/// as a base class of the arc iterators, |
| 164 | 164 |
/// thus they will convert to this type. |
| 165 | 165 |
class Arc {
|
| 166 | 166 |
public: |
| 167 | 167 |
/// Default constructor |
| 168 | 168 |
|
| 169 | 169 |
/// @warning The default constructor sets the iterator |
| 170 | 170 |
/// to an undefined value. |
| 171 | 171 |
Arc() { }
|
| 172 | 172 |
/// Copy constructor. |
| 173 | 173 |
|
| 174 | 174 |
/// Copy constructor. |
| 175 | 175 |
/// |
| 176 | 176 |
Arc(const Arc&) { }
|
| 177 | 177 |
/// Initialize the iterator to be invalid. |
| 178 | 178 |
|
| 179 | 179 |
/// Initialize the iterator to be invalid. |
| 180 | 180 |
/// |
| 181 | 181 |
Arc(Invalid) { }
|
| 182 | 182 |
/// Equality operator |
| 183 | 183 |
|
| 184 | 184 |
/// Two iterators are equal if and only if they point to the |
| 185 | 185 |
/// same object or both are invalid. |
| 186 | 186 |
bool operator==(Arc) const { return true; }
|
| 187 | 187 |
/// Inequality operator |
| 188 | 188 |
|
| 189 | 189 |
/// \sa operator==(Arc n) |
| 190 | 190 |
/// |
| 191 | 191 |
bool operator!=(Arc) const { return true; }
|
| 192 | 192 |
|
| 193 | 193 |
/// Artificial ordering operator. |
| 194 | 194 |
|
| 195 | 195 |
/// To allow the use of digraph descriptors as key type in std::map or |
| 196 | 196 |
/// similar associative container we require this. |
| 197 | 197 |
/// |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup graph_concepts |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief The concept of Undirected Graphs. |
| 22 | 22 |
|
| 23 | 23 |
#ifndef LEMON_CONCEPT_GRAPH_H |
| 24 | 24 |
#define LEMON_CONCEPT_GRAPH_H |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/concepts/graph_components.h> |
| 27 | 27 |
#include <lemon/concepts/graph.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
|
| 30 | 30 |
namespace lemon {
|
| 31 | 31 |
namespace concepts {
|
| 32 | 32 |
|
| 33 | 33 |
/// \ingroup graph_concepts |
| 34 | 34 |
/// |
| 35 | 35 |
/// \brief Class describing the concept of Undirected Graphs. |
| 36 | 36 |
/// |
| 37 | 37 |
/// This class describes the common interface of all Undirected |
| 38 | 38 |
/// Graphs. |
| 39 | 39 |
/// |
| 40 | 40 |
/// As all concept describing classes it provides only interface |
| 41 | 41 |
/// without any sensible implementation. So any algorithm for |
| 42 | 42 |
/// undirected graph should compile with this class, but it will not |
| 43 | 43 |
/// run properly, of course. |
| 44 | 44 |
/// |
| 45 | 45 |
/// The LEMON undirected graphs also fulfill the concept of |
| 46 | 46 |
/// directed graphs (\ref lemon::concepts::Digraph "Digraph |
| 47 | 47 |
/// Concept"). Each edges can be seen as two opposite |
| 48 | 48 |
/// directed arc and consequently the undirected graph can be |
| 49 | 49 |
/// seen as the direceted graph of these directed arcs. The |
| 50 | 50 |
/// Graph has the Edge inner class for the edges and |
| 51 | 51 |
/// the Arc type for the directed arcs. The Arc type is |
| 52 | 52 |
/// convertible to Edge or inherited from it so from a directed |
| 53 | 53 |
/// arc we can get the represented edge. |
| 54 | 54 |
/// |
| 55 | 55 |
/// In the sense of the LEMON each edge has a default |
| 56 | 56 |
/// direction (it should be in every computer implementation, |
| 57 | 57 |
/// because the order of edge's nodes defines an |
| 58 | 58 |
/// orientation). With the default orientation we can define that |
| 59 | 59 |
/// the directed arc is forward or backward directed. With the \c |
| 60 | 60 |
/// direction() and \c direct() function we can get the direction |
| 61 | 61 |
/// of the directed arc and we can direct an edge. |
| 62 | 62 |
/// |
| 63 | 63 |
/// The EdgeIt is an iterator for the edges. We can use |
| 64 | 64 |
/// the EdgeMap to map values for the edges. The InArcIt and |
| 65 | 65 |
/// OutArcIt iterates on the same edges but with opposite |
| 66 | 66 |
/// direction. The IncEdgeIt iterates also on the same edges |
| 67 | 67 |
/// as the OutArcIt and InArcIt but it is not convertible to Arc just |
| 68 | 68 |
/// to Edge. |
| 69 | 69 |
class Graph {
|
| 70 | 70 |
public: |
| 71 | 71 |
/// \brief The undirected graph should be tagged by the |
| 72 | 72 |
/// UndirectedTag. |
| 73 | 73 |
/// |
| 74 | 74 |
/// The undirected graph should be tagged by the UndirectedTag. This |
| 75 | 75 |
/// tag helps the enable_if technics to make compile time |
| 76 | 76 |
/// specializations for undirected graphs. |
| 77 | 77 |
typedef True UndirectedTag; |
| 78 | 78 |
|
| 79 | 79 |
/// \brief The base type of node iterators, |
| 80 | 80 |
/// or in other words, the trivial node iterator. |
| 81 | 81 |
/// |
| 82 | 82 |
/// This is the base type of each node iterator, |
| 83 | 83 |
/// thus each kind of node iterator converts to this. |
| 84 | 84 |
/// More precisely each kind of node iterator should be inherited |
| 85 | 85 |
/// from the trivial node iterator. |
| 86 | 86 |
class Node {
|
| 87 | 87 |
public: |
| 88 | 88 |
/// Default constructor |
| 89 | 89 |
|
| 90 | 90 |
/// @warning The default constructor sets the iterator |
| 91 | 91 |
/// to an undefined value. |
| 92 | 92 |
Node() { }
|
| 93 | 93 |
/// Copy constructor. |
| 94 | 94 |
|
| 95 | 95 |
/// Copy constructor. |
| 96 | 96 |
/// |
| 97 | 97 |
Node(const Node&) { }
|
| 98 | 98 |
|
| 99 | 99 |
/// Invalid constructor \& conversion. |
| 100 | 100 |
|
| 101 | 101 |
/// This constructor initializes the iterator to be invalid. |
| 102 | 102 |
/// \sa Invalid for more details. |
| 103 | 103 |
Node(Invalid) { }
|
| 104 | 104 |
/// Equality operator |
| 105 | 105 |
|
| 106 | 106 |
/// Two iterators are equal if and only if they point to the |
| 107 | 107 |
/// same object or both are invalid. |
| 108 | 108 |
bool operator==(Node) const { return true; }
|
| 109 | 109 |
|
| 110 | 110 |
/// Inequality operator |
| 111 | 111 |
|
| 112 | 112 |
/// \sa operator==(Node n) |
| 113 | 113 |
/// |
| 114 | 114 |
bool operator!=(Node) const { return true; }
|
| 115 | 115 |
|
| 116 | 116 |
/// Artificial ordering operator. |
| 117 | 117 |
|
| 118 | 118 |
/// To allow the use of graph descriptors as key type in std::map or |
| 119 | 119 |
/// similar associative container we require this. |
| 120 | 120 |
/// |
| 121 | 121 |
/// \note This operator only have to define some strict ordering of |
| 122 | 122 |
/// the items; this order has nothing to do with the iteration |
| 123 | 123 |
/// ordering of the items. |
| 124 | 124 |
bool operator<(Node) const { return false; }
|
| 125 | 125 |
|
| 126 | 126 |
}; |
| 127 | 127 |
|
| 128 | 128 |
/// This iterator goes through each node. |
| 129 | 129 |
|
| 130 | 130 |
/// This iterator goes through each node. |
| 131 | 131 |
/// Its usage is quite simple, for example you can count the number |
| 132 | 132 |
/// of nodes in graph \c g of type \c Graph like this: |
| 133 | 133 |
///\code |
| 134 | 134 |
/// int count=0; |
| 135 | 135 |
/// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count; |
| 136 | 136 |
///\endcode |
| 137 | 137 |
class NodeIt : public Node {
|
| 138 | 138 |
public: |
| 139 | 139 |
/// Default constructor |
| 140 | 140 |
|
| 141 | 141 |
/// @warning The default constructor sets the iterator |
| 142 | 142 |
/// to an undefined value. |
| 143 | 143 |
NodeIt() { }
|
| 144 | 144 |
/// Copy constructor. |
| 145 | 145 |
|
| 146 | 146 |
/// Copy constructor. |
| 147 | 147 |
/// |
| 148 | 148 |
NodeIt(const NodeIt& n) : Node(n) { }
|
| 149 | 149 |
/// Invalid constructor \& conversion. |
| 150 | 150 |
|
| 151 | 151 |
/// Initialize the iterator to be invalid. |
| 152 | 152 |
/// \sa Invalid for more details. |
| 153 | 153 |
NodeIt(Invalid) { }
|
| 154 | 154 |
/// Sets the iterator to the first node. |
| 155 | 155 |
|
| 156 | 156 |
/// Sets the iterator to the first node of \c g. |
| 157 | 157 |
/// |
| 158 | 158 |
NodeIt(const Graph&) { }
|
| 159 | 159 |
/// Node -> NodeIt conversion. |
| 160 | 160 |
|
| 161 | 161 |
/// Sets the iterator to the node of \c the graph pointed by |
| 162 | 162 |
/// the trivial iterator. |
| 163 | 163 |
/// This feature necessitates that each time we |
| 164 | 164 |
/// iterate the arc-set, the iteration order is the same. |
| 165 | 165 |
NodeIt(const Graph&, const Node&) { }
|
| 166 | 166 |
/// Next node. |
| 167 | 167 |
|
| 168 | 168 |
/// Assign the iterator to the next node. |
| 169 | 169 |
/// |
| 170 | 170 |
NodeIt& operator++() { return *this; }
|
| 171 | 171 |
}; |
| 172 | 172 |
|
| 173 | 173 |
|
| 174 | 174 |
/// The base type of the edge iterators. |
| 175 | 175 |
|
| 176 | 176 |
/// The base type of the edge iterators. |
| 177 | 177 |
/// |
| 178 | 178 |
class Edge {
|
| 179 | 179 |
public: |
| 180 | 180 |
/// Default constructor |
| 181 | 181 |
|
| 182 | 182 |
/// @warning The default constructor sets the iterator |
| 183 | 183 |
/// to an undefined value. |
| 184 | 184 |
Edge() { }
|
| 185 | 185 |
/// Copy constructor. |
| 186 | 186 |
|
| 187 | 187 |
/// Copy constructor. |
| 188 | 188 |
/// |
| 189 | 189 |
Edge(const Edge&) { }
|
| 190 | 190 |
/// Initialize the iterator to be invalid. |
| 191 | 191 |
|
| 192 | 192 |
/// Initialize the iterator to be invalid. |
| 193 | 193 |
/// |
| 194 | 194 |
Edge(Invalid) { }
|
| 195 | 195 |
/// Equality operator |
| 196 | 196 |
|
| 197 | 197 |
/// Two iterators are equal if and only if they point to the |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup graph_concepts |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief The concept of graph components. |
| 22 | 22 |
|
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_CONCEPT_GRAPH_COMPONENTS_H |
| 25 | 25 |
#define LEMON_CONCEPT_GRAPH_COMPONENTS_H |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/core.h> |
| 28 | 28 |
#include <lemon/concepts/maps.h> |
| 29 | 29 |
|
| 30 | 30 |
#include <lemon/bits/alteration_notifier.h> |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
namespace concepts {
|
| 34 | 34 |
|
| 35 | 35 |
/// \brief Skeleton class for graph Node and Arc types |
| 36 | 36 |
/// |
| 37 | 37 |
/// This class describes the interface of Node and Arc (and Edge |
| 38 | 38 |
/// in undirected graphs) subtypes of graph types. |
| 39 | 39 |
/// |
| 40 | 40 |
/// \note This class is a template class so that we can use it to |
| 41 | 41 |
/// create graph skeleton classes. The reason for this is than Node |
| 42 | 42 |
/// and Arc types should \em not derive from the same base class. |
| 43 | 43 |
/// For Node you should instantiate it with character 'n' and for Arc |
| 44 | 44 |
/// with 'a'. |
| 45 | 45 |
|
| 46 | 46 |
#ifndef DOXYGEN |
| 47 | 47 |
template <char _selector = '0'> |
| 48 | 48 |
#endif |
| 49 | 49 |
class GraphItem {
|
| 50 | 50 |
public: |
| 51 | 51 |
/// \brief Default constructor. |
| 52 | 52 |
/// |
| 53 | 53 |
/// \warning The default constructor is not required to set |
| 54 | 54 |
/// the item to some well-defined value. So you should consider it |
| 55 | 55 |
/// as uninitialized. |
| 56 | 56 |
GraphItem() {}
|
| 57 | 57 |
/// \brief Copy constructor. |
| 58 | 58 |
/// |
| 59 | 59 |
/// Copy constructor. |
| 60 | 60 |
/// |
| 61 | 61 |
GraphItem(const GraphItem &) {}
|
| 62 | 62 |
/// \brief Invalid constructor \& conversion. |
| 63 | 63 |
/// |
| 64 | 64 |
/// This constructor initializes the item to be invalid. |
| 65 | 65 |
/// \sa Invalid for more details. |
| 66 | 66 |
GraphItem(Invalid) {}
|
| 67 | 67 |
/// \brief Assign operator for nodes. |
| 68 | 68 |
/// |
| 69 | 69 |
/// The nodes are assignable. |
| 70 | 70 |
/// |
| 71 | 71 |
GraphItem& operator=(GraphItem const&) { return *this; }
|
| 72 | 72 |
/// \brief Equality operator. |
| 73 | 73 |
/// |
| 74 | 74 |
/// Two iterators are equal if and only if they represents the |
| 75 | 75 |
/// same node in the graph or both are invalid. |
| 76 | 76 |
bool operator==(GraphItem) const { return false; }
|
| 77 | 77 |
/// \brief Inequality operator. |
| 78 | 78 |
/// |
| 79 | 79 |
/// \sa operator==(const Node& n) |
| 80 | 80 |
/// |
| 81 | 81 |
bool operator!=(GraphItem) const { return false; }
|
| 82 | 82 |
|
| 83 | 83 |
/// \brief Artificial ordering operator. |
| 84 | 84 |
/// |
| 85 | 85 |
/// To allow the use of graph descriptors as key type in std::map or |
| 86 | 86 |
/// similar associative container we require this. |
| 87 | 87 |
/// |
| 88 | 88 |
/// \note This operator only have to define some strict ordering of |
| 89 | 89 |
/// the items; this order has nothing to do with the iteration |
| 90 | 90 |
/// ordering of the items. |
| 91 | 91 |
bool operator<(GraphItem) const { return false; }
|
| 92 | 92 |
|
| 93 | 93 |
template<typename _GraphItem> |
| 94 | 94 |
struct Constraints {
|
| 95 | 95 |
void constraints() {
|
| 96 | 96 |
_GraphItem i1; |
| 97 | 97 |
_GraphItem i2 = i1; |
| 98 | 98 |
_GraphItem i3 = INVALID; |
| 99 | 99 |
|
| 100 | 100 |
i1 = i2 = i3; |
| 101 | 101 |
|
| 102 | 102 |
bool b; |
| 103 | 103 |
// b = (ia == ib) && (ia != ib) && (ia < ib); |
| 104 | 104 |
b = (ia == ib) && (ia != ib); |
| 105 | 105 |
b = (ia == INVALID) && (ib != INVALID); |
| 106 | 106 |
b = (ia < ib); |
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 | 109 |
const _GraphItem &ia; |
| 110 | 110 |
const _GraphItem &ib; |
| 111 | 111 |
}; |
| 112 | 112 |
}; |
| 113 | 113 |
|
| 114 | 114 |
/// \brief An empty base directed graph class. |
| 115 | 115 |
/// |
| 116 | 116 |
/// This class provides the minimal set of features needed for a |
| 117 | 117 |
/// directed graph structure. All digraph concepts have to be |
| 118 | 118 |
/// conform to this base directed graph. It just provides types |
| 119 | 119 |
/// for nodes and arcs and functions to get the source and the |
| 120 | 120 |
/// target of the arcs. |
| 121 | 121 |
class BaseDigraphComponent {
|
| 122 | 122 |
public: |
| 123 | 123 |
|
| 124 | 124 |
typedef BaseDigraphComponent Digraph; |
| 125 | 125 |
|
| 126 | 126 |
/// \brief Node class of the digraph. |
| 127 | 127 |
/// |
| 128 | 128 |
/// This class represents the Nodes of the digraph. |
| 129 | 129 |
/// |
| 130 | 130 |
typedef GraphItem<'n'> Node; |
| 131 | 131 |
|
| 132 | 132 |
/// \brief Arc class of the digraph. |
| 133 | 133 |
/// |
| 134 | 134 |
/// This class represents the Arcs of the digraph. |
| 135 | 135 |
/// |
| 136 | 136 |
typedef GraphItem<'e'> Arc; |
| 137 | 137 |
|
| 138 | 138 |
/// \brief Gives back the target node of an arc. |
| 139 | 139 |
/// |
| 140 | 140 |
/// Gives back the target node of an arc. |
| 141 | 141 |
/// |
| 142 | 142 |
Node target(const Arc&) const { return INVALID;}
|
| 143 | 143 |
|
| 144 | 144 |
/// \brief Gives back the source node of an arc. |
| 145 | 145 |
/// |
| 146 | 146 |
/// Gives back the source node of an arc. |
| 147 | 147 |
/// |
| 148 | 148 |
Node source(const Arc&) const { return INVALID;}
|
| 149 | 149 |
|
| 150 | 150 |
/// \brief Gives back the opposite node on the given arc. |
| 151 | 151 |
/// |
| 152 | 152 |
/// Gives back the opposite node on the given arc. |
| 153 | 153 |
Node oppositeNode(const Node&, const Arc&) const {
|
| 154 | 154 |
return INVALID; |
| 155 | 155 |
} |
| 156 | 156 |
|
| 157 | 157 |
template <typename _Digraph> |
| 158 | 158 |
struct Constraints {
|
| 159 | 159 |
typedef typename _Digraph::Node Node; |
| 160 | 160 |
typedef typename _Digraph::Arc Arc; |
| 161 | 161 |
|
| 162 | 162 |
void constraints() {
|
| 163 | 163 |
checkConcept<GraphItem<'n'>, Node>(); |
| 164 | 164 |
checkConcept<GraphItem<'a'>, Arc>(); |
| 165 | 165 |
{
|
| 166 | 166 |
Node n; |
| 167 | 167 |
Arc e(INVALID); |
| 168 | 168 |
n = digraph.source(e); |
| 169 | 169 |
n = digraph.target(e); |
| 170 | 170 |
n = digraph.oppositeNode(n, e); |
| 171 | 171 |
} |
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
const _Digraph& digraph; |
| 175 | 175 |
}; |
| 176 | 176 |
}; |
| 177 | 177 |
|
| 178 | 178 |
/// \brief An empty base undirected graph class. |
| 179 | 179 |
/// |
| 180 | 180 |
/// This class provides the minimal set of features needed for an |
| 181 | 181 |
/// undirected graph structure. All undirected graph concepts have |
| 182 | 182 |
/// to be conform to this base graph. It just provides types for |
| 183 | 183 |
/// nodes, arcs and edges and functions to get the |
| 184 | 184 |
/// source and the target of the arcs and edges, |
| 185 | 185 |
/// conversion from arcs to edges and function to get |
| 186 | 186 |
/// both direction of the edges. |
| 187 | 187 |
class BaseGraphComponent : public BaseDigraphComponent {
|
| 188 | 188 |
public: |
| 189 | 189 |
typedef BaseDigraphComponent::Node Node; |
| 190 | 190 |
typedef BaseDigraphComponent::Arc Arc; |
| 191 | 191 |
/// \brief Undirected arc class of the graph. |
| 192 | 192 |
/// |
| 193 | 193 |
/// This class represents the edges of the graph. |
| 194 | 194 |
/// The undirected graphs can be used as a directed graph which |
| 195 | 195 |
/// for each arc contains the opposite arc too so the graph is |
| 196 | 196 |
/// bidirected. The edge represents two opposite |
| 197 | 197 |
/// directed arcs. |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup concept |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief The concept of heaps. |
| 22 | 22 |
|
| 23 | 23 |
#ifndef LEMON_CONCEPT_HEAP_H |
| 24 | 24 |
#define LEMON_CONCEPT_HEAP_H |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
|
| 28 | 28 |
namespace lemon {
|
| 29 | 29 |
|
| 30 | 30 |
namespace concepts {
|
| 31 | 31 |
|
| 32 | 32 |
/// \addtogroup concept |
| 33 | 33 |
/// @{
|
| 34 | 34 |
|
| 35 | 35 |
/// \brief The heap concept. |
| 36 | 36 |
/// |
| 37 | 37 |
/// Concept class describing the main interface of heaps. |
| 38 | 38 |
template <typename Priority, typename ItemIntMap> |
| 39 | 39 |
class Heap {
|
| 40 | 40 |
public: |
| 41 | 41 |
|
| 42 | 42 |
/// Type of the items stored in the heap. |
| 43 | 43 |
typedef typename ItemIntMap::Key Item; |
| 44 | 44 |
|
| 45 | 45 |
/// Type of the priorities. |
| 46 | 46 |
typedef Priority Prio; |
| 47 | 47 |
|
| 48 | 48 |
/// \brief Type to represent the states of the items. |
| 49 | 49 |
/// |
| 50 | 50 |
/// Each item has a state associated to it. It can be "in heap", |
| 51 | 51 |
/// "pre heap" or "post heap". The later two are indifferent |
| 52 | 52 |
/// from the point of view of the heap, but may be useful for |
| 53 | 53 |
/// the user. |
| 54 | 54 |
/// |
| 55 | 55 |
/// The \c ItemIntMap must be initialized in such a way, that it |
| 56 | 56 |
/// assigns \c PRE_HEAP (<tt>-1</tt>) to every item. |
| 57 | 57 |
enum State {
|
| 58 | 58 |
IN_HEAP = 0, |
| 59 | 59 |
PRE_HEAP = -1, |
| 60 | 60 |
POST_HEAP = -2 |
| 61 | 61 |
}; |
| 62 | 62 |
|
| 63 | 63 |
/// \brief The constructor. |
| 64 | 64 |
/// |
| 65 | 65 |
/// The constructor. |
| 66 | 66 |
/// \param map A map that assigns \c int values to keys of type |
| 67 | 67 |
/// \c Item. It is used internally by the heap implementations to |
| 68 | 68 |
/// handle the cross references. The assigned value must be |
| 69 | 69 |
/// \c PRE_HEAP (<tt>-1</tt>) for every item. |
| 70 | 70 |
explicit Heap(ItemIntMap &map) {}
|
| 71 | 71 |
|
| 72 | 72 |
/// \brief The number of items stored in the heap. |
| 73 | 73 |
/// |
| 74 | 74 |
/// Returns the number of items stored in the heap. |
| 75 | 75 |
int size() const { return 0; }
|
| 76 | 76 |
|
| 77 | 77 |
/// \brief Checks if the heap is empty. |
| 78 | 78 |
/// |
| 79 | 79 |
/// Returns \c true if the heap is empty. |
| 80 | 80 |
bool empty() const { return false; }
|
| 81 | 81 |
|
| 82 | 82 |
/// \brief Makes the heap empty. |
| 83 | 83 |
/// |
| 84 | 84 |
/// Makes the heap empty. |
| 85 | 85 |
void clear(); |
| 86 | 86 |
|
| 87 | 87 |
/// \brief Inserts an item into the heap with the given priority. |
| 88 | 88 |
/// |
| 89 | 89 |
/// Inserts the given item into the heap with the given priority. |
| 90 | 90 |
/// \param i The item to insert. |
| 91 | 91 |
/// \param p The priority of the item. |
| 92 | 92 |
void push(const Item &i, const Prio &p) {}
|
| 93 | 93 |
|
| 94 | 94 |
/// \brief Returns the item having minimum priority. |
| 95 | 95 |
/// |
| 96 | 96 |
/// Returns the item having minimum priority. |
| 97 | 97 |
/// \pre The heap must be non-empty. |
| 98 | 98 |
Item top() const {}
|
| 99 | 99 |
|
| 100 | 100 |
/// \brief The minimum priority. |
| 101 | 101 |
/// |
| 102 | 102 |
/// Returns the minimum priority. |
| 103 | 103 |
/// \pre The heap must be non-empty. |
| 104 | 104 |
Prio prio() const {}
|
| 105 | 105 |
|
| 106 | 106 |
/// \brief Removes the item having minimum priority. |
| 107 | 107 |
/// |
| 108 | 108 |
/// Removes the item having minimum priority. |
| 109 | 109 |
/// \pre The heap must be non-empty. |
| 110 | 110 |
void pop() {}
|
| 111 | 111 |
|
| 112 | 112 |
/// \brief Removes an item from the heap. |
| 113 | 113 |
/// |
| 114 | 114 |
/// Removes the given item from the heap if it is already stored. |
| 115 | 115 |
/// \param i The item to delete. |
| 116 | 116 |
void erase(const Item &i) {}
|
| 117 | 117 |
|
| 118 | 118 |
/// \brief The priority of an item. |
| 119 | 119 |
/// |
| 120 | 120 |
/// Returns the priority of the given item. |
| 121 | 121 |
/// \pre \c i must be in the heap. |
| 122 | 122 |
/// \param i The item. |
| 123 | 123 |
Prio operator[](const Item &i) const {}
|
| 124 | 124 |
|
| 125 | 125 |
/// \brief Sets the priority of an item or inserts it, if it is |
| 126 | 126 |
/// not stored in the heap. |
| 127 | 127 |
/// |
| 128 | 128 |
/// This method sets the priority of the given item if it is |
| 129 | 129 |
/// already stored in the heap. |
| 130 | 130 |
/// Otherwise it inserts the given item with the given priority. |
| 131 | 131 |
/// |
| 132 | 132 |
/// \param i The item. |
| 133 | 133 |
/// \param p The priority. |
| 134 | 134 |
void set(const Item &i, const Prio &p) {}
|
| 135 | 135 |
|
| 136 | 136 |
/// \brief Decreases the priority of an item to the given value. |
| 137 | 137 |
/// |
| 138 | 138 |
/// Decreases the priority of an item to the given value. |
| 139 | 139 |
/// \pre \c i must be stored in the heap with priority at least \c p. |
| 140 | 140 |
/// \param i The item. |
| 141 | 141 |
/// \param p The priority. |
| 142 | 142 |
void decrease(const Item &i, const Prio &p) {}
|
| 143 | 143 |
|
| 144 | 144 |
/// \brief Increases the priority of an item to the given value. |
| 145 | 145 |
/// |
| 146 | 146 |
/// Increases the priority of an item to the given value. |
| 147 | 147 |
/// \pre \c i must be stored in the heap with priority at most \c p. |
| 148 | 148 |
/// \param i The item. |
| 149 | 149 |
/// \param p The priority. |
| 150 | 150 |
void increase(const Item &i, const Prio &p) {}
|
| 151 | 151 |
|
| 152 | 152 |
/// \brief Returns if an item is in, has already been in, or has |
| 153 | 153 |
/// never been in the heap. |
| 154 | 154 |
/// |
| 155 | 155 |
/// This method returns \c PRE_HEAP if the given item has never |
| 156 | 156 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
| 157 | 157 |
/// and \c POST_HEAP otherwise. |
| 158 | 158 |
/// In the latter case it is possible that the item will get back |
| 159 | 159 |
/// to the heap again. |
| 160 | 160 |
/// \param i The item. |
| 161 | 161 |
State state(const Item &i) const {}
|
| 162 | 162 |
|
| 163 | 163 |
/// \brief Sets the state of an item in the heap. |
| 164 | 164 |
/// |
| 165 | 165 |
/// Sets the state of the given item in the heap. It can be used |
| 166 | 166 |
/// to manually clear the heap when it is important to achive the |
| 167 | 167 |
/// better time complexity. |
| 168 | 168 |
/// \param i The item. |
| 169 | 169 |
/// \param st The state. It should not be \c IN_HEAP. |
| 170 | 170 |
void state(const Item& i, State st) {}
|
| 171 | 171 |
|
| 172 | 172 |
|
| 173 | 173 |
template <typename _Heap> |
| 174 | 174 |
struct Constraints {
|
| 175 | 175 |
public: |
| 176 | 176 |
void constraints() {
|
| 177 | 177 |
typedef typename _Heap::Item OwnItem; |
| 178 | 178 |
typedef typename _Heap::Prio OwnPrio; |
| 179 | 179 |
typedef typename _Heap::State OwnState; |
| 180 | 180 |
|
| 181 | 181 |
Item item; |
| 182 | 182 |
Prio prio; |
| 183 | 183 |
item=Item(); |
| 184 | 184 |
prio=Prio(); |
| 185 | 185 |
ignore_unused_variable_warning(item); |
| 186 | 186 |
ignore_unused_variable_warning(prio); |
| 187 | 187 |
|
| 188 | 188 |
OwnItem own_item; |
| 189 | 189 |
OwnPrio own_prio; |
| 190 | 190 |
OwnState own_state; |
| 191 | 191 |
own_item=Item(); |
| 192 | 192 |
own_prio=Prio(); |
| 193 | 193 |
ignore_unused_variable_warning(own_item); |
| 194 | 194 |
ignore_unused_variable_warning(own_prio); |
| 195 | 195 |
ignore_unused_variable_warning(own_state); |
| 196 | 196 |
|
| 197 | 197 |
_Heap heap1(map); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CONCEPT_MAPS_H |
| 20 | 20 |
#define LEMON_CONCEPT_MAPS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/concept_check.h> |
| 24 | 24 |
|
| 25 | 25 |
///\ingroup map_concepts |
| 26 | 26 |
///\file |
| 27 | 27 |
///\brief The concept of maps. |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
namespace concepts {
|
| 32 | 32 |
|
| 33 | 33 |
/// \addtogroup map_concepts |
| 34 | 34 |
/// @{
|
| 35 | 35 |
|
| 36 | 36 |
/// Readable map concept |
| 37 | 37 |
|
| 38 | 38 |
/// Readable map concept. |
| 39 | 39 |
/// |
| 40 | 40 |
template<typename K, typename T> |
| 41 | 41 |
class ReadMap |
| 42 | 42 |
{
|
| 43 | 43 |
public: |
| 44 | 44 |
/// The key type of the map. |
| 45 | 45 |
typedef K Key; |
| 46 | 46 |
/// \brief The value type of the map. |
| 47 | 47 |
/// (The type of objects associated with the keys). |
| 48 | 48 |
typedef T Value; |
| 49 | 49 |
|
| 50 | 50 |
/// Returns the value associated with the given key. |
| 51 | 51 |
Value operator[](const Key &) const {
|
| 52 | 52 |
return *static_cast<Value *>(0); |
| 53 | 53 |
} |
| 54 | 54 |
|
| 55 | 55 |
template<typename _ReadMap> |
| 56 | 56 |
struct Constraints {
|
| 57 | 57 |
void constraints() {
|
| 58 | 58 |
Value val = m[key]; |
| 59 | 59 |
val = m[key]; |
| 60 | 60 |
typename _ReadMap::Value own_val = m[own_key]; |
| 61 | 61 |
own_val = m[own_key]; |
| 62 | 62 |
|
| 63 | 63 |
ignore_unused_variable_warning(key); |
| 64 | 64 |
ignore_unused_variable_warning(val); |
| 65 | 65 |
ignore_unused_variable_warning(own_key); |
| 66 | 66 |
ignore_unused_variable_warning(own_val); |
| 67 | 67 |
} |
| 68 | 68 |
const Key& key; |
| 69 | 69 |
const typename _ReadMap::Key& own_key; |
| 70 | 70 |
const _ReadMap& m; |
| 71 | 71 |
}; |
| 72 | 72 |
|
| 73 | 73 |
}; |
| 74 | 74 |
|
| 75 | 75 |
|
| 76 | 76 |
/// Writable map concept |
| 77 | 77 |
|
| 78 | 78 |
/// Writable map concept. |
| 79 | 79 |
/// |
| 80 | 80 |
template<typename K, typename T> |
| 81 | 81 |
class WriteMap |
| 82 | 82 |
{
|
| 83 | 83 |
public: |
| 84 | 84 |
/// The key type of the map. |
| 85 | 85 |
typedef K Key; |
| 86 | 86 |
/// \brief The value type of the map. |
| 87 | 87 |
/// (The type of objects associated with the keys). |
| 88 | 88 |
typedef T Value; |
| 89 | 89 |
|
| 90 | 90 |
/// Sets the value associated with the given key. |
| 91 | 91 |
void set(const Key &, const Value &) {}
|
| 92 | 92 |
|
| 93 | 93 |
/// Default constructor. |
| 94 | 94 |
WriteMap() {}
|
| 95 | 95 |
|
| 96 | 96 |
template <typename _WriteMap> |
| 97 | 97 |
struct Constraints {
|
| 98 | 98 |
void constraints() {
|
| 99 | 99 |
m.set(key, val); |
| 100 | 100 |
m.set(own_key, own_val); |
| 101 | 101 |
|
| 102 | 102 |
ignore_unused_variable_warning(key); |
| 103 | 103 |
ignore_unused_variable_warning(val); |
| 104 | 104 |
ignore_unused_variable_warning(own_key); |
| 105 | 105 |
ignore_unused_variable_warning(own_val); |
| 106 | 106 |
} |
| 107 | 107 |
const Key& key; |
| 108 | 108 |
const Value& val; |
| 109 | 109 |
const typename _WriteMap::Key& own_key; |
| 110 | 110 |
const typename _WriteMap::Value& own_val; |
| 111 | 111 |
_WriteMap& m; |
| 112 | 112 |
}; |
| 113 | 113 |
}; |
| 114 | 114 |
|
| 115 | 115 |
/// Read/writable map concept |
| 116 | 116 |
|
| 117 | 117 |
/// Read/writable map concept. |
| 118 | 118 |
/// |
| 119 | 119 |
template<typename K, typename T> |
| 120 | 120 |
class ReadWriteMap : public ReadMap<K,T>, |
| 121 | 121 |
public WriteMap<K,T> |
| 122 | 122 |
{
|
| 123 | 123 |
public: |
| 124 | 124 |
/// The key type of the map. |
| 125 | 125 |
typedef K Key; |
| 126 | 126 |
/// \brief The value type of the map. |
| 127 | 127 |
/// (The type of objects associated with the keys). |
| 128 | 128 |
typedef T Value; |
| 129 | 129 |
|
| 130 | 130 |
/// Returns the value associated with the given key. |
| 131 | 131 |
Value operator[](const Key &) const {
|
| 132 | 132 |
return *static_cast<Value *>(0); |
| 133 | 133 |
} |
| 134 | 134 |
|
| 135 | 135 |
/// Sets the value associated with the given key. |
| 136 | 136 |
void set(const Key &, const Value &) {}
|
| 137 | 137 |
|
| 138 | 138 |
template<typename _ReadWriteMap> |
| 139 | 139 |
struct Constraints {
|
| 140 | 140 |
void constraints() {
|
| 141 | 141 |
checkConcept<ReadMap<K, T>, _ReadWriteMap >(); |
| 142 | 142 |
checkConcept<WriteMap<K, T>, _ReadWriteMap >(); |
| 143 | 143 |
} |
| 144 | 144 |
}; |
| 145 | 145 |
}; |
| 146 | 146 |
|
| 147 | 147 |
|
| 148 | 148 |
/// Dereferable map concept |
| 149 | 149 |
|
| 150 | 150 |
/// Dereferable map concept. |
| 151 | 151 |
/// |
| 152 | 152 |
template<typename K, typename T, typename R, typename CR> |
| 153 | 153 |
class ReferenceMap : public ReadWriteMap<K,T> |
| 154 | 154 |
{
|
| 155 | 155 |
public: |
| 156 | 156 |
/// Tag for reference maps. |
| 157 | 157 |
typedef True ReferenceMapTag; |
| 158 | 158 |
/// The key type of the map. |
| 159 | 159 |
typedef K Key; |
| 160 | 160 |
/// \brief The value type of the map. |
| 161 | 161 |
/// (The type of objects associated with the keys). |
| 162 | 162 |
typedef T Value; |
| 163 | 163 |
/// The reference type of the map. |
| 164 | 164 |
typedef R Reference; |
| 165 | 165 |
/// The const reference type of the map. |
| 166 | 166 |
typedef CR ConstReference; |
| 167 | 167 |
|
| 168 | 168 |
public: |
| 169 | 169 |
|
| 170 | 170 |
/// Returns a reference to the value associated with the given key. |
| 171 | 171 |
Reference operator[](const Key &) {
|
| 172 | 172 |
return *static_cast<Value *>(0); |
| 173 | 173 |
} |
| 174 | 174 |
|
| 175 | 175 |
/// Returns a const reference to the value associated with the given key. |
| 176 | 176 |
ConstReference operator[](const Key &) const {
|
| 177 | 177 |
return *static_cast<Value *>(0); |
| 178 | 178 |
} |
| 179 | 179 |
|
| 180 | 180 |
/// Sets the value associated with the given key. |
| 181 | 181 |
void set(const Key &k,const Value &t) { operator[](k)=t; }
|
| 182 | 182 |
|
| 183 | 183 |
template<typename _ReferenceMap> |
| 184 | 184 |
struct Constraints {
|
| 185 | 185 |
void constraints() {
|
| 186 | 186 |
checkConcept<ReadWriteMap<K, T>, _ReferenceMap >(); |
| 187 | 187 |
ref = m[key]; |
| 188 | 188 |
m[key] = val; |
| 189 | 189 |
m[key] = ref; |
| 190 | 190 |
m[key] = cref; |
| 191 | 191 |
own_ref = m[own_key]; |
| 192 | 192 |
m[own_key] = own_val; |
| 193 | 193 |
m[own_key] = own_ref; |
| 194 | 194 |
m[own_key] = own_cref; |
| 195 | 195 |
m[key] = m[own_key]; |
| 196 | 196 |
m[own_key] = m[key]; |
| 197 | 197 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup concept |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief Classes for representing paths in digraphs. |
| 22 | 22 |
/// |
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_CONCEPT_PATH_H |
| 25 | 25 |
#define LEMON_CONCEPT_PATH_H |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/core.h> |
| 28 | 28 |
#include <lemon/concept_check.h> |
| 29 | 29 |
|
| 30 | 30 |
namespace lemon {
|
| 31 | 31 |
namespace concepts {
|
| 32 | 32 |
|
| 33 | 33 |
/// \addtogroup concept |
| 34 | 34 |
/// @{
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief A skeleton structure for representing directed paths in |
| 37 | 37 |
/// a digraph. |
| 38 | 38 |
/// |
| 39 | 39 |
/// A skeleton structure for representing directed paths in a |
| 40 | 40 |
/// digraph. |
| 41 | 41 |
/// \tparam _Digraph The digraph type in which the path is. |
| 42 | 42 |
/// |
| 43 | 43 |
/// In a sense, the path can be treated as a list of arcs. The |
| 44 | 44 |
/// lemon path type stores just this list. As a consequence it |
| 45 | 45 |
/// cannot enumerate the nodes in the path and the zero length |
| 46 | 46 |
/// paths cannot store the source. |
| 47 | 47 |
/// |
| 48 | 48 |
template <typename _Digraph> |
| 49 | 49 |
class Path {
|
| 50 | 50 |
public: |
| 51 | 51 |
|
| 52 | 52 |
/// Type of the underlying digraph. |
| 53 | 53 |
typedef _Digraph Digraph; |
| 54 | 54 |
/// Arc type of the underlying digraph. |
| 55 | 55 |
typedef typename Digraph::Arc Arc; |
| 56 | 56 |
|
| 57 | 57 |
class ArcIt; |
| 58 | 58 |
|
| 59 | 59 |
/// \brief Default constructor |
| 60 | 60 |
Path() {}
|
| 61 | 61 |
|
| 62 | 62 |
/// \brief Template constructor |
| 63 | 63 |
template <typename CPath> |
| 64 | 64 |
Path(const CPath& cpath) {}
|
| 65 | 65 |
|
| 66 | 66 |
/// \brief Template assigment |
| 67 | 67 |
template <typename CPath> |
| 68 | 68 |
Path& operator=(const CPath& cpath) {
|
| 69 | 69 |
ignore_unused_variable_warning(cpath); |
| 70 | 70 |
return *this; |
| 71 | 71 |
} |
| 72 | 72 |
|
| 73 | 73 |
/// Length of the path ie. the number of arcs in the path. |
| 74 | 74 |
int length() const { return 0;}
|
| 75 | 75 |
|
| 76 | 76 |
/// Returns whether the path is empty. |
| 77 | 77 |
bool empty() const { return true;}
|
| 78 | 78 |
|
| 79 | 79 |
/// Resets the path to an empty path. |
| 80 | 80 |
void clear() {}
|
| 81 | 81 |
|
| 82 | 82 |
/// \brief LEMON style iterator for path arcs |
| 83 | 83 |
/// |
| 84 | 84 |
/// This class is used to iterate on the arcs of the paths. |
| 85 | 85 |
class ArcIt {
|
| 86 | 86 |
public: |
| 87 | 87 |
/// Default constructor |
| 88 | 88 |
ArcIt() {}
|
| 89 | 89 |
/// Invalid constructor |
| 90 | 90 |
ArcIt(Invalid) {}
|
| 91 | 91 |
/// Constructor for first arc |
| 92 | 92 |
ArcIt(const Path &) {}
|
| 93 | 93 |
|
| 94 | 94 |
/// Conversion to Arc |
| 95 | 95 |
operator Arc() const { return INVALID; }
|
| 96 | 96 |
|
| 97 | 97 |
/// Next arc |
| 98 | 98 |
ArcIt& operator++() {return *this;}
|
| 99 | 99 |
|
| 100 | 100 |
/// Comparison operator |
| 101 | 101 |
bool operator==(const ArcIt&) const {return true;}
|
| 102 | 102 |
/// Comparison operator |
| 103 | 103 |
bool operator!=(const ArcIt&) const {return true;}
|
| 104 | 104 |
/// Comparison operator |
| 105 | 105 |
bool operator<(const ArcIt&) const {return false;}
|
| 106 | 106 |
|
| 107 | 107 |
}; |
| 108 | 108 |
|
| 109 | 109 |
template <typename _Path> |
| 110 | 110 |
struct Constraints {
|
| 111 | 111 |
void constraints() {
|
| 112 | 112 |
Path<Digraph> pc; |
| 113 | 113 |
_Path p, pp(pc); |
| 114 | 114 |
int l = p.length(); |
| 115 | 115 |
int e = p.empty(); |
| 116 | 116 |
p.clear(); |
| 117 | 117 |
|
| 118 | 118 |
p = pc; |
| 119 | 119 |
|
| 120 | 120 |
typename _Path::ArcIt id, ii(INVALID), i(p); |
| 121 | 121 |
|
| 122 | 122 |
++i; |
| 123 | 123 |
typename Digraph::Arc ed = i; |
| 124 | 124 |
|
| 125 | 125 |
e = (i == ii); |
| 126 | 126 |
e = (i != ii); |
| 127 | 127 |
e = (i < ii); |
| 128 | 128 |
|
| 129 | 129 |
ignore_unused_variable_warning(l); |
| 130 | 130 |
ignore_unused_variable_warning(pp); |
| 131 | 131 |
ignore_unused_variable_warning(e); |
| 132 | 132 |
ignore_unused_variable_warning(id); |
| 133 | 133 |
ignore_unused_variable_warning(ii); |
| 134 | 134 |
ignore_unused_variable_warning(ed); |
| 135 | 135 |
} |
| 136 | 136 |
}; |
| 137 | 137 |
|
| 138 | 138 |
}; |
| 139 | 139 |
|
| 140 | 140 |
namespace _path_bits {
|
| 141 | 141 |
|
| 142 | 142 |
template <typename _Digraph, typename _Path, typename RevPathTag = void> |
| 143 | 143 |
struct PathDumperConstraints {
|
| 144 | 144 |
void constraints() {
|
| 145 | 145 |
int l = p.length(); |
| 146 | 146 |
int e = p.empty(); |
| 147 | 147 |
|
| 148 | 148 |
typename _Path::ArcIt id, i(p); |
| 149 | 149 |
|
| 150 | 150 |
++i; |
| 151 | 151 |
typename _Digraph::Arc ed = i; |
| 152 | 152 |
|
| 153 | 153 |
e = (i == INVALID); |
| 154 | 154 |
e = (i != INVALID); |
| 155 | 155 |
|
| 156 | 156 |
ignore_unused_variable_warning(l); |
| 157 | 157 |
ignore_unused_variable_warning(e); |
| 158 | 158 |
ignore_unused_variable_warning(id); |
| 159 | 159 |
ignore_unused_variable_warning(ed); |
| 160 | 160 |
} |
| 161 | 161 |
_Path& p; |
| 162 | 162 |
}; |
| 163 | 163 |
|
| 164 | 164 |
template <typename _Digraph, typename _Path> |
| 165 | 165 |
struct PathDumperConstraints< |
| 166 | 166 |
_Digraph, _Path, |
| 167 | 167 |
typename enable_if<typename _Path::RevPathTag, void>::type |
| 168 | 168 |
> {
|
| 169 | 169 |
void constraints() {
|
| 170 | 170 |
int l = p.length(); |
| 171 | 171 |
int e = p.empty(); |
| 172 | 172 |
|
| 173 | 173 |
typename _Path::RevArcIt id, i(p); |
| 174 | 174 |
|
| 175 | 175 |
++i; |
| 176 | 176 |
typename _Digraph::Arc ed = i; |
| 177 | 177 |
|
| 178 | 178 |
e = (i == INVALID); |
| 179 | 179 |
e = (i != INVALID); |
| 180 | 180 |
|
| 181 | 181 |
ignore_unused_variable_warning(l); |
| 182 | 182 |
ignore_unused_variable_warning(e); |
| 183 | 183 |
ignore_unused_variable_warning(id); |
| 184 | 184 |
ignore_unused_variable_warning(ed); |
| 185 | 185 |
} |
| 186 | 186 |
_Path& p; |
| 187 | 187 |
}; |
| 188 | 188 |
|
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 | 191 |
|
| 192 | 192 |
/// \brief A skeleton structure for path dumpers. |
| 193 | 193 |
/// |
| 194 | 194 |
/// A skeleton structure for path dumpers. The path dumpers are |
| 195 | 195 |
/// the generalization of the paths. The path dumpers can |
| 196 | 196 |
/// enumerate the arcs of the path wheter in forward or in |
| 197 | 197 |
/// backward order. In most time these classes are not used |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CONNECTIVITY_H |
| 20 | 20 |
#define LEMON_CONNECTIVITY_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/dfs.h> |
| 23 | 23 |
#include <lemon/bfs.h> |
| 24 | 24 |
#include <lemon/core.h> |
| 25 | 25 |
#include <lemon/maps.h> |
| 26 | 26 |
#include <lemon/adaptors.h> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/concepts/digraph.h> |
| 29 | 29 |
#include <lemon/concepts/graph.h> |
| 30 | 30 |
#include <lemon/concept_check.h> |
| 31 | 31 |
|
| 32 | 32 |
#include <stack> |
| 33 | 33 |
#include <functional> |
| 34 | 34 |
|
| 35 | 35 |
/// \ingroup connectivity |
| 36 | 36 |
/// \file |
| 37 | 37 |
/// \brief Connectivity algorithms |
| 38 | 38 |
/// |
| 39 | 39 |
/// Connectivity algorithms |
| 40 | 40 |
|
| 41 | 41 |
namespace lemon {
|
| 42 | 42 |
|
| 43 | 43 |
/// \ingroup connectivity |
| 44 | 44 |
/// |
| 45 | 45 |
/// \brief Check whether the given undirected graph is connected. |
| 46 | 46 |
/// |
| 47 | 47 |
/// Check whether the given undirected graph is connected. |
| 48 | 48 |
/// \param graph The undirected graph. |
| 49 | 49 |
/// \return %True when there is path between any two nodes in the graph. |
| 50 | 50 |
/// \note By definition, the empty graph is connected. |
| 51 | 51 |
template <typename Graph> |
| 52 | 52 |
bool connected(const Graph& graph) {
|
| 53 | 53 |
checkConcept<concepts::Graph, Graph>(); |
| 54 | 54 |
typedef typename Graph::NodeIt NodeIt; |
| 55 | 55 |
if (NodeIt(graph) == INVALID) return true; |
| 56 | 56 |
Dfs<Graph> dfs(graph); |
| 57 | 57 |
dfs.run(NodeIt(graph)); |
| 58 | 58 |
for (NodeIt it(graph); it != INVALID; ++it) {
|
| 59 | 59 |
if (!dfs.reached(it)) {
|
| 60 | 60 |
return false; |
| 61 | 61 |
} |
| 62 | 62 |
} |
| 63 | 63 |
return true; |
| 64 | 64 |
} |
| 65 | 65 |
|
| 66 | 66 |
/// \ingroup connectivity |
| 67 | 67 |
/// |
| 68 | 68 |
/// \brief Count the number of connected components of an undirected graph |
| 69 | 69 |
/// |
| 70 | 70 |
/// Count the number of connected components of an undirected graph |
| 71 | 71 |
/// |
| 72 | 72 |
/// \param graph The graph. It must be undirected. |
| 73 | 73 |
/// \return The number of components |
| 74 | 74 |
/// \note By definition, the empty graph consists |
| 75 | 75 |
/// of zero connected components. |
| 76 | 76 |
template <typename Graph> |
| 77 | 77 |
int countConnectedComponents(const Graph &graph) {
|
| 78 | 78 |
checkConcept<concepts::Graph, Graph>(); |
| 79 | 79 |
typedef typename Graph::Node Node; |
| 80 | 80 |
typedef typename Graph::Arc Arc; |
| 81 | 81 |
|
| 82 | 82 |
typedef NullMap<Node, Arc> PredMap; |
| 83 | 83 |
typedef NullMap<Node, int> DistMap; |
| 84 | 84 |
|
| 85 | 85 |
int compNum = 0; |
| 86 | 86 |
typename Bfs<Graph>:: |
| 87 | 87 |
template SetPredMap<PredMap>:: |
| 88 | 88 |
template SetDistMap<DistMap>:: |
| 89 | 89 |
Create bfs(graph); |
| 90 | 90 |
|
| 91 | 91 |
PredMap predMap; |
| 92 | 92 |
bfs.predMap(predMap); |
| 93 | 93 |
|
| 94 | 94 |
DistMap distMap; |
| 95 | 95 |
bfs.distMap(distMap); |
| 96 | 96 |
|
| 97 | 97 |
bfs.init(); |
| 98 | 98 |
for(typename Graph::NodeIt n(graph); n != INVALID; ++n) {
|
| 99 | 99 |
if (!bfs.reached(n)) {
|
| 100 | 100 |
bfs.addSource(n); |
| 101 | 101 |
bfs.start(); |
| 102 | 102 |
++compNum; |
| 103 | 103 |
} |
| 104 | 104 |
} |
| 105 | 105 |
return compNum; |
| 106 | 106 |
} |
| 107 | 107 |
|
| 108 | 108 |
/// \ingroup connectivity |
| 109 | 109 |
/// |
| 110 | 110 |
/// \brief Find the connected components of an undirected graph |
| 111 | 111 |
/// |
| 112 | 112 |
/// Find the connected components of an undirected graph. |
| 113 | 113 |
/// |
| 114 | 114 |
/// \param graph The graph. It must be undirected. |
| 115 | 115 |
/// \retval compMap A writable node map. The values will be set from 0 to |
| 116 | 116 |
/// the number of the connected components minus one. Each values of the map |
| 117 | 117 |
/// will be set exactly once, the values of a certain component will be |
| 118 | 118 |
/// set continuously. |
| 119 | 119 |
/// \return The number of components |
| 120 | 120 |
/// |
| 121 | 121 |
template <class Graph, class NodeMap> |
| 122 | 122 |
int connectedComponents(const Graph &graph, NodeMap &compMap) {
|
| 123 | 123 |
checkConcept<concepts::Graph, Graph>(); |
| 124 | 124 |
typedef typename Graph::Node Node; |
| 125 | 125 |
typedef typename Graph::Arc Arc; |
| 126 | 126 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>(); |
| 127 | 127 |
|
| 128 | 128 |
typedef NullMap<Node, Arc> PredMap; |
| 129 | 129 |
typedef NullMap<Node, int> DistMap; |
| 130 | 130 |
|
| 131 | 131 |
int compNum = 0; |
| 132 | 132 |
typename Bfs<Graph>:: |
| 133 | 133 |
template SetPredMap<PredMap>:: |
| 134 | 134 |
template SetDistMap<DistMap>:: |
| 135 | 135 |
Create bfs(graph); |
| 136 | 136 |
|
| 137 | 137 |
PredMap predMap; |
| 138 | 138 |
bfs.predMap(predMap); |
| 139 | 139 |
|
| 140 | 140 |
DistMap distMap; |
| 141 | 141 |
bfs.distMap(distMap); |
| 142 | 142 |
|
| 143 | 143 |
bfs.init(); |
| 144 | 144 |
for(typename Graph::NodeIt n(graph); n != INVALID; ++n) {
|
| 145 | 145 |
if(!bfs.reached(n)) {
|
| 146 | 146 |
bfs.addSource(n); |
| 147 | 147 |
while (!bfs.emptyQueue()) {
|
| 148 | 148 |
compMap.set(bfs.nextNode(), compNum); |
| 149 | 149 |
bfs.processNextNode(); |
| 150 | 150 |
} |
| 151 | 151 |
++compNum; |
| 152 | 152 |
} |
| 153 | 153 |
} |
| 154 | 154 |
return compNum; |
| 155 | 155 |
} |
| 156 | 156 |
|
| 157 | 157 |
namespace _connectivity_bits {
|
| 158 | 158 |
|
| 159 | 159 |
template <typename Digraph, typename Iterator > |
| 160 | 160 |
struct LeaveOrderVisitor : public DfsVisitor<Digraph> {
|
| 161 | 161 |
public: |
| 162 | 162 |
typedef typename Digraph::Node Node; |
| 163 | 163 |
LeaveOrderVisitor(Iterator it) : _it(it) {}
|
| 164 | 164 |
|
| 165 | 165 |
void leave(const Node& node) {
|
| 166 | 166 |
*(_it++) = node; |
| 167 | 167 |
} |
| 168 | 168 |
|
| 169 | 169 |
private: |
| 170 | 170 |
Iterator _it; |
| 171 | 171 |
}; |
| 172 | 172 |
|
| 173 | 173 |
template <typename Digraph, typename Map> |
| 174 | 174 |
struct FillMapVisitor : public DfsVisitor<Digraph> {
|
| 175 | 175 |
public: |
| 176 | 176 |
typedef typename Digraph::Node Node; |
| 177 | 177 |
typedef typename Map::Value Value; |
| 178 | 178 |
|
| 179 | 179 |
FillMapVisitor(Map& map, Value& value) |
| 180 | 180 |
: _map(map), _value(value) {}
|
| 181 | 181 |
|
| 182 | 182 |
void reach(const Node& node) {
|
| 183 | 183 |
_map.set(node, _value); |
| 184 | 184 |
} |
| 185 | 185 |
private: |
| 186 | 186 |
Map& _map; |
| 187 | 187 |
Value& _value; |
| 188 | 188 |
}; |
| 189 | 189 |
|
| 190 | 190 |
template <typename Digraph, typename ArcMap> |
| 191 | 191 |
struct StronglyConnectedCutArcsVisitor : public DfsVisitor<Digraph> {
|
| 192 | 192 |
public: |
| 193 | 193 |
typedef typename Digraph::Node Node; |
| 194 | 194 |
typedef typename Digraph::Arc Arc; |
| 195 | 195 |
|
| 196 | 196 |
StronglyConnectedCutArcsVisitor(const Digraph& digraph, |
| 197 | 197 |
ArcMap& cutMap, |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CORE_H |
| 20 | 20 |
#define LEMON_CORE_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <algorithm> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/bits/enable_if.h> |
| 26 | 26 |
#include <lemon/bits/traits.h> |
| 27 | 27 |
#include <lemon/assert.h> |
| 28 | 28 |
|
| 29 | 29 |
///\file |
| 30 | 30 |
///\brief LEMON core utilities. |
| 31 | 31 |
/// |
| 32 | 32 |
///This header file contains core utilities for LEMON. |
| 33 | 33 |
///It is automatically included by all graph types, therefore it usually |
| 34 | 34 |
///do not have to be included directly. |
| 35 | 35 |
|
| 36 | 36 |
namespace lemon {
|
| 37 | 37 |
|
| 38 | 38 |
/// \brief Dummy type to make it easier to create invalid iterators. |
| 39 | 39 |
/// |
| 40 | 40 |
/// Dummy type to make it easier to create invalid iterators. |
| 41 | 41 |
/// See \ref INVALID for the usage. |
| 42 | 42 |
struct Invalid {
|
| 43 | 43 |
public: |
| 44 | 44 |
bool operator==(Invalid) { return true; }
|
| 45 | 45 |
bool operator!=(Invalid) { return false; }
|
| 46 | 46 |
bool operator< (Invalid) { return false; }
|
| 47 | 47 |
}; |
| 48 | 48 |
|
| 49 | 49 |
/// \brief Invalid iterators. |
| 50 | 50 |
/// |
| 51 | 51 |
/// \ref Invalid is a global type that converts to each iterator |
| 52 | 52 |
/// in such a way that the value of the target iterator will be invalid. |
| 53 | 53 |
#ifdef LEMON_ONLY_TEMPLATES |
| 54 | 54 |
const Invalid INVALID = Invalid(); |
| 55 | 55 |
#else |
| 56 | 56 |
extern const Invalid INVALID; |
| 57 | 57 |
#endif |
| 58 | 58 |
|
| 59 | 59 |
/// \addtogroup gutils |
| 60 | 60 |
/// @{
|
| 61 | 61 |
|
| 62 | 62 |
///Create convenience typedefs for the digraph types and iterators |
| 63 | 63 |
|
| 64 | 64 |
///This \c \#define creates convenient type definitions for the following |
| 65 | 65 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
| 66 | 66 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
| 67 | 67 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
| 68 | 68 |
/// |
| 69 | 69 |
///\note If the graph type is a dependent type, ie. the graph type depend |
| 70 | 70 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
| 71 | 71 |
///macro. |
| 72 | 72 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
| 73 | 73 |
typedef Digraph::Node Node; \ |
| 74 | 74 |
typedef Digraph::NodeIt NodeIt; \ |
| 75 | 75 |
typedef Digraph::Arc Arc; \ |
| 76 | 76 |
typedef Digraph::ArcIt ArcIt; \ |
| 77 | 77 |
typedef Digraph::InArcIt InArcIt; \ |
| 78 | 78 |
typedef Digraph::OutArcIt OutArcIt; \ |
| 79 | 79 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
| 80 | 80 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
| 81 | 81 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
| 82 | 82 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
| 83 | 83 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
| 84 | 84 |
typedef Digraph::ArcMap<double> DoubleArcMap |
| 85 | 85 |
|
| 86 | 86 |
///Create convenience typedefs for the digraph types and iterators |
| 87 | 87 |
|
| 88 | 88 |
///\see DIGRAPH_TYPEDEFS |
| 89 | 89 |
/// |
| 90 | 90 |
///\note Use this macro, if the graph type is a dependent type, |
| 91 | 91 |
///ie. the graph type depend on a template parameter. |
| 92 | 92 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
| 93 | 93 |
typedef typename Digraph::Node Node; \ |
| 94 | 94 |
typedef typename Digraph::NodeIt NodeIt; \ |
| 95 | 95 |
typedef typename Digraph::Arc Arc; \ |
| 96 | 96 |
typedef typename Digraph::ArcIt ArcIt; \ |
| 97 | 97 |
typedef typename Digraph::InArcIt InArcIt; \ |
| 98 | 98 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
| 99 | 99 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
| 100 | 100 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
| 101 | 101 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
| 102 | 102 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
| 103 | 103 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
| 104 | 104 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
| 105 | 105 |
|
| 106 | 106 |
///Create convenience typedefs for the graph types and iterators |
| 107 | 107 |
|
| 108 | 108 |
///This \c \#define creates the same convenient type definitions as defined |
| 109 | 109 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
| 110 | 110 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
| 111 | 111 |
///\c DoubleEdgeMap. |
| 112 | 112 |
/// |
| 113 | 113 |
///\note If the graph type is a dependent type, ie. the graph type depend |
| 114 | 114 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS() |
| 115 | 115 |
///macro. |
| 116 | 116 |
#define GRAPH_TYPEDEFS(Graph) \ |
| 117 | 117 |
DIGRAPH_TYPEDEFS(Graph); \ |
| 118 | 118 |
typedef Graph::Edge Edge; \ |
| 119 | 119 |
typedef Graph::EdgeIt EdgeIt; \ |
| 120 | 120 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
| 121 | 121 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
| 122 | 122 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
| 123 | 123 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
| 124 | 124 |
|
| 125 | 125 |
///Create convenience typedefs for the graph types and iterators |
| 126 | 126 |
|
| 127 | 127 |
///\see GRAPH_TYPEDEFS |
| 128 | 128 |
/// |
| 129 | 129 |
///\note Use this macro, if the graph type is a dependent type, |
| 130 | 130 |
///ie. the graph type depend on a template parameter. |
| 131 | 131 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
| 132 | 132 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
| 133 | 133 |
typedef typename Graph::Edge Edge; \ |
| 134 | 134 |
typedef typename Graph::EdgeIt EdgeIt; \ |
| 135 | 135 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
| 136 | 136 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
| 137 | 137 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
| 138 | 138 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
| 139 | 139 |
|
| 140 | 140 |
/// \brief Function to count the items in a graph. |
| 141 | 141 |
/// |
| 142 | 142 |
/// This function counts the items (nodes, arcs etc.) in a graph. |
| 143 | 143 |
/// The complexity of the function is linear because |
| 144 | 144 |
/// it iterates on all of the items. |
| 145 | 145 |
template <typename Graph, typename Item> |
| 146 | 146 |
inline int countItems(const Graph& g) {
|
| 147 | 147 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
| 148 | 148 |
int num = 0; |
| 149 | 149 |
for (ItemIt it(g); it != INVALID; ++it) {
|
| 150 | 150 |
++num; |
| 151 | 151 |
} |
| 152 | 152 |
return num; |
| 153 | 153 |
} |
| 154 | 154 |
|
| 155 | 155 |
// Node counting: |
| 156 | 156 |
|
| 157 | 157 |
namespace _core_bits {
|
| 158 | 158 |
|
| 159 | 159 |
template <typename Graph, typename Enable = void> |
| 160 | 160 |
struct CountNodesSelector {
|
| 161 | 161 |
static int count(const Graph &g) {
|
| 162 | 162 |
return countItems<Graph, typename Graph::Node>(g); |
| 163 | 163 |
} |
| 164 | 164 |
}; |
| 165 | 165 |
|
| 166 | 166 |
template <typename Graph> |
| 167 | 167 |
struct CountNodesSelector< |
| 168 | 168 |
Graph, typename |
| 169 | 169 |
enable_if<typename Graph::NodeNumTag, void>::type> |
| 170 | 170 |
{
|
| 171 | 171 |
static int count(const Graph &g) {
|
| 172 | 172 |
return g.nodeNum(); |
| 173 | 173 |
} |
| 174 | 174 |
}; |
| 175 | 175 |
} |
| 176 | 176 |
|
| 177 | 177 |
/// \brief Function to count the nodes in the graph. |
| 178 | 178 |
/// |
| 179 | 179 |
/// This function counts the nodes in the graph. |
| 180 | 180 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some |
| 181 | 181 |
/// graph structures it is specialized to run in <em>O</em>(1). |
| 182 | 182 |
/// |
| 183 | 183 |
/// \note If the graph contains a \c nodeNum() member function and a |
| 184 | 184 |
/// \c NodeNumTag tag then this function calls directly the member |
| 185 | 185 |
/// function to query the cardinality of the node set. |
| 186 | 186 |
template <typename Graph> |
| 187 | 187 |
inline int countNodes(const Graph& g) {
|
| 188 | 188 |
return _core_bits::CountNodesSelector<Graph>::count(g); |
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 | 191 |
// Arc counting: |
| 192 | 192 |
|
| 193 | 193 |
namespace _core_bits {
|
| 194 | 194 |
|
| 195 | 195 |
template <typename Graph, typename Enable = void> |
| 196 | 196 |
struct CountArcsSelector {
|
| 197 | 197 |
static int count(const Graph &g) {
|
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_COUNTER_H |
| 20 | 20 |
#define LEMON_COUNTER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <string> |
| 23 | 23 |
#include <iostream> |
| 24 | 24 |
|
| 25 | 25 |
///\ingroup timecount |
| 26 | 26 |
///\file |
| 27 | 27 |
///\brief Tools for counting steps and events |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon |
| 30 | 30 |
{
|
| 31 | 31 |
|
| 32 | 32 |
template<class P> class _NoSubCounter; |
| 33 | 33 |
|
| 34 | 34 |
template<class P> |
| 35 | 35 |
class _SubCounter |
| 36 | 36 |
{
|
| 37 | 37 |
P &_parent; |
| 38 | 38 |
std::string _title; |
| 39 | 39 |
std::ostream &_os; |
| 40 | 40 |
int count; |
| 41 | 41 |
public: |
| 42 | 42 |
|
| 43 | 43 |
typedef _SubCounter<_SubCounter<P> > SubCounter; |
| 44 | 44 |
typedef _NoSubCounter<_SubCounter<P> > NoSubCounter; |
| 45 | 45 |
|
| 46 | 46 |
_SubCounter(P &parent) |
| 47 | 47 |
: _parent(parent), _title(), _os(std::cerr), count(0) {}
|
| 48 | 48 |
_SubCounter(P &parent,std::string title,std::ostream &os=std::cerr) |
| 49 | 49 |
: _parent(parent), _title(title), _os(os), count(0) {}
|
| 50 | 50 |
_SubCounter(P &parent,const char *title,std::ostream &os=std::cerr) |
| 51 | 51 |
: _parent(parent), _title(title), _os(os), count(0) {}
|
| 52 | 52 |
~_SubCounter() {
|
| 53 | 53 |
_os << _title << count <<std::endl; |
| 54 | 54 |
_parent+=count; |
| 55 | 55 |
} |
| 56 | 56 |
_SubCounter &operator++() { count++; return *this;}
|
| 57 | 57 |
int operator++(int) { return count++; }
|
| 58 | 58 |
_SubCounter &operator--() { count--; return *this;}
|
| 59 | 59 |
int operator--(int) { return count--; }
|
| 60 | 60 |
_SubCounter &operator+=(int c) { count+=c; return *this;}
|
| 61 | 61 |
_SubCounter &operator-=(int c) { count-=c; return *this;}
|
| 62 | 62 |
operator int() {return count;}
|
| 63 | 63 |
}; |
| 64 | 64 |
|
| 65 | 65 |
template<class P> |
| 66 | 66 |
class _NoSubCounter |
| 67 | 67 |
{
|
| 68 | 68 |
P &_parent; |
| 69 | 69 |
public: |
| 70 | 70 |
typedef _NoSubCounter<_NoSubCounter<P> > SubCounter; |
| 71 | 71 |
typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter; |
| 72 | 72 |
|
| 73 | 73 |
_NoSubCounter(P &parent) :_parent(parent) {}
|
| 74 | 74 |
_NoSubCounter(P &parent,std::string,std::ostream &) |
| 75 | 75 |
:_parent(parent) {}
|
| 76 | 76 |
_NoSubCounter(P &parent,std::string) |
| 77 | 77 |
:_parent(parent) {}
|
| 78 | 78 |
_NoSubCounter(P &parent,const char *,std::ostream &) |
| 79 | 79 |
:_parent(parent) {}
|
| 80 | 80 |
_NoSubCounter(P &parent,const char *) |
| 81 | 81 |
:_parent(parent) {}
|
| 82 | 82 |
~_NoSubCounter() {}
|
| 83 | 83 |
_NoSubCounter &operator++() { ++_parent; return *this;}
|
| 84 | 84 |
int operator++(int) { _parent++; return 0;}
|
| 85 | 85 |
_NoSubCounter &operator--() { --_parent; return *this;}
|
| 86 | 86 |
int operator--(int) { _parent--; return 0;}
|
| 87 | 87 |
_NoSubCounter &operator+=(int c) { _parent+=c; return *this;}
|
| 88 | 88 |
_NoSubCounter &operator-=(int c) { _parent-=c; return *this;}
|
| 89 | 89 |
operator int() {return 0;}
|
| 90 | 90 |
}; |
| 91 | 91 |
|
| 92 | 92 |
|
| 93 | 93 |
/// \addtogroup timecount |
| 94 | 94 |
/// @{
|
| 95 | 95 |
|
| 96 | 96 |
/// A counter class |
| 97 | 97 |
|
| 98 | 98 |
/// This class makes it easier to count certain events (e.g. for debug |
| 99 | 99 |
/// reasons). |
| 100 | 100 |
/// You can increment or decrement the counter using \c operator++, |
| 101 | 101 |
/// \c operator--, \c operator+= and \c operator-=. You can also |
| 102 | 102 |
/// define subcounters for the different phases of the algorithm or |
| 103 | 103 |
/// for different types of operations. |
| 104 | 104 |
/// A report containing the given title and the value of the counter |
| 105 | 105 |
/// is automatically printed on destruction. |
| 106 | 106 |
/// |
| 107 | 107 |
/// The following example shows the usage of counters and subcounters. |
| 108 | 108 |
/// \code |
| 109 | 109 |
/// // Bubble sort |
| 110 | 110 |
/// std::vector<T> v; |
| 111 | 111 |
/// ... |
| 112 | 112 |
/// Counter op("Operations: ");
|
| 113 | 113 |
/// Counter::SubCounter as(op, "Assignments: "); |
| 114 | 114 |
/// Counter::SubCounter co(op, "Comparisons: "); |
| 115 | 115 |
/// for (int i = v.size()-1; i > 0; --i) {
|
| 116 | 116 |
/// for (int j = 0; j < i; ++j) {
|
| 117 | 117 |
/// if (v[j] > v[j+1]) {
|
| 118 | 118 |
/// T tmp = v[j]; |
| 119 | 119 |
/// v[j] = v[j+1]; |
| 120 | 120 |
/// v[j+1] = tmp; |
| 121 | 121 |
/// as += 3; // three assignments |
| 122 | 122 |
/// } |
| 123 | 123 |
/// ++co; // one comparison |
| 124 | 124 |
/// } |
| 125 | 125 |
/// } |
| 126 | 126 |
/// \endcode |
| 127 | 127 |
/// |
| 128 | 128 |
/// This code prints out something like that: |
| 129 | 129 |
/// \code |
| 130 | 130 |
/// Comparisons: 45 |
| 131 | 131 |
/// Assignments: 57 |
| 132 | 132 |
/// Operations: 102 |
| 133 | 133 |
/// \endcode |
| 134 | 134 |
/// |
| 135 | 135 |
/// \sa NoCounter |
| 136 | 136 |
class Counter |
| 137 | 137 |
{
|
| 138 | 138 |
std::string _title; |
| 139 | 139 |
std::ostream &_os; |
| 140 | 140 |
int count; |
| 141 | 141 |
public: |
| 142 | 142 |
|
| 143 | 143 |
/// SubCounter class |
| 144 | 144 |
|
| 145 | 145 |
/// This class can be used to setup subcounters for a \ref Counter |
| 146 | 146 |
/// to have finer reports. A subcounter provides exactly the same |
| 147 | 147 |
/// operations as the main \ref Counter, but it also increments and |
| 148 | 148 |
/// decrements the value of its parent. |
| 149 | 149 |
/// Subcounters can also have subcounters. |
| 150 | 150 |
/// |
| 151 | 151 |
/// The parent counter must be given as the first parameter of the |
| 152 | 152 |
/// constructor. Apart from that a title and an \c ostream object |
| 153 | 153 |
/// can also be given just like for the main \ref Counter. |
| 154 | 154 |
/// |
| 155 | 155 |
/// A report containing the given title and the value of the |
| 156 | 156 |
/// subcounter is automatically printed on destruction. If you |
| 157 | 157 |
/// would like to turn off this report, use \ref NoSubCounter |
| 158 | 158 |
/// instead. |
| 159 | 159 |
/// |
| 160 | 160 |
/// \sa NoSubCounter |
| 161 | 161 |
typedef _SubCounter<Counter> SubCounter; |
| 162 | 162 |
|
| 163 | 163 |
/// SubCounter class without printing report on destruction |
| 164 | 164 |
|
| 165 | 165 |
/// This class can be used to setup subcounters for a \ref Counter. |
| 166 | 166 |
/// It is the same as \ref SubCounter but it does not print report |
| 167 | 167 |
/// on destruction. (It modifies the value of its parent, so 'No' |
| 168 | 168 |
/// only means 'do not print'.) |
| 169 | 169 |
/// |
| 170 | 170 |
/// Replacing \ref SubCounter "SubCounter"s with \ref NoSubCounter |
| 171 | 171 |
/// "NoSubCounter"s makes it possible to turn off reporting |
| 172 | 172 |
/// subcounter values without actually removing the definitions |
| 173 | 173 |
/// and the increment or decrement operators. |
| 174 | 174 |
/// |
| 175 | 175 |
/// \sa SubCounter |
| 176 | 176 |
typedef _NoSubCounter<Counter> NoSubCounter; |
| 177 | 177 |
|
| 178 | 178 |
/// Constructor. |
| 179 | 179 |
Counter() : _title(), _os(std::cerr), count(0) {}
|
| 180 | 180 |
/// Constructor. |
| 181 | 181 |
Counter(std::string title,std::ostream &os=std::cerr) |
| 182 | 182 |
: _title(title), _os(os), count(0) {}
|
| 183 | 183 |
/// Constructor. |
| 184 | 184 |
Counter(const char *title,std::ostream &os=std::cerr) |
| 185 | 185 |
: _title(title), _os(os), count(0) {}
|
| 186 | 186 |
/// Destructor. Prints the given title and the value of the counter. |
| 187 | 187 |
~Counter() {
|
| 188 | 188 |
_os << _title << count <<std::endl; |
| 189 | 189 |
} |
| 190 | 190 |
///\e |
| 191 | 191 |
Counter &operator++() { count++; return *this;}
|
| 192 | 192 |
///\e |
| 193 | 193 |
int operator++(int) { return count++;}
|
| 194 | 194 |
///\e |
| 195 | 195 |
Counter &operator--() { count--; return *this;}
|
| 196 | 196 |
///\e |
| 197 | 197 |
int operator--(int) { return count--;}
|
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_DFS_H |
| 20 | 20 |
#define LEMON_DFS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup search |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief DFS algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
#include <lemon/path.h> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
///Default traits class of Dfs class. |
| 36 | 36 |
|
| 37 | 37 |
///Default traits class of Dfs class. |
| 38 | 38 |
///\tparam GR Digraph type. |
| 39 | 39 |
template<class GR> |
| 40 | 40 |
struct DfsDefaultTraits |
| 41 | 41 |
{
|
| 42 | 42 |
///The type of the digraph the algorithm runs on. |
| 43 | 43 |
typedef GR Digraph; |
| 44 | 44 |
|
| 45 | 45 |
///\brief The type of the map that stores the predecessor |
| 46 | 46 |
///arcs of the %DFS paths. |
| 47 | 47 |
/// |
| 48 | 48 |
///The type of the map that stores the predecessor |
| 49 | 49 |
///arcs of the %DFS paths. |
| 50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 52 | 52 |
///Instantiates a PredMap. |
| 53 | 53 |
|
| 54 | 54 |
///This function instantiates a PredMap. |
| 55 | 55 |
///\param g is the digraph, to which we would like to define the |
| 56 | 56 |
///PredMap. |
| 57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
| 58 | 58 |
{
|
| 59 | 59 |
return new PredMap(g); |
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 | 62 |
///The type of the map that indicates which nodes are processed. |
| 63 | 63 |
|
| 64 | 64 |
///The type of the map that indicates which nodes are processed. |
| 65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 67 | 67 |
///Instantiates a ProcessedMap. |
| 68 | 68 |
|
| 69 | 69 |
///This function instantiates a ProcessedMap. |
| 70 | 70 |
///\param g is the digraph, to which |
| 71 | 71 |
///we would like to define the ProcessedMap |
| 72 | 72 |
#ifdef DOXYGEN |
| 73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 74 | 74 |
#else |
| 75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 76 | 76 |
#endif |
| 77 | 77 |
{
|
| 78 | 78 |
return new ProcessedMap(); |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
///The type of the map that indicates which nodes are reached. |
| 82 | 82 |
|
| 83 | 83 |
///The type of the map that indicates which nodes are reached. |
| 84 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 85 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 86 | 86 |
///Instantiates a ReachedMap. |
| 87 | 87 |
|
| 88 | 88 |
///This function instantiates a ReachedMap. |
| 89 | 89 |
///\param g is the digraph, to which |
| 90 | 90 |
///we would like to define the ReachedMap. |
| 91 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 92 | 92 |
{
|
| 93 | 93 |
return new ReachedMap(g); |
| 94 | 94 |
} |
| 95 | 95 |
|
| 96 | 96 |
///The type of the map that stores the distances of the nodes. |
| 97 | 97 |
|
| 98 | 98 |
///The type of the map that stores the distances of the nodes. |
| 99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 101 | 101 |
///Instantiates a DistMap. |
| 102 | 102 |
|
| 103 | 103 |
///This function instantiates a DistMap. |
| 104 | 104 |
///\param g is the digraph, to which we would like to define the |
| 105 | 105 |
///DistMap. |
| 106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
| 107 | 107 |
{
|
| 108 | 108 |
return new DistMap(g); |
| 109 | 109 |
} |
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
///%DFS algorithm class. |
| 113 | 113 |
|
| 114 | 114 |
///\ingroup search |
| 115 | 115 |
///This class provides an efficient implementation of the %DFS algorithm. |
| 116 | 116 |
/// |
| 117 | 117 |
///There is also a \ref dfs() "function-type interface" for the DFS |
| 118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
| 119 | 119 |
///used easier. |
| 120 | 120 |
/// |
| 121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 122 | 122 |
///The default type is \ref ListDigraph. |
| 123 | 123 |
#ifdef DOXYGEN |
| 124 | 124 |
template <typename GR, |
| 125 | 125 |
typename TR> |
| 126 | 126 |
#else |
| 127 | 127 |
template <typename GR=ListDigraph, |
| 128 | 128 |
typename TR=DfsDefaultTraits<GR> > |
| 129 | 129 |
#endif |
| 130 | 130 |
class Dfs {
|
| 131 | 131 |
public: |
| 132 | 132 |
|
| 133 | 133 |
///The type of the digraph the algorithm runs on. |
| 134 | 134 |
typedef typename TR::Digraph Digraph; |
| 135 | 135 |
|
| 136 | 136 |
///\brief The type of the map that stores the predecessor arcs of the |
| 137 | 137 |
///DFS paths. |
| 138 | 138 |
typedef typename TR::PredMap PredMap; |
| 139 | 139 |
///The type of the map that stores the distances of the nodes. |
| 140 | 140 |
typedef typename TR::DistMap DistMap; |
| 141 | 141 |
///The type of the map that indicates which nodes are reached. |
| 142 | 142 |
typedef typename TR::ReachedMap ReachedMap; |
| 143 | 143 |
///The type of the map that indicates which nodes are processed. |
| 144 | 144 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 145 | 145 |
///The type of the paths. |
| 146 | 146 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 147 | 147 |
|
| 148 | 148 |
///The \ref DfsDefaultTraits "traits class" of the algorithm. |
| 149 | 149 |
typedef TR Traits; |
| 150 | 150 |
|
| 151 | 151 |
private: |
| 152 | 152 |
|
| 153 | 153 |
typedef typename Digraph::Node Node; |
| 154 | 154 |
typedef typename Digraph::NodeIt NodeIt; |
| 155 | 155 |
typedef typename Digraph::Arc Arc; |
| 156 | 156 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 157 | 157 |
|
| 158 | 158 |
//Pointer to the underlying digraph. |
| 159 | 159 |
const Digraph *G; |
| 160 | 160 |
//Pointer to the map of predecessor arcs. |
| 161 | 161 |
PredMap *_pred; |
| 162 | 162 |
//Indicates if _pred is locally allocated (true) or not. |
| 163 | 163 |
bool local_pred; |
| 164 | 164 |
//Pointer to the map of distances. |
| 165 | 165 |
DistMap *_dist; |
| 166 | 166 |
//Indicates if _dist is locally allocated (true) or not. |
| 167 | 167 |
bool local_dist; |
| 168 | 168 |
//Pointer to the map of reached status of the nodes. |
| 169 | 169 |
ReachedMap *_reached; |
| 170 | 170 |
//Indicates if _reached is locally allocated (true) or not. |
| 171 | 171 |
bool local_reached; |
| 172 | 172 |
//Pointer to the map of processed status of the nodes. |
| 173 | 173 |
ProcessedMap *_processed; |
| 174 | 174 |
//Indicates if _processed is locally allocated (true) or not. |
| 175 | 175 |
bool local_processed; |
| 176 | 176 |
|
| 177 | 177 |
std::vector<typename Digraph::OutArcIt> _stack; |
| 178 | 178 |
int _stack_head; |
| 179 | 179 |
|
| 180 | 180 |
//Creates the maps if necessary. |
| 181 | 181 |
void create_maps() |
| 182 | 182 |
{
|
| 183 | 183 |
if(!_pred) {
|
| 184 | 184 |
local_pred = true; |
| 185 | 185 |
_pred = Traits::createPredMap(*G); |
| 186 | 186 |
} |
| 187 | 187 |
if(!_dist) {
|
| 188 | 188 |
local_dist = true; |
| 189 | 189 |
_dist = Traits::createDistMap(*G); |
| 190 | 190 |
} |
| 191 | 191 |
if(!_reached) {
|
| 192 | 192 |
local_reached = true; |
| 193 | 193 |
_reached = Traits::createReachedMap(*G); |
| 194 | 194 |
} |
| 195 | 195 |
if(!_processed) {
|
| 196 | 196 |
local_processed = true; |
| 197 | 197 |
_processed = Traits::createProcessedMap(*G); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_DIJKSTRA_H |
| 20 | 20 |
#define LEMON_DIJKSTRA_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup shortest_path |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Dijkstra algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <limits> |
| 27 | 27 |
#include <lemon/list_graph.h> |
| 28 | 28 |
#include <lemon/bin_heap.h> |
| 29 | 29 |
#include <lemon/bits/path_dump.h> |
| 30 | 30 |
#include <lemon/core.h> |
| 31 | 31 |
#include <lemon/error.h> |
| 32 | 32 |
#include <lemon/maps.h> |
| 33 | 33 |
#include <lemon/path.h> |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
/// \brief Default operation traits for the Dijkstra algorithm class. |
| 38 | 38 |
/// |
| 39 | 39 |
/// This operation traits class defines all computational operations and |
| 40 | 40 |
/// constants which are used in the Dijkstra algorithm. |
| 41 | 41 |
template <typename Value> |
| 42 | 42 |
struct DijkstraDefaultOperationTraits {
|
| 43 | 43 |
/// \brief Gives back the zero value of the type. |
| 44 | 44 |
static Value zero() {
|
| 45 | 45 |
return static_cast<Value>(0); |
| 46 | 46 |
} |
| 47 | 47 |
/// \brief Gives back the sum of the given two elements. |
| 48 | 48 |
static Value plus(const Value& left, const Value& right) {
|
| 49 | 49 |
return left + right; |
| 50 | 50 |
} |
| 51 | 51 |
/// \brief Gives back true only if the first value is less than the second. |
| 52 | 52 |
static bool less(const Value& left, const Value& right) {
|
| 53 | 53 |
return left < right; |
| 54 | 54 |
} |
| 55 | 55 |
}; |
| 56 | 56 |
|
| 57 | 57 |
///Default traits class of Dijkstra class. |
| 58 | 58 |
|
| 59 | 59 |
///Default traits class of Dijkstra class. |
| 60 | 60 |
///\tparam GR The type of the digraph. |
| 61 | 61 |
///\tparam LM The type of the length map. |
| 62 | 62 |
template<class GR, class LM> |
| 63 | 63 |
struct DijkstraDefaultTraits |
| 64 | 64 |
{
|
| 65 | 65 |
///The type of the digraph the algorithm runs on. |
| 66 | 66 |
typedef GR Digraph; |
| 67 | 67 |
|
| 68 | 68 |
///The type of the map that stores the arc lengths. |
| 69 | 69 |
|
| 70 | 70 |
///The type of the map that stores the arc lengths. |
| 71 | 71 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 72 | 72 |
typedef LM LengthMap; |
| 73 | 73 |
///The type of the length of the arcs. |
| 74 | 74 |
typedef typename LM::Value Value; |
| 75 | 75 |
|
| 76 | 76 |
/// Operation traits for Dijkstra algorithm. |
| 77 | 77 |
|
| 78 | 78 |
/// This class defines the operations that are used in the algorithm. |
| 79 | 79 |
/// \see DijkstraDefaultOperationTraits |
| 80 | 80 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
| 81 | 81 |
|
| 82 | 82 |
/// The cross reference type used by the heap. |
| 83 | 83 |
|
| 84 | 84 |
/// The cross reference type used by the heap. |
| 85 | 85 |
/// Usually it is \c Digraph::NodeMap<int>. |
| 86 | 86 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
| 87 | 87 |
///Instantiates a \ref HeapCrossRef. |
| 88 | 88 |
|
| 89 | 89 |
///This function instantiates a \ref HeapCrossRef. |
| 90 | 90 |
/// \param g is the digraph, to which we would like to define the |
| 91 | 91 |
/// \ref HeapCrossRef. |
| 92 | 92 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
| 93 | 93 |
{
|
| 94 | 94 |
return new HeapCrossRef(g); |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 | 97 |
///The heap type used by the Dijkstra algorithm. |
| 98 | 98 |
|
| 99 | 99 |
///The heap type used by the Dijkstra algorithm. |
| 100 | 100 |
/// |
| 101 | 101 |
///\sa BinHeap |
| 102 | 102 |
///\sa Dijkstra |
| 103 | 103 |
typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
| 104 | 104 |
///Instantiates a \ref Heap. |
| 105 | 105 |
|
| 106 | 106 |
///This function instantiates a \ref Heap. |
| 107 | 107 |
static Heap *createHeap(HeapCrossRef& r) |
| 108 | 108 |
{
|
| 109 | 109 |
return new Heap(r); |
| 110 | 110 |
} |
| 111 | 111 |
|
| 112 | 112 |
///\brief The type of the map that stores the predecessor |
| 113 | 113 |
///arcs of the shortest paths. |
| 114 | 114 |
/// |
| 115 | 115 |
///The type of the map that stores the predecessor |
| 116 | 116 |
///arcs of the shortest paths. |
| 117 | 117 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 118 | 118 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 119 | 119 |
///Instantiates a PredMap. |
| 120 | 120 |
|
| 121 | 121 |
///This function instantiates a PredMap. |
| 122 | 122 |
///\param g is the digraph, to which we would like to define the |
| 123 | 123 |
///PredMap. |
| 124 | 124 |
static PredMap *createPredMap(const Digraph &g) |
| 125 | 125 |
{
|
| 126 | 126 |
return new PredMap(g); |
| 127 | 127 |
} |
| 128 | 128 |
|
| 129 | 129 |
///The type of the map that indicates which nodes are processed. |
| 130 | 130 |
|
| 131 | 131 |
///The type of the map that indicates which nodes are processed. |
| 132 | 132 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 133 | 133 |
///By default it is a NullMap. |
| 134 | 134 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 135 | 135 |
///Instantiates a ProcessedMap. |
| 136 | 136 |
|
| 137 | 137 |
///This function instantiates a ProcessedMap. |
| 138 | 138 |
///\param g is the digraph, to which |
| 139 | 139 |
///we would like to define the ProcessedMap |
| 140 | 140 |
#ifdef DOXYGEN |
| 141 | 141 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 142 | 142 |
#else |
| 143 | 143 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 144 | 144 |
#endif |
| 145 | 145 |
{
|
| 146 | 146 |
return new ProcessedMap(); |
| 147 | 147 |
} |
| 148 | 148 |
|
| 149 | 149 |
///The type of the map that stores the distances of the nodes. |
| 150 | 150 |
|
| 151 | 151 |
///The type of the map that stores the distances of the nodes. |
| 152 | 152 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 153 | 153 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
| 154 | 154 |
///Instantiates a DistMap. |
| 155 | 155 |
|
| 156 | 156 |
///This function instantiates a DistMap. |
| 157 | 157 |
///\param g is the digraph, to which we would like to define |
| 158 | 158 |
///the DistMap |
| 159 | 159 |
static DistMap *createDistMap(const Digraph &g) |
| 160 | 160 |
{
|
| 161 | 161 |
return new DistMap(g); |
| 162 | 162 |
} |
| 163 | 163 |
}; |
| 164 | 164 |
|
| 165 | 165 |
///%Dijkstra algorithm class. |
| 166 | 166 |
|
| 167 | 167 |
/// \ingroup shortest_path |
| 168 | 168 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
| 169 | 169 |
/// |
| 170 | 170 |
///The arc lengths are passed to the algorithm using a |
| 171 | 171 |
///\ref concepts::ReadMap "ReadMap", |
| 172 | 172 |
///so it is easy to change it to any kind of length. |
| 173 | 173 |
///The type of the length is determined by the |
| 174 | 174 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
| 175 | 175 |
///It is also possible to change the underlying priority heap. |
| 176 | 176 |
/// |
| 177 | 177 |
///There is also a \ref dijkstra() "function-type interface" for the |
| 178 | 178 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
| 179 | 179 |
///it can be used easier. |
| 180 | 180 |
/// |
| 181 | 181 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 182 | 182 |
///The default type is \ref ListDigraph. |
| 183 | 183 |
///\tparam LM A \ref concepts::ReadMap "readable" arc map that specifies |
| 184 | 184 |
///the lengths of the arcs. |
| 185 | 185 |
///It is read once for each arc, so the map may involve in |
| 186 | 186 |
///relatively time consuming process to compute the arc lengths if |
| 187 | 187 |
///it is necessary. The default map type is \ref |
| 188 | 188 |
///concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 189 | 189 |
#ifdef DOXYGEN |
| 190 | 190 |
template <typename GR, typename LM, typename TR> |
| 191 | 191 |
#else |
| 192 | 192 |
template <typename GR=ListDigraph, |
| 193 | 193 |
typename LM=typename GR::template ArcMap<int>, |
| 194 | 194 |
typename TR=DijkstraDefaultTraits<GR,LM> > |
| 195 | 195 |
#endif |
| 196 | 196 |
class Dijkstra {
|
| 197 | 197 |
public: |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_DIM2_H |
| 20 | 20 |
#define LEMON_DIM2_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iostream> |
| 23 | 23 |
|
| 24 | 24 |
///\ingroup misc |
| 25 | 25 |
///\file |
| 26 | 26 |
///\brief A simple two dimensional vector and a bounding box implementation |
| 27 | 27 |
/// |
| 28 | 28 |
/// The class \ref lemon::dim2::Point "dim2::Point" implements |
| 29 | 29 |
/// a two dimensional vector with the usual operations. |
| 30 | 30 |
/// |
| 31 | 31 |
/// The class \ref lemon::dim2::Box "dim2::Box" can be used to determine |
| 32 | 32 |
/// the rectangular bounding box of a set of |
| 33 | 33 |
/// \ref lemon::dim2::Point "dim2::Point"'s. |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
///Tools for handling two dimensional coordinates |
| 38 | 38 |
|
| 39 | 39 |
///This namespace is a storage of several |
| 40 | 40 |
///tools for handling two dimensional coordinates |
| 41 | 41 |
namespace dim2 {
|
| 42 | 42 |
|
| 43 | 43 |
/// \addtogroup misc |
| 44 | 44 |
/// @{
|
| 45 | 45 |
|
| 46 | 46 |
/// Two dimensional vector (plain vector) |
| 47 | 47 |
|
| 48 | 48 |
/// A simple two dimensional vector (plain vector) implementation |
| 49 | 49 |
/// with the usual vector operations. |
| 50 | 50 |
template<typename T> |
| 51 | 51 |
class Point {
|
| 52 | 52 |
|
| 53 | 53 |
public: |
| 54 | 54 |
|
| 55 | 55 |
typedef T Value; |
| 56 | 56 |
|
| 57 | 57 |
///First coordinate |
| 58 | 58 |
T x; |
| 59 | 59 |
///Second coordinate |
| 60 | 60 |
T y; |
| 61 | 61 |
|
| 62 | 62 |
///Default constructor |
| 63 | 63 |
Point() {}
|
| 64 | 64 |
|
| 65 | 65 |
///Construct an instance from coordinates |
| 66 | 66 |
Point(T a, T b) : x(a), y(b) { }
|
| 67 | 67 |
|
| 68 | 68 |
///Returns the dimension of the vector (i.e. returns 2). |
| 69 | 69 |
|
| 70 | 70 |
///The dimension of the vector. |
| 71 | 71 |
///This function always returns 2. |
| 72 | 72 |
int size() const { return 2; }
|
| 73 | 73 |
|
| 74 | 74 |
///Subscripting operator |
| 75 | 75 |
|
| 76 | 76 |
///\c p[0] is \c p.x and \c p[1] is \c p.y |
| 77 | 77 |
/// |
| 78 | 78 |
T& operator[](int idx) { return idx == 0 ? x : y; }
|
| 79 | 79 |
|
| 80 | 80 |
///Const subscripting operator |
| 81 | 81 |
|
| 82 | 82 |
///\c p[0] is \c p.x and \c p[1] is \c p.y |
| 83 | 83 |
/// |
| 84 | 84 |
const T& operator[](int idx) const { return idx == 0 ? x : y; }
|
| 85 | 85 |
|
| 86 | 86 |
///Conversion constructor |
| 87 | 87 |
template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
|
| 88 | 88 |
|
| 89 | 89 |
///Give back the square of the norm of the vector |
| 90 | 90 |
T normSquare() const {
|
| 91 | 91 |
return x*x+y*y; |
| 92 | 92 |
} |
| 93 | 93 |
|
| 94 | 94 |
///Increment the left hand side by \c u |
| 95 | 95 |
Point<T>& operator +=(const Point<T>& u) {
|
| 96 | 96 |
x += u.x; |
| 97 | 97 |
y += u.y; |
| 98 | 98 |
return *this; |
| 99 | 99 |
} |
| 100 | 100 |
|
| 101 | 101 |
///Decrement the left hand side by \c u |
| 102 | 102 |
Point<T>& operator -=(const Point<T>& u) {
|
| 103 | 103 |
x -= u.x; |
| 104 | 104 |
y -= u.y; |
| 105 | 105 |
return *this; |
| 106 | 106 |
} |
| 107 | 107 |
|
| 108 | 108 |
///Multiply the left hand side with a scalar |
| 109 | 109 |
Point<T>& operator *=(const T &u) {
|
| 110 | 110 |
x *= u; |
| 111 | 111 |
y *= u; |
| 112 | 112 |
return *this; |
| 113 | 113 |
} |
| 114 | 114 |
|
| 115 | 115 |
///Divide the left hand side by a scalar |
| 116 | 116 |
Point<T>& operator /=(const T &u) {
|
| 117 | 117 |
x /= u; |
| 118 | 118 |
y /= u; |
| 119 | 119 |
return *this; |
| 120 | 120 |
} |
| 121 | 121 |
|
| 122 | 122 |
///Return the scalar product of two vectors |
| 123 | 123 |
T operator *(const Point<T>& u) const {
|
| 124 | 124 |
return x*u.x+y*u.y; |
| 125 | 125 |
} |
| 126 | 126 |
|
| 127 | 127 |
///Return the sum of two vectors |
| 128 | 128 |
Point<T> operator+(const Point<T> &u) const {
|
| 129 | 129 |
Point<T> b=*this; |
| 130 | 130 |
return b+=u; |
| 131 | 131 |
} |
| 132 | 132 |
|
| 133 | 133 |
///Return the negative of the vector |
| 134 | 134 |
Point<T> operator-() const {
|
| 135 | 135 |
Point<T> b=*this; |
| 136 | 136 |
b.x=-b.x; b.y=-b.y; |
| 137 | 137 |
return b; |
| 138 | 138 |
} |
| 139 | 139 |
|
| 140 | 140 |
///Return the difference of two vectors |
| 141 | 141 |
Point<T> operator-(const Point<T> &u) const {
|
| 142 | 142 |
Point<T> b=*this; |
| 143 | 143 |
return b-=u; |
| 144 | 144 |
} |
| 145 | 145 |
|
| 146 | 146 |
///Return a vector multiplied by a scalar |
| 147 | 147 |
Point<T> operator*(const T &u) const {
|
| 148 | 148 |
Point<T> b=*this; |
| 149 | 149 |
return b*=u; |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
///Return a vector divided by a scalar |
| 153 | 153 |
Point<T> operator/(const T &u) const {
|
| 154 | 154 |
Point<T> b=*this; |
| 155 | 155 |
return b/=u; |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
///Test equality |
| 159 | 159 |
bool operator==(const Point<T> &u) const {
|
| 160 | 160 |
return (x==u.x) && (y==u.y); |
| 161 | 161 |
} |
| 162 | 162 |
|
| 163 | 163 |
///Test inequality |
| 164 | 164 |
bool operator!=(Point u) const {
|
| 165 | 165 |
return (x!=u.x) || (y!=u.y); |
| 166 | 166 |
} |
| 167 | 167 |
|
| 168 | 168 |
}; |
| 169 | 169 |
|
| 170 | 170 |
///Return a Point |
| 171 | 171 |
|
| 172 | 172 |
///Return a Point. |
| 173 | 173 |
///\relates Point |
| 174 | 174 |
template <typename T> |
| 175 | 175 |
inline Point<T> makePoint(const T& x, const T& y) {
|
| 176 | 176 |
return Point<T>(x, y); |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
///Return a vector multiplied by a scalar |
| 180 | 180 |
|
| 181 | 181 |
///Return a vector multiplied by a scalar. |
| 182 | 182 |
///\relates Point |
| 183 | 183 |
template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
|
| 184 | 184 |
return x*u; |
| 185 | 185 |
} |
| 186 | 186 |
|
| 187 | 187 |
///Read a plain vector from a stream |
| 188 | 188 |
|
| 189 | 189 |
///Read a plain vector from a stream. |
| 190 | 190 |
///\relates Point |
| 191 | 191 |
/// |
| 192 | 192 |
template<typename T> |
| 193 | 193 |
inline std::istream& operator>>(std::istream &is, Point<T> &z) {
|
| 194 | 194 |
char c; |
| 195 | 195 |
if (is >> c) {
|
| 196 | 196 |
if (c != '(') is.putback(c);
|
| 197 | 197 |
} else {
|
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_DIMACS_H |
| 20 | 20 |
#define LEMON_DIMACS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iostream> |
| 23 | 23 |
#include <string> |
| 24 | 24 |
#include <vector> |
| 25 | 25 |
#include <lemon/maps.h> |
| 26 | 26 |
#include <lemon/error.h> |
| 27 | 27 |
|
| 28 | 28 |
/// \ingroup dimacs_group |
| 29 | 29 |
/// \file |
| 30 | 30 |
/// \brief DIMACS file format reader. |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
/// \addtogroup dimacs_group |
| 35 | 35 |
/// @{
|
| 36 | 36 |
|
| 37 | 37 |
/// DIMACS file type descriptor. |
| 38 | 38 |
struct DimacsDescriptor |
| 39 | 39 |
{
|
| 40 | 40 |
///File type enum |
| 41 | 41 |
enum Type |
| 42 | 42 |
{
|
| 43 | 43 |
NONE, MIN, MAX, SP, MAT |
| 44 | 44 |
}; |
| 45 | 45 |
///The file type |
| 46 | 46 |
Type type; |
| 47 | 47 |
///The number of nodes in the graph |
| 48 | 48 |
int nodeNum; |
| 49 | 49 |
///The number of edges in the graph |
| 50 | 50 |
int edgeNum; |
| 51 | 51 |
int lineShift; |
| 52 | 52 |
/// Constructor. Sets the type to NONE. |
| 53 | 53 |
DimacsDescriptor() : type(NONE) {}
|
| 54 | 54 |
}; |
| 55 | 55 |
|
| 56 | 56 |
///Discover the type of a DIMACS file |
| 57 | 57 |
|
| 58 | 58 |
///It starts seeking the begining of the file for the problem type |
| 59 | 59 |
///and size info. The found data is returned in a special struct |
| 60 | 60 |
///that can be evaluated and passed to the appropriate reader |
| 61 | 61 |
///function. |
| 62 | 62 |
DimacsDescriptor dimacsType(std::istream& is) |
| 63 | 63 |
{
|
| 64 | 64 |
DimacsDescriptor r; |
| 65 | 65 |
std::string problem,str; |
| 66 | 66 |
char c; |
| 67 | 67 |
r.lineShift=0; |
| 68 | 68 |
while (is >> c) |
| 69 | 69 |
switch(c) |
| 70 | 70 |
{
|
| 71 | 71 |
case 'p': |
| 72 | 72 |
if(is >> problem >> r.nodeNum >> r.edgeNum) |
| 73 | 73 |
{
|
| 74 | 74 |
getline(is, str); |
| 75 | 75 |
r.lineShift++; |
| 76 | 76 |
if(problem=="min") r.type=DimacsDescriptor::MIN; |
| 77 | 77 |
else if(problem=="max") r.type=DimacsDescriptor::MAX; |
| 78 | 78 |
else if(problem=="sp") r.type=DimacsDescriptor::SP; |
| 79 | 79 |
else if(problem=="mat") r.type=DimacsDescriptor::MAT; |
| 80 | 80 |
else throw FormatError("Unknown problem type");
|
| 81 | 81 |
return r; |
| 82 | 82 |
} |
| 83 | 83 |
else |
| 84 | 84 |
{
|
| 85 | 85 |
throw FormatError("Missing or wrong problem type declaration.");
|
| 86 | 86 |
} |
| 87 | 87 |
break; |
| 88 | 88 |
case 'c': |
| 89 | 89 |
getline(is, str); |
| 90 | 90 |
r.lineShift++; |
| 91 | 91 |
break; |
| 92 | 92 |
default: |
| 93 | 93 |
throw FormatError("Unknown DIMACS declaration.");
|
| 94 | 94 |
} |
| 95 | 95 |
throw FormatError("Missing problem type declaration.");
|
| 96 | 96 |
} |
| 97 | 97 |
|
| 98 | 98 |
|
| 99 | 99 |
|
| 100 | 100 |
/// DIMACS minimum cost flow reader function. |
| 101 | 101 |
/// |
| 102 | 102 |
/// This function reads a minimum cost flow instance from DIMACS format, |
| 103 | 103 |
/// i.e. from a DIMACS file having a line starting with |
| 104 | 104 |
/// \code |
| 105 | 105 |
/// p min |
| 106 | 106 |
/// \endcode |
| 107 | 107 |
/// At the beginning, \c g is cleared by \c g.clear(). The supply |
| 108 | 108 |
/// amount of the nodes are written to \c supply (signed). The |
| 109 | 109 |
/// lower bounds, capacities and costs of the arcs are written to |
| 110 | 110 |
/// \c lower, \c capacity and \c cost. |
| 111 | 111 |
/// |
| 112 | 112 |
/// If the file type was previously evaluated by dimacsType(), then |
| 113 | 113 |
/// the descriptor struct should be given by the \c dest parameter. |
| 114 | 114 |
template <typename Digraph, typename LowerMap, |
| 115 | 115 |
typename CapacityMap, typename CostMap, |
| 116 | 116 |
typename SupplyMap> |
| 117 | 117 |
void readDimacsMin(std::istream& is, |
| 118 | 118 |
Digraph &g, |
| 119 | 119 |
LowerMap& lower, |
| 120 | 120 |
CapacityMap& capacity, |
| 121 | 121 |
CostMap& cost, |
| 122 | 122 |
SupplyMap& supply, |
| 123 | 123 |
DimacsDescriptor desc=DimacsDescriptor()) |
| 124 | 124 |
{
|
| 125 | 125 |
g.clear(); |
| 126 | 126 |
std::vector<typename Digraph::Node> nodes; |
| 127 | 127 |
typename Digraph::Arc e; |
| 128 | 128 |
std::string problem, str; |
| 129 | 129 |
char c; |
| 130 | 130 |
int i, j; |
| 131 | 131 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
| 132 | 132 |
if(desc.type!=DimacsDescriptor::MIN) |
| 133 | 133 |
throw FormatError("Problem type mismatch");
|
| 134 | 134 |
|
| 135 | 135 |
nodes.resize(desc.nodeNum + 1); |
| 136 | 136 |
for (int k = 1; k <= desc.nodeNum; ++k) {
|
| 137 | 137 |
nodes[k] = g.addNode(); |
| 138 | 138 |
supply.set(nodes[k], 0); |
| 139 | 139 |
} |
| 140 | 140 |
|
| 141 | 141 |
typename SupplyMap::Value sup; |
| 142 | 142 |
typename CapacityMap::Value low; |
| 143 | 143 |
typename CapacityMap::Value cap; |
| 144 | 144 |
typename CostMap::Value co; |
| 145 | 145 |
while (is >> c) {
|
| 146 | 146 |
switch (c) {
|
| 147 | 147 |
case 'c': // comment line |
| 148 | 148 |
getline(is, str); |
| 149 | 149 |
break; |
| 150 | 150 |
case 'n': // node definition line |
| 151 | 151 |
is >> i >> sup; |
| 152 | 152 |
getline(is, str); |
| 153 | 153 |
supply.set(nodes[i], sup); |
| 154 | 154 |
break; |
| 155 | 155 |
case 'a': // arc (arc) definition line |
| 156 | 156 |
is >> i >> j >> low >> cap >> co; |
| 157 | 157 |
getline(is, str); |
| 158 | 158 |
e = g.addArc(nodes[i], nodes[j]); |
| 159 | 159 |
lower.set(e, low); |
| 160 | 160 |
if (cap >= 0) |
| 161 | 161 |
capacity.set(e, cap); |
| 162 | 162 |
else |
| 163 | 163 |
capacity.set(e, -1); |
| 164 | 164 |
cost.set(e, co); |
| 165 | 165 |
break; |
| 166 | 166 |
} |
| 167 | 167 |
} |
| 168 | 168 |
} |
| 169 | 169 |
|
| 170 | 170 |
template<typename Digraph, typename CapacityMap> |
| 171 | 171 |
void _readDimacs(std::istream& is, |
| 172 | 172 |
Digraph &g, |
| 173 | 173 |
CapacityMap& capacity, |
| 174 | 174 |
typename Digraph::Node &s, |
| 175 | 175 |
typename Digraph::Node &t, |
| 176 | 176 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
| 177 | 177 |
g.clear(); |
| 178 | 178 |
s=t=INVALID; |
| 179 | 179 |
std::vector<typename Digraph::Node> nodes; |
| 180 | 180 |
typename Digraph::Arc e; |
| 181 | 181 |
char c, d; |
| 182 | 182 |
int i, j; |
| 183 | 183 |
typename CapacityMap::Value _cap; |
| 184 | 184 |
std::string str; |
| 185 | 185 |
nodes.resize(desc.nodeNum + 1); |
| 186 | 186 |
for (int k = 1; k <= desc.nodeNum; ++k) {
|
| 187 | 187 |
nodes[k] = g.addNode(); |
| 188 | 188 |
} |
| 189 | 189 |
|
| 190 | 190 |
while (is >> c) {
|
| 191 | 191 |
switch (c) {
|
| 192 | 192 |
case 'c': // comment line |
| 193 | 193 |
getline(is, str); |
| 194 | 194 |
break; |
| 195 | 195 |
case 'n': // node definition line |
| 196 | 196 |
if (desc.type==DimacsDescriptor::SP) { // shortest path problem
|
| 197 | 197 |
is >> i; |
| 198 | 198 |
getline(is, str); |
| 199 | 199 |
s = nodes[i]; |
| 200 | 200 |
} |
| 201 | 201 |
if (desc.type==DimacsDescriptor::MAX) { // max flow problem
|
| 202 | 202 |
is >> i >> d; |
| 203 | 203 |
getline(is, str); |
| 204 | 204 |
if (d == 's') s = nodes[i]; |
| 205 | 205 |
if (d == 't') t = nodes[i]; |
| 206 | 206 |
} |
| 207 | 207 |
break; |
| 208 | 208 |
case 'a': // arc (arc) definition line |
| 209 | 209 |
if (desc.type==DimacsDescriptor::SP || |
| 210 | 210 |
desc.type==DimacsDescriptor::MAX) {
|
| 211 | 211 |
is >> i >> j >> _cap; |
| 212 | 212 |
getline(is, str); |
| 213 | 213 |
e = g.addArc(nodes[i], nodes[j]); |
| 214 | 214 |
capacity.set(e, _cap); |
| 215 | 215 |
} else {
|
| 216 | 216 |
is >> i >> j; |
| 217 | 217 |
getline(is, str); |
| 218 | 218 |
g.addArc(nodes[i], nodes[j]); |
| 219 | 219 |
} |
| 220 | 220 |
break; |
| 221 | 221 |
} |
| 222 | 222 |
} |
| 223 | 223 |
} |
| 224 | 224 |
|
| 225 | 225 |
/// DIMACS maximum flow reader function. |
| 226 | 226 |
/// |
| 227 | 227 |
/// This function reads a maximum flow instance from DIMACS format, |
| 228 | 228 |
/// i.e. from a DIMACS file having a line starting with |
| 229 | 229 |
/// \code |
| 230 | 230 |
/// p max |
| 231 | 231 |
/// \endcode |
| 232 | 232 |
/// At the beginning, \c g is cleared by \c g.clear(). The arc |
| 233 | 233 |
/// capacities are written to \c capacity and \c s and \c t are |
| 234 | 234 |
/// set to the source and the target nodes. |
| 235 | 235 |
/// |
| 236 | 236 |
/// If the file type was previously evaluated by dimacsType(), then |
| 237 | 237 |
/// the descriptor struct should be given by the \c dest parameter. |
| 238 | 238 |
template<typename Digraph, typename CapacityMap> |
| 239 | 239 |
void readDimacsMax(std::istream& is, |
| 240 | 240 |
Digraph &g, |
| 241 | 241 |
CapacityMap& capacity, |
| 242 | 242 |
typename Digraph::Node &s, |
| 243 | 243 |
typename Digraph::Node &t, |
| 244 | 244 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
| 245 | 245 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
| 246 | 246 |
if(desc.type!=DimacsDescriptor::MAX) |
| 247 | 247 |
throw FormatError("Problem type mismatch");
|
| 248 | 248 |
_readDimacs(is,g,capacity,s,t,desc); |
| 249 | 249 |
} |
| 250 | 250 |
|
| 251 | 251 |
/// DIMACS shortest path reader function. |
| 252 | 252 |
/// |
| 253 | 253 |
/// This function reads a shortest path instance from DIMACS format, |
| 254 | 254 |
/// i.e. from a DIMACS file having a line starting with |
| 255 | 255 |
/// \code |
| 256 | 256 |
/// p sp |
| 257 | 257 |
/// \endcode |
| 258 | 258 |
/// At the beginning, \c g is cleared by \c g.clear(). The arc |
| 259 | 259 |
/// lengths are written to \c length and \c s is set to the |
| 260 | 260 |
/// source node. |
| 261 | 261 |
/// |
| 262 | 262 |
/// If the file type was previously evaluated by dimacsType(), then |
| 263 | 263 |
/// the descriptor struct should be given by the \c dest parameter. |
| 264 | 264 |
template<typename Digraph, typename LengthMap> |
| 265 | 265 |
void readDimacsSp(std::istream& is, |
| 266 | 266 |
Digraph &g, |
| 267 | 267 |
LengthMap& length, |
| 268 | 268 |
typename Digraph::Node &s, |
| 269 | 269 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
| 270 | 270 |
typename Digraph::Node t; |
| 271 | 271 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
| 272 | 272 |
if(desc.type!=DimacsDescriptor::SP) |
| 273 | 273 |
throw FormatError("Problem type mismatch");
|
| 274 | 274 |
_readDimacs(is, g, length, s, t,desc); |
| 275 | 275 |
} |
| 276 | 276 |
|
| 277 | 277 |
/// DIMACS capacitated digraph reader function. |
| 278 | 278 |
/// |
| 279 | 279 |
/// This function reads an arc capacitated digraph instance from |
| 280 | 280 |
/// DIMACS 'mat' or 'sp' format. |
| 281 | 281 |
/// At the beginning, \c g is cleared by \c g.clear() |
| 282 | 282 |
/// and the arc capacities/lengths are written to \c capacity. |
| 283 | 283 |
/// |
| 284 | 284 |
/// If the file type was previously evaluated by dimacsType(), then |
| 285 | 285 |
/// the descriptor struct should be given by the \c dest parameter. |
| 286 | 286 |
template<typename Digraph, typename CapacityMap> |
| 287 | 287 |
void readDimacsCap(std::istream& is, |
| 288 | 288 |
Digraph &g, |
| 289 | 289 |
CapacityMap& capacity, |
| 290 | 290 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
| 291 | 291 |
typename Digraph::Node u,v; |
| 292 | 292 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
| 293 | 293 |
if(desc.type!=DimacsDescriptor::MAX || desc.type!=DimacsDescriptor::SP) |
| 294 | 294 |
throw FormatError("Problem type mismatch");
|
| 295 | 295 |
_readDimacs(is, g, capacity, u, v, desc); |
| 296 | 296 |
} |
| 297 | 297 |
|
| 298 | 298 |
/// DIMACS plain digraph reader function. |
| 299 | 299 |
/// |
| 300 | 300 |
/// This function reads a digraph without any designated nodes and |
| 301 | 301 |
/// maps from DIMACS format, i.e. from DIMACS files having a line |
| 302 | 302 |
/// starting with |
| 303 | 303 |
/// \code |
| 304 | 304 |
/// p mat |
| 305 | 305 |
/// \endcode |
| 306 | 306 |
/// At the beginning, \c g is cleared by \c g.clear(). |
| 307 | 307 |
/// |
| 308 | 308 |
/// If the file type was previously evaluated by dimacsType(), then |
| 309 | 309 |
/// the descriptor struct should be given by the \c dest parameter. |
| 310 | 310 |
template<typename Digraph> |
| 311 | 311 |
void readDimacsMat(std::istream& is, Digraph &g, |
| 312 | 312 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
| 313 | 313 |
typename Digraph::Node u,v; |
| 314 | 314 |
NullMap<typename Digraph::Arc, int> n; |
| 315 | 315 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
| 316 | 316 |
if(desc.type!=DimacsDescriptor::MAT) |
| 317 | 317 |
throw FormatError("Problem type mismatch");
|
| 318 | 318 |
_readDimacs(is, g, n, u, v, desc); |
| 319 | 319 |
} |
| 320 | 320 |
|
| 321 | 321 |
/// DIMACS plain digraph writer function. |
| 322 | 322 |
/// |
| 323 | 323 |
/// This function writes a digraph without any designated nodes and |
| 324 | 324 |
/// maps into DIMACS format, i.e. into DIMACS file having a line |
| 325 | 325 |
/// starting with |
| 326 | 326 |
/// \code |
| 327 | 327 |
/// p mat |
| 328 | 328 |
/// \endcode |
| 329 | 329 |
/// If \c comment is not empty, then it will be printed in the first line |
| 330 | 330 |
/// prefixed by 'c'. |
| 331 | 331 |
template<typename Digraph> |
| 332 | 332 |
void writeDimacsMat(std::ostream& os, const Digraph &g, |
| 333 | 333 |
std::string comment="") {
|
| 334 | 334 |
typedef typename Digraph::NodeIt NodeIt; |
| 335 | 335 |
typedef typename Digraph::ArcIt ArcIt; |
| 336 | 336 |
|
| 337 |
if(!comment.empty()) |
|
| 337 |
if(!comment.empty()) |
|
| 338 | 338 |
os << "c " << comment << std::endl; |
| 339 | 339 |
os << "p mat " << g.nodeNum() << " " << g.arcNum() << std::endl; |
| 340 | 340 |
|
| 341 | 341 |
typename Digraph::template NodeMap<int> nodes(g); |
| 342 | 342 |
int i = 1; |
| 343 | 343 |
for(NodeIt v(g); v != INVALID; ++v) {
|
| 344 | 344 |
nodes.set(v, i); |
| 345 | 345 |
++i; |
| 346 | 346 |
} |
| 347 | 347 |
for(ArcIt e(g); e != INVALID; ++e) {
|
| 348 | 348 |
os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)] |
| 349 | 349 |
<< std::endl; |
| 350 | 350 |
} |
| 351 | 351 |
} |
| 352 | 352 |
|
| 353 | 353 |
/// @} |
| 354 | 354 |
|
| 355 | 355 |
} //namespace lemon |
| 356 | 356 |
|
| 357 | 357 |
#endif //LEMON_DIMACS_H |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_ELEVATOR_H |
| 20 | 20 |
#define LEMON_ELEVATOR_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup auxdat |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Elevator class |
| 25 | 25 |
/// |
| 26 | 26 |
///Elevator class implements an efficient data structure |
| 27 | 27 |
///for labeling items in push-relabel type algorithms. |
| 28 | 28 |
/// |
| 29 | 29 |
|
| 30 | 30 |
#include <lemon/bits/traits.h> |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
///Class for handling "labels" in push-relabel type algorithms. |
| 35 | 35 |
|
| 36 | 36 |
///A class for handling "labels" in push-relabel type algorithms. |
| 37 | 37 |
/// |
| 38 | 38 |
///\ingroup auxdat |
| 39 | 39 |
///Using this class you can assign "labels" (nonnegative integer numbers) |
| 40 | 40 |
///to the edges or nodes of a graph, manipulate and query them through |
| 41 | 41 |
///operations typically arising in "push-relabel" type algorithms. |
| 42 | 42 |
/// |
| 43 | 43 |
///Each item is either \em active or not, and you can also choose a |
| 44 | 44 |
///highest level active item. |
| 45 | 45 |
/// |
| 46 | 46 |
///\sa LinkedElevator |
| 47 | 47 |
/// |
| 48 | 48 |
///\param Graph Type of the underlying graph. |
| 49 | 49 |
///\param Item Type of the items the data is assigned to (Graph::Node, |
| 50 | 50 |
///Graph::Arc, Graph::Edge). |
| 51 | 51 |
template<class Graph, class Item> |
| 52 | 52 |
class Elevator |
| 53 | 53 |
{
|
| 54 | 54 |
public: |
| 55 | 55 |
|
| 56 | 56 |
typedef Item Key; |
| 57 | 57 |
typedef int Value; |
| 58 | 58 |
|
| 59 | 59 |
private: |
| 60 | 60 |
|
| 61 | 61 |
typedef Item *Vit; |
| 62 | 62 |
typedef typename ItemSetTraits<Graph,Item>::template Map<Vit>::Type VitMap; |
| 63 | 63 |
typedef typename ItemSetTraits<Graph,Item>::template Map<int>::Type IntMap; |
| 64 | 64 |
|
| 65 | 65 |
const Graph &_g; |
| 66 | 66 |
int _max_level; |
| 67 | 67 |
int _item_num; |
| 68 | 68 |
VitMap _where; |
| 69 | 69 |
IntMap _level; |
| 70 | 70 |
std::vector<Item> _items; |
| 71 | 71 |
std::vector<Vit> _first; |
| 72 | 72 |
std::vector<Vit> _last_active; |
| 73 | 73 |
|
| 74 | 74 |
int _highest_active; |
| 75 | 75 |
|
| 76 | 76 |
void copy(Item i, Vit p) |
| 77 | 77 |
{
|
| 78 | 78 |
_where.set(*p=i,p); |
| 79 | 79 |
} |
| 80 | 80 |
void copy(Vit s, Vit p) |
| 81 | 81 |
{
|
| 82 | 82 |
if(s!=p) |
| 83 | 83 |
{
|
| 84 | 84 |
Item i=*s; |
| 85 | 85 |
*p=i; |
| 86 | 86 |
_where.set(i,p); |
| 87 | 87 |
} |
| 88 | 88 |
} |
| 89 | 89 |
void swap(Vit i, Vit j) |
| 90 | 90 |
{
|
| 91 | 91 |
Item ti=*i; |
| 92 | 92 |
Vit ct = _where[ti]; |
| 93 | 93 |
_where.set(ti,_where[*i=*j]); |
| 94 | 94 |
_where.set(*j,ct); |
| 95 | 95 |
*j=ti; |
| 96 | 96 |
} |
| 97 | 97 |
|
| 98 | 98 |
public: |
| 99 | 99 |
|
| 100 | 100 |
///Constructor with given maximum level. |
| 101 | 101 |
|
| 102 | 102 |
///Constructor with given maximum level. |
| 103 | 103 |
/// |
| 104 | 104 |
///\param graph The underlying graph. |
| 105 | 105 |
///\param max_level The maximum allowed level. |
| 106 | 106 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>. |
| 107 | 107 |
Elevator(const Graph &graph,int max_level) : |
| 108 | 108 |
_g(graph), |
| 109 | 109 |
_max_level(max_level), |
| 110 | 110 |
_item_num(_max_level), |
| 111 | 111 |
_where(graph), |
| 112 | 112 |
_level(graph,0), |
| 113 | 113 |
_items(_max_level), |
| 114 | 114 |
_first(_max_level+2), |
| 115 | 115 |
_last_active(_max_level+2), |
| 116 | 116 |
_highest_active(-1) {}
|
| 117 | 117 |
///Constructor. |
| 118 | 118 |
|
| 119 | 119 |
///Constructor. |
| 120 | 120 |
/// |
| 121 | 121 |
///\param graph The underlying graph. |
| 122 | 122 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>, |
| 123 | 123 |
///where \c max_level is equal to the number of labeled items in the graph. |
| 124 | 124 |
Elevator(const Graph &graph) : |
| 125 | 125 |
_g(graph), |
| 126 | 126 |
_max_level(countItems<Graph, Item>(graph)), |
| 127 | 127 |
_item_num(_max_level), |
| 128 | 128 |
_where(graph), |
| 129 | 129 |
_level(graph,0), |
| 130 | 130 |
_items(_max_level), |
| 131 | 131 |
_first(_max_level+2), |
| 132 | 132 |
_last_active(_max_level+2), |
| 133 | 133 |
_highest_active(-1) |
| 134 | 134 |
{
|
| 135 | 135 |
} |
| 136 | 136 |
|
| 137 | 137 |
///Activate item \c i. |
| 138 | 138 |
|
| 139 | 139 |
///Activate item \c i. |
| 140 | 140 |
///\pre Item \c i shouldn't be active before. |
| 141 | 141 |
void activate(Item i) |
| 142 | 142 |
{
|
| 143 | 143 |
const int l=_level[i]; |
| 144 | 144 |
swap(_where[i],++_last_active[l]); |
| 145 | 145 |
if(l>_highest_active) _highest_active=l; |
| 146 | 146 |
} |
| 147 | 147 |
|
| 148 | 148 |
///Deactivate item \c i. |
| 149 | 149 |
|
| 150 | 150 |
///Deactivate item \c i. |
| 151 | 151 |
///\pre Item \c i must be active before. |
| 152 | 152 |
void deactivate(Item i) |
| 153 | 153 |
{
|
| 154 | 154 |
swap(_where[i],_last_active[_level[i]]--); |
| 155 | 155 |
while(_highest_active>=0 && |
| 156 | 156 |
_last_active[_highest_active]<_first[_highest_active]) |
| 157 | 157 |
_highest_active--; |
| 158 | 158 |
} |
| 159 | 159 |
|
| 160 | 160 |
///Query whether item \c i is active |
| 161 | 161 |
bool active(Item i) const { return _where[i]<=_last_active[_level[i]]; }
|
| 162 | 162 |
|
| 163 | 163 |
///Return the level of item \c i. |
| 164 | 164 |
int operator[](Item i) const { return _level[i]; }
|
| 165 | 165 |
|
| 166 | 166 |
///Return the number of items on level \c l. |
| 167 | 167 |
int onLevel(int l) const |
| 168 | 168 |
{
|
| 169 | 169 |
return _first[l+1]-_first[l]; |
| 170 | 170 |
} |
| 171 | 171 |
///Return true if level \c l is empty. |
| 172 | 172 |
bool emptyLevel(int l) const |
| 173 | 173 |
{
|
| 174 | 174 |
return _first[l+1]-_first[l]==0; |
| 175 | 175 |
} |
| 176 | 176 |
///Return the number of items above level \c l. |
| 177 | 177 |
int aboveLevel(int l) const |
| 178 | 178 |
{
|
| 179 | 179 |
return _first[_max_level+1]-_first[l+1]; |
| 180 | 180 |
} |
| 181 | 181 |
///Return the number of active items on level \c l. |
| 182 | 182 |
int activesOnLevel(int l) const |
| 183 | 183 |
{
|
| 184 | 184 |
return _last_active[l]-_first[l]+1; |
| 185 | 185 |
} |
| 186 | 186 |
///Return true if there is no active item on level \c l. |
| 187 | 187 |
bool activeFree(int l) const |
| 188 | 188 |
{
|
| 189 | 189 |
return _last_active[l]<_first[l]; |
| 190 | 190 |
} |
| 191 | 191 |
///Return the maximum allowed level. |
| 192 | 192 |
int maxLevel() const |
| 193 | 193 |
{
|
| 194 | 194 |
return _max_level; |
| 195 | 195 |
} |
| 196 | 196 |
|
| 197 | 197 |
///\name Highest Active Item |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_ERROR_H |
| 20 | 20 |
#define LEMON_ERROR_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup exceptions |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Basic exception classes and error handling. |
| 25 | 25 |
|
| 26 | 26 |
#include <exception> |
| 27 | 27 |
#include <string> |
| 28 | 28 |
#include <sstream> |
| 29 | 29 |
#include <iostream> |
| 30 | 30 |
#include <cstdlib> |
| 31 | 31 |
#include <memory> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
/// \addtogroup exceptions |
| 36 | 36 |
/// @{
|
| 37 | 37 |
|
| 38 | 38 |
/// \brief Generic exception class. |
| 39 | 39 |
/// |
| 40 | 40 |
/// Base class for exceptions used in LEMON. |
| 41 | 41 |
/// |
| 42 | 42 |
class Exception : public std::exception {
|
| 43 | 43 |
public: |
| 44 | 44 |
///Constructor |
| 45 | 45 |
Exception() throw() {}
|
| 46 | 46 |
///Virtual destructor |
| 47 | 47 |
virtual ~Exception() throw() {}
|
| 48 | 48 |
///A short description of the exception |
| 49 | 49 |
virtual const char* what() const throw() {
|
| 50 | 50 |
return "lemon::Exception"; |
| 51 | 51 |
} |
| 52 | 52 |
}; |
| 53 | 53 |
|
| 54 | 54 |
/// \brief Input-Output error |
| 55 | 55 |
/// |
| 56 | 56 |
/// This exception is thrown when a file operation cannot be |
| 57 | 57 |
/// succeeded. |
| 58 | 58 |
class IoError : public Exception {
|
| 59 | 59 |
protected: |
| 60 | 60 |
std::string _message; |
| 61 | 61 |
std::string _file; |
| 62 | 62 |
|
| 63 | 63 |
mutable std::string _what; |
| 64 | 64 |
public: |
| 65 | 65 |
|
| 66 | 66 |
/// Copy constructor |
| 67 | 67 |
IoError(const IoError &error) throw() : Exception() {
|
| 68 | 68 |
message(error._message); |
| 69 | 69 |
file(error._file); |
| 70 | 70 |
} |
| 71 | 71 |
|
| 72 | 72 |
/// Constructor |
| 73 | 73 |
explicit IoError(const char *message) throw() {
|
| 74 | 74 |
IoError::message(message); |
| 75 | 75 |
} |
| 76 | 76 |
|
| 77 | 77 |
/// Constructor |
| 78 | 78 |
explicit IoError(const std::string &message) throw() {
|
| 79 | 79 |
IoError::message(message); |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 | 82 |
/// Constructor |
| 83 | 83 |
explicit IoError(const char *message, |
| 84 | 84 |
const std::string &file) throw() {
|
| 85 | 85 |
IoError::message(message); |
| 86 | 86 |
IoError::file(file); |
| 87 | 87 |
} |
| 88 | 88 |
|
| 89 | 89 |
/// Constructor |
| 90 | 90 |
explicit IoError(const std::string &message, |
| 91 | 91 |
const std::string &file) throw() {
|
| 92 | 92 |
IoError::message(message); |
| 93 | 93 |
IoError::file(file); |
| 94 | 94 |
} |
| 95 | 95 |
|
| 96 | 96 |
/// Virtual destructor |
| 97 | 97 |
virtual ~IoError() throw() {}
|
| 98 | 98 |
|
| 99 | 99 |
/// Set the error message |
| 100 | 100 |
void message(const char *message) throw() {
|
| 101 | 101 |
try {
|
| 102 | 102 |
_message = message; |
| 103 | 103 |
} catch (...) {}
|
| 104 | 104 |
} |
| 105 | 105 |
|
| 106 | 106 |
/// Set the error message |
| 107 | 107 |
void message(const std::string& message) throw() {
|
| 108 | 108 |
try {
|
| 109 | 109 |
_message = message; |
| 110 | 110 |
} catch (...) {}
|
| 111 | 111 |
} |
| 112 | 112 |
|
| 113 | 113 |
/// Set the file name |
| 114 | 114 |
void file(const std::string &file) throw() {
|
| 115 | 115 |
try {
|
| 116 | 116 |
_file = file; |
| 117 | 117 |
} catch (...) {}
|
| 118 | 118 |
} |
| 119 | 119 |
|
| 120 | 120 |
/// Returns the error message |
| 121 | 121 |
const std::string& message() const throw() {
|
| 122 | 122 |
return _message; |
| 123 | 123 |
} |
| 124 | 124 |
|
| 125 | 125 |
/// \brief Returns the filename |
| 126 | 126 |
/// |
| 127 | 127 |
/// Returns the filename or an empty string if it was not specified. |
| 128 | 128 |
const std::string& file() const throw() {
|
| 129 | 129 |
return _file; |
| 130 | 130 |
} |
| 131 | 131 |
|
| 132 | 132 |
/// \brief Returns a short error message |
| 133 | 133 |
/// |
| 134 | 134 |
/// Returns a short error message which contains the message and the |
| 135 | 135 |
/// file name. |
| 136 | 136 |
virtual const char* what() const throw() {
|
| 137 | 137 |
try {
|
| 138 | 138 |
_what.clear(); |
| 139 | 139 |
std::ostringstream oss; |
| 140 | 140 |
oss << "lemon:IoError" << ": "; |
| 141 | 141 |
oss << _message; |
| 142 | 142 |
if (!_file.empty()) {
|
| 143 | 143 |
oss << " ('" << _file << "')";
|
| 144 | 144 |
} |
| 145 | 145 |
_what = oss.str(); |
| 146 | 146 |
} |
| 147 | 147 |
catch (...) {}
|
| 148 | 148 |
if (!_what.empty()) return _what.c_str(); |
| 149 | 149 |
else return "lemon:IoError"; |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
}; |
| 153 | 153 |
|
| 154 | 154 |
/// \brief Format error |
| 155 | 155 |
/// |
| 156 | 156 |
/// This exception is thrown when an input file has wrong |
| 157 | 157 |
/// format or a data representation is not legal. |
| 158 | 158 |
class FormatError : public Exception {
|
| 159 | 159 |
protected: |
| 160 | 160 |
std::string _message; |
| 161 | 161 |
std::string _file; |
| 162 | 162 |
int _line; |
| 163 | 163 |
|
| 164 | 164 |
mutable std::string _what; |
| 165 | 165 |
public: |
| 166 | 166 |
|
| 167 | 167 |
/// Copy constructor |
| 168 | 168 |
FormatError(const FormatError &error) throw() : Exception() {
|
| 169 | 169 |
message(error._message); |
| 170 | 170 |
file(error._file); |
| 171 | 171 |
line(error._line); |
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
/// Constructor |
| 175 | 175 |
explicit FormatError(const char *message) throw() {
|
| 176 | 176 |
FormatError::message(message); |
| 177 | 177 |
_line = 0; |
| 178 | 178 |
} |
| 179 | 179 |
|
| 180 | 180 |
/// Constructor |
| 181 | 181 |
explicit FormatError(const std::string &message) throw() {
|
| 182 | 182 |
FormatError::message(message); |
| 183 | 183 |
_line = 0; |
| 184 | 184 |
} |
| 185 | 185 |
|
| 186 | 186 |
/// Constructor |
| 187 | 187 |
explicit FormatError(const char *message, |
| 188 | 188 |
const std::string &file, int line = 0) throw() {
|
| 189 | 189 |
FormatError::message(message); |
| 190 | 190 |
FormatError::file(file); |
| 191 | 191 |
FormatError::line(line); |
| 192 | 192 |
} |
| 193 | 193 |
|
| 194 | 194 |
/// Constructor |
| 195 | 195 |
explicit FormatError(const std::string &message, |
| 196 | 196 |
const std::string &file, int line = 0) throw() {
|
| 197 | 197 |
FormatError::message(message); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_FULL_GRAPH_H |
| 20 | 20 |
#define LEMON_FULL_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/bits/graph_extender.h> |
| 24 | 24 |
|
| 25 | 25 |
///\ingroup graphs |
| 26 | 26 |
///\file |
| 27 | 27 |
///\brief FullGraph and FullDigraph classes. |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
class FullDigraphBase {
|
| 32 | 32 |
public: |
| 33 | 33 |
|
| 34 | 34 |
typedef FullDigraphBase Graph; |
| 35 | 35 |
|
| 36 | 36 |
class Node; |
| 37 | 37 |
class Arc; |
| 38 | 38 |
|
| 39 | 39 |
protected: |
| 40 | 40 |
|
| 41 | 41 |
int _node_num; |
| 42 | 42 |
int _arc_num; |
| 43 | 43 |
|
| 44 | 44 |
FullDigraphBase() {}
|
| 45 | 45 |
|
| 46 | 46 |
void construct(int n) { _node_num = n; _arc_num = n * n; }
|
| 47 | 47 |
|
| 48 | 48 |
public: |
| 49 | 49 |
|
| 50 | 50 |
typedef True NodeNumTag; |
| 51 | 51 |
typedef True ArcNumTag; |
| 52 | 52 |
|
| 53 | 53 |
Node operator()(int ix) const { return Node(ix); }
|
| 54 | 54 |
int index(const Node& node) const { return node._id; }
|
| 55 | 55 |
|
| 56 | 56 |
Arc arc(const Node& s, const Node& t) const {
|
| 57 | 57 |
return Arc(s._id * _node_num + t._id); |
| 58 | 58 |
} |
| 59 | 59 |
|
| 60 | 60 |
int nodeNum() const { return _node_num; }
|
| 61 | 61 |
int arcNum() const { return _arc_num; }
|
| 62 | 62 |
|
| 63 | 63 |
int maxNodeId() const { return _node_num - 1; }
|
| 64 | 64 |
int maxArcId() const { return _arc_num - 1; }
|
| 65 | 65 |
|
| 66 | 66 |
Node source(Arc arc) const { return arc._id / _node_num; }
|
| 67 | 67 |
Node target(Arc arc) const { return arc._id % _node_num; }
|
| 68 | 68 |
|
| 69 | 69 |
static int id(Node node) { return node._id; }
|
| 70 | 70 |
static int id(Arc arc) { return arc._id; }
|
| 71 | 71 |
|
| 72 | 72 |
static Node nodeFromId(int id) { return Node(id);}
|
| 73 | 73 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 74 | 74 |
|
| 75 | 75 |
typedef True FindArcTag; |
| 76 | 76 |
|
| 77 | 77 |
Arc findArc(Node s, Node t, Arc prev = INVALID) const {
|
| 78 | 78 |
return prev == INVALID ? arc(s, t) : INVALID; |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
class Node {
|
| 82 | 82 |
friend class FullDigraphBase; |
| 83 | 83 |
|
| 84 | 84 |
protected: |
| 85 | 85 |
int _id; |
| 86 | 86 |
Node(int id) : _id(id) {}
|
| 87 | 87 |
public: |
| 88 | 88 |
Node() {}
|
| 89 | 89 |
Node (Invalid) : _id(-1) {}
|
| 90 | 90 |
bool operator==(const Node node) const {return _id == node._id;}
|
| 91 | 91 |
bool operator!=(const Node node) const {return _id != node._id;}
|
| 92 | 92 |
bool operator<(const Node node) const {return _id < node._id;}
|
| 93 | 93 |
}; |
| 94 | 94 |
|
| 95 | 95 |
class Arc {
|
| 96 | 96 |
friend class FullDigraphBase; |
| 97 | 97 |
|
| 98 | 98 |
protected: |
| 99 | 99 |
int _id; // _node_num * source + target; |
| 100 | 100 |
|
| 101 | 101 |
Arc(int id) : _id(id) {}
|
| 102 | 102 |
|
| 103 | 103 |
public: |
| 104 | 104 |
Arc() { }
|
| 105 | 105 |
Arc (Invalid) { _id = -1; }
|
| 106 | 106 |
bool operator==(const Arc arc) const {return _id == arc._id;}
|
| 107 | 107 |
bool operator!=(const Arc arc) const {return _id != arc._id;}
|
| 108 | 108 |
bool operator<(const Arc arc) const {return _id < arc._id;}
|
| 109 | 109 |
}; |
| 110 | 110 |
|
| 111 | 111 |
void first(Node& node) const {
|
| 112 | 112 |
node._id = _node_num - 1; |
| 113 | 113 |
} |
| 114 | 114 |
|
| 115 | 115 |
static void next(Node& node) {
|
| 116 | 116 |
--node._id; |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
void first(Arc& arc) const {
|
| 120 | 120 |
arc._id = _arc_num - 1; |
| 121 | 121 |
} |
| 122 | 122 |
|
| 123 | 123 |
static void next(Arc& arc) {
|
| 124 | 124 |
--arc._id; |
| 125 | 125 |
} |
| 126 | 126 |
|
| 127 | 127 |
void firstOut(Arc& arc, const Node& node) const {
|
| 128 | 128 |
arc._id = (node._id + 1) * _node_num - 1; |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
void nextOut(Arc& arc) const {
|
| 132 | 132 |
if (arc._id % _node_num == 0) arc._id = 0; |
| 133 | 133 |
--arc._id; |
| 134 | 134 |
} |
| 135 | 135 |
|
| 136 | 136 |
void firstIn(Arc& arc, const Node& node) const {
|
| 137 | 137 |
arc._id = _arc_num + node._id - _node_num; |
| 138 | 138 |
} |
| 139 | 139 |
|
| 140 | 140 |
void nextIn(Arc& arc) const {
|
| 141 | 141 |
arc._id -= _node_num; |
| 142 | 142 |
if (arc._id < 0) arc._id = -1; |
| 143 | 143 |
} |
| 144 | 144 |
|
| 145 | 145 |
}; |
| 146 | 146 |
|
| 147 | 147 |
typedef DigraphExtender<FullDigraphBase> ExtendedFullDigraphBase; |
| 148 | 148 |
|
| 149 | 149 |
/// \ingroup graphs |
| 150 | 150 |
/// |
| 151 | 151 |
/// \brief A full digraph class. |
| 152 | 152 |
/// |
| 153 | 153 |
/// This is a simple and fast directed full graph implementation. |
| 154 | 154 |
/// From each node go arcs to each node (including the source node), |
| 155 | 155 |
/// therefore the number of the arcs in the digraph is the square of |
| 156 | 156 |
/// the node number. This digraph type is completely static, so you |
| 157 | 157 |
/// can neither add nor delete either arcs or nodes, and it needs |
| 158 | 158 |
/// constant space in memory. |
| 159 | 159 |
/// |
| 160 | 160 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept |
| 161 | 161 |
/// and it also has an important extra feature that its maps are |
| 162 | 162 |
/// real \ref concepts::ReferenceMap "reference map"s. |
| 163 | 163 |
/// |
| 164 | 164 |
/// The \c FullDigraph and \c FullGraph classes are very similar, |
| 165 | 165 |
/// but there are two differences. While this class conforms only |
| 166 | 166 |
/// to the \ref concepts::Digraph "Digraph" concept, the \c FullGraph |
| 167 | 167 |
/// class conforms to the \ref concepts::Graph "Graph" concept, |
| 168 | 168 |
/// moreover \c FullGraph does not contain a loop arc for each |
| 169 | 169 |
/// node as \c FullDigraph does. |
| 170 | 170 |
/// |
| 171 | 171 |
/// \sa FullGraph |
| 172 | 172 |
class FullDigraph : public ExtendedFullDigraphBase {
|
| 173 | 173 |
public: |
| 174 | 174 |
|
| 175 | 175 |
typedef ExtendedFullDigraphBase Parent; |
| 176 | 176 |
|
| 177 | 177 |
/// \brief Constructor |
| 178 | 178 |
FullDigraph() { construct(0); }
|
| 179 | 179 |
|
| 180 | 180 |
/// \brief Constructor |
| 181 | 181 |
/// |
| 182 | 182 |
/// Constructor. |
| 183 | 183 |
/// \param n The number of the nodes. |
| 184 | 184 |
FullDigraph(int n) { construct(n); }
|
| 185 | 185 |
|
| 186 | 186 |
/// \brief Resizes the digraph |
| 187 | 187 |
/// |
| 188 | 188 |
/// Resizes the digraph. The function will fully destroy and |
| 189 | 189 |
/// rebuild the digraph. This cause that the maps of the digraph will |
| 190 | 190 |
/// reallocated automatically and the previous values will be lost. |
| 191 | 191 |
void resize(int n) {
|
| 192 | 192 |
Parent::notifier(Arc()).clear(); |
| 193 | 193 |
Parent::notifier(Node()).clear(); |
| 194 | 194 |
construct(n); |
| 195 | 195 |
Parent::notifier(Node()).build(); |
| 196 | 196 |
Parent::notifier(Arc()).build(); |
| 197 | 197 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_GRAPH_TO_EPS_H |
| 20 | 20 |
#define LEMON_GRAPH_TO_EPS_H |
| 21 | 21 |
|
| 22 | 22 |
#include<iostream> |
| 23 | 23 |
#include<fstream> |
| 24 | 24 |
#include<sstream> |
| 25 | 25 |
#include<algorithm> |
| 26 | 26 |
#include<vector> |
| 27 | 27 |
|
| 28 | 28 |
#ifndef WIN32 |
| 29 | 29 |
#include<sys/time.h> |
| 30 | 30 |
#include<ctime> |
| 31 | 31 |
#else |
| 32 | 32 |
#define WIN32_LEAN_AND_MEAN |
| 33 | 33 |
#define NOMINMAX |
| 34 | 34 |
#include<windows.h> |
| 35 | 35 |
#endif |
| 36 | 36 |
|
| 37 | 37 |
#include<lemon/math.h> |
| 38 | 38 |
#include<lemon/core.h> |
| 39 | 39 |
#include<lemon/dim2.h> |
| 40 | 40 |
#include<lemon/maps.h> |
| 41 | 41 |
#include<lemon/color.h> |
| 42 | 42 |
#include<lemon/bits/bezier.h> |
| 43 | 43 |
#include<lemon/error.h> |
| 44 | 44 |
|
| 45 | 45 |
|
| 46 | 46 |
///\ingroup eps_io |
| 47 | 47 |
///\file |
| 48 | 48 |
///\brief A well configurable tool for visualizing graphs |
| 49 | 49 |
|
| 50 | 50 |
namespace lemon {
|
| 51 | 51 |
|
| 52 | 52 |
namespace _graph_to_eps_bits {
|
| 53 | 53 |
template<class MT> |
| 54 | 54 |
class _NegY {
|
| 55 | 55 |
public: |
| 56 | 56 |
typedef typename MT::Key Key; |
| 57 | 57 |
typedef typename MT::Value Value; |
| 58 | 58 |
const MT ↦ |
| 59 | 59 |
int yscale; |
| 60 | 60 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {}
|
| 61 | 61 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);}
|
| 62 | 62 |
}; |
| 63 | 63 |
} |
| 64 | 64 |
|
| 65 | 65 |
///Default traits class of GraphToEps |
| 66 | 66 |
|
| 67 | 67 |
///Default traits class of \ref GraphToEps. |
| 68 | 68 |
/// |
| 69 | 69 |
///\c G is the type of the underlying graph. |
| 70 | 70 |
template<class G> |
| 71 | 71 |
struct DefaultGraphToEpsTraits |
| 72 | 72 |
{
|
| 73 | 73 |
typedef G Graph; |
| 74 | 74 |
typedef typename Graph::Node Node; |
| 75 | 75 |
typedef typename Graph::NodeIt NodeIt; |
| 76 | 76 |
typedef typename Graph::Arc Arc; |
| 77 | 77 |
typedef typename Graph::ArcIt ArcIt; |
| 78 | 78 |
typedef typename Graph::InArcIt InArcIt; |
| 79 | 79 |
typedef typename Graph::OutArcIt OutArcIt; |
| 80 | 80 |
|
| 81 | 81 |
|
| 82 | 82 |
const Graph &g; |
| 83 | 83 |
|
| 84 | 84 |
std::ostream& os; |
| 85 | 85 |
|
| 86 | 86 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
| 87 | 87 |
CoordsMapType _coords; |
| 88 | 88 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
| 89 | 89 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
| 90 | 90 |
|
| 91 | 91 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
| 92 | 92 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
| 93 | 93 |
|
| 94 | 94 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
| 95 | 95 |
|
| 96 | 96 |
double _arcWidthScale; |
| 97 | 97 |
|
| 98 | 98 |
double _nodeScale; |
| 99 | 99 |
double _xBorder, _yBorder; |
| 100 | 100 |
double _scale; |
| 101 | 101 |
double _nodeBorderQuotient; |
| 102 | 102 |
|
| 103 | 103 |
bool _drawArrows; |
| 104 | 104 |
double _arrowLength, _arrowWidth; |
| 105 | 105 |
|
| 106 | 106 |
bool _showNodes, _showArcs; |
| 107 | 107 |
|
| 108 | 108 |
bool _enableParallel; |
| 109 | 109 |
double _parArcDist; |
| 110 | 110 |
|
| 111 | 111 |
bool _showNodeText; |
| 112 | 112 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
| 113 | 113 |
double _nodeTextSize; |
| 114 | 114 |
|
| 115 | 115 |
bool _showNodePsText; |
| 116 | 116 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
| 117 | 117 |
char *_nodePsTextsPreamble; |
| 118 | 118 |
|
| 119 | 119 |
bool _undirected; |
| 120 | 120 |
|
| 121 | 121 |
bool _pleaseRemoveOsStream; |
| 122 | 122 |
|
| 123 | 123 |
bool _scaleToA4; |
| 124 | 124 |
|
| 125 | 125 |
std::string _title; |
| 126 | 126 |
std::string _copyright; |
| 127 | 127 |
|
| 128 | 128 |
enum NodeTextColorType |
| 129 | 129 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
|
| 130 | 130 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
| 131 | 131 |
|
| 132 | 132 |
bool _autoNodeScale; |
| 133 | 133 |
bool _autoArcWidthScale; |
| 134 | 134 |
|
| 135 | 135 |
bool _absoluteNodeSizes; |
| 136 | 136 |
bool _absoluteArcWidths; |
| 137 | 137 |
|
| 138 | 138 |
bool _negY; |
| 139 | 139 |
|
| 140 | 140 |
bool _preScale; |
| 141 | 141 |
///Constructor |
| 142 | 142 |
|
| 143 | 143 |
///Constructor |
| 144 | 144 |
///\param _g Reference to the graph to be printed. |
| 145 | 145 |
///\param _os Reference to the output stream. |
| 146 | 146 |
///\param _os Reference to the output stream. |
| 147 | 147 |
///By default it is <tt>std::cout</tt>. |
| 148 | 148 |
///\param _pros If it is \c true, then the \c ostream referenced by \c _os |
| 149 | 149 |
///will be explicitly deallocated by the destructor. |
| 150 | 150 |
DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, |
| 151 | 151 |
bool _pros=false) : |
| 152 | 152 |
g(_g), os(_os), |
| 153 | 153 |
_coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0), |
| 154 | 154 |
_nodeColors(WHITE), _arcColors(BLACK), |
| 155 | 155 |
_arcWidths(1.0), _arcWidthScale(0.003), |
| 156 | 156 |
_nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), |
| 157 | 157 |
_nodeBorderQuotient(.1), |
| 158 | 158 |
_drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
| 159 | 159 |
_showNodes(true), _showArcs(true), |
| 160 | 160 |
_enableParallel(false), _parArcDist(1), |
| 161 | 161 |
_showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
| 162 | 162 |
_showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
| 163 | 163 |
_undirected(lemon::UndirectedTagIndicator<G>::value), |
| 164 | 164 |
_pleaseRemoveOsStream(_pros), _scaleToA4(false), |
| 165 | 165 |
_nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), |
| 166 | 166 |
_autoNodeScale(false), |
| 167 | 167 |
_autoArcWidthScale(false), |
| 168 | 168 |
_absoluteNodeSizes(false), |
| 169 | 169 |
_absoluteArcWidths(false), |
| 170 | 170 |
_negY(false), |
| 171 | 171 |
_preScale(true) |
| 172 | 172 |
{}
|
| 173 | 173 |
}; |
| 174 | 174 |
|
| 175 | 175 |
///Auxiliary class to implement the named parameters of \ref graphToEps() |
| 176 | 176 |
|
| 177 | 177 |
///Auxiliary class to implement the named parameters of \ref graphToEps(). |
| 178 | 178 |
/// |
| 179 | 179 |
///For detailed examples see the \ref graph_to_eps_demo.cc demo file. |
| 180 | 180 |
template<class T> class GraphToEps : public T |
| 181 | 181 |
{
|
| 182 | 182 |
// Can't believe it is required by the C++ standard |
| 183 | 183 |
using T::g; |
| 184 | 184 |
using T::os; |
| 185 | 185 |
|
| 186 | 186 |
using T::_coords; |
| 187 | 187 |
using T::_nodeSizes; |
| 188 | 188 |
using T::_nodeShapes; |
| 189 | 189 |
using T::_nodeColors; |
| 190 | 190 |
using T::_arcColors; |
| 191 | 191 |
using T::_arcWidths; |
| 192 | 192 |
|
| 193 | 193 |
using T::_arcWidthScale; |
| 194 | 194 |
using T::_nodeScale; |
| 195 | 195 |
using T::_xBorder; |
| 196 | 196 |
using T::_yBorder; |
| 197 | 197 |
using T::_scale; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef GRID_GRAPH_H |
| 20 | 20 |
#define GRID_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/bits/graph_extender.h> |
| 24 | 24 |
#include <lemon/dim2.h> |
| 25 | 25 |
#include <lemon/assert.h> |
| 26 | 26 |
|
| 27 | 27 |
///\ingroup graphs |
| 28 | 28 |
///\file |
| 29 | 29 |
///\brief GridGraph class. |
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
|
| 33 | 33 |
class GridGraphBase {
|
| 34 | 34 |
|
| 35 | 35 |
public: |
| 36 | 36 |
|
| 37 | 37 |
typedef GridGraphBase Graph; |
| 38 | 38 |
|
| 39 | 39 |
class Node; |
| 40 | 40 |
class Edge; |
| 41 | 41 |
class Arc; |
| 42 | 42 |
|
| 43 | 43 |
public: |
| 44 | 44 |
|
| 45 | 45 |
GridGraphBase() {}
|
| 46 | 46 |
|
| 47 | 47 |
protected: |
| 48 | 48 |
|
| 49 | 49 |
void construct(int width, int height) {
|
| 50 | 50 |
_width = width; _height = height; |
| 51 | 51 |
_node_num = width * height; |
| 52 | 52 |
_edge_num = 2 * _node_num - width - height; |
| 53 | 53 |
_edge_limit = _node_num - _width; |
| 54 | 54 |
} |
| 55 | 55 |
|
| 56 | 56 |
public: |
| 57 | 57 |
|
| 58 | 58 |
Node operator()(int i, int j) const {
|
| 59 | 59 |
LEMON_DEBUG(0 <= i && i < _width && |
| 60 | 60 |
0 <= j && j < _height, "Index out of range"); |
| 61 | 61 |
return Node(i + j * _width); |
| 62 | 62 |
} |
| 63 | 63 |
|
| 64 | 64 |
int col(Node n) const {
|
| 65 | 65 |
return n._id % _width; |
| 66 | 66 |
} |
| 67 | 67 |
|
| 68 | 68 |
int row(Node n) const {
|
| 69 | 69 |
return n._id / _width; |
| 70 | 70 |
} |
| 71 | 71 |
|
| 72 | 72 |
dim2::Point<int> pos(Node n) const {
|
| 73 | 73 |
return dim2::Point<int>(col(n), row(n)); |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 | 76 |
int width() const {
|
| 77 | 77 |
return _width; |
| 78 | 78 |
} |
| 79 | 79 |
|
| 80 | 80 |
int height() const {
|
| 81 | 81 |
return _height; |
| 82 | 82 |
} |
| 83 | 83 |
|
| 84 | 84 |
typedef True NodeNumTag; |
| 85 | 85 |
typedef True EdgeNumTag; |
| 86 | 86 |
typedef True ArcNumTag; |
| 87 | 87 |
|
| 88 | 88 |
int nodeNum() const { return _node_num; }
|
| 89 | 89 |
int edgeNum() const { return _edge_num; }
|
| 90 | 90 |
int arcNum() const { return 2 * _edge_num; }
|
| 91 | 91 |
|
| 92 | 92 |
Node u(Edge edge) const {
|
| 93 | 93 |
if (edge._id < _edge_limit) {
|
| 94 | 94 |
return edge._id; |
| 95 | 95 |
} else {
|
| 96 | 96 |
return (edge._id - _edge_limit) % (_width - 1) + |
| 97 | 97 |
(edge._id - _edge_limit) / (_width - 1) * _width; |
| 98 | 98 |
} |
| 99 | 99 |
} |
| 100 | 100 |
|
| 101 | 101 |
Node v(Edge edge) const {
|
| 102 | 102 |
if (edge._id < _edge_limit) {
|
| 103 | 103 |
return edge._id + _width; |
| 104 | 104 |
} else {
|
| 105 | 105 |
return (edge._id - _edge_limit) % (_width - 1) + |
| 106 | 106 |
(edge._id - _edge_limit) / (_width - 1) * _width + 1; |
| 107 | 107 |
} |
| 108 | 108 |
} |
| 109 | 109 |
|
| 110 | 110 |
Node source(Arc arc) const {
|
| 111 | 111 |
return (arc._id & 1) == 1 ? u(arc) : v(arc); |
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 | 114 |
Node target(Arc arc) const {
|
| 115 | 115 |
return (arc._id & 1) == 1 ? v(arc) : u(arc); |
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 | 118 |
static int id(Node node) { return node._id; }
|
| 119 | 119 |
static int id(Edge edge) { return edge._id; }
|
| 120 | 120 |
static int id(Arc arc) { return arc._id; }
|
| 121 | 121 |
|
| 122 | 122 |
int maxNodeId() const { return _node_num - 1; }
|
| 123 | 123 |
int maxEdgeId() const { return _edge_num - 1; }
|
| 124 | 124 |
int maxArcId() const { return 2 * _edge_num - 1; }
|
| 125 | 125 |
|
| 126 | 126 |
static Node nodeFromId(int id) { return Node(id);}
|
| 127 | 127 |
static Edge edgeFromId(int id) { return Edge(id);}
|
| 128 | 128 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 129 | 129 |
|
| 130 | 130 |
typedef True FindEdgeTag; |
| 131 | 131 |
typedef True FindArcTag; |
| 132 | 132 |
|
| 133 | 133 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
|
| 134 | 134 |
if (prev != INVALID) return INVALID; |
| 135 | 135 |
if (v._id > u._id) {
|
| 136 | 136 |
if (v._id - u._id == _width) |
| 137 | 137 |
return Edge(u._id); |
| 138 | 138 |
if (v._id - u._id == 1 && u._id % _width < _width - 1) {
|
| 139 | 139 |
return Edge(u._id / _width * (_width - 1) + |
| 140 | 140 |
u._id % _width + _edge_limit); |
| 141 | 141 |
} |
| 142 | 142 |
} else {
|
| 143 | 143 |
if (u._id - v._id == _width) |
| 144 | 144 |
return Edge(v._id); |
| 145 | 145 |
if (u._id - v._id == 1 && v._id % _width < _width - 1) {
|
| 146 | 146 |
return Edge(v._id / _width * (_width - 1) + |
| 147 | 147 |
v._id % _width + _edge_limit); |
| 148 | 148 |
} |
| 149 | 149 |
} |
| 150 | 150 |
return INVALID; |
| 151 | 151 |
} |
| 152 | 152 |
|
| 153 | 153 |
Arc findArc(Node u, Node v, Arc prev = INVALID) const {
|
| 154 | 154 |
if (prev != INVALID) return INVALID; |
| 155 | 155 |
if (v._id > u._id) {
|
| 156 | 156 |
if (v._id - u._id == _width) |
| 157 | 157 |
return Arc((u._id << 1) | 1); |
| 158 | 158 |
if (v._id - u._id == 1 && u._id % _width < _width - 1) {
|
| 159 | 159 |
return Arc(((u._id / _width * (_width - 1) + |
| 160 | 160 |
u._id % _width + _edge_limit) << 1) | 1); |
| 161 | 161 |
} |
| 162 | 162 |
} else {
|
| 163 | 163 |
if (u._id - v._id == _width) |
| 164 | 164 |
return Arc(v._id << 1); |
| 165 | 165 |
if (u._id - v._id == 1 && v._id % _width < _width - 1) {
|
| 166 | 166 |
return Arc((v._id / _width * (_width - 1) + |
| 167 | 167 |
v._id % _width + _edge_limit) << 1); |
| 168 | 168 |
} |
| 169 | 169 |
} |
| 170 | 170 |
return INVALID; |
| 171 | 171 |
} |
| 172 | 172 |
|
| 173 | 173 |
class Node {
|
| 174 | 174 |
friend class GridGraphBase; |
| 175 | 175 |
|
| 176 | 176 |
protected: |
| 177 | 177 |
int _id; |
| 178 | 178 |
Node(int id) : _id(id) {}
|
| 179 | 179 |
public: |
| 180 | 180 |
Node() {}
|
| 181 | 181 |
Node (Invalid) : _id(-1) {}
|
| 182 | 182 |
bool operator==(const Node node) const {return _id == node._id;}
|
| 183 | 183 |
bool operator!=(const Node node) const {return _id != node._id;}
|
| 184 | 184 |
bool operator<(const Node node) const {return _id < node._id;}
|
| 185 | 185 |
}; |
| 186 | 186 |
|
| 187 | 187 |
class Edge {
|
| 188 | 188 |
friend class GridGraphBase; |
| 189 | 189 |
friend class Arc; |
| 190 | 190 |
|
| 191 | 191 |
protected: |
| 192 | 192 |
int _id; |
| 193 | 193 |
|
| 194 | 194 |
Edge(int id) : _id(id) {}
|
| 195 | 195 |
|
| 196 | 196 |
public: |
| 197 | 197 |
Edge() {}
|
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_HAO_ORLIN_H |
| 20 | 20 |
#define LEMON_HAO_ORLIN_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <list> |
| 24 | 24 |
#include <limits> |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/maps.h> |
| 27 | 27 |
#include <lemon/core.h> |
| 28 | 28 |
#include <lemon/tolerance.h> |
| 29 | 29 |
|
| 30 | 30 |
/// \file |
| 31 | 31 |
/// \ingroup min_cut |
| 32 | 32 |
/// \brief Implementation of the Hao-Orlin algorithm. |
| 33 | 33 |
/// |
| 34 | 34 |
/// Implementation of the Hao-Orlin algorithm class for testing network |
| 35 | 35 |
/// reliability. |
| 36 | 36 |
|
| 37 | 37 |
namespace lemon {
|
| 38 | 38 |
|
| 39 | 39 |
/// \ingroup min_cut |
| 40 | 40 |
/// |
| 41 | 41 |
/// \brief %Hao-Orlin algorithm to find a minimum cut in directed graphs. |
| 42 | 42 |
/// |
| 43 | 43 |
/// Hao-Orlin calculates a minimum cut in a directed graph |
| 44 | 44 |
/// \f$D=(V,A)\f$. It takes a fixed node \f$ source \in V \f$ and |
| 45 | 45 |
/// consists of two phases: in the first phase it determines a |
| 46 | 46 |
/// minimum cut with \f$ source \f$ on the source-side (i.e. a set |
| 47 | 47 |
/// \f$ X\subsetneq V \f$ with \f$ source \in X \f$ and minimal |
| 48 | 48 |
/// out-degree) and in the second phase it determines a minimum cut |
| 49 | 49 |
/// with \f$ source \f$ on the sink-side (i.e. a set |
| 50 | 50 |
/// \f$ X\subsetneq V \f$ with \f$ source \notin X \f$ and minimal |
| 51 | 51 |
/// out-degree). Obviously, the smaller of these two cuts will be a |
| 52 | 52 |
/// minimum cut of \f$ D \f$. The algorithm is a modified |
| 53 | 53 |
/// push-relabel preflow algorithm and our implementation calculates |
| 54 | 54 |
/// the minimum cut in \f$ O(n^2\sqrt{m}) \f$ time (we use the
|
| 55 | 55 |
/// highest-label rule), or in \f$O(nm)\f$ for unit capacities. The |
| 56 | 56 |
/// purpose of such algorithm is testing network reliability. For an |
| 57 | 57 |
/// undirected graph you can run just the first phase of the |
| 58 | 58 |
/// algorithm or you can use the algorithm of Nagamochi and Ibaraki |
| 59 | 59 |
/// which solves the undirected problem in |
| 60 | 60 |
/// \f$ O(nm + n^2 \log(n)) \f$ time: it is implemented in the |
| 61 | 61 |
/// NagamochiIbaraki algorithm class. |
| 62 | 62 |
/// |
| 63 | 63 |
/// \param _Digraph is the graph type of the algorithm. |
| 64 | 64 |
/// \param _CapacityMap is an edge map of capacities which should |
| 65 | 65 |
/// be any numreric type. The default type is _Digraph::ArcMap<int>. |
| 66 | 66 |
/// \param _Tolerance is the handler of the inexact computation. The |
| 67 | 67 |
/// default type for this is Tolerance<CapacityMap::Value>. |
| 68 | 68 |
#ifdef DOXYGEN |
| 69 | 69 |
template <typename _Digraph, typename _CapacityMap, typename _Tolerance> |
| 70 | 70 |
#else |
| 71 | 71 |
template <typename _Digraph, |
| 72 | 72 |
typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
| 73 | 73 |
typename _Tolerance = Tolerance<typename _CapacityMap::Value> > |
| 74 | 74 |
#endif |
| 75 | 75 |
class HaoOrlin {
|
| 76 | 76 |
private: |
| 77 | 77 |
|
| 78 | 78 |
typedef _Digraph Digraph; |
| 79 | 79 |
typedef _CapacityMap CapacityMap; |
| 80 | 80 |
typedef _Tolerance Tolerance; |
| 81 | 81 |
|
| 82 | 82 |
typedef typename CapacityMap::Value Value; |
| 83 | 83 |
|
| 84 | 84 |
TEMPLATE_GRAPH_TYPEDEFS(Digraph); |
| 85 | 85 |
|
| 86 | 86 |
const Digraph& _graph; |
| 87 | 87 |
const CapacityMap* _capacity; |
| 88 | 88 |
|
| 89 | 89 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
| 90 | 90 |
FlowMap* _flow; |
| 91 | 91 |
|
| 92 | 92 |
Node _source; |
| 93 | 93 |
|
| 94 | 94 |
int _node_num; |
| 95 | 95 |
|
| 96 | 96 |
// Bucketing structure |
| 97 | 97 |
std::vector<Node> _first, _last; |
| 98 | 98 |
typename Digraph::template NodeMap<Node>* _next; |
| 99 | 99 |
typename Digraph::template NodeMap<Node>* _prev; |
| 100 | 100 |
typename Digraph::template NodeMap<bool>* _active; |
| 101 | 101 |
typename Digraph::template NodeMap<int>* _bucket; |
| 102 | 102 |
|
| 103 | 103 |
std::vector<bool> _dormant; |
| 104 | 104 |
|
| 105 | 105 |
std::list<std::list<int> > _sets; |
| 106 | 106 |
std::list<int>::iterator _highest; |
| 107 | 107 |
|
| 108 | 108 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
| 109 | 109 |
ExcessMap* _excess; |
| 110 | 110 |
|
| 111 | 111 |
typedef typename Digraph::template NodeMap<bool> SourceSetMap; |
| 112 | 112 |
SourceSetMap* _source_set; |
| 113 | 113 |
|
| 114 | 114 |
Value _min_cut; |
| 115 | 115 |
|
| 116 | 116 |
typedef typename Digraph::template NodeMap<bool> MinCutMap; |
| 117 | 117 |
MinCutMap* _min_cut_map; |
| 118 | 118 |
|
| 119 | 119 |
Tolerance _tolerance; |
| 120 | 120 |
|
| 121 | 121 |
public: |
| 122 | 122 |
|
| 123 | 123 |
/// \brief Constructor |
| 124 | 124 |
/// |
| 125 | 125 |
/// Constructor of the algorithm class. |
| 126 | 126 |
HaoOrlin(const Digraph& graph, const CapacityMap& capacity, |
| 127 | 127 |
const Tolerance& tolerance = Tolerance()) : |
| 128 | 128 |
_graph(graph), _capacity(&capacity), _flow(0), _source(), |
| 129 | 129 |
_node_num(), _first(), _last(), _next(0), _prev(0), |
| 130 | 130 |
_active(0), _bucket(0), _dormant(), _sets(), _highest(), |
| 131 | 131 |
_excess(0), _source_set(0), _min_cut(), _min_cut_map(0), |
| 132 | 132 |
_tolerance(tolerance) {}
|
| 133 | 133 |
|
| 134 | 134 |
~HaoOrlin() {
|
| 135 | 135 |
if (_min_cut_map) {
|
| 136 | 136 |
delete _min_cut_map; |
| 137 | 137 |
} |
| 138 | 138 |
if (_source_set) {
|
| 139 | 139 |
delete _source_set; |
| 140 | 140 |
} |
| 141 | 141 |
if (_excess) {
|
| 142 | 142 |
delete _excess; |
| 143 | 143 |
} |
| 144 | 144 |
if (_next) {
|
| 145 | 145 |
delete _next; |
| 146 | 146 |
} |
| 147 | 147 |
if (_prev) {
|
| 148 | 148 |
delete _prev; |
| 149 | 149 |
} |
| 150 | 150 |
if (_active) {
|
| 151 | 151 |
delete _active; |
| 152 | 152 |
} |
| 153 | 153 |
if (_bucket) {
|
| 154 | 154 |
delete _bucket; |
| 155 | 155 |
} |
| 156 | 156 |
if (_flow) {
|
| 157 | 157 |
delete _flow; |
| 158 | 158 |
} |
| 159 | 159 |
} |
| 160 | 160 |
|
| 161 | 161 |
private: |
| 162 | 162 |
|
| 163 | 163 |
void activate(const Node& i) {
|
| 164 | 164 |
_active->set(i, true); |
| 165 | 165 |
|
| 166 | 166 |
int bucket = (*_bucket)[i]; |
| 167 | 167 |
|
| 168 | 168 |
if ((*_prev)[i] == INVALID || (*_active)[(*_prev)[i]]) return; |
| 169 | 169 |
//unlace |
| 170 | 170 |
_next->set((*_prev)[i], (*_next)[i]); |
| 171 | 171 |
if ((*_next)[i] != INVALID) {
|
| 172 | 172 |
_prev->set((*_next)[i], (*_prev)[i]); |
| 173 | 173 |
} else {
|
| 174 | 174 |
_last[bucket] = (*_prev)[i]; |
| 175 | 175 |
} |
| 176 | 176 |
//lace |
| 177 | 177 |
_next->set(i, _first[bucket]); |
| 178 | 178 |
_prev->set(_first[bucket], i); |
| 179 | 179 |
_prev->set(i, INVALID); |
| 180 | 180 |
_first[bucket] = i; |
| 181 | 181 |
} |
| 182 | 182 |
|
| 183 | 183 |
void deactivate(const Node& i) {
|
| 184 | 184 |
_active->set(i, false); |
| 185 | 185 |
int bucket = (*_bucket)[i]; |
| 186 | 186 |
|
| 187 | 187 |
if ((*_next)[i] == INVALID || !(*_active)[(*_next)[i]]) return; |
| 188 | 188 |
|
| 189 | 189 |
//unlace |
| 190 | 190 |
_prev->set((*_next)[i], (*_prev)[i]); |
| 191 | 191 |
if ((*_prev)[i] != INVALID) {
|
| 192 | 192 |
_next->set((*_prev)[i], (*_next)[i]); |
| 193 | 193 |
} else {
|
| 194 | 194 |
_first[bucket] = (*_next)[i]; |
| 195 | 195 |
} |
| 196 | 196 |
//lace |
| 197 | 197 |
_prev->set(i, _last[bucket]); |
| 198 | 198 |
_next->set(_last[bucket], i); |
| 199 | 199 |
_next->set(i, INVALID); |
| 200 | 200 |
_last[bucket] = i; |
| 201 | 201 |
} |
| 202 | 202 |
|
| 203 | 203 |
void addItem(const Node& i, int bucket) {
|
| 204 | 204 |
(*_bucket)[i] = bucket; |
| 205 | 205 |
if (_last[bucket] != INVALID) {
|
| 206 | 206 |
_prev->set(i, _last[bucket]); |
| 207 | 207 |
_next->set(_last[bucket], i); |
| 208 | 208 |
_next->set(i, INVALID); |
| 209 | 209 |
_last[bucket] = i; |
| 210 | 210 |
} else {
|
| 211 | 211 |
_prev->set(i, INVALID); |
| 212 | 212 |
_first[bucket] = i; |
| 213 | 213 |
_next->set(i, INVALID); |
| 214 | 214 |
_last[bucket] = i; |
| 215 | 215 |
} |
| 216 | 216 |
} |
| 217 | 217 |
|
| 218 | 218 |
void findMinCutOut() {
|
| 219 | 219 |
|
| 220 | 220 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 221 | 221 |
_excess->set(n, 0); |
| 222 | 222 |
} |
| 223 | 223 |
|
| 224 | 224 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 225 | 225 |
_flow->set(a, 0); |
| 226 | 226 |
} |
| 227 | 227 |
|
| 228 | 228 |
int bucket_num = 0; |
| 229 | 229 |
std::vector<Node> queue(_node_num); |
| 230 | 230 |
int qfirst = 0, qlast = 0, qsep = 0; |
| 231 | 231 |
|
| 232 | 232 |
{
|
| 233 | 233 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
| 234 | 234 |
|
| 235 | 235 |
reached.set(_source, true); |
| 236 | 236 |
bool first_set = true; |
| 237 | 237 |
|
| 238 | 238 |
for (NodeIt t(_graph); t != INVALID; ++t) {
|
| 239 | 239 |
if (reached[t]) continue; |
| 240 | 240 |
_sets.push_front(std::list<int>()); |
| 241 |
|
|
| 241 |
|
|
| 242 | 242 |
queue[qlast++] = t; |
| 243 | 243 |
reached.set(t, true); |
| 244 | 244 |
|
| 245 | 245 |
while (qfirst != qlast) {
|
| 246 | 246 |
if (qsep == qfirst) {
|
| 247 | 247 |
++bucket_num; |
| 248 | 248 |
_sets.front().push_front(bucket_num); |
| 249 | 249 |
_dormant[bucket_num] = !first_set; |
| 250 | 250 |
_first[bucket_num] = _last[bucket_num] = INVALID; |
| 251 | 251 |
qsep = qlast; |
| 252 | 252 |
} |
| 253 | 253 |
|
| 254 | 254 |
Node n = queue[qfirst++]; |
| 255 | 255 |
addItem(n, bucket_num); |
| 256 | 256 |
|
| 257 | 257 |
for (InArcIt a(_graph, n); a != INVALID; ++a) {
|
| 258 | 258 |
Node u = _graph.source(a); |
| 259 | 259 |
if (!reached[u] && _tolerance.positive((*_capacity)[a])) {
|
| 260 | 260 |
reached.set(u, true); |
| 261 | 261 |
queue[qlast++] = u; |
| 262 | 262 |
} |
| 263 | 263 |
} |
| 264 | 264 |
} |
| 265 | 265 |
first_set = false; |
| 266 | 266 |
} |
| 267 | 267 |
|
| 268 | 268 |
++bucket_num; |
| 269 | 269 |
_bucket->set(_source, 0); |
| 270 | 270 |
_dormant[0] = true; |
| 271 | 271 |
} |
| 272 | 272 |
_source_set->set(_source, true); |
| 273 | 273 |
|
| 274 | 274 |
Node target = _last[_sets.back().back()]; |
| 275 | 275 |
{
|
| 276 | 276 |
for (OutArcIt a(_graph, _source); a != INVALID; ++a) {
|
| 277 | 277 |
if (_tolerance.positive((*_capacity)[a])) {
|
| 278 | 278 |
Node u = _graph.target(a); |
| 279 | 279 |
_flow->set(a, (*_capacity)[a]); |
| 280 | 280 |
_excess->set(u, (*_excess)[u] + (*_capacity)[a]); |
| 281 | 281 |
if (!(*_active)[u] && u != _source) {
|
| 282 | 282 |
activate(u); |
| 283 | 283 |
} |
| 284 | 284 |
} |
| 285 | 285 |
} |
| 286 | 286 |
|
| 287 | 287 |
if ((*_active)[target]) {
|
| 288 | 288 |
deactivate(target); |
| 289 | 289 |
} |
| 290 | 290 |
|
| 291 | 291 |
_highest = _sets.back().begin(); |
| 292 | 292 |
while (_highest != _sets.back().end() && |
| 293 | 293 |
!(*_active)[_first[*_highest]]) {
|
| 294 | 294 |
++_highest; |
| 295 | 295 |
} |
| 296 | 296 |
} |
| 297 | 297 |
|
| 298 | 298 |
while (true) {
|
| 299 | 299 |
while (_highest != _sets.back().end()) {
|
| 300 | 300 |
Node n = _first[*_highest]; |
| 301 | 301 |
Value excess = (*_excess)[n]; |
| 302 | 302 |
int next_bucket = _node_num; |
| 303 | 303 |
|
| 304 | 304 |
int under_bucket; |
| 305 | 305 |
if (++std::list<int>::iterator(_highest) == _sets.back().end()) {
|
| 306 | 306 |
under_bucket = -1; |
| 307 | 307 |
} else {
|
| 308 | 308 |
under_bucket = *(++std::list<int>::iterator(_highest)); |
| 309 | 309 |
} |
| 310 | 310 |
|
| 311 | 311 |
for (OutArcIt a(_graph, n); a != INVALID; ++a) {
|
| 312 | 312 |
Node v = _graph.target(a); |
| 313 | 313 |
if (_dormant[(*_bucket)[v]]) continue; |
| 314 | 314 |
Value rem = (*_capacity)[a] - (*_flow)[a]; |
| 315 | 315 |
if (!_tolerance.positive(rem)) continue; |
| 316 | 316 |
if ((*_bucket)[v] == under_bucket) {
|
| 317 | 317 |
if (!(*_active)[v] && v != target) {
|
| 318 | 318 |
activate(v); |
| 319 | 319 |
} |
| 320 | 320 |
if (!_tolerance.less(rem, excess)) {
|
| 321 | 321 |
_flow->set(a, (*_flow)[a] + excess); |
| 322 | 322 |
_excess->set(v, (*_excess)[v] + excess); |
| 323 | 323 |
excess = 0; |
| 324 | 324 |
goto no_more_push; |
| 325 | 325 |
} else {
|
| 326 | 326 |
excess -= rem; |
| 327 | 327 |
_excess->set(v, (*_excess)[v] + rem); |
| 328 | 328 |
_flow->set(a, (*_capacity)[a]); |
| 329 | 329 |
} |
| 330 | 330 |
} else if (next_bucket > (*_bucket)[v]) {
|
| 331 | 331 |
next_bucket = (*_bucket)[v]; |
| 332 | 332 |
} |
| 333 | 333 |
} |
| 334 | 334 |
|
| 335 | 335 |
for (InArcIt a(_graph, n); a != INVALID; ++a) {
|
| 336 | 336 |
Node v = _graph.source(a); |
| 337 | 337 |
if (_dormant[(*_bucket)[v]]) continue; |
| 338 | 338 |
Value rem = (*_flow)[a]; |
| 339 | 339 |
if (!_tolerance.positive(rem)) continue; |
| 340 | 340 |
if ((*_bucket)[v] == under_bucket) {
|
| 341 | 341 |
if (!(*_active)[v] && v != target) {
|
| 342 | 342 |
activate(v); |
| 343 | 343 |
} |
| 344 | 344 |
if (!_tolerance.less(rem, excess)) {
|
| 345 | 345 |
_flow->set(a, (*_flow)[a] - excess); |
| 346 | 346 |
_excess->set(v, (*_excess)[v] + excess); |
| 347 | 347 |
excess = 0; |
| 348 | 348 |
goto no_more_push; |
| 349 | 349 |
} else {
|
| 350 | 350 |
excess -= rem; |
| 351 | 351 |
_excess->set(v, (*_excess)[v] + rem); |
| 352 | 352 |
_flow->set(a, 0); |
| 353 | 353 |
} |
| 354 | 354 |
} else if (next_bucket > (*_bucket)[v]) {
|
| 355 | 355 |
next_bucket = (*_bucket)[v]; |
| 356 | 356 |
} |
| 357 | 357 |
} |
| 358 | 358 |
|
| 359 | 359 |
no_more_push: |
| 360 | 360 |
|
| 361 | 361 |
_excess->set(n, excess); |
| 362 | 362 |
|
| 363 | 363 |
if (excess != 0) {
|
| 364 | 364 |
if ((*_next)[n] == INVALID) {
|
| 365 | 365 |
typename std::list<std::list<int> >::iterator new_set = |
| 366 | 366 |
_sets.insert(--_sets.end(), std::list<int>()); |
| 367 | 367 |
new_set->splice(new_set->end(), _sets.back(), |
| 368 | 368 |
_sets.back().begin(), ++_highest); |
| 369 | 369 |
for (std::list<int>::iterator it = new_set->begin(); |
| 370 | 370 |
it != new_set->end(); ++it) {
|
| 371 | 371 |
_dormant[*it] = true; |
| 372 | 372 |
} |
| 373 | 373 |
while (_highest != _sets.back().end() && |
| 374 | 374 |
!(*_active)[_first[*_highest]]) {
|
| 375 | 375 |
++_highest; |
| 376 | 376 |
} |
| 377 | 377 |
} else if (next_bucket == _node_num) {
|
| 378 | 378 |
_first[(*_bucket)[n]] = (*_next)[n]; |
| 379 | 379 |
_prev->set((*_next)[n], INVALID); |
| 380 | 380 |
|
| 381 | 381 |
std::list<std::list<int> >::iterator new_set = |
| 382 | 382 |
_sets.insert(--_sets.end(), std::list<int>()); |
| 383 | 383 |
|
| 384 | 384 |
new_set->push_front(bucket_num); |
| 385 | 385 |
_bucket->set(n, bucket_num); |
| 386 | 386 |
_first[bucket_num] = _last[bucket_num] = n; |
| 387 | 387 |
_next->set(n, INVALID); |
| 388 | 388 |
_prev->set(n, INVALID); |
| 389 | 389 |
_dormant[bucket_num] = true; |
| 390 | 390 |
++bucket_num; |
| 391 | 391 |
|
| 392 | 392 |
while (_highest != _sets.back().end() && |
| 393 | 393 |
!(*_active)[_first[*_highest]]) {
|
| 394 | 394 |
++_highest; |
| 395 | 395 |
} |
| 396 | 396 |
} else {
|
| 397 | 397 |
_first[*_highest] = (*_next)[n]; |
| 398 | 398 |
_prev->set((*_next)[n], INVALID); |
| 399 | 399 |
|
| 400 | 400 |
while (next_bucket != *_highest) {
|
| 401 | 401 |
--_highest; |
| 402 | 402 |
} |
| 403 | 403 |
|
| 404 | 404 |
if (_highest == _sets.back().begin()) {
|
| 405 | 405 |
_sets.back().push_front(bucket_num); |
| 406 | 406 |
_dormant[bucket_num] = false; |
| 407 | 407 |
_first[bucket_num] = _last[bucket_num] = INVALID; |
| 408 | 408 |
++bucket_num; |
| 409 | 409 |
} |
| 410 | 410 |
--_highest; |
| 411 | 411 |
|
| 412 | 412 |
_bucket->set(n, *_highest); |
| 413 | 413 |
_next->set(n, _first[*_highest]); |
| 414 | 414 |
if (_first[*_highest] != INVALID) {
|
| 415 | 415 |
_prev->set(_first[*_highest], n); |
| 416 | 416 |
} else {
|
| 417 | 417 |
_last[*_highest] = n; |
| 418 | 418 |
} |
| 419 | 419 |
_first[*_highest] = n; |
| 420 | 420 |
} |
| 421 | 421 |
} else {
|
| 422 | 422 |
|
| 423 | 423 |
deactivate(n); |
| 424 | 424 |
if (!(*_active)[_first[*_highest]]) {
|
| 425 | 425 |
++_highest; |
| 426 | 426 |
if (_highest != _sets.back().end() && |
| 427 | 427 |
!(*_active)[_first[*_highest]]) {
|
| 428 | 428 |
_highest = _sets.back().end(); |
| 429 | 429 |
} |
| 430 | 430 |
} |
| 431 | 431 |
} |
| 432 | 432 |
} |
| 433 | 433 |
|
| 434 | 434 |
if ((*_excess)[target] < _min_cut) {
|
| 435 | 435 |
_min_cut = (*_excess)[target]; |
| 436 | 436 |
for (NodeIt i(_graph); i != INVALID; ++i) {
|
| 437 | 437 |
_min_cut_map->set(i, true); |
| 438 | 438 |
} |
| 439 | 439 |
for (std::list<int>::iterator it = _sets.back().begin(); |
| 440 | 440 |
it != _sets.back().end(); ++it) {
|
| 441 | 441 |
Node n = _first[*it]; |
| 442 | 442 |
while (n != INVALID) {
|
| 443 | 443 |
_min_cut_map->set(n, false); |
| 444 | 444 |
n = (*_next)[n]; |
| 445 | 445 |
} |
| 446 | 446 |
} |
| 447 | 447 |
} |
| 448 | 448 |
|
| 449 | 449 |
{
|
| 450 | 450 |
Node new_target; |
| 451 | 451 |
if ((*_prev)[target] != INVALID || (*_next)[target] != INVALID) {
|
| 452 | 452 |
if ((*_next)[target] == INVALID) {
|
| 453 | 453 |
_last[(*_bucket)[target]] = (*_prev)[target]; |
| 454 | 454 |
new_target = (*_prev)[target]; |
| 455 | 455 |
} else {
|
| 456 | 456 |
_prev->set((*_next)[target], (*_prev)[target]); |
| 457 | 457 |
new_target = (*_next)[target]; |
| 458 | 458 |
} |
| 459 | 459 |
if ((*_prev)[target] == INVALID) {
|
| 460 | 460 |
_first[(*_bucket)[target]] = (*_next)[target]; |
| 461 | 461 |
} else {
|
| 462 | 462 |
_next->set((*_prev)[target], (*_next)[target]); |
| 463 | 463 |
} |
| 464 | 464 |
} else {
|
| 465 | 465 |
_sets.back().pop_back(); |
| 466 | 466 |
if (_sets.back().empty()) {
|
| 467 | 467 |
_sets.pop_back(); |
| 468 | 468 |
if (_sets.empty()) |
| 469 | 469 |
break; |
| 470 | 470 |
for (std::list<int>::iterator it = _sets.back().begin(); |
| 471 | 471 |
it != _sets.back().end(); ++it) {
|
| 472 | 472 |
_dormant[*it] = false; |
| 473 | 473 |
} |
| 474 | 474 |
} |
| 475 | 475 |
new_target = _last[_sets.back().back()]; |
| 476 | 476 |
} |
| 477 | 477 |
|
| 478 | 478 |
_bucket->set(target, 0); |
| 479 | 479 |
|
| 480 | 480 |
_source_set->set(target, true); |
| 481 | 481 |
for (OutArcIt a(_graph, target); a != INVALID; ++a) {
|
| 482 | 482 |
Value rem = (*_capacity)[a] - (*_flow)[a]; |
| 483 | 483 |
if (!_tolerance.positive(rem)) continue; |
| 484 | 484 |
Node v = _graph.target(a); |
| 485 | 485 |
if (!(*_active)[v] && !(*_source_set)[v]) {
|
| 486 | 486 |
activate(v); |
| 487 | 487 |
} |
| 488 | 488 |
_excess->set(v, (*_excess)[v] + rem); |
| 489 | 489 |
_flow->set(a, (*_capacity)[a]); |
| 490 | 490 |
} |
| 491 | 491 |
|
| 492 | 492 |
for (InArcIt a(_graph, target); a != INVALID; ++a) {
|
| 493 | 493 |
Value rem = (*_flow)[a]; |
| 494 | 494 |
if (!_tolerance.positive(rem)) continue; |
| 495 | 495 |
Node v = _graph.source(a); |
| 496 | 496 |
if (!(*_active)[v] && !(*_source_set)[v]) {
|
| 497 | 497 |
activate(v); |
| 498 | 498 |
} |
| 499 | 499 |
_excess->set(v, (*_excess)[v] + rem); |
| 500 | 500 |
_flow->set(a, 0); |
| 501 | 501 |
} |
| 502 | 502 |
|
| 503 | 503 |
target = new_target; |
| 504 | 504 |
if ((*_active)[target]) {
|
| 505 | 505 |
deactivate(target); |
| 506 | 506 |
} |
| 507 | 507 |
|
| 508 | 508 |
_highest = _sets.back().begin(); |
| 509 | 509 |
while (_highest != _sets.back().end() && |
| 510 | 510 |
!(*_active)[_first[*_highest]]) {
|
| 511 | 511 |
++_highest; |
| 512 | 512 |
} |
| 513 | 513 |
} |
| 514 | 514 |
} |
| 515 | 515 |
} |
| 516 | 516 |
|
| 517 | 517 |
void findMinCutIn() {
|
| 518 | 518 |
|
| 519 | 519 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 520 | 520 |
_excess->set(n, 0); |
| 521 | 521 |
} |
| 522 | 522 |
|
| 523 | 523 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 524 | 524 |
_flow->set(a, 0); |
| 525 | 525 |
} |
| 526 | 526 |
|
| 527 | 527 |
int bucket_num = 0; |
| 528 | 528 |
std::vector<Node> queue(_node_num); |
| 529 | 529 |
int qfirst = 0, qlast = 0, qsep = 0; |
| 530 | 530 |
|
| 531 | 531 |
{
|
| 532 | 532 |
typename Digraph::template NodeMap<bool> reached(_graph, false); |
| 533 | 533 |
|
| 534 | 534 |
reached.set(_source, true); |
| 535 | 535 |
|
| 536 | 536 |
bool first_set = true; |
| 537 | 537 |
|
| 538 | 538 |
for (NodeIt t(_graph); t != INVALID; ++t) {
|
| 539 | 539 |
if (reached[t]) continue; |
| 540 | 540 |
_sets.push_front(std::list<int>()); |
| 541 |
|
|
| 541 |
|
|
| 542 | 542 |
queue[qlast++] = t; |
| 543 | 543 |
reached.set(t, true); |
| 544 | 544 |
|
| 545 | 545 |
while (qfirst != qlast) {
|
| 546 | 546 |
if (qsep == qfirst) {
|
| 547 | 547 |
++bucket_num; |
| 548 | 548 |
_sets.front().push_front(bucket_num); |
| 549 | 549 |
_dormant[bucket_num] = !first_set; |
| 550 | 550 |
_first[bucket_num] = _last[bucket_num] = INVALID; |
| 551 | 551 |
qsep = qlast; |
| 552 | 552 |
} |
| 553 | 553 |
|
| 554 | 554 |
Node n = queue[qfirst++]; |
| 555 | 555 |
addItem(n, bucket_num); |
| 556 | 556 |
|
| 557 | 557 |
for (OutArcIt a(_graph, n); a != INVALID; ++a) {
|
| 558 | 558 |
Node u = _graph.target(a); |
| 559 | 559 |
if (!reached[u] && _tolerance.positive((*_capacity)[a])) {
|
| 560 | 560 |
reached.set(u, true); |
| 561 | 561 |
queue[qlast++] = u; |
| 562 | 562 |
} |
| 563 | 563 |
} |
| 564 | 564 |
} |
| 565 | 565 |
first_set = false; |
| 566 | 566 |
} |
| 567 | 567 |
|
| 568 | 568 |
++bucket_num; |
| 569 | 569 |
_bucket->set(_source, 0); |
| 570 | 570 |
_dormant[0] = true; |
| 571 | 571 |
} |
| 572 | 572 |
_source_set->set(_source, true); |
| 573 | 573 |
|
| 574 | 574 |
Node target = _last[_sets.back().back()]; |
| 575 | 575 |
{
|
| 576 | 576 |
for (InArcIt a(_graph, _source); a != INVALID; ++a) {
|
| 577 | 577 |
if (_tolerance.positive((*_capacity)[a])) {
|
| 578 | 578 |
Node u = _graph.source(a); |
| 579 | 579 |
_flow->set(a, (*_capacity)[a]); |
| 580 | 580 |
_excess->set(u, (*_excess)[u] + (*_capacity)[a]); |
| 581 | 581 |
if (!(*_active)[u] && u != _source) {
|
| 582 | 582 |
activate(u); |
| 583 | 583 |
} |
| 584 | 584 |
} |
| 585 | 585 |
} |
| 586 | 586 |
if ((*_active)[target]) {
|
| 587 | 587 |
deactivate(target); |
| 588 | 588 |
} |
| 589 | 589 |
|
| 590 | 590 |
_highest = _sets.back().begin(); |
| 591 | 591 |
while (_highest != _sets.back().end() && |
| 592 | 592 |
!(*_active)[_first[*_highest]]) {
|
| 593 | 593 |
++_highest; |
| 594 | 594 |
} |
| 595 | 595 |
} |
| 596 | 596 |
|
| 597 | 597 |
|
| 598 | 598 |
while (true) {
|
| 599 | 599 |
while (_highest != _sets.back().end()) {
|
| 600 | 600 |
Node n = _first[*_highest]; |
| 601 | 601 |
Value excess = (*_excess)[n]; |
| 602 | 602 |
int next_bucket = _node_num; |
| 603 | 603 |
|
| 604 | 604 |
int under_bucket; |
| 605 | 605 |
if (++std::list<int>::iterator(_highest) == _sets.back().end()) {
|
| 606 | 606 |
under_bucket = -1; |
| 607 | 607 |
} else {
|
| 608 | 608 |
under_bucket = *(++std::list<int>::iterator(_highest)); |
| 609 | 609 |
} |
| 610 | 610 |
|
| 611 | 611 |
for (InArcIt a(_graph, n); a != INVALID; ++a) {
|
| 612 | 612 |
Node v = _graph.source(a); |
| 613 | 613 |
if (_dormant[(*_bucket)[v]]) continue; |
| 614 | 614 |
Value rem = (*_capacity)[a] - (*_flow)[a]; |
| 615 | 615 |
if (!_tolerance.positive(rem)) continue; |
| 616 | 616 |
if ((*_bucket)[v] == under_bucket) {
|
| 617 | 617 |
if (!(*_active)[v] && v != target) {
|
| 618 | 618 |
activate(v); |
| 619 | 619 |
} |
| 620 | 620 |
if (!_tolerance.less(rem, excess)) {
|
| 621 | 621 |
_flow->set(a, (*_flow)[a] + excess); |
| 622 | 622 |
_excess->set(v, (*_excess)[v] + excess); |
| 623 | 623 |
excess = 0; |
| 624 | 624 |
goto no_more_push; |
| 625 | 625 |
} else {
|
| 626 | 626 |
excess -= rem; |
| 627 | 627 |
_excess->set(v, (*_excess)[v] + rem); |
| 628 | 628 |
_flow->set(a, (*_capacity)[a]); |
| 629 | 629 |
} |
| 630 | 630 |
} else if (next_bucket > (*_bucket)[v]) {
|
| 631 | 631 |
next_bucket = (*_bucket)[v]; |
| 632 | 632 |
} |
| 633 | 633 |
} |
| 634 | 634 |
|
| 635 | 635 |
for (OutArcIt a(_graph, n); a != INVALID; ++a) {
|
| 636 | 636 |
Node v = _graph.target(a); |
| 637 | 637 |
if (_dormant[(*_bucket)[v]]) continue; |
| 638 | 638 |
Value rem = (*_flow)[a]; |
| 639 | 639 |
if (!_tolerance.positive(rem)) continue; |
| 640 | 640 |
if ((*_bucket)[v] == under_bucket) {
|
| 641 | 641 |
if (!(*_active)[v] && v != target) {
|
| 642 | 642 |
activate(v); |
| 643 | 643 |
} |
| 644 | 644 |
if (!_tolerance.less(rem, excess)) {
|
| 645 | 645 |
_flow->set(a, (*_flow)[a] - excess); |
| 646 | 646 |
_excess->set(v, (*_excess)[v] + excess); |
| 647 | 647 |
excess = 0; |
| 648 | 648 |
goto no_more_push; |
| 649 | 649 |
} else {
|
| 650 | 650 |
excess -= rem; |
| 651 | 651 |
_excess->set(v, (*_excess)[v] + rem); |
| 652 | 652 |
_flow->set(a, 0); |
| 653 | 653 |
} |
| 654 | 654 |
} else if (next_bucket > (*_bucket)[v]) {
|
| 655 | 655 |
next_bucket = (*_bucket)[v]; |
| 656 | 656 |
} |
| 657 | 657 |
} |
| 658 | 658 |
|
| 659 | 659 |
no_more_push: |
| 660 | 660 |
|
| 661 | 661 |
_excess->set(n, excess); |
| 662 | 662 |
|
| 663 | 663 |
if (excess != 0) {
|
| 664 | 664 |
if ((*_next)[n] == INVALID) {
|
| 665 | 665 |
typename std::list<std::list<int> >::iterator new_set = |
| 666 | 666 |
_sets.insert(--_sets.end(), std::list<int>()); |
| 667 | 667 |
new_set->splice(new_set->end(), _sets.back(), |
| 668 | 668 |
_sets.back().begin(), ++_highest); |
| 669 | 669 |
for (std::list<int>::iterator it = new_set->begin(); |
| 670 | 670 |
it != new_set->end(); ++it) {
|
| 671 | 671 |
_dormant[*it] = true; |
| 672 | 672 |
} |
| 673 | 673 |
while (_highest != _sets.back().end() && |
| 674 | 674 |
!(*_active)[_first[*_highest]]) {
|
| 675 | 675 |
++_highest; |
| 676 | 676 |
} |
| 677 | 677 |
} else if (next_bucket == _node_num) {
|
| 678 | 678 |
_first[(*_bucket)[n]] = (*_next)[n]; |
| 679 | 679 |
_prev->set((*_next)[n], INVALID); |
| 680 | 680 |
|
| 681 | 681 |
std::list<std::list<int> >::iterator new_set = |
| 682 | 682 |
_sets.insert(--_sets.end(), std::list<int>()); |
| 683 | 683 |
|
| 684 | 684 |
new_set->push_front(bucket_num); |
| 685 | 685 |
_bucket->set(n, bucket_num); |
| 686 | 686 |
_first[bucket_num] = _last[bucket_num] = n; |
| 687 | 687 |
_next->set(n, INVALID); |
| 688 | 688 |
_prev->set(n, INVALID); |
| 689 | 689 |
_dormant[bucket_num] = true; |
| 690 | 690 |
++bucket_num; |
| 691 | 691 |
|
| 692 | 692 |
while (_highest != _sets.back().end() && |
| 693 | 693 |
!(*_active)[_first[*_highest]]) {
|
| 694 | 694 |
++_highest; |
| 695 | 695 |
} |
| 696 | 696 |
} else {
|
| 697 | 697 |
_first[*_highest] = (*_next)[n]; |
| 698 | 698 |
_prev->set((*_next)[n], INVALID); |
| 699 | 699 |
|
| 700 | 700 |
while (next_bucket != *_highest) {
|
| 701 | 701 |
--_highest; |
| 702 | 702 |
} |
| 703 | 703 |
if (_highest == _sets.back().begin()) {
|
| 704 | 704 |
_sets.back().push_front(bucket_num); |
| 705 | 705 |
_dormant[bucket_num] = false; |
| 706 | 706 |
_first[bucket_num] = _last[bucket_num] = INVALID; |
| 707 | 707 |
++bucket_num; |
| 708 | 708 |
} |
| 709 | 709 |
--_highest; |
| 710 | 710 |
|
| 711 | 711 |
_bucket->set(n, *_highest); |
| 712 | 712 |
_next->set(n, _first[*_highest]); |
| 713 | 713 |
if (_first[*_highest] != INVALID) {
|
| 714 | 714 |
_prev->set(_first[*_highest], n); |
| 715 | 715 |
} else {
|
| 716 | 716 |
_last[*_highest] = n; |
| 717 | 717 |
} |
| 718 | 718 |
_first[*_highest] = n; |
| 719 | 719 |
} |
| 720 | 720 |
} else {
|
| 721 | 721 |
|
| 722 | 722 |
deactivate(n); |
| 723 | 723 |
if (!(*_active)[_first[*_highest]]) {
|
| 724 | 724 |
++_highest; |
| 725 | 725 |
if (_highest != _sets.back().end() && |
| 726 | 726 |
!(*_active)[_first[*_highest]]) {
|
| 727 | 727 |
_highest = _sets.back().end(); |
| 728 | 728 |
} |
| 729 | 729 |
} |
| 730 | 730 |
} |
| 731 | 731 |
} |
| 732 | 732 |
|
| 733 | 733 |
if ((*_excess)[target] < _min_cut) {
|
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef HYPERCUBE_GRAPH_H |
| 20 | 20 |
#define HYPERCUBE_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <lemon/core.h> |
| 24 | 24 |
#include <lemon/assert.h> |
| 25 | 25 |
#include <lemon/bits/graph_extender.h> |
| 26 | 26 |
|
| 27 | 27 |
///\ingroup graphs |
| 28 | 28 |
///\file |
| 29 | 29 |
///\brief HypercubeGraph class. |
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
|
| 33 | 33 |
class HypercubeGraphBase {
|
| 34 | 34 |
|
| 35 | 35 |
public: |
| 36 | 36 |
|
| 37 | 37 |
typedef HypercubeGraphBase Graph; |
| 38 | 38 |
|
| 39 | 39 |
class Node; |
| 40 | 40 |
class Edge; |
| 41 | 41 |
class Arc; |
| 42 | 42 |
|
| 43 | 43 |
public: |
| 44 | 44 |
|
| 45 | 45 |
HypercubeGraphBase() {}
|
| 46 | 46 |
|
| 47 | 47 |
protected: |
| 48 | 48 |
|
| 49 | 49 |
void construct(int dim) {
|
| 50 | 50 |
LEMON_ASSERT(dim >= 1, "The number of dimensions must be at least 1."); |
| 51 | 51 |
_dim = dim; |
| 52 | 52 |
_node_num = 1 << dim; |
| 53 | 53 |
_edge_num = dim * (1 << (dim-1)); |
| 54 | 54 |
} |
| 55 | 55 |
|
| 56 | 56 |
public: |
| 57 | 57 |
|
| 58 | 58 |
typedef True NodeNumTag; |
| 59 | 59 |
typedef True EdgeNumTag; |
| 60 | 60 |
typedef True ArcNumTag; |
| 61 | 61 |
|
| 62 | 62 |
int nodeNum() const { return _node_num; }
|
| 63 | 63 |
int edgeNum() const { return _edge_num; }
|
| 64 | 64 |
int arcNum() const { return 2 * _edge_num; }
|
| 65 | 65 |
|
| 66 | 66 |
int maxNodeId() const { return _node_num - 1; }
|
| 67 | 67 |
int maxEdgeId() const { return _edge_num - 1; }
|
| 68 | 68 |
int maxArcId() const { return 2 * _edge_num - 1; }
|
| 69 | 69 |
|
| 70 | 70 |
static Node nodeFromId(int id) { return Node(id); }
|
| 71 | 71 |
static Edge edgeFromId(int id) { return Edge(id); }
|
| 72 | 72 |
static Arc arcFromId(int id) { return Arc(id); }
|
| 73 | 73 |
|
| 74 | 74 |
static int id(Node node) { return node._id; }
|
| 75 | 75 |
static int id(Edge edge) { return edge._id; }
|
| 76 | 76 |
static int id(Arc arc) { return arc._id; }
|
| 77 | 77 |
|
| 78 | 78 |
Node u(Edge edge) const {
|
| 79 | 79 |
int base = edge._id & ((1 << (_dim-1)) - 1); |
| 80 | 80 |
int k = edge._id >> (_dim-1); |
| 81 | 81 |
return ((base >> k) << (k+1)) | (base & ((1 << k) - 1)); |
| 82 | 82 |
} |
| 83 | 83 |
|
| 84 | 84 |
Node v(Edge edge) const {
|
| 85 | 85 |
int base = edge._id & ((1 << (_dim-1)) - 1); |
| 86 | 86 |
int k = edge._id >> (_dim-1); |
| 87 | 87 |
return ((base >> k) << (k+1)) | (base & ((1 << k) - 1)) | (1 << k); |
| 88 | 88 |
} |
| 89 | 89 |
|
| 90 | 90 |
Node source(Arc arc) const {
|
| 91 | 91 |
return (arc._id & 1) == 1 ? u(arc) : v(arc); |
| 92 | 92 |
} |
| 93 | 93 |
|
| 94 | 94 |
Node target(Arc arc) const {
|
| 95 | 95 |
return (arc._id & 1) == 1 ? v(arc) : u(arc); |
| 96 | 96 |
} |
| 97 | 97 |
|
| 98 | 98 |
typedef True FindEdgeTag; |
| 99 | 99 |
typedef True FindArcTag; |
| 100 | 100 |
|
| 101 | 101 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
|
| 102 | 102 |
if (prev != INVALID) return INVALID; |
| 103 | 103 |
int d = u._id ^ v._id; |
| 104 | 104 |
int k = 0; |
| 105 | 105 |
if (d == 0) return INVALID; |
| 106 | 106 |
for ( ; (d & 1) == 0; d >>= 1) ++k; |
| 107 | 107 |
if (d >> 1 != 0) return INVALID; |
| 108 | 108 |
return (k << (_dim-1)) | ((u._id >> (k+1)) << k) | |
| 109 | 109 |
(u._id & ((1 << k) - 1)); |
| 110 | 110 |
} |
| 111 | 111 |
|
| 112 | 112 |
Arc findArc(Node u, Node v, Arc prev = INVALID) const {
|
| 113 | 113 |
Edge edge = findEdge(u, v, prev); |
| 114 | 114 |
if (edge == INVALID) return INVALID; |
| 115 | 115 |
int k = edge._id >> (_dim-1); |
| 116 | 116 |
return ((u._id >> k) & 1) == 1 ? edge._id << 1 : (edge._id << 1) | 1; |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
class Node {
|
| 120 | 120 |
friend class HypercubeGraphBase; |
| 121 | 121 |
|
| 122 | 122 |
protected: |
| 123 | 123 |
int _id; |
| 124 | 124 |
Node(int id) : _id(id) {}
|
| 125 | 125 |
public: |
| 126 | 126 |
Node() {}
|
| 127 | 127 |
Node (Invalid) : _id(-1) {}
|
| 128 | 128 |
bool operator==(const Node node) const {return _id == node._id;}
|
| 129 | 129 |
bool operator!=(const Node node) const {return _id != node._id;}
|
| 130 | 130 |
bool operator<(const Node node) const {return _id < node._id;}
|
| 131 | 131 |
}; |
| 132 | 132 |
|
| 133 | 133 |
class Edge {
|
| 134 | 134 |
friend class HypercubeGraphBase; |
| 135 | 135 |
friend class Arc; |
| 136 | 136 |
|
| 137 | 137 |
protected: |
| 138 | 138 |
int _id; |
| 139 | 139 |
|
| 140 | 140 |
Edge(int id) : _id(id) {}
|
| 141 | 141 |
|
| 142 | 142 |
public: |
| 143 | 143 |
Edge() {}
|
| 144 | 144 |
Edge (Invalid) : _id(-1) {}
|
| 145 | 145 |
bool operator==(const Edge edge) const {return _id == edge._id;}
|
| 146 | 146 |
bool operator!=(const Edge edge) const {return _id != edge._id;}
|
| 147 | 147 |
bool operator<(const Edge edge) const {return _id < edge._id;}
|
| 148 | 148 |
}; |
| 149 | 149 |
|
| 150 | 150 |
class Arc {
|
| 151 | 151 |
friend class HypercubeGraphBase; |
| 152 | 152 |
|
| 153 | 153 |
protected: |
| 154 | 154 |
int _id; |
| 155 | 155 |
|
| 156 | 156 |
Arc(int id) : _id(id) {}
|
| 157 | 157 |
|
| 158 | 158 |
public: |
| 159 | 159 |
Arc() {}
|
| 160 | 160 |
Arc (Invalid) : _id(-1) {}
|
| 161 | 161 |
operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
|
| 162 | 162 |
bool operator==(const Arc arc) const {return _id == arc._id;}
|
| 163 | 163 |
bool operator!=(const Arc arc) const {return _id != arc._id;}
|
| 164 | 164 |
bool operator<(const Arc arc) const {return _id < arc._id;}
|
| 165 | 165 |
}; |
| 166 | 166 |
|
| 167 | 167 |
void first(Node& node) const {
|
| 168 | 168 |
node._id = _node_num - 1; |
| 169 | 169 |
} |
| 170 | 170 |
|
| 171 | 171 |
static void next(Node& node) {
|
| 172 | 172 |
--node._id; |
| 173 | 173 |
} |
| 174 | 174 |
|
| 175 | 175 |
void first(Edge& edge) const {
|
| 176 | 176 |
edge._id = _edge_num - 1; |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
static void next(Edge& edge) {
|
| 180 | 180 |
--edge._id; |
| 181 | 181 |
} |
| 182 | 182 |
|
| 183 | 183 |
void first(Arc& arc) const {
|
| 184 | 184 |
arc._id = 2 * _edge_num - 1; |
| 185 | 185 |
} |
| 186 | 186 |
|
| 187 | 187 |
static void next(Arc& arc) {
|
| 188 | 188 |
--arc._id; |
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 | 191 |
void firstInc(Edge& edge, bool& dir, const Node& node) const {
|
| 192 | 192 |
edge._id = node._id >> 1; |
| 193 | 193 |
dir = (node._id & 1) == 0; |
| 194 | 194 |
} |
| 195 | 195 |
|
| 196 | 196 |
void nextInc(Edge& edge, bool& dir) const {
|
| 197 | 197 |
Node n = dir ? u(edge) : v(edge); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_KRUSKAL_H |
| 20 | 20 |
#define LEMON_KRUSKAL_H |
| 21 | 21 |
|
| 22 | 22 |
#include <algorithm> |
| 23 | 23 |
#include <vector> |
| 24 | 24 |
#include <lemon/unionfind.h> |
| 25 | 25 |
#include <lemon/maps.h> |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/core.h> |
| 28 | 28 |
#include <lemon/bits/traits.h> |
| 29 | 29 |
|
| 30 | 30 |
///\ingroup spantree |
| 31 | 31 |
///\file |
| 32 | 32 |
///\brief Kruskal's algorithm to compute a minimum cost spanning tree |
| 33 | 33 |
/// |
| 34 | 34 |
///Kruskal's algorithm to compute a minimum cost spanning tree. |
| 35 | 35 |
/// |
| 36 | 36 |
|
| 37 | 37 |
namespace lemon {
|
| 38 | 38 |
|
| 39 | 39 |
namespace _kruskal_bits {
|
| 40 | 40 |
|
| 41 | 41 |
// Kruskal for directed graphs. |
| 42 | 42 |
|
| 43 | 43 |
template <typename Digraph, typename In, typename Out> |
| 44 | 44 |
typename disable_if<lemon::UndirectedTagIndicator<Digraph>, |
| 45 | 45 |
typename In::value_type::second_type >::type |
| 46 | 46 |
kruskal(const Digraph& digraph, const In& in, Out& out,dummy<0> = 0) {
|
| 47 | 47 |
typedef typename In::value_type::second_type Value; |
| 48 | 48 |
typedef typename Digraph::template NodeMap<int> IndexMap; |
| 49 | 49 |
typedef typename Digraph::Node Node; |
| 50 | 50 |
|
| 51 | 51 |
IndexMap index(digraph); |
| 52 | 52 |
UnionFind<IndexMap> uf(index); |
| 53 | 53 |
for (typename Digraph::NodeIt it(digraph); it != INVALID; ++it) {
|
| 54 | 54 |
uf.insert(it); |
| 55 | 55 |
} |
| 56 | 56 |
|
| 57 | 57 |
Value tree_value = 0; |
| 58 | 58 |
for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) {
|
| 59 | 59 |
if (uf.join(digraph.target(it->first),digraph.source(it->first))) {
|
| 60 | 60 |
out.set(it->first, true); |
| 61 | 61 |
tree_value += it->second; |
| 62 | 62 |
} |
| 63 | 63 |
else {
|
| 64 | 64 |
out.set(it->first, false); |
| 65 | 65 |
} |
| 66 | 66 |
} |
| 67 | 67 |
return tree_value; |
| 68 | 68 |
} |
| 69 | 69 |
|
| 70 | 70 |
// Kruskal for undirected graphs. |
| 71 | 71 |
|
| 72 | 72 |
template <typename Graph, typename In, typename Out> |
| 73 | 73 |
typename enable_if<lemon::UndirectedTagIndicator<Graph>, |
| 74 | 74 |
typename In::value_type::second_type >::type |
| 75 | 75 |
kruskal(const Graph& graph, const In& in, Out& out,dummy<1> = 1) {
|
| 76 | 76 |
typedef typename In::value_type::second_type Value; |
| 77 | 77 |
typedef typename Graph::template NodeMap<int> IndexMap; |
| 78 | 78 |
typedef typename Graph::Node Node; |
| 79 | 79 |
|
| 80 | 80 |
IndexMap index(graph); |
| 81 | 81 |
UnionFind<IndexMap> uf(index); |
| 82 | 82 |
for (typename Graph::NodeIt it(graph); it != INVALID; ++it) {
|
| 83 | 83 |
uf.insert(it); |
| 84 | 84 |
} |
| 85 | 85 |
|
| 86 | 86 |
Value tree_value = 0; |
| 87 | 87 |
for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) {
|
| 88 | 88 |
if (uf.join(graph.u(it->first),graph.v(it->first))) {
|
| 89 | 89 |
out.set(it->first, true); |
| 90 | 90 |
tree_value += it->second; |
| 91 | 91 |
} |
| 92 | 92 |
else {
|
| 93 | 93 |
out.set(it->first, false); |
| 94 | 94 |
} |
| 95 | 95 |
} |
| 96 | 96 |
return tree_value; |
| 97 | 97 |
} |
| 98 | 98 |
|
| 99 | 99 |
|
| 100 | 100 |
template <typename Sequence> |
| 101 | 101 |
struct PairComp {
|
| 102 | 102 |
typedef typename Sequence::value_type Value; |
| 103 | 103 |
bool operator()(const Value& left, const Value& right) {
|
| 104 | 104 |
return left.second < right.second; |
| 105 | 105 |
} |
| 106 | 106 |
}; |
| 107 | 107 |
|
| 108 | 108 |
template <typename In, typename Enable = void> |
| 109 | 109 |
struct SequenceInputIndicator {
|
| 110 | 110 |
static const bool value = false; |
| 111 | 111 |
}; |
| 112 | 112 |
|
| 113 | 113 |
template <typename In> |
| 114 | 114 |
struct SequenceInputIndicator<In, |
| 115 | 115 |
typename exists<typename In::value_type::first_type>::type> {
|
| 116 | 116 |
static const bool value = true; |
| 117 | 117 |
}; |
| 118 | 118 |
|
| 119 | 119 |
template <typename In, typename Enable = void> |
| 120 | 120 |
struct MapInputIndicator {
|
| 121 | 121 |
static const bool value = false; |
| 122 | 122 |
}; |
| 123 | 123 |
|
| 124 | 124 |
template <typename In> |
| 125 | 125 |
struct MapInputIndicator<In, |
| 126 | 126 |
typename exists<typename In::Value>::type> {
|
| 127 | 127 |
static const bool value = true; |
| 128 | 128 |
}; |
| 129 | 129 |
|
| 130 | 130 |
template <typename In, typename Enable = void> |
| 131 | 131 |
struct SequenceOutputIndicator {
|
| 132 | 132 |
static const bool value = false; |
| 133 | 133 |
}; |
| 134 | 134 |
|
| 135 | 135 |
template <typename Out> |
| 136 | 136 |
struct SequenceOutputIndicator<Out, |
| 137 | 137 |
typename exists<typename Out::value_type>::type> {
|
| 138 | 138 |
static const bool value = true; |
| 139 | 139 |
}; |
| 140 | 140 |
|
| 141 | 141 |
template <typename Out, typename Enable = void> |
| 142 | 142 |
struct MapOutputIndicator {
|
| 143 | 143 |
static const bool value = false; |
| 144 | 144 |
}; |
| 145 | 145 |
|
| 146 | 146 |
template <typename Out> |
| 147 | 147 |
struct MapOutputIndicator<Out, |
| 148 | 148 |
typename exists<typename Out::Value>::type> {
|
| 149 | 149 |
static const bool value = true; |
| 150 | 150 |
}; |
| 151 | 151 |
|
| 152 | 152 |
template <typename In, typename InEnable = void> |
| 153 | 153 |
struct KruskalValueSelector {};
|
| 154 | 154 |
|
| 155 | 155 |
template <typename In> |
| 156 | 156 |
struct KruskalValueSelector<In, |
| 157 | 157 |
typename enable_if<SequenceInputIndicator<In>, void>::type> |
| 158 | 158 |
{
|
| 159 | 159 |
typedef typename In::value_type::second_type Value; |
| 160 | 160 |
}; |
| 161 | 161 |
|
| 162 | 162 |
template <typename In> |
| 163 | 163 |
struct KruskalValueSelector<In, |
| 164 | 164 |
typename enable_if<MapInputIndicator<In>, void>::type> |
| 165 | 165 |
{
|
| 166 | 166 |
typedef typename In::Value Value; |
| 167 | 167 |
}; |
| 168 | 168 |
|
| 169 | 169 |
template <typename Graph, typename In, typename Out, |
| 170 | 170 |
typename InEnable = void> |
| 171 | 171 |
struct KruskalInputSelector {};
|
| 172 | 172 |
|
| 173 | 173 |
template <typename Graph, typename In, typename Out, |
| 174 | 174 |
typename InEnable = void> |
| 175 | 175 |
struct KruskalOutputSelector {};
|
| 176 | 176 |
|
| 177 | 177 |
template <typename Graph, typename In, typename Out> |
| 178 | 178 |
struct KruskalInputSelector<Graph, In, Out, |
| 179 | 179 |
typename enable_if<SequenceInputIndicator<In>, void>::type > |
| 180 | 180 |
{
|
| 181 | 181 |
typedef typename In::value_type::second_type Value; |
| 182 | 182 |
|
| 183 | 183 |
static Value kruskal(const Graph& graph, const In& in, Out& out) {
|
| 184 | 184 |
return KruskalOutputSelector<Graph, In, Out>:: |
| 185 | 185 |
kruskal(graph, in, out); |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
}; |
| 189 | 189 |
|
| 190 | 190 |
template <typename Graph, typename In, typename Out> |
| 191 | 191 |
struct KruskalInputSelector<Graph, In, Out, |
| 192 | 192 |
typename enable_if<MapInputIndicator<In>, void>::type > |
| 193 | 193 |
{
|
| 194 | 194 |
typedef typename In::Value Value; |
| 195 | 195 |
static Value kruskal(const Graph& graph, const In& in, Out& out) {
|
| 196 | 196 |
typedef typename In::Key MapArc; |
| 197 | 197 |
typedef typename In::Value Value; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup lemon_io |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief \ref lgf-format "LEMON Graph Format" reader. |
| 22 | 22 |
|
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_LGF_READER_H |
| 25 | 25 |
#define LEMON_LGF_READER_H |
| 26 | 26 |
|
| 27 | 27 |
#include <iostream> |
| 28 | 28 |
#include <fstream> |
| 29 | 29 |
#include <sstream> |
| 30 | 30 |
|
| 31 | 31 |
#include <set> |
| 32 | 32 |
#include <map> |
| 33 | 33 |
|
| 34 | 34 |
#include <lemon/core.h> |
| 35 | 35 |
|
| 36 | 36 |
#include <lemon/lgf_writer.h> |
| 37 | 37 |
|
| 38 | 38 |
#include <lemon/concept_check.h> |
| 39 | 39 |
#include <lemon/concepts/maps.h> |
| 40 | 40 |
|
| 41 | 41 |
namespace lemon {
|
| 42 | 42 |
|
| 43 | 43 |
namespace _reader_bits {
|
| 44 | 44 |
|
| 45 | 45 |
template <typename Value> |
| 46 | 46 |
struct DefaultConverter {
|
| 47 | 47 |
Value operator()(const std::string& str) {
|
| 48 | 48 |
std::istringstream is(str); |
| 49 | 49 |
Value value; |
| 50 | 50 |
if (!(is >> value)) {
|
| 51 | 51 |
throw FormatError("Cannot read token");
|
| 52 | 52 |
} |
| 53 | 53 |
|
| 54 | 54 |
char c; |
| 55 | 55 |
if (is >> std::ws >> c) {
|
| 56 | 56 |
throw FormatError("Remaining characters in token");
|
| 57 | 57 |
} |
| 58 | 58 |
return value; |
| 59 | 59 |
} |
| 60 | 60 |
}; |
| 61 | 61 |
|
| 62 | 62 |
template <> |
| 63 | 63 |
struct DefaultConverter<std::string> {
|
| 64 | 64 |
std::string operator()(const std::string& str) {
|
| 65 | 65 |
return str; |
| 66 | 66 |
} |
| 67 | 67 |
}; |
| 68 | 68 |
|
| 69 | 69 |
template <typename _Item> |
| 70 | 70 |
class MapStorageBase {
|
| 71 | 71 |
public: |
| 72 | 72 |
typedef _Item Item; |
| 73 | 73 |
|
| 74 | 74 |
public: |
| 75 | 75 |
MapStorageBase() {}
|
| 76 | 76 |
virtual ~MapStorageBase() {}
|
| 77 | 77 |
|
| 78 | 78 |
virtual void set(const Item& item, const std::string& value) = 0; |
| 79 | 79 |
|
| 80 | 80 |
}; |
| 81 | 81 |
|
| 82 | 82 |
template <typename _Item, typename _Map, |
| 83 | 83 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
| 84 | 84 |
class MapStorage : public MapStorageBase<_Item> {
|
| 85 | 85 |
public: |
| 86 | 86 |
typedef _Map Map; |
| 87 | 87 |
typedef _Converter Converter; |
| 88 | 88 |
typedef _Item Item; |
| 89 | 89 |
|
| 90 | 90 |
private: |
| 91 | 91 |
Map& _map; |
| 92 | 92 |
Converter _converter; |
| 93 | 93 |
|
| 94 | 94 |
public: |
| 95 | 95 |
MapStorage(Map& map, const Converter& converter = Converter()) |
| 96 | 96 |
: _map(map), _converter(converter) {}
|
| 97 | 97 |
virtual ~MapStorage() {}
|
| 98 | 98 |
|
| 99 | 99 |
virtual void set(const Item& item ,const std::string& value) {
|
| 100 | 100 |
_map.set(item, _converter(value)); |
| 101 | 101 |
} |
| 102 | 102 |
}; |
| 103 | 103 |
|
| 104 | 104 |
template <typename _Graph, bool _dir, typename _Map, |
| 105 | 105 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
| 106 | 106 |
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
|
| 107 | 107 |
public: |
| 108 | 108 |
typedef _Map Map; |
| 109 | 109 |
typedef _Converter Converter; |
| 110 | 110 |
typedef _Graph Graph; |
| 111 | 111 |
typedef typename Graph::Edge Item; |
| 112 | 112 |
static const bool dir = _dir; |
| 113 | 113 |
|
| 114 | 114 |
private: |
| 115 | 115 |
const Graph& _graph; |
| 116 | 116 |
Map& _map; |
| 117 | 117 |
Converter _converter; |
| 118 | 118 |
|
| 119 | 119 |
public: |
| 120 | 120 |
GraphArcMapStorage(const Graph& graph, Map& map, |
| 121 | 121 |
const Converter& converter = Converter()) |
| 122 | 122 |
: _graph(graph), _map(map), _converter(converter) {}
|
| 123 | 123 |
virtual ~GraphArcMapStorage() {}
|
| 124 | 124 |
|
| 125 | 125 |
virtual void set(const Item& item ,const std::string& value) {
|
| 126 | 126 |
_map.set(_graph.direct(item, dir), _converter(value)); |
| 127 | 127 |
} |
| 128 | 128 |
}; |
| 129 | 129 |
|
| 130 | 130 |
class ValueStorageBase {
|
| 131 | 131 |
public: |
| 132 | 132 |
ValueStorageBase() {}
|
| 133 | 133 |
virtual ~ValueStorageBase() {}
|
| 134 | 134 |
|
| 135 | 135 |
virtual void set(const std::string&) = 0; |
| 136 | 136 |
}; |
| 137 | 137 |
|
| 138 | 138 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
| 139 | 139 |
class ValueStorage : public ValueStorageBase {
|
| 140 | 140 |
public: |
| 141 | 141 |
typedef _Value Value; |
| 142 | 142 |
typedef _Converter Converter; |
| 143 | 143 |
|
| 144 | 144 |
private: |
| 145 | 145 |
Value& _value; |
| 146 | 146 |
Converter _converter; |
| 147 | 147 |
|
| 148 | 148 |
public: |
| 149 | 149 |
ValueStorage(Value& value, const Converter& converter = Converter()) |
| 150 | 150 |
: _value(value), _converter(converter) {}
|
| 151 | 151 |
|
| 152 | 152 |
virtual void set(const std::string& value) {
|
| 153 | 153 |
_value = _converter(value); |
| 154 | 154 |
} |
| 155 | 155 |
}; |
| 156 | 156 |
|
| 157 | 157 |
template <typename Value> |
| 158 | 158 |
struct MapLookUpConverter {
|
| 159 | 159 |
const std::map<std::string, Value>& _map; |
| 160 | 160 |
|
| 161 | 161 |
MapLookUpConverter(const std::map<std::string, Value>& map) |
| 162 | 162 |
: _map(map) {}
|
| 163 | 163 |
|
| 164 | 164 |
Value operator()(const std::string& str) {
|
| 165 | 165 |
typename std::map<std::string, Value>::const_iterator it = |
| 166 | 166 |
_map.find(str); |
| 167 | 167 |
if (it == _map.end()) {
|
| 168 | 168 |
std::ostringstream msg; |
| 169 | 169 |
msg << "Item not found: " << str; |
| 170 | 170 |
throw FormatError(msg.str()); |
| 171 | 171 |
} |
| 172 | 172 |
return it->second; |
| 173 | 173 |
} |
| 174 | 174 |
}; |
| 175 | 175 |
|
| 176 | 176 |
template <typename Graph> |
| 177 | 177 |
struct GraphArcLookUpConverter {
|
| 178 | 178 |
const Graph& _graph; |
| 179 | 179 |
const std::map<std::string, typename Graph::Edge>& _map; |
| 180 | 180 |
|
| 181 | 181 |
GraphArcLookUpConverter(const Graph& graph, |
| 182 | 182 |
const std::map<std::string, |
| 183 | 183 |
typename Graph::Edge>& map) |
| 184 | 184 |
: _graph(graph), _map(map) {}
|
| 185 | 185 |
|
| 186 | 186 |
typename Graph::Arc operator()(const std::string& str) {
|
| 187 | 187 |
if (str.empty() || (str[0] != '+' && str[0] != '-')) {
|
| 188 | 188 |
throw FormatError("Item must start with '+' or '-'");
|
| 189 | 189 |
} |
| 190 | 190 |
typename std::map<std::string, typename Graph::Edge> |
| 191 | 191 |
::const_iterator it = _map.find(str.substr(1)); |
| 192 | 192 |
if (it == _map.end()) {
|
| 193 | 193 |
throw FormatError("Item not found");
|
| 194 | 194 |
} |
| 195 | 195 |
return _graph.direct(it->second, str[0] == '+'); |
| 196 | 196 |
} |
| 197 | 197 |
}; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup lemon_io |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief \ref lgf-format "LEMON Graph Format" writer. |
| 22 | 22 |
|
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_LGF_WRITER_H |
| 25 | 25 |
#define LEMON_LGF_WRITER_H |
| 26 | 26 |
|
| 27 | 27 |
#include <iostream> |
| 28 | 28 |
#include <fstream> |
| 29 | 29 |
#include <sstream> |
| 30 | 30 |
|
| 31 | 31 |
#include <algorithm> |
| 32 | 32 |
|
| 33 | 33 |
#include <vector> |
| 34 | 34 |
#include <functional> |
| 35 | 35 |
|
| 36 | 36 |
#include <lemon/core.h> |
| 37 | 37 |
#include <lemon/maps.h> |
| 38 | 38 |
|
| 39 | 39 |
#include <lemon/concept_check.h> |
| 40 | 40 |
#include <lemon/concepts/maps.h> |
| 41 | 41 |
|
| 42 | 42 |
namespace lemon {
|
| 43 | 43 |
|
| 44 | 44 |
namespace _writer_bits {
|
| 45 | 45 |
|
| 46 | 46 |
template <typename Value> |
| 47 | 47 |
struct DefaultConverter {
|
| 48 | 48 |
std::string operator()(const Value& value) {
|
| 49 | 49 |
std::ostringstream os; |
| 50 | 50 |
os << value; |
| 51 | 51 |
return os.str(); |
| 52 | 52 |
} |
| 53 | 53 |
}; |
| 54 | 54 |
|
| 55 | 55 |
template <typename T> |
| 56 | 56 |
bool operator<(const T&, const T&) {
|
| 57 | 57 |
throw FormatError("Label map is not comparable");
|
| 58 | 58 |
} |
| 59 | 59 |
|
| 60 | 60 |
template <typename _Map> |
| 61 | 61 |
class MapLess {
|
| 62 | 62 |
public: |
| 63 | 63 |
typedef _Map Map; |
| 64 | 64 |
typedef typename Map::Key Item; |
| 65 | 65 |
|
| 66 | 66 |
private: |
| 67 | 67 |
const Map& _map; |
| 68 | 68 |
|
| 69 | 69 |
public: |
| 70 | 70 |
MapLess(const Map& map) : _map(map) {}
|
| 71 | 71 |
|
| 72 | 72 |
bool operator()(const Item& left, const Item& right) {
|
| 73 | 73 |
return _map[left] < _map[right]; |
| 74 | 74 |
} |
| 75 | 75 |
}; |
| 76 | 76 |
|
| 77 | 77 |
template <typename _Graph, bool _dir, typename _Map> |
| 78 | 78 |
class GraphArcMapLess {
|
| 79 | 79 |
public: |
| 80 | 80 |
typedef _Map Map; |
| 81 | 81 |
typedef _Graph Graph; |
| 82 | 82 |
typedef typename Graph::Edge Item; |
| 83 | 83 |
|
| 84 | 84 |
private: |
| 85 | 85 |
const Graph& _graph; |
| 86 | 86 |
const Map& _map; |
| 87 | 87 |
|
| 88 | 88 |
public: |
| 89 | 89 |
GraphArcMapLess(const Graph& graph, const Map& map) |
| 90 | 90 |
: _graph(graph), _map(map) {}
|
| 91 | 91 |
|
| 92 | 92 |
bool operator()(const Item& left, const Item& right) {
|
| 93 | 93 |
return _map[_graph.direct(left, _dir)] < |
| 94 | 94 |
_map[_graph.direct(right, _dir)]; |
| 95 | 95 |
} |
| 96 | 96 |
}; |
| 97 | 97 |
|
| 98 | 98 |
template <typename _Item> |
| 99 | 99 |
class MapStorageBase {
|
| 100 | 100 |
public: |
| 101 | 101 |
typedef _Item Item; |
| 102 | 102 |
|
| 103 | 103 |
public: |
| 104 | 104 |
MapStorageBase() {}
|
| 105 | 105 |
virtual ~MapStorageBase() {}
|
| 106 | 106 |
|
| 107 | 107 |
virtual std::string get(const Item& item) = 0; |
| 108 | 108 |
virtual void sort(std::vector<Item>&) = 0; |
| 109 | 109 |
}; |
| 110 | 110 |
|
| 111 | 111 |
template <typename _Item, typename _Map, |
| 112 | 112 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
| 113 | 113 |
class MapStorage : public MapStorageBase<_Item> {
|
| 114 | 114 |
public: |
| 115 | 115 |
typedef _Map Map; |
| 116 | 116 |
typedef _Converter Converter; |
| 117 | 117 |
typedef _Item Item; |
| 118 | 118 |
|
| 119 | 119 |
private: |
| 120 | 120 |
const Map& _map; |
| 121 | 121 |
Converter _converter; |
| 122 | 122 |
|
| 123 | 123 |
public: |
| 124 | 124 |
MapStorage(const Map& map, const Converter& converter = Converter()) |
| 125 | 125 |
: _map(map), _converter(converter) {}
|
| 126 | 126 |
virtual ~MapStorage() {}
|
| 127 | 127 |
|
| 128 | 128 |
virtual std::string get(const Item& item) {
|
| 129 | 129 |
return _converter(_map[item]); |
| 130 | 130 |
} |
| 131 | 131 |
virtual void sort(std::vector<Item>& items) {
|
| 132 | 132 |
MapLess<Map> less(_map); |
| 133 | 133 |
std::sort(items.begin(), items.end(), less); |
| 134 | 134 |
} |
| 135 | 135 |
}; |
| 136 | 136 |
|
| 137 | 137 |
template <typename _Graph, bool _dir, typename _Map, |
| 138 | 138 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
| 139 | 139 |
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
|
| 140 | 140 |
public: |
| 141 | 141 |
typedef _Map Map; |
| 142 | 142 |
typedef _Converter Converter; |
| 143 | 143 |
typedef _Graph Graph; |
| 144 | 144 |
typedef typename Graph::Edge Item; |
| 145 | 145 |
static const bool dir = _dir; |
| 146 | 146 |
|
| 147 | 147 |
private: |
| 148 | 148 |
const Graph& _graph; |
| 149 | 149 |
const Map& _map; |
| 150 | 150 |
Converter _converter; |
| 151 | 151 |
|
| 152 | 152 |
public: |
| 153 | 153 |
GraphArcMapStorage(const Graph& graph, const Map& map, |
| 154 | 154 |
const Converter& converter = Converter()) |
| 155 | 155 |
: _graph(graph), _map(map), _converter(converter) {}
|
| 156 | 156 |
virtual ~GraphArcMapStorage() {}
|
| 157 | 157 |
|
| 158 | 158 |
virtual std::string get(const Item& item) {
|
| 159 | 159 |
return _converter(_map[_graph.direct(item, dir)]); |
| 160 | 160 |
} |
| 161 | 161 |
virtual void sort(std::vector<Item>& items) {
|
| 162 | 162 |
GraphArcMapLess<Graph, dir, Map> less(_graph, _map); |
| 163 | 163 |
std::sort(items.begin(), items.end(), less); |
| 164 | 164 |
} |
| 165 | 165 |
}; |
| 166 | 166 |
|
| 167 | 167 |
class ValueStorageBase {
|
| 168 | 168 |
public: |
| 169 | 169 |
ValueStorageBase() {}
|
| 170 | 170 |
virtual ~ValueStorageBase() {}
|
| 171 | 171 |
|
| 172 | 172 |
virtual std::string get() = 0; |
| 173 | 173 |
}; |
| 174 | 174 |
|
| 175 | 175 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
| 176 | 176 |
class ValueStorage : public ValueStorageBase {
|
| 177 | 177 |
public: |
| 178 | 178 |
typedef _Value Value; |
| 179 | 179 |
typedef _Converter Converter; |
| 180 | 180 |
|
| 181 | 181 |
private: |
| 182 | 182 |
const Value& _value; |
| 183 | 183 |
Converter _converter; |
| 184 | 184 |
|
| 185 | 185 |
public: |
| 186 | 186 |
ValueStorage(const Value& value, const Converter& converter = Converter()) |
| 187 | 187 |
: _value(value), _converter(converter) {}
|
| 188 | 188 |
|
| 189 | 189 |
virtual std::string get() {
|
| 190 | 190 |
return _converter(_value); |
| 191 | 191 |
} |
| 192 | 192 |
}; |
| 193 | 193 |
|
| 194 | 194 |
template <typename Value> |
| 195 | 195 |
struct MapLookUpConverter {
|
| 196 | 196 |
const std::map<Value, std::string>& _map; |
| 197 | 197 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_LIST_GRAPH_H |
| 20 | 20 |
#define LEMON_LIST_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graphs |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief ListDigraph, ListGraph classes. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
#include <lemon/error.h> |
| 28 | 28 |
#include <lemon/bits/graph_extender.h> |
| 29 | 29 |
|
| 30 | 30 |
#include <vector> |
| 31 | 31 |
#include <list> |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
class ListDigraphBase {
|
| 36 | 36 |
|
| 37 | 37 |
protected: |
| 38 | 38 |
struct NodeT {
|
| 39 | 39 |
int first_in, first_out; |
| 40 | 40 |
int prev, next; |
| 41 | 41 |
}; |
| 42 | 42 |
|
| 43 | 43 |
struct ArcT {
|
| 44 | 44 |
int target, source; |
| 45 | 45 |
int prev_in, prev_out; |
| 46 | 46 |
int next_in, next_out; |
| 47 | 47 |
}; |
| 48 | 48 |
|
| 49 | 49 |
std::vector<NodeT> nodes; |
| 50 | 50 |
|
| 51 | 51 |
int first_node; |
| 52 | 52 |
|
| 53 | 53 |
int first_free_node; |
| 54 | 54 |
|
| 55 | 55 |
std::vector<ArcT> arcs; |
| 56 | 56 |
|
| 57 | 57 |
int first_free_arc; |
| 58 | 58 |
|
| 59 | 59 |
public: |
| 60 | 60 |
|
| 61 | 61 |
typedef ListDigraphBase Digraph; |
| 62 | 62 |
|
| 63 | 63 |
class Node {
|
| 64 | 64 |
friend class ListDigraphBase; |
| 65 | 65 |
protected: |
| 66 | 66 |
|
| 67 | 67 |
int id; |
| 68 | 68 |
explicit Node(int pid) { id = pid;}
|
| 69 | 69 |
|
| 70 | 70 |
public: |
| 71 | 71 |
Node() {}
|
| 72 | 72 |
Node (Invalid) { id = -1; }
|
| 73 | 73 |
bool operator==(const Node& node) const {return id == node.id;}
|
| 74 | 74 |
bool operator!=(const Node& node) const {return id != node.id;}
|
| 75 | 75 |
bool operator<(const Node& node) const {return id < node.id;}
|
| 76 | 76 |
}; |
| 77 | 77 |
|
| 78 | 78 |
class Arc {
|
| 79 | 79 |
friend class ListDigraphBase; |
| 80 | 80 |
protected: |
| 81 | 81 |
|
| 82 | 82 |
int id; |
| 83 | 83 |
explicit Arc(int pid) { id = pid;}
|
| 84 | 84 |
|
| 85 | 85 |
public: |
| 86 | 86 |
Arc() {}
|
| 87 | 87 |
Arc (Invalid) { id = -1; }
|
| 88 | 88 |
bool operator==(const Arc& arc) const {return id == arc.id;}
|
| 89 | 89 |
bool operator!=(const Arc& arc) const {return id != arc.id;}
|
| 90 | 90 |
bool operator<(const Arc& arc) const {return id < arc.id;}
|
| 91 | 91 |
}; |
| 92 | 92 |
|
| 93 | 93 |
|
| 94 | 94 |
|
| 95 | 95 |
ListDigraphBase() |
| 96 | 96 |
: nodes(), first_node(-1), |
| 97 | 97 |
first_free_node(-1), arcs(), first_free_arc(-1) {}
|
| 98 | 98 |
|
| 99 | 99 |
|
| 100 | 100 |
int maxNodeId() const { return nodes.size()-1; }
|
| 101 | 101 |
int maxArcId() const { return arcs.size()-1; }
|
| 102 | 102 |
|
| 103 | 103 |
Node source(Arc e) const { return Node(arcs[e.id].source); }
|
| 104 | 104 |
Node target(Arc e) const { return Node(arcs[e.id].target); }
|
| 105 | 105 |
|
| 106 | 106 |
|
| 107 | 107 |
void first(Node& node) const {
|
| 108 | 108 |
node.id = first_node; |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 | 111 |
void next(Node& node) const {
|
| 112 | 112 |
node.id = nodes[node.id].next; |
| 113 | 113 |
} |
| 114 | 114 |
|
| 115 | 115 |
|
| 116 | 116 |
void first(Arc& arc) const {
|
| 117 | 117 |
int n; |
| 118 | 118 |
for(n = first_node; |
| 119 | 119 |
n!=-1 && nodes[n].first_in == -1; |
| 120 | 120 |
n = nodes[n].next) {}
|
| 121 | 121 |
arc.id = (n == -1) ? -1 : nodes[n].first_in; |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
void next(Arc& arc) const {
|
| 125 | 125 |
if (arcs[arc.id].next_in != -1) {
|
| 126 | 126 |
arc.id = arcs[arc.id].next_in; |
| 127 | 127 |
} else {
|
| 128 | 128 |
int n; |
| 129 | 129 |
for(n = nodes[arcs[arc.id].target].next; |
| 130 | 130 |
n!=-1 && nodes[n].first_in == -1; |
| 131 | 131 |
n = nodes[n].next) {}
|
| 132 | 132 |
arc.id = (n == -1) ? -1 : nodes[n].first_in; |
| 133 | 133 |
} |
| 134 | 134 |
} |
| 135 | 135 |
|
| 136 | 136 |
void firstOut(Arc &e, const Node& v) const {
|
| 137 | 137 |
e.id = nodes[v.id].first_out; |
| 138 | 138 |
} |
| 139 | 139 |
void nextOut(Arc &e) const {
|
| 140 | 140 |
e.id=arcs[e.id].next_out; |
| 141 | 141 |
} |
| 142 | 142 |
|
| 143 | 143 |
void firstIn(Arc &e, const Node& v) const {
|
| 144 | 144 |
e.id = nodes[v.id].first_in; |
| 145 | 145 |
} |
| 146 | 146 |
void nextIn(Arc &e) const {
|
| 147 | 147 |
e.id=arcs[e.id].next_in; |
| 148 | 148 |
} |
| 149 | 149 |
|
| 150 | 150 |
|
| 151 | 151 |
static int id(Node v) { return v.id; }
|
| 152 | 152 |
static int id(Arc e) { return e.id; }
|
| 153 | 153 |
|
| 154 | 154 |
static Node nodeFromId(int id) { return Node(id);}
|
| 155 | 155 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 156 | 156 |
|
| 157 | 157 |
bool valid(Node n) const {
|
| 158 | 158 |
return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
| 159 | 159 |
nodes[n.id].prev != -2; |
| 160 | 160 |
} |
| 161 | 161 |
|
| 162 | 162 |
bool valid(Arc a) const {
|
| 163 | 163 |
return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
| 164 | 164 |
arcs[a.id].prev_in != -2; |
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 | 167 |
Node addNode() {
|
| 168 | 168 |
int n; |
| 169 | 169 |
|
| 170 | 170 |
if(first_free_node==-1) {
|
| 171 | 171 |
n = nodes.size(); |
| 172 | 172 |
nodes.push_back(NodeT()); |
| 173 | 173 |
} else {
|
| 174 | 174 |
n = first_free_node; |
| 175 | 175 |
first_free_node = nodes[n].next; |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
nodes[n].next = first_node; |
| 179 | 179 |
if(first_node != -1) nodes[first_node].prev = n; |
| 180 | 180 |
first_node = n; |
| 181 | 181 |
nodes[n].prev = -1; |
| 182 | 182 |
|
| 183 | 183 |
nodes[n].first_in = nodes[n].first_out = -1; |
| 184 | 184 |
|
| 185 | 185 |
return Node(n); |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
Arc addArc(Node u, Node v) {
|
| 189 | 189 |
int n; |
| 190 | 190 |
|
| 191 | 191 |
if (first_free_arc == -1) {
|
| 192 | 192 |
n = arcs.size(); |
| 193 | 193 |
arcs.push_back(ArcT()); |
| 194 | 194 |
} else {
|
| 195 | 195 |
n = first_free_arc; |
| 196 | 196 |
first_free_arc = arcs[n].next_in; |
| 197 | 197 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_MAPS_H |
| 20 | 20 |
#define LEMON_MAPS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iterator> |
| 23 | 23 |
#include <functional> |
| 24 | 24 |
#include <vector> |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
|
| 28 | 28 |
///\file |
| 29 | 29 |
///\ingroup maps |
| 30 | 30 |
///\brief Miscellaneous property maps |
| 31 | 31 |
|
| 32 | 32 |
#include <map> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \addtogroup maps |
| 37 | 37 |
/// @{
|
| 38 | 38 |
|
| 39 | 39 |
/// Base class of maps. |
| 40 | 40 |
|
| 41 | 41 |
/// Base class of maps. It provides the necessary type definitions |
| 42 | 42 |
/// required by the map %concepts. |
| 43 | 43 |
template<typename K, typename V> |
| 44 | 44 |
class MapBase {
|
| 45 | 45 |
public: |
| 46 | 46 |
/// \brief The key type of the map. |
| 47 | 47 |
typedef K Key; |
| 48 | 48 |
/// \brief The value type of the map. |
| 49 | 49 |
/// (The type of objects associated with the keys). |
| 50 | 50 |
typedef V Value; |
| 51 | 51 |
}; |
| 52 | 52 |
|
| 53 | 53 |
|
| 54 | 54 |
/// Null map. (a.k.a. DoNothingMap) |
| 55 | 55 |
|
| 56 | 56 |
/// This map can be used if you have to provide a map only for |
| 57 | 57 |
/// its type definitions, or if you have to provide a writable map, |
| 58 | 58 |
/// but data written to it is not required (i.e. it will be sent to |
| 59 | 59 |
/// <tt>/dev/null</tt>). |
| 60 | 60 |
/// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 61 | 61 |
/// |
| 62 | 62 |
/// \sa ConstMap |
| 63 | 63 |
template<typename K, typename V> |
| 64 | 64 |
class NullMap : public MapBase<K, V> {
|
| 65 | 65 |
public: |
| 66 | 66 |
typedef MapBase<K, V> Parent; |
| 67 | 67 |
typedef typename Parent::Key Key; |
| 68 | 68 |
typedef typename Parent::Value Value; |
| 69 | 69 |
|
| 70 | 70 |
/// Gives back a default constructed element. |
| 71 | 71 |
Value operator[](const Key&) const { return Value(); }
|
| 72 | 72 |
/// Absorbs the value. |
| 73 | 73 |
void set(const Key&, const Value&) {}
|
| 74 | 74 |
}; |
| 75 | 75 |
|
| 76 | 76 |
/// Returns a \c NullMap class |
| 77 | 77 |
|
| 78 | 78 |
/// This function just returns a \c NullMap class. |
| 79 | 79 |
/// \relates NullMap |
| 80 | 80 |
template <typename K, typename V> |
| 81 | 81 |
NullMap<K, V> nullMap() {
|
| 82 | 82 |
return NullMap<K, V>(); |
| 83 | 83 |
} |
| 84 | 84 |
|
| 85 | 85 |
|
| 86 | 86 |
/// Constant map. |
| 87 | 87 |
|
| 88 | 88 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
| 89 | 89 |
/// value to each key. |
| 90 | 90 |
/// |
| 91 | 91 |
/// In other aspects it is equivalent to \c NullMap. |
| 92 | 92 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
| 93 | 93 |
/// concept, but it absorbs the data written to it. |
| 94 | 94 |
/// |
| 95 | 95 |
/// The simplest way of using this map is through the constMap() |
| 96 | 96 |
/// function. |
| 97 | 97 |
/// |
| 98 | 98 |
/// \sa NullMap |
| 99 | 99 |
/// \sa IdentityMap |
| 100 | 100 |
template<typename K, typename V> |
| 101 | 101 |
class ConstMap : public MapBase<K, V> {
|
| 102 | 102 |
private: |
| 103 | 103 |
V _value; |
| 104 | 104 |
public: |
| 105 | 105 |
typedef MapBase<K, V> Parent; |
| 106 | 106 |
typedef typename Parent::Key Key; |
| 107 | 107 |
typedef typename Parent::Value Value; |
| 108 | 108 |
|
| 109 | 109 |
/// Default constructor |
| 110 | 110 |
|
| 111 | 111 |
/// Default constructor. |
| 112 | 112 |
/// The value of the map will be default constructed. |
| 113 | 113 |
ConstMap() {}
|
| 114 | 114 |
|
| 115 | 115 |
/// Constructor with specified initial value |
| 116 | 116 |
|
| 117 | 117 |
/// Constructor with specified initial value. |
| 118 | 118 |
/// \param v The initial value of the map. |
| 119 | 119 |
ConstMap(const Value &v) : _value(v) {}
|
| 120 | 120 |
|
| 121 | 121 |
/// Gives back the specified value. |
| 122 | 122 |
Value operator[](const Key&) const { return _value; }
|
| 123 | 123 |
|
| 124 | 124 |
/// Absorbs the value. |
| 125 | 125 |
void set(const Key&, const Value&) {}
|
| 126 | 126 |
|
| 127 | 127 |
/// Sets the value that is assigned to each key. |
| 128 | 128 |
void setAll(const Value &v) {
|
| 129 | 129 |
_value = v; |
| 130 | 130 |
} |
| 131 | 131 |
|
| 132 | 132 |
template<typename V1> |
| 133 | 133 |
ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {}
|
| 134 | 134 |
}; |
| 135 | 135 |
|
| 136 | 136 |
/// Returns a \c ConstMap class |
| 137 | 137 |
|
| 138 | 138 |
/// This function just returns a \c ConstMap class. |
| 139 | 139 |
/// \relates ConstMap |
| 140 | 140 |
template<typename K, typename V> |
| 141 | 141 |
inline ConstMap<K, V> constMap(const V &v) {
|
| 142 | 142 |
return ConstMap<K, V>(v); |
| 143 | 143 |
} |
| 144 | 144 |
|
| 145 | 145 |
template<typename K, typename V> |
| 146 | 146 |
inline ConstMap<K, V> constMap() {
|
| 147 | 147 |
return ConstMap<K, V>(); |
| 148 | 148 |
} |
| 149 | 149 |
|
| 150 | 150 |
|
| 151 | 151 |
template<typename T, T v> |
| 152 | 152 |
struct Const {};
|
| 153 | 153 |
|
| 154 | 154 |
/// Constant map with inlined constant value. |
| 155 | 155 |
|
| 156 | 156 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
| 157 | 157 |
/// value to each key. |
| 158 | 158 |
/// |
| 159 | 159 |
/// In other aspects it is equivalent to \c NullMap. |
| 160 | 160 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
| 161 | 161 |
/// concept, but it absorbs the data written to it. |
| 162 | 162 |
/// |
| 163 | 163 |
/// The simplest way of using this map is through the constMap() |
| 164 | 164 |
/// function. |
| 165 | 165 |
/// |
| 166 | 166 |
/// \sa NullMap |
| 167 | 167 |
/// \sa IdentityMap |
| 168 | 168 |
template<typename K, typename V, V v> |
| 169 | 169 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
|
| 170 | 170 |
public: |
| 171 | 171 |
typedef MapBase<K, V> Parent; |
| 172 | 172 |
typedef typename Parent::Key Key; |
| 173 | 173 |
typedef typename Parent::Value Value; |
| 174 | 174 |
|
| 175 | 175 |
/// Constructor. |
| 176 | 176 |
ConstMap() {}
|
| 177 | 177 |
|
| 178 | 178 |
/// Gives back the specified value. |
| 179 | 179 |
Value operator[](const Key&) const { return v; }
|
| 180 | 180 |
|
| 181 | 181 |
/// Absorbs the value. |
| 182 | 182 |
void set(const Key&, const Value&) {}
|
| 183 | 183 |
}; |
| 184 | 184 |
|
| 185 | 185 |
/// Returns a \c ConstMap class with inlined constant value |
| 186 | 186 |
|
| 187 | 187 |
/// This function just returns a \c ConstMap class with inlined |
| 188 | 188 |
/// constant value. |
| 189 | 189 |
/// \relates ConstMap |
| 190 | 190 |
template<typename K, typename V, V v> |
| 191 | 191 |
inline ConstMap<K, Const<V, v> > constMap() {
|
| 192 | 192 |
return ConstMap<K, Const<V, v> >(); |
| 193 | 193 |
} |
| 194 | 194 |
|
| 195 | 195 |
|
| 196 | 196 |
/// Identity map. |
| 197 | 197 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_MATH_H |
| 20 | 20 |
#define LEMON_MATH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup misc |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Some extensions to the standard \c cmath library. |
| 25 | 25 |
/// |
| 26 | 26 |
///Some extensions to the standard \c cmath library. |
| 27 | 27 |
/// |
| 28 | 28 |
///This file includes the standard math library (cmath). |
| 29 | 29 |
|
| 30 | 30 |
#include<cmath> |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
/// \addtogroup misc |
| 35 | 35 |
/// @{
|
| 36 | 36 |
|
| 37 | 37 |
/// The Euler constant |
| 38 | 38 |
const long double E = 2.7182818284590452353602874713526625L; |
| 39 | 39 |
/// log_2(e) |
| 40 | 40 |
const long double LOG2E = 1.4426950408889634073599246810018921L; |
| 41 | 41 |
/// log_10(e) |
| 42 | 42 |
const long double LOG10E = 0.4342944819032518276511289189166051L; |
| 43 | 43 |
/// ln(2) |
| 44 | 44 |
const long double LN2 = 0.6931471805599453094172321214581766L; |
| 45 | 45 |
/// ln(10) |
| 46 | 46 |
const long double LN10 = 2.3025850929940456840179914546843642L; |
| 47 | 47 |
/// pi |
| 48 | 48 |
const long double PI = 3.1415926535897932384626433832795029L; |
| 49 | 49 |
/// pi/2 |
| 50 | 50 |
const long double PI_2 = 1.5707963267948966192313216916397514L; |
| 51 | 51 |
/// pi/4 |
| 52 | 52 |
const long double PI_4 = 0.7853981633974483096156608458198757L; |
| 53 | 53 |
/// sqrt(2) |
| 54 | 54 |
const long double SQRT2 = 1.4142135623730950488016887242096981L; |
| 55 | 55 |
/// 1/sqrt(2) |
| 56 | 56 |
const long double SQRT1_2 = 0.7071067811865475244008443621048490L; |
| 57 | 57 |
|
| 58 | 58 |
|
| 59 | 59 |
/// @} |
| 60 | 60 |
|
| 61 | 61 |
} //namespace lemon |
| 62 | 62 |
|
| 63 | 63 |
#endif //LEMON_TOLERANCE_H |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_MAX_MATCHING_H |
| 20 | 20 |
#define LEMON_MAX_MATCHING_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <queue> |
| 24 | 24 |
#include <set> |
| 25 | 25 |
#include <limits> |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/core.h> |
| 28 | 28 |
#include <lemon/unionfind.h> |
| 29 | 29 |
#include <lemon/bin_heap.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
|
| 32 | 32 |
///\ingroup matching |
| 33 | 33 |
///\file |
| 34 | 34 |
///\brief Maximum matching algorithms in general graphs. |
| 35 | 35 |
|
| 36 | 36 |
namespace lemon {
|
| 37 | 37 |
|
| 38 | 38 |
/// \ingroup matching |
| 39 | 39 |
/// |
| 40 | 40 |
/// \brief Edmonds' alternating forest maximum matching algorithm. |
| 41 | 41 |
/// |
| 42 | 42 |
/// This class implements Edmonds' alternating forest matching |
| 43 | 43 |
/// algorithm. The algorithm can be started from an arbitrary initial |
| 44 | 44 |
/// matching (the default is the empty one) |
| 45 | 45 |
/// |
| 46 | 46 |
/// The dual solution of the problem is a map of the nodes to |
| 47 | 47 |
/// MaxMatching::Status, having values \c EVEN/D, \c ODD/A and \c |
| 48 | 48 |
/// MATCHED/C showing the Gallai-Edmonds decomposition of the |
| 49 | 49 |
/// graph. The nodes in \c EVEN/D induce a graph with |
| 50 | 50 |
/// factor-critical components, the nodes in \c ODD/A form the |
| 51 | 51 |
/// barrier, and the nodes in \c MATCHED/C induce a graph having a |
| 52 | 52 |
/// perfect matching. The number of the factor-critical components |
| 53 | 53 |
/// minus the number of barrier nodes is a lower bound on the |
| 54 | 54 |
/// unmatched nodes, and the matching is optimal if and only if this bound is |
| 55 | 55 |
/// tight. This decomposition can be attained by calling \c |
| 56 | 56 |
/// decomposition() after running the algorithm. |
| 57 | 57 |
/// |
| 58 | 58 |
/// \param _Graph The graph type the algorithm runs on. |
| 59 | 59 |
template <typename _Graph> |
| 60 | 60 |
class MaxMatching {
|
| 61 | 61 |
public: |
| 62 | 62 |
|
| 63 | 63 |
typedef _Graph Graph; |
| 64 | 64 |
typedef typename Graph::template NodeMap<typename Graph::Arc> |
| 65 | 65 |
MatchingMap; |
| 66 | 66 |
|
| 67 | 67 |
///\brief Indicates the Gallai-Edmonds decomposition of the graph. |
| 68 | 68 |
/// |
| 69 | 69 |
///Indicates the Gallai-Edmonds decomposition of the graph. The |
| 70 | 70 |
///nodes with Status \c EVEN/D induce a graph with factor-critical |
| 71 | 71 |
///components, the nodes in \c ODD/A form the canonical barrier, |
| 72 | 72 |
///and the nodes in \c MATCHED/C induce a graph having a perfect |
| 73 | 73 |
///matching. |
| 74 | 74 |
enum Status {
|
| 75 | 75 |
EVEN = 1, D = 1, MATCHED = 0, C = 0, ODD = -1, A = -1, UNMATCHED = -2 |
| 76 | 76 |
}; |
| 77 | 77 |
|
| 78 | 78 |
typedef typename Graph::template NodeMap<Status> StatusMap; |
| 79 | 79 |
|
| 80 | 80 |
private: |
| 81 | 81 |
|
| 82 | 82 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 83 | 83 |
|
| 84 | 84 |
typedef UnionFindEnum<IntNodeMap> BlossomSet; |
| 85 | 85 |
typedef ExtendFindEnum<IntNodeMap> TreeSet; |
| 86 | 86 |
typedef RangeMap<Node> NodeIntMap; |
| 87 | 87 |
typedef MatchingMap EarMap; |
| 88 | 88 |
typedef std::vector<Node> NodeQueue; |
| 89 | 89 |
|
| 90 | 90 |
const Graph& _graph; |
| 91 | 91 |
MatchingMap* _matching; |
| 92 | 92 |
StatusMap* _status; |
| 93 | 93 |
|
| 94 | 94 |
EarMap* _ear; |
| 95 | 95 |
|
| 96 | 96 |
IntNodeMap* _blossom_set_index; |
| 97 | 97 |
BlossomSet* _blossom_set; |
| 98 | 98 |
NodeIntMap* _blossom_rep; |
| 99 | 99 |
|
| 100 | 100 |
IntNodeMap* _tree_set_index; |
| 101 | 101 |
TreeSet* _tree_set; |
| 102 | 102 |
|
| 103 | 103 |
NodeQueue _node_queue; |
| 104 | 104 |
int _process, _postpone, _last; |
| 105 | 105 |
|
| 106 | 106 |
int _node_num; |
| 107 | 107 |
|
| 108 | 108 |
private: |
| 109 | 109 |
|
| 110 | 110 |
void createStructures() {
|
| 111 | 111 |
_node_num = countNodes(_graph); |
| 112 | 112 |
if (!_matching) {
|
| 113 | 113 |
_matching = new MatchingMap(_graph); |
| 114 | 114 |
} |
| 115 | 115 |
if (!_status) {
|
| 116 | 116 |
_status = new StatusMap(_graph); |
| 117 | 117 |
} |
| 118 | 118 |
if (!_ear) {
|
| 119 | 119 |
_ear = new EarMap(_graph); |
| 120 | 120 |
} |
| 121 | 121 |
if (!_blossom_set) {
|
| 122 | 122 |
_blossom_set_index = new IntNodeMap(_graph); |
| 123 | 123 |
_blossom_set = new BlossomSet(*_blossom_set_index); |
| 124 | 124 |
} |
| 125 | 125 |
if (!_blossom_rep) {
|
| 126 | 126 |
_blossom_rep = new NodeIntMap(_node_num); |
| 127 | 127 |
} |
| 128 | 128 |
if (!_tree_set) {
|
| 129 | 129 |
_tree_set_index = new IntNodeMap(_graph); |
| 130 | 130 |
_tree_set = new TreeSet(*_tree_set_index); |
| 131 | 131 |
} |
| 132 | 132 |
_node_queue.resize(_node_num); |
| 133 | 133 |
} |
| 134 | 134 |
|
| 135 | 135 |
void destroyStructures() {
|
| 136 | 136 |
if (_matching) {
|
| 137 | 137 |
delete _matching; |
| 138 | 138 |
} |
| 139 | 139 |
if (_status) {
|
| 140 | 140 |
delete _status; |
| 141 | 141 |
} |
| 142 | 142 |
if (_ear) {
|
| 143 | 143 |
delete _ear; |
| 144 | 144 |
} |
| 145 | 145 |
if (_blossom_set) {
|
| 146 | 146 |
delete _blossom_set; |
| 147 | 147 |
delete _blossom_set_index; |
| 148 | 148 |
} |
| 149 | 149 |
if (_blossom_rep) {
|
| 150 | 150 |
delete _blossom_rep; |
| 151 | 151 |
} |
| 152 | 152 |
if (_tree_set) {
|
| 153 | 153 |
delete _tree_set_index; |
| 154 | 154 |
delete _tree_set; |
| 155 | 155 |
} |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
void processDense(const Node& n) {
|
| 159 | 159 |
_process = _postpone = _last = 0; |
| 160 | 160 |
_node_queue[_last++] = n; |
| 161 | 161 |
|
| 162 | 162 |
while (_process != _last) {
|
| 163 | 163 |
Node u = _node_queue[_process++]; |
| 164 | 164 |
for (OutArcIt a(_graph, u); a != INVALID; ++a) {
|
| 165 | 165 |
Node v = _graph.target(a); |
| 166 | 166 |
if ((*_status)[v] == MATCHED) {
|
| 167 | 167 |
extendOnArc(a); |
| 168 | 168 |
} else if ((*_status)[v] == UNMATCHED) {
|
| 169 | 169 |
augmentOnArc(a); |
| 170 | 170 |
return; |
| 171 | 171 |
} |
| 172 | 172 |
} |
| 173 | 173 |
} |
| 174 | 174 |
|
| 175 | 175 |
while (_postpone != _last) {
|
| 176 | 176 |
Node u = _node_queue[_postpone++]; |
| 177 | 177 |
|
| 178 | 178 |
for (OutArcIt a(_graph, u); a != INVALID ; ++a) {
|
| 179 | 179 |
Node v = _graph.target(a); |
| 180 | 180 |
|
| 181 | 181 |
if ((*_status)[v] == EVEN) {
|
| 182 | 182 |
if (_blossom_set->find(u) != _blossom_set->find(v)) {
|
| 183 | 183 |
shrinkOnEdge(a); |
| 184 | 184 |
} |
| 185 | 185 |
} |
| 186 | 186 |
|
| 187 | 187 |
while (_process != _last) {
|
| 188 | 188 |
Node w = _node_queue[_process++]; |
| 189 | 189 |
for (OutArcIt b(_graph, w); b != INVALID; ++b) {
|
| 190 | 190 |
Node x = _graph.target(b); |
| 191 | 191 |
if ((*_status)[x] == MATCHED) {
|
| 192 | 192 |
extendOnArc(b); |
| 193 | 193 |
} else if ((*_status)[x] == UNMATCHED) {
|
| 194 | 194 |
augmentOnArc(b); |
| 195 | 195 |
return; |
| 196 | 196 |
} |
| 197 | 197 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_NAUTY_READER_H |
| 20 | 20 |
#define LEMON_NAUTY_READER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <iostream> |
| 24 | 24 |
#include <string> |
| 25 | 25 |
|
| 26 | 26 |
/// \ingroup nauty_group |
| 27 | 27 |
/// \file |
| 28 | 28 |
/// \brief Nauty file reader. |
| 29 | 29 |
|
| 30 | 30 |
namespace lemon {
|
| 31 | 31 |
|
| 32 | 32 |
/// \ingroup nauty_group |
| 33 | 33 |
/// |
| 34 | 34 |
/// \brief Nauty file reader |
| 35 | 35 |
/// |
| 36 | 36 |
/// The \e geng program is in the \e gtools suite of the nauty |
| 37 | 37 |
/// package. This tool can generate all non-isomorphic undirected |
| 38 | 38 |
/// graphs of several classes with given node number (e.g. |
| 39 | 39 |
/// general, connected, biconnected, triangle-free, 4-cycle-free, |
| 40 | 40 |
/// bipartite and graphs with given edge number and degree |
| 41 | 41 |
/// constraints). This function reads a \e nauty \e graph6 \e format |
| 42 | 42 |
/// line from the given stream and builds it in the given graph. |
| 43 | 43 |
/// |
| 44 | 44 |
/// The site of nauty package: http://cs.anu.edu.au/~bdm/nauty/ |
| 45 | 45 |
/// |
| 46 | 46 |
/// For example, the number of all non-isomorphic planar graphs |
| 47 | 47 |
/// can be computed with the following code. |
| 48 | 48 |
///\code |
| 49 | 49 |
/// int num = 0; |
| 50 | 50 |
/// SmartGraph graph; |
| 51 | 51 |
/// while (readNautyGraph(graph, std::cin)) {
|
| 52 | 52 |
/// PlanarityChecking<SmartGraph> pc(graph); |
| 53 | 53 |
/// if (pc.run()) ++num; |
| 54 | 54 |
/// } |
| 55 | 55 |
/// std::cout << "Number of planar graphs: " << num << std::endl; |
| 56 | 56 |
///\endcode |
| 57 | 57 |
/// |
| 58 | 58 |
/// The nauty files are quite huge, therefore instead of the direct |
| 59 | 59 |
/// file generation pipelining is recommended. For example, |
| 60 | 60 |
///\code |
| 61 | 61 |
/// ./geng -c 10 | ./num_of_planar_graphs |
| 62 | 62 |
///\endcode |
| 63 | 63 |
template <typename Graph> |
| 64 | 64 |
std::istream& readNautyGraph(Graph& graph, std::istream& is = std::cin) {
|
| 65 | 65 |
graph.clear(); |
| 66 | 66 |
|
| 67 | 67 |
std::string line; |
| 68 | 68 |
if (getline(is, line)) {
|
| 69 | 69 |
int index = 0; |
| 70 | 70 |
|
| 71 | 71 |
int n; |
| 72 | 72 |
|
| 73 | 73 |
if (line[index] == '>') {
|
| 74 | 74 |
index += 10; |
| 75 | 75 |
} |
| 76 | 76 |
|
| 77 | 77 |
char c = line[index++]; c -= 63; |
| 78 | 78 |
if (c != 63) {
|
| 79 | 79 |
n = int(c); |
| 80 | 80 |
} else {
|
| 81 | 81 |
c = line[index++]; c -= 63; |
| 82 | 82 |
n = (int(c) << 12); |
| 83 | 83 |
c = line[index++]; c -= 63; |
| 84 | 84 |
n |= (int(c) << 6); |
| 85 | 85 |
c = line[index++]; c -= 63; |
| 86 | 86 |
n |= int(c); |
| 87 | 87 |
} |
| 88 | 88 |
|
| 89 | 89 |
std::vector<typename Graph::Node> nodes; |
| 90 | 90 |
for (int i = 0; i < n; ++i) {
|
| 91 | 91 |
nodes.push_back(graph.addNode()); |
| 92 | 92 |
} |
| 93 | 93 |
|
| 94 | 94 |
int bit = -1; |
| 95 | 95 |
for (int j = 0; j < n; ++j) {
|
| 96 | 96 |
for (int i = 0; i < j; ++i) {
|
| 97 | 97 |
if (bit == -1) {
|
| 98 | 98 |
c = line[index++]; c -= 63; |
| 99 | 99 |
bit = 5; |
| 100 | 100 |
} |
| 101 | 101 |
bool b = (c & (1 << (bit--))) != 0; |
| 102 | 102 |
|
| 103 | 103 |
if (b) {
|
| 104 | 104 |
graph.addEdge(nodes[i], nodes[j]); |
| 105 | 105 |
} |
| 106 | 106 |
} |
| 107 | 107 |
} |
| 108 | 108 |
} |
| 109 | 109 |
return is; |
| 110 | 110 |
} |
| 111 | 111 |
} |
| 112 | 112 |
|
| 113 | 113 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup paths |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief Classes for representing paths in digraphs. |
| 22 | 22 |
/// |
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_PATH_H |
| 25 | 25 |
#define LEMON_PATH_H |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <algorithm> |
| 29 | 29 |
|
| 30 | 30 |
#include <lemon/error.h> |
| 31 | 31 |
#include <lemon/core.h> |
| 32 | 32 |
#include <lemon/concepts/path.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \addtogroup paths |
| 37 | 37 |
/// @{
|
| 38 | 38 |
|
| 39 | 39 |
|
| 40 | 40 |
/// \brief A structure for representing directed paths in a digraph. |
| 41 | 41 |
/// |
| 42 | 42 |
/// A structure for representing directed path in a digraph. |
| 43 | 43 |
/// \tparam _Digraph The digraph type in which the path is. |
| 44 | 44 |
/// |
| 45 | 45 |
/// In a sense, the path can be treated as a list of arcs. The |
| 46 | 46 |
/// lemon path type stores just this list. As a consequence, it |
| 47 | 47 |
/// cannot enumerate the nodes of the path and the source node of |
| 48 | 48 |
/// a zero length path is undefined. |
| 49 | 49 |
/// |
| 50 | 50 |
/// This implementation is a back and front insertable and erasable |
| 51 | 51 |
/// path type. It can be indexed in O(1) time. The front and back |
| 52 | 52 |
/// insertion and erase is done in O(1) (amortized) time. The |
| 53 | 53 |
/// implementation uses two vectors for storing the front and back |
| 54 | 54 |
/// insertions. |
| 55 | 55 |
template <typename _Digraph> |
| 56 | 56 |
class Path {
|
| 57 | 57 |
public: |
| 58 | 58 |
|
| 59 | 59 |
typedef _Digraph Digraph; |
| 60 | 60 |
typedef typename Digraph::Arc Arc; |
| 61 | 61 |
|
| 62 | 62 |
/// \brief Default constructor |
| 63 | 63 |
/// |
| 64 | 64 |
/// Default constructor |
| 65 | 65 |
Path() {}
|
| 66 | 66 |
|
| 67 | 67 |
/// \brief Template copy constructor |
| 68 | 68 |
/// |
| 69 | 69 |
/// This constuctor initializes the path from any other path type. |
| 70 | 70 |
/// It simply makes a copy of the given path. |
| 71 | 71 |
template <typename CPath> |
| 72 | 72 |
Path(const CPath& cpath) {
|
| 73 | 73 |
copyPath(*this, cpath); |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 | 76 |
/// \brief Template copy assignment |
| 77 | 77 |
/// |
| 78 | 78 |
/// This operator makes a copy of a path of any other type. |
| 79 | 79 |
template <typename CPath> |
| 80 | 80 |
Path& operator=(const CPath& cpath) {
|
| 81 | 81 |
copyPath(*this, cpath); |
| 82 | 82 |
return *this; |
| 83 | 83 |
} |
| 84 | 84 |
|
| 85 | 85 |
/// \brief LEMON style iterator for path arcs |
| 86 | 86 |
/// |
| 87 | 87 |
/// This class is used to iterate on the arcs of the paths. |
| 88 | 88 |
class ArcIt {
|
| 89 | 89 |
friend class Path; |
| 90 | 90 |
public: |
| 91 | 91 |
/// \brief Default constructor |
| 92 | 92 |
ArcIt() {}
|
| 93 | 93 |
/// \brief Invalid constructor |
| 94 | 94 |
ArcIt(Invalid) : path(0), idx(-1) {}
|
| 95 | 95 |
/// \brief Initializate the iterator to the first arc of path |
| 96 | 96 |
ArcIt(const Path &_path) |
| 97 | 97 |
: path(&_path), idx(_path.empty() ? -1 : 0) {}
|
| 98 | 98 |
|
| 99 | 99 |
private: |
| 100 | 100 |
|
| 101 | 101 |
ArcIt(const Path &_path, int _idx) |
| 102 | 102 |
: path(&_path), idx(_idx) {}
|
| 103 | 103 |
|
| 104 | 104 |
public: |
| 105 | 105 |
|
| 106 | 106 |
/// \brief Conversion to Arc |
| 107 | 107 |
operator const Arc&() const {
|
| 108 | 108 |
return path->nth(idx); |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 | 111 |
/// \brief Next arc |
| 112 | 112 |
ArcIt& operator++() {
|
| 113 | 113 |
++idx; |
| 114 | 114 |
if (idx >= path->length()) idx = -1; |
| 115 | 115 |
return *this; |
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 | 118 |
/// \brief Comparison operator |
| 119 | 119 |
bool operator==(const ArcIt& e) const { return idx==e.idx; }
|
| 120 | 120 |
/// \brief Comparison operator |
| 121 | 121 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
|
| 122 | 122 |
/// \brief Comparison operator |
| 123 | 123 |
bool operator<(const ArcIt& e) const { return idx<e.idx; }
|
| 124 | 124 |
|
| 125 | 125 |
private: |
| 126 | 126 |
const Path *path; |
| 127 | 127 |
int idx; |
| 128 | 128 |
}; |
| 129 | 129 |
|
| 130 | 130 |
/// \brief Length of the path. |
| 131 | 131 |
int length() const { return head.size() + tail.size(); }
|
| 132 | 132 |
/// \brief Return whether the path is empty. |
| 133 | 133 |
bool empty() const { return head.empty() && tail.empty(); }
|
| 134 | 134 |
|
| 135 | 135 |
/// \brief Reset the path to an empty one. |
| 136 | 136 |
void clear() { head.clear(); tail.clear(); }
|
| 137 | 137 |
|
| 138 | 138 |
/// \brief The nth arc. |
| 139 | 139 |
/// |
| 140 | 140 |
/// \pre n is in the [0..length() - 1] range |
| 141 | 141 |
const Arc& nth(int n) const {
|
| 142 | 142 |
return n < int(head.size()) ? *(head.rbegin() + n) : |
| 143 | 143 |
*(tail.begin() + (n - head.size())); |
| 144 | 144 |
} |
| 145 | 145 |
|
| 146 | 146 |
/// \brief Initialize arc iterator to point to the nth arc |
| 147 | 147 |
/// |
| 148 | 148 |
/// \pre n is in the [0..length() - 1] range |
| 149 | 149 |
ArcIt nthIt(int n) const {
|
| 150 | 150 |
return ArcIt(*this, n); |
| 151 | 151 |
} |
| 152 | 152 |
|
| 153 | 153 |
/// \brief The first arc of the path |
| 154 | 154 |
const Arc& front() const {
|
| 155 | 155 |
return head.empty() ? tail.front() : head.back(); |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
/// \brief Add a new arc before the current path |
| 159 | 159 |
void addFront(const Arc& arc) {
|
| 160 | 160 |
head.push_back(arc); |
| 161 | 161 |
} |
| 162 | 162 |
|
| 163 | 163 |
/// \brief Erase the first arc of the path |
| 164 | 164 |
void eraseFront() {
|
| 165 | 165 |
if (!head.empty()) {
|
| 166 | 166 |
head.pop_back(); |
| 167 | 167 |
} else {
|
| 168 | 168 |
head.clear(); |
| 169 | 169 |
int halfsize = tail.size() / 2; |
| 170 | 170 |
head.resize(halfsize); |
| 171 | 171 |
std::copy(tail.begin() + 1, tail.begin() + halfsize + 1, |
| 172 | 172 |
head.rbegin()); |
| 173 | 173 |
std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin()); |
| 174 | 174 |
tail.resize(tail.size() - halfsize - 1); |
| 175 | 175 |
} |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
/// \brief The last arc of the path |
| 179 | 179 |
const Arc& back() const {
|
| 180 | 180 |
return tail.empty() ? head.front() : tail.back(); |
| 181 | 181 |
} |
| 182 | 182 |
|
| 183 | 183 |
/// \brief Add a new arc behind the current path |
| 184 | 184 |
void addBack(const Arc& arc) {
|
| 185 | 185 |
tail.push_back(arc); |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
/// \brief Erase the last arc of the path |
| 189 | 189 |
void eraseBack() {
|
| 190 | 190 |
if (!tail.empty()) {
|
| 191 | 191 |
tail.pop_back(); |
| 192 | 192 |
} else {
|
| 193 | 193 |
int halfsize = head.size() / 2; |
| 194 | 194 |
tail.resize(halfsize); |
| 195 | 195 |
std::copy(head.begin() + 1, head.begin() + halfsize + 1, |
| 196 | 196 |
tail.rbegin()); |
| 197 | 197 |
std::copy(head.begin() + halfsize + 1, head.end(), head.begin()); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_PREFLOW_H |
| 20 | 20 |
#define LEMON_PREFLOW_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/tolerance.h> |
| 23 | 23 |
#include <lemon/elevator.h> |
| 24 | 24 |
|
| 25 | 25 |
/// \file |
| 26 | 26 |
/// \ingroup max_flow |
| 27 | 27 |
/// \brief Implementation of the preflow algorithm. |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
/// \brief Default traits class of Preflow class. |
| 32 | 32 |
/// |
| 33 | 33 |
/// Default traits class of Preflow class. |
| 34 | 34 |
/// \tparam _Digraph Digraph type. |
| 35 | 35 |
/// \tparam _CapacityMap Capacity map type. |
| 36 | 36 |
template <typename _Digraph, typename _CapacityMap> |
| 37 | 37 |
struct PreflowDefaultTraits {
|
| 38 | 38 |
|
| 39 | 39 |
/// \brief The type of the digraph the algorithm runs on. |
| 40 | 40 |
typedef _Digraph Digraph; |
| 41 | 41 |
|
| 42 | 42 |
/// \brief The type of the map that stores the arc capacities. |
| 43 | 43 |
/// |
| 44 | 44 |
/// The type of the map that stores the arc capacities. |
| 45 | 45 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 46 | 46 |
typedef _CapacityMap CapacityMap; |
| 47 | 47 |
|
| 48 | 48 |
/// \brief The type of the flow values. |
| 49 | 49 |
typedef typename CapacityMap::Value Value; |
| 50 | 50 |
|
| 51 | 51 |
/// \brief The type of the map that stores the flow values. |
| 52 | 52 |
/// |
| 53 | 53 |
/// The type of the map that stores the flow values. |
| 54 | 54 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 55 | 55 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief Instantiates a FlowMap. |
| 58 | 58 |
/// |
| 59 | 59 |
/// This function instantiates a \ref FlowMap. |
| 60 | 60 |
/// \param digraph The digraph, to which we would like to define |
| 61 | 61 |
/// the flow map. |
| 62 | 62 |
static FlowMap* createFlowMap(const Digraph& digraph) {
|
| 63 | 63 |
return new FlowMap(digraph); |
| 64 | 64 |
} |
| 65 | 65 |
|
| 66 | 66 |
/// \brief The elevator type used by Preflow algorithm. |
| 67 | 67 |
/// |
| 68 | 68 |
/// The elevator type used by Preflow algorithm. |
| 69 | 69 |
/// |
| 70 | 70 |
/// \sa Elevator |
| 71 | 71 |
/// \sa LinkedElevator |
| 72 | 72 |
typedef LinkedElevator<Digraph, typename Digraph::Node> Elevator; |
| 73 | 73 |
|
| 74 | 74 |
/// \brief Instantiates an Elevator. |
| 75 | 75 |
/// |
| 76 | 76 |
/// This function instantiates an \ref Elevator. |
| 77 | 77 |
/// \param digraph The digraph, to which we would like to define |
| 78 | 78 |
/// the elevator. |
| 79 | 79 |
/// \param max_level The maximum level of the elevator. |
| 80 | 80 |
static Elevator* createElevator(const Digraph& digraph, int max_level) {
|
| 81 | 81 |
return new Elevator(digraph, max_level); |
| 82 | 82 |
} |
| 83 | 83 |
|
| 84 | 84 |
/// \brief The tolerance used by the algorithm |
| 85 | 85 |
/// |
| 86 | 86 |
/// The tolerance used by the algorithm to handle inexact computation. |
| 87 | 87 |
typedef lemon::Tolerance<Value> Tolerance; |
| 88 | 88 |
|
| 89 | 89 |
}; |
| 90 | 90 |
|
| 91 | 91 |
|
| 92 | 92 |
/// \ingroup max_flow |
| 93 | 93 |
/// |
| 94 | 94 |
/// \brief %Preflow algorithm class. |
| 95 | 95 |
/// |
| 96 | 96 |
/// This class provides an implementation of Goldberg-Tarjan's \e preflow |
| 97 | 97 |
/// \e push-relabel algorithm producing a flow of maximum value in a |
| 98 | 98 |
/// digraph. The preflow algorithms are the fastest known maximum |
| 99 | 99 |
/// flow algorithms. The current implementation use a mixture of the |
| 100 | 100 |
/// \e "highest label" and the \e "bound decrease" heuristics. |
| 101 | 101 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
|
| 102 | 102 |
/// |
| 103 | 103 |
/// The algorithm consists of two phases. After the first phase |
| 104 | 104 |
/// the maximum flow value and the minimum cut is obtained. The |
| 105 | 105 |
/// second phase constructs a feasible maximum flow on each arc. |
| 106 | 106 |
/// |
| 107 | 107 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 108 | 108 |
/// \tparam _CapacityMap The type of the capacity map. The default map |
| 109 | 109 |
/// type is \ref concepts::Digraph::ArcMap "_Digraph::ArcMap<int>". |
| 110 | 110 |
#ifdef DOXYGEN |
| 111 | 111 |
template <typename _Digraph, typename _CapacityMap, typename _Traits> |
| 112 | 112 |
#else |
| 113 | 113 |
template <typename _Digraph, |
| 114 | 114 |
typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
| 115 | 115 |
typename _Traits = PreflowDefaultTraits<_Digraph, _CapacityMap> > |
| 116 | 116 |
#endif |
| 117 | 117 |
class Preflow {
|
| 118 | 118 |
public: |
| 119 | 119 |
|
| 120 | 120 |
///The \ref PreflowDefaultTraits "traits class" of the algorithm. |
| 121 | 121 |
typedef _Traits Traits; |
| 122 | 122 |
///The type of the digraph the algorithm runs on. |
| 123 | 123 |
typedef typename Traits::Digraph Digraph; |
| 124 | 124 |
///The type of the capacity map. |
| 125 | 125 |
typedef typename Traits::CapacityMap CapacityMap; |
| 126 | 126 |
///The type of the flow values. |
| 127 | 127 |
typedef typename Traits::Value Value; |
| 128 | 128 |
|
| 129 | 129 |
///The type of the flow map. |
| 130 | 130 |
typedef typename Traits::FlowMap FlowMap; |
| 131 | 131 |
///The type of the elevator. |
| 132 | 132 |
typedef typename Traits::Elevator Elevator; |
| 133 | 133 |
///The type of the tolerance. |
| 134 | 134 |
typedef typename Traits::Tolerance Tolerance; |
| 135 | 135 |
|
| 136 | 136 |
private: |
| 137 | 137 |
|
| 138 | 138 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 139 | 139 |
|
| 140 | 140 |
const Digraph& _graph; |
| 141 | 141 |
const CapacityMap* _capacity; |
| 142 | 142 |
|
| 143 | 143 |
int _node_num; |
| 144 | 144 |
|
| 145 | 145 |
Node _source, _target; |
| 146 | 146 |
|
| 147 | 147 |
FlowMap* _flow; |
| 148 | 148 |
bool _local_flow; |
| 149 | 149 |
|
| 150 | 150 |
Elevator* _level; |
| 151 | 151 |
bool _local_level; |
| 152 | 152 |
|
| 153 | 153 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
| 154 | 154 |
ExcessMap* _excess; |
| 155 | 155 |
|
| 156 | 156 |
Tolerance _tolerance; |
| 157 | 157 |
|
| 158 | 158 |
bool _phase; |
| 159 | 159 |
|
| 160 | 160 |
|
| 161 | 161 |
void createStructures() {
|
| 162 | 162 |
_node_num = countNodes(_graph); |
| 163 | 163 |
|
| 164 | 164 |
if (!_flow) {
|
| 165 | 165 |
_flow = Traits::createFlowMap(_graph); |
| 166 | 166 |
_local_flow = true; |
| 167 | 167 |
} |
| 168 | 168 |
if (!_level) {
|
| 169 | 169 |
_level = Traits::createElevator(_graph, _node_num); |
| 170 | 170 |
_local_level = true; |
| 171 | 171 |
} |
| 172 | 172 |
if (!_excess) {
|
| 173 | 173 |
_excess = new ExcessMap(_graph); |
| 174 | 174 |
} |
| 175 | 175 |
} |
| 176 | 176 |
|
| 177 | 177 |
void destroyStructures() {
|
| 178 | 178 |
if (_local_flow) {
|
| 179 | 179 |
delete _flow; |
| 180 | 180 |
} |
| 181 | 181 |
if (_local_level) {
|
| 182 | 182 |
delete _level; |
| 183 | 183 |
} |
| 184 | 184 |
if (_excess) {
|
| 185 | 185 |
delete _excess; |
| 186 | 186 |
} |
| 187 | 187 |
} |
| 188 | 188 |
|
| 189 | 189 |
public: |
| 190 | 190 |
|
| 191 | 191 |
typedef Preflow Create; |
| 192 | 192 |
|
| 193 | 193 |
///\name Named Template Parameters |
| 194 | 194 |
|
| 195 | 195 |
///@{
|
| 196 | 196 |
|
| 197 | 197 |
template <typename _FlowMap> |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\file |
| 20 | 20 |
///\brief Instantiation of the Random class. |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/random.h> |
| 23 | 23 |
|
| 24 | 24 |
namespace lemon {
|
| 25 | 25 |
/// \brief Global random number generator instance |
| 26 | 26 |
/// |
| 27 | 27 |
/// A global Mersenne Twister random number generator instance. |
| 28 | 28 |
Random rnd; |
| 29 | 29 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/* |
| 20 | 20 |
* This file contains the reimplemented version of the Mersenne Twister |
| 21 | 21 |
* Generator of Matsumoto and Nishimura. |
| 22 | 22 |
* |
| 23 | 23 |
* See the appropriate copyright notice below. |
| 24 | 24 |
* |
| 25 | 25 |
* Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, |
| 26 | 26 |
* All rights reserved. |
| 27 | 27 |
* |
| 28 | 28 |
* Redistribution and use in source and binary forms, with or without |
| 29 | 29 |
* modification, are permitted provided that the following conditions |
| 30 | 30 |
* are met: |
| 31 | 31 |
* |
| 32 | 32 |
* 1. Redistributions of source code must retain the above copyright |
| 33 | 33 |
* notice, this list of conditions and the following disclaimer. |
| 34 | 34 |
* |
| 35 | 35 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 36 | 36 |
* notice, this list of conditions and the following disclaimer in the |
| 37 | 37 |
* documentation and/or other materials provided with the distribution. |
| 38 | 38 |
* |
| 39 | 39 |
* 3. The names of its contributors may not be used to endorse or promote |
| 40 | 40 |
* products derived from this software without specific prior written |
| 41 | 41 |
* permission. |
| 42 | 42 |
* |
| 43 | 43 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 44 | 44 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 45 | 45 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 46 | 46 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 47 | 47 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 48 | 48 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 49 | 49 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 50 | 50 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 51 | 51 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 52 | 52 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 53 | 53 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 54 | 54 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 55 | 55 |
* |
| 56 | 56 |
* |
| 57 | 57 |
* Any feedback is very welcome. |
| 58 | 58 |
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html |
| 59 | 59 |
* email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) |
| 60 | 60 |
*/ |
| 61 | 61 |
|
| 62 | 62 |
#ifndef LEMON_RANDOM_H |
| 63 | 63 |
#define LEMON_RANDOM_H |
| 64 | 64 |
|
| 65 | 65 |
#include <algorithm> |
| 66 | 66 |
#include <iterator> |
| 67 | 67 |
#include <vector> |
| 68 | 68 |
#include <limits> |
| 69 | 69 |
#include <fstream> |
| 70 | 70 |
|
| 71 | 71 |
#include <lemon/math.h> |
| 72 | 72 |
#include <lemon/dim2.h> |
| 73 | 73 |
|
| 74 | 74 |
#ifndef WIN32 |
| 75 | 75 |
#include <sys/time.h> |
| 76 | 76 |
#include <ctime> |
| 77 | 77 |
#include <sys/types.h> |
| 78 | 78 |
#include <unistd.h> |
| 79 | 79 |
#else |
| 80 | 80 |
#include <windows.h> |
| 81 | 81 |
#endif |
| 82 | 82 |
|
| 83 | 83 |
///\ingroup misc |
| 84 | 84 |
///\file |
| 85 | 85 |
///\brief Mersenne Twister random number generator |
| 86 | 86 |
|
| 87 | 87 |
namespace lemon {
|
| 88 | 88 |
|
| 89 | 89 |
namespace _random_bits {
|
| 90 | 90 |
|
| 91 | 91 |
template <typename _Word, int _bits = std::numeric_limits<_Word>::digits> |
| 92 | 92 |
struct RandomTraits {};
|
| 93 | 93 |
|
| 94 | 94 |
template <typename _Word> |
| 95 | 95 |
struct RandomTraits<_Word, 32> {
|
| 96 | 96 |
|
| 97 | 97 |
typedef _Word Word; |
| 98 | 98 |
static const int bits = 32; |
| 99 | 99 |
|
| 100 | 100 |
static const int length = 624; |
| 101 | 101 |
static const int shift = 397; |
| 102 | 102 |
|
| 103 | 103 |
static const Word mul = 0x6c078965u; |
| 104 | 104 |
static const Word arrayInit = 0x012BD6AAu; |
| 105 | 105 |
static const Word arrayMul1 = 0x0019660Du; |
| 106 | 106 |
static const Word arrayMul2 = 0x5D588B65u; |
| 107 | 107 |
|
| 108 | 108 |
static const Word mask = 0x9908B0DFu; |
| 109 | 109 |
static const Word loMask = (1u << 31) - 1; |
| 110 | 110 |
static const Word hiMask = ~loMask; |
| 111 | 111 |
|
| 112 | 112 |
|
| 113 | 113 |
static Word tempering(Word rnd) {
|
| 114 | 114 |
rnd ^= (rnd >> 11); |
| 115 | 115 |
rnd ^= (rnd << 7) & 0x9D2C5680u; |
| 116 | 116 |
rnd ^= (rnd << 15) & 0xEFC60000u; |
| 117 | 117 |
rnd ^= (rnd >> 18); |
| 118 | 118 |
return rnd; |
| 119 | 119 |
} |
| 120 | 120 |
|
| 121 | 121 |
}; |
| 122 | 122 |
|
| 123 | 123 |
template <typename _Word> |
| 124 | 124 |
struct RandomTraits<_Word, 64> {
|
| 125 | 125 |
|
| 126 | 126 |
typedef _Word Word; |
| 127 | 127 |
static const int bits = 64; |
| 128 | 128 |
|
| 129 | 129 |
static const int length = 312; |
| 130 | 130 |
static const int shift = 156; |
| 131 | 131 |
|
| 132 | 132 |
static const Word mul = Word(0x5851F42Du) << 32 | Word(0x4C957F2Du); |
| 133 | 133 |
static const Word arrayInit = Word(0x00000000u) << 32 |Word(0x012BD6AAu); |
| 134 | 134 |
static const Word arrayMul1 = Word(0x369DEA0Fu) << 32 |Word(0x31A53F85u); |
| 135 | 135 |
static const Word arrayMul2 = Word(0x27BB2EE6u) << 32 |Word(0x87B0B0FDu); |
| 136 | 136 |
|
| 137 | 137 |
static const Word mask = Word(0xB5026F5Au) << 32 | Word(0xA96619E9u); |
| 138 | 138 |
static const Word loMask = (Word(1u) << 31) - 1; |
| 139 | 139 |
static const Word hiMask = ~loMask; |
| 140 | 140 |
|
| 141 | 141 |
static Word tempering(Word rnd) {
|
| 142 | 142 |
rnd ^= (rnd >> 29) & (Word(0x55555555u) << 32 | Word(0x55555555u)); |
| 143 | 143 |
rnd ^= (rnd << 17) & (Word(0x71D67FFFu) << 32 | Word(0xEDA60000u)); |
| 144 | 144 |
rnd ^= (rnd << 37) & (Word(0xFFF7EEE0u) << 32 | Word(0x00000000u)); |
| 145 | 145 |
rnd ^= (rnd >> 43); |
| 146 | 146 |
return rnd; |
| 147 | 147 |
} |
| 148 | 148 |
|
| 149 | 149 |
}; |
| 150 | 150 |
|
| 151 | 151 |
template <typename _Word> |
| 152 | 152 |
class RandomCore {
|
| 153 | 153 |
public: |
| 154 | 154 |
|
| 155 | 155 |
typedef _Word Word; |
| 156 | 156 |
|
| 157 | 157 |
private: |
| 158 | 158 |
|
| 159 | 159 |
static const int bits = RandomTraits<Word>::bits; |
| 160 | 160 |
|
| 161 | 161 |
static const int length = RandomTraits<Word>::length; |
| 162 | 162 |
static const int shift = RandomTraits<Word>::shift; |
| 163 | 163 |
|
| 164 | 164 |
public: |
| 165 | 165 |
|
| 166 | 166 |
void initState() {
|
| 167 | 167 |
static const Word seedArray[4] = {
|
| 168 | 168 |
0x12345u, 0x23456u, 0x34567u, 0x45678u |
| 169 | 169 |
}; |
| 170 | 170 |
|
| 171 | 171 |
initState(seedArray, seedArray + 4); |
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
void initState(Word seed) {
|
| 175 | 175 |
|
| 176 | 176 |
static const Word mul = RandomTraits<Word>::mul; |
| 177 | 177 |
|
| 178 | 178 |
current = state; |
| 179 | 179 |
|
| 180 | 180 |
Word *curr = state + length - 1; |
| 181 | 181 |
curr[0] = seed; --curr; |
| 182 | 182 |
for (int i = 1; i < length; ++i) {
|
| 183 | 183 |
curr[0] = (mul * ( curr[1] ^ (curr[1] >> (bits - 2)) ) + i); |
| 184 | 184 |
--curr; |
| 185 | 185 |
} |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
template <typename Iterator> |
| 189 | 189 |
void initState(Iterator begin, Iterator end) {
|
| 190 | 190 |
|
| 191 | 191 |
static const Word init = RandomTraits<Word>::arrayInit; |
| 192 | 192 |
static const Word mul1 = RandomTraits<Word>::arrayMul1; |
| 193 | 193 |
static const Word mul2 = RandomTraits<Word>::arrayMul2; |
| 194 | 194 |
|
| 195 | 195 |
|
| 196 | 196 |
Word *curr = state + length - 1; --curr; |
| 197 | 197 |
Iterator it = begin; int cnt = 0; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_SMART_GRAPH_H |
| 20 | 20 |
#define LEMON_SMART_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graphs |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief SmartDigraph and SmartGraph classes. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/bits/graph_extender.h> |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
class SmartDigraph; |
| 35 | 35 |
///Base of SmartDigraph |
| 36 | 36 |
|
| 37 | 37 |
///Base of SmartDigraph |
| 38 | 38 |
/// |
| 39 | 39 |
class SmartDigraphBase {
|
| 40 | 40 |
protected: |
| 41 | 41 |
|
| 42 | 42 |
struct NodeT |
| 43 | 43 |
{
|
| 44 | 44 |
int first_in, first_out; |
| 45 | 45 |
NodeT() {}
|
| 46 | 46 |
}; |
| 47 | 47 |
struct ArcT |
| 48 | 48 |
{
|
| 49 | 49 |
int target, source, next_in, next_out; |
| 50 | 50 |
ArcT() {}
|
| 51 | 51 |
}; |
| 52 | 52 |
|
| 53 | 53 |
std::vector<NodeT> nodes; |
| 54 | 54 |
std::vector<ArcT> arcs; |
| 55 | 55 |
|
| 56 | 56 |
public: |
| 57 | 57 |
|
| 58 | 58 |
typedef SmartDigraphBase Graph; |
| 59 | 59 |
|
| 60 | 60 |
class Node; |
| 61 | 61 |
class Arc; |
| 62 | 62 |
|
| 63 | 63 |
public: |
| 64 | 64 |
|
| 65 | 65 |
SmartDigraphBase() : nodes(), arcs() { }
|
| 66 | 66 |
SmartDigraphBase(const SmartDigraphBase &_g) |
| 67 | 67 |
: nodes(_g.nodes), arcs(_g.arcs) { }
|
| 68 | 68 |
|
| 69 | 69 |
typedef True NodeNumTag; |
| 70 | 70 |
typedef True ArcNumTag; |
| 71 | 71 |
|
| 72 | 72 |
int nodeNum() const { return nodes.size(); }
|
| 73 | 73 |
int arcNum() const { return arcs.size(); }
|
| 74 | 74 |
|
| 75 | 75 |
int maxNodeId() const { return nodes.size()-1; }
|
| 76 | 76 |
int maxArcId() const { return arcs.size()-1; }
|
| 77 | 77 |
|
| 78 | 78 |
Node addNode() {
|
| 79 | 79 |
int n = nodes.size(); |
| 80 | 80 |
nodes.push_back(NodeT()); |
| 81 | 81 |
nodes[n].first_in = -1; |
| 82 | 82 |
nodes[n].first_out = -1; |
| 83 | 83 |
return Node(n); |
| 84 | 84 |
} |
| 85 | 85 |
|
| 86 | 86 |
Arc addArc(Node u, Node v) {
|
| 87 | 87 |
int n = arcs.size(); |
| 88 | 88 |
arcs.push_back(ArcT()); |
| 89 | 89 |
arcs[n].source = u._id; |
| 90 | 90 |
arcs[n].target = v._id; |
| 91 | 91 |
arcs[n].next_out = nodes[u._id].first_out; |
| 92 | 92 |
arcs[n].next_in = nodes[v._id].first_in; |
| 93 | 93 |
nodes[u._id].first_out = nodes[v._id].first_in = n; |
| 94 | 94 |
|
| 95 | 95 |
return Arc(n); |
| 96 | 96 |
} |
| 97 | 97 |
|
| 98 | 98 |
void clear() {
|
| 99 | 99 |
arcs.clear(); |
| 100 | 100 |
nodes.clear(); |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 | 103 |
Node source(Arc a) const { return Node(arcs[a._id].source); }
|
| 104 | 104 |
Node target(Arc a) const { return Node(arcs[a._id].target); }
|
| 105 | 105 |
|
| 106 | 106 |
static int id(Node v) { return v._id; }
|
| 107 | 107 |
static int id(Arc a) { return a._id; }
|
| 108 | 108 |
|
| 109 | 109 |
static Node nodeFromId(int id) { return Node(id);}
|
| 110 | 110 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 111 | 111 |
|
| 112 | 112 |
bool valid(Node n) const {
|
| 113 | 113 |
return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
| 114 | 114 |
} |
| 115 | 115 |
bool valid(Arc a) const {
|
| 116 | 116 |
return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
class Node {
|
| 120 | 120 |
friend class SmartDigraphBase; |
| 121 | 121 |
friend class SmartDigraph; |
| 122 | 122 |
|
| 123 | 123 |
protected: |
| 124 | 124 |
int _id; |
| 125 | 125 |
explicit Node(int id) : _id(id) {}
|
| 126 | 126 |
public: |
| 127 | 127 |
Node() {}
|
| 128 | 128 |
Node (Invalid) : _id(-1) {}
|
| 129 | 129 |
bool operator==(const Node i) const {return _id == i._id;}
|
| 130 | 130 |
bool operator!=(const Node i) const {return _id != i._id;}
|
| 131 | 131 |
bool operator<(const Node i) const {return _id < i._id;}
|
| 132 | 132 |
}; |
| 133 | 133 |
|
| 134 | 134 |
|
| 135 | 135 |
class Arc {
|
| 136 | 136 |
friend class SmartDigraphBase; |
| 137 | 137 |
friend class SmartDigraph; |
| 138 | 138 |
|
| 139 | 139 |
protected: |
| 140 | 140 |
int _id; |
| 141 | 141 |
explicit Arc(int id) : _id(id) {}
|
| 142 | 142 |
public: |
| 143 | 143 |
Arc() { }
|
| 144 | 144 |
Arc (Invalid) : _id(-1) {}
|
| 145 | 145 |
bool operator==(const Arc i) const {return _id == i._id;}
|
| 146 | 146 |
bool operator!=(const Arc i) const {return _id != i._id;}
|
| 147 | 147 |
bool operator<(const Arc i) const {return _id < i._id;}
|
| 148 | 148 |
}; |
| 149 | 149 |
|
| 150 | 150 |
void first(Node& node) const {
|
| 151 | 151 |
node._id = nodes.size() - 1; |
| 152 | 152 |
} |
| 153 | 153 |
|
| 154 | 154 |
static void next(Node& node) {
|
| 155 | 155 |
--node._id; |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
void first(Arc& arc) const {
|
| 159 | 159 |
arc._id = arcs.size() - 1; |
| 160 | 160 |
} |
| 161 | 161 |
|
| 162 | 162 |
static void next(Arc& arc) {
|
| 163 | 163 |
--arc._id; |
| 164 | 164 |
} |
| 165 | 165 |
|
| 166 | 166 |
void firstOut(Arc& arc, const Node& node) const {
|
| 167 | 167 |
arc._id = nodes[node._id].first_out; |
| 168 | 168 |
} |
| 169 | 169 |
|
| 170 | 170 |
void nextOut(Arc& arc) const {
|
| 171 | 171 |
arc._id = arcs[arc._id].next_out; |
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
void firstIn(Arc& arc, const Node& node) const {
|
| 175 | 175 |
arc._id = nodes[node._id].first_in; |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
void nextIn(Arc& arc) const {
|
| 179 | 179 |
arc._id = arcs[arc._id].next_in; |
| 180 | 180 |
} |
| 181 | 181 |
|
| 182 | 182 |
}; |
| 183 | 183 |
|
| 184 | 184 |
typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase; |
| 185 | 185 |
|
| 186 | 186 |
///\ingroup graphs |
| 187 | 187 |
/// |
| 188 | 188 |
///\brief A smart directed graph class. |
| 189 | 189 |
/// |
| 190 | 190 |
///This is a simple and fast digraph implementation. |
| 191 | 191 |
///It is also quite memory efficient, but at the price |
| 192 | 192 |
///that <b> it does support only limited (only stack-like) |
| 193 | 193 |
///node and arc deletions</b>. |
| 194 | 194 |
///It conforms to the \ref concepts::Digraph "Digraph concept" with |
| 195 | 195 |
///an important extra feature that its maps are real \ref |
| 196 | 196 |
///concepts::ReferenceMap "reference map"s. |
| 197 | 197 |
/// |
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_SUURBALLE_H |
| 20 | 20 |
#define LEMON_SUURBALLE_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup shortest_path |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief An algorithm for finding arc-disjoint paths between two |
| 25 | 25 |
/// nodes having minimum total length. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <lemon/bin_heap.h> |
| 29 | 29 |
#include <lemon/path.h> |
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
|
| 33 | 33 |
/// \addtogroup shortest_path |
| 34 | 34 |
/// @{
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Algorithm for finding arc-disjoint paths between two nodes |
| 37 | 37 |
/// having minimum total length. |
| 38 | 38 |
/// |
| 39 | 39 |
/// \ref lemon::Suurballe "Suurballe" implements an algorithm for |
| 40 | 40 |
/// finding arc-disjoint paths having minimum total length (cost) |
| 41 | 41 |
/// from a given source node to a given target node in a digraph. |
| 42 | 42 |
/// |
| 43 | 43 |
/// In fact, this implementation is the specialization of the |
| 44 | 44 |
/// \ref CapacityScaling "successive shortest path" algorithm. |
| 45 | 45 |
/// |
| 46 | 46 |
/// \tparam Digraph The digraph type the algorithm runs on. |
| 47 | 47 |
/// The default value is \c ListDigraph. |
| 48 | 48 |
/// \tparam LengthMap The type of the length (cost) map. |
| 49 | 49 |
/// The default value is <tt>Digraph::ArcMap<int></tt>. |
| 50 | 50 |
/// |
| 51 | 51 |
/// \warning Length values should be \e non-negative \e integers. |
| 52 | 52 |
/// |
| 53 | 53 |
/// \note For finding node-disjoint paths this algorithm can be used |
| 54 | 54 |
/// with \ref SplitNodes. |
| 55 | 55 |
#ifdef DOXYGEN |
| 56 | 56 |
template <typename Digraph, typename LengthMap> |
| 57 | 57 |
#else |
| 58 | 58 |
template < typename Digraph = ListDigraph, |
| 59 | 59 |
typename LengthMap = typename Digraph::template ArcMap<int> > |
| 60 | 60 |
#endif |
| 61 | 61 |
class Suurballe |
| 62 | 62 |
{
|
| 63 | 63 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 64 | 64 |
|
| 65 | 65 |
typedef typename LengthMap::Value Length; |
| 66 | 66 |
typedef ConstMap<Arc, int> ConstArcMap; |
| 67 | 67 |
typedef typename Digraph::template NodeMap<Arc> PredMap; |
| 68 | 68 |
|
| 69 | 69 |
public: |
| 70 | 70 |
|
| 71 | 71 |
/// The type of the flow map. |
| 72 | 72 |
typedef typename Digraph::template ArcMap<int> FlowMap; |
| 73 | 73 |
/// The type of the potential map. |
| 74 | 74 |
typedef typename Digraph::template NodeMap<Length> PotentialMap; |
| 75 | 75 |
/// The type of the path structures. |
| 76 | 76 |
typedef SimplePath<Digraph> Path; |
| 77 | 77 |
|
| 78 | 78 |
private: |
| 79 |
|
|
| 79 |
|
|
| 80 | 80 |
/// \brief Special implementation of the Dijkstra algorithm |
| 81 | 81 |
/// for finding shortest paths in the residual network. |
| 82 | 82 |
/// |
| 83 | 83 |
/// \ref ResidualDijkstra is a special implementation of the |
| 84 | 84 |
/// \ref Dijkstra algorithm for finding shortest paths in the |
| 85 | 85 |
/// residual network of the digraph with respect to the reduced arc |
| 86 | 86 |
/// lengths and modifying the node potentials according to the |
| 87 | 87 |
/// distance of the nodes. |
| 88 | 88 |
class ResidualDijkstra |
| 89 | 89 |
{
|
| 90 | 90 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
| 91 | 91 |
typedef BinHeap<Length, HeapCrossRef> Heap; |
| 92 | 92 |
|
| 93 | 93 |
private: |
| 94 | 94 |
|
| 95 | 95 |
// The digraph the algorithm runs on |
| 96 | 96 |
const Digraph &_graph; |
| 97 | 97 |
|
| 98 | 98 |
// The main maps |
| 99 | 99 |
const FlowMap &_flow; |
| 100 | 100 |
const LengthMap &_length; |
| 101 | 101 |
PotentialMap &_potential; |
| 102 | 102 |
|
| 103 | 103 |
// The distance map |
| 104 | 104 |
PotentialMap _dist; |
| 105 | 105 |
// The pred arc map |
| 106 | 106 |
PredMap &_pred; |
| 107 | 107 |
// The processed (i.e. permanently labeled) nodes |
| 108 | 108 |
std::vector<Node> _proc_nodes; |
| 109 |
|
|
| 109 |
|
|
| 110 | 110 |
Node _s; |
| 111 | 111 |
Node _t; |
| 112 | 112 |
|
| 113 | 113 |
public: |
| 114 | 114 |
|
| 115 | 115 |
/// Constructor. |
| 116 | 116 |
ResidualDijkstra( const Digraph &digraph, |
| 117 | 117 |
const FlowMap &flow, |
| 118 | 118 |
const LengthMap &length, |
| 119 | 119 |
PotentialMap &potential, |
| 120 | 120 |
PredMap &pred, |
| 121 | 121 |
Node s, Node t ) : |
| 122 | 122 |
_graph(digraph), _flow(flow), _length(length), _potential(potential), |
| 123 | 123 |
_dist(digraph), _pred(pred), _s(s), _t(t) {}
|
| 124 | 124 |
|
| 125 | 125 |
/// \brief Run the algorithm. It returns \c true if a path is found |
| 126 | 126 |
/// from the source node to the target node. |
| 127 | 127 |
bool run() {
|
| 128 | 128 |
HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP); |
| 129 | 129 |
Heap heap(heap_cross_ref); |
| 130 | 130 |
heap.push(_s, 0); |
| 131 | 131 |
_pred[_s] = INVALID; |
| 132 | 132 |
_proc_nodes.clear(); |
| 133 | 133 |
|
| 134 | 134 |
// Process nodes |
| 135 | 135 |
while (!heap.empty() && heap.top() != _t) {
|
| 136 | 136 |
Node u = heap.top(), v; |
| 137 | 137 |
Length d = heap.prio() + _potential[u], nd; |
| 138 | 138 |
_dist[u] = heap.prio(); |
| 139 | 139 |
heap.pop(); |
| 140 | 140 |
_proc_nodes.push_back(u); |
| 141 | 141 |
|
| 142 | 142 |
// Traverse outgoing arcs |
| 143 | 143 |
for (OutArcIt e(_graph, u); e != INVALID; ++e) {
|
| 144 | 144 |
if (_flow[e] == 0) {
|
| 145 | 145 |
v = _graph.target(e); |
| 146 | 146 |
switch(heap.state(v)) {
|
| 147 | 147 |
case Heap::PRE_HEAP: |
| 148 | 148 |
heap.push(v, d + _length[e] - _potential[v]); |
| 149 | 149 |
_pred[v] = e; |
| 150 | 150 |
break; |
| 151 | 151 |
case Heap::IN_HEAP: |
| 152 | 152 |
nd = d + _length[e] - _potential[v]; |
| 153 | 153 |
if (nd < heap[v]) {
|
| 154 | 154 |
heap.decrease(v, nd); |
| 155 | 155 |
_pred[v] = e; |
| 156 | 156 |
} |
| 157 | 157 |
break; |
| 158 | 158 |
case Heap::POST_HEAP: |
| 159 | 159 |
break; |
| 160 | 160 |
} |
| 161 | 161 |
} |
| 162 | 162 |
} |
| 163 | 163 |
|
| 164 | 164 |
// Traverse incoming arcs |
| 165 | 165 |
for (InArcIt e(_graph, u); e != INVALID; ++e) {
|
| 166 | 166 |
if (_flow[e] == 1) {
|
| 167 | 167 |
v = _graph.source(e); |
| 168 | 168 |
switch(heap.state(v)) {
|
| 169 | 169 |
case Heap::PRE_HEAP: |
| 170 | 170 |
heap.push(v, d - _length[e] - _potential[v]); |
| 171 | 171 |
_pred[v] = e; |
| 172 | 172 |
break; |
| 173 | 173 |
case Heap::IN_HEAP: |
| 174 | 174 |
nd = d - _length[e] - _potential[v]; |
| 175 | 175 |
if (nd < heap[v]) {
|
| 176 | 176 |
heap.decrease(v, nd); |
| 177 | 177 |
_pred[v] = e; |
| 178 | 178 |
} |
| 179 | 179 |
break; |
| 180 | 180 |
case Heap::POST_HEAP: |
| 181 | 181 |
break; |
| 182 | 182 |
} |
| 183 | 183 |
} |
| 184 | 184 |
} |
| 185 | 185 |
} |
| 186 | 186 |
if (heap.empty()) return false; |
| 187 | 187 |
|
| 188 | 188 |
// Update potentials of processed nodes |
| 189 | 189 |
Length t_dist = heap.prio(); |
| 190 | 190 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) |
| 191 | 191 |
_potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist; |
| 192 | 192 |
return true; |
| 193 | 193 |
} |
| 194 | 194 |
|
| 195 | 195 |
}; //class ResidualDijkstra |
| 196 | 196 |
|
| 197 | 197 |
private: |
| 198 | 198 |
|
| 199 | 199 |
// The digraph the algorithm runs on |
| 200 | 200 |
const Digraph &_graph; |
| 201 | 201 |
// The length map |
| 202 | 202 |
const LengthMap &_length; |
| 203 |
|
|
| 203 |
|
|
| 204 | 204 |
// Arc map of the current flow |
| 205 | 205 |
FlowMap *_flow; |
| 206 | 206 |
bool _local_flow; |
| 207 | 207 |
// Node map of the current potentials |
| 208 | 208 |
PotentialMap *_potential; |
| 209 | 209 |
bool _local_potential; |
| 210 | 210 |
|
| 211 | 211 |
// The source node |
| 212 | 212 |
Node _source; |
| 213 | 213 |
// The target node |
| 214 | 214 |
Node _target; |
| 215 | 215 |
|
| 216 | 216 |
// Container to store the found paths |
| 217 | 217 |
std::vector< SimplePath<Digraph> > paths; |
| 218 | 218 |
int _path_num; |
| 219 | 219 |
|
| 220 | 220 |
// The pred arc map |
| 221 | 221 |
PredMap _pred; |
| 222 | 222 |
// Implementation of the Dijkstra algorithm for finding augmenting |
| 223 | 223 |
// shortest paths in the residual network |
| 224 | 224 |
ResidualDijkstra *_dijkstra; |
| 225 | 225 |
|
| 226 | 226 |
public: |
| 227 | 227 |
|
| 228 | 228 |
/// \brief Constructor. |
| 229 | 229 |
/// |
| 230 | 230 |
/// Constructor. |
| 231 | 231 |
/// |
| 232 | 232 |
/// \param digraph The digraph the algorithm runs on. |
| 233 | 233 |
/// \param length The length (cost) values of the arcs. |
| 234 | 234 |
/// \param s The source node. |
| 235 | 235 |
/// \param t The target node. |
| 236 | 236 |
Suurballe( const Digraph &digraph, |
| 237 | 237 |
const LengthMap &length, |
| 238 | 238 |
Node s, Node t ) : |
| 239 | 239 |
_graph(digraph), _length(length), _flow(0), _local_flow(false), |
| 240 | 240 |
_potential(0), _local_potential(false), _source(s), _target(t), |
| 241 | 241 |
_pred(digraph) {}
|
| 242 | 242 |
|
| 243 | 243 |
/// Destructor. |
| 244 | 244 |
~Suurballe() {
|
| 245 | 245 |
if (_local_flow) delete _flow; |
| 246 | 246 |
if (_local_potential) delete _potential; |
| 247 | 247 |
delete _dijkstra; |
| 248 | 248 |
} |
| 249 | 249 |
|
| 250 | 250 |
/// \brief Set the flow map. |
| 251 | 251 |
/// |
| 252 | 252 |
/// This function sets the flow map. |
| 253 | 253 |
/// |
| 254 | 254 |
/// The found flow contains only 0 and 1 values. It is the union of |
| 255 | 255 |
/// the found arc-disjoint paths. |
| 256 | 256 |
/// |
| 257 | 257 |
/// \return \c (*this) |
| 258 | 258 |
Suurballe& flowMap(FlowMap &map) {
|
| 259 | 259 |
if (_local_flow) {
|
| 260 | 260 |
delete _flow; |
| 261 | 261 |
_local_flow = false; |
| 262 | 262 |
} |
| 263 | 263 |
_flow = ↦ |
| 264 | 264 |
return *this; |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 | 267 |
/// \brief Set the potential map. |
| 268 | 268 |
/// |
| 269 | 269 |
/// This function sets the potential map. |
| 270 | 270 |
/// |
| 271 |
/// The potentials provide the dual solution of the underlying |
|
| 271 |
/// The potentials provide the dual solution of the underlying |
|
| 272 | 272 |
/// minimum cost flow problem. |
| 273 | 273 |
/// |
| 274 | 274 |
/// \return \c (*this) |
| 275 | 275 |
Suurballe& potentialMap(PotentialMap &map) {
|
| 276 | 276 |
if (_local_potential) {
|
| 277 | 277 |
delete _potential; |
| 278 | 278 |
_local_potential = false; |
| 279 | 279 |
} |
| 280 | 280 |
_potential = ↦ |
| 281 | 281 |
return *this; |
| 282 | 282 |
} |
| 283 | 283 |
|
| 284 | 284 |
/// \name Execution control |
| 285 | 285 |
/// The simplest way to execute the algorithm is to call the run() |
| 286 | 286 |
/// function. |
| 287 | 287 |
/// \n |
| 288 | 288 |
/// If you only need the flow that is the union of the found |
| 289 | 289 |
/// arc-disjoint paths, you may call init() and findFlow(). |
| 290 | 290 |
|
| 291 | 291 |
/// @{
|
| 292 | 292 |
|
| 293 | 293 |
/// \brief Run the algorithm. |
| 294 | 294 |
/// |
| 295 | 295 |
/// This function runs the algorithm. |
| 296 | 296 |
/// |
| 297 | 297 |
/// \param k The number of paths to be found. |
| 298 | 298 |
/// |
| 299 | 299 |
/// \return \c k if there are at least \c k arc-disjoint paths from |
| 300 | 300 |
/// \c s to \c t in the digraph. Otherwise it returns the number of |
| 301 | 301 |
/// arc-disjoint paths found. |
| 302 | 302 |
/// |
| 303 | 303 |
/// \note Apart from the return value, <tt>s.run(k)</tt> is just a |
| 304 | 304 |
/// shortcut of the following code. |
| 305 | 305 |
/// \code |
| 306 | 306 |
/// s.init(); |
| 307 | 307 |
/// s.findFlow(k); |
| 308 | 308 |
/// s.findPaths(); |
| 309 | 309 |
/// \endcode |
| 310 | 310 |
int run(int k = 2) {
|
| 311 | 311 |
init(); |
| 312 | 312 |
findFlow(k); |
| 313 | 313 |
findPaths(); |
| 314 | 314 |
return _path_num; |
| 315 | 315 |
} |
| 316 | 316 |
|
| 317 | 317 |
/// \brief Initialize the algorithm. |
| 318 | 318 |
/// |
| 319 | 319 |
/// This function initializes the algorithm. |
| 320 | 320 |
void init() {
|
| 321 | 321 |
// Initialize maps |
| 322 | 322 |
if (!_flow) {
|
| 323 | 323 |
_flow = new FlowMap(_graph); |
| 324 | 324 |
_local_flow = true; |
| 325 | 325 |
} |
| 326 | 326 |
if (!_potential) {
|
| 327 | 327 |
_potential = new PotentialMap(_graph); |
| 328 | 328 |
_local_potential = true; |
| 329 | 329 |
} |
| 330 | 330 |
for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0; |
| 331 | 331 |
for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0; |
| 332 | 332 |
|
| 333 |
_dijkstra = new ResidualDijkstra( _graph, *_flow, _length, |
|
| 333 |
_dijkstra = new ResidualDijkstra( _graph, *_flow, _length, |
|
| 334 | 334 |
*_potential, _pred, |
| 335 | 335 |
_source, _target ); |
| 336 | 336 |
} |
| 337 | 337 |
|
| 338 | 338 |
/// \brief Execute the successive shortest path algorithm to find |
| 339 | 339 |
/// an optimal flow. |
| 340 | 340 |
/// |
| 341 | 341 |
/// This function executes the successive shortest path algorithm to |
| 342 | 342 |
/// find a minimum cost flow, which is the union of \c k or less |
| 343 | 343 |
/// arc-disjoint paths. |
| 344 | 344 |
/// |
| 345 | 345 |
/// \return \c k if there are at least \c k arc-disjoint paths from |
| 346 | 346 |
/// \c s to \c t in the digraph. Otherwise it returns the number of |
| 347 | 347 |
/// arc-disjoint paths found. |
| 348 | 348 |
/// |
| 349 | 349 |
/// \pre \ref init() must be called before using this function. |
| 350 | 350 |
int findFlow(int k = 2) {
|
| 351 | 351 |
// Find shortest paths |
| 352 | 352 |
_path_num = 0; |
| 353 | 353 |
while (_path_num < k) {
|
| 354 | 354 |
// Run Dijkstra |
| 355 | 355 |
if (!_dijkstra->run()) break; |
| 356 | 356 |
++_path_num; |
| 357 | 357 |
|
| 358 | 358 |
// Set the flow along the found shortest path |
| 359 | 359 |
Node u = _target; |
| 360 | 360 |
Arc e; |
| 361 | 361 |
while ((e = _pred[u]) != INVALID) {
|
| 362 | 362 |
if (u == _graph.target(e)) {
|
| 363 | 363 |
(*_flow)[e] = 1; |
| 364 | 364 |
u = _graph.source(e); |
| 365 | 365 |
} else {
|
| 366 | 366 |
(*_flow)[e] = 0; |
| 367 | 367 |
u = _graph.target(e); |
| 368 | 368 |
} |
| 369 | 369 |
} |
| 370 | 370 |
} |
| 371 | 371 |
return _path_num; |
| 372 | 372 |
} |
| 373 |
|
|
| 373 |
|
|
| 374 | 374 |
/// \brief Compute the paths from the flow. |
| 375 | 375 |
/// |
| 376 | 376 |
/// This function computes the paths from the flow. |
| 377 | 377 |
/// |
| 378 | 378 |
/// \pre \ref init() and \ref findFlow() must be called before using |
| 379 | 379 |
/// this function. |
| 380 | 380 |
void findPaths() {
|
| 381 | 381 |
// Create the residual flow map (the union of the paths not found |
| 382 | 382 |
// so far) |
| 383 | 383 |
FlowMap res_flow(_graph); |
| 384 | 384 |
for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a]; |
| 385 | 385 |
|
| 386 | 386 |
paths.clear(); |
| 387 | 387 |
paths.resize(_path_num); |
| 388 | 388 |
for (int i = 0; i < _path_num; ++i) {
|
| 389 | 389 |
Node n = _source; |
| 390 | 390 |
while (n != _target) {
|
| 391 | 391 |
OutArcIt e(_graph, n); |
| 392 | 392 |
for ( ; res_flow[e] == 0; ++e) ; |
| 393 | 393 |
n = _graph.target(e); |
| 394 | 394 |
paths[i].addBack(e); |
| 395 | 395 |
res_flow[e] = 0; |
| 396 | 396 |
} |
| 397 | 397 |
} |
| 398 | 398 |
} |
| 399 | 399 |
|
| 400 | 400 |
/// @} |
| 401 | 401 |
|
| 402 | 402 |
/// \name Query Functions |
| 403 | 403 |
/// The results of the algorithm can be obtained using these |
| 404 | 404 |
/// functions. |
| 405 | 405 |
/// \n The algorithm should be executed before using them. |
| 406 | 406 |
|
| 407 | 407 |
/// @{
|
| 408 | 408 |
|
| 409 | 409 |
/// \brief Return a const reference to the arc map storing the |
| 410 | 410 |
/// found flow. |
| 411 | 411 |
/// |
| 412 | 412 |
/// This function returns a const reference to the arc map storing |
| 413 | 413 |
/// the flow that is the union of the found arc-disjoint paths. |
| 414 | 414 |
/// |
| 415 | 415 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 416 | 416 |
/// this function. |
| 417 | 417 |
const FlowMap& flowMap() const {
|
| 418 | 418 |
return *_flow; |
| 419 | 419 |
} |
| 420 | 420 |
|
| 421 | 421 |
/// \brief Return a const reference to the node map storing the |
| 422 | 422 |
/// found potentials (the dual solution). |
| 423 | 423 |
/// |
| 424 | 424 |
/// This function returns a const reference to the node map storing |
| 425 | 425 |
/// the found potentials that provide the dual solution of the |
| 426 | 426 |
/// underlying minimum cost flow problem. |
| 427 | 427 |
/// |
| 428 | 428 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 429 | 429 |
/// this function. |
| 430 | 430 |
const PotentialMap& potentialMap() const {
|
| 431 | 431 |
return *_potential; |
| 432 | 432 |
} |
| 433 | 433 |
|
| 434 | 434 |
/// \brief Return the flow on the given arc. |
| 435 | 435 |
/// |
| 436 | 436 |
/// This function returns the flow on the given arc. |
| 437 | 437 |
/// It is \c 1 if the arc is involved in one of the found paths, |
| 438 | 438 |
/// otherwise it is \c 0. |
| 439 | 439 |
/// |
| 440 | 440 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 441 | 441 |
/// this function. |
| 442 | 442 |
int flow(const Arc& arc) const {
|
| 443 | 443 |
return (*_flow)[arc]; |
| 444 | 444 |
} |
| 445 | 445 |
|
| 446 | 446 |
/// \brief Return the potential of the given node. |
| 447 | 447 |
/// |
| 448 | 448 |
/// This function returns the potential of the given node. |
| 449 | 449 |
/// |
| 450 | 450 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 451 | 451 |
/// this function. |
| 452 | 452 |
Length potential(const Node& node) const {
|
| 453 | 453 |
return (*_potential)[node]; |
| 454 | 454 |
} |
| 455 | 455 |
|
| 456 | 456 |
/// \brief Return the total length (cost) of the found paths (flow). |
| 457 | 457 |
/// |
| 458 | 458 |
/// This function returns the total length (cost) of the found paths |
| 459 | 459 |
/// (flow). The complexity of the function is \f$ O(e) \f$. |
| 460 | 460 |
/// |
| 461 | 461 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 462 | 462 |
/// this function. |
| 463 | 463 |
Length totalLength() const {
|
| 464 | 464 |
Length c = 0; |
| 465 | 465 |
for (ArcIt e(_graph); e != INVALID; ++e) |
| 466 | 466 |
c += (*_flow)[e] * _length[e]; |
| 467 | 467 |
return c; |
| 468 | 468 |
} |
| 469 | 469 |
|
| 470 | 470 |
/// \brief Return the number of the found paths. |
| 471 | 471 |
/// |
| 472 | 472 |
/// This function returns the number of the found paths. |
| 473 | 473 |
/// |
| 474 | 474 |
/// \pre \ref run() or \ref findFlow() must be called before using |
| 475 | 475 |
/// this function. |
| 476 | 476 |
int pathNum() const {
|
| 477 | 477 |
return _path_num; |
| 478 | 478 |
} |
| 479 | 479 |
|
| 480 | 480 |
/// \brief Return a const reference to the specified path. |
| 481 | 481 |
/// |
| 482 | 482 |
/// This function returns a const reference to the specified path. |
| 483 | 483 |
/// |
| 484 | 484 |
/// \param i The function returns the \c i-th path. |
| 485 | 485 |
/// \c i must be between \c 0 and <tt>%pathNum()-1</tt>. |
| 486 | 486 |
/// |
| 487 | 487 |
/// \pre \ref run() or \ref findPaths() must be called before using |
| 488 | 488 |
/// this function. |
| 489 | 489 |
Path path(int i) const {
|
| 490 | 490 |
return paths[i]; |
| 491 | 491 |
} |
| 492 | 492 |
|
| 493 | 493 |
/// @} |
| 494 | 494 |
|
| 495 | 495 |
}; //class Suurballe |
| 496 | 496 |
|
| 497 | 497 |
///@} |
| 498 | 498 |
|
| 499 | 499 |
} //namespace lemon |
| 500 | 500 |
|
| 501 | 501 |
#endif //LEMON_SUURBALLE_H |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_TIME_MEASURE_H |
| 20 | 20 |
#define LEMON_TIME_MEASURE_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup timecount |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Tools for measuring cpu usage |
| 25 | 25 |
|
| 26 | 26 |
#ifdef WIN32 |
| 27 | 27 |
#define WIN32_LEAN_AND_MEAN |
| 28 | 28 |
#define NOMINMAX |
| 29 | 29 |
#include <windows.h> |
| 30 | 30 |
#include <cmath> |
| 31 | 31 |
#else |
| 32 | 32 |
#include <sys/times.h> |
| 33 | 33 |
#include <sys/time.h> |
| 34 | 34 |
#endif |
| 35 | 35 |
|
| 36 | 36 |
#include <string> |
| 37 | 37 |
#include <fstream> |
| 38 | 38 |
#include <iostream> |
| 39 | 39 |
|
| 40 | 40 |
namespace lemon {
|
| 41 | 41 |
|
| 42 | 42 |
/// \addtogroup timecount |
| 43 | 43 |
/// @{
|
| 44 | 44 |
|
| 45 | 45 |
/// A class to store (cpu)time instances. |
| 46 | 46 |
|
| 47 | 47 |
/// This class stores five time values. |
| 48 | 48 |
/// - a real time |
| 49 | 49 |
/// - a user cpu time |
| 50 | 50 |
/// - a system cpu time |
| 51 | 51 |
/// - a user cpu time of children |
| 52 | 52 |
/// - a system cpu time of children |
| 53 | 53 |
/// |
| 54 | 54 |
/// TimeStamp's can be added to or substracted from each other and |
| 55 | 55 |
/// they can be pushed to a stream. |
| 56 | 56 |
/// |
| 57 | 57 |
/// In most cases, perhaps the \ref Timer or the \ref TimeReport |
| 58 | 58 |
/// class is what you want to use instead. |
| 59 | 59 |
|
| 60 | 60 |
class TimeStamp |
| 61 | 61 |
{
|
| 62 | 62 |
double utime; |
| 63 | 63 |
double stime; |
| 64 | 64 |
double cutime; |
| 65 | 65 |
double cstime; |
| 66 | 66 |
double rtime; |
| 67 | 67 |
|
| 68 | 68 |
void _reset() {
|
| 69 | 69 |
utime = stime = cutime = cstime = rtime = 0; |
| 70 | 70 |
} |
| 71 | 71 |
|
| 72 | 72 |
public: |
| 73 | 73 |
|
| 74 | 74 |
///Read the current time values of the process |
| 75 | 75 |
void stamp() |
| 76 | 76 |
{
|
| 77 | 77 |
#ifndef WIN32 |
| 78 | 78 |
timeval tv; |
| 79 | 79 |
gettimeofday(&tv, 0); |
| 80 | 80 |
rtime=tv.tv_sec+double(tv.tv_usec)/1e6; |
| 81 | 81 |
|
| 82 | 82 |
tms ts; |
| 83 | 83 |
double tck=sysconf(_SC_CLK_TCK); |
| 84 | 84 |
times(&ts); |
| 85 | 85 |
utime=ts.tms_utime/tck; |
| 86 | 86 |
stime=ts.tms_stime/tck; |
| 87 | 87 |
cutime=ts.tms_cutime/tck; |
| 88 | 88 |
cstime=ts.tms_cstime/tck; |
| 89 | 89 |
#else |
| 90 | 90 |
static const double ch = 4294967296.0e-7; |
| 91 | 91 |
static const double cl = 1.0e-7; |
| 92 | 92 |
|
| 93 | 93 |
FILETIME system; |
| 94 | 94 |
GetSystemTimeAsFileTime(&system); |
| 95 | 95 |
rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime; |
| 96 | 96 |
|
| 97 | 97 |
FILETIME create, exit, kernel, user; |
| 98 | 98 |
if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
|
| 99 | 99 |
utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime; |
| 100 | 100 |
stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime; |
| 101 | 101 |
cutime = 0; |
| 102 | 102 |
cstime = 0; |
| 103 | 103 |
} else {
|
| 104 | 104 |
rtime = 0; |
| 105 | 105 |
utime = 0; |
| 106 | 106 |
stime = 0; |
| 107 | 107 |
cutime = 0; |
| 108 | 108 |
cstime = 0; |
| 109 | 109 |
} |
| 110 | 110 |
#endif |
| 111 | 111 |
} |
| 112 | 112 |
|
| 113 | 113 |
/// Constructor initializing with zero |
| 114 | 114 |
TimeStamp() |
| 115 | 115 |
{ _reset(); }
|
| 116 | 116 |
///Constructor initializing with the current time values of the process |
| 117 | 117 |
TimeStamp(void *) { stamp();}
|
| 118 | 118 |
|
| 119 | 119 |
///Set every time value to zero |
| 120 | 120 |
TimeStamp &reset() {_reset();return *this;}
|
| 121 | 121 |
|
| 122 | 122 |
///\e |
| 123 | 123 |
TimeStamp &operator+=(const TimeStamp &b) |
| 124 | 124 |
{
|
| 125 | 125 |
utime+=b.utime; |
| 126 | 126 |
stime+=b.stime; |
| 127 | 127 |
cutime+=b.cutime; |
| 128 | 128 |
cstime+=b.cstime; |
| 129 | 129 |
rtime+=b.rtime; |
| 130 | 130 |
return *this; |
| 131 | 131 |
} |
| 132 | 132 |
///\e |
| 133 | 133 |
TimeStamp operator+(const TimeStamp &b) const |
| 134 | 134 |
{
|
| 135 | 135 |
TimeStamp t(*this); |
| 136 | 136 |
return t+=b; |
| 137 | 137 |
} |
| 138 | 138 |
///\e |
| 139 | 139 |
TimeStamp &operator-=(const TimeStamp &b) |
| 140 | 140 |
{
|
| 141 | 141 |
utime-=b.utime; |
| 142 | 142 |
stime-=b.stime; |
| 143 | 143 |
cutime-=b.cutime; |
| 144 | 144 |
cstime-=b.cstime; |
| 145 | 145 |
rtime-=b.rtime; |
| 146 | 146 |
return *this; |
| 147 | 147 |
} |
| 148 | 148 |
///\e |
| 149 | 149 |
TimeStamp operator-(const TimeStamp &b) const |
| 150 | 150 |
{
|
| 151 | 151 |
TimeStamp t(*this); |
| 152 | 152 |
return t-=b; |
| 153 | 153 |
} |
| 154 | 154 |
///\e |
| 155 | 155 |
TimeStamp &operator*=(double b) |
| 156 | 156 |
{
|
| 157 | 157 |
utime*=b; |
| 158 | 158 |
stime*=b; |
| 159 | 159 |
cutime*=b; |
| 160 | 160 |
cstime*=b; |
| 161 | 161 |
rtime*=b; |
| 162 | 162 |
return *this; |
| 163 | 163 |
} |
| 164 | 164 |
///\e |
| 165 | 165 |
TimeStamp operator*(double b) const |
| 166 | 166 |
{
|
| 167 | 167 |
TimeStamp t(*this); |
| 168 | 168 |
return t*=b; |
| 169 | 169 |
} |
| 170 | 170 |
friend TimeStamp operator*(double b,const TimeStamp &t); |
| 171 | 171 |
///\e |
| 172 | 172 |
TimeStamp &operator/=(double b) |
| 173 | 173 |
{
|
| 174 | 174 |
utime/=b; |
| 175 | 175 |
stime/=b; |
| 176 | 176 |
cutime/=b; |
| 177 | 177 |
cstime/=b; |
| 178 | 178 |
rtime/=b; |
| 179 | 179 |
return *this; |
| 180 | 180 |
} |
| 181 | 181 |
///\e |
| 182 | 182 |
TimeStamp operator/(double b) const |
| 183 | 183 |
{
|
| 184 | 184 |
TimeStamp t(*this); |
| 185 | 185 |
return t/=b; |
| 186 | 186 |
} |
| 187 | 187 |
///The time ellapsed since the last call of stamp() |
| 188 | 188 |
TimeStamp ellapsed() const |
| 189 | 189 |
{
|
| 190 | 190 |
TimeStamp t(NULL); |
| 191 | 191 |
return t-*this; |
| 192 | 192 |
} |
| 193 | 193 |
|
| 194 | 194 |
friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); |
| 195 | 195 |
|
| 196 | 196 |
///Gives back the user time of the process |
| 197 | 197 |
double userTime() const |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_TOLERANCE_H |
| 20 | 20 |
#define LEMON_TOLERANCE_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup misc |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief A basic tool to handle the anomalies of calculation with |
| 25 | 25 |
///floating point numbers. |
| 26 | 26 |
/// |
| 27 | 27 |
|
| 28 | 28 |
namespace lemon {
|
| 29 | 29 |
|
| 30 | 30 |
/// \addtogroup misc |
| 31 | 31 |
/// @{
|
| 32 | 32 |
|
| 33 | 33 |
///\brief A class to provide a basic way to |
| 34 | 34 |
///handle the comparison of numbers that are obtained |
| 35 | 35 |
///as a result of a probably inexact computation. |
| 36 | 36 |
/// |
| 37 | 37 |
///\ref Tolerance is a class to provide a basic way to |
| 38 | 38 |
///handle the comparison of numbers that are obtained |
| 39 | 39 |
///as a result of a probably inexact computation. |
| 40 | 40 |
/// |
| 41 | 41 |
///This is an abstract class, it should be specialized for all |
| 42 | 42 |
///numerical data types. These specialized classes like |
| 43 | 43 |
///Tolerance<double> may offer additional tuning parameters. |
| 44 | 44 |
/// |
| 45 | 45 |
///\sa Tolerance<float> |
| 46 | 46 |
///\sa Tolerance<double> |
| 47 | 47 |
///\sa Tolerance<long double> |
| 48 | 48 |
///\sa Tolerance<int> |
| 49 | 49 |
///\sa Tolerance<long long int> |
| 50 | 50 |
///\sa Tolerance<unsigned int> |
| 51 | 51 |
///\sa Tolerance<unsigned long long int> |
| 52 | 52 |
|
| 53 | 53 |
template<class T> |
| 54 | 54 |
class Tolerance |
| 55 | 55 |
{
|
| 56 | 56 |
public: |
| 57 | 57 |
typedef T Value; |
| 58 | 58 |
|
| 59 | 59 |
///\name Comparisons |
| 60 | 60 |
///The concept is that these bool functions return \c true only if |
| 61 | 61 |
///the related comparisons hold even if some numerical error appeared |
| 62 | 62 |
///during the computations. |
| 63 | 63 |
|
| 64 | 64 |
///@{
|
| 65 | 65 |
|
| 66 | 66 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 67 | 67 |
static bool less(Value a,Value b) {return false;}
|
| 68 | 68 |
///Returns \c true if \c a is \e surely different from \c b |
| 69 | 69 |
static bool different(Value a,Value b) {return false;}
|
| 70 | 70 |
///Returns \c true if \c a is \e surely positive |
| 71 | 71 |
static bool positive(Value a) {return false;}
|
| 72 | 72 |
///Returns \c true if \c a is \e surely negative |
| 73 | 73 |
static bool negative(Value a) {return false;}
|
| 74 | 74 |
///Returns \c true if \c a is \e surely non-zero |
| 75 | 75 |
static bool nonZero(Value a) {return false;}
|
| 76 | 76 |
|
| 77 | 77 |
///@} |
| 78 | 78 |
|
| 79 | 79 |
///Returns the zero value. |
| 80 | 80 |
static Value zero() {return T();}
|
| 81 | 81 |
|
| 82 | 82 |
// static bool finite(Value a) {}
|
| 83 | 83 |
// static Value big() {}
|
| 84 | 84 |
// static Value negativeBig() {}
|
| 85 | 85 |
}; |
| 86 | 86 |
|
| 87 | 87 |
|
| 88 | 88 |
///Float specialization of Tolerance. |
| 89 | 89 |
|
| 90 | 90 |
///Float specialization of Tolerance. |
| 91 | 91 |
///\sa Tolerance |
| 92 | 92 |
///\relates Tolerance |
| 93 | 93 |
template<> |
| 94 | 94 |
class Tolerance<float> |
| 95 | 95 |
{
|
| 96 | 96 |
static float def_epsilon; |
| 97 | 97 |
float _epsilon; |
| 98 | 98 |
public: |
| 99 | 99 |
///\e |
| 100 | 100 |
typedef float Value; |
| 101 | 101 |
|
| 102 | 102 |
///Constructor setting the epsilon tolerance to the default value. |
| 103 | 103 |
Tolerance() : _epsilon(def_epsilon) {}
|
| 104 | 104 |
///Constructor setting the epsilon tolerance to the given value. |
| 105 | 105 |
Tolerance(float e) : _epsilon(e) {}
|
| 106 | 106 |
|
| 107 | 107 |
///Returns the epsilon value. |
| 108 | 108 |
Value epsilon() const {return _epsilon;}
|
| 109 | 109 |
///Sets the epsilon value. |
| 110 | 110 |
void epsilon(Value e) {_epsilon=e;}
|
| 111 | 111 |
|
| 112 | 112 |
///Returns the default epsilon value. |
| 113 | 113 |
static Value defaultEpsilon() {return def_epsilon;}
|
| 114 | 114 |
///Sets the default epsilon value. |
| 115 | 115 |
static void defaultEpsilon(Value e) {def_epsilon=e;}
|
| 116 | 116 |
|
| 117 | 117 |
///\name Comparisons |
| 118 | 118 |
///See \ref lemon::Tolerance "Tolerance" for more details. |
| 119 | 119 |
|
| 120 | 120 |
///@{
|
| 121 | 121 |
|
| 122 | 122 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 123 | 123 |
bool less(Value a,Value b) const {return a+_epsilon<b;}
|
| 124 | 124 |
///Returns \c true if \c a is \e surely different from \c b |
| 125 | 125 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
|
| 126 | 126 |
///Returns \c true if \c a is \e surely positive |
| 127 | 127 |
bool positive(Value a) const { return _epsilon<a; }
|
| 128 | 128 |
///Returns \c true if \c a is \e surely negative |
| 129 | 129 |
bool negative(Value a) const { return -_epsilon>a; }
|
| 130 | 130 |
///Returns \c true if \c a is \e surely non-zero |
| 131 | 131 |
bool nonZero(Value a) const { return positive(a)||negative(a); }
|
| 132 | 132 |
|
| 133 | 133 |
///@} |
| 134 | 134 |
|
| 135 | 135 |
///Returns zero |
| 136 | 136 |
static Value zero() {return 0;}
|
| 137 | 137 |
}; |
| 138 | 138 |
|
| 139 | 139 |
///Double specialization of Tolerance. |
| 140 | 140 |
|
| 141 | 141 |
///Double specialization of Tolerance. |
| 142 | 142 |
///\sa Tolerance |
| 143 | 143 |
///\relates Tolerance |
| 144 | 144 |
template<> |
| 145 | 145 |
class Tolerance<double> |
| 146 | 146 |
{
|
| 147 | 147 |
static double def_epsilon; |
| 148 | 148 |
double _epsilon; |
| 149 | 149 |
public: |
| 150 | 150 |
///\e |
| 151 | 151 |
typedef double Value; |
| 152 | 152 |
|
| 153 | 153 |
///Constructor setting the epsilon tolerance to the default value. |
| 154 | 154 |
Tolerance() : _epsilon(def_epsilon) {}
|
| 155 | 155 |
///Constructor setting the epsilon tolerance to the given value. |
| 156 | 156 |
Tolerance(double e) : _epsilon(e) {}
|
| 157 | 157 |
|
| 158 | 158 |
///Returns the epsilon value. |
| 159 | 159 |
Value epsilon() const {return _epsilon;}
|
| 160 | 160 |
///Sets the epsilon value. |
| 161 | 161 |
void epsilon(Value e) {_epsilon=e;}
|
| 162 | 162 |
|
| 163 | 163 |
///Returns the default epsilon value. |
| 164 | 164 |
static Value defaultEpsilon() {return def_epsilon;}
|
| 165 | 165 |
///Sets the default epsilon value. |
| 166 | 166 |
static void defaultEpsilon(Value e) {def_epsilon=e;}
|
| 167 | 167 |
|
| 168 | 168 |
///\name Comparisons |
| 169 | 169 |
///See \ref lemon::Tolerance "Tolerance" for more details. |
| 170 | 170 |
|
| 171 | 171 |
///@{
|
| 172 | 172 |
|
| 173 | 173 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 174 | 174 |
bool less(Value a,Value b) const {return a+_epsilon<b;}
|
| 175 | 175 |
///Returns \c true if \c a is \e surely different from \c b |
| 176 | 176 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
|
| 177 | 177 |
///Returns \c true if \c a is \e surely positive |
| 178 | 178 |
bool positive(Value a) const { return _epsilon<a; }
|
| 179 | 179 |
///Returns \c true if \c a is \e surely negative |
| 180 | 180 |
bool negative(Value a) const { return -_epsilon>a; }
|
| 181 | 181 |
///Returns \c true if \c a is \e surely non-zero |
| 182 | 182 |
bool nonZero(Value a) const { return positive(a)||negative(a); }
|
| 183 | 183 |
|
| 184 | 184 |
///@} |
| 185 | 185 |
|
| 186 | 186 |
///Returns zero |
| 187 | 187 |
static Value zero() {return 0;}
|
| 188 | 188 |
}; |
| 189 | 189 |
|
| 190 | 190 |
///Long double specialization of Tolerance. |
| 191 | 191 |
|
| 192 | 192 |
///Long double specialization of Tolerance. |
| 193 | 193 |
///\sa Tolerance |
| 194 | 194 |
///\relates Tolerance |
| 195 | 195 |
template<> |
| 196 | 196 |
class Tolerance<long double> |
| 197 | 197 |
{
|
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_UNION_FIND_H |
| 20 | 20 |
#define LEMON_UNION_FIND_H |
| 21 | 21 |
|
| 22 | 22 |
//!\ingroup auxdat |
| 23 | 23 |
//!\file |
| 24 | 24 |
//!\brief Union-Find data structures. |
| 25 | 25 |
//! |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <list> |
| 29 | 29 |
#include <utility> |
| 30 | 30 |
#include <algorithm> |
| 31 | 31 |
#include <functional> |
| 32 | 32 |
|
| 33 | 33 |
#include <lemon/core.h> |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
/// \ingroup auxdat |
| 38 | 38 |
/// |
| 39 | 39 |
/// \brief A \e Union-Find data structure implementation |
| 40 | 40 |
/// |
| 41 | 41 |
/// The class implements the \e Union-Find data structure. |
| 42 | 42 |
/// The union operation uses rank heuristic, while |
| 43 | 43 |
/// the find operation uses path compression. |
| 44 | 44 |
/// This is a very simple but efficient implementation, providing |
| 45 | 45 |
/// only four methods: join (union), find, insert and size. |
| 46 | 46 |
/// For more features see the \ref UnionFindEnum class. |
| 47 | 47 |
/// |
| 48 | 48 |
/// It is primarily used in Kruskal algorithm for finding minimal |
| 49 | 49 |
/// cost spanning tree in a graph. |
| 50 | 50 |
/// \sa kruskal() |
| 51 | 51 |
/// |
| 52 | 52 |
/// \pre You need to add all the elements by the \ref insert() |
| 53 | 53 |
/// method. |
| 54 | 54 |
template <typename _ItemIntMap> |
| 55 | 55 |
class UnionFind {
|
| 56 | 56 |
public: |
| 57 | 57 |
|
| 58 | 58 |
typedef _ItemIntMap ItemIntMap; |
| 59 | 59 |
typedef typename ItemIntMap::Key Item; |
| 60 | 60 |
|
| 61 | 61 |
private: |
| 62 | 62 |
// If the items vector stores negative value for an item then |
| 63 | 63 |
// that item is root item and it has -items[it] component size. |
| 64 | 64 |
// Else the items[it] contains the index of the parent. |
| 65 | 65 |
std::vector<int> items; |
| 66 | 66 |
ItemIntMap& index; |
| 67 | 67 |
|
| 68 | 68 |
bool rep(int idx) const {
|
| 69 | 69 |
return items[idx] < 0; |
| 70 | 70 |
} |
| 71 | 71 |
|
| 72 | 72 |
int repIndex(int idx) const {
|
| 73 | 73 |
int k = idx; |
| 74 | 74 |
while (!rep(k)) {
|
| 75 | 75 |
k = items[k] ; |
| 76 | 76 |
} |
| 77 | 77 |
while (idx != k) {
|
| 78 | 78 |
int next = items[idx]; |
| 79 | 79 |
const_cast<int&>(items[idx]) = k; |
| 80 | 80 |
idx = next; |
| 81 | 81 |
} |
| 82 | 82 |
return k; |
| 83 | 83 |
} |
| 84 | 84 |
|
| 85 | 85 |
public: |
| 86 | 86 |
|
| 87 | 87 |
/// \brief Constructor |
| 88 | 88 |
/// |
| 89 | 89 |
/// Constructor of the UnionFind class. You should give an item to |
| 90 | 90 |
/// integer map which will be used from the data structure. If you |
| 91 | 91 |
/// modify directly this map that may cause segmentation fault, |
| 92 | 92 |
/// invalid data structure, or infinite loop when you use again |
| 93 | 93 |
/// the union-find. |
| 94 | 94 |
UnionFind(ItemIntMap& m) : index(m) {}
|
| 95 | 95 |
|
| 96 | 96 |
/// \brief Returns the index of the element's component. |
| 97 | 97 |
/// |
| 98 | 98 |
/// The method returns the index of the element's component. |
| 99 | 99 |
/// This is an integer between zero and the number of inserted elements. |
| 100 | 100 |
/// |
| 101 | 101 |
int find(const Item& a) {
|
| 102 | 102 |
return repIndex(index[a]); |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 | 105 |
/// \brief Clears the union-find data structure |
| 106 | 106 |
/// |
| 107 | 107 |
/// Erase each item from the data structure. |
| 108 | 108 |
void clear() {
|
| 109 | 109 |
items.clear(); |
| 110 | 110 |
} |
| 111 | 111 |
|
| 112 | 112 |
/// \brief Inserts a new element into the structure. |
| 113 | 113 |
/// |
| 114 | 114 |
/// This method inserts a new element into the data structure. |
| 115 | 115 |
/// |
| 116 | 116 |
/// The method returns the index of the new component. |
| 117 | 117 |
int insert(const Item& a) {
|
| 118 | 118 |
int n = items.size(); |
| 119 | 119 |
items.push_back(-1); |
| 120 | 120 |
index.set(a,n); |
| 121 | 121 |
return n; |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
/// \brief Joining the components of element \e a and element \e b. |
| 125 | 125 |
/// |
| 126 | 126 |
/// This is the \e union operation of the Union-Find structure. |
| 127 | 127 |
/// Joins the component of element \e a and component of |
| 128 | 128 |
/// element \e b. If \e a and \e b are in the same component then |
| 129 | 129 |
/// it returns false otherwise it returns true. |
| 130 | 130 |
bool join(const Item& a, const Item& b) {
|
| 131 | 131 |
int ka = repIndex(index[a]); |
| 132 | 132 |
int kb = repIndex(index[b]); |
| 133 | 133 |
|
| 134 | 134 |
if ( ka == kb ) |
| 135 | 135 |
return false; |
| 136 | 136 |
|
| 137 | 137 |
if (items[ka] < items[kb]) {
|
| 138 | 138 |
items[ka] += items[kb]; |
| 139 | 139 |
items[kb] = ka; |
| 140 | 140 |
} else {
|
| 141 | 141 |
items[kb] += items[ka]; |
| 142 | 142 |
items[ka] = kb; |
| 143 | 143 |
} |
| 144 | 144 |
return true; |
| 145 | 145 |
} |
| 146 | 146 |
|
| 147 | 147 |
/// \brief Returns the size of the component of element \e a. |
| 148 | 148 |
/// |
| 149 | 149 |
/// Returns the size of the component of element \e a. |
| 150 | 150 |
int size(const Item& a) {
|
| 151 | 151 |
int k = repIndex(index[a]); |
| 152 | 152 |
return - items[k]; |
| 153 | 153 |
} |
| 154 | 154 |
|
| 155 | 155 |
}; |
| 156 | 156 |
|
| 157 | 157 |
/// \ingroup auxdat |
| 158 | 158 |
/// |
| 159 | 159 |
/// \brief A \e Union-Find data structure implementation which |
| 160 | 160 |
/// is able to enumerate the components. |
| 161 | 161 |
/// |
| 162 | 162 |
/// The class implements a \e Union-Find data structure |
| 163 | 163 |
/// which is able to enumerate the components and the items in |
| 164 | 164 |
/// a component. If you don't need this feature then perhaps it's |
| 165 | 165 |
/// better to use the \ref UnionFind class which is more efficient. |
| 166 | 166 |
/// |
| 167 | 167 |
/// The union operation uses rank heuristic, while |
| 168 | 168 |
/// the find operation uses path compression. |
| 169 | 169 |
/// |
| 170 | 170 |
/// \pre You need to add all the elements by the \ref insert() |
| 171 | 171 |
/// method. |
| 172 | 172 |
/// |
| 173 | 173 |
template <typename _ItemIntMap> |
| 174 | 174 |
class UnionFindEnum {
|
| 175 | 175 |
public: |
| 176 | 176 |
|
| 177 | 177 |
typedef _ItemIntMap ItemIntMap; |
| 178 | 178 |
typedef typename ItemIntMap::Key Item; |
| 179 | 179 |
|
| 180 | 180 |
private: |
| 181 | 181 |
|
| 182 | 182 |
ItemIntMap& index; |
| 183 | 183 |
|
| 184 | 184 |
// If the parent stores negative value for an item then that item |
| 185 | 185 |
// is root item and it has ~(items[it].parent) component id. Else |
| 186 | 186 |
// the items[it].parent contains the index of the parent. |
| 187 | 187 |
// |
| 188 | 188 |
// The \c next and \c prev provides the double-linked |
| 189 | 189 |
// cyclic list of one component's items. |
| 190 | 190 |
struct ItemT {
|
| 191 | 191 |
int parent; |
| 192 | 192 |
Item item; |
| 193 | 193 |
|
| 194 | 194 |
int next, prev; |
| 195 | 195 |
}; |
| 196 | 196 |
|
| 197 | 197 |
std::vector<ItemT> items; |
| ... | ... |
@@ -1000,385 +1000,385 @@ |
| 1000 | 1000 |
int parent; |
| 1001 | 1001 |
Item item; |
| 1002 | 1002 |
Value prio; |
| 1003 | 1003 |
int next, prev; |
| 1004 | 1004 |
int left, right; |
| 1005 | 1005 |
int size; |
| 1006 | 1006 |
}; |
| 1007 | 1007 |
|
| 1008 | 1008 |
int first_free_node; |
| 1009 | 1009 |
std::vector<ItemNode> nodes; |
| 1010 | 1010 |
|
| 1011 | 1011 |
int newNode() {
|
| 1012 | 1012 |
if (first_free_node < 0) {
|
| 1013 | 1013 |
int id = nodes.size(); |
| 1014 | 1014 |
nodes.push_back(ItemNode()); |
| 1015 | 1015 |
return id; |
| 1016 | 1016 |
} else {
|
| 1017 | 1017 |
int id = first_free_node; |
| 1018 | 1018 |
first_free_node = nodes[id].next; |
| 1019 | 1019 |
return id; |
| 1020 | 1020 |
} |
| 1021 | 1021 |
} |
| 1022 | 1022 |
|
| 1023 | 1023 |
void deleteNode(int id) {
|
| 1024 | 1024 |
nodes[id].next = first_free_node; |
| 1025 | 1025 |
first_free_node = id; |
| 1026 | 1026 |
} |
| 1027 | 1027 |
|
| 1028 | 1028 |
Comp comp; |
| 1029 | 1029 |
|
| 1030 | 1030 |
int findClass(int id) const {
|
| 1031 | 1031 |
int kd = id; |
| 1032 | 1032 |
while (kd >= 0) {
|
| 1033 | 1033 |
kd = nodes[kd].parent; |
| 1034 | 1034 |
} |
| 1035 | 1035 |
return ~kd; |
| 1036 | 1036 |
} |
| 1037 | 1037 |
|
| 1038 | 1038 |
int leftNode(int id) const {
|
| 1039 | 1039 |
int kd = ~(classes[id].parent); |
| 1040 | 1040 |
for (int i = 0; i < classes[id].depth; ++i) {
|
| 1041 | 1041 |
kd = nodes[kd].left; |
| 1042 | 1042 |
} |
| 1043 | 1043 |
return kd; |
| 1044 | 1044 |
} |
| 1045 | 1045 |
|
| 1046 | 1046 |
int nextNode(int id) const {
|
| 1047 | 1047 |
int depth = 0; |
| 1048 | 1048 |
while (id >= 0 && nodes[id].next == -1) {
|
| 1049 | 1049 |
id = nodes[id].parent; |
| 1050 | 1050 |
++depth; |
| 1051 | 1051 |
} |
| 1052 | 1052 |
if (id < 0) {
|
| 1053 | 1053 |
return -1; |
| 1054 | 1054 |
} |
| 1055 | 1055 |
id = nodes[id].next; |
| 1056 | 1056 |
while (depth--) {
|
| 1057 | 1057 |
id = nodes[id].left; |
| 1058 | 1058 |
} |
| 1059 | 1059 |
return id; |
| 1060 | 1060 |
} |
| 1061 | 1061 |
|
| 1062 | 1062 |
|
| 1063 | 1063 |
void setPrio(int id) {
|
| 1064 | 1064 |
int jd = nodes[id].left; |
| 1065 | 1065 |
nodes[id].prio = nodes[jd].prio; |
| 1066 | 1066 |
nodes[id].item = nodes[jd].item; |
| 1067 | 1067 |
jd = nodes[jd].next; |
| 1068 | 1068 |
while (jd != -1) {
|
| 1069 | 1069 |
if (comp(nodes[jd].prio, nodes[id].prio)) {
|
| 1070 | 1070 |
nodes[id].prio = nodes[jd].prio; |
| 1071 | 1071 |
nodes[id].item = nodes[jd].item; |
| 1072 | 1072 |
} |
| 1073 | 1073 |
jd = nodes[jd].next; |
| 1074 | 1074 |
} |
| 1075 | 1075 |
} |
| 1076 | 1076 |
|
| 1077 | 1077 |
void push(int id, int jd) {
|
| 1078 | 1078 |
nodes[id].size = 1; |
| 1079 | 1079 |
nodes[id].left = nodes[id].right = jd; |
| 1080 | 1080 |
nodes[jd].next = nodes[jd].prev = -1; |
| 1081 | 1081 |
nodes[jd].parent = id; |
| 1082 | 1082 |
} |
| 1083 | 1083 |
|
| 1084 | 1084 |
void pushAfter(int id, int jd) {
|
| 1085 | 1085 |
int kd = nodes[id].parent; |
| 1086 | 1086 |
if (nodes[id].next != -1) {
|
| 1087 | 1087 |
nodes[nodes[id].next].prev = jd; |
| 1088 | 1088 |
if (kd >= 0) {
|
| 1089 | 1089 |
nodes[kd].size += 1; |
| 1090 | 1090 |
} |
| 1091 | 1091 |
} else {
|
| 1092 | 1092 |
if (kd >= 0) {
|
| 1093 | 1093 |
nodes[kd].right = jd; |
| 1094 | 1094 |
nodes[kd].size += 1; |
| 1095 | 1095 |
} |
| 1096 | 1096 |
} |
| 1097 | 1097 |
nodes[jd].next = nodes[id].next; |
| 1098 | 1098 |
nodes[jd].prev = id; |
| 1099 | 1099 |
nodes[id].next = jd; |
| 1100 | 1100 |
nodes[jd].parent = kd; |
| 1101 | 1101 |
} |
| 1102 | 1102 |
|
| 1103 | 1103 |
void pushRight(int id, int jd) {
|
| 1104 | 1104 |
nodes[id].size += 1; |
| 1105 | 1105 |
nodes[jd].prev = nodes[id].right; |
| 1106 | 1106 |
nodes[jd].next = -1; |
| 1107 | 1107 |
nodes[nodes[id].right].next = jd; |
| 1108 | 1108 |
nodes[id].right = jd; |
| 1109 | 1109 |
nodes[jd].parent = id; |
| 1110 | 1110 |
} |
| 1111 | 1111 |
|
| 1112 | 1112 |
void popRight(int id) {
|
| 1113 | 1113 |
nodes[id].size -= 1; |
| 1114 | 1114 |
int jd = nodes[id].right; |
| 1115 | 1115 |
nodes[nodes[jd].prev].next = -1; |
| 1116 | 1116 |
nodes[id].right = nodes[jd].prev; |
| 1117 | 1117 |
} |
| 1118 | 1118 |
|
| 1119 | 1119 |
void splice(int id, int jd) {
|
| 1120 | 1120 |
nodes[id].size += nodes[jd].size; |
| 1121 | 1121 |
nodes[nodes[id].right].next = nodes[jd].left; |
| 1122 | 1122 |
nodes[nodes[jd].left].prev = nodes[id].right; |
| 1123 | 1123 |
int kd = nodes[jd].left; |
| 1124 | 1124 |
while (kd != -1) {
|
| 1125 | 1125 |
nodes[kd].parent = id; |
| 1126 | 1126 |
kd = nodes[kd].next; |
| 1127 | 1127 |
} |
| 1128 | 1128 |
nodes[id].right = nodes[jd].right; |
| 1129 | 1129 |
} |
| 1130 | 1130 |
|
| 1131 | 1131 |
void split(int id, int jd) {
|
| 1132 | 1132 |
int kd = nodes[id].parent; |
| 1133 | 1133 |
nodes[kd].right = nodes[id].prev; |
| 1134 | 1134 |
nodes[nodes[id].prev].next = -1; |
| 1135 | 1135 |
|
| 1136 | 1136 |
nodes[jd].left = id; |
| 1137 | 1137 |
nodes[id].prev = -1; |
| 1138 | 1138 |
int num = 0; |
| 1139 | 1139 |
while (id != -1) {
|
| 1140 | 1140 |
nodes[id].parent = jd; |
| 1141 | 1141 |
nodes[jd].right = id; |
| 1142 | 1142 |
id = nodes[id].next; |
| 1143 | 1143 |
++num; |
| 1144 | 1144 |
} |
| 1145 | 1145 |
nodes[kd].size -= num; |
| 1146 | 1146 |
nodes[jd].size = num; |
| 1147 | 1147 |
} |
| 1148 | 1148 |
|
| 1149 | 1149 |
void pushLeft(int id, int jd) {
|
| 1150 | 1150 |
nodes[id].size += 1; |
| 1151 | 1151 |
nodes[jd].next = nodes[id].left; |
| 1152 | 1152 |
nodes[jd].prev = -1; |
| 1153 | 1153 |
nodes[nodes[id].left].prev = jd; |
| 1154 | 1154 |
nodes[id].left = jd; |
| 1155 | 1155 |
nodes[jd].parent = id; |
| 1156 | 1156 |
} |
| 1157 | 1157 |
|
| 1158 | 1158 |
void popLeft(int id) {
|
| 1159 | 1159 |
nodes[id].size -= 1; |
| 1160 | 1160 |
int jd = nodes[id].left; |
| 1161 | 1161 |
nodes[nodes[jd].next].prev = -1; |
| 1162 | 1162 |
nodes[id].left = nodes[jd].next; |
| 1163 | 1163 |
} |
| 1164 | 1164 |
|
| 1165 | 1165 |
void repairLeft(int id) {
|
| 1166 | 1166 |
int jd = ~(classes[id].parent); |
| 1167 | 1167 |
while (nodes[jd].left != -1) {
|
| 1168 | 1168 |
int kd = nodes[jd].left; |
| 1169 | 1169 |
if (nodes[jd].size == 1) {
|
| 1170 | 1170 |
if (nodes[jd].parent < 0) {
|
| 1171 | 1171 |
classes[id].parent = ~kd; |
| 1172 | 1172 |
classes[id].depth -= 1; |
| 1173 | 1173 |
nodes[kd].parent = ~id; |
| 1174 | 1174 |
deleteNode(jd); |
| 1175 | 1175 |
jd = kd; |
| 1176 | 1176 |
} else {
|
| 1177 | 1177 |
int pd = nodes[jd].parent; |
| 1178 | 1178 |
if (nodes[nodes[jd].next].size < cmax) {
|
| 1179 | 1179 |
pushLeft(nodes[jd].next, nodes[jd].left); |
| 1180 | 1180 |
if (less(jd, nodes[jd].next) || |
| 1181 | 1181 |
nodes[jd].item == nodes[pd].item) {
|
| 1182 | 1182 |
nodes[nodes[jd].next].prio = nodes[jd].prio; |
| 1183 | 1183 |
nodes[nodes[jd].next].item = nodes[jd].item; |
| 1184 | 1184 |
} |
| 1185 | 1185 |
popLeft(pd); |
| 1186 | 1186 |
deleteNode(jd); |
| 1187 | 1187 |
jd = pd; |
| 1188 | 1188 |
} else {
|
| 1189 | 1189 |
int ld = nodes[nodes[jd].next].left; |
| 1190 | 1190 |
popLeft(nodes[jd].next); |
| 1191 | 1191 |
pushRight(jd, ld); |
| 1192 |
if (less(ld, nodes[jd].left) || |
|
| 1192 |
if (less(ld, nodes[jd].left) || |
|
| 1193 | 1193 |
nodes[ld].item == nodes[pd].item) {
|
| 1194 | 1194 |
nodes[jd].item = nodes[ld].item; |
| 1195 | 1195 |
nodes[jd].prio = nodes[ld].prio; |
| 1196 | 1196 |
} |
| 1197 | 1197 |
if (nodes[nodes[jd].next].item == nodes[ld].item) {
|
| 1198 | 1198 |
setPrio(nodes[jd].next); |
| 1199 | 1199 |
} |
| 1200 | 1200 |
jd = nodes[jd].left; |
| 1201 | 1201 |
} |
| 1202 | 1202 |
} |
| 1203 | 1203 |
} else {
|
| 1204 | 1204 |
jd = nodes[jd].left; |
| 1205 | 1205 |
} |
| 1206 | 1206 |
} |
| 1207 | 1207 |
} |
| 1208 | 1208 |
|
| 1209 | 1209 |
void repairRight(int id) {
|
| 1210 | 1210 |
int jd = ~(classes[id].parent); |
| 1211 | 1211 |
while (nodes[jd].right != -1) {
|
| 1212 | 1212 |
int kd = nodes[jd].right; |
| 1213 | 1213 |
if (nodes[jd].size == 1) {
|
| 1214 | 1214 |
if (nodes[jd].parent < 0) {
|
| 1215 | 1215 |
classes[id].parent = ~kd; |
| 1216 | 1216 |
classes[id].depth -= 1; |
| 1217 | 1217 |
nodes[kd].parent = ~id; |
| 1218 | 1218 |
deleteNode(jd); |
| 1219 | 1219 |
jd = kd; |
| 1220 | 1220 |
} else {
|
| 1221 | 1221 |
int pd = nodes[jd].parent; |
| 1222 | 1222 |
if (nodes[nodes[jd].prev].size < cmax) {
|
| 1223 | 1223 |
pushRight(nodes[jd].prev, nodes[jd].right); |
| 1224 | 1224 |
if (less(jd, nodes[jd].prev) || |
| 1225 | 1225 |
nodes[jd].item == nodes[pd].item) {
|
| 1226 | 1226 |
nodes[nodes[jd].prev].prio = nodes[jd].prio; |
| 1227 | 1227 |
nodes[nodes[jd].prev].item = nodes[jd].item; |
| 1228 | 1228 |
} |
| 1229 | 1229 |
popRight(pd); |
| 1230 | 1230 |
deleteNode(jd); |
| 1231 | 1231 |
jd = pd; |
| 1232 | 1232 |
} else {
|
| 1233 | 1233 |
int ld = nodes[nodes[jd].prev].right; |
| 1234 | 1234 |
popRight(nodes[jd].prev); |
| 1235 | 1235 |
pushLeft(jd, ld); |
| 1236 | 1236 |
if (less(ld, nodes[jd].right) || |
| 1237 | 1237 |
nodes[ld].item == nodes[pd].item) {
|
| 1238 | 1238 |
nodes[jd].item = nodes[ld].item; |
| 1239 | 1239 |
nodes[jd].prio = nodes[ld].prio; |
| 1240 | 1240 |
} |
| 1241 | 1241 |
if (nodes[nodes[jd].prev].item == nodes[ld].item) {
|
| 1242 | 1242 |
setPrio(nodes[jd].prev); |
| 1243 | 1243 |
} |
| 1244 | 1244 |
jd = nodes[jd].right; |
| 1245 | 1245 |
} |
| 1246 | 1246 |
} |
| 1247 | 1247 |
} else {
|
| 1248 | 1248 |
jd = nodes[jd].right; |
| 1249 | 1249 |
} |
| 1250 | 1250 |
} |
| 1251 | 1251 |
} |
| 1252 | 1252 |
|
| 1253 | 1253 |
|
| 1254 | 1254 |
bool less(int id, int jd) const {
|
| 1255 | 1255 |
return comp(nodes[id].prio, nodes[jd].prio); |
| 1256 | 1256 |
} |
| 1257 | 1257 |
|
| 1258 | 1258 |
public: |
| 1259 | 1259 |
|
| 1260 | 1260 |
/// \brief Returns true when the given class is alive. |
| 1261 | 1261 |
/// |
| 1262 | 1262 |
/// Returns true when the given class is alive, ie. the class is |
| 1263 | 1263 |
/// not nested into other class. |
| 1264 | 1264 |
bool alive(int cls) const {
|
| 1265 | 1265 |
return classes[cls].parent < 0; |
| 1266 | 1266 |
} |
| 1267 | 1267 |
|
| 1268 | 1268 |
/// \brief Returns true when the given class is trivial. |
| 1269 | 1269 |
/// |
| 1270 | 1270 |
/// Returns true when the given class is trivial, ie. the class |
| 1271 | 1271 |
/// contains just one item directly. |
| 1272 | 1272 |
bool trivial(int cls) const {
|
| 1273 | 1273 |
return classes[cls].left == -1; |
| 1274 | 1274 |
} |
| 1275 | 1275 |
|
| 1276 | 1276 |
/// \brief Constructs the union-find. |
| 1277 | 1277 |
/// |
| 1278 | 1278 |
/// Constructs the union-find. |
| 1279 | 1279 |
/// \brief _index The index map of the union-find. The data |
| 1280 | 1280 |
/// structure uses internally for store references. |
| 1281 | 1281 |
HeapUnionFind(ItemIntMap& _index) |
| 1282 | 1282 |
: index(_index), first_class(-1), |
| 1283 | 1283 |
first_free_class(-1), first_free_node(-1) {}
|
| 1284 | 1284 |
|
| 1285 | 1285 |
/// \brief Insert a new node into a new component. |
| 1286 | 1286 |
/// |
| 1287 | 1287 |
/// Insert a new node into a new component. |
| 1288 | 1288 |
/// \param item The item of the new node. |
| 1289 | 1289 |
/// \param prio The priority of the new node. |
| 1290 | 1290 |
/// \return The class id of the one-item-heap. |
| 1291 | 1291 |
int insert(const Item& item, const Value& prio) {
|
| 1292 | 1292 |
int id = newNode(); |
| 1293 | 1293 |
nodes[id].item = item; |
| 1294 | 1294 |
nodes[id].prio = prio; |
| 1295 | 1295 |
nodes[id].size = 0; |
| 1296 | 1296 |
|
| 1297 | 1297 |
nodes[id].prev = -1; |
| 1298 | 1298 |
nodes[id].next = -1; |
| 1299 | 1299 |
|
| 1300 | 1300 |
nodes[id].left = -1; |
| 1301 | 1301 |
nodes[id].right = -1; |
| 1302 | 1302 |
|
| 1303 | 1303 |
nodes[id].item = item; |
| 1304 | 1304 |
index[item] = id; |
| 1305 | 1305 |
|
| 1306 | 1306 |
int class_id = newClass(); |
| 1307 | 1307 |
classes[class_id].parent = ~id; |
| 1308 | 1308 |
classes[class_id].depth = 0; |
| 1309 | 1309 |
|
| 1310 | 1310 |
classes[class_id].left = -1; |
| 1311 | 1311 |
classes[class_id].right = -1; |
| 1312 | 1312 |
|
| 1313 | 1313 |
if (first_class != -1) {
|
| 1314 | 1314 |
classes[first_class].prev = class_id; |
| 1315 | 1315 |
} |
| 1316 | 1316 |
classes[class_id].next = first_class; |
| 1317 | 1317 |
classes[class_id].prev = -1; |
| 1318 | 1318 |
first_class = class_id; |
| 1319 | 1319 |
|
| 1320 | 1320 |
nodes[id].parent = ~class_id; |
| 1321 | 1321 |
|
| 1322 | 1322 |
return class_id; |
| 1323 | 1323 |
} |
| 1324 | 1324 |
|
| 1325 | 1325 |
/// \brief The class of the item. |
| 1326 | 1326 |
/// |
| 1327 | 1327 |
/// \return The alive class id of the item, which is not nested into |
| 1328 | 1328 |
/// other classes. |
| 1329 | 1329 |
/// |
| 1330 | 1330 |
/// The time complexity is O(log(n)). |
| 1331 | 1331 |
int find(const Item& item) const {
|
| 1332 | 1332 |
return findClass(index[item]); |
| 1333 | 1333 |
} |
| 1334 | 1334 |
|
| 1335 | 1335 |
/// \brief Joins the classes. |
| 1336 | 1336 |
/// |
| 1337 | 1337 |
/// The current function joins the given classes. The parameter is |
| 1338 | 1338 |
/// an STL range which should be contains valid class ids. The |
| 1339 | 1339 |
/// time complexity is O(log(n)*k) where n is the overall number |
| 1340 | 1340 |
/// of the joined nodes and k is the number of classes. |
| 1341 | 1341 |
/// \return The class of the joined classes. |
| 1342 | 1342 |
/// \pre The range should contain at least two class ids. |
| 1343 | 1343 |
template <typename Iterator> |
| 1344 | 1344 |
int join(Iterator begin, Iterator end) {
|
| 1345 | 1345 |
std::vector<int> cs; |
| 1346 | 1346 |
for (Iterator it = begin; it != end; ++it) {
|
| 1347 | 1347 |
cs.push_back(*it); |
| 1348 | 1348 |
} |
| 1349 | 1349 |
|
| 1350 | 1350 |
int class_id = newClass(); |
| 1351 | 1351 |
{ // creation union-find
|
| 1352 | 1352 |
|
| 1353 | 1353 |
if (first_class != -1) {
|
| 1354 | 1354 |
classes[first_class].prev = class_id; |
| 1355 | 1355 |
} |
| 1356 | 1356 |
classes[class_id].next = first_class; |
| 1357 | 1357 |
classes[class_id].prev = -1; |
| 1358 | 1358 |
first_class = class_id; |
| 1359 | 1359 |
|
| 1360 | 1360 |
classes[class_id].depth = classes[cs[0]].depth; |
| 1361 | 1361 |
classes[class_id].parent = classes[cs[0]].parent; |
| 1362 | 1362 |
nodes[~(classes[class_id].parent)].parent = ~class_id; |
| 1363 | 1363 |
|
| 1364 | 1364 |
int l = cs[0]; |
| 1365 | 1365 |
|
| 1366 | 1366 |
classes[class_id].left = l; |
| 1367 | 1367 |
classes[class_id].right = l; |
| 1368 | 1368 |
|
| 1369 | 1369 |
if (classes[l].next != -1) {
|
| 1370 | 1370 |
classes[classes[l].next].prev = classes[l].prev; |
| 1371 | 1371 |
} |
| 1372 | 1372 |
classes[classes[l].prev].next = classes[l].next; |
| 1373 | 1373 |
|
| 1374 | 1374 |
classes[l].prev = -1; |
| 1375 | 1375 |
classes[l].next = -1; |
| 1376 | 1376 |
|
| 1377 | 1377 |
classes[l].depth = leftNode(l); |
| 1378 | 1378 |
classes[l].parent = class_id; |
| 1379 | 1379 |
|
| 1380 | 1380 |
} |
| 1381 | 1381 |
|
| 1382 | 1382 |
{ // merging of heap
|
| 1383 | 1383 |
int l = class_id; |
| 1384 | 1384 |
for (int ci = 1; ci < int(cs.size()); ++ci) {
|
| 1 | 1 |
EXTRA_DIST += \ |
| 2 | 2 |
test/CMakeLists.txt |
| 3 | 3 |
|
| 4 | 4 |
noinst_HEADERS += \ |
| 5 | 5 |
test/graph_test.h \ |
| 6 |
|
|
| 6 |
test/test_tools.h |
|
| 7 | 7 |
|
| 8 | 8 |
check_PROGRAMS += \ |
| 9 | 9 |
test/bfs_test \ |
| 10 |
test/circulation_test \ |
|
| 11 |
test/counter_test \ |
|
| 10 |
test/circulation_test \ |
|
| 11 |
test/counter_test \ |
|
| 12 | 12 |
test/dfs_test \ |
| 13 | 13 |
test/digraph_test \ |
| 14 | 14 |
test/dijkstra_test \ |
| 15 |
|
|
| 15 |
test/dim_test \ |
|
| 16 | 16 |
test/error_test \ |
| 17 | 17 |
test/graph_adaptor_test \ |
| 18 | 18 |
test/graph_copy_test \ |
| 19 | 19 |
test/graph_test \ |
| 20 | 20 |
test/graph_utils_test \ |
| 21 | 21 |
test/hao_orlin_test \ |
| 22 | 22 |
test/heap_test \ |
| 23 | 23 |
test/kruskal_test \ |
| 24 |
|
|
| 24 |
test/maps_test \ |
|
| 25 | 25 |
test/max_matching_test \ |
| 26 |
test/path_test \ |
|
| 27 |
test/preflow_test \ |
|
| 28 |
test/random_test \ |
|
| 29 |
test/suurballe_test \ |
|
| 30 |
test/test_tools_fail \ |
|
| 31 |
test/test_tools_pass \ |
|
| 32 |
|
|
| 26 |
test/path_test \ |
|
| 27 |
test/preflow_test \ |
|
| 28 |
test/random_test \ |
|
| 29 |
test/suurballe_test \ |
|
| 30 |
test/test_tools_fail \ |
|
| 31 |
test/test_tools_pass \ |
|
| 32 |
test/time_measure_test \ |
|
| 33 | 33 |
test/unionfind_test |
| 34 | 34 |
|
| 35 | 35 |
TESTS += $(check_PROGRAMS) |
| 36 | 36 |
XFAIL_TESTS += test/test_tools_fail$(EXEEXT) |
| 37 | 37 |
|
| 38 | 38 |
test_bfs_test_SOURCES = test/bfs_test.cc |
| 39 | 39 |
test_circulation_test_SOURCES = test/circulation_test.cc |
| 40 | 40 |
test_counter_test_SOURCES = test/counter_test.cc |
| 41 | 41 |
test_dfs_test_SOURCES = test/dfs_test.cc |
| 42 | 42 |
test_digraph_test_SOURCES = test/digraph_test.cc |
| 43 | 43 |
test_dijkstra_test_SOURCES = test/dijkstra_test.cc |
| 44 | 44 |
test_dim_test_SOURCES = test/dim_test.cc |
| 45 | 45 |
test_error_test_SOURCES = test/error_test.cc |
| 46 | 46 |
test_graph_adaptor_test_SOURCES = test/graph_adaptor_test.cc |
| 47 | 47 |
test_graph_copy_test_SOURCES = test/graph_copy_test.cc |
| 48 | 48 |
test_graph_test_SOURCES = test/graph_test.cc |
| 49 | 49 |
test_graph_utils_test_SOURCES = test/graph_utils_test.cc |
| 50 | 50 |
test_heap_test_SOURCES = test/heap_test.cc |
| 51 | 51 |
test_kruskal_test_SOURCES = test/kruskal_test.cc |
| 52 | 52 |
test_hao_orlin_test_SOURCES = test/hao_orlin_test.cc |
| 53 | 53 |
test_maps_test_SOURCES = test/maps_test.cc |
| 54 | 54 |
test_max_matching_test_SOURCES = test/max_matching_test.cc |
| 55 | 55 |
test_path_test_SOURCES = test/path_test.cc |
| 56 | 56 |
test_preflow_test_SOURCES = test/preflow_test.cc |
| 57 | 57 |
test_suurballe_test_SOURCES = test/suurballe_test.cc |
| 58 | 58 |
test_random_test_SOURCES = test/random_test.cc |
| 59 | 59 |
test_test_tools_fail_SOURCES = test/test_tools_fail.cc |
| 60 | 60 |
test_test_tools_pass_SOURCES = test/test_tools_pass.cc |
| 61 | 61 |
test_time_measure_test_SOURCES = test/time_measure_test.cc |
| 62 | 62 |
test_unionfind_test_SOURCES = test/unionfind_test.cc |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/smart_graph.h> |
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/bfs.h> |
| 24 | 24 |
#include <lemon/path.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "graph_test.h" |
| 27 | 27 |
#include "test_tools.h" |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| 30 | 30 |
|
| 31 | 31 |
char test_lgf[] = |
| 32 | 32 |
"@nodes\n" |
| 33 | 33 |
"label\n" |
| 34 | 34 |
"0\n" |
| 35 | 35 |
"1\n" |
| 36 | 36 |
"2\n" |
| 37 | 37 |
"3\n" |
| 38 | 38 |
"4\n" |
| 39 | 39 |
"5\n" |
| 40 | 40 |
"@arcs\n" |
| 41 | 41 |
" label\n" |
| 42 | 42 |
"0 1 0\n" |
| 43 | 43 |
"1 2 1\n" |
| 44 | 44 |
"2 3 2\n" |
| 45 | 45 |
"3 4 3\n" |
| 46 | 46 |
"0 3 4\n" |
| 47 | 47 |
"0 3 5\n" |
| 48 | 48 |
"5 2 6\n" |
| 49 | 49 |
"@attributes\n" |
| 50 | 50 |
"source 0\n" |
| 51 | 51 |
"target 4\n"; |
| 52 | 52 |
|
| 53 | 53 |
void checkBfsCompile() |
| 54 | 54 |
{
|
| 55 | 55 |
typedef concepts::Digraph Digraph; |
| 56 | 56 |
typedef Bfs<Digraph> BType; |
| 57 | 57 |
typedef Digraph::Node Node; |
| 58 | 58 |
typedef Digraph::Arc Arc; |
| 59 | 59 |
|
| 60 | 60 |
Digraph G; |
| 61 | 61 |
Node s, t; |
| 62 | 62 |
Arc e; |
| 63 | 63 |
int l; |
| 64 | 64 |
bool b; |
| 65 | 65 |
BType::DistMap d(G); |
| 66 | 66 |
BType::PredMap p(G); |
| 67 | 67 |
Path<Digraph> pp; |
| 68 | 68 |
|
| 69 | 69 |
{
|
| 70 | 70 |
BType bfs_test(G); |
| 71 | 71 |
|
| 72 | 72 |
bfs_test.run(s); |
| 73 | 73 |
bfs_test.run(s,t); |
| 74 | 74 |
bfs_test.run(); |
| 75 | 75 |
|
| 76 | 76 |
l = bfs_test.dist(t); |
| 77 | 77 |
e = bfs_test.predArc(t); |
| 78 | 78 |
s = bfs_test.predNode(t); |
| 79 | 79 |
b = bfs_test.reached(t); |
| 80 | 80 |
d = bfs_test.distMap(); |
| 81 | 81 |
p = bfs_test.predMap(); |
| 82 | 82 |
pp = bfs_test.path(t); |
| 83 | 83 |
} |
| 84 | 84 |
{
|
| 85 | 85 |
BType |
| 86 | 86 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
| 87 | 87 |
::SetDistMap<concepts::ReadWriteMap<Node,int> > |
| 88 | 88 |
::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
| 89 | 89 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
| 90 | 90 |
::SetStandardProcessedMap |
| 91 | 91 |
::Create bfs_test(G); |
| 92 | 92 |
|
| 93 | 93 |
bfs_test.run(s); |
| 94 | 94 |
bfs_test.run(s,t); |
| 95 | 95 |
bfs_test.run(); |
| 96 | 96 |
|
| 97 | 97 |
l = bfs_test.dist(t); |
| 98 | 98 |
e = bfs_test.predArc(t); |
| 99 | 99 |
s = bfs_test.predNode(t); |
| 100 | 100 |
b = bfs_test.reached(t); |
| 101 | 101 |
pp = bfs_test.path(t); |
| 102 | 102 |
} |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 | 105 |
void checkBfsFunctionCompile() |
| 106 | 106 |
{
|
| 107 | 107 |
typedef int VType; |
| 108 | 108 |
typedef concepts::Digraph Digraph; |
| 109 | 109 |
typedef Digraph::Arc Arc; |
| 110 | 110 |
typedef Digraph::Node Node; |
| 111 | 111 |
|
| 112 | 112 |
Digraph g; |
| 113 | 113 |
bool b; |
| 114 | 114 |
bfs(g).run(Node()); |
| 115 | 115 |
b=bfs(g).run(Node(),Node()); |
| 116 | 116 |
bfs(g).run(); |
| 117 | 117 |
bfs(g) |
| 118 | 118 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 119 | 119 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 120 | 120 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 121 | 121 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 122 | 122 |
.run(Node()); |
| 123 | 123 |
b=bfs(g) |
| 124 | 124 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 125 | 125 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 126 | 126 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 127 | 127 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 128 | 128 |
.path(concepts::Path<Digraph>()) |
| 129 | 129 |
.dist(VType()) |
| 130 | 130 |
.run(Node(),Node()); |
| 131 | 131 |
bfs(g) |
| 132 | 132 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 133 | 133 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 134 | 134 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 135 | 135 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 136 | 136 |
.run(); |
| 137 | 137 |
} |
| 138 | 138 |
|
| 139 | 139 |
template <class Digraph> |
| 140 | 140 |
void checkBfs() {
|
| 141 | 141 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 142 | 142 |
|
| 143 | 143 |
Digraph G; |
| 144 | 144 |
Node s, t; |
| 145 | 145 |
|
| 146 | 146 |
std::istringstream input(test_lgf); |
| 147 | 147 |
digraphReader(G, input). |
| 148 | 148 |
node("source", s).
|
| 149 | 149 |
node("target", t).
|
| 150 | 150 |
run(); |
| 151 | 151 |
|
| 152 | 152 |
Bfs<Digraph> bfs_test(G); |
| 153 | 153 |
bfs_test.run(s); |
| 154 | 154 |
|
| 155 | 155 |
check(bfs_test.dist(t)==2,"Bfs found a wrong path."); |
| 156 | 156 |
|
| 157 | 157 |
Path<Digraph> p = bfs_test.path(t); |
| 158 | 158 |
check(p.length()==2,"path() found a wrong path."); |
| 159 | 159 |
check(checkPath(G, p),"path() found a wrong path."); |
| 160 | 160 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
| 161 | 161 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
| 162 | 162 |
|
| 163 | 163 |
|
| 164 | 164 |
for(ArcIt a(G); a!=INVALID; ++a) {
|
| 165 | 165 |
Node u=G.source(a); |
| 166 | 166 |
Node v=G.target(a); |
| 167 | 167 |
check( !bfs_test.reached(u) || |
| 168 | 168 |
(bfs_test.dist(v) <= bfs_test.dist(u)+1), |
| 169 | 169 |
"Wrong output. " << G.id(u) << "->" << G.id(v)); |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
for(NodeIt v(G); v!=INVALID; ++v) {
|
| 173 | 173 |
if (bfs_test.reached(v)) {
|
| 174 | 174 |
check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree."); |
| 175 | 175 |
if (bfs_test.predArc(v)!=INVALID ) {
|
| 176 | 176 |
Arc a=bfs_test.predArc(v); |
| 177 | 177 |
Node u=G.source(a); |
| 178 | 178 |
check(u==bfs_test.predNode(v),"Wrong tree."); |
| 179 | 179 |
check(bfs_test.dist(v) - bfs_test.dist(u) == 1, |
| 180 | 180 |
"Wrong distance. Difference: " |
| 181 | 181 |
<< std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1)); |
| 182 | 182 |
} |
| 183 | 183 |
} |
| 184 | 184 |
} |
| 185 | 185 |
|
| 186 | 186 |
{
|
| 187 | 187 |
NullMap<Node,Arc> myPredMap; |
| 188 | 188 |
bfs(G).predMap(myPredMap).run(s); |
| 189 | 189 |
} |
| 190 | 190 |
} |
| 191 | 191 |
|
| 192 | 192 |
int main() |
| 193 | 193 |
{
|
| 194 | 194 |
checkBfs<ListDigraph>(); |
| 195 | 195 |
checkBfs<SmartDigraph>(); |
| 196 | 196 |
return 0; |
| 197 | 197 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <iostream> |
| 20 | 20 |
|
| 21 | 21 |
#include "test_tools.h" |
| 22 | 22 |
#include <lemon/list_graph.h> |
| 23 | 23 |
#include <lemon/circulation.h> |
| 24 | 24 |
#include <lemon/lgf_reader.h> |
| 25 | 25 |
#include <lemon/concepts/digraph.h> |
| 26 | 26 |
#include <lemon/concepts/maps.h> |
| 27 | 27 |
|
| 28 | 28 |
using namespace lemon; |
| 29 | 29 |
|
| 30 | 30 |
char test_lgf[] = |
| 31 | 31 |
"@nodes\n" |
| 32 | 32 |
"label\n" |
| 33 | 33 |
"0\n" |
| 34 | 34 |
"1\n" |
| 35 | 35 |
"2\n" |
| 36 | 36 |
"3\n" |
| 37 | 37 |
"4\n" |
| 38 | 38 |
"5\n" |
| 39 | 39 |
"@arcs\n" |
| 40 | 40 |
" lcap ucap\n" |
| 41 | 41 |
"0 1 2 10\n" |
| 42 | 42 |
"0 2 2 6\n" |
| 43 | 43 |
"1 3 4 7\n" |
| 44 | 44 |
"1 4 0 5\n" |
| 45 | 45 |
"2 4 1 3\n" |
| 46 | 46 |
"3 5 3 8\n" |
| 47 | 47 |
"4 5 3 7\n" |
| 48 | 48 |
"@attributes\n" |
| 49 | 49 |
"source 0\n" |
| 50 | 50 |
"sink 5\n"; |
| 51 | 51 |
|
| 52 | 52 |
void checkCirculationCompile() |
| 53 | 53 |
{
|
| 54 | 54 |
typedef int VType; |
| 55 | 55 |
typedef concepts::Digraph Digraph; |
| 56 | 56 |
|
| 57 | 57 |
typedef Digraph::Node Node; |
| 58 | 58 |
typedef Digraph::Arc Arc; |
| 59 | 59 |
typedef concepts::ReadMap<Arc,VType> CapMap; |
| 60 | 60 |
typedef concepts::ReadMap<Node,VType> DeltaMap; |
| 61 | 61 |
typedef concepts::ReadWriteMap<Arc,VType> FlowMap; |
| 62 | 62 |
typedef concepts::WriteMap<Node,bool> BarrierMap; |
| 63 | 63 |
|
| 64 | 64 |
typedef Elevator<Digraph, Digraph::Node> Elev; |
| 65 | 65 |
typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev; |
| 66 | 66 |
|
| 67 | 67 |
Digraph g; |
| 68 | 68 |
Node n; |
| 69 | 69 |
Arc a; |
| 70 | 70 |
CapMap lcap, ucap; |
| 71 | 71 |
DeltaMap delta; |
| 72 | 72 |
FlowMap flow; |
| 73 | 73 |
BarrierMap bar; |
| 74 | 74 |
|
| 75 | 75 |
Circulation<Digraph, CapMap, CapMap, DeltaMap> |
| 76 | 76 |
::SetFlowMap<FlowMap> |
| 77 | 77 |
::SetElevator<Elev> |
| 78 | 78 |
::SetStandardElevator<LinkedElev> |
| 79 | 79 |
::Create circ_test(g,lcap,ucap,delta); |
| 80 | 80 |
|
| 81 | 81 |
circ_test.lowerCapMap(lcap); |
| 82 | 82 |
circ_test.upperCapMap(ucap); |
| 83 | 83 |
circ_test.deltaMap(delta); |
| 84 | 84 |
flow = circ_test.flowMap(); |
| 85 | 85 |
circ_test.flowMap(flow); |
| 86 | 86 |
|
| 87 | 87 |
circ_test.init(); |
| 88 | 88 |
circ_test.greedyInit(); |
| 89 | 89 |
circ_test.start(); |
| 90 | 90 |
circ_test.run(); |
| 91 | 91 |
|
| 92 | 92 |
circ_test.barrier(n); |
| 93 | 93 |
circ_test.barrierMap(bar); |
| 94 | 94 |
circ_test.flow(a); |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 | 97 |
template <class G, class LM, class UM, class DM> |
| 98 | 98 |
void checkCirculation(const G& g, const LM& lm, const UM& um, |
| 99 | 99 |
const DM& dm, bool find) |
| 100 | 100 |
{
|
| 101 | 101 |
Circulation<G, LM, UM, DM> circ(g, lm, um, dm); |
| 102 | 102 |
bool ret = circ.run(); |
| 103 | 103 |
if (find) {
|
| 104 | 104 |
check(ret, "A feasible solution should have been found."); |
| 105 | 105 |
check(circ.checkFlow(), "The found flow is corrupt."); |
| 106 | 106 |
check(!circ.checkBarrier(), "A barrier should not have been found."); |
| 107 | 107 |
} else {
|
| 108 | 108 |
check(!ret, "A feasible solution should not have been found."); |
| 109 | 109 |
check(circ.checkBarrier(), "The found barrier is corrupt."); |
| 110 | 110 |
} |
| 111 | 111 |
} |
| 112 | 112 |
|
| 113 | 113 |
int main (int, char*[]) |
| 114 | 114 |
{
|
| 115 | 115 |
typedef ListDigraph Digraph; |
| 116 | 116 |
DIGRAPH_TYPEDEFS(Digraph); |
| 117 | 117 |
|
| 118 | 118 |
Digraph g; |
| 119 | 119 |
IntArcMap lo(g), up(g); |
| 120 | 120 |
IntNodeMap delta(g, 0); |
| 121 | 121 |
Node s, t; |
| 122 | 122 |
|
| 123 | 123 |
std::istringstream input(test_lgf); |
| 124 | 124 |
DigraphReader<Digraph>(g,input). |
| 125 | 125 |
arcMap("lcap", lo).
|
| 126 | 126 |
arcMap("ucap", up).
|
| 127 | 127 |
node("source",s).
|
| 128 | 128 |
node("sink",t).
|
| 129 | 129 |
run(); |
| 130 | 130 |
|
| 131 | 131 |
delta[s] = 7; delta[t] = -7; |
| 132 | 132 |
checkCirculation(g, lo, up, delta, true); |
| 133 | 133 |
|
| 134 | 134 |
delta[s] = 13; delta[t] = -13; |
| 135 | 135 |
checkCirculation(g, lo, up, delta, true); |
| 136 | 136 |
|
| 137 | 137 |
delta[s] = 6; delta[t] = -6; |
| 138 | 138 |
checkCirculation(g, lo, up, delta, false); |
| 139 | 139 |
|
| 140 | 140 |
delta[s] = 14; delta[t] = -14; |
| 141 | 141 |
checkCirculation(g, lo, up, delta, false); |
| 142 | 142 |
|
| 143 | 143 |
delta[s] = 7; delta[t] = -13; |
| 144 | 144 |
checkCirculation(g, lo, up, delta, true); |
| 145 | 145 |
|
| 146 | 146 |
delta[s] = 5; delta[t] = -15; |
| 147 | 147 |
checkCirculation(g, lo, up, delta, true); |
| 148 | 148 |
|
| 149 | 149 |
delta[s] = 10; delta[t] = -11; |
| 150 | 150 |
checkCirculation(g, lo, up, delta, true); |
| 151 | 151 |
|
| 152 | 152 |
delta[s] = 11; delta[t] = -10; |
| 153 | 153 |
checkCirculation(g, lo, up, delta, false); |
| 154 | 154 |
|
| 155 | 155 |
return 0; |
| 156 | 156 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/counter.h> |
| 20 | 20 |
#include <vector> |
| 21 | 21 |
|
| 22 | 22 |
using namespace lemon; |
| 23 | 23 |
|
| 24 | 24 |
template <typename T> |
| 25 | 25 |
void bubbleSort(std::vector<T>& v) {
|
| 26 | 26 |
Counter op("Bubble Sort - Operations: ");
|
| 27 | 27 |
Counter::NoSubCounter as(op, "Assignments: "); |
| 28 | 28 |
Counter::NoSubCounter co(op, "Comparisons: "); |
| 29 | 29 |
for (int i = v.size()-1; i > 0; --i) {
|
| 30 | 30 |
for (int j = 0; j < i; ++j) {
|
| 31 | 31 |
if (v[j] > v[j+1]) {
|
| 32 | 32 |
T tmp = v[j]; |
| 33 | 33 |
v[j] = v[j+1]; |
| 34 | 34 |
v[j+1] = tmp; |
| 35 | 35 |
as += 3; |
| 36 | 36 |
} |
| 37 | 37 |
++co; |
| 38 | 38 |
} |
| 39 | 39 |
} |
| 40 | 40 |
} |
| 41 | 41 |
|
| 42 | 42 |
template <typename T> |
| 43 | 43 |
void insertionSort(std::vector<T>& v) {
|
| 44 | 44 |
Counter op("Insertion Sort - Operations: ");
|
| 45 | 45 |
Counter::NoSubCounter as(op, "Assignments: "); |
| 46 | 46 |
Counter::NoSubCounter co(op, "Comparisons: "); |
| 47 | 47 |
for (int i = 1; i < int(v.size()); ++i) {
|
| 48 | 48 |
T value = v[i]; |
| 49 | 49 |
++as; |
| 50 | 50 |
int j = i; |
| 51 | 51 |
while (j > 0 && v[j-1] > value) {
|
| 52 | 52 |
v[j] = v[j-1]; |
| 53 | 53 |
--j; |
| 54 | 54 |
++co; ++as; |
| 55 | 55 |
} |
| 56 | 56 |
v[j] = value; |
| 57 | 57 |
++as; |
| 58 | 58 |
} |
| 59 | 59 |
} |
| 60 | 60 |
|
| 61 | 61 |
template <typename MyCounter> |
| 62 | 62 |
void counterTest() {
|
| 63 | 63 |
MyCounter c("Main Counter: ");
|
| 64 | 64 |
c++; |
| 65 | 65 |
typename MyCounter::SubCounter d(c, "SubCounter: "); |
| 66 | 66 |
d++; |
| 67 | 67 |
typename MyCounter::SubCounter::NoSubCounter e(d, "SubSubCounter: "); |
| 68 | 68 |
e++; |
| 69 | 69 |
d+=3; |
| 70 | 70 |
c-=4; |
| 71 | 71 |
e-=2; |
| 72 | 72 |
c.reset(2); |
| 73 | 73 |
c.reset(); |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 | 76 |
void init(std::vector<int>& v) {
|
| 77 | 77 |
v[0] = 10; v[1] = 60; v[2] = 20; v[3] = 90; v[4] = 100; |
| 78 | 78 |
v[5] = 80; v[6] = 40; v[7] = 30; v[8] = 50; v[9] = 70; |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
int main() |
| 82 | 82 |
{
|
| 83 | 83 |
counterTest<Counter>(); |
| 84 | 84 |
counterTest<NoCounter>(); |
| 85 | 85 |
|
| 86 | 86 |
std::vector<int> x(10); |
| 87 | 87 |
init(x); bubbleSort(x); |
| 88 | 88 |
init(x); insertionSort(x); |
| 89 | 89 |
|
| 90 | 90 |
return 0; |
| 91 | 91 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/smart_graph.h> |
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/dfs.h> |
| 24 | 24 |
#include <lemon/path.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "graph_test.h" |
| 27 | 27 |
#include "test_tools.h" |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| 30 | 30 |
|
| 31 | 31 |
char test_lgf[] = |
| 32 | 32 |
"@nodes\n" |
| 33 | 33 |
"label\n" |
| 34 | 34 |
"0\n" |
| 35 | 35 |
"1\n" |
| 36 | 36 |
"2\n" |
| 37 | 37 |
"3\n" |
| 38 | 38 |
"4\n" |
| 39 | 39 |
"5\n" |
| 40 | 40 |
"6\n" |
| 41 | 41 |
"@arcs\n" |
| 42 | 42 |
" label\n" |
| 43 | 43 |
"0 1 0\n" |
| 44 | 44 |
"1 2 1\n" |
| 45 | 45 |
"2 3 2\n" |
| 46 | 46 |
"1 4 3\n" |
| 47 | 47 |
"4 2 4\n" |
| 48 | 48 |
"4 5 5\n" |
| 49 | 49 |
"5 0 6\n" |
| 50 | 50 |
"6 3 7\n" |
| 51 | 51 |
"@attributes\n" |
| 52 | 52 |
"source 0\n" |
| 53 | 53 |
"target 5\n"; |
| 54 | 54 |
|
| 55 | 55 |
void checkDfsCompile() |
| 56 | 56 |
{
|
| 57 | 57 |
typedef concepts::Digraph Digraph; |
| 58 | 58 |
typedef Dfs<Digraph> DType; |
| 59 | 59 |
typedef Digraph::Node Node; |
| 60 | 60 |
typedef Digraph::Arc Arc; |
| 61 | 61 |
|
| 62 | 62 |
Digraph G; |
| 63 | 63 |
Node s, t; |
| 64 | 64 |
Arc e; |
| 65 | 65 |
int l; |
| 66 | 66 |
bool b; |
| 67 | 67 |
DType::DistMap d(G); |
| 68 | 68 |
DType::PredMap p(G); |
| 69 | 69 |
Path<Digraph> pp; |
| 70 | 70 |
|
| 71 | 71 |
{
|
| 72 | 72 |
DType dfs_test(G); |
| 73 | 73 |
|
| 74 | 74 |
dfs_test.run(s); |
| 75 | 75 |
dfs_test.run(s,t); |
| 76 | 76 |
dfs_test.run(); |
| 77 | 77 |
|
| 78 | 78 |
l = dfs_test.dist(t); |
| 79 | 79 |
e = dfs_test.predArc(t); |
| 80 | 80 |
s = dfs_test.predNode(t); |
| 81 | 81 |
b = dfs_test.reached(t); |
| 82 | 82 |
d = dfs_test.distMap(); |
| 83 | 83 |
p = dfs_test.predMap(); |
| 84 | 84 |
pp = dfs_test.path(t); |
| 85 | 85 |
} |
| 86 | 86 |
{
|
| 87 | 87 |
DType |
| 88 | 88 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
| 89 | 89 |
::SetDistMap<concepts::ReadWriteMap<Node,int> > |
| 90 | 90 |
::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
| 91 | 91 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
| 92 | 92 |
::SetStandardProcessedMap |
| 93 | 93 |
::Create dfs_test(G); |
| 94 | 94 |
|
| 95 | 95 |
dfs_test.run(s); |
| 96 | 96 |
dfs_test.run(s,t); |
| 97 | 97 |
dfs_test.run(); |
| 98 | 98 |
|
| 99 | 99 |
l = dfs_test.dist(t); |
| 100 | 100 |
e = dfs_test.predArc(t); |
| 101 | 101 |
s = dfs_test.predNode(t); |
| 102 | 102 |
b = dfs_test.reached(t); |
| 103 | 103 |
pp = dfs_test.path(t); |
| 104 | 104 |
} |
| 105 | 105 |
} |
| 106 | 106 |
|
| 107 | 107 |
void checkDfsFunctionCompile() |
| 108 | 108 |
{
|
| 109 | 109 |
typedef int VType; |
| 110 | 110 |
typedef concepts::Digraph Digraph; |
| 111 | 111 |
typedef Digraph::Arc Arc; |
| 112 | 112 |
typedef Digraph::Node Node; |
| 113 | 113 |
|
| 114 | 114 |
Digraph g; |
| 115 | 115 |
bool b; |
| 116 | 116 |
dfs(g).run(Node()); |
| 117 | 117 |
b=dfs(g).run(Node(),Node()); |
| 118 | 118 |
dfs(g).run(); |
| 119 | 119 |
dfs(g) |
| 120 | 120 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 121 | 121 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 122 | 122 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 123 | 123 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 124 | 124 |
.run(Node()); |
| 125 | 125 |
b=dfs(g) |
| 126 | 126 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 127 | 127 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 128 | 128 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 129 | 129 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 130 | 130 |
.path(concepts::Path<Digraph>()) |
| 131 | 131 |
.dist(VType()) |
| 132 | 132 |
.run(Node(),Node()); |
| 133 | 133 |
dfs(g) |
| 134 | 134 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 135 | 135 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 136 | 136 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 137 | 137 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 138 | 138 |
.run(); |
| 139 | 139 |
} |
| 140 | 140 |
|
| 141 | 141 |
template <class Digraph> |
| 142 | 142 |
void checkDfs() {
|
| 143 | 143 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 144 | 144 |
|
| 145 | 145 |
Digraph G; |
| 146 | 146 |
Node s, t; |
| 147 | 147 |
|
| 148 | 148 |
std::istringstream input(test_lgf); |
| 149 | 149 |
digraphReader(G, input). |
| 150 | 150 |
node("source", s).
|
| 151 | 151 |
node("target", t).
|
| 152 | 152 |
run(); |
| 153 | 153 |
|
| 154 | 154 |
Dfs<Digraph> dfs_test(G); |
| 155 | 155 |
dfs_test.run(s); |
| 156 | 156 |
|
| 157 | 157 |
Path<Digraph> p = dfs_test.path(t); |
| 158 | 158 |
check(p.length() == dfs_test.dist(t),"path() found a wrong path."); |
| 159 | 159 |
check(checkPath(G, p),"path() found a wrong path."); |
| 160 | 160 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
| 161 | 161 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
| 162 | 162 |
|
| 163 | 163 |
for(NodeIt v(G); v!=INVALID; ++v) {
|
| 164 | 164 |
if (dfs_test.reached(v)) {
|
| 165 | 165 |
check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree."); |
| 166 | 166 |
if (dfs_test.predArc(v)!=INVALID ) {
|
| 167 | 167 |
Arc e=dfs_test.predArc(v); |
| 168 | 168 |
Node u=G.source(e); |
| 169 | 169 |
check(u==dfs_test.predNode(v),"Wrong tree."); |
| 170 | 170 |
check(dfs_test.dist(v) - dfs_test.dist(u) == 1, |
| 171 | 171 |
"Wrong distance. (" << dfs_test.dist(u) << "->"
|
| 172 | 172 |
<< dfs_test.dist(v) << ")"); |
| 173 | 173 |
} |
| 174 | 174 |
} |
| 175 | 175 |
} |
| 176 | 176 |
|
| 177 | 177 |
{
|
| 178 | 178 |
NullMap<Node,Arc> myPredMap; |
| 179 | 179 |
dfs(G).predMap(myPredMap).run(s); |
| 180 | 180 |
} |
| 181 | 181 |
} |
| 182 | 182 |
|
| 183 | 183 |
int main() |
| 184 | 184 |
{
|
| 185 | 185 |
checkDfs<ListDigraph>(); |
| 186 | 186 |
checkDfs<SmartDigraph>(); |
| 187 | 187 |
return 0; |
| 188 | 188 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/list_graph.h> |
| 21 | 21 |
#include <lemon/smart_graph.h> |
| 22 | 22 |
#include <lemon/full_graph.h> |
| 23 | 23 |
|
| 24 | 24 |
#include "test_tools.h" |
| 25 | 25 |
#include "graph_test.h" |
| 26 | 26 |
|
| 27 | 27 |
using namespace lemon; |
| 28 | 28 |
using namespace lemon::concepts; |
| 29 | 29 |
|
| 30 | 30 |
template <class Digraph> |
| 31 | 31 |
void checkDigraphBuild() {
|
| 32 | 32 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 33 | 33 |
Digraph G; |
| 34 | 34 |
|
| 35 | 35 |
checkGraphNodeList(G, 0); |
| 36 | 36 |
checkGraphArcList(G, 0); |
| 37 | 37 |
|
| 38 | 38 |
Node |
| 39 | 39 |
n1 = G.addNode(), |
| 40 | 40 |
n2 = G.addNode(), |
| 41 | 41 |
n3 = G.addNode(); |
| 42 | 42 |
checkGraphNodeList(G, 3); |
| 43 | 43 |
checkGraphArcList(G, 0); |
| 44 | 44 |
|
| 45 | 45 |
Arc a1 = G.addArc(n1, n2); |
| 46 | 46 |
check(G.source(a1) == n1 && G.target(a1) == n2, "Wrong arc"); |
| 47 | 47 |
checkGraphNodeList(G, 3); |
| 48 | 48 |
checkGraphArcList(G, 1); |
| 49 | 49 |
|
| 50 | 50 |
checkGraphOutArcList(G, n1, 1); |
| 51 | 51 |
checkGraphOutArcList(G, n2, 0); |
| 52 | 52 |
checkGraphOutArcList(G, n3, 0); |
| 53 | 53 |
|
| 54 | 54 |
checkGraphInArcList(G, n1, 0); |
| 55 | 55 |
checkGraphInArcList(G, n2, 1); |
| 56 | 56 |
checkGraphInArcList(G, n3, 0); |
| 57 | 57 |
|
| 58 | 58 |
checkGraphConArcList(G, 1); |
| 59 | 59 |
|
| 60 | 60 |
Arc a2 = G.addArc(n2, n1), |
| 61 | 61 |
a3 = G.addArc(n2, n3), |
| 62 | 62 |
a4 = G.addArc(n2, n3); |
| 63 | 63 |
|
| 64 | 64 |
checkGraphNodeList(G, 3); |
| 65 | 65 |
checkGraphArcList(G, 4); |
| 66 | 66 |
|
| 67 | 67 |
checkGraphOutArcList(G, n1, 1); |
| 68 | 68 |
checkGraphOutArcList(G, n2, 3); |
| 69 | 69 |
checkGraphOutArcList(G, n3, 0); |
| 70 | 70 |
|
| 71 | 71 |
checkGraphInArcList(G, n1, 1); |
| 72 | 72 |
checkGraphInArcList(G, n2, 1); |
| 73 | 73 |
checkGraphInArcList(G, n3, 2); |
| 74 | 74 |
|
| 75 | 75 |
checkGraphConArcList(G, 4); |
| 76 | 76 |
|
| 77 | 77 |
checkNodeIds(G); |
| 78 | 78 |
checkArcIds(G); |
| 79 | 79 |
checkGraphNodeMap(G); |
| 80 | 80 |
checkGraphArcMap(G); |
| 81 | 81 |
} |
| 82 | 82 |
|
| 83 | 83 |
template <class Digraph> |
| 84 | 84 |
void checkDigraphSplit() {
|
| 85 | 85 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 86 | 86 |
|
| 87 | 87 |
Digraph G; |
| 88 | 88 |
Node n1 = G.addNode(), n2 = G.addNode(), n3 = G.addNode(); |
| 89 | 89 |
Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n2, n1), |
| 90 | 90 |
a3 = G.addArc(n2, n3), a4 = G.addArc(n2, n3); |
| 91 | 91 |
|
| 92 | 92 |
Node n4 = G.split(n2); |
| 93 | 93 |
|
| 94 | 94 |
check(G.target(OutArcIt(G, n2)) == n4 && |
| 95 | 95 |
G.source(InArcIt(G, n4)) == n2, |
| 96 | 96 |
"Wrong split."); |
| 97 | 97 |
|
| 98 | 98 |
checkGraphNodeList(G, 4); |
| 99 | 99 |
checkGraphArcList(G, 5); |
| 100 | 100 |
|
| 101 | 101 |
checkGraphOutArcList(G, n1, 1); |
| 102 | 102 |
checkGraphOutArcList(G, n2, 1); |
| 103 | 103 |
checkGraphOutArcList(G, n3, 0); |
| 104 | 104 |
checkGraphOutArcList(G, n4, 3); |
| 105 | 105 |
|
| 106 | 106 |
checkGraphInArcList(G, n1, 1); |
| 107 | 107 |
checkGraphInArcList(G, n2, 1); |
| 108 | 108 |
checkGraphInArcList(G, n3, 2); |
| 109 | 109 |
checkGraphInArcList(G, n4, 1); |
| 110 | 110 |
|
| 111 | 111 |
checkGraphConArcList(G, 5); |
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 | 114 |
template <class Digraph> |
| 115 | 115 |
void checkDigraphAlter() {
|
| 116 | 116 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 117 | 117 |
|
| 118 | 118 |
Digraph G; |
| 119 | 119 |
Node n1 = G.addNode(), n2 = G.addNode(), |
| 120 | 120 |
n3 = G.addNode(), n4 = G.addNode(); |
| 121 | 121 |
Arc a1 = G.addArc(n1, n2), a2 = G.addArc(n4, n1), |
| 122 | 122 |
a3 = G.addArc(n4, n3), a4 = G.addArc(n4, n3), |
| 123 | 123 |
a5 = G.addArc(n2, n4); |
| 124 | 124 |
|
| 125 | 125 |
checkGraphNodeList(G, 4); |
| 126 | 126 |
checkGraphArcList(G, 5); |
| 127 | 127 |
|
| 128 | 128 |
// Check changeSource() and changeTarget() |
| 129 | 129 |
G.changeTarget(a4, n1); |
| 130 | 130 |
|
| 131 | 131 |
checkGraphNodeList(G, 4); |
| 132 | 132 |
checkGraphArcList(G, 5); |
| 133 | 133 |
|
| 134 | 134 |
checkGraphOutArcList(G, n1, 1); |
| 135 | 135 |
checkGraphOutArcList(G, n2, 1); |
| 136 | 136 |
checkGraphOutArcList(G, n3, 0); |
| 137 | 137 |
checkGraphOutArcList(G, n4, 3); |
| 138 | 138 |
|
| 139 | 139 |
checkGraphInArcList(G, n1, 2); |
| 140 | 140 |
checkGraphInArcList(G, n2, 1); |
| 141 | 141 |
checkGraphInArcList(G, n3, 1); |
| 142 | 142 |
checkGraphInArcList(G, n4, 1); |
| 143 | 143 |
|
| 144 | 144 |
checkGraphConArcList(G, 5); |
| 145 | 145 |
|
| 146 | 146 |
G.changeSource(a4, n3); |
| 147 | 147 |
|
| 148 | 148 |
checkGraphNodeList(G, 4); |
| 149 | 149 |
checkGraphArcList(G, 5); |
| 150 | 150 |
|
| 151 | 151 |
checkGraphOutArcList(G, n1, 1); |
| 152 | 152 |
checkGraphOutArcList(G, n2, 1); |
| 153 | 153 |
checkGraphOutArcList(G, n3, 1); |
| 154 | 154 |
checkGraphOutArcList(G, n4, 2); |
| 155 | 155 |
|
| 156 | 156 |
checkGraphInArcList(G, n1, 2); |
| 157 | 157 |
checkGraphInArcList(G, n2, 1); |
| 158 | 158 |
checkGraphInArcList(G, n3, 1); |
| 159 | 159 |
checkGraphInArcList(G, n4, 1); |
| 160 | 160 |
|
| 161 | 161 |
checkGraphConArcList(G, 5); |
| 162 | 162 |
|
| 163 | 163 |
// Check contract() |
| 164 | 164 |
G.contract(n2, n4, false); |
| 165 | 165 |
|
| 166 | 166 |
checkGraphNodeList(G, 3); |
| 167 | 167 |
checkGraphArcList(G, 5); |
| 168 | 168 |
|
| 169 | 169 |
checkGraphOutArcList(G, n1, 1); |
| 170 | 170 |
checkGraphOutArcList(G, n2, 3); |
| 171 | 171 |
checkGraphOutArcList(G, n3, 1); |
| 172 | 172 |
|
| 173 | 173 |
checkGraphInArcList(G, n1, 2); |
| 174 | 174 |
checkGraphInArcList(G, n2, 2); |
| 175 | 175 |
checkGraphInArcList(G, n3, 1); |
| 176 | 176 |
|
| 177 | 177 |
checkGraphConArcList(G, 5); |
| 178 | 178 |
|
| 179 | 179 |
G.contract(n2, n1); |
| 180 | 180 |
|
| 181 | 181 |
checkGraphNodeList(G, 2); |
| 182 | 182 |
checkGraphArcList(G, 3); |
| 183 | 183 |
|
| 184 | 184 |
checkGraphOutArcList(G, n2, 2); |
| 185 | 185 |
checkGraphOutArcList(G, n3, 1); |
| 186 | 186 |
|
| 187 | 187 |
checkGraphInArcList(G, n2, 2); |
| 188 | 188 |
checkGraphInArcList(G, n3, 1); |
| 189 | 189 |
|
| 190 | 190 |
checkGraphConArcList(G, 3); |
| 191 | 191 |
} |
| 192 | 192 |
|
| 193 | 193 |
template <class Digraph> |
| 194 | 194 |
void checkDigraphErase() {
|
| 195 | 195 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 196 | 196 |
|
| 197 | 197 |
Digraph G; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/smart_graph.h> |
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/dijkstra.h> |
| 24 | 24 |
#include <lemon/path.h> |
| 25 | 25 |
#include <lemon/bin_heap.h> |
| 26 | 26 |
|
| 27 | 27 |
#include "graph_test.h" |
| 28 | 28 |
#include "test_tools.h" |
| 29 | 29 |
|
| 30 | 30 |
using namespace lemon; |
| 31 | 31 |
|
| 32 | 32 |
char test_lgf[] = |
| 33 | 33 |
"@nodes\n" |
| 34 | 34 |
"label\n" |
| 35 | 35 |
"0\n" |
| 36 | 36 |
"1\n" |
| 37 | 37 |
"2\n" |
| 38 | 38 |
"3\n" |
| 39 | 39 |
"4\n" |
| 40 | 40 |
"@arcs\n" |
| 41 | 41 |
" label length\n" |
| 42 | 42 |
"0 1 0 1\n" |
| 43 | 43 |
"1 2 1 1\n" |
| 44 | 44 |
"2 3 2 1\n" |
| 45 | 45 |
"0 3 4 5\n" |
| 46 | 46 |
"0 3 5 10\n" |
| 47 | 47 |
"0 3 6 7\n" |
| 48 | 48 |
"4 2 7 1\n" |
| 49 | 49 |
"@attributes\n" |
| 50 | 50 |
"source 0\n" |
| 51 | 51 |
"target 3\n"; |
| 52 | 52 |
|
| 53 | 53 |
void checkDijkstraCompile() |
| 54 | 54 |
{
|
| 55 | 55 |
typedef int VType; |
| 56 | 56 |
typedef concepts::Digraph Digraph; |
| 57 | 57 |
typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap; |
| 58 | 58 |
typedef Dijkstra<Digraph, LengthMap> DType; |
| 59 | 59 |
typedef Digraph::Node Node; |
| 60 | 60 |
typedef Digraph::Arc Arc; |
| 61 | 61 |
|
| 62 | 62 |
Digraph G; |
| 63 | 63 |
Node s, t; |
| 64 | 64 |
Arc e; |
| 65 | 65 |
VType l; |
| 66 | 66 |
bool b; |
| 67 | 67 |
DType::DistMap d(G); |
| 68 | 68 |
DType::PredMap p(G); |
| 69 | 69 |
LengthMap length; |
| 70 | 70 |
Path<Digraph> pp; |
| 71 | 71 |
|
| 72 | 72 |
{
|
| 73 | 73 |
DType dijkstra_test(G,length); |
| 74 | 74 |
|
| 75 | 75 |
dijkstra_test.run(s); |
| 76 | 76 |
dijkstra_test.run(s,t); |
| 77 | 77 |
|
| 78 | 78 |
l = dijkstra_test.dist(t); |
| 79 | 79 |
e = dijkstra_test.predArc(t); |
| 80 | 80 |
s = dijkstra_test.predNode(t); |
| 81 | 81 |
b = dijkstra_test.reached(t); |
| 82 | 82 |
d = dijkstra_test.distMap(); |
| 83 | 83 |
p = dijkstra_test.predMap(); |
| 84 | 84 |
pp = dijkstra_test.path(t); |
| 85 | 85 |
} |
| 86 | 86 |
{
|
| 87 | 87 |
DType |
| 88 | 88 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
| 89 | 89 |
::SetDistMap<concepts::ReadWriteMap<Node,VType> > |
| 90 | 90 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
| 91 | 91 |
::SetStandardProcessedMap |
| 92 | 92 |
::SetOperationTraits<DijkstraDefaultOperationTraits<VType> > |
| 93 | 93 |
::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > > |
| 94 | 94 |
::SetStandardHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > > |
| 95 | 95 |
::Create dijkstra_test(G,length); |
| 96 | 96 |
|
| 97 | 97 |
dijkstra_test.run(s); |
| 98 | 98 |
dijkstra_test.run(s,t); |
| 99 | 99 |
|
| 100 | 100 |
l = dijkstra_test.dist(t); |
| 101 | 101 |
e = dijkstra_test.predArc(t); |
| 102 | 102 |
s = dijkstra_test.predNode(t); |
| 103 | 103 |
b = dijkstra_test.reached(t); |
| 104 | 104 |
pp = dijkstra_test.path(t); |
| 105 | 105 |
} |
| 106 | 106 |
|
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 | 109 |
void checkDijkstraFunctionCompile() |
| 110 | 110 |
{
|
| 111 | 111 |
typedef int VType; |
| 112 | 112 |
typedef concepts::Digraph Digraph; |
| 113 | 113 |
typedef Digraph::Arc Arc; |
| 114 | 114 |
typedef Digraph::Node Node; |
| 115 | 115 |
typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap; |
| 116 | 116 |
|
| 117 | 117 |
Digraph g; |
| 118 | 118 |
bool b; |
| 119 | 119 |
dijkstra(g,LengthMap()).run(Node()); |
| 120 | 120 |
b=dijkstra(g,LengthMap()).run(Node(),Node()); |
| 121 | 121 |
dijkstra(g,LengthMap()) |
| 122 | 122 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 123 | 123 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 124 | 124 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 125 | 125 |
.run(Node()); |
| 126 | 126 |
b=dijkstra(g,LengthMap()) |
| 127 | 127 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 128 | 128 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 129 | 129 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 130 | 130 |
.path(concepts::Path<Digraph>()) |
| 131 | 131 |
.dist(VType()) |
| 132 | 132 |
.run(Node(),Node()); |
| 133 | 133 |
} |
| 134 | 134 |
|
| 135 | 135 |
template <class Digraph> |
| 136 | 136 |
void checkDijkstra() {
|
| 137 | 137 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 138 | 138 |
typedef typename Digraph::template ArcMap<int> LengthMap; |
| 139 | 139 |
|
| 140 | 140 |
Digraph G; |
| 141 | 141 |
Node s, t; |
| 142 | 142 |
LengthMap length(G); |
| 143 | 143 |
|
| 144 | 144 |
std::istringstream input(test_lgf); |
| 145 | 145 |
digraphReader(G, input). |
| 146 | 146 |
arcMap("length", length).
|
| 147 | 147 |
node("source", s).
|
| 148 | 148 |
node("target", t).
|
| 149 | 149 |
run(); |
| 150 | 150 |
|
| 151 | 151 |
Dijkstra<Digraph, LengthMap> |
| 152 | 152 |
dijkstra_test(G, length); |
| 153 | 153 |
dijkstra_test.run(s); |
| 154 | 154 |
|
| 155 | 155 |
check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path."); |
| 156 | 156 |
|
| 157 | 157 |
Path<Digraph> p = dijkstra_test.path(t); |
| 158 | 158 |
check(p.length()==3,"path() found a wrong path."); |
| 159 | 159 |
check(checkPath(G, p),"path() found a wrong path."); |
| 160 | 160 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
| 161 | 161 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
| 162 | 162 |
|
| 163 | 163 |
for(ArcIt e(G); e!=INVALID; ++e) {
|
| 164 | 164 |
Node u=G.source(e); |
| 165 | 165 |
Node v=G.target(e); |
| 166 | 166 |
check( !dijkstra_test.reached(u) || |
| 167 | 167 |
(dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]), |
| 168 | 168 |
"Wrong output. dist(target)-dist(source)-arc_length=" << |
| 169 | 169 |
dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]); |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
for(NodeIt v(G); v!=INVALID; ++v) {
|
| 173 | 173 |
if (dijkstra_test.reached(v)) {
|
| 174 | 174 |
check(v==s || dijkstra_test.predArc(v)!=INVALID, "Wrong tree."); |
| 175 | 175 |
if (dijkstra_test.predArc(v)!=INVALID ) {
|
| 176 | 176 |
Arc e=dijkstra_test.predArc(v); |
| 177 | 177 |
Node u=G.source(e); |
| 178 | 178 |
check(u==dijkstra_test.predNode(v),"Wrong tree."); |
| 179 | 179 |
check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e], |
| 180 | 180 |
"Wrong distance! Difference: " << |
| 181 | 181 |
std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e])); |
| 182 | 182 |
} |
| 183 | 183 |
} |
| 184 | 184 |
} |
| 185 | 185 |
|
| 186 | 186 |
{
|
| 187 | 187 |
NullMap<Node,Arc> myPredMap; |
| 188 | 188 |
dijkstra(G,length).predMap(myPredMap).run(s); |
| 189 | 189 |
} |
| 190 | 190 |
} |
| 191 | 191 |
|
| 192 | 192 |
int main() {
|
| 193 | 193 |
checkDijkstra<ListDigraph>(); |
| 194 | 194 |
checkDijkstra<SmartDigraph>(); |
| 195 | 195 |
return 0; |
| 196 | 196 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/dim2.h> |
| 20 | 20 |
#include <iostream> |
| 21 | 21 |
#include "test_tools.h" |
| 22 | 22 |
|
| 23 | 23 |
using namespace std; |
| 24 | 24 |
using namespace lemon; |
| 25 | 25 |
|
| 26 | 26 |
int main() |
| 27 | 27 |
{
|
| 28 | 28 |
typedef dim2::Point<int> Point; |
| 29 | 29 |
|
| 30 | 30 |
Point p; |
| 31 | 31 |
check(p.size()==2, "Wrong dim2::Point initialization."); |
| 32 | 32 |
|
| 33 | 33 |
Point a(1,2); |
| 34 | 34 |
Point b(3,4); |
| 35 | 35 |
check(a[0]==1 && a[1]==2, "Wrong dim2::Point initialization."); |
| 36 | 36 |
|
| 37 | 37 |
p = a+b; |
| 38 | 38 |
check(p.x==4 && p.y==6, "Wrong dim2::Point addition."); |
| 39 | 39 |
|
| 40 | 40 |
p = a-b; |
| 41 | 41 |
check(p.x==-2 && p.y==-2, "Wrong dim2::Point subtraction."); |
| 42 | 42 |
|
| 43 | 43 |
check(a.normSquare()==5,"Wrong dim2::Point norm calculation."); |
| 44 | 44 |
check(a*b==11, "Wrong dim2::Point scalar product."); |
| 45 | 45 |
|
| 46 | 46 |
int l=2; |
| 47 | 47 |
p = a*l; |
| 48 | 48 |
check(p.x==2 && p.y==4, "Wrong dim2::Point multiplication by a scalar."); |
| 49 | 49 |
|
| 50 | 50 |
p = b/l; |
| 51 | 51 |
check(p.x==1 && p.y==2, "Wrong dim2::Point division by a scalar."); |
| 52 | 52 |
|
| 53 | 53 |
typedef dim2::Box<int> Box; |
| 54 | 54 |
Box box1; |
| 55 | 55 |
check(box1.empty(), "Wrong empty() in dim2::Box."); |
| 56 | 56 |
|
| 57 | 57 |
box1.add(a); |
| 58 | 58 |
check(!box1.empty(), "Wrong empty() in dim2::Box."); |
| 59 | 59 |
box1.add(b); |
| 60 | 60 |
|
| 61 | 61 |
check(box1.left()==1 && box1.bottom()==2 && |
| 62 | 62 |
box1.right()==3 && box1.top()==4, |
| 63 | 63 |
"Wrong addition of points to dim2::Box."); |
| 64 | 64 |
|
| 65 | 65 |
check(box1.inside(Point(2,3)), "Wrong inside() in dim2::Box."); |
| 66 | 66 |
check(box1.inside(Point(1,3)), "Wrong inside() in dim2::Box."); |
| 67 | 67 |
check(!box1.inside(Point(0,3)), "Wrong inside() in dim2::Box."); |
| 68 | 68 |
|
| 69 | 69 |
Box box2(Point(2,2)); |
| 70 | 70 |
check(!box2.empty(), "Wrong empty() in dim2::Box."); |
| 71 | 71 |
|
| 72 | 72 |
box2.bottomLeft(Point(2,0)); |
| 73 | 73 |
box2.topRight(Point(5,3)); |
| 74 | 74 |
Box box3 = box1 & box2; |
| 75 | 75 |
check(!box3.empty() && |
| 76 | 76 |
box3.left()==2 && box3.bottom()==2 && |
| 77 | 77 |
box3.right()==3 && box3.top()==3, |
| 78 | 78 |
"Wrong intersection of two dim2::Box objects."); |
| 79 | 79 |
|
| 80 | 80 |
box1.add(box2); |
| 81 | 81 |
check(!box1.empty() && |
| 82 | 82 |
box1.left()==1 && box1.bottom()==0 && |
| 83 | 83 |
box1.right()==5 && box1.top()==4, |
| 84 | 84 |
"Wrong addition of two dim2::Box objects."); |
| 85 | 85 |
|
| 86 | 86 |
return 0; |
| 87 | 87 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <iostream> |
| 20 | 20 |
|
| 21 | 21 |
#include <lemon/error.h> |
| 22 | 22 |
#include "test_tools.h" |
| 23 | 23 |
|
| 24 | 24 |
using namespace lemon; |
| 25 | 25 |
|
| 26 | 26 |
#ifdef LEMON_ENABLE_ASSERTS |
| 27 | 27 |
#undef LEMON_ENABLE_ASSERTS |
| 28 | 28 |
#endif |
| 29 | 29 |
|
| 30 | 30 |
#ifdef LEMON_DISABLE_ASSERTS |
| 31 | 31 |
#undef LEMON_DISABLE_ASSERTS |
| 32 | 32 |
#endif |
| 33 | 33 |
|
| 34 | 34 |
#ifdef NDEBUG |
| 35 | 35 |
#undef NDEBUG |
| 36 | 36 |
#endif |
| 37 | 37 |
|
| 38 | 38 |
//checking disabled asserts |
| 39 | 39 |
#define LEMON_DISABLE_ASSERTS |
| 40 | 40 |
#include <lemon/assert.h> |
| 41 | 41 |
|
| 42 | 42 |
void no_assertion_text_disable() {
|
| 43 | 43 |
LEMON_ASSERT(true, "This is a fault message"); |
| 44 | 44 |
} |
| 45 | 45 |
|
| 46 | 46 |
void assertion_text_disable() {
|
| 47 | 47 |
LEMON_ASSERT(false, "This is a fault message"); |
| 48 | 48 |
} |
| 49 | 49 |
|
| 50 | 50 |
void check_assertion_disable() {
|
| 51 | 51 |
no_assertion_text_disable(); |
| 52 | 52 |
assertion_text_disable(); |
| 53 | 53 |
} |
| 54 | 54 |
#undef LEMON_DISABLE_ASSERTS |
| 55 | 55 |
|
| 56 | 56 |
//checking custom assert handler |
| 57 | 57 |
#define LEMON_ASSERT_CUSTOM |
| 58 | 58 |
|
| 59 | 59 |
static int cnt = 0; |
| 60 | 60 |
void my_assert_handler(const char*, int, const char*, |
| 61 | 61 |
const char*, const char*) {
|
| 62 | 62 |
++cnt; |
| 63 | 63 |
} |
| 64 | 64 |
|
| 65 | 65 |
#define LEMON_CUSTOM_ASSERT_HANDLER my_assert_handler |
| 66 | 66 |
#include <lemon/assert.h> |
| 67 | 67 |
|
| 68 | 68 |
void no_assertion_text_custom() {
|
| 69 | 69 |
LEMON_ASSERT(true, "This is a fault message"); |
| 70 | 70 |
} |
| 71 | 71 |
|
| 72 | 72 |
void assertion_text_custom() {
|
| 73 | 73 |
LEMON_ASSERT(false, "This is a fault message"); |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 | 76 |
void check_assertion_custom() {
|
| 77 | 77 |
no_assertion_text_custom(); |
| 78 | 78 |
assertion_text_custom(); |
| 79 | 79 |
check(cnt == 1, "The custom assert handler does not work"); |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 | 82 |
#undef LEMON_ASSERT_CUSTOM |
| 83 | 83 |
|
| 84 | 84 |
|
| 85 | 85 |
int main() {
|
| 86 | 86 |
check_assertion_disable(); |
| 87 | 87 |
check_assertion_custom(); |
| 88 | 88 |
|
| 89 | 89 |
return 0; |
| 90 | 90 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include<iostream> |
| 20 | 20 |
#include<lemon/concept_check.h> |
| 21 | 21 |
|
| 22 | 22 |
#include<lemon/list_graph.h> |
| 23 | 23 |
#include<lemon/smart_graph.h> |
| 24 | 24 |
|
| 25 | 25 |
#include<lemon/concepts/digraph.h> |
| 26 | 26 |
#include<lemon/concepts/graph.h> |
| 27 | 27 |
|
| 28 | 28 |
#include<lemon/adaptors.h> |
| 29 | 29 |
|
| 30 | 30 |
#include <limits> |
| 31 | 31 |
#include <lemon/bfs.h> |
| 32 | 32 |
#include <lemon/path.h> |
| 33 | 33 |
|
| 34 | 34 |
#include"test/test_tools.h" |
| 35 | 35 |
#include"test/graph_test.h" |
| 36 | 36 |
|
| 37 | 37 |
using namespace lemon; |
| 38 | 38 |
|
| 39 | 39 |
void checkReverseDigraph() {
|
| 40 | 40 |
checkConcept<concepts::Digraph, ReverseDigraph<concepts::Digraph> >(); |
| 41 | 41 |
|
| 42 | 42 |
typedef ListDigraph Digraph; |
| 43 | 43 |
typedef ReverseDigraph<Digraph> Adaptor; |
| 44 | 44 |
|
| 45 | 45 |
Digraph digraph; |
| 46 | 46 |
Adaptor adaptor(digraph); |
| 47 | 47 |
|
| 48 | 48 |
Digraph::Node n1 = digraph.addNode(); |
| 49 | 49 |
Digraph::Node n2 = digraph.addNode(); |
| 50 | 50 |
Digraph::Node n3 = digraph.addNode(); |
| 51 | 51 |
|
| 52 | 52 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
| 53 | 53 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
| 54 | 54 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
| 55 | 55 |
|
| 56 | 56 |
checkGraphNodeList(adaptor, 3); |
| 57 | 57 |
checkGraphArcList(adaptor, 3); |
| 58 | 58 |
checkGraphConArcList(adaptor, 3); |
| 59 | 59 |
|
| 60 | 60 |
checkGraphOutArcList(adaptor, n1, 0); |
| 61 | 61 |
checkGraphOutArcList(adaptor, n2, 1); |
| 62 | 62 |
checkGraphOutArcList(adaptor, n3, 2); |
| 63 | 63 |
|
| 64 | 64 |
checkGraphInArcList(adaptor, n1, 2); |
| 65 | 65 |
checkGraphInArcList(adaptor, n2, 1); |
| 66 | 66 |
checkGraphInArcList(adaptor, n3, 0); |
| 67 | 67 |
|
| 68 | 68 |
checkNodeIds(adaptor); |
| 69 | 69 |
checkArcIds(adaptor); |
| 70 | 70 |
|
| 71 | 71 |
checkGraphNodeMap(adaptor); |
| 72 | 72 |
checkGraphArcMap(adaptor); |
| 73 | 73 |
|
| 74 | 74 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) {
|
| 75 | 75 |
check(adaptor.source(a) == digraph.target(a), "Wrong reverse"); |
| 76 | 76 |
check(adaptor.target(a) == digraph.source(a), "Wrong reverse"); |
| 77 | 77 |
} |
| 78 | 78 |
} |
| 79 | 79 |
|
| 80 | 80 |
void checkSubDigraph() {
|
| 81 | 81 |
checkConcept<concepts::Digraph, |
| 82 | 82 |
SubDigraph<concepts::Digraph, |
| 83 | 83 |
concepts::Digraph::NodeMap<bool>, |
| 84 | 84 |
concepts::Digraph::ArcMap<bool> > >(); |
| 85 | 85 |
|
| 86 | 86 |
typedef ListDigraph Digraph; |
| 87 | 87 |
typedef Digraph::NodeMap<bool> NodeFilter; |
| 88 | 88 |
typedef Digraph::ArcMap<bool> ArcFilter; |
| 89 | 89 |
typedef SubDigraph<Digraph, NodeFilter, ArcFilter> Adaptor; |
| 90 | 90 |
|
| 91 | 91 |
Digraph digraph; |
| 92 | 92 |
NodeFilter node_filter(digraph); |
| 93 | 93 |
ArcFilter arc_filter(digraph); |
| 94 | 94 |
Adaptor adaptor(digraph, node_filter, arc_filter); |
| 95 | 95 |
|
| 96 | 96 |
Digraph::Node n1 = digraph.addNode(); |
| 97 | 97 |
Digraph::Node n2 = digraph.addNode(); |
| 98 | 98 |
Digraph::Node n3 = digraph.addNode(); |
| 99 | 99 |
|
| 100 | 100 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
| 101 | 101 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
| 102 | 102 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
| 103 | 103 |
|
| 104 | 104 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = true; |
| 105 | 105 |
arc_filter[a1] = arc_filter[a2] = arc_filter[a3] = true; |
| 106 |
|
|
| 106 |
|
|
| 107 | 107 |
checkGraphNodeList(adaptor, 3); |
| 108 | 108 |
checkGraphArcList(adaptor, 3); |
| 109 | 109 |
checkGraphConArcList(adaptor, 3); |
| 110 | 110 |
|
| 111 | 111 |
checkGraphOutArcList(adaptor, n1, 2); |
| 112 | 112 |
checkGraphOutArcList(adaptor, n2, 1); |
| 113 | 113 |
checkGraphOutArcList(adaptor, n3, 0); |
| 114 | 114 |
|
| 115 | 115 |
checkGraphInArcList(adaptor, n1, 0); |
| 116 | 116 |
checkGraphInArcList(adaptor, n2, 1); |
| 117 | 117 |
checkGraphInArcList(adaptor, n3, 2); |
| 118 | 118 |
|
| 119 | 119 |
checkNodeIds(adaptor); |
| 120 | 120 |
checkArcIds(adaptor); |
| 121 | 121 |
|
| 122 | 122 |
checkGraphNodeMap(adaptor); |
| 123 | 123 |
checkGraphArcMap(adaptor); |
| 124 | 124 |
|
| 125 | 125 |
arc_filter[a2] = false; |
| 126 | 126 |
|
| 127 | 127 |
checkGraphNodeList(adaptor, 3); |
| 128 | 128 |
checkGraphArcList(adaptor, 2); |
| 129 | 129 |
checkGraphConArcList(adaptor, 2); |
| 130 | 130 |
|
| 131 | 131 |
checkGraphOutArcList(adaptor, n1, 1); |
| 132 | 132 |
checkGraphOutArcList(adaptor, n2, 1); |
| 133 | 133 |
checkGraphOutArcList(adaptor, n3, 0); |
| 134 | 134 |
|
| 135 | 135 |
checkGraphInArcList(adaptor, n1, 0); |
| 136 | 136 |
checkGraphInArcList(adaptor, n2, 1); |
| 137 | 137 |
checkGraphInArcList(adaptor, n3, 1); |
| 138 | 138 |
|
| 139 | 139 |
checkNodeIds(adaptor); |
| 140 | 140 |
checkArcIds(adaptor); |
| 141 | 141 |
|
| 142 | 142 |
checkGraphNodeMap(adaptor); |
| 143 | 143 |
checkGraphArcMap(adaptor); |
| 144 | 144 |
|
| 145 | 145 |
node_filter[n1] = false; |
| 146 | 146 |
|
| 147 | 147 |
checkGraphNodeList(adaptor, 2); |
| 148 | 148 |
checkGraphArcList(adaptor, 1); |
| 149 | 149 |
checkGraphConArcList(adaptor, 1); |
| 150 | 150 |
|
| 151 | 151 |
checkGraphOutArcList(adaptor, n2, 1); |
| 152 | 152 |
checkGraphOutArcList(adaptor, n3, 0); |
| 153 | 153 |
|
| 154 | 154 |
checkGraphInArcList(adaptor, n2, 0); |
| 155 | 155 |
checkGraphInArcList(adaptor, n3, 1); |
| 156 | 156 |
|
| 157 | 157 |
checkNodeIds(adaptor); |
| 158 | 158 |
checkArcIds(adaptor); |
| 159 | 159 |
|
| 160 | 160 |
checkGraphNodeMap(adaptor); |
| 161 | 161 |
checkGraphArcMap(adaptor); |
| 162 | 162 |
|
| 163 | 163 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = false; |
| 164 | 164 |
arc_filter[a1] = arc_filter[a2] = arc_filter[a3] = false; |
| 165 | 165 |
|
| 166 | 166 |
checkGraphNodeList(adaptor, 0); |
| 167 | 167 |
checkGraphArcList(adaptor, 0); |
| 168 | 168 |
checkGraphConArcList(adaptor, 0); |
| 169 | 169 |
|
| 170 | 170 |
checkNodeIds(adaptor); |
| 171 | 171 |
checkArcIds(adaptor); |
| 172 | 172 |
|
| 173 | 173 |
checkGraphNodeMap(adaptor); |
| 174 | 174 |
checkGraphArcMap(adaptor); |
| 175 | 175 |
} |
| 176 | 176 |
|
| 177 | 177 |
void checkFilterNodes1() {
|
| 178 | 178 |
checkConcept<concepts::Digraph, |
| 179 | 179 |
FilterNodes<concepts::Digraph, |
| 180 | 180 |
concepts::Digraph::NodeMap<bool> > >(); |
| 181 | 181 |
|
| 182 | 182 |
typedef ListDigraph Digraph; |
| 183 | 183 |
typedef Digraph::NodeMap<bool> NodeFilter; |
| 184 | 184 |
typedef FilterNodes<Digraph, NodeFilter> Adaptor; |
| 185 | 185 |
|
| 186 | 186 |
Digraph digraph; |
| 187 | 187 |
NodeFilter node_filter(digraph); |
| 188 | 188 |
Adaptor adaptor(digraph, node_filter); |
| 189 | 189 |
|
| 190 | 190 |
Digraph::Node n1 = digraph.addNode(); |
| 191 | 191 |
Digraph::Node n2 = digraph.addNode(); |
| 192 | 192 |
Digraph::Node n3 = digraph.addNode(); |
| 193 | 193 |
|
| 194 | 194 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
| 195 | 195 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
| 196 | 196 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
| 197 | 197 |
|
| 198 | 198 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = true; |
| 199 |
|
|
| 199 |
|
|
| 200 | 200 |
checkGraphNodeList(adaptor, 3); |
| 201 | 201 |
checkGraphArcList(adaptor, 3); |
| 202 | 202 |
checkGraphConArcList(adaptor, 3); |
| 203 | 203 |
|
| 204 | 204 |
checkGraphOutArcList(adaptor, n1, 2); |
| 205 | 205 |
checkGraphOutArcList(adaptor, n2, 1); |
| 206 | 206 |
checkGraphOutArcList(adaptor, n3, 0); |
| 207 | 207 |
|
| 208 | 208 |
checkGraphInArcList(adaptor, n1, 0); |
| 209 | 209 |
checkGraphInArcList(adaptor, n2, 1); |
| 210 | 210 |
checkGraphInArcList(adaptor, n3, 2); |
| 211 | 211 |
|
| 212 | 212 |
checkNodeIds(adaptor); |
| 213 | 213 |
checkArcIds(adaptor); |
| 214 | 214 |
|
| 215 | 215 |
checkGraphNodeMap(adaptor); |
| 216 | 216 |
checkGraphArcMap(adaptor); |
| 217 | 217 |
|
| 218 | 218 |
node_filter[n1] = false; |
| 219 | 219 |
|
| 220 | 220 |
checkGraphNodeList(adaptor, 2); |
| 221 | 221 |
checkGraphArcList(adaptor, 1); |
| 222 | 222 |
checkGraphConArcList(adaptor, 1); |
| 223 | 223 |
|
| 224 | 224 |
checkGraphOutArcList(adaptor, n2, 1); |
| 225 | 225 |
checkGraphOutArcList(adaptor, n3, 0); |
| 226 | 226 |
|
| 227 | 227 |
checkGraphInArcList(adaptor, n2, 0); |
| 228 | 228 |
checkGraphInArcList(adaptor, n3, 1); |
| 229 | 229 |
|
| 230 | 230 |
checkNodeIds(adaptor); |
| 231 | 231 |
checkArcIds(adaptor); |
| 232 | 232 |
|
| 233 | 233 |
checkGraphNodeMap(adaptor); |
| 234 | 234 |
checkGraphArcMap(adaptor); |
| 235 | 235 |
|
| 236 | 236 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = false; |
| 237 | 237 |
|
| 238 | 238 |
checkGraphNodeList(adaptor, 0); |
| 239 | 239 |
checkGraphArcList(adaptor, 0); |
| 240 | 240 |
checkGraphConArcList(adaptor, 0); |
| 241 | 241 |
|
| 242 | 242 |
checkNodeIds(adaptor); |
| 243 | 243 |
checkArcIds(adaptor); |
| 244 | 244 |
|
| 245 | 245 |
checkGraphNodeMap(adaptor); |
| 246 | 246 |
checkGraphArcMap(adaptor); |
| 247 | 247 |
} |
| 248 | 248 |
|
| 249 | 249 |
void checkFilterArcs() {
|
| 250 | 250 |
checkConcept<concepts::Digraph, |
| 251 | 251 |
FilterArcs<concepts::Digraph, |
| 252 | 252 |
concepts::Digraph::ArcMap<bool> > >(); |
| 253 | 253 |
|
| 254 | 254 |
typedef ListDigraph Digraph; |
| 255 | 255 |
typedef Digraph::ArcMap<bool> ArcFilter; |
| 256 | 256 |
typedef FilterArcs<Digraph, ArcFilter> Adaptor; |
| 257 | 257 |
|
| 258 | 258 |
Digraph digraph; |
| 259 | 259 |
ArcFilter arc_filter(digraph); |
| 260 | 260 |
Adaptor adaptor(digraph, arc_filter); |
| 261 | 261 |
|
| 262 | 262 |
Digraph::Node n1 = digraph.addNode(); |
| 263 | 263 |
Digraph::Node n2 = digraph.addNode(); |
| 264 | 264 |
Digraph::Node n3 = digraph.addNode(); |
| 265 | 265 |
|
| 266 | 266 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
| 267 | 267 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
| 268 | 268 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
| 269 | 269 |
|
| 270 | 270 |
arc_filter[a1] = arc_filter[a2] = arc_filter[a3] = true; |
| 271 |
|
|
| 271 |
|
|
| 272 | 272 |
checkGraphNodeList(adaptor, 3); |
| 273 | 273 |
checkGraphArcList(adaptor, 3); |
| 274 | 274 |
checkGraphConArcList(adaptor, 3); |
| 275 | 275 |
|
| 276 | 276 |
checkGraphOutArcList(adaptor, n1, 2); |
| 277 | 277 |
checkGraphOutArcList(adaptor, n2, 1); |
| 278 | 278 |
checkGraphOutArcList(adaptor, n3, 0); |
| 279 | 279 |
|
| 280 | 280 |
checkGraphInArcList(adaptor, n1, 0); |
| 281 | 281 |
checkGraphInArcList(adaptor, n2, 1); |
| 282 | 282 |
checkGraphInArcList(adaptor, n3, 2); |
| 283 | 283 |
|
| 284 | 284 |
checkNodeIds(adaptor); |
| 285 | 285 |
checkArcIds(adaptor); |
| 286 | 286 |
|
| 287 | 287 |
checkGraphNodeMap(adaptor); |
| 288 | 288 |
checkGraphArcMap(adaptor); |
| 289 | 289 |
|
| 290 | 290 |
arc_filter[a2] = false; |
| 291 | 291 |
|
| 292 | 292 |
checkGraphNodeList(adaptor, 3); |
| 293 | 293 |
checkGraphArcList(adaptor, 2); |
| 294 | 294 |
checkGraphConArcList(adaptor, 2); |
| 295 | 295 |
|
| 296 | 296 |
checkGraphOutArcList(adaptor, n1, 1); |
| 297 | 297 |
checkGraphOutArcList(adaptor, n2, 1); |
| 298 | 298 |
checkGraphOutArcList(adaptor, n3, 0); |
| 299 | 299 |
|
| 300 | 300 |
checkGraphInArcList(adaptor, n1, 0); |
| 301 | 301 |
checkGraphInArcList(adaptor, n2, 1); |
| 302 | 302 |
checkGraphInArcList(adaptor, n3, 1); |
| 303 | 303 |
|
| 304 | 304 |
checkNodeIds(adaptor); |
| 305 | 305 |
checkArcIds(adaptor); |
| 306 | 306 |
|
| 307 | 307 |
checkGraphNodeMap(adaptor); |
| 308 | 308 |
checkGraphArcMap(adaptor); |
| 309 | 309 |
|
| 310 | 310 |
arc_filter[a1] = arc_filter[a2] = arc_filter[a3] = false; |
| 311 | 311 |
|
| 312 | 312 |
checkGraphNodeList(adaptor, 3); |
| 313 | 313 |
checkGraphArcList(adaptor, 0); |
| 314 | 314 |
checkGraphConArcList(adaptor, 0); |
| 315 | 315 |
|
| 316 | 316 |
checkNodeIds(adaptor); |
| 317 | 317 |
checkArcIds(adaptor); |
| 318 | 318 |
|
| 319 | 319 |
checkGraphNodeMap(adaptor); |
| 320 | 320 |
checkGraphArcMap(adaptor); |
| 321 | 321 |
} |
| 322 | 322 |
|
| 323 | 323 |
void checkUndirector() {
|
| 324 | 324 |
checkConcept<concepts::Graph, Undirector<concepts::Digraph> >(); |
| 325 | 325 |
|
| 326 | 326 |
typedef ListDigraph Digraph; |
| 327 | 327 |
typedef Undirector<Digraph> Adaptor; |
| 328 | 328 |
|
| 329 | 329 |
Digraph digraph; |
| 330 | 330 |
Adaptor adaptor(digraph); |
| 331 | 331 |
|
| 332 | 332 |
Digraph::Node n1 = digraph.addNode(); |
| 333 | 333 |
Digraph::Node n2 = digraph.addNode(); |
| 334 | 334 |
Digraph::Node n3 = digraph.addNode(); |
| 335 | 335 |
|
| 336 | 336 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
| 337 | 337 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
| 338 | 338 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
| 339 | 339 |
|
| 340 | 340 |
checkGraphNodeList(adaptor, 3); |
| 341 | 341 |
checkGraphArcList(adaptor, 6); |
| 342 | 342 |
checkGraphEdgeList(adaptor, 3); |
| 343 | 343 |
checkGraphConArcList(adaptor, 6); |
| 344 | 344 |
checkGraphConEdgeList(adaptor, 3); |
| 345 | 345 |
|
| 346 | 346 |
checkGraphOutArcList(adaptor, n1, 2); |
| 347 | 347 |
checkGraphOutArcList(adaptor, n2, 2); |
| 348 | 348 |
checkGraphOutArcList(adaptor, n3, 2); |
| 349 | 349 |
|
| 350 | 350 |
checkGraphInArcList(adaptor, n1, 2); |
| 351 | 351 |
checkGraphInArcList(adaptor, n2, 2); |
| 352 | 352 |
checkGraphInArcList(adaptor, n3, 2); |
| 353 | 353 |
|
| 354 | 354 |
checkGraphIncEdgeList(adaptor, n1, 2); |
| 355 | 355 |
checkGraphIncEdgeList(adaptor, n2, 2); |
| 356 | 356 |
checkGraphIncEdgeList(adaptor, n3, 2); |
| 357 | 357 |
|
| 358 | 358 |
checkNodeIds(adaptor); |
| 359 | 359 |
checkArcIds(adaptor); |
| 360 | 360 |
checkEdgeIds(adaptor); |
| 361 | 361 |
|
| 362 | 362 |
checkGraphNodeMap(adaptor); |
| 363 | 363 |
checkGraphArcMap(adaptor); |
| 364 | 364 |
checkGraphEdgeMap(adaptor); |
| 365 | 365 |
|
| 366 | 366 |
for (Adaptor::EdgeIt e(adaptor); e != INVALID; ++e) {
|
| 367 | 367 |
check(adaptor.u(e) == digraph.source(e), "Wrong undir"); |
| 368 | 368 |
check(adaptor.v(e) == digraph.target(e), "Wrong undir"); |
| 369 | 369 |
} |
| 370 | 370 |
|
| 371 | 371 |
} |
| 372 | 372 |
|
| 373 | 373 |
void checkResidual() {
|
| 374 | 374 |
checkConcept<concepts::Digraph, |
| 375 | 375 |
Residual<concepts::Digraph, |
| 376 | 376 |
concepts::Digraph::ArcMap<int>, |
| 377 | 377 |
concepts::Digraph::ArcMap<int> > >(); |
| 378 | 378 |
|
| 379 | 379 |
typedef ListDigraph Digraph; |
| 380 | 380 |
typedef Digraph::ArcMap<int> IntArcMap; |
| 381 | 381 |
typedef Residual<Digraph, IntArcMap> Adaptor; |
| 382 | 382 |
|
| 383 | 383 |
Digraph digraph; |
| 384 | 384 |
IntArcMap capacity(digraph), flow(digraph); |
| 385 | 385 |
Adaptor adaptor(digraph, capacity, flow); |
| 386 | 386 |
|
| 387 | 387 |
Digraph::Node n1 = digraph.addNode(); |
| 388 | 388 |
Digraph::Node n2 = digraph.addNode(); |
| 389 | 389 |
Digraph::Node n3 = digraph.addNode(); |
| 390 | 390 |
Digraph::Node n4 = digraph.addNode(); |
| 391 | 391 |
|
| 392 | 392 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
| 393 | 393 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
| 394 | 394 |
Digraph::Arc a3 = digraph.addArc(n1, n4); |
| 395 | 395 |
Digraph::Arc a4 = digraph.addArc(n2, n3); |
| 396 | 396 |
Digraph::Arc a5 = digraph.addArc(n2, n4); |
| 397 | 397 |
Digraph::Arc a6 = digraph.addArc(n3, n4); |
| 398 | 398 |
|
| 399 | 399 |
capacity[a1] = 8; |
| 400 | 400 |
capacity[a2] = 6; |
| 401 | 401 |
capacity[a3] = 4; |
| 402 | 402 |
capacity[a4] = 4; |
| 403 | 403 |
capacity[a5] = 6; |
| 404 | 404 |
capacity[a6] = 10; |
| 405 | 405 |
|
| 406 | 406 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) {
|
| 407 | 407 |
flow[a] = 0; |
| 408 | 408 |
} |
| 409 | 409 |
|
| 410 | 410 |
checkGraphNodeList(adaptor, 4); |
| 411 | 411 |
checkGraphArcList(adaptor, 6); |
| 412 | 412 |
checkGraphConArcList(adaptor, 6); |
| 413 | 413 |
|
| 414 | 414 |
checkGraphOutArcList(adaptor, n1, 3); |
| 415 | 415 |
checkGraphOutArcList(adaptor, n2, 2); |
| 416 | 416 |
checkGraphOutArcList(adaptor, n3, 1); |
| 417 | 417 |
checkGraphOutArcList(adaptor, n4, 0); |
| 418 | 418 |
|
| 419 | 419 |
checkGraphInArcList(adaptor, n1, 0); |
| 420 | 420 |
checkGraphInArcList(adaptor, n2, 1); |
| 421 | 421 |
checkGraphInArcList(adaptor, n3, 2); |
| 422 | 422 |
checkGraphInArcList(adaptor, n4, 3); |
| 423 | 423 |
|
| 424 | 424 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) {
|
| 425 | 425 |
flow[a] = capacity[a] / 2; |
| 426 | 426 |
} |
| 427 | 427 |
|
| 428 | 428 |
checkGraphNodeList(adaptor, 4); |
| 429 | 429 |
checkGraphArcList(adaptor, 12); |
| 430 | 430 |
checkGraphConArcList(adaptor, 12); |
| 431 | 431 |
|
| 432 | 432 |
checkGraphOutArcList(adaptor, n1, 3); |
| 433 | 433 |
checkGraphOutArcList(adaptor, n2, 3); |
| 434 | 434 |
checkGraphOutArcList(adaptor, n3, 3); |
| 435 | 435 |
checkGraphOutArcList(adaptor, n4, 3); |
| 436 | 436 |
|
| 437 | 437 |
checkGraphInArcList(adaptor, n1, 3); |
| 438 | 438 |
checkGraphInArcList(adaptor, n2, 3); |
| 439 | 439 |
checkGraphInArcList(adaptor, n3, 3); |
| 440 | 440 |
checkGraphInArcList(adaptor, n4, 3); |
| 441 | 441 |
|
| 442 | 442 |
checkNodeIds(adaptor); |
| 443 | 443 |
checkArcIds(adaptor); |
| 444 | 444 |
|
| 445 | 445 |
checkGraphNodeMap(adaptor); |
| 446 | 446 |
checkGraphArcMap(adaptor); |
| 447 | 447 |
|
| 448 | 448 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) {
|
| 449 | 449 |
flow[a] = capacity[a]; |
| 450 | 450 |
} |
| 451 | 451 |
|
| 452 | 452 |
checkGraphNodeList(adaptor, 4); |
| 453 | 453 |
checkGraphArcList(adaptor, 6); |
| 454 | 454 |
checkGraphConArcList(adaptor, 6); |
| 455 | 455 |
|
| 456 | 456 |
checkGraphOutArcList(adaptor, n1, 0); |
| 457 | 457 |
checkGraphOutArcList(adaptor, n2, 1); |
| 458 | 458 |
checkGraphOutArcList(adaptor, n3, 2); |
| 459 | 459 |
checkGraphOutArcList(adaptor, n4, 3); |
| 460 | 460 |
|
| 461 | 461 |
checkGraphInArcList(adaptor, n1, 3); |
| 462 | 462 |
checkGraphInArcList(adaptor, n2, 2); |
| 463 | 463 |
checkGraphInArcList(adaptor, n3, 1); |
| 464 | 464 |
checkGraphInArcList(adaptor, n4, 0); |
| 465 | 465 |
|
| 466 | 466 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) {
|
| 467 | 467 |
flow[a] = 0; |
| 468 | 468 |
} |
| 469 | 469 |
|
| 470 | 470 |
int flow_value = 0; |
| 471 | 471 |
while (true) {
|
| 472 | 472 |
|
| 473 | 473 |
Bfs<Adaptor> bfs(adaptor); |
| 474 | 474 |
bfs.run(n1, n4); |
| 475 | 475 |
|
| 476 | 476 |
if (!bfs.reached(n4)) break; |
| 477 | 477 |
|
| 478 | 478 |
Path<Adaptor> p = bfs.path(n4); |
| 479 | 479 |
|
| 480 | 480 |
int min = std::numeric_limits<int>::max(); |
| 481 | 481 |
for (Path<Adaptor>::ArcIt a(p); a != INVALID; ++a) {
|
| 482 | 482 |
if (adaptor.residualCapacity(a) < min) |
| 483 | 483 |
min = adaptor.residualCapacity(a); |
| 484 | 484 |
} |
| 485 | 485 |
|
| 486 | 486 |
for (Path<Adaptor>::ArcIt a(p); a != INVALID; ++a) {
|
| 487 | 487 |
adaptor.augment(a, min); |
| 488 | 488 |
} |
| 489 | 489 |
flow_value += min; |
| 490 | 490 |
} |
| 491 | 491 |
|
| 492 | 492 |
check(flow_value == 18, "Wrong flow with res graph adaptor"); |
| 493 | 493 |
|
| 494 | 494 |
} |
| 495 | 495 |
|
| 496 | 496 |
void checkSplitNodes() {
|
| 497 | 497 |
checkConcept<concepts::Digraph, SplitNodes<concepts::Digraph> >(); |
| 498 | 498 |
|
| 499 | 499 |
typedef ListDigraph Digraph; |
| 500 | 500 |
typedef SplitNodes<Digraph> Adaptor; |
| 501 | 501 |
|
| 502 | 502 |
Digraph digraph; |
| 503 | 503 |
Adaptor adaptor(digraph); |
| 504 | 504 |
|
| 505 | 505 |
Digraph::Node n1 = digraph.addNode(); |
| 506 | 506 |
Digraph::Node n2 = digraph.addNode(); |
| 507 | 507 |
Digraph::Node n3 = digraph.addNode(); |
| 508 | 508 |
|
| 509 | 509 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
| 510 | 510 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
| 511 | 511 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
| 512 | 512 |
|
| 513 | 513 |
checkGraphNodeList(adaptor, 6); |
| 514 | 514 |
checkGraphArcList(adaptor, 6); |
| 515 | 515 |
checkGraphConArcList(adaptor, 6); |
| 516 | 516 |
|
| 517 | 517 |
checkGraphOutArcList(adaptor, adaptor.inNode(n1), 1); |
| 518 | 518 |
checkGraphOutArcList(adaptor, adaptor.outNode(n1), 2); |
| 519 | 519 |
checkGraphOutArcList(adaptor, adaptor.inNode(n2), 1); |
| 520 | 520 |
checkGraphOutArcList(adaptor, adaptor.outNode(n2), 1); |
| 521 | 521 |
checkGraphOutArcList(adaptor, adaptor.inNode(n3), 1); |
| 522 | 522 |
checkGraphOutArcList(adaptor, adaptor.outNode(n3), 0); |
| 523 | 523 |
|
| 524 | 524 |
checkGraphInArcList(adaptor, adaptor.inNode(n1), 0); |
| 525 | 525 |
checkGraphInArcList(adaptor, adaptor.outNode(n1), 1); |
| 526 | 526 |
checkGraphInArcList(adaptor, adaptor.inNode(n2), 1); |
| 527 | 527 |
checkGraphInArcList(adaptor, adaptor.outNode(n2), 1); |
| 528 | 528 |
checkGraphInArcList(adaptor, adaptor.inNode(n3), 2); |
| 529 | 529 |
checkGraphInArcList(adaptor, adaptor.outNode(n3), 1); |
| 530 | 530 |
|
| 531 | 531 |
checkNodeIds(adaptor); |
| 532 | 532 |
checkArcIds(adaptor); |
| 533 | 533 |
|
| 534 | 534 |
checkGraphNodeMap(adaptor); |
| 535 | 535 |
checkGraphArcMap(adaptor); |
| 536 | 536 |
|
| 537 | 537 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) {
|
| 538 | 538 |
if (adaptor.origArc(a)) {
|
| 539 | 539 |
Digraph::Arc oa = a; |
| 540 | 540 |
check(adaptor.source(a) == adaptor.outNode(digraph.source(oa)), |
| 541 | 541 |
"Wrong split"); |
| 542 | 542 |
check(adaptor.target(a) == adaptor.inNode(digraph.target(oa)), |
| 543 | 543 |
"Wrong split"); |
| 544 | 544 |
} else {
|
| 545 | 545 |
Digraph::Node on = a; |
| 546 | 546 |
check(adaptor.source(a) == adaptor.inNode(on), "Wrong split"); |
| 547 | 547 |
check(adaptor.target(a) == adaptor.outNode(on), "Wrong split"); |
| 548 | 548 |
} |
| 549 | 549 |
} |
| 550 | 550 |
} |
| 551 | 551 |
|
| 552 | 552 |
void checkSubGraph() {
|
| 553 | 553 |
checkConcept<concepts::Graph, |
| 554 | 554 |
SubGraph<concepts::Graph, |
| 555 | 555 |
concepts::Graph::NodeMap<bool>, |
| 556 | 556 |
concepts::Graph::EdgeMap<bool> > >(); |
| 557 | 557 |
|
| 558 | 558 |
typedef ListGraph Graph; |
| 559 | 559 |
typedef Graph::NodeMap<bool> NodeFilter; |
| 560 | 560 |
typedef Graph::EdgeMap<bool> EdgeFilter; |
| 561 | 561 |
typedef SubGraph<Graph, NodeFilter, EdgeFilter> Adaptor; |
| 562 | 562 |
|
| 563 | 563 |
Graph graph; |
| 564 | 564 |
NodeFilter node_filter(graph); |
| 565 | 565 |
EdgeFilter edge_filter(graph); |
| 566 | 566 |
Adaptor adaptor(graph, node_filter, edge_filter); |
| 567 | 567 |
|
| 568 | 568 |
Graph::Node n1 = graph.addNode(); |
| 569 | 569 |
Graph::Node n2 = graph.addNode(); |
| 570 | 570 |
Graph::Node n3 = graph.addNode(); |
| 571 | 571 |
Graph::Node n4 = graph.addNode(); |
| 572 | 572 |
|
| 573 | 573 |
Graph::Edge e1 = graph.addEdge(n1, n2); |
| 574 | 574 |
Graph::Edge e2 = graph.addEdge(n1, n3); |
| 575 | 575 |
Graph::Edge e3 = graph.addEdge(n2, n3); |
| 576 | 576 |
Graph::Edge e4 = graph.addEdge(n3, n4); |
| 577 | 577 |
|
| 578 | 578 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = node_filter[n4] = true; |
| 579 | 579 |
edge_filter[e1] = edge_filter[e2] = edge_filter[e3] = edge_filter[e4] = true; |
| 580 |
|
|
| 580 |
|
|
| 581 | 581 |
checkGraphNodeList(adaptor, 4); |
| 582 | 582 |
checkGraphArcList(adaptor, 8); |
| 583 | 583 |
checkGraphEdgeList(adaptor, 4); |
| 584 | 584 |
checkGraphConArcList(adaptor, 8); |
| 585 | 585 |
checkGraphConEdgeList(adaptor, 4); |
| 586 | 586 |
|
| 587 | 587 |
checkGraphOutArcList(adaptor, n1, 2); |
| 588 | 588 |
checkGraphOutArcList(adaptor, n2, 2); |
| 589 | 589 |
checkGraphOutArcList(adaptor, n3, 3); |
| 590 | 590 |
checkGraphOutArcList(adaptor, n4, 1); |
| 591 | 591 |
|
| 592 | 592 |
checkGraphInArcList(adaptor, n1, 2); |
| 593 | 593 |
checkGraphInArcList(adaptor, n2, 2); |
| 594 | 594 |
checkGraphInArcList(adaptor, n3, 3); |
| 595 | 595 |
checkGraphInArcList(adaptor, n4, 1); |
| 596 | 596 |
|
| 597 | 597 |
checkGraphIncEdgeList(adaptor, n1, 2); |
| 598 | 598 |
checkGraphIncEdgeList(adaptor, n2, 2); |
| 599 | 599 |
checkGraphIncEdgeList(adaptor, n3, 3); |
| 600 | 600 |
checkGraphIncEdgeList(adaptor, n4, 1); |
| 601 | 601 |
|
| 602 | 602 |
checkNodeIds(adaptor); |
| 603 | 603 |
checkArcIds(adaptor); |
| 604 | 604 |
checkEdgeIds(adaptor); |
| 605 | 605 |
|
| 606 | 606 |
checkGraphNodeMap(adaptor); |
| 607 | 607 |
checkGraphArcMap(adaptor); |
| 608 | 608 |
checkGraphEdgeMap(adaptor); |
| 609 | 609 |
|
| 610 | 610 |
edge_filter[e2] = false; |
| 611 | 611 |
|
| 612 | 612 |
checkGraphNodeList(adaptor, 4); |
| 613 | 613 |
checkGraphArcList(adaptor, 6); |
| 614 | 614 |
checkGraphEdgeList(adaptor, 3); |
| 615 | 615 |
checkGraphConArcList(adaptor, 6); |
| 616 | 616 |
checkGraphConEdgeList(adaptor, 3); |
| 617 | 617 |
|
| 618 | 618 |
checkGraphOutArcList(adaptor, n1, 1); |
| 619 | 619 |
checkGraphOutArcList(adaptor, n2, 2); |
| 620 | 620 |
checkGraphOutArcList(adaptor, n3, 2); |
| 621 | 621 |
checkGraphOutArcList(adaptor, n4, 1); |
| 622 | 622 |
|
| 623 | 623 |
checkGraphInArcList(adaptor, n1, 1); |
| 624 | 624 |
checkGraphInArcList(adaptor, n2, 2); |
| 625 | 625 |
checkGraphInArcList(adaptor, n3, 2); |
| 626 | 626 |
checkGraphInArcList(adaptor, n4, 1); |
| 627 | 627 |
|
| 628 | 628 |
checkGraphIncEdgeList(adaptor, n1, 1); |
| 629 | 629 |
checkGraphIncEdgeList(adaptor, n2, 2); |
| 630 | 630 |
checkGraphIncEdgeList(adaptor, n3, 2); |
| 631 | 631 |
checkGraphIncEdgeList(adaptor, n4, 1); |
| 632 | 632 |
|
| 633 | 633 |
checkNodeIds(adaptor); |
| 634 | 634 |
checkArcIds(adaptor); |
| 635 | 635 |
checkEdgeIds(adaptor); |
| 636 | 636 |
|
| 637 | 637 |
checkGraphNodeMap(adaptor); |
| 638 | 638 |
checkGraphArcMap(adaptor); |
| 639 | 639 |
checkGraphEdgeMap(adaptor); |
| 640 | 640 |
|
| 641 | 641 |
node_filter[n1] = false; |
| 642 | 642 |
|
| 643 | 643 |
checkGraphNodeList(adaptor, 3); |
| 644 | 644 |
checkGraphArcList(adaptor, 4); |
| 645 | 645 |
checkGraphEdgeList(adaptor, 2); |
| 646 | 646 |
checkGraphConArcList(adaptor, 4); |
| 647 | 647 |
checkGraphConEdgeList(adaptor, 2); |
| 648 | 648 |
|
| 649 | 649 |
checkGraphOutArcList(adaptor, n2, 1); |
| 650 | 650 |
checkGraphOutArcList(adaptor, n3, 2); |
| 651 | 651 |
checkGraphOutArcList(adaptor, n4, 1); |
| 652 | 652 |
|
| 653 | 653 |
checkGraphInArcList(adaptor, n2, 1); |
| 654 | 654 |
checkGraphInArcList(adaptor, n3, 2); |
| 655 | 655 |
checkGraphInArcList(adaptor, n4, 1); |
| 656 | 656 |
|
| 657 | 657 |
checkGraphIncEdgeList(adaptor, n2, 1); |
| 658 | 658 |
checkGraphIncEdgeList(adaptor, n3, 2); |
| 659 | 659 |
checkGraphIncEdgeList(adaptor, n4, 1); |
| 660 | 660 |
|
| 661 | 661 |
checkNodeIds(adaptor); |
| 662 | 662 |
checkArcIds(adaptor); |
| 663 | 663 |
checkEdgeIds(adaptor); |
| 664 | 664 |
|
| 665 | 665 |
checkGraphNodeMap(adaptor); |
| 666 | 666 |
checkGraphArcMap(adaptor); |
| 667 | 667 |
checkGraphEdgeMap(adaptor); |
| 668 | 668 |
|
| 669 | 669 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = node_filter[n4] = false; |
| 670 | 670 |
edge_filter[e1] = edge_filter[e2] = edge_filter[e3] = edge_filter[e4] = false; |
| 671 | 671 |
|
| 672 | 672 |
checkGraphNodeList(adaptor, 0); |
| 673 | 673 |
checkGraphArcList(adaptor, 0); |
| 674 | 674 |
checkGraphEdgeList(adaptor, 0); |
| 675 | 675 |
checkGraphConArcList(adaptor, 0); |
| 676 | 676 |
checkGraphConEdgeList(adaptor, 0); |
| 677 | 677 |
|
| 678 | 678 |
checkNodeIds(adaptor); |
| 679 | 679 |
checkArcIds(adaptor); |
| 680 | 680 |
checkEdgeIds(adaptor); |
| 681 | 681 |
|
| 682 | 682 |
checkGraphNodeMap(adaptor); |
| 683 | 683 |
checkGraphArcMap(adaptor); |
| 684 | 684 |
checkGraphEdgeMap(adaptor); |
| 685 | 685 |
} |
| 686 | 686 |
|
| 687 | 687 |
void checkFilterNodes2() {
|
| 688 | 688 |
checkConcept<concepts::Graph, |
| 689 | 689 |
FilterNodes<concepts::Graph, |
| 690 | 690 |
concepts::Graph::NodeMap<bool> > >(); |
| 691 | 691 |
|
| 692 | 692 |
typedef ListGraph Graph; |
| 693 | 693 |
typedef Graph::NodeMap<bool> NodeFilter; |
| 694 | 694 |
typedef FilterNodes<Graph, NodeFilter> Adaptor; |
| 695 | 695 |
|
| 696 | 696 |
Graph graph; |
| 697 | 697 |
NodeFilter node_filter(graph); |
| 698 | 698 |
Adaptor adaptor(graph, node_filter); |
| 699 | 699 |
|
| 700 | 700 |
Graph::Node n1 = graph.addNode(); |
| 701 | 701 |
Graph::Node n2 = graph.addNode(); |
| 702 | 702 |
Graph::Node n3 = graph.addNode(); |
| 703 | 703 |
Graph::Node n4 = graph.addNode(); |
| 704 | 704 |
|
| 705 | 705 |
Graph::Edge e1 = graph.addEdge(n1, n2); |
| 706 | 706 |
Graph::Edge e2 = graph.addEdge(n1, n3); |
| 707 | 707 |
Graph::Edge e3 = graph.addEdge(n2, n3); |
| 708 | 708 |
Graph::Edge e4 = graph.addEdge(n3, n4); |
| 709 | 709 |
|
| 710 | 710 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = node_filter[n4] = true; |
| 711 |
|
|
| 711 |
|
|
| 712 | 712 |
checkGraphNodeList(adaptor, 4); |
| 713 | 713 |
checkGraphArcList(adaptor, 8); |
| 714 | 714 |
checkGraphEdgeList(adaptor, 4); |
| 715 | 715 |
checkGraphConArcList(adaptor, 8); |
| 716 | 716 |
checkGraphConEdgeList(adaptor, 4); |
| 717 | 717 |
|
| 718 | 718 |
checkGraphOutArcList(adaptor, n1, 2); |
| 719 | 719 |
checkGraphOutArcList(adaptor, n2, 2); |
| 720 | 720 |
checkGraphOutArcList(adaptor, n3, 3); |
| 721 | 721 |
checkGraphOutArcList(adaptor, n4, 1); |
| 722 | 722 |
|
| 723 | 723 |
checkGraphInArcList(adaptor, n1, 2); |
| 724 | 724 |
checkGraphInArcList(adaptor, n2, 2); |
| 725 | 725 |
checkGraphInArcList(adaptor, n3, 3); |
| 726 | 726 |
checkGraphInArcList(adaptor, n4, 1); |
| 727 | 727 |
|
| 728 | 728 |
checkGraphIncEdgeList(adaptor, n1, 2); |
| 729 | 729 |
checkGraphIncEdgeList(adaptor, n2, 2); |
| 730 | 730 |
checkGraphIncEdgeList(adaptor, n3, 3); |
| 731 | 731 |
checkGraphIncEdgeList(adaptor, n4, 1); |
| 732 | 732 |
|
| 733 | 733 |
checkNodeIds(adaptor); |
| 734 | 734 |
checkArcIds(adaptor); |
| 735 | 735 |
checkEdgeIds(adaptor); |
| 736 | 736 |
|
| 737 | 737 |
checkGraphNodeMap(adaptor); |
| 738 | 738 |
checkGraphArcMap(adaptor); |
| 739 | 739 |
checkGraphEdgeMap(adaptor); |
| 740 | 740 |
|
| 741 | 741 |
node_filter[n1] = false; |
| 742 | 742 |
|
| 743 | 743 |
checkGraphNodeList(adaptor, 3); |
| 744 | 744 |
checkGraphArcList(adaptor, 4); |
| 745 | 745 |
checkGraphEdgeList(adaptor, 2); |
| 746 | 746 |
checkGraphConArcList(adaptor, 4); |
| 747 | 747 |
checkGraphConEdgeList(adaptor, 2); |
| 748 | 748 |
|
| 749 | 749 |
checkGraphOutArcList(adaptor, n2, 1); |
| 750 | 750 |
checkGraphOutArcList(adaptor, n3, 2); |
| 751 | 751 |
checkGraphOutArcList(adaptor, n4, 1); |
| 752 | 752 |
|
| 753 | 753 |
checkGraphInArcList(adaptor, n2, 1); |
| 754 | 754 |
checkGraphInArcList(adaptor, n3, 2); |
| 755 | 755 |
checkGraphInArcList(adaptor, n4, 1); |
| 756 | 756 |
|
| 757 | 757 |
checkGraphIncEdgeList(adaptor, n2, 1); |
| 758 | 758 |
checkGraphIncEdgeList(adaptor, n3, 2); |
| 759 | 759 |
checkGraphIncEdgeList(adaptor, n4, 1); |
| 760 | 760 |
|
| 761 | 761 |
checkNodeIds(adaptor); |
| 762 | 762 |
checkArcIds(adaptor); |
| 763 | 763 |
checkEdgeIds(adaptor); |
| 764 | 764 |
|
| 765 | 765 |
checkGraphNodeMap(adaptor); |
| 766 | 766 |
checkGraphArcMap(adaptor); |
| 767 | 767 |
checkGraphEdgeMap(adaptor); |
| 768 | 768 |
|
| 769 | 769 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = node_filter[n4] = false; |
| 770 | 770 |
|
| 771 | 771 |
checkGraphNodeList(adaptor, 0); |
| 772 | 772 |
checkGraphArcList(adaptor, 0); |
| 773 | 773 |
checkGraphEdgeList(adaptor, 0); |
| 774 | 774 |
checkGraphConArcList(adaptor, 0); |
| 775 | 775 |
checkGraphConEdgeList(adaptor, 0); |
| 776 | 776 |
|
| 777 | 777 |
checkNodeIds(adaptor); |
| 778 | 778 |
checkArcIds(adaptor); |
| 779 | 779 |
checkEdgeIds(adaptor); |
| 780 | 780 |
|
| 781 | 781 |
checkGraphNodeMap(adaptor); |
| 782 | 782 |
checkGraphArcMap(adaptor); |
| 783 | 783 |
checkGraphEdgeMap(adaptor); |
| 784 | 784 |
} |
| 785 | 785 |
|
| 786 | 786 |
void checkFilterEdges() {
|
| 787 | 787 |
checkConcept<concepts::Graph, |
| 788 | 788 |
FilterEdges<concepts::Graph, |
| 789 | 789 |
concepts::Graph::EdgeMap<bool> > >(); |
| 790 | 790 |
|
| 791 | 791 |
typedef ListGraph Graph; |
| 792 | 792 |
typedef Graph::EdgeMap<bool> EdgeFilter; |
| 793 | 793 |
typedef FilterEdges<Graph, EdgeFilter> Adaptor; |
| 794 | 794 |
|
| 795 | 795 |
Graph graph; |
| 796 | 796 |
EdgeFilter edge_filter(graph); |
| 797 | 797 |
Adaptor adaptor(graph, edge_filter); |
| 798 | 798 |
|
| 799 | 799 |
Graph::Node n1 = graph.addNode(); |
| 800 | 800 |
Graph::Node n2 = graph.addNode(); |
| 801 | 801 |
Graph::Node n3 = graph.addNode(); |
| 802 | 802 |
Graph::Node n4 = graph.addNode(); |
| 803 | 803 |
|
| 804 | 804 |
Graph::Edge e1 = graph.addEdge(n1, n2); |
| 805 | 805 |
Graph::Edge e2 = graph.addEdge(n1, n3); |
| 806 | 806 |
Graph::Edge e3 = graph.addEdge(n2, n3); |
| 807 | 807 |
Graph::Edge e4 = graph.addEdge(n3, n4); |
| 808 | 808 |
|
| 809 | 809 |
edge_filter[e1] = edge_filter[e2] = edge_filter[e3] = edge_filter[e4] = true; |
| 810 |
|
|
| 810 |
|
|
| 811 | 811 |
checkGraphNodeList(adaptor, 4); |
| 812 | 812 |
checkGraphArcList(adaptor, 8); |
| 813 | 813 |
checkGraphEdgeList(adaptor, 4); |
| 814 | 814 |
checkGraphConArcList(adaptor, 8); |
| 815 | 815 |
checkGraphConEdgeList(adaptor, 4); |
| 816 | 816 |
|
| 817 | 817 |
checkGraphOutArcList(adaptor, n1, 2); |
| 818 | 818 |
checkGraphOutArcList(adaptor, n2, 2); |
| 819 | 819 |
checkGraphOutArcList(adaptor, n3, 3); |
| 820 | 820 |
checkGraphOutArcList(adaptor, n4, 1); |
| 821 | 821 |
|
| 822 | 822 |
checkGraphInArcList(adaptor, n1, 2); |
| 823 | 823 |
checkGraphInArcList(adaptor, n2, 2); |
| 824 | 824 |
checkGraphInArcList(adaptor, n3, 3); |
| 825 | 825 |
checkGraphInArcList(adaptor, n4, 1); |
| 826 | 826 |
|
| 827 | 827 |
checkGraphIncEdgeList(adaptor, n1, 2); |
| 828 | 828 |
checkGraphIncEdgeList(adaptor, n2, 2); |
| 829 | 829 |
checkGraphIncEdgeList(adaptor, n3, 3); |
| 830 | 830 |
checkGraphIncEdgeList(adaptor, n4, 1); |
| 831 | 831 |
|
| 832 | 832 |
checkNodeIds(adaptor); |
| 833 | 833 |
checkArcIds(adaptor); |
| 834 | 834 |
checkEdgeIds(adaptor); |
| 835 | 835 |
|
| 836 | 836 |
checkGraphNodeMap(adaptor); |
| 837 | 837 |
checkGraphArcMap(adaptor); |
| 838 | 838 |
checkGraphEdgeMap(adaptor); |
| 839 | 839 |
|
| 840 | 840 |
edge_filter[e2] = false; |
| 841 | 841 |
|
| 842 | 842 |
checkGraphNodeList(adaptor, 4); |
| 843 | 843 |
checkGraphArcList(adaptor, 6); |
| 844 | 844 |
checkGraphEdgeList(adaptor, 3); |
| 845 | 845 |
checkGraphConArcList(adaptor, 6); |
| 846 | 846 |
checkGraphConEdgeList(adaptor, 3); |
| 847 | 847 |
|
| 848 | 848 |
checkGraphOutArcList(adaptor, n1, 1); |
| 849 | 849 |
checkGraphOutArcList(adaptor, n2, 2); |
| 850 | 850 |
checkGraphOutArcList(adaptor, n3, 2); |
| 851 | 851 |
checkGraphOutArcList(adaptor, n4, 1); |
| 852 | 852 |
|
| 853 | 853 |
checkGraphInArcList(adaptor, n1, 1); |
| 854 | 854 |
checkGraphInArcList(adaptor, n2, 2); |
| 855 | 855 |
checkGraphInArcList(adaptor, n3, 2); |
| 856 | 856 |
checkGraphInArcList(adaptor, n4, 1); |
| 857 | 857 |
|
| 858 | 858 |
checkGraphIncEdgeList(adaptor, n1, 1); |
| 859 | 859 |
checkGraphIncEdgeList(adaptor, n2, 2); |
| 860 | 860 |
checkGraphIncEdgeList(adaptor, n3, 2); |
| 861 | 861 |
checkGraphIncEdgeList(adaptor, n4, 1); |
| 862 | 862 |
|
| 863 | 863 |
checkNodeIds(adaptor); |
| 864 | 864 |
checkArcIds(adaptor); |
| 865 | 865 |
checkEdgeIds(adaptor); |
| 866 | 866 |
|
| 867 | 867 |
checkGraphNodeMap(adaptor); |
| 868 | 868 |
checkGraphArcMap(adaptor); |
| 869 | 869 |
checkGraphEdgeMap(adaptor); |
| 870 | 870 |
|
| 871 | 871 |
edge_filter[e1] = edge_filter[e2] = edge_filter[e3] = edge_filter[e4] = false; |
| 872 | 872 |
|
| 873 | 873 |
checkGraphNodeList(adaptor, 4); |
| 874 | 874 |
checkGraphArcList(adaptor, 0); |
| 875 | 875 |
checkGraphEdgeList(adaptor, 0); |
| 876 | 876 |
checkGraphConArcList(adaptor, 0); |
| 877 | 877 |
checkGraphConEdgeList(adaptor, 0); |
| 878 | 878 |
|
| 879 | 879 |
checkNodeIds(adaptor); |
| 880 | 880 |
checkArcIds(adaptor); |
| 881 | 881 |
checkEdgeIds(adaptor); |
| 882 | 882 |
|
| 883 | 883 |
checkGraphNodeMap(adaptor); |
| 884 | 884 |
checkGraphArcMap(adaptor); |
| 885 | 885 |
checkGraphEdgeMap(adaptor); |
| 886 | 886 |
} |
| 887 | 887 |
|
| 888 | 888 |
void checkOrienter() {
|
| 889 | 889 |
checkConcept<concepts::Digraph, |
| 890 | 890 |
Orienter<concepts::Graph, concepts::Graph::EdgeMap<bool> > >(); |
| 891 | 891 |
|
| 892 | 892 |
typedef ListGraph Graph; |
| 893 | 893 |
typedef ListGraph::EdgeMap<bool> DirMap; |
| 894 | 894 |
typedef Orienter<Graph> Adaptor; |
| 895 | 895 |
|
| 896 | 896 |
Graph graph; |
| 897 | 897 |
DirMap dir(graph, true); |
| 898 | 898 |
Adaptor adaptor(graph, dir); |
| 899 | 899 |
|
| 900 | 900 |
Graph::Node n1 = graph.addNode(); |
| 901 | 901 |
Graph::Node n2 = graph.addNode(); |
| 902 | 902 |
Graph::Node n3 = graph.addNode(); |
| 903 | 903 |
|
| 904 | 904 |
Graph::Edge e1 = graph.addEdge(n1, n2); |
| 905 | 905 |
Graph::Edge e2 = graph.addEdge(n1, n3); |
| 906 | 906 |
Graph::Edge e3 = graph.addEdge(n2, n3); |
| 907 | 907 |
|
| 908 | 908 |
checkGraphNodeList(adaptor, 3); |
| 909 | 909 |
checkGraphArcList(adaptor, 3); |
| 910 | 910 |
checkGraphConArcList(adaptor, 3); |
| 911 | 911 |
|
| 912 | 912 |
{
|
| 913 | 913 |
dir[e1] = true; |
| 914 | 914 |
Adaptor::Node u = adaptor.source(e1); |
| 915 | 915 |
Adaptor::Node v = adaptor.target(e1); |
| 916 | 916 |
|
| 917 | 917 |
dir[e1] = false; |
| 918 | 918 |
check (u == adaptor.target(e1), "Wrong dir"); |
| 919 | 919 |
check (v == adaptor.source(e1), "Wrong dir"); |
| 920 | 920 |
|
| 921 | 921 |
check ((u == n1 && v == n2) || (u == n2 && v == n1), "Wrong dir"); |
| 922 | 922 |
dir[e1] = n1 == u; |
| 923 | 923 |
} |
| 924 | 924 |
|
| 925 | 925 |
{
|
| 926 | 926 |
dir[e2] = true; |
| 927 | 927 |
Adaptor::Node u = adaptor.source(e2); |
| 928 | 928 |
Adaptor::Node v = adaptor.target(e2); |
| 929 | 929 |
|
| 930 | 930 |
dir[e2] = false; |
| 931 | 931 |
check (u == adaptor.target(e2), "Wrong dir"); |
| 932 | 932 |
check (v == adaptor.source(e2), "Wrong dir"); |
| 933 | 933 |
|
| 934 | 934 |
check ((u == n1 && v == n3) || (u == n3 && v == n1), "Wrong dir"); |
| 935 | 935 |
dir[e2] = n3 == u; |
| 936 | 936 |
} |
| 937 | 937 |
|
| 938 | 938 |
{
|
| 939 | 939 |
dir[e3] = true; |
| 940 | 940 |
Adaptor::Node u = adaptor.source(e3); |
| 941 | 941 |
Adaptor::Node v = adaptor.target(e3); |
| 942 | 942 |
|
| 943 | 943 |
dir[e3] = false; |
| 944 | 944 |
check (u == adaptor.target(e3), "Wrong dir"); |
| 945 | 945 |
check (v == adaptor.source(e3), "Wrong dir"); |
| 946 | 946 |
|
| 947 | 947 |
check ((u == n2 && v == n3) || (u == n3 && v == n2), "Wrong dir"); |
| 948 | 948 |
dir[e3] = n2 == u; |
| 949 | 949 |
} |
| 950 | 950 |
|
| 951 | 951 |
checkGraphOutArcList(adaptor, n1, 1); |
| 952 | 952 |
checkGraphOutArcList(adaptor, n2, 1); |
| 953 | 953 |
checkGraphOutArcList(adaptor, n3, 1); |
| 954 | 954 |
|
| 955 | 955 |
checkGraphInArcList(adaptor, n1, 1); |
| 956 | 956 |
checkGraphInArcList(adaptor, n2, 1); |
| 957 | 957 |
checkGraphInArcList(adaptor, n3, 1); |
| 958 | 958 |
|
| 959 | 959 |
checkNodeIds(adaptor); |
| 960 | 960 |
checkArcIds(adaptor); |
| 961 | 961 |
|
| 962 | 962 |
checkGraphNodeMap(adaptor); |
| 963 | 963 |
checkGraphArcMap(adaptor); |
| 964 | 964 |
|
| 965 | 965 |
} |
| 966 | 966 |
|
| 967 | 967 |
|
| 968 | 968 |
int main(int, const char **) {
|
| 969 | 969 |
|
| 970 | 970 |
checkReverseDigraph(); |
| 971 | 971 |
checkSubDigraph(); |
| 972 | 972 |
checkFilterNodes1(); |
| 973 | 973 |
checkFilterArcs(); |
| 974 | 974 |
checkUndirector(); |
| 975 | 975 |
checkResidual(); |
| 976 | 976 |
checkSplitNodes(); |
| 977 | 977 |
|
| 978 | 978 |
checkSubGraph(); |
| 979 | 979 |
checkFilterNodes2(); |
| 980 | 980 |
checkFilterEdges(); |
| 981 | 981 |
checkOrienter(); |
| 982 | 982 |
|
| 983 | 983 |
return 0; |
| 984 | 984 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/smart_graph.h> |
| 20 | 20 |
#include <lemon/list_graph.h> |
| 21 | 21 |
#include <lemon/lgf_reader.h> |
| 22 | 22 |
#include <lemon/error.h> |
| 23 | 23 |
|
| 24 | 24 |
#include "test_tools.h" |
| 25 | 25 |
|
| 26 | 26 |
using namespace std; |
| 27 | 27 |
using namespace lemon; |
| 28 | 28 |
|
| 29 | 29 |
void digraph_copy_test() {
|
| 30 | 30 |
const int nn = 10; |
| 31 | 31 |
|
| 32 | 32 |
SmartDigraph from; |
| 33 | 33 |
SmartDigraph::NodeMap<int> fnm(from); |
| 34 | 34 |
SmartDigraph::ArcMap<int> fam(from); |
| 35 | 35 |
SmartDigraph::Node fn = INVALID; |
| 36 | 36 |
SmartDigraph::Arc fa = INVALID; |
| 37 | 37 |
|
| 38 | 38 |
std::vector<SmartDigraph::Node> fnv; |
| 39 | 39 |
for (int i = 0; i < nn; ++i) {
|
| 40 | 40 |
SmartDigraph::Node node = from.addNode(); |
| 41 | 41 |
fnv.push_back(node); |
| 42 | 42 |
fnm[node] = i * i; |
| 43 | 43 |
if (i == 0) fn = node; |
| 44 | 44 |
} |
| 45 | 45 |
|
| 46 | 46 |
for (int i = 0; i < nn; ++i) {
|
| 47 | 47 |
for (int j = 0; j < nn; ++j) {
|
| 48 | 48 |
SmartDigraph::Arc arc = from.addArc(fnv[i], fnv[j]); |
| 49 | 49 |
fam[arc] = i + j * j; |
| 50 | 50 |
if (i == 0 && j == 0) fa = arc; |
| 51 | 51 |
} |
| 52 | 52 |
} |
| 53 | 53 |
|
| 54 | 54 |
ListDigraph to; |
| 55 | 55 |
ListDigraph::NodeMap<int> tnm(to); |
| 56 | 56 |
ListDigraph::ArcMap<int> tam(to); |
| 57 | 57 |
ListDigraph::Node tn; |
| 58 | 58 |
ListDigraph::Arc ta; |
| 59 | 59 |
|
| 60 | 60 |
SmartDigraph::NodeMap<ListDigraph::Node> nr(from); |
| 61 | 61 |
SmartDigraph::ArcMap<ListDigraph::Arc> er(from); |
| 62 | 62 |
|
| 63 | 63 |
ListDigraph::NodeMap<SmartDigraph::Node> ncr(to); |
| 64 | 64 |
ListDigraph::ArcMap<SmartDigraph::Arc> ecr(to); |
| 65 | 65 |
|
| 66 | 66 |
digraphCopy(from, to). |
| 67 | 67 |
nodeMap(fnm, tnm).arcMap(fam, tam). |
| 68 | 68 |
nodeRef(nr).arcRef(er). |
| 69 | 69 |
nodeCrossRef(ncr).arcCrossRef(ecr). |
| 70 | 70 |
node(fn, tn).arc(fa, ta).run(); |
| 71 | 71 |
|
| 72 | 72 |
for (SmartDigraph::NodeIt it(from); it != INVALID; ++it) {
|
| 73 | 73 |
check(ncr[nr[it]] == it, "Wrong copy."); |
| 74 | 74 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
| 75 | 75 |
} |
| 76 | 76 |
|
| 77 | 77 |
for (SmartDigraph::ArcIt it(from); it != INVALID; ++it) {
|
| 78 | 78 |
check(ecr[er[it]] == it, "Wrong copy."); |
| 79 | 79 |
check(fam[it] == tam[er[it]], "Wrong copy."); |
| 80 | 80 |
check(nr[from.source(it)] == to.source(er[it]), "Wrong copy."); |
| 81 | 81 |
check(nr[from.target(it)] == to.target(er[it]), "Wrong copy."); |
| 82 | 82 |
} |
| 83 | 83 |
|
| 84 | 84 |
for (ListDigraph::NodeIt it(to); it != INVALID; ++it) {
|
| 85 | 85 |
check(nr[ncr[it]] == it, "Wrong copy."); |
| 86 | 86 |
} |
| 87 | 87 |
|
| 88 | 88 |
for (ListDigraph::ArcIt it(to); it != INVALID; ++it) {
|
| 89 | 89 |
check(er[ecr[it]] == it, "Wrong copy."); |
| 90 | 90 |
} |
| 91 | 91 |
check(tn == nr[fn], "Wrong copy."); |
| 92 | 92 |
check(ta == er[fa], "Wrong copy."); |
| 93 | 93 |
} |
| 94 | 94 |
|
| 95 | 95 |
void graph_copy_test() {
|
| 96 | 96 |
const int nn = 10; |
| 97 | 97 |
|
| 98 | 98 |
SmartGraph from; |
| 99 | 99 |
SmartGraph::NodeMap<int> fnm(from); |
| 100 | 100 |
SmartGraph::ArcMap<int> fam(from); |
| 101 | 101 |
SmartGraph::EdgeMap<int> fem(from); |
| 102 | 102 |
SmartGraph::Node fn = INVALID; |
| 103 | 103 |
SmartGraph::Arc fa = INVALID; |
| 104 | 104 |
SmartGraph::Edge fe = INVALID; |
| 105 | 105 |
|
| 106 | 106 |
std::vector<SmartGraph::Node> fnv; |
| 107 | 107 |
for (int i = 0; i < nn; ++i) {
|
| 108 | 108 |
SmartGraph::Node node = from.addNode(); |
| 109 | 109 |
fnv.push_back(node); |
| 110 | 110 |
fnm[node] = i * i; |
| 111 | 111 |
if (i == 0) fn = node; |
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 | 114 |
for (int i = 0; i < nn; ++i) {
|
| 115 | 115 |
for (int j = 0; j < nn; ++j) {
|
| 116 | 116 |
SmartGraph::Edge edge = from.addEdge(fnv[i], fnv[j]); |
| 117 | 117 |
fem[edge] = i * i + j * j; |
| 118 | 118 |
fam[from.direct(edge, true)] = i + j * j; |
| 119 | 119 |
fam[from.direct(edge, false)] = i * i + j; |
| 120 | 120 |
if (i == 0 && j == 0) fa = from.direct(edge, true); |
| 121 | 121 |
if (i == 0 && j == 0) fe = edge; |
| 122 | 122 |
} |
| 123 | 123 |
} |
| 124 | 124 |
|
| 125 | 125 |
ListGraph to; |
| 126 | 126 |
ListGraph::NodeMap<int> tnm(to); |
| 127 | 127 |
ListGraph::ArcMap<int> tam(to); |
| 128 | 128 |
ListGraph::EdgeMap<int> tem(to); |
| 129 | 129 |
ListGraph::Node tn; |
| 130 | 130 |
ListGraph::Arc ta; |
| 131 | 131 |
ListGraph::Edge te; |
| 132 | 132 |
|
| 133 | 133 |
SmartGraph::NodeMap<ListGraph::Node> nr(from); |
| 134 | 134 |
SmartGraph::ArcMap<ListGraph::Arc> ar(from); |
| 135 | 135 |
SmartGraph::EdgeMap<ListGraph::Edge> er(from); |
| 136 | 136 |
|
| 137 | 137 |
ListGraph::NodeMap<SmartGraph::Node> ncr(to); |
| 138 | 138 |
ListGraph::ArcMap<SmartGraph::Arc> acr(to); |
| 139 | 139 |
ListGraph::EdgeMap<SmartGraph::Edge> ecr(to); |
| 140 | 140 |
|
| 141 | 141 |
graphCopy(from, to). |
| 142 | 142 |
nodeMap(fnm, tnm).arcMap(fam, tam).edgeMap(fem, tem). |
| 143 | 143 |
nodeRef(nr).arcRef(ar).edgeRef(er). |
| 144 | 144 |
nodeCrossRef(ncr).arcCrossRef(acr).edgeCrossRef(ecr). |
| 145 | 145 |
node(fn, tn).arc(fa, ta).edge(fe, te).run(); |
| 146 | 146 |
|
| 147 | 147 |
for (SmartGraph::NodeIt it(from); it != INVALID; ++it) {
|
| 148 | 148 |
check(ncr[nr[it]] == it, "Wrong copy."); |
| 149 | 149 |
check(fnm[it] == tnm[nr[it]], "Wrong copy."); |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
for (SmartGraph::ArcIt it(from); it != INVALID; ++it) {
|
| 153 | 153 |
check(acr[ar[it]] == it, "Wrong copy."); |
| 154 | 154 |
check(fam[it] == tam[ar[it]], "Wrong copy."); |
| 155 | 155 |
check(nr[from.source(it)] == to.source(ar[it]), "Wrong copy."); |
| 156 | 156 |
check(nr[from.target(it)] == to.target(ar[it]), "Wrong copy."); |
| 157 | 157 |
} |
| 158 | 158 |
|
| 159 | 159 |
for (SmartGraph::EdgeIt it(from); it != INVALID; ++it) {
|
| 160 | 160 |
check(ecr[er[it]] == it, "Wrong copy."); |
| 161 | 161 |
check(fem[it] == tem[er[it]], "Wrong copy."); |
| 162 | 162 |
check(nr[from.u(it)] == to.u(er[it]) || nr[from.u(it)] == to.v(er[it]), |
| 163 | 163 |
"Wrong copy."); |
| 164 | 164 |
check(nr[from.v(it)] == to.u(er[it]) || nr[from.v(it)] == to.v(er[it]), |
| 165 | 165 |
"Wrong copy."); |
| 166 | 166 |
check((from.u(it) != from.v(it)) == (to.u(er[it]) != to.v(er[it])), |
| 167 | 167 |
"Wrong copy."); |
| 168 | 168 |
} |
| 169 | 169 |
|
| 170 | 170 |
for (ListGraph::NodeIt it(to); it != INVALID; ++it) {
|
| 171 | 171 |
check(nr[ncr[it]] == it, "Wrong copy."); |
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
for (ListGraph::ArcIt it(to); it != INVALID; ++it) {
|
| 175 | 175 |
check(ar[acr[it]] == it, "Wrong copy."); |
| 176 | 176 |
} |
| 177 | 177 |
for (ListGraph::EdgeIt it(to); it != INVALID; ++it) {
|
| 178 | 178 |
check(er[ecr[it]] == it, "Wrong copy."); |
| 179 | 179 |
} |
| 180 | 180 |
check(tn == nr[fn], "Wrong copy."); |
| 181 | 181 |
check(ta == ar[fa], "Wrong copy."); |
| 182 | 182 |
check(te == er[fe], "Wrong copy."); |
| 183 | 183 |
} |
| 184 | 184 |
|
| 185 | 185 |
|
| 186 | 186 |
int main() {
|
| 187 | 187 |
digraph_copy_test(); |
| 188 | 188 |
graph_copy_test(); |
| 189 | 189 |
|
| 190 | 190 |
return 0; |
| 191 | 191 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/concepts/graph.h> |
| 20 | 20 |
#include <lemon/list_graph.h> |
| 21 | 21 |
#include <lemon/smart_graph.h> |
| 22 | 22 |
#include <lemon/full_graph.h> |
| 23 | 23 |
#include <lemon/grid_graph.h> |
| 24 | 24 |
#include <lemon/hypercube_graph.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "test_tools.h" |
| 27 | 27 |
#include "graph_test.h" |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| 30 | 30 |
using namespace lemon::concepts; |
| 31 | 31 |
|
| 32 | 32 |
template <class Graph> |
| 33 | 33 |
void checkGraphBuild() {
|
| 34 | 34 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 35 | 35 |
|
| 36 | 36 |
Graph G; |
| 37 | 37 |
checkGraphNodeList(G, 0); |
| 38 | 38 |
checkGraphEdgeList(G, 0); |
| 39 | 39 |
checkGraphArcList(G, 0); |
| 40 | 40 |
|
| 41 | 41 |
Node |
| 42 | 42 |
n1 = G.addNode(), |
| 43 | 43 |
n2 = G.addNode(), |
| 44 | 44 |
n3 = G.addNode(); |
| 45 | 45 |
checkGraphNodeList(G, 3); |
| 46 | 46 |
checkGraphEdgeList(G, 0); |
| 47 | 47 |
checkGraphArcList(G, 0); |
| 48 | 48 |
|
| 49 | 49 |
Edge e1 = G.addEdge(n1, n2); |
| 50 | 50 |
check((G.u(e1) == n1 && G.v(e1) == n2) || (G.u(e1) == n2 && G.v(e1) == n1), |
| 51 | 51 |
"Wrong edge"); |
| 52 | 52 |
|
| 53 | 53 |
checkGraphNodeList(G, 3); |
| 54 | 54 |
checkGraphEdgeList(G, 1); |
| 55 | 55 |
checkGraphArcList(G, 2); |
| 56 | 56 |
|
| 57 | 57 |
checkGraphIncEdgeArcLists(G, n1, 1); |
| 58 | 58 |
checkGraphIncEdgeArcLists(G, n2, 1); |
| 59 | 59 |
checkGraphIncEdgeArcLists(G, n3, 0); |
| 60 | 60 |
|
| 61 | 61 |
checkGraphConEdgeList(G, 1); |
| 62 | 62 |
checkGraphConArcList(G, 2); |
| 63 | 63 |
|
| 64 | 64 |
Edge e2 = G.addEdge(n2, n1), |
| 65 | 65 |
e3 = G.addEdge(n2, n3); |
| 66 | 66 |
|
| 67 | 67 |
checkGraphNodeList(G, 3); |
| 68 | 68 |
checkGraphEdgeList(G, 3); |
| 69 | 69 |
checkGraphArcList(G, 6); |
| 70 | 70 |
|
| 71 | 71 |
checkGraphIncEdgeArcLists(G, n1, 2); |
| 72 | 72 |
checkGraphIncEdgeArcLists(G, n2, 3); |
| 73 | 73 |
checkGraphIncEdgeArcLists(G, n3, 1); |
| 74 | 74 |
|
| 75 | 75 |
checkGraphConEdgeList(G, 3); |
| 76 | 76 |
checkGraphConArcList(G, 6); |
| 77 | 77 |
|
| 78 | 78 |
checkArcDirections(G); |
| 79 | 79 |
|
| 80 | 80 |
checkNodeIds(G); |
| 81 | 81 |
checkArcIds(G); |
| 82 | 82 |
checkEdgeIds(G); |
| 83 | 83 |
checkGraphNodeMap(G); |
| 84 | 84 |
checkGraphArcMap(G); |
| 85 | 85 |
checkGraphEdgeMap(G); |
| 86 | 86 |
} |
| 87 | 87 |
|
| 88 | 88 |
template <class Graph> |
| 89 | 89 |
void checkGraphAlter() {
|
| 90 | 90 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 91 | 91 |
|
| 92 | 92 |
Graph G; |
| 93 | 93 |
Node n1 = G.addNode(), n2 = G.addNode(), |
| 94 | 94 |
n3 = G.addNode(), n4 = G.addNode(); |
| 95 | 95 |
Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1), |
| 96 | 96 |
e3 = G.addEdge(n2, n3), e4 = G.addEdge(n1, n4), |
| 97 | 97 |
e5 = G.addEdge(n4, n3); |
| 98 | 98 |
|
| 99 | 99 |
checkGraphNodeList(G, 4); |
| 100 | 100 |
checkGraphEdgeList(G, 5); |
| 101 | 101 |
checkGraphArcList(G, 10); |
| 102 | 102 |
|
| 103 | 103 |
// Check changeU() and changeV() |
| 104 | 104 |
if (G.u(e2) == n2) {
|
| 105 | 105 |
G.changeU(e2, n3); |
| 106 | 106 |
} else {
|
| 107 | 107 |
G.changeV(e2, n3); |
| 108 | 108 |
} |
| 109 | 109 |
|
| 110 | 110 |
checkGraphNodeList(G, 4); |
| 111 | 111 |
checkGraphEdgeList(G, 5); |
| 112 | 112 |
checkGraphArcList(G, 10); |
| 113 | 113 |
|
| 114 | 114 |
checkGraphIncEdgeArcLists(G, n1, 3); |
| 115 | 115 |
checkGraphIncEdgeArcLists(G, n2, 2); |
| 116 | 116 |
checkGraphIncEdgeArcLists(G, n3, 3); |
| 117 | 117 |
checkGraphIncEdgeArcLists(G, n4, 2); |
| 118 | 118 |
|
| 119 | 119 |
checkGraphConEdgeList(G, 5); |
| 120 | 120 |
checkGraphConArcList(G, 10); |
| 121 | 121 |
|
| 122 | 122 |
if (G.u(e2) == n1) {
|
| 123 | 123 |
G.changeU(e2, n2); |
| 124 | 124 |
} else {
|
| 125 | 125 |
G.changeV(e2, n2); |
| 126 | 126 |
} |
| 127 | 127 |
|
| 128 | 128 |
checkGraphNodeList(G, 4); |
| 129 | 129 |
checkGraphEdgeList(G, 5); |
| 130 | 130 |
checkGraphArcList(G, 10); |
| 131 | 131 |
|
| 132 | 132 |
checkGraphIncEdgeArcLists(G, n1, 2); |
| 133 | 133 |
checkGraphIncEdgeArcLists(G, n2, 3); |
| 134 | 134 |
checkGraphIncEdgeArcLists(G, n3, 3); |
| 135 | 135 |
checkGraphIncEdgeArcLists(G, n4, 2); |
| 136 | 136 |
|
| 137 | 137 |
checkGraphConEdgeList(G, 5); |
| 138 | 138 |
checkGraphConArcList(G, 10); |
| 139 | 139 |
|
| 140 | 140 |
// Check contract() |
| 141 | 141 |
G.contract(n1, n4, false); |
| 142 | 142 |
|
| 143 | 143 |
checkGraphNodeList(G, 3); |
| 144 | 144 |
checkGraphEdgeList(G, 5); |
| 145 | 145 |
checkGraphArcList(G, 10); |
| 146 | 146 |
|
| 147 | 147 |
checkGraphIncEdgeArcLists(G, n1, 4); |
| 148 | 148 |
checkGraphIncEdgeArcLists(G, n2, 3); |
| 149 | 149 |
checkGraphIncEdgeArcLists(G, n3, 3); |
| 150 | 150 |
|
| 151 | 151 |
checkGraphConEdgeList(G, 5); |
| 152 | 152 |
checkGraphConArcList(G, 10); |
| 153 | 153 |
|
| 154 | 154 |
G.contract(n2, n3); |
| 155 | 155 |
|
| 156 | 156 |
checkGraphNodeList(G, 2); |
| 157 | 157 |
checkGraphEdgeList(G, 3); |
| 158 | 158 |
checkGraphArcList(G, 6); |
| 159 | 159 |
|
| 160 | 160 |
checkGraphIncEdgeArcLists(G, n1, 4); |
| 161 | 161 |
checkGraphIncEdgeArcLists(G, n2, 2); |
| 162 | 162 |
|
| 163 | 163 |
checkGraphConEdgeList(G, 3); |
| 164 | 164 |
checkGraphConArcList(G, 6); |
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 | 167 |
template <class Graph> |
| 168 | 168 |
void checkGraphErase() {
|
| 169 | 169 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 170 | 170 |
|
| 171 | 171 |
Graph G; |
| 172 | 172 |
Node n1 = G.addNode(), n2 = G.addNode(), |
| 173 | 173 |
n3 = G.addNode(), n4 = G.addNode(); |
| 174 | 174 |
Edge e1 = G.addEdge(n1, n2), e2 = G.addEdge(n2, n1), |
| 175 | 175 |
e3 = G.addEdge(n2, n3), e4 = G.addEdge(n1, n4), |
| 176 | 176 |
e5 = G.addEdge(n4, n3); |
| 177 | 177 |
|
| 178 | 178 |
// Check edge deletion |
| 179 | 179 |
G.erase(e2); |
| 180 | 180 |
|
| 181 | 181 |
checkGraphNodeList(G, 4); |
| 182 | 182 |
checkGraphEdgeList(G, 4); |
| 183 | 183 |
checkGraphArcList(G, 8); |
| 184 | 184 |
|
| 185 | 185 |
checkGraphIncEdgeArcLists(G, n1, 2); |
| 186 | 186 |
checkGraphIncEdgeArcLists(G, n2, 2); |
| 187 | 187 |
checkGraphIncEdgeArcLists(G, n3, 2); |
| 188 | 188 |
checkGraphIncEdgeArcLists(G, n4, 2); |
| 189 | 189 |
|
| 190 | 190 |
checkGraphConEdgeList(G, 4); |
| 191 | 191 |
checkGraphConArcList(G, 8); |
| 192 | 192 |
|
| 193 | 193 |
// Check node deletion |
| 194 | 194 |
G.erase(n3); |
| 195 | 195 |
|
| 196 | 196 |
checkGraphNodeList(G, 3); |
| 197 | 197 |
checkGraphEdgeList(G, 2); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_TEST_GRAPH_TEST_H |
| 20 | 20 |
#define LEMON_TEST_GRAPH_TEST_H |
| 21 | 21 |
|
| 22 | 22 |
#include <set> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/core.h> |
| 25 | 25 |
#include <lemon/maps.h> |
| 26 | 26 |
|
| 27 | 27 |
#include "test_tools.h" |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
template<class Graph> |
| 32 | 32 |
void checkGraphNodeList(const Graph &G, int cnt) |
| 33 | 33 |
{
|
| 34 | 34 |
typename Graph::NodeIt n(G); |
| 35 | 35 |
for(int i=0;i<cnt;i++) {
|
| 36 | 36 |
check(n!=INVALID,"Wrong Node list linking."); |
| 37 | 37 |
++n; |
| 38 | 38 |
} |
| 39 | 39 |
check(n==INVALID,"Wrong Node list linking."); |
| 40 | 40 |
check(countNodes(G)==cnt,"Wrong Node number."); |
| 41 | 41 |
} |
| 42 | 42 |
|
| 43 | 43 |
template<class Graph> |
| 44 | 44 |
void checkGraphArcList(const Graph &G, int cnt) |
| 45 | 45 |
{
|
| 46 | 46 |
typename Graph::ArcIt e(G); |
| 47 | 47 |
for(int i=0;i<cnt;i++) {
|
| 48 | 48 |
check(e!=INVALID,"Wrong Arc list linking."); |
| 49 | 49 |
check(G.oppositeNode(G.source(e), e) == G.target(e), |
| 50 | 50 |
"Wrong opposite node"); |
| 51 | 51 |
check(G.oppositeNode(G.target(e), e) == G.source(e), |
| 52 | 52 |
"Wrong opposite node"); |
| 53 | 53 |
++e; |
| 54 | 54 |
} |
| 55 | 55 |
check(e==INVALID,"Wrong Arc list linking."); |
| 56 | 56 |
check(countArcs(G)==cnt,"Wrong Arc number."); |
| 57 | 57 |
} |
| 58 | 58 |
|
| 59 | 59 |
template<class Graph> |
| 60 | 60 |
void checkGraphOutArcList(const Graph &G, typename Graph::Node n, int cnt) |
| 61 | 61 |
{
|
| 62 | 62 |
typename Graph::OutArcIt e(G,n); |
| 63 | 63 |
for(int i=0;i<cnt;i++) {
|
| 64 | 64 |
check(e!=INVALID,"Wrong OutArc list linking."); |
| 65 | 65 |
check(n==G.source(e),"Wrong OutArc list linking."); |
| 66 | 66 |
check(n==G.baseNode(e),"Wrong OutArc list linking."); |
| 67 | 67 |
check(G.target(e)==G.runningNode(e),"Wrong OutArc list linking."); |
| 68 | 68 |
++e; |
| 69 | 69 |
} |
| 70 | 70 |
check(e==INVALID,"Wrong OutArc list linking."); |
| 71 | 71 |
check(countOutArcs(G,n)==cnt,"Wrong OutArc number."); |
| 72 | 72 |
} |
| 73 | 73 |
|
| 74 | 74 |
template<class Graph> |
| 75 | 75 |
void checkGraphInArcList(const Graph &G, typename Graph::Node n, int cnt) |
| 76 | 76 |
{
|
| 77 | 77 |
typename Graph::InArcIt e(G,n); |
| 78 | 78 |
for(int i=0;i<cnt;i++) {
|
| 79 | 79 |
check(e!=INVALID,"Wrong InArc list linking."); |
| 80 | 80 |
check(n==G.target(e),"Wrong InArc list linking."); |
| 81 | 81 |
check(n==G.baseNode(e),"Wrong OutArc list linking."); |
| 82 | 82 |
check(G.source(e)==G.runningNode(e),"Wrong OutArc list linking."); |
| 83 | 83 |
++e; |
| 84 | 84 |
} |
| 85 | 85 |
check(e==INVALID,"Wrong InArc list linking."); |
| 86 | 86 |
check(countInArcs(G,n)==cnt,"Wrong InArc number."); |
| 87 | 87 |
} |
| 88 | 88 |
|
| 89 | 89 |
template<class Graph> |
| 90 | 90 |
void checkGraphEdgeList(const Graph &G, int cnt) |
| 91 | 91 |
{
|
| 92 | 92 |
typename Graph::EdgeIt e(G); |
| 93 | 93 |
for(int i=0;i<cnt;i++) {
|
| 94 | 94 |
check(e!=INVALID,"Wrong Edge list linking."); |
| 95 | 95 |
check(G.oppositeNode(G.u(e), e) == G.v(e), "Wrong opposite node"); |
| 96 | 96 |
check(G.oppositeNode(G.v(e), e) == G.u(e), "Wrong opposite node"); |
| 97 | 97 |
++e; |
| 98 | 98 |
} |
| 99 | 99 |
check(e==INVALID,"Wrong Edge list linking."); |
| 100 | 100 |
check(countEdges(G)==cnt,"Wrong Edge number."); |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 | 103 |
template<class Graph> |
| 104 | 104 |
void checkGraphIncEdgeList(const Graph &G, typename Graph::Node n, int cnt) |
| 105 | 105 |
{
|
| 106 | 106 |
typename Graph::IncEdgeIt e(G,n); |
| 107 | 107 |
for(int i=0;i<cnt;i++) {
|
| 108 | 108 |
check(e!=INVALID,"Wrong IncEdge list linking."); |
| 109 | 109 |
check(n==G.u(e) || n==G.v(e),"Wrong IncEdge list linking."); |
| 110 | 110 |
check(n==G.baseNode(e),"Wrong OutArc list linking."); |
| 111 | 111 |
check(G.u(e)==G.runningNode(e) || G.v(e)==G.runningNode(e), |
| 112 | 112 |
"Wrong OutArc list linking."); |
| 113 | 113 |
++e; |
| 114 | 114 |
} |
| 115 | 115 |
check(e==INVALID,"Wrong IncEdge list linking."); |
| 116 | 116 |
check(countIncEdges(G,n)==cnt,"Wrong IncEdge number."); |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
template <class Graph> |
| 120 | 120 |
void checkGraphIncEdgeArcLists(const Graph &G, typename Graph::Node n, |
| 121 | 121 |
int cnt) |
| 122 | 122 |
{
|
| 123 | 123 |
checkGraphIncEdgeList(G, n, cnt); |
| 124 | 124 |
checkGraphOutArcList(G, n, cnt); |
| 125 | 125 |
checkGraphInArcList(G, n, cnt); |
| 126 | 126 |
} |
| 127 | 127 |
|
| 128 | 128 |
template <class Graph> |
| 129 | 129 |
void checkGraphConArcList(const Graph &G, int cnt) {
|
| 130 | 130 |
int i = 0; |
| 131 | 131 |
for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
|
| 132 | 132 |
for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
|
| 133 | 133 |
for (ConArcIt<Graph> a(G, u, v); a != INVALID; ++a) {
|
| 134 | 134 |
check(G.source(a) == u, "Wrong iterator."); |
| 135 | 135 |
check(G.target(a) == v, "Wrong iterator."); |
| 136 | 136 |
++i; |
| 137 | 137 |
} |
| 138 | 138 |
} |
| 139 | 139 |
} |
| 140 | 140 |
check(cnt == i, "Wrong iterator."); |
| 141 | 141 |
} |
| 142 | 142 |
|
| 143 | 143 |
template <class Graph> |
| 144 | 144 |
void checkGraphConEdgeList(const Graph &G, int cnt) {
|
| 145 | 145 |
int i = 0; |
| 146 | 146 |
for (typename Graph::NodeIt u(G); u != INVALID; ++u) {
|
| 147 | 147 |
for (typename Graph::NodeIt v(G); v != INVALID; ++v) {
|
| 148 | 148 |
for (ConEdgeIt<Graph> e(G, u, v); e != INVALID; ++e) {
|
| 149 | 149 |
check((G.u(e) == u && G.v(e) == v) || |
| 150 | 150 |
(G.u(e) == v && G.v(e) == u), "Wrong iterator."); |
| 151 | 151 |
i += u == v ? 2 : 1; |
| 152 | 152 |
} |
| 153 | 153 |
} |
| 154 | 154 |
} |
| 155 | 155 |
check(2 * cnt == i, "Wrong iterator."); |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
template <typename Graph> |
| 159 | 159 |
void checkArcDirections(const Graph& G) {
|
| 160 | 160 |
for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
|
| 161 | 161 |
check(G.source(a) == G.target(G.oppositeArc(a)), "Wrong direction"); |
| 162 | 162 |
check(G.target(a) == G.source(G.oppositeArc(a)), "Wrong direction"); |
| 163 | 163 |
check(G.direct(a, G.direction(a)) == a, "Wrong direction"); |
| 164 | 164 |
} |
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 | 167 |
template <typename Graph> |
| 168 | 168 |
void checkNodeIds(const Graph& G) {
|
| 169 | 169 |
std::set<int> values; |
| 170 | 170 |
for (typename Graph::NodeIt n(G); n != INVALID; ++n) {
|
| 171 | 171 |
check(G.nodeFromId(G.id(n)) == n, "Wrong id"); |
| 172 | 172 |
check(values.find(G.id(n)) == values.end(), "Wrong id"); |
| 173 | 173 |
check(G.id(n) <= G.maxNodeId(), "Wrong maximum id"); |
| 174 | 174 |
values.insert(G.id(n)); |
| 175 | 175 |
} |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
template <typename Graph> |
| 179 | 179 |
void checkArcIds(const Graph& G) {
|
| 180 | 180 |
std::set<int> values; |
| 181 | 181 |
for (typename Graph::ArcIt a(G); a != INVALID; ++a) {
|
| 182 | 182 |
check(G.arcFromId(G.id(a)) == a, "Wrong id"); |
| 183 | 183 |
check(values.find(G.id(a)) == values.end(), "Wrong id"); |
| 184 | 184 |
check(G.id(a) <= G.maxArcId(), "Wrong maximum id"); |
| 185 | 185 |
values.insert(G.id(a)); |
| 186 | 186 |
} |
| 187 | 187 |
} |
| 188 | 188 |
|
| 189 | 189 |
template <typename Graph> |
| 190 | 190 |
void checkEdgeIds(const Graph& G) {
|
| 191 | 191 |
std::set<int> values; |
| 192 | 192 |
for (typename Graph::EdgeIt e(G); e != INVALID; ++e) {
|
| 193 | 193 |
check(G.edgeFromId(G.id(e)) == e, "Wrong id"); |
| 194 | 194 |
check(values.find(G.id(e)) == values.end(), "Wrong id"); |
| 195 | 195 |
check(G.id(e) <= G.maxEdgeId(), "Wrong maximum id"); |
| 196 | 196 |
values.insert(G.id(e)); |
| 197 | 197 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <cstdlib> |
| 20 | 20 |
#include <ctime> |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/random.h> |
| 23 | 23 |
#include <lemon/list_graph.h> |
| 24 | 24 |
#include <lemon/smart_graph.h> |
| 25 | 25 |
#include <lemon/maps.h> |
| 26 | 26 |
|
| 27 | 27 |
#include "graph_test.h" |
| 28 | 28 |
#include "test_tools.h" |
| 29 | 29 |
|
| 30 | 30 |
using namespace lemon; |
| 31 | 31 |
|
| 32 | 32 |
template <typename Digraph> |
| 33 | 33 |
void checkFindArcs() {
|
| 34 | 34 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 35 | 35 |
|
| 36 | 36 |
{
|
| 37 | 37 |
Digraph digraph; |
| 38 | 38 |
for (int i = 0; i < 10; ++i) {
|
| 39 | 39 |
digraph.addNode(); |
| 40 | 40 |
} |
| 41 | 41 |
DescriptorMap<Digraph, Node> nodes(digraph); |
| 42 | 42 |
typename DescriptorMap<Digraph, Node>::InverseMap invNodes(nodes); |
| 43 | 43 |
for (int i = 0; i < 100; ++i) {
|
| 44 | 44 |
int src = rnd[invNodes.size()]; |
| 45 | 45 |
int trg = rnd[invNodes.size()]; |
| 46 | 46 |
digraph.addArc(invNodes[src], invNodes[trg]); |
| 47 | 47 |
} |
| 48 | 48 |
typename Digraph::template ArcMap<bool> found(digraph, false); |
| 49 | 49 |
DescriptorMap<Digraph, Arc> arcs(digraph); |
| 50 | 50 |
for (NodeIt src(digraph); src != INVALID; ++src) {
|
| 51 | 51 |
for (NodeIt trg(digraph); trg != INVALID; ++trg) {
|
| 52 | 52 |
for (ConArcIt<Digraph> con(digraph, src, trg); con != INVALID; ++con) {
|
| 53 | 53 |
check(digraph.source(con) == src, "Wrong source."); |
| 54 | 54 |
check(digraph.target(con) == trg, "Wrong target."); |
| 55 | 55 |
check(found[con] == false, "The arc found already."); |
| 56 | 56 |
found[con] = true; |
| 57 | 57 |
} |
| 58 | 58 |
} |
| 59 | 59 |
} |
| 60 | 60 |
for (ArcIt it(digraph); it != INVALID; ++it) {
|
| 61 | 61 |
check(found[it] == true, "The arc is not found."); |
| 62 | 62 |
} |
| 63 | 63 |
} |
| 64 | 64 |
|
| 65 | 65 |
{
|
| 66 | 66 |
int num = 5; |
| 67 | 67 |
Digraph fg; |
| 68 | 68 |
std::vector<Node> nodes; |
| 69 | 69 |
for (int i = 0; i < num; ++i) {
|
| 70 | 70 |
nodes.push_back(fg.addNode()); |
| 71 | 71 |
} |
| 72 | 72 |
for (int i = 0; i < num * num; ++i) {
|
| 73 | 73 |
fg.addArc(nodes[i / num], nodes[i % num]); |
| 74 | 74 |
} |
| 75 | 75 |
check(countNodes(fg) == num, "Wrong node number."); |
| 76 | 76 |
check(countArcs(fg) == num*num, "Wrong arc number."); |
| 77 | 77 |
for (NodeIt src(fg); src != INVALID; ++src) {
|
| 78 | 78 |
for (NodeIt trg(fg); trg != INVALID; ++trg) {
|
| 79 | 79 |
ConArcIt<Digraph> con(fg, src, trg); |
| 80 | 80 |
check(con != INVALID, "There is no connecting arc."); |
| 81 | 81 |
check(fg.source(con) == src, "Wrong source."); |
| 82 | 82 |
check(fg.target(con) == trg, "Wrong target."); |
| 83 | 83 |
check(++con == INVALID, "There is more connecting arc."); |
| 84 | 84 |
} |
| 85 | 85 |
} |
| 86 | 86 |
ArcLookUp<Digraph> al1(fg); |
| 87 | 87 |
DynArcLookUp<Digraph> al2(fg); |
| 88 | 88 |
AllArcLookUp<Digraph> al3(fg); |
| 89 | 89 |
for (NodeIt src(fg); src != INVALID; ++src) {
|
| 90 | 90 |
for (NodeIt trg(fg); trg != INVALID; ++trg) {
|
| 91 | 91 |
Arc con1 = al1(src, trg); |
| 92 | 92 |
Arc con2 = al2(src, trg); |
| 93 | 93 |
Arc con3 = al3(src, trg); |
| 94 | 94 |
Arc con4 = findArc(fg, src, trg); |
| 95 | 95 |
check(con1 == con2 && con2 == con3 && con3 == con4, |
| 96 | 96 |
"Different results.") |
| 97 | 97 |
check(con1 != INVALID, "There is no connecting arc."); |
| 98 | 98 |
check(fg.source(con1) == src, "Wrong source."); |
| 99 | 99 |
check(fg.target(con1) == trg, "Wrong target."); |
| 100 | 100 |
check(al3(src, trg, con3) == INVALID, |
| 101 | 101 |
"There is more connecting arc."); |
| 102 | 102 |
check(findArc(fg, src, trg, con4) == INVALID, |
| 103 | 103 |
"There is more connecting arc."); |
| 104 | 104 |
} |
| 105 | 105 |
} |
| 106 | 106 |
} |
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 | 109 |
template <typename Graph> |
| 110 | 110 |
void checkFindEdges() {
|
| 111 | 111 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 112 | 112 |
Graph graph; |
| 113 | 113 |
for (int i = 0; i < 10; ++i) {
|
| 114 | 114 |
graph.addNode(); |
| 115 | 115 |
} |
| 116 | 116 |
DescriptorMap<Graph, Node> nodes(graph); |
| 117 | 117 |
typename DescriptorMap<Graph, Node>::InverseMap invNodes(nodes); |
| 118 | 118 |
for (int i = 0; i < 100; ++i) {
|
| 119 | 119 |
int src = rnd[invNodes.size()]; |
| 120 | 120 |
int trg = rnd[invNodes.size()]; |
| 121 | 121 |
graph.addEdge(invNodes[src], invNodes[trg]); |
| 122 | 122 |
} |
| 123 | 123 |
typename Graph::template EdgeMap<int> found(graph, 0); |
| 124 | 124 |
DescriptorMap<Graph, Edge> edges(graph); |
| 125 | 125 |
for (NodeIt src(graph); src != INVALID; ++src) {
|
| 126 | 126 |
for (NodeIt trg(graph); trg != INVALID; ++trg) {
|
| 127 | 127 |
for (ConEdgeIt<Graph> con(graph, src, trg); con != INVALID; ++con) {
|
| 128 | 128 |
check( (graph.u(con) == src && graph.v(con) == trg) || |
| 129 | 129 |
(graph.v(con) == src && graph.u(con) == trg), |
| 130 | 130 |
"Wrong end nodes."); |
| 131 | 131 |
++found[con]; |
| 132 | 132 |
check(found[con] <= 2, "The edge found more than twice."); |
| 133 | 133 |
} |
| 134 | 134 |
} |
| 135 | 135 |
} |
| 136 | 136 |
for (EdgeIt it(graph); it != INVALID; ++it) {
|
| 137 | 137 |
check( (graph.u(it) != graph.v(it) && found[it] == 2) || |
| 138 | 138 |
(graph.u(it) == graph.v(it) && found[it] == 1), |
| 139 | 139 |
"The edge is not found correctly."); |
| 140 | 140 |
} |
| 141 | 141 |
} |
| 142 | 142 |
|
| 143 | 143 |
template <class Digraph> |
| 144 | 144 |
void checkDeg() |
| 145 | 145 |
{
|
| 146 | 146 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 147 | 147 |
|
| 148 | 148 |
const int nodeNum = 10; |
| 149 | 149 |
const int arcNum = 100; |
| 150 | 150 |
Digraph digraph; |
| 151 | 151 |
InDegMap<Digraph> inDeg(digraph); |
| 152 | 152 |
OutDegMap<Digraph> outDeg(digraph); |
| 153 | 153 |
std::vector<Node> nodes(nodeNum); |
| 154 | 154 |
for (int i = 0; i < nodeNum; ++i) {
|
| 155 | 155 |
nodes[i] = digraph.addNode(); |
| 156 | 156 |
} |
| 157 | 157 |
std::vector<Arc> arcs(arcNum); |
| 158 | 158 |
for (int i = 0; i < arcNum; ++i) {
|
| 159 | 159 |
arcs[i] = digraph.addArc(nodes[rnd[nodeNum]], nodes[rnd[nodeNum]]); |
| 160 | 160 |
} |
| 161 | 161 |
for (int i = 0; i < nodeNum; ++i) {
|
| 162 | 162 |
check(inDeg[nodes[i]] == countInArcs(digraph, nodes[i]), |
| 163 | 163 |
"Wrong in degree map"); |
| 164 | 164 |
} |
| 165 | 165 |
for (int i = 0; i < nodeNum; ++i) {
|
| 166 | 166 |
check(outDeg[nodes[i]] == countOutArcs(digraph, nodes[i]), |
| 167 | 167 |
"Wrong out degree map"); |
| 168 | 168 |
} |
| 169 | 169 |
} |
| 170 | 170 |
|
| 171 | 171 |
template <class Digraph> |
| 172 | 172 |
void checkSnapDeg() |
| 173 | 173 |
{
|
| 174 | 174 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 175 | 175 |
|
| 176 | 176 |
Digraph g; |
| 177 | 177 |
Node n1=g.addNode(); |
| 178 | 178 |
Node n2=g.addNode(); |
| 179 | 179 |
|
| 180 | 180 |
InDegMap<Digraph> ind(g); |
| 181 | 181 |
|
| 182 | 182 |
g.addArc(n1,n2); |
| 183 | 183 |
|
| 184 | 184 |
typename Digraph::Snapshot snap(g); |
| 185 | 185 |
|
| 186 | 186 |
OutDegMap<Digraph> outd(g); |
| 187 | 187 |
|
| 188 | 188 |
check(ind[n1]==0 && ind[n2]==1, "Wrong InDegMap value."); |
| 189 | 189 |
check(outd[n1]==1 && outd[n2]==0, "Wrong OutDegMap value."); |
| 190 | 190 |
|
| 191 | 191 |
g.addArc(n1,n2); |
| 192 | 192 |
g.addArc(n2,n1); |
| 193 | 193 |
|
| 194 | 194 |
check(ind[n1]==1 && ind[n2]==2, "Wrong InDegMap value."); |
| 195 | 195 |
check(outd[n1]==2 && outd[n2]==1, "Wrong OutDegMap value."); |
| 196 | 196 |
|
| 197 | 197 |
snap.restore(); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <sstream> |
| 20 | 20 |
|
| 21 | 21 |
#include <lemon/smart_graph.h> |
| 22 | 22 |
#include <lemon/hao_orlin.h> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/lgf_reader.h> |
| 25 | 25 |
#include "test_tools.h" |
| 26 | 26 |
|
| 27 | 27 |
using namespace lemon; |
| 28 | 28 |
using namespace std; |
| 29 | 29 |
|
| 30 | 30 |
const std::string lgf = |
| 31 | 31 |
"@nodes\n" |
| 32 | 32 |
"label\n" |
| 33 | 33 |
"0\n" |
| 34 | 34 |
"1\n" |
| 35 | 35 |
"2\n" |
| 36 | 36 |
"3\n" |
| 37 | 37 |
"4\n" |
| 38 | 38 |
"5\n" |
| 39 | 39 |
"@edges\n" |
| 40 | 40 |
" label capacity\n" |
| 41 | 41 |
"0 1 0 2\n" |
| 42 | 42 |
"1 2 1 2\n" |
| 43 | 43 |
"2 0 2 2\n" |
| 44 | 44 |
"3 4 3 2\n" |
| 45 | 45 |
"4 5 4 2\n" |
| 46 | 46 |
"5 3 5 2\n" |
| 47 | 47 |
"2 3 6 3\n"; |
| 48 | 48 |
|
| 49 | 49 |
int main() {
|
| 50 | 50 |
SmartGraph graph; |
| 51 | 51 |
SmartGraph::EdgeMap<int> capacity(graph); |
| 52 | 52 |
|
| 53 | 53 |
istringstream lgfs(lgf); |
| 54 | 54 |
graphReader(graph, lgfs). |
| 55 | 55 |
edgeMap("capacity", capacity).run();
|
| 56 | 56 |
|
| 57 | 57 |
HaoOrlin<SmartGraph, SmartGraph::EdgeMap<int> > ho(graph, capacity); |
| 58 | 58 |
ho.run(); |
| 59 | 59 |
|
| 60 | 60 |
check(ho.minCutValue() == 3, "Wrong cut value"); |
| 61 | 61 |
|
| 62 | 62 |
return 0; |
| 63 | 63 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <iostream> |
| 20 | 20 |
#include <fstream> |
| 21 | 21 |
#include <string> |
| 22 | 22 |
#include <vector> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/concept_check.h> |
| 25 | 25 |
#include <lemon/concepts/heap.h> |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/smart_graph.h> |
| 28 | 28 |
|
| 29 | 29 |
#include <lemon/lgf_reader.h> |
| 30 | 30 |
#include <lemon/dijkstra.h> |
| 31 | 31 |
#include <lemon/maps.h> |
| 32 | 32 |
|
| 33 | 33 |
#include <lemon/bin_heap.h> |
| 34 | 34 |
|
| 35 | 35 |
#include "test_tools.h" |
| 36 | 36 |
|
| 37 | 37 |
using namespace lemon; |
| 38 | 38 |
using namespace lemon::concepts; |
| 39 | 39 |
|
| 40 | 40 |
typedef ListDigraph Digraph; |
| 41 | 41 |
DIGRAPH_TYPEDEFS(Digraph); |
| 42 | 42 |
|
| 43 | 43 |
char test_lgf[] = |
| 44 | 44 |
"@nodes\n" |
| 45 | 45 |
"label\n" |
| 46 | 46 |
"0\n" |
| 47 | 47 |
"1\n" |
| 48 | 48 |
"2\n" |
| 49 | 49 |
"3\n" |
| 50 | 50 |
"4\n" |
| 51 | 51 |
"5\n" |
| 52 | 52 |
"6\n" |
| 53 | 53 |
"7\n" |
| 54 | 54 |
"8\n" |
| 55 | 55 |
"9\n" |
| 56 | 56 |
"@arcs\n" |
| 57 | 57 |
" label capacity\n" |
| 58 | 58 |
"0 5 0 94\n" |
| 59 | 59 |
"3 9 1 11\n" |
| 60 | 60 |
"8 7 2 83\n" |
| 61 | 61 |
"1 2 3 94\n" |
| 62 | 62 |
"5 7 4 35\n" |
| 63 | 63 |
"7 4 5 84\n" |
| 64 | 64 |
"9 5 6 38\n" |
| 65 | 65 |
"0 4 7 96\n" |
| 66 | 66 |
"6 7 8 6\n" |
| 67 | 67 |
"3 1 9 27\n" |
| 68 | 68 |
"5 2 10 77\n" |
| 69 | 69 |
"5 6 11 69\n" |
| 70 | 70 |
"6 5 12 41\n" |
| 71 | 71 |
"4 6 13 70\n" |
| 72 | 72 |
"3 2 14 45\n" |
| 73 | 73 |
"7 9 15 93\n" |
| 74 | 74 |
"5 9 16 50\n" |
| 75 | 75 |
"9 0 17 94\n" |
| 76 | 76 |
"9 6 18 67\n" |
| 77 | 77 |
"0 9 19 86\n" |
| 78 | 78 |
"@attributes\n" |
| 79 | 79 |
"source 3\n"; |
| 80 | 80 |
|
| 81 | 81 |
int test_seq[] = { 2, 28, 19, 27, 33, 25, 13, 41, 10, 26, 1, 9, 4, 34};
|
| 82 | 82 |
int test_inc[] = {20, 28, 34, 16, 0, 46, 44, 0, 42, 32, 14, 8, 6, 37};
|
| 83 | 83 |
|
| 84 | 84 |
int test_len = sizeof(test_seq) / sizeof(test_seq[0]); |
| 85 | 85 |
|
| 86 | 86 |
template <typename Heap> |
| 87 | 87 |
void heapSortTest() {
|
| 88 | 88 |
RangeMap<int> map(test_len, -1); |
| 89 | 89 |
|
| 90 | 90 |
Heap heap(map); |
| 91 | 91 |
|
| 92 | 92 |
std::vector<int> v(test_len); |
| 93 | 93 |
|
| 94 | 94 |
for (int i = 0; i < test_len; ++i) {
|
| 95 | 95 |
v[i] = test_seq[i]; |
| 96 | 96 |
heap.push(i, v[i]); |
| 97 | 97 |
} |
| 98 | 98 |
std::sort(v.begin(), v.end()); |
| 99 | 99 |
for (int i = 0; i < test_len; ++i) {
|
| 100 | 100 |
check(v[i] == heap.prio() ,"Wrong order in heap sort."); |
| 101 | 101 |
heap.pop(); |
| 102 | 102 |
} |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 | 105 |
template <typename Heap> |
| 106 | 106 |
void heapIncreaseTest() {
|
| 107 | 107 |
RangeMap<int> map(test_len, -1); |
| 108 | 108 |
|
| 109 | 109 |
Heap heap(map); |
| 110 | 110 |
|
| 111 | 111 |
std::vector<int> v(test_len); |
| 112 | 112 |
|
| 113 | 113 |
for (int i = 0; i < test_len; ++i) {
|
| 114 | 114 |
v[i] = test_seq[i]; |
| 115 | 115 |
heap.push(i, v[i]); |
| 116 | 116 |
} |
| 117 | 117 |
for (int i = 0; i < test_len; ++i) {
|
| 118 | 118 |
v[i] += test_inc[i]; |
| 119 | 119 |
heap.increase(i, v[i]); |
| 120 | 120 |
} |
| 121 | 121 |
std::sort(v.begin(), v.end()); |
| 122 | 122 |
for (int i = 0; i < test_len; ++i) {
|
| 123 | 123 |
check(v[i] == heap.prio() ,"Wrong order in heap increase test."); |
| 124 | 124 |
heap.pop(); |
| 125 | 125 |
} |
| 126 | 126 |
} |
| 127 | 127 |
|
| 128 | 128 |
|
| 129 | 129 |
|
| 130 | 130 |
template <typename Heap> |
| 131 | 131 |
void dijkstraHeapTest(const Digraph& digraph, const IntArcMap& length, |
| 132 | 132 |
Node source) {
|
| 133 | 133 |
|
| 134 | 134 |
typename Dijkstra<Digraph, IntArcMap>::template SetStandardHeap<Heap>:: |
| 135 | 135 |
Create dijkstra(digraph, length); |
| 136 | 136 |
|
| 137 | 137 |
dijkstra.run(source); |
| 138 | 138 |
|
| 139 | 139 |
for(ArcIt a(digraph); a != INVALID; ++a) {
|
| 140 | 140 |
Node s = digraph.source(a); |
| 141 | 141 |
Node t = digraph.target(a); |
| 142 | 142 |
if (dijkstra.reached(s)) {
|
| 143 | 143 |
check( dijkstra.dist(t) - dijkstra.dist(s) <= length[a], |
| 144 | 144 |
"Error in a shortest path tree!"); |
| 145 | 145 |
} |
| 146 | 146 |
} |
| 147 | 147 |
|
| 148 | 148 |
for(NodeIt n(digraph); n != INVALID; ++n) {
|
| 149 | 149 |
if ( dijkstra.reached(n) && dijkstra.predArc(n) != INVALID ) {
|
| 150 | 150 |
Arc a = dijkstra.predArc(n); |
| 151 | 151 |
Node s = digraph.source(a); |
| 152 | 152 |
check( dijkstra.dist(n) - dijkstra.dist(s) == length[a], |
| 153 | 153 |
"Error in a shortest path tree!"); |
| 154 | 154 |
} |
| 155 | 155 |
} |
| 156 | 156 |
|
| 157 | 157 |
} |
| 158 | 158 |
|
| 159 | 159 |
int main() {
|
| 160 | 160 |
|
| 161 | 161 |
typedef int Item; |
| 162 | 162 |
typedef int Prio; |
| 163 | 163 |
typedef RangeMap<int> ItemIntMap; |
| 164 | 164 |
|
| 165 | 165 |
Digraph digraph; |
| 166 | 166 |
IntArcMap length(digraph); |
| 167 | 167 |
Node source; |
| 168 | 168 |
|
| 169 | 169 |
std::istringstream input(test_lgf); |
| 170 | 170 |
digraphReader(digraph, input). |
| 171 | 171 |
arcMap("capacity", length).
|
| 172 | 172 |
node("source", source).
|
| 173 | 173 |
run(); |
| 174 | 174 |
|
| 175 | 175 |
{
|
| 176 | 176 |
typedef BinHeap<Prio, ItemIntMap> IntHeap; |
| 177 | 177 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
| 178 | 178 |
heapSortTest<IntHeap>(); |
| 179 | 179 |
heapIncreaseTest<IntHeap>(); |
| 180 | 180 |
|
| 181 | 181 |
typedef BinHeap<Prio, IntNodeMap > NodeHeap; |
| 182 | 182 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
| 183 | 183 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
| 184 | 184 |
} |
| 185 | 185 |
|
| 186 | 186 |
return 0; |
| 187 | 187 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <iostream> |
| 20 | 20 |
#include <vector> |
| 21 | 21 |
|
| 22 | 22 |
#include "test_tools.h" |
| 23 | 23 |
#include <lemon/maps.h> |
| 24 | 24 |
#include <lemon/kruskal.h> |
| 25 | 25 |
#include <lemon/list_graph.h> |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
#include <lemon/concepts/digraph.h> |
| 29 | 29 |
#include <lemon/concepts/graph.h> |
| 30 | 30 |
|
| 31 | 31 |
using namespace std; |
| 32 | 32 |
using namespace lemon; |
| 33 | 33 |
|
| 34 | 34 |
void checkCompileKruskal() |
| 35 | 35 |
{
|
| 36 | 36 |
concepts::WriteMap<concepts::Digraph::Arc,bool> w; |
| 37 | 37 |
concepts::WriteMap<concepts::Graph::Edge,bool> uw; |
| 38 | 38 |
|
| 39 | 39 |
concepts::ReadMap<concepts::Digraph::Arc,int> r; |
| 40 | 40 |
concepts::ReadMap<concepts::Graph::Edge,int> ur; |
| 41 | 41 |
|
| 42 | 42 |
concepts::Digraph g; |
| 43 | 43 |
concepts::Graph ug; |
| 44 | 44 |
|
| 45 | 45 |
kruskal(g, r, w); |
| 46 | 46 |
kruskal(ug, ur, uw); |
| 47 | 47 |
|
| 48 | 48 |
std::vector<std::pair<concepts::Digraph::Arc, int> > rs; |
| 49 | 49 |
std::vector<std::pair<concepts::Graph::Edge, int> > urs; |
| 50 | 50 |
|
| 51 | 51 |
kruskal(g, rs, w); |
| 52 | 52 |
kruskal(ug, urs, uw); |
| 53 | 53 |
|
| 54 | 54 |
std::vector<concepts::Digraph::Arc> ws; |
| 55 | 55 |
std::vector<concepts::Graph::Edge> uws; |
| 56 | 56 |
|
| 57 | 57 |
kruskal(g, r, ws.begin()); |
| 58 | 58 |
kruskal(ug, ur, uws.begin()); |
| 59 | 59 |
} |
| 60 | 60 |
|
| 61 | 61 |
int main() {
|
| 62 | 62 |
|
| 63 | 63 |
typedef ListGraph::Node Node; |
| 64 | 64 |
typedef ListGraph::Edge Edge; |
| 65 | 65 |
typedef ListGraph::NodeIt NodeIt; |
| 66 | 66 |
typedef ListGraph::ArcIt ArcIt; |
| 67 | 67 |
|
| 68 | 68 |
ListGraph G; |
| 69 | 69 |
|
| 70 | 70 |
Node s=G.addNode(); |
| 71 | 71 |
Node v1=G.addNode(); |
| 72 | 72 |
Node v2=G.addNode(); |
| 73 | 73 |
Node v3=G.addNode(); |
| 74 | 74 |
Node v4=G.addNode(); |
| 75 | 75 |
Node t=G.addNode(); |
| 76 | 76 |
|
| 77 | 77 |
Edge e1 = G.addEdge(s, v1); |
| 78 | 78 |
Edge e2 = G.addEdge(s, v2); |
| 79 | 79 |
Edge e3 = G.addEdge(v1, v2); |
| 80 | 80 |
Edge e4 = G.addEdge(v2, v1); |
| 81 | 81 |
Edge e5 = G.addEdge(v1, v3); |
| 82 | 82 |
Edge e6 = G.addEdge(v3, v2); |
| 83 | 83 |
Edge e7 = G.addEdge(v2, v4); |
| 84 | 84 |
Edge e8 = G.addEdge(v4, v3); |
| 85 | 85 |
Edge e9 = G.addEdge(v3, t); |
| 86 | 86 |
Edge e10 = G.addEdge(v4, t); |
| 87 | 87 |
|
| 88 | 88 |
typedef ListGraph::EdgeMap<int> ECostMap; |
| 89 | 89 |
typedef ListGraph::EdgeMap<bool> EBoolMap; |
| 90 | 90 |
|
| 91 | 91 |
ECostMap edge_cost_map(G, 2); |
| 92 | 92 |
EBoolMap tree_map(G); |
| 93 | 93 |
|
| 94 | 94 |
|
| 95 | 95 |
//Test with const map. |
| 96 | 96 |
check(kruskal(G, ConstMap<ListGraph::Edge,int>(2), tree_map)==10, |
| 97 | 97 |
"Total cost should be 10"); |
| 98 | 98 |
//Test with an edge map (filled with uniform costs). |
| 99 | 99 |
check(kruskal(G, edge_cost_map, tree_map)==10, |
| 100 | 100 |
"Total cost should be 10"); |
| 101 | 101 |
|
| 102 | 102 |
edge_cost_map.set(e1, -10); |
| 103 | 103 |
edge_cost_map.set(e2, -9); |
| 104 | 104 |
edge_cost_map.set(e3, -8); |
| 105 | 105 |
edge_cost_map.set(e4, -7); |
| 106 | 106 |
edge_cost_map.set(e5, -6); |
| 107 | 107 |
edge_cost_map.set(e6, -5); |
| 108 | 108 |
edge_cost_map.set(e7, -4); |
| 109 | 109 |
edge_cost_map.set(e8, -3); |
| 110 | 110 |
edge_cost_map.set(e9, -2); |
| 111 | 111 |
edge_cost_map.set(e10, -1); |
| 112 | 112 |
|
| 113 | 113 |
vector<Edge> tree_edge_vec(5); |
| 114 | 114 |
|
| 115 | 115 |
//Test with a edge map and inserter. |
| 116 | 116 |
check(kruskal(G, edge_cost_map, |
| 117 | 117 |
tree_edge_vec.begin()) |
| 118 | 118 |
==-31, |
| 119 | 119 |
"Total cost should be -31."); |
| 120 | 120 |
|
| 121 | 121 |
tree_edge_vec.clear(); |
| 122 | 122 |
|
| 123 | 123 |
check(kruskal(G, edge_cost_map, |
| 124 | 124 |
back_inserter(tree_edge_vec)) |
| 125 | 125 |
==-31, |
| 126 | 126 |
"Total cost should be -31."); |
| 127 | 127 |
|
| 128 | 128 |
// tree_edge_vec.clear(); |
| 129 | 129 |
|
| 130 | 130 |
// //The above test could also be coded like this: |
| 131 | 131 |
// check(kruskal(G, |
| 132 | 132 |
// makeKruskalMapInput(G, edge_cost_map), |
| 133 | 133 |
// makeKruskalSequenceOutput(back_inserter(tree_edge_vec))) |
| 134 | 134 |
// ==-31, |
| 135 | 135 |
// "Total cost should be -31."); |
| 136 | 136 |
|
| 137 | 137 |
check(tree_edge_vec.size()==5,"The tree should have 5 edges."); |
| 138 | 138 |
|
| 139 | 139 |
check(tree_edge_vec[0]==e1 && |
| 140 | 140 |
tree_edge_vec[1]==e2 && |
| 141 | 141 |
tree_edge_vec[2]==e5 && |
| 142 | 142 |
tree_edge_vec[3]==e7 && |
| 143 | 143 |
tree_edge_vec[4]==e9, |
| 144 | 144 |
"Wrong tree."); |
| 145 | 145 |
|
| 146 | 146 |
return 0; |
| 147 | 147 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <deque> |
| 20 | 20 |
#include <set> |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/concept_check.h> |
| 23 | 23 |
#include <lemon/concepts/maps.h> |
| 24 | 24 |
#include <lemon/maps.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "test_tools.h" |
| 27 | 27 |
|
| 28 | 28 |
using namespace lemon; |
| 29 | 29 |
using namespace lemon::concepts; |
| 30 | 30 |
|
| 31 | 31 |
struct A {};
|
| 32 | 32 |
inline bool operator<(A, A) { return true; }
|
| 33 | 33 |
struct B {};
|
| 34 | 34 |
|
| 35 | 35 |
class C {
|
| 36 | 36 |
int x; |
| 37 | 37 |
public: |
| 38 | 38 |
C(int _x) : x(_x) {}
|
| 39 | 39 |
}; |
| 40 | 40 |
|
| 41 | 41 |
class F {
|
| 42 | 42 |
public: |
| 43 | 43 |
typedef A argument_type; |
| 44 | 44 |
typedef B result_type; |
| 45 | 45 |
|
| 46 | 46 |
B operator()(const A&) const { return B(); }
|
| 47 | 47 |
private: |
| 48 | 48 |
F& operator=(const F&); |
| 49 | 49 |
}; |
| 50 | 50 |
|
| 51 | 51 |
int func(A) { return 3; }
|
| 52 | 52 |
|
| 53 | 53 |
int binc(int a, B) { return a+1; }
|
| 54 | 54 |
|
| 55 | 55 |
typedef ReadMap<A, double> DoubleMap; |
| 56 | 56 |
typedef ReadWriteMap<A, double> DoubleWriteMap; |
| 57 | 57 |
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap; |
| 58 | 58 |
|
| 59 | 59 |
typedef ReadMap<A, bool> BoolMap; |
| 60 | 60 |
typedef ReadWriteMap<A, bool> BoolWriteMap; |
| 61 | 61 |
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap; |
| 62 | 62 |
|
| 63 | 63 |
int main() |
| 64 | 64 |
{
|
| 65 | 65 |
// Map concepts |
| 66 | 66 |
checkConcept<ReadMap<A,B>, ReadMap<A,B> >(); |
| 67 | 67 |
checkConcept<ReadMap<A,C>, ReadMap<A,C> >(); |
| 68 | 68 |
checkConcept<WriteMap<A,B>, WriteMap<A,B> >(); |
| 69 | 69 |
checkConcept<WriteMap<A,C>, WriteMap<A,C> >(); |
| 70 | 70 |
checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >(); |
| 71 | 71 |
checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >(); |
| 72 | 72 |
checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >(); |
| 73 | 73 |
checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >(); |
| 74 | 74 |
|
| 75 | 75 |
// NullMap |
| 76 | 76 |
{
|
| 77 | 77 |
checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >(); |
| 78 | 78 |
NullMap<A,B> map1; |
| 79 | 79 |
NullMap<A,B> map2 = map1; |
| 80 | 80 |
map1 = nullMap<A,B>(); |
| 81 | 81 |
} |
| 82 | 82 |
|
| 83 | 83 |
// ConstMap |
| 84 | 84 |
{
|
| 85 | 85 |
checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >(); |
| 86 | 86 |
checkConcept<ReadWriteMap<A,C>, ConstMap<A,C> >(); |
| 87 | 87 |
ConstMap<A,B> map1; |
| 88 | 88 |
ConstMap<A,B> map2 = B(); |
| 89 | 89 |
ConstMap<A,B> map3 = map1; |
| 90 | 90 |
map1 = constMap<A>(B()); |
| 91 | 91 |
map1 = constMap<A,B>(); |
| 92 | 92 |
map1.setAll(B()); |
| 93 | 93 |
ConstMap<A,C> map4(C(1)); |
| 94 | 94 |
ConstMap<A,C> map5 = map4; |
| 95 | 95 |
map4 = constMap<A>(C(2)); |
| 96 | 96 |
map4.setAll(C(3)); |
| 97 | 97 |
|
| 98 | 98 |
checkConcept<ReadWriteMap<A,int>, ConstMap<A,int> >(); |
| 99 | 99 |
check(constMap<A>(10)[A()] == 10, "Something is wrong with ConstMap"); |
| 100 | 100 |
|
| 101 | 101 |
checkConcept<ReadWriteMap<A,int>, ConstMap<A,Const<int,10> > >(); |
| 102 | 102 |
ConstMap<A,Const<int,10> > map6; |
| 103 | 103 |
ConstMap<A,Const<int,10> > map7 = map6; |
| 104 | 104 |
map6 = constMap<A,int,10>(); |
| 105 | 105 |
map7 = constMap<A,Const<int,10> >(); |
| 106 | 106 |
check(map6[A()] == 10 && map7[A()] == 10, |
| 107 | 107 |
"Something is wrong with ConstMap"); |
| 108 | 108 |
} |
| 109 | 109 |
|
| 110 | 110 |
// IdentityMap |
| 111 | 111 |
{
|
| 112 | 112 |
checkConcept<ReadMap<A,A>, IdentityMap<A> >(); |
| 113 | 113 |
IdentityMap<A> map1; |
| 114 | 114 |
IdentityMap<A> map2 = map1; |
| 115 | 115 |
map1 = identityMap<A>(); |
| 116 | 116 |
|
| 117 | 117 |
checkConcept<ReadMap<double,double>, IdentityMap<double> >(); |
| 118 | 118 |
check(identityMap<double>()[1.0] == 1.0 && |
| 119 | 119 |
identityMap<double>()[3.14] == 3.14, |
| 120 | 120 |
"Something is wrong with IdentityMap"); |
| 121 | 121 |
} |
| 122 | 122 |
|
| 123 | 123 |
// RangeMap |
| 124 | 124 |
{
|
| 125 | 125 |
checkConcept<ReferenceMap<int,B,B&,const B&>, RangeMap<B> >(); |
| 126 | 126 |
RangeMap<B> map1; |
| 127 | 127 |
RangeMap<B> map2(10); |
| 128 | 128 |
RangeMap<B> map3(10,B()); |
| 129 | 129 |
RangeMap<B> map4 = map1; |
| 130 | 130 |
RangeMap<B> map5 = rangeMap<B>(); |
| 131 | 131 |
RangeMap<B> map6 = rangeMap<B>(10); |
| 132 | 132 |
RangeMap<B> map7 = rangeMap(10,B()); |
| 133 | 133 |
|
| 134 | 134 |
checkConcept< ReferenceMap<int, double, double&, const double&>, |
| 135 | 135 |
RangeMap<double> >(); |
| 136 | 136 |
std::vector<double> v(10, 0); |
| 137 | 137 |
v[5] = 100; |
| 138 | 138 |
RangeMap<double> map8(v); |
| 139 | 139 |
RangeMap<double> map9 = rangeMap(v); |
| 140 | 140 |
check(map9.size() == 10 && map9[2] == 0 && map9[5] == 100, |
| 141 | 141 |
"Something is wrong with RangeMap"); |
| 142 | 142 |
} |
| 143 | 143 |
|
| 144 | 144 |
// SparseMap |
| 145 | 145 |
{
|
| 146 | 146 |
checkConcept<ReferenceMap<A,B,B&,const B&>, SparseMap<A,B> >(); |
| 147 | 147 |
SparseMap<A,B> map1; |
| 148 | 148 |
SparseMap<A,B> map2 = B(); |
| 149 | 149 |
SparseMap<A,B> map3 = sparseMap<A,B>(); |
| 150 | 150 |
SparseMap<A,B> map4 = sparseMap<A>(B()); |
| 151 | 151 |
|
| 152 | 152 |
checkConcept< ReferenceMap<double, int, int&, const int&>, |
| 153 | 153 |
SparseMap<double, int> >(); |
| 154 | 154 |
std::map<double, int> m; |
| 155 | 155 |
SparseMap<double, int> map5(m); |
| 156 | 156 |
SparseMap<double, int> map6(m,10); |
| 157 | 157 |
SparseMap<double, int> map7 = sparseMap(m); |
| 158 | 158 |
SparseMap<double, int> map8 = sparseMap(m,10); |
| 159 | 159 |
|
| 160 | 160 |
check(map5[1.0] == 0 && map5[3.14] == 0 && |
| 161 | 161 |
map6[1.0] == 10 && map6[3.14] == 10, |
| 162 | 162 |
"Something is wrong with SparseMap"); |
| 163 | 163 |
map5[1.0] = map6[3.14] = 100; |
| 164 | 164 |
check(map5[1.0] == 100 && map5[3.14] == 0 && |
| 165 | 165 |
map6[1.0] == 10 && map6[3.14] == 100, |
| 166 | 166 |
"Something is wrong with SparseMap"); |
| 167 | 167 |
} |
| 168 | 168 |
|
| 169 | 169 |
// ComposeMap |
| 170 | 170 |
{
|
| 171 | 171 |
typedef ComposeMap<DoubleMap, ReadMap<B,A> > CompMap; |
| 172 | 172 |
checkConcept<ReadMap<B,double>, CompMap>(); |
| 173 | 173 |
CompMap map1(DoubleMap(),ReadMap<B,A>()); |
| 174 | 174 |
CompMap map2 = composeMap(DoubleMap(), ReadMap<B,A>()); |
| 175 | 175 |
|
| 176 | 176 |
SparseMap<double, bool> m1(false); m1[3.14] = true; |
| 177 | 177 |
RangeMap<double> m2(2); m2[0] = 3.0; m2[1] = 3.14; |
| 178 | 178 |
check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], |
| 179 | 179 |
"Something is wrong with ComposeMap") |
| 180 | 180 |
} |
| 181 | 181 |
|
| 182 | 182 |
// CombineMap |
| 183 | 183 |
{
|
| 184 | 184 |
typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap; |
| 185 | 185 |
checkConcept<ReadMap<A,double>, CombMap>(); |
| 186 | 186 |
CombMap map1(DoubleMap(), DoubleMap()); |
| 187 | 187 |
CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>()); |
| 188 | 188 |
|
| 189 | 189 |
check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3, |
| 190 | 190 |
"Something is wrong with CombineMap"); |
| 191 | 191 |
} |
| 192 | 192 |
|
| 193 | 193 |
// FunctorToMap, MapToFunctor |
| 194 | 194 |
{
|
| 195 | 195 |
checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >(); |
| 196 | 196 |
checkConcept<ReadMap<A,B>, FunctorToMap<F> >(); |
| 197 | 197 |
FunctorToMap<F> map1; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <iostream> |
| 20 | 20 |
#include <sstream> |
| 21 | 21 |
#include <vector> |
| 22 | 22 |
#include <queue> |
| 23 | 23 |
#include <lemon/math.h> |
| 24 | 24 |
#include <cstdlib> |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/max_matching.h> |
| 27 | 27 |
#include <lemon/smart_graph.h> |
| 28 | 28 |
#include <lemon/lgf_reader.h> |
| 29 | 29 |
|
| 30 | 30 |
#include "test_tools.h" |
| 31 | 31 |
|
| 32 | 32 |
using namespace std; |
| 33 | 33 |
using namespace lemon; |
| 34 | 34 |
|
| 35 | 35 |
GRAPH_TYPEDEFS(SmartGraph); |
| 36 | 36 |
|
| 37 | 37 |
|
| 38 | 38 |
const int lgfn = 3; |
| 39 | 39 |
const std::string lgf[lgfn] = {
|
| 40 | 40 |
"@nodes\n" |
| 41 | 41 |
"label\n" |
| 42 | 42 |
"0\n" |
| 43 | 43 |
"1\n" |
| 44 | 44 |
"2\n" |
| 45 | 45 |
"3\n" |
| 46 | 46 |
"4\n" |
| 47 | 47 |
"5\n" |
| 48 | 48 |
"6\n" |
| 49 | 49 |
"7\n" |
| 50 | 50 |
"@edges\n" |
| 51 | 51 |
" label weight\n" |
| 52 | 52 |
"7 4 0 984\n" |
| 53 | 53 |
"0 7 1 73\n" |
| 54 | 54 |
"7 1 2 204\n" |
| 55 | 55 |
"2 3 3 583\n" |
| 56 | 56 |
"2 7 4 565\n" |
| 57 | 57 |
"2 1 5 582\n" |
| 58 | 58 |
"0 4 6 551\n" |
| 59 | 59 |
"2 5 7 385\n" |
| 60 | 60 |
"1 5 8 561\n" |
| 61 | 61 |
"5 3 9 484\n" |
| 62 | 62 |
"7 5 10 904\n" |
| 63 | 63 |
"3 6 11 47\n" |
| 64 | 64 |
"7 6 12 888\n" |
| 65 | 65 |
"3 0 13 747\n" |
| 66 | 66 |
"6 1 14 310\n", |
| 67 | 67 |
|
| 68 | 68 |
"@nodes\n" |
| 69 | 69 |
"label\n" |
| 70 | 70 |
"0\n" |
| 71 | 71 |
"1\n" |
| 72 | 72 |
"2\n" |
| 73 | 73 |
"3\n" |
| 74 | 74 |
"4\n" |
| 75 | 75 |
"5\n" |
| 76 | 76 |
"6\n" |
| 77 | 77 |
"7\n" |
| 78 | 78 |
"@edges\n" |
| 79 | 79 |
" label weight\n" |
| 80 | 80 |
"2 5 0 710\n" |
| 81 | 81 |
"0 5 1 241\n" |
| 82 | 82 |
"2 4 2 856\n" |
| 83 | 83 |
"2 6 3 762\n" |
| 84 | 84 |
"4 1 4 747\n" |
| 85 | 85 |
"6 1 5 962\n" |
| 86 | 86 |
"4 7 6 723\n" |
| 87 | 87 |
"1 7 7 661\n" |
| 88 | 88 |
"2 3 8 376\n" |
| 89 | 89 |
"1 0 9 416\n" |
| 90 | 90 |
"6 7 10 391\n", |
| 91 | 91 |
|
| 92 | 92 |
"@nodes\n" |
| 93 | 93 |
"label\n" |
| 94 | 94 |
"0\n" |
| 95 | 95 |
"1\n" |
| 96 | 96 |
"2\n" |
| 97 | 97 |
"3\n" |
| 98 | 98 |
"4\n" |
| 99 | 99 |
"5\n" |
| 100 | 100 |
"6\n" |
| 101 | 101 |
"7\n" |
| 102 | 102 |
"@edges\n" |
| 103 | 103 |
" label weight\n" |
| 104 | 104 |
"6 2 0 553\n" |
| 105 | 105 |
"0 7 1 653\n" |
| 106 | 106 |
"6 3 2 22\n" |
| 107 | 107 |
"4 7 3 846\n" |
| 108 | 108 |
"7 2 4 981\n" |
| 109 | 109 |
"7 6 5 250\n" |
| 110 | 110 |
"5 2 6 539\n", |
| 111 | 111 |
}; |
| 112 | 112 |
|
| 113 | 113 |
void checkMatching(const SmartGraph& graph, |
| 114 | 114 |
const MaxMatching<SmartGraph>& mm) {
|
| 115 | 115 |
int num = 0; |
| 116 | 116 |
|
| 117 | 117 |
IntNodeMap comp_index(graph); |
| 118 | 118 |
UnionFind<IntNodeMap> comp(comp_index); |
| 119 | 119 |
|
| 120 | 120 |
int barrier_num = 0; |
| 121 | 121 |
|
| 122 | 122 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
| 123 | 123 |
check(mm.decomposition(n) == MaxMatching<SmartGraph>::EVEN || |
| 124 | 124 |
mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition"); |
| 125 | 125 |
if (mm.decomposition(n) == MaxMatching<SmartGraph>::ODD) {
|
| 126 | 126 |
++barrier_num; |
| 127 | 127 |
} else {
|
| 128 | 128 |
comp.insert(n); |
| 129 | 129 |
} |
| 130 | 130 |
} |
| 131 | 131 |
|
| 132 | 132 |
for (EdgeIt e(graph); e != INVALID; ++e) {
|
| 133 | 133 |
if (mm.matching(e)) {
|
| 134 | 134 |
check(e == mm.matching(graph.u(e)), "Wrong matching"); |
| 135 | 135 |
check(e == mm.matching(graph.v(e)), "Wrong matching"); |
| 136 | 136 |
++num; |
| 137 | 137 |
} |
| 138 | 138 |
check(mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::EVEN || |
| 139 | 139 |
mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::MATCHED, |
| 140 | 140 |
"Wrong Gallai-Edmonds decomposition"); |
| 141 | 141 |
|
| 142 | 142 |
check(mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::EVEN || |
| 143 | 143 |
mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::MATCHED, |
| 144 | 144 |
"Wrong Gallai-Edmonds decomposition"); |
| 145 | 145 |
|
| 146 | 146 |
if (mm.decomposition(graph.u(e)) != MaxMatching<SmartGraph>::ODD && |
| 147 | 147 |
mm.decomposition(graph.v(e)) != MaxMatching<SmartGraph>::ODD) {
|
| 148 | 148 |
comp.join(graph.u(e), graph.v(e)); |
| 149 | 149 |
} |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
std::set<int> comp_root; |
| 153 | 153 |
int odd_comp_num = 0; |
| 154 | 154 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
| 155 | 155 |
if (mm.decomposition(n) != MaxMatching<SmartGraph>::ODD) {
|
| 156 | 156 |
int root = comp.find(n); |
| 157 | 157 |
if (comp_root.find(root) == comp_root.end()) {
|
| 158 | 158 |
comp_root.insert(root); |
| 159 | 159 |
if (comp.size(n) % 2 == 1) {
|
| 160 | 160 |
++odd_comp_num; |
| 161 | 161 |
} |
| 162 | 162 |
} |
| 163 | 163 |
} |
| 164 | 164 |
} |
| 165 | 165 |
|
| 166 | 166 |
check(mm.matchingSize() == num, "Wrong matching"); |
| 167 | 167 |
check(2 * num == countNodes(graph) - (odd_comp_num - barrier_num), |
| 168 | 168 |
"Wrong matching"); |
| 169 | 169 |
return; |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
void checkWeightedMatching(const SmartGraph& graph, |
| 173 | 173 |
const SmartGraph::EdgeMap<int>& weight, |
| 174 | 174 |
const MaxWeightedMatching<SmartGraph>& mwm) {
|
| 175 | 175 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
| 176 | 176 |
if (graph.u(e) == graph.v(e)) continue; |
| 177 | 177 |
int rw = mwm.nodeValue(graph.u(e)) + mwm.nodeValue(graph.v(e)); |
| 178 | 178 |
|
| 179 | 179 |
for (int i = 0; i < mwm.blossomNum(); ++i) {
|
| 180 | 180 |
bool s = false, t = false; |
| 181 | 181 |
for (MaxWeightedMatching<SmartGraph>::BlossomIt n(mwm, i); |
| 182 | 182 |
n != INVALID; ++n) {
|
| 183 | 183 |
if (graph.u(e) == n) s = true; |
| 184 | 184 |
if (graph.v(e) == n) t = true; |
| 185 | 185 |
} |
| 186 | 186 |
if (s == true && t == true) {
|
| 187 | 187 |
rw += mwm.blossomValue(i); |
| 188 | 188 |
} |
| 189 | 189 |
} |
| 190 | 190 |
rw -= weight[e] * mwm.dualScale; |
| 191 | 191 |
|
| 192 | 192 |
check(rw >= 0, "Negative reduced weight"); |
| 193 | 193 |
check(rw == 0 || !mwm.matching(e), |
| 194 | 194 |
"Non-zero reduced weight on matching edge"); |
| 195 | 195 |
} |
| 196 | 196 |
|
| 197 | 197 |
int pv = 0; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <string> |
| 20 | 20 |
#include <iostream> |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/concepts/path.h> |
| 23 | 23 |
#include <lemon/concepts/digraph.h> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/path.h> |
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
|
| 28 | 28 |
#include "test_tools.h" |
| 29 | 29 |
|
| 30 | 30 |
using namespace std; |
| 31 | 31 |
using namespace lemon; |
| 32 | 32 |
|
| 33 | 33 |
void check_concepts() {
|
| 34 | 34 |
checkConcept<concepts::Path<ListDigraph>, concepts::Path<ListDigraph> >(); |
| 35 | 35 |
checkConcept<concepts::Path<ListDigraph>, Path<ListDigraph> >(); |
| 36 | 36 |
checkConcept<concepts::Path<ListDigraph>, SimplePath<ListDigraph> >(); |
| 37 | 37 |
checkConcept<concepts::Path<ListDigraph>, StaticPath<ListDigraph> >(); |
| 38 | 38 |
checkConcept<concepts::Path<ListDigraph>, ListPath<ListDigraph> >(); |
| 39 | 39 |
} |
| 40 | 40 |
|
| 41 | 41 |
int main() {
|
| 42 | 42 |
check_concepts(); |
| 43 | 43 |
return 0; |
| 44 | 44 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <iostream> |
| 20 | 20 |
|
| 21 | 21 |
#include "test_tools.h" |
| 22 | 22 |
#include <lemon/smart_graph.h> |
| 23 | 23 |
#include <lemon/preflow.h> |
| 24 | 24 |
#include <lemon/concepts/digraph.h> |
| 25 | 25 |
#include <lemon/concepts/maps.h> |
| 26 | 26 |
#include <lemon/lgf_reader.h> |
| 27 | 27 |
#include <lemon/elevator.h> |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| 30 | 30 |
|
| 31 | 31 |
char test_lgf[] = |
| 32 | 32 |
"@nodes\n" |
| 33 | 33 |
"label\n" |
| 34 | 34 |
"0\n" |
| 35 | 35 |
"1\n" |
| 36 | 36 |
"2\n" |
| 37 | 37 |
"3\n" |
| 38 | 38 |
"4\n" |
| 39 | 39 |
"5\n" |
| 40 | 40 |
"6\n" |
| 41 | 41 |
"7\n" |
| 42 | 42 |
"8\n" |
| 43 | 43 |
"9\n" |
| 44 | 44 |
"@arcs\n" |
| 45 | 45 |
" label capacity\n" |
| 46 | 46 |
"0 1 0 20\n" |
| 47 | 47 |
"0 2 1 0\n" |
| 48 | 48 |
"1 1 2 3\n" |
| 49 | 49 |
"1 2 3 8\n" |
| 50 | 50 |
"1 3 4 8\n" |
| 51 | 51 |
"2 5 5 5\n" |
| 52 | 52 |
"3 2 6 5\n" |
| 53 | 53 |
"3 5 7 5\n" |
| 54 | 54 |
"3 6 8 5\n" |
| 55 | 55 |
"4 3 9 3\n" |
| 56 | 56 |
"5 7 10 3\n" |
| 57 | 57 |
"5 6 11 10\n" |
| 58 | 58 |
"5 8 12 10\n" |
| 59 | 59 |
"6 8 13 8\n" |
| 60 | 60 |
"8 9 14 20\n" |
| 61 | 61 |
"8 1 15 5\n" |
| 62 | 62 |
"9 5 16 5\n" |
| 63 | 63 |
"@attributes\n" |
| 64 | 64 |
"source 1\n" |
| 65 | 65 |
"target 8\n"; |
| 66 | 66 |
|
| 67 | 67 |
void checkPreflowCompile() |
| 68 | 68 |
{
|
| 69 | 69 |
typedef int VType; |
| 70 | 70 |
typedef concepts::Digraph Digraph; |
| 71 | 71 |
|
| 72 | 72 |
typedef Digraph::Node Node; |
| 73 | 73 |
typedef Digraph::Arc Arc; |
| 74 | 74 |
typedef concepts::ReadMap<Arc,VType> CapMap; |
| 75 | 75 |
typedef concepts::ReadWriteMap<Arc,VType> FlowMap; |
| 76 | 76 |
typedef concepts::WriteMap<Node,bool> CutMap; |
| 77 | 77 |
|
| 78 | 78 |
typedef Elevator<Digraph, Digraph::Node> Elev; |
| 79 | 79 |
typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev; |
| 80 | 80 |
|
| 81 | 81 |
Digraph g; |
| 82 | 82 |
Node n; |
| 83 | 83 |
Arc e; |
| 84 | 84 |
CapMap cap; |
| 85 | 85 |
FlowMap flow; |
| 86 | 86 |
CutMap cut; |
| 87 | 87 |
|
| 88 | 88 |
Preflow<Digraph, CapMap> |
| 89 | 89 |
::SetFlowMap<FlowMap> |
| 90 | 90 |
::SetElevator<Elev> |
| 91 | 91 |
::SetStandardElevator<LinkedElev> |
| 92 | 92 |
::Create preflow_test(g,cap,n,n); |
| 93 | 93 |
|
| 94 | 94 |
preflow_test.capacityMap(cap); |
| 95 | 95 |
flow = preflow_test.flowMap(); |
| 96 | 96 |
preflow_test.flowMap(flow); |
| 97 | 97 |
preflow_test.source(n); |
| 98 | 98 |
preflow_test.target(n); |
| 99 | 99 |
|
| 100 | 100 |
preflow_test.init(); |
| 101 | 101 |
preflow_test.init(cap); |
| 102 | 102 |
preflow_test.startFirstPhase(); |
| 103 | 103 |
preflow_test.startSecondPhase(); |
| 104 | 104 |
preflow_test.run(); |
| 105 | 105 |
preflow_test.runMinCut(); |
| 106 | 106 |
|
| 107 | 107 |
preflow_test.flowValue(); |
| 108 | 108 |
preflow_test.minCut(n); |
| 109 | 109 |
preflow_test.minCutMap(cut); |
| 110 | 110 |
preflow_test.flow(e); |
| 111 | 111 |
|
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 | 114 |
int cutValue (const SmartDigraph& g, |
| 115 | 115 |
const SmartDigraph::NodeMap<bool>& cut, |
| 116 | 116 |
const SmartDigraph::ArcMap<int>& cap) {
|
| 117 | 117 |
|
| 118 | 118 |
int c=0; |
| 119 | 119 |
for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
|
| 120 | 120 |
if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e]; |
| 121 | 121 |
} |
| 122 | 122 |
return c; |
| 123 | 123 |
} |
| 124 | 124 |
|
| 125 | 125 |
bool checkFlow(const SmartDigraph& g, |
| 126 | 126 |
const SmartDigraph::ArcMap<int>& flow, |
| 127 | 127 |
const SmartDigraph::ArcMap<int>& cap, |
| 128 | 128 |
SmartDigraph::Node s, SmartDigraph::Node t) {
|
| 129 | 129 |
|
| 130 | 130 |
for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
|
| 131 | 131 |
if (flow[e] < 0 || flow[e] > cap[e]) return false; |
| 132 | 132 |
} |
| 133 | 133 |
|
| 134 | 134 |
for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
|
| 135 | 135 |
if (n == s || n == t) continue; |
| 136 | 136 |
int sum = 0; |
| 137 | 137 |
for (SmartDigraph::OutArcIt e(g, n); e != INVALID; ++e) {
|
| 138 | 138 |
sum += flow[e]; |
| 139 | 139 |
} |
| 140 | 140 |
for (SmartDigraph::InArcIt e(g, n); e != INVALID; ++e) {
|
| 141 | 141 |
sum -= flow[e]; |
| 142 | 142 |
} |
| 143 | 143 |
if (sum != 0) return false; |
| 144 | 144 |
} |
| 145 | 145 |
return true; |
| 146 | 146 |
} |
| 147 | 147 |
|
| 148 | 148 |
int main() {
|
| 149 | 149 |
|
| 150 | 150 |
typedef SmartDigraph Digraph; |
| 151 | 151 |
|
| 152 | 152 |
typedef Digraph::Node Node; |
| 153 | 153 |
typedef Digraph::NodeIt NodeIt; |
| 154 | 154 |
typedef Digraph::ArcIt ArcIt; |
| 155 | 155 |
typedef Digraph::ArcMap<int> CapMap; |
| 156 | 156 |
typedef Digraph::ArcMap<int> FlowMap; |
| 157 | 157 |
typedef Digraph::NodeMap<bool> CutMap; |
| 158 | 158 |
|
| 159 | 159 |
typedef Preflow<Digraph, CapMap> PType; |
| 160 | 160 |
|
| 161 | 161 |
Digraph g; |
| 162 | 162 |
Node s, t; |
| 163 | 163 |
CapMap cap(g); |
| 164 | 164 |
std::istringstream input(test_lgf); |
| 165 | 165 |
DigraphReader<Digraph>(g,input). |
| 166 | 166 |
arcMap("capacity", cap).
|
| 167 | 167 |
node("source",s).
|
| 168 | 168 |
node("target",t).
|
| 169 | 169 |
run(); |
| 170 | 170 |
|
| 171 | 171 |
PType preflow_test(g, cap, s, t); |
| 172 | 172 |
preflow_test.run(); |
| 173 | 173 |
|
| 174 | 174 |
check(checkFlow(g, preflow_test.flowMap(), cap, s, t), |
| 175 | 175 |
"The flow is not feasible."); |
| 176 | 176 |
|
| 177 | 177 |
CutMap min_cut(g); |
| 178 | 178 |
preflow_test.minCutMap(min_cut); |
| 179 | 179 |
int min_cut_value=cutValue(g,min_cut,cap); |
| 180 | 180 |
|
| 181 | 181 |
check(preflow_test.flowValue() == min_cut_value, |
| 182 | 182 |
"The max flow value is not equal to the three min cut values."); |
| 183 | 183 |
|
| 184 | 184 |
FlowMap flow(g); |
| 185 | 185 |
for(ArcIt e(g); e!=INVALID; ++e) flow[e] = preflow_test.flowMap()[e]; |
| 186 | 186 |
|
| 187 | 187 |
int flow_value=preflow_test.flowValue(); |
| 188 | 188 |
|
| 189 | 189 |
for(ArcIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e]; |
| 190 | 190 |
preflow_test.init(flow); |
| 191 | 191 |
preflow_test.startFirstPhase(); |
| 192 | 192 |
|
| 193 | 193 |
CutMap min_cut1(g); |
| 194 | 194 |
preflow_test.minCutMap(min_cut1); |
| 195 | 195 |
min_cut_value=cutValue(g,min_cut1,cap); |
| 196 | 196 |
|
| 197 | 197 |
check(preflow_test.flowValue() == min_cut_value && |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/random.h> |
| 20 | 20 |
#include "test_tools.h" |
| 21 | 21 |
|
| 22 | 22 |
int seed_array[] = {1, 2};
|
| 23 | 23 |
|
| 24 | 24 |
int main() |
| 25 | 25 |
{
|
| 26 | 26 |
double a=lemon::rnd(); |
| 27 | 27 |
check(a<1.0&&a>0.0,"This should be in [0,1)"); |
| 28 | 28 |
a=lemon::rnd.gauss(); |
| 29 | 29 |
a=lemon::rnd.gamma(3.45,0); |
| 30 | 30 |
a=lemon::rnd.gamma(4); |
| 31 | 31 |
//Does gamma work with integer k? |
| 32 | 32 |
a=lemon::rnd.gamma(4.0,0); |
| 33 | 33 |
a=lemon::rnd.poisson(.5); |
| 34 | 34 |
|
| 35 | 35 |
lemon::rnd.seed(100); |
| 36 | 36 |
lemon::rnd.seed(seed_array, seed_array + |
| 37 | 37 |
(sizeof(seed_array) / sizeof(seed_array[0]))); |
| 38 | 38 |
|
| 39 | 39 |
return 0; |
| 40 | 40 |
} |
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <iostream> |
| 20 | 20 |
|
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/path.h> |
| 24 | 24 |
#include <lemon/suurballe.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "test_tools.h" |
| 27 | 27 |
|
| 28 | 28 |
using namespace lemon; |
| 29 | 29 |
|
| 30 | 30 |
char test_lgf[] = |
| 31 | 31 |
"@nodes\n" |
| 32 | 32 |
"label supply1 supply2 supply3\n" |
| 33 | 33 |
"1 0 20 27\n" |
| 34 | 34 |
"2 0 -4 0\n" |
| 35 | 35 |
"3 0 0 0\n" |
| 36 | 36 |
"4 0 0 0\n" |
| 37 | 37 |
"5 0 9 0\n" |
| 38 | 38 |
"6 0 -6 0\n" |
| 39 | 39 |
"7 0 0 0\n" |
| 40 | 40 |
"8 0 0 0\n" |
| 41 | 41 |
"9 0 3 0\n" |
| 42 | 42 |
"10 0 -2 0\n" |
| 43 | 43 |
"11 0 0 0\n" |
| 44 | 44 |
"12 0 -20 -27\n" |
| 45 | 45 |
"@arcs\n" |
| 46 | 46 |
" cost capacity lower1 lower2\n" |
| 47 | 47 |
" 1 2 70 11 0 8\n" |
| 48 | 48 |
" 1 3 150 3 0 1\n" |
| 49 | 49 |
" 1 4 80 15 0 2\n" |
| 50 | 50 |
" 2 8 80 12 0 0\n" |
| 51 | 51 |
" 3 5 140 5 0 3\n" |
| 52 | 52 |
" 4 6 60 10 0 1\n" |
| 53 | 53 |
" 4 7 80 2 0 0\n" |
| 54 | 54 |
" 4 8 110 3 0 0\n" |
| 55 | 55 |
" 5 7 60 14 0 0\n" |
| 56 | 56 |
" 5 11 120 12 0 0\n" |
| 57 | 57 |
" 6 3 0 3 0 0\n" |
| 58 | 58 |
" 6 9 140 4 0 0\n" |
| 59 | 59 |
" 6 10 90 8 0 0\n" |
| 60 | 60 |
" 7 1 30 5 0 0\n" |
| 61 | 61 |
" 8 12 60 16 0 4\n" |
| 62 | 62 |
" 9 12 50 6 0 0\n" |
| 63 | 63 |
"10 12 70 13 0 5\n" |
| 64 | 64 |
"10 2 100 7 0 0\n" |
| 65 | 65 |
"10 7 60 10 0 0\n" |
| 66 | 66 |
"11 10 20 14 0 6\n" |
| 67 | 67 |
"12 11 30 10 0 0\n" |
| 68 | 68 |
"@attributes\n" |
| 69 | 69 |
"source 1\n" |
| 70 | 70 |
"target 12\n" |
| 71 | 71 |
"@end\n"; |
| 72 | 72 |
|
| 73 | 73 |
// Check the feasibility of the flow |
| 74 | 74 |
template <typename Digraph, typename FlowMap> |
| 75 |
bool checkFlow( const Digraph& gr, const FlowMap& flow, |
|
| 75 |
bool checkFlow( const Digraph& gr, const FlowMap& flow, |
|
| 76 | 76 |
typename Digraph::Node s, typename Digraph::Node t, |
| 77 | 77 |
int value ) |
| 78 | 78 |
{
|
| 79 | 79 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 80 | 80 |
for (ArcIt e(gr); e != INVALID; ++e) |
| 81 | 81 |
if (!(flow[e] == 0 || flow[e] == 1)) return false; |
| 82 | 82 |
|
| 83 | 83 |
for (NodeIt n(gr); n != INVALID; ++n) {
|
| 84 | 84 |
int sum = 0; |
| 85 | 85 |
for (OutArcIt e(gr, n); e != INVALID; ++e) |
| 86 | 86 |
sum += flow[e]; |
| 87 | 87 |
for (InArcIt e(gr, n); e != INVALID; ++e) |
| 88 | 88 |
sum -= flow[e]; |
| 89 | 89 |
if (n == s && sum != value) return false; |
| 90 | 90 |
if (n == t && sum != -value) return false; |
| 91 | 91 |
if (n != s && n != t && sum != 0) return false; |
| 92 | 92 |
} |
| 93 | 93 |
|
| 94 | 94 |
return true; |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 | 97 |
// Check the optimalitiy of the flow |
| 98 |
template < typename Digraph, typename CostMap, |
|
| 98 |
template < typename Digraph, typename CostMap, |
|
| 99 | 99 |
typename FlowMap, typename PotentialMap > |
| 100 | 100 |
bool checkOptimality( const Digraph& gr, const CostMap& cost, |
| 101 | 101 |
const FlowMap& flow, const PotentialMap& pi ) |
| 102 | 102 |
{
|
| 103 | 103 |
// Check the "Complementary Slackness" optimality condition |
| 104 | 104 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 105 | 105 |
bool opt = true; |
| 106 | 106 |
for (ArcIt e(gr); e != INVALID; ++e) {
|
| 107 | 107 |
typename CostMap::Value red_cost = |
| 108 | 108 |
cost[e] + pi[gr.source(e)] - pi[gr.target(e)]; |
| 109 | 109 |
opt = (flow[e] == 0 && red_cost >= 0) || |
| 110 | 110 |
(flow[e] == 1 && red_cost <= 0); |
| 111 | 111 |
if (!opt) break; |
| 112 | 112 |
} |
| 113 | 113 |
return opt; |
| 114 | 114 |
} |
| 115 | 115 |
|
| 116 | 116 |
// Check a path |
| 117 | 117 |
template <typename Digraph, typename Path> |
| 118 | 118 |
bool checkPath( const Digraph& gr, const Path& path, |
| 119 | 119 |
typename Digraph::Node s, typename Digraph::Node t) |
| 120 | 120 |
{
|
| 121 | 121 |
// Check the "Complementary Slackness" optimality condition |
| 122 | 122 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 123 | 123 |
Node n = s; |
| 124 | 124 |
for (int i = 0; i < path.length(); ++i) {
|
| 125 | 125 |
if (gr.source(path.nth(i)) != n) return false; |
| 126 | 126 |
n = gr.target(path.nth(i)); |
| 127 | 127 |
} |
| 128 | 128 |
return n == t; |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
|
| 132 | 132 |
int main() |
| 133 | 133 |
{
|
| 134 | 134 |
DIGRAPH_TYPEDEFS(ListDigraph); |
| 135 | 135 |
|
| 136 | 136 |
// Read the test digraph |
| 137 | 137 |
ListDigraph digraph; |
| 138 | 138 |
ListDigraph::ArcMap<int> length(digraph); |
| 139 | 139 |
Node source, target; |
| 140 | 140 |
|
| 141 | 141 |
std::istringstream input(test_lgf); |
| 142 | 142 |
DigraphReader<ListDigraph>(digraph, input). |
| 143 | 143 |
arcMap("cost", length).
|
| 144 | 144 |
node("source", source).
|
| 145 | 145 |
node("target", target).
|
| 146 | 146 |
run(); |
| 147 |
|
|
| 147 |
|
|
| 148 | 148 |
// Find 2 paths |
| 149 | 149 |
{
|
| 150 | 150 |
Suurballe<ListDigraph> suurballe(digraph, length, source, target); |
| 151 | 151 |
check(suurballe.run(2) == 2, "Wrong number of paths"); |
| 152 | 152 |
check(checkFlow(digraph, suurballe.flowMap(), source, target, 2), |
| 153 | 153 |
"The flow is not feasible"); |
| 154 | 154 |
check(suurballe.totalLength() == 510, "The flow is not optimal"); |
| 155 |
check(checkOptimality(digraph, length, suurballe.flowMap(), |
|
| 155 |
check(checkOptimality(digraph, length, suurballe.flowMap(), |
|
| 156 | 156 |
suurballe.potentialMap()), |
| 157 | 157 |
"Wrong potentials"); |
| 158 | 158 |
for (int i = 0; i < suurballe.pathNum(); ++i) |
| 159 | 159 |
check(checkPath(digraph, suurballe.path(i), source, target), |
| 160 | 160 |
"Wrong path"); |
| 161 | 161 |
} |
| 162 | 162 |
|
| 163 | 163 |
// Find 3 paths |
| 164 | 164 |
{
|
| 165 | 165 |
Suurballe<ListDigraph> suurballe(digraph, length, source, target); |
| 166 | 166 |
check(suurballe.run(3) == 3, "Wrong number of paths"); |
| 167 | 167 |
check(checkFlow(digraph, suurballe.flowMap(), source, target, 3), |
| 168 | 168 |
"The flow is not feasible"); |
| 169 | 169 |
check(suurballe.totalLength() == 1040, "The flow is not optimal"); |
| 170 |
check(checkOptimality(digraph, length, suurballe.flowMap(), |
|
| 170 |
check(checkOptimality(digraph, length, suurballe.flowMap(), |
|
| 171 | 171 |
suurballe.potentialMap()), |
| 172 | 172 |
"Wrong potentials"); |
| 173 | 173 |
for (int i = 0; i < suurballe.pathNum(); ++i) |
| 174 | 174 |
check(checkPath(digraph, suurballe.path(i), source, target), |
| 175 | 175 |
"Wrong path"); |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
// Find 5 paths (only 3 can be found) |
| 179 | 179 |
{
|
| 180 | 180 |
Suurballe<ListDigraph> suurballe(digraph, length, source, target); |
| 181 | 181 |
check(suurballe.run(5) == 3, "Wrong number of paths"); |
| 182 | 182 |
check(checkFlow(digraph, suurballe.flowMap(), source, target, 3), |
| 183 | 183 |
"The flow is not feasible"); |
| 184 | 184 |
check(suurballe.totalLength() == 1040, "The flow is not optimal"); |
| 185 |
check(checkOptimality(digraph, length, suurballe.flowMap(), |
|
| 185 |
check(checkOptimality(digraph, length, suurballe.flowMap(), |
|
| 186 | 186 |
suurballe.potentialMap()), |
| 187 | 187 |
"Wrong potentials"); |
| 188 | 188 |
for (int i = 0; i < suurballe.pathNum(); ++i) |
| 189 | 189 |
check(checkPath(digraph, suurballe.path(i), source, target), |
| 190 | 190 |
"Wrong path"); |
| 191 | 191 |
} |
| 192 | 192 |
|
| 193 | 193 |
return 0; |
| 194 | 194 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_TEST_TEST_TOOLS_H |
| 20 | 20 |
#define LEMON_TEST_TEST_TOOLS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup misc |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Some utilities to write test programs. |
| 25 | 25 |
|
| 26 | 26 |
#include <iostream> |
| 27 | 27 |
#include <stdlib.h> |
| 28 | 28 |
|
| 29 | 29 |
///If \c rc is fail, writes an error message and exits. |
| 30 | 30 |
|
| 31 | 31 |
///If \c rc is fail, writes an error message and exits. |
| 32 | 32 |
///The error message contains the file name and the line number of the |
| 33 | 33 |
///source code in a standard from, which makes it possible to go there |
| 34 | 34 |
///using good source browsers like e.g. \c emacs. |
| 35 | 35 |
/// |
| 36 | 36 |
///For example |
| 37 | 37 |
///\code check(0==1,"This is obviously false.");\endcode will |
| 38 | 38 |
///print something like this (and then exits). |
| 39 | 39 |
///\verbatim file_name.cc:123: error: This is obviously false. \endverbatim |
| 40 | 40 |
#define check(rc, msg) \ |
| 41 | 41 |
if(!(rc)) { \
|
| 42 | 42 |
std::cerr << __FILE__ ":" << __LINE__ << ": error: " << msg << std::endl; \ |
| 43 | 43 |
abort(); \ |
| 44 | 44 |
} else { } \
|
| 45 | 45 |
|
| 46 | 46 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include "test_tools.h" |
| 20 | 20 |
|
| 21 | 21 |
int main() |
| 22 | 22 |
{
|
| 23 | 23 |
check(false, "Don't panic. Failing is the right behaviour here."); |
| 24 | 24 |
return 0; |
| 25 | 25 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include "test_tools.h" |
| 20 | 20 |
|
| 21 | 21 |
int main() |
| 22 | 22 |
{
|
| 23 | 23 |
check(true, "It should pass."); |
| 24 | 24 |
return 0; |
| 25 | 25 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/time_measure.h> |
| 20 | 20 |
|
| 21 | 21 |
using namespace lemon; |
| 22 | 22 |
|
| 23 | 23 |
void f() |
| 24 | 24 |
{
|
| 25 | 25 |
double d=0; |
| 26 | 26 |
for(int i=0;i<1000;i++) |
| 27 | 27 |
d+=0.1; |
| 28 | 28 |
} |
| 29 | 29 |
|
| 30 | 30 |
void g() |
| 31 | 31 |
{
|
| 32 | 32 |
static Timer T; |
| 33 | 33 |
|
| 34 | 34 |
for(int i=0;i<1000;i++) |
| 35 | 35 |
TimeStamp x(T); |
| 36 | 36 |
} |
| 37 | 37 |
|
| 38 | 38 |
int main() |
| 39 | 39 |
{
|
| 40 | 40 |
Timer T; |
| 41 | 41 |
unsigned int n; |
| 42 | 42 |
for(n=0;T.realTime()<1.0;n++) ; |
| 43 | 43 |
std::cout << T << " (" << n << " time queries)\n";
|
| 44 | 44 |
T.restart(); |
| 45 | 45 |
while(T.realTime()<2.0) ; |
| 46 | 46 |
std::cout << T << '\n'; |
| 47 | 47 |
TimeStamp full; |
| 48 | 48 |
TimeStamp t; |
| 49 | 49 |
t=runningTimeTest(f,1,&n,&full); |
| 50 | 50 |
std::cout << t << " (" << n << " tests)\n";
|
| 51 | 51 |
std::cout << "Total: " << full << "\n"; |
| 52 | 52 |
|
| 53 | 53 |
t=runningTimeTest(g,1,&n,&full); |
| 54 | 54 |
std::cout << t << " (" << n << " tests)\n";
|
| 55 | 55 |
std::cout << "Total: " << full << "\n"; |
| 56 | 56 |
|
| 57 | 57 |
return 0; |
| 58 | 58 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/list_graph.h> |
| 20 | 20 |
#include <lemon/maps.h> |
| 21 | 21 |
#include <lemon/unionfind.h> |
| 22 | 22 |
#include "test_tools.h" |
| 23 | 23 |
|
| 24 | 24 |
using namespace lemon; |
| 25 | 25 |
using namespace std; |
| 26 | 26 |
|
| 27 | 27 |
typedef UnionFindEnum<ListGraph::NodeMap<int> > UFE; |
| 28 | 28 |
|
| 29 | 29 |
int main() {
|
| 30 | 30 |
ListGraph g; |
| 31 | 31 |
ListGraph::NodeMap<int> base(g); |
| 32 | 32 |
UFE U(base); |
| 33 | 33 |
vector<ListGraph::Node> n; |
| 34 | 34 |
|
| 35 | 35 |
for(int i=0;i<20;i++) n.push_back(g.addNode()); |
| 36 | 36 |
|
| 37 | 37 |
U.insert(n[1]); |
| 38 | 38 |
U.insert(n[2]); |
| 39 | 39 |
|
| 40 | 40 |
check(U.join(n[1],n[2]) != -1, "Something is wrong with UnionFindEnum"); |
| 41 | 41 |
|
| 42 | 42 |
U.insert(n[3]); |
| 43 | 43 |
U.insert(n[4]); |
| 44 | 44 |
U.insert(n[5]); |
| 45 | 45 |
U.insert(n[6]); |
| 46 | 46 |
U.insert(n[7]); |
| 47 | 47 |
|
| 48 | 48 |
|
| 49 | 49 |
check(U.join(n[1],n[4]) != -1, "Something is wrong with UnionFindEnum"); |
| 50 | 50 |
check(U.join(n[2],n[4]) == -1, "Something is wrong with UnionFindEnum"); |
| 51 | 51 |
check(U.join(n[3],n[5]) != -1, "Something is wrong with UnionFindEnum"); |
| 52 | 52 |
|
| 53 | 53 |
|
| 54 | 54 |
U.insert(n[8],U.find(n[5])); |
| 55 | 55 |
|
| 56 | 56 |
|
| 57 | 57 |
check(U.size(U.find(n[4])) == 3, "Something is wrong with UnionFindEnum"); |
| 58 | 58 |
check(U.size(U.find(n[5])) == 3, "Something is wrong with UnionFindEnum"); |
| 59 | 59 |
check(U.size(U.find(n[6])) == 1, "Something is wrong with UnionFindEnum"); |
| 60 | 60 |
check(U.size(U.find(n[2])) == 3, "Something is wrong with UnionFindEnum"); |
| 61 | 61 |
|
| 62 | 62 |
|
| 63 | 63 |
U.insert(n[9]); |
| 64 | 64 |
U.insert(n[10],U.find(n[9])); |
| 65 | 65 |
|
| 66 | 66 |
|
| 67 | 67 |
check(U.join(n[8],n[10]) != -1, "Something is wrong with UnionFindEnum"); |
| 68 | 68 |
|
| 69 | 69 |
|
| 70 | 70 |
check(U.size(U.find(n[4])) == 3, "Something is wrong with UnionFindEnum"); |
| 71 | 71 |
check(U.size(U.find(n[9])) == 5, "Something is wrong with UnionFindEnum"); |
| 72 | 72 |
|
| 73 | 73 |
check(U.size(U.find(n[8])) == 5, "Something is wrong with UnionFindEnum"); |
| 74 | 74 |
|
| 75 | 75 |
U.erase(n[9]); |
| 76 | 76 |
U.erase(n[1]); |
| 77 | 77 |
|
| 78 | 78 |
check(U.size(U.find(n[10])) == 4, "Something is wrong with UnionFindEnum"); |
| 79 | 79 |
check(U.size(U.find(n[2])) == 2, "Something is wrong with UnionFindEnum"); |
| 80 | 80 |
|
| 81 | 81 |
U.erase(n[6]); |
| 82 | 82 |
U.split(U.find(n[8])); |
| 83 | 83 |
|
| 84 | 84 |
|
| 85 | 85 |
check(U.size(U.find(n[4])) == 2, "Something is wrong with UnionFindEnum"); |
| 86 | 86 |
check(U.size(U.find(n[3])) == 1, "Something is wrong with UnionFindEnum"); |
| 87 | 87 |
check(U.size(U.find(n[2])) == 2, "Something is wrong with UnionFindEnum"); |
| 88 | 88 |
|
| 89 | 89 |
|
| 90 | 90 |
check(U.join(n[3],n[4]) != -1, "Something is wrong with UnionFindEnum"); |
| 91 | 91 |
check(U.join(n[2],n[4]) == -1, "Something is wrong with UnionFindEnum"); |
| 92 | 92 |
|
| 93 | 93 |
|
| 94 | 94 |
check(U.size(U.find(n[4])) == 3, "Something is wrong with UnionFindEnum"); |
| 95 | 95 |
check(U.size(U.find(n[3])) == 3, "Something is wrong with UnionFindEnum"); |
| 96 | 96 |
check(U.size(U.find(n[2])) == 3, "Something is wrong with UnionFindEnum"); |
| 97 | 97 |
|
| 98 | 98 |
U.eraseClass(U.find(n[4])); |
| 99 | 99 |
U.eraseClass(U.find(n[7])); |
| 100 | 100 |
|
| 101 | 101 |
return 0; |
| 102 | 102 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup tools |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief DIMACS to LGF converter. |
| 22 | 22 |
/// |
| 23 | 23 |
/// This program converts various DIMACS formats to the LEMON Digraph Format |
| 24 | 24 |
/// (LGF). |
| 25 | 25 |
/// |
| 26 | 26 |
/// See |
| 27 | 27 |
/// \verbatim |
| 28 | 28 |
/// dimacs-to-lgf --help |
| 29 | 29 |
/// \endverbatim |
| 30 | 30 |
/// for more info on usage. |
| 31 | 31 |
/// |
| 32 | 32 |
|
| 33 | 33 |
#include <iostream> |
| 34 | 34 |
#include <fstream> |
| 35 | 35 |
#include <cstring> |
| 36 | 36 |
|
| 37 | 37 |
#include <lemon/smart_graph.h> |
| 38 | 38 |
#include <lemon/dimacs.h> |
| 39 | 39 |
#include <lemon/lgf_writer.h> |
| 40 | 40 |
|
| 41 | 41 |
#include <lemon/arg_parser.h> |
| 42 | 42 |
#include <lemon/error.h> |
| 43 | 43 |
|
| 44 | 44 |
using namespace std; |
| 45 | 45 |
using namespace lemon; |
| 46 | 46 |
|
| 47 | 47 |
|
| 48 | 48 |
int main(int argc, const char *argv[]) {
|
| 49 | 49 |
typedef SmartDigraph Digraph; |
| 50 | 50 |
|
| 51 | 51 |
typedef Digraph::Arc Arc; |
| 52 | 52 |
typedef Digraph::Node Node; |
| 53 | 53 |
typedef Digraph::ArcIt ArcIt; |
| 54 | 54 |
typedef Digraph::NodeIt NodeIt; |
| 55 | 55 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
| 56 | 56 |
typedef Digraph::NodeMap<double> DoubleNodeMap; |
| 57 | 57 |
|
| 58 | 58 |
std::string inputName; |
| 59 | 59 |
std::string outputName; |
| 60 | 60 |
|
| 61 | 61 |
ArgParser ap(argc, argv); |
| 62 | 62 |
ap.other("[INFILE [OUTFILE]]",
|
| 63 | 63 |
"If either the INFILE or OUTFILE file is missing the standard\n" |
| 64 | 64 |
" input/output will be used instead.") |
| 65 | 65 |
.run(); |
| 66 | 66 |
|
| 67 | 67 |
ifstream input; |
| 68 | 68 |
ofstream output; |
| 69 | 69 |
|
| 70 | 70 |
switch(ap.files().size()) |
| 71 | 71 |
{
|
| 72 | 72 |
case 2: |
| 73 | 73 |
output.open(ap.files()[1].c_str()); |
| 74 | 74 |
if (!output) {
|
| 75 | 75 |
throw IoError("Cannot open the file for writing", ap.files()[1]);
|
| 76 | 76 |
} |
| 77 | 77 |
case 1: |
| 78 | 78 |
input.open(ap.files()[0].c_str()); |
| 79 | 79 |
if (!input) {
|
| 80 | 80 |
throw IoError("File cannot be found", ap.files()[0]);
|
| 81 | 81 |
} |
| 82 | 82 |
case 0: |
| 83 | 83 |
break; |
| 84 | 84 |
default: |
| 85 | 85 |
cerr << ap.commandName() << ": too many arguments\n"; |
| 86 | 86 |
return 1; |
| 87 | 87 |
} |
| 88 | 88 |
istream& is = (ap.files().size()<1 ? cin : input); |
| 89 | 89 |
ostream& os = (ap.files().size()<2 ? cout : output); |
| 90 | 90 |
|
| 91 | 91 |
DimacsDescriptor desc = dimacsType(is); |
| 92 | 92 |
switch(desc.type) |
| 93 | 93 |
{
|
| 94 | 94 |
case DimacsDescriptor::MIN: |
| 95 | 95 |
{
|
| 96 | 96 |
Digraph digraph; |
| 97 | 97 |
DoubleArcMap lower(digraph), capacity(digraph), cost(digraph); |
| 98 | 98 |
DoubleNodeMap supply(digraph); |
| 99 | 99 |
readDimacsMin(is, digraph, lower, capacity, cost, supply, desc); |
| 100 | 100 |
DigraphWriter<Digraph>(digraph, os). |
| 101 | 101 |
nodeMap("supply", supply).
|
| 102 | 102 |
arcMap("lower", lower).
|
| 103 | 103 |
arcMap("capacity", capacity).
|
| 104 | 104 |
arcMap("cost", cost).
|
| 105 | 105 |
attribute("problem","min").
|
| 106 | 106 |
run(); |
| 107 | 107 |
} |
| 108 | 108 |
break; |
| 109 | 109 |
case DimacsDescriptor::MAX: |
| 110 | 110 |
{
|
| 111 | 111 |
Digraph digraph; |
| 112 | 112 |
Node s, t; |
| 113 | 113 |
DoubleArcMap capacity(digraph); |
| 114 | 114 |
readDimacsMax(is, digraph, capacity, s, t, desc); |
| 115 | 115 |
DigraphWriter<Digraph>(digraph, os). |
| 116 | 116 |
arcMap("capacity", capacity).
|
| 117 | 117 |
node("source", s).
|
| 118 | 118 |
node("target", t).
|
| 119 | 119 |
attribute("problem","max").
|
| 120 | 120 |
run(); |
| 121 | 121 |
} |
| 122 | 122 |
break; |
| 123 | 123 |
case DimacsDescriptor::SP: |
| 124 | 124 |
{
|
| 125 | 125 |
Digraph digraph; |
| 126 | 126 |
Node s; |
| 127 | 127 |
DoubleArcMap capacity(digraph); |
| 128 | 128 |
readDimacsSp(is, digraph, capacity, s, desc); |
| 129 | 129 |
DigraphWriter<Digraph>(digraph, os). |
| 130 | 130 |
arcMap("capacity", capacity).
|
| 131 | 131 |
node("source", s).
|
| 132 | 132 |
attribute("problem","sp").
|
| 133 | 133 |
run(); |
| 134 | 134 |
} |
| 135 | 135 |
break; |
| 136 | 136 |
case DimacsDescriptor::MAT: |
| 137 | 137 |
{
|
| 138 | 138 |
Digraph digraph; |
| 139 | 139 |
readDimacsMat(is, digraph,desc); |
| 140 | 140 |
DigraphWriter<Digraph>(digraph, os). |
| 141 | 141 |
attribute("problem","mat").
|
| 142 | 142 |
run(); |
| 143 | 143 |
} |
| 144 | 144 |
break; |
| 145 | 145 |
default: |
| 146 | 146 |
break; |
| 147 | 147 |
} |
| 148 | 148 |
return 0; |
| 149 | 149 |
} |
0 comments (0 inline)