0
16
1
22
13
5
13
5
6
2
94
76
| 1 |
%!PS-Adobe-2.0 EPSF-2.0 |
|
| 2 |
%%Creator: LEMON, graphToEps() |
|
| 3 |
%%CreationDate: Fri Oct 19 18:32:32 2007 |
|
| 4 |
%%BoundingBox: 0 0 596 842 |
|
| 5 |
%%DocumentPaperSizes: a4 |
|
| 6 |
%%EndComments |
|
| 7 |
/lb { setlinewidth setrgbcolor newpath moveto
|
|
| 8 |
4 2 roll 1 index 1 index curveto stroke } bind def |
|
| 9 |
/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def
|
|
| 10 |
/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def
|
|
| 11 |
/sq { newpath 2 index 1 index add 2 index 2 index add moveto
|
|
| 12 |
2 index 1 index sub 2 index 2 index add lineto |
|
| 13 |
2 index 1 index sub 2 index 2 index sub lineto |
|
| 14 |
2 index 1 index add 2 index 2 index sub lineto |
|
| 15 |
closepath pop pop pop} bind def |
|
| 16 |
/di { newpath 2 index 1 index add 2 index moveto
|
|
| 17 |
2 index 2 index 2 index add lineto |
|
| 18 |
2 index 1 index sub 2 index lineto |
|
| 19 |
2 index 2 index 2 index sub lineto |
|
| 20 |
closepath pop pop pop} bind def |
|
| 21 |
/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill
|
|
| 22 |
setrgbcolor 1.1 div c fill |
|
| 23 |
} bind def |
|
| 24 |
/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill
|
|
| 25 |
setrgbcolor 1.1 div sq fill |
|
| 26 |
} bind def |
|
| 27 |
/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill
|
|
| 28 |
setrgbcolor 1.1 div di fill |
|
| 29 |
} bind def |
|
| 30 |
/nfemale { 0 0 0 setrgbcolor 3 index 0.0909091 1.5 mul mul setlinewidth
|
|
| 31 |
newpath 5 index 5 index moveto 5 index 5 index 5 index 3.01 mul sub |
|
| 32 |
lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub moveto |
|
| 33 |
5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto stroke |
|
| 34 |
5 index 5 index 5 index c fill |
|
| 35 |
setrgbcolor 1.1 div c fill |
|
| 36 |
} bind def |
|
| 37 |
/nmale {
|
|
| 38 |
0 0 0 setrgbcolor 3 index 0.0909091 1.5 mul mul setlinewidth |
|
| 39 |
newpath 5 index 5 index moveto |
|
| 40 |
5 index 4 index 1 mul 1.5 mul add |
|
| 41 |
5 index 5 index 3 sqrt 1.5 mul mul add |
|
| 42 |
1 index 1 index lineto |
|
| 43 |
1 index 1 index 7 index sub moveto |
|
| 44 |
1 index 1 index lineto |
|
| 45 |
exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub lineto |
|
| 46 |
stroke |
|
| 47 |
5 index 5 index 5 index c fill |
|
| 48 |
setrgbcolor 1.1 div c fill |
|
| 49 |
} bind def |
|
| 50 |
/arrl 1 def |
|
| 51 |
/arrw 0.3 def |
|
| 52 |
/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def
|
|
| 53 |
/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx exch def
|
|
| 54 |
/w exch def /len exch def |
|
| 55 |
newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto |
|
| 56 |
len w sub arrl sub dx dy lrl |
|
| 57 |
arrw dy dx neg lrl |
|
| 58 |
dx arrl w add mul dy w 2 div arrw add mul sub |
|
| 59 |
dy arrl w add mul dx w 2 div arrw add mul add rlineto |
|
| 60 |
dx arrl w add mul neg dy w 2 div arrw add mul sub |
|
| 61 |
dy arrl w add mul neg dx w 2 div arrw add mul add rlineto |
|
| 62 |
arrw dy dx neg lrl |
|
| 63 |
len w sub arrl sub neg dx dy lrl |
|
| 64 |
closepath fill } bind def |
|
| 65 |
/cshow { 2 index 2 index moveto dup stringwidth pop
|
|
| 66 |
neg 2 div fosi .35 mul neg rmoveto show pop pop} def |
|
| 67 |
|
|
| 68 |
gsave |
|
| 69 |
15 138.307 translate |
|
| 70 |
12.7843 dup scale |
|
| 71 |
90 rotate |
|
| 72 |
0.608112 -43.6081 translate |
|
| 73 |
%Edges: |
|
| 74 |
gsave |
|
| 75 |
9 31 9.5 30.5 10 30 0 0 0 0.091217 lb |
|
| 76 |
9 31 5.5 34.5 2 38 0 0 0 0.091217 lb |
|
| 77 |
9 31 25.5 16 42 1 0 0 0 0.091217 lb |
|
| 78 |
3 40 23 20.5 43 1 0 0 0 0.091217 lb |
|
| 79 |
3 40 22.5 20.5 42 1 0 0 0 0.091217 lb |
|
| 80 |
3 40 2.5 40.5 2 41 0 0 0 0.091217 lb |
|
| 81 |
13 25 10.5 24.5 8 24 0 0 0 0.091217 lb |
|
| 82 |
13 25 12 27 11 29 0 0 0 0.091217 lb |
|
| 83 |
3 4 2.5 3 2 2 0 0 0 0.091217 lb |
|
| 84 |
3 4 4.5 3 6 2 0 0 0 0.091217 lb |
|
| 85 |
6 25 7 24.5 8 24 0 0 0 0.091217 lb |
|
| 86 |
6 25 6 24.5 6 24 0 0 0 0.091217 lb |
|
| 87 |
34 2 33.5 2 33 2 0 0 0 0.091217 lb |
|
| 88 |
34 2 35 2 36 2 0 0 0 0.091217 lb |
|
| 89 |
6 8 16 9 26 10 0 0 0 0.091217 lb |
|
| 90 |
6 8 6 10.5 6 13 0 0 0 0.091217 lb |
|
| 91 |
6 8 6 7.5 6 7 0 0 0 0.091217 lb |
|
| 92 |
26 10 27.5 8.5 29 7 0 0 0 0.091217 lb |
|
| 93 |
26 10 27.5 9 29 8 0 0 0 0.091217 lb |
|
| 94 |
10 30 10.5 29.5 11 29 0 0 0 0.091217 lb |
|
| 95 |
8 24 7 23.5 6 23 0 0 0 0.091217 lb |
|
| 96 |
8 24 8 24.5 8 25 0 0 0 0.091217 lb |
|
| 97 |
33 2 32.5 2 32 2 0 0 0 0.091217 lb |
|
| 98 |
29 7 17.5 7 6 7 0 0 0 0.091217 lb |
|
| 99 |
2 2 1.5 22 1 42 0 0 0 0.091217 lb |
|
| 100 |
2 2 3.5 2 5 2 0 0 0 0.091217 lb |
|
| 101 |
21 15 13.5 14.5 6 14 0 0 0 0.091217 lb |
|
| 102 |
21 15 21 15.5 21 16 0 0 0 0.091217 lb |
|
| 103 |
1 42 0.5 42.5 0 43 0 0 0 0.091217 lb |
|
| 104 |
1 42 1.5 41.5 2 41 0 0 0 0.091217 lb |
|
| 105 |
6 15 6 15.5 6 16 0 0 0 0.091217 lb |
|
| 106 |
6 15 6 14.5 6 14 0 0 0 0.091217 lb |
|
| 107 |
43 1 22 0.5 1 0 0 0 0 0.091217 lb |
|
| 108 |
31 2 18.5 2 6 2 0 0 0 0.091217 lb |
|
| 109 |
31 2 31.5 2 32 2 0 0 0 0.091217 lb |
|
| 110 |
6 24 6 23.5 6 23 0 0 0 0.091217 lb |
|
| 111 |
6 16 6 16.5 6 17 0 0 0 0.091217 lb |
|
| 112 |
6 23 6 20 6 17 0 0 0 0.091217 lb |
|
| 113 |
6 2 5.5 2 5 2 0 0 0 0.091217 lb |
|
| 114 |
6 2 6 4.5 6 7 0 0 0 0.091217 lb |
|
| 115 |
0 43 0.5 21.5 1 0 0 0 0 0.091217 lb |
|
| 116 |
1 1 19.5 1.5 38 2 0 0 0 0.091217 lb |
|
| 117 |
1 1 1 0.5 1 0 0 0 0 0.091217 lb |
|
| 118 |
2 38 5.5 31.5 9 25 0 0 0 0.091217 lb |
|
| 119 |
25 13 15.5 13 6 13 0 0 0 0.091217 lb |
|
| 120 |
25 13 15.5 13.5 6 14 0 0 0 0.091217 lb |
|
| 121 |
8 25 8.5 25 9 25 0 0 0 0.091217 lb |
|
| 122 |
11 29 24.5 15.5 38 2 0 0 0 0.091217 lb |
|
| 123 |
6 17 11.5 18 17 19 0 0 0 0.091217 lb |
|
| 124 |
16 23 26.5 12.5 37 2 0 0 0 0.091217 lb |
|
| 125 |
16 23 18.5 19.5 21 16 0 0 0 0.091217 lb |
|
| 126 |
36 2 36.5 2 37 2 0 0 0 0.091217 lb |
|
| 127 |
36 2 32.5 5 29 8 0 0 0 0.091217 lb |
|
| 128 |
6 13 6 13.5 6 14 0 0 0 0.091217 lb |
|
| 129 |
37 2 37.5 2 38 2 0 0 0 0.091217 lb |
|
| 130 |
21 16 19 17.5 17 19 0 0 0 0.091217 lb |
|
| 131 |
grestore |
|
| 132 |
%Nodes: |
|
| 133 |
gsave |
|
| 134 |
29 8 0.304556 1 1 1 nc |
|
| 135 |
2 41 0.304556 1 1 1 nc |
|
| 136 |
6 7 0.304556 1 1 1 nc |
|
| 137 |
5 2 0.304556 1 1 1 nc |
|
| 138 |
17 19 0.304556 1 1 1 nc |
|
| 139 |
21 16 0.304556 1 1 1 nc |
|
| 140 |
1 0 0.304556 1 1 1 nc |
|
| 141 |
9 25 0.304556 1 1 1 nc |
|
| 142 |
6 14 0.304556 1 1 1 nc |
|
| 143 |
42 1 0.304556 1 1 1 nc |
|
| 144 |
38 2 0.304556 1 1 1 nc |
|
| 145 |
37 2 0.304556 1 1 1 nc |
|
| 146 |
6 13 0.304556 1 1 1 nc |
|
| 147 |
36 2 0.304556 1 1 1 nc |
|
| 148 |
16 23 0.304556 1 1 1 nc |
|
| 149 |
6 17 0.304556 1 1 1 nc |
|
| 150 |
11 29 0.304556 1 1 1 nc |
|
| 151 |
8 25 0.304556 1 1 1 nc |
|
| 152 |
32 2 0.304556 1 1 1 nc |
|
| 153 |
25 13 0.304556 1 1 1 nc |
|
| 154 |
2 38 0.304556 1 1 1 nc |
|
| 155 |
1 1 0.304556 1 1 1 nc |
|
| 156 |
0 43 0.304556 1 1 1 nc |
|
| 157 |
6 2 0.304556 1 1 1 nc |
|
| 158 |
6 23 0.304556 1 1 1 nc |
|
| 159 |
6 16 0.304556 1 1 1 nc |
|
| 160 |
6 24 0.304556 1 1 1 nc |
|
| 161 |
31 2 0.304556 1 1 1 nc |
|
| 162 |
43 1 0.304556 1 1 1 nc |
|
| 163 |
6 15 0.304556 1 1 1 nc |
|
| 164 |
1 42 0.304556 1 1 1 nc |
|
| 165 |
21 15 0.304556 1 1 1 nc |
|
| 166 |
2 2 0.304556 1 1 1 nc |
|
| 167 |
29 7 0.304556 1 1 1 nc |
|
| 168 |
33 2 0.304556 1 1 1 nc |
|
| 169 |
8 24 0.304556 1 1 1 nc |
|
| 170 |
10 30 0.304556 1 1 1 nc |
|
| 171 |
26 10 0.304556 1 1 1 nc |
|
| 172 |
6 8 0.304556 1 1 1 nc |
|
| 173 |
34 2 0.304556 1 1 1 nc |
|
| 174 |
6 25 0.304556 1 1 1 nc |
|
| 175 |
3 4 0.304556 1 1 1 nc |
|
| 176 |
13 25 0.304556 1 1 1 nc |
|
| 177 |
3 40 0.304556 1 1 1 nc |
|
| 178 |
9 31 0.304556 1 1 1 nc |
|
| 179 |
grestore |
|
| 180 |
grestore |
|
| 181 |
showpage |
| ... | ... |
@@ -80,96 +80,118 @@ |
| 80 | 80 |
Build the programs in the tools subdirectory (default). |
| 81 | 81 |
|
| 82 | 82 |
--disable-tools |
| 83 | 83 |
|
| 84 | 84 |
Do not build the programs in the tools subdirectory. |
| 85 | 85 |
|
| 86 | 86 |
--with-glpk[=PREFIX] |
| 87 | 87 |
|
| 88 | 88 |
Enable GLPK support (default). You should specify the prefix too if |
| 89 | 89 |
you installed GLPK to some non-standard location (e.g. your home |
| 90 | 90 |
directory). If it is not found, GLPK support will be disabled. |
| 91 | 91 |
|
| 92 | 92 |
--with-glpk-includedir=DIR |
| 93 | 93 |
|
| 94 | 94 |
The directory where the GLPK header files are located. This is only |
| 95 | 95 |
useful when the GLPK headers and libraries are not under the same |
| 96 | 96 |
prefix (which is unlikely). |
| 97 | 97 |
|
| 98 | 98 |
--with-glpk-libdir=DIR |
| 99 | 99 |
|
| 100 | 100 |
The directory where the GLPK libraries are located. This is only |
| 101 | 101 |
useful when the GLPK headers and libraries are not under the same |
| 102 | 102 |
prefix (which is unlikely). |
| 103 | 103 |
|
| 104 | 104 |
--without-glpk |
| 105 | 105 |
|
| 106 | 106 |
Disable GLPK support. |
| 107 | 107 |
|
| 108 | 108 |
--with-cplex[=PREFIX] |
| 109 | 109 |
|
| 110 | 110 |
Enable CPLEX support (default). You should specify the prefix too |
| 111 | 111 |
if you installed CPLEX to some non-standard location |
| 112 | 112 |
(e.g. /opt/ilog/cplex75). If it is not found, CPLEX support will be |
| 113 | 113 |
disabled. |
| 114 | 114 |
|
| 115 | 115 |
--with-cplex-includedir=DIR |
| 116 | 116 |
|
| 117 | 117 |
The directory where the CPLEX header files are located. This is |
| 118 | 118 |
only useful when the CPLEX headers and libraries are not under the |
| 119 | 119 |
same prefix (e.g. /usr/local/cplex/cplex75/include). |
| 120 | 120 |
|
| 121 | 121 |
--with-cplex-libdir=DIR |
| 122 | 122 |
|
| 123 | 123 |
The directory where the CPLEX libraries are located. This is only |
| 124 | 124 |
useful when the CPLEX headers and libraries are not under the same |
| 125 | 125 |
prefix (e.g. |
| 126 | 126 |
/usr/local/cplex/cplex75/lib/i86_linux2_glibc2.2_gcc3.0/static_pic_mt). |
| 127 | 127 |
|
| 128 | 128 |
--without-cplex |
| 129 | 129 |
|
| 130 | 130 |
Disable CPLEX support. |
| 131 | 131 |
|
| 132 | 132 |
--with-soplex[=PREFIX] |
| 133 | 133 |
|
| 134 | 134 |
Enable SoPlex support (default). You should specify the prefix too if |
| 135 | 135 |
you installed SoPlex to some non-standard location (e.g. your home |
| 136 | 136 |
directory). If it is not found, SoPlex support will be disabled. |
| 137 | 137 |
|
| 138 | 138 |
--with-soplex-includedir=DIR |
| 139 | 139 |
|
| 140 | 140 |
The directory where the SoPlex header files are located. This is only |
| 141 | 141 |
useful when the SoPlex headers and libraries are not under the same |
| 142 | 142 |
prefix (which is unlikely). |
| 143 | 143 |
|
| 144 | 144 |
--with-soplex-libdir=DIR |
| 145 | 145 |
|
| 146 | 146 |
The directory where the SoPlex libraries are located. This is only |
| 147 | 147 |
useful when the SoPlex headers and libraries are not under the same |
| 148 | 148 |
prefix (which is unlikely). |
| 149 | 149 |
|
| 150 | 150 |
--without-soplex |
| 151 | 151 |
|
| 152 | 152 |
Disable SoPlex support. |
| 153 | 153 |
|
| 154 | 154 |
--with-coin[=PREFIX] |
| 155 | 155 |
|
| 156 | 156 |
Enable support for COIN-OR solvers (CLP and CBC). You should |
| 157 | 157 |
specify the prefix too. (by default, COIN-OR tools install |
| 158 | 158 |
themselves to the source code directory). This command enables the |
| 159 | 159 |
solvers that are actually found. |
| 160 | 160 |
|
| 161 | 161 |
--with-coin-includedir=DIR |
| 162 | 162 |
|
| 163 | 163 |
The directory where the COIN-OR header files are located. This is |
| 164 | 164 |
only useful when the COIN-OR headers and libraries are not under |
| 165 | 165 |
the same prefix (which is unlikely). |
| 166 | 166 |
|
| 167 | 167 |
--with-coin-libdir=DIR |
| 168 | 168 |
|
| 169 | 169 |
The directory where the COIN-OR libraries are located. This is only |
| 170 | 170 |
useful when the COIN-OR headers and libraries are not under the |
| 171 | 171 |
same prefix (which is unlikely). |
| 172 | 172 |
|
| 173 | 173 |
--without-coin |
| 174 | 174 |
|
| 175 | 175 |
Disable COIN-OR support. |
| 176 |
|
|
| 177 |
|
|
| 178 |
Makefile Variables |
|
| 179 |
================== |
|
| 180 |
|
|
| 181 |
Some Makefile variables are reserved by the GNU Coding Standards for |
|
| 182 |
the use of the "user" - the person building the package. For instance, |
|
| 183 |
CXX and CXXFLAGS are such variables, and have the same meaning as |
|
| 184 |
explained in the previous section. These variables can be set on the |
|
| 185 |
command line when invoking `make' like this: |
|
| 186 |
`make [VARIABLE=VALUE]...' |
|
| 187 |
|
|
| 188 |
WARNINGCXXFLAGS is a non-standard Makefile variable introduced by us |
|
| 189 |
to hold several compiler flags related to warnings. Its default value |
|
| 190 |
can be overridden when invoking `make'. For example to disable all |
|
| 191 |
warning flags use `make WARNINGCXXFLAGS='. |
|
| 192 |
|
|
| 193 |
In order to turn off a single flag from the default set of warning |
|
| 194 |
flags, you can use the CXXFLAGS variable, since this is passed after |
|
| 195 |
WARNINGCXXFLAGS. For example to turn off `-Wold-style-cast' (which is |
|
| 196 |
used by default when g++ is detected) you can use |
|
| 197 |
`make CXXFLAGS="-g -O2 -Wno-old-style-cast"'. |
| 1 | 1 |
SET(PACKAGE_NAME ${PROJECT_NAME})
|
| 2 | 2 |
SET(PACKAGE_VERSION ${PROJECT_VERSION})
|
| 3 | 3 |
SET(abs_top_srcdir ${PROJECT_SOURCE_DIR})
|
| 4 | 4 |
SET(abs_top_builddir ${PROJECT_BINARY_DIR})
|
| 5 | 5 |
|
| 6 | 6 |
CONFIGURE_FILE( |
| 7 | 7 |
${PROJECT_SOURCE_DIR}/doc/Doxyfile.in
|
| 8 | 8 |
${PROJECT_BINARY_DIR}/doc/Doxyfile
|
| 9 | 9 |
@ONLY |
| 10 | 10 |
) |
| 11 | 11 |
|
| 12 | 12 |
IF(DOXYGEN_EXECUTABLE AND PYTHONINTERP_FOUND AND GHOSTSCRIPT_EXECUTABLE) |
| 13 | 13 |
FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/)
|
| 14 | 14 |
SET(GHOSTSCRIPT_OPTIONS -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha) |
| 15 | 15 |
ADD_CUSTOM_TARGET(html |
| 16 | 16 |
COMMAND ${CMAKE_COMMAND} -E remove_directory gen-images
|
| 17 | 17 |
COMMAND ${CMAKE_COMMAND} -E make_directory gen-images
|
| 18 | 18 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/bipartite_matching.png ${CMAKE_CURRENT_SOURCE_DIR}/images/bipartite_matching.eps
|
| 19 | 19 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/bipartite_partitions.png ${CMAKE_CURRENT_SOURCE_DIR}/images/bipartite_partitions.eps
|
| 20 | 20 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/connected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/connected_components.eps
|
| 21 | 21 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/edge_biconnected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/edge_biconnected_components.eps
|
| 22 | 22 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/grid_graph.png ${CMAKE_CURRENT_SOURCE_DIR}/images/grid_graph.eps
|
| 23 | 23 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/node_biconnected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/node_biconnected_components.eps
|
| 24 | 24 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_0.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_0.eps
|
| 25 | 25 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_1.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_1.eps
|
| 26 | 26 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_2.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_2.eps
|
| 27 | 27 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_3.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_3.eps
|
| 28 | 28 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_4.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_4.eps
|
| 29 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/planar.png ${CMAKE_CURRENT_SOURCE_DIR}/images/planar.eps
|
|
| 29 | 30 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/strongly_connected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/strongly_connected_components.eps
|
| 30 | 31 |
COMMAND ${CMAKE_COMMAND} -E remove_directory html
|
| 31 | 32 |
COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/scripts/bib2dox.py ${CMAKE_CURRENT_SOURCE_DIR}/references.bib >references.dox
|
| 32 | 33 |
COMMAND ${DOXYGEN_EXECUTABLE} Doxyfile
|
| 33 | 34 |
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
| 34 | 35 |
) |
| 35 | 36 |
|
| 36 | 37 |
SET_TARGET_PROPERTIES(html PROPERTIES PROJECT_LABEL BUILD_DOC) |
| 37 | 38 |
|
| 38 | 39 |
IF(UNIX) |
| 39 | 40 |
INSTALL( |
| 40 | 41 |
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/
|
| 41 | 42 |
DESTINATION share/doc/lemon/html |
| 42 | 43 |
COMPONENT html_documentation |
| 43 | 44 |
) |
| 44 | 45 |
ELSEIF(WIN32) |
| 45 | 46 |
INSTALL( |
| 46 | 47 |
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/
|
| 47 | 48 |
DESTINATION doc |
| 48 | 49 |
COMPONENT html_documentation |
| 49 | 50 |
) |
| 50 | 51 |
ENDIF() |
| 51 | 52 |
|
| 52 | 53 |
ENDIF() |
| 1 | 1 |
EXTRA_DIST += \ |
| 2 | 2 |
doc/Doxyfile.in \ |
| 3 | 3 |
doc/DoxygenLayout.xml \ |
| 4 | 4 |
doc/coding_style.dox \ |
| 5 | 5 |
doc/dirs.dox \ |
| 6 | 6 |
doc/groups.dox \ |
| 7 | 7 |
doc/lgf.dox \ |
| 8 | 8 |
doc/license.dox \ |
| 9 | 9 |
doc/mainpage.dox \ |
| 10 | 10 |
doc/migration.dox \ |
| 11 | 11 |
doc/min_cost_flow.dox \ |
| 12 | 12 |
doc/named-param.dox \ |
| 13 | 13 |
doc/namespaces.dox \ |
| 14 | 14 |
doc/html \ |
| 15 | 15 |
doc/CMakeLists.txt |
| 16 | 16 |
|
| 17 | 17 |
DOC_EPS_IMAGES18 = \ |
| 18 | 18 |
grid_graph.eps \ |
| 19 | 19 |
nodeshape_0.eps \ |
| 20 | 20 |
nodeshape_1.eps \ |
| 21 | 21 |
nodeshape_2.eps \ |
| 22 | 22 |
nodeshape_3.eps \ |
| 23 | 23 |
nodeshape_4.eps |
| 24 | 24 |
|
| 25 | 25 |
DOC_EPS_IMAGES27 = \ |
| 26 | 26 |
bipartite_matching.eps \ |
| 27 | 27 |
bipartite_partitions.eps \ |
| 28 | 28 |
connected_components.eps \ |
| 29 | 29 |
edge_biconnected_components.eps \ |
| 30 | 30 |
node_biconnected_components.eps \ |
| 31 |
planar.eps \ |
|
| 31 | 32 |
strongly_connected_components.eps |
| 32 | 33 |
|
| 33 | 34 |
DOC_EPS_IMAGES = \ |
| 34 | 35 |
$(DOC_EPS_IMAGES18) \ |
| 35 | 36 |
$(DOC_EPS_IMAGES27) |
| 36 | 37 |
|
| 37 | 38 |
DOC_PNG_IMAGES = \ |
| 38 | 39 |
$(DOC_EPS_IMAGES:%.eps=doc/gen-images/%.png) |
| 39 | 40 |
|
| 40 | 41 |
EXTRA_DIST += $(DOC_EPS_IMAGES:%=doc/images/%) |
| 41 | 42 |
|
| 42 | 43 |
doc/html: |
| 43 | 44 |
$(MAKE) $(AM_MAKEFLAGS) html |
| 44 | 45 |
|
| 45 | 46 |
GS_COMMAND=gs -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 |
| 46 | 47 |
|
| 47 | 48 |
$(DOC_EPS_IMAGES18:%.eps=doc/gen-images/%.png): doc/gen-images/%.png: doc/images/%.eps |
| 48 | 49 |
-mkdir doc/gen-images |
| 49 | 50 |
if test ${gs_found} = yes; then \
|
| 50 | 51 |
$(GS_COMMAND) -sDEVICE=pngalpha -r18 -sOutputFile=$@ $<; \ |
| 51 | 52 |
else \ |
| 52 | 53 |
echo; \ |
| 53 | 54 |
echo "Ghostscript not found."; \ |
| 54 | 55 |
echo; \ |
| 55 | 56 |
exit 1; \ |
| 56 | 57 |
fi |
| 57 | 58 |
|
| 58 | 59 |
$(DOC_EPS_IMAGES27:%.eps=doc/gen-images/%.png): doc/gen-images/%.png: doc/images/%.eps |
| 59 | 60 |
-mkdir doc/gen-images |
| 60 | 61 |
if test ${gs_found} = yes; then \
|
| 61 | 62 |
$(GS_COMMAND) -sDEVICE=pngalpha -r27 -sOutputFile=$@ $<; \ |
| 62 | 63 |
else \ |
| 63 | 64 |
echo; \ |
| 64 | 65 |
echo "Ghostscript not found."; \ |
| 65 | 66 |
echo; \ |
| 66 | 67 |
exit 1; \ |
| 67 | 68 |
fi |
| 68 | 69 |
|
| 69 | 70 |
references.dox: doc/references.bib |
| 70 | 71 |
if test ${python_found} = yes; then \
|
| 71 | 72 |
cd doc; \ |
| 72 | 73 |
python @abs_top_srcdir@/scripts/bib2dox.py @abs_top_builddir@/$< >$@; \ |
| 73 | 74 |
cd ..; \ |
| 74 | 75 |
else \ |
| 75 | 76 |
echo; \ |
| 76 | 77 |
echo "Python not found."; \ |
| 77 | 78 |
echo; \ |
| 78 | 79 |
exit 1; \ |
| 79 | 80 |
fi |
| 80 | 81 |
|
| 81 | 82 |
html-local: $(DOC_PNG_IMAGES) references.dox |
| 82 | 83 |
if test ${doxygen_found} = yes; then \
|
| 83 | 84 |
cd doc; \ |
| 84 | 85 |
doxygen Doxyfile; \ |
| 85 | 86 |
cd ..; \ |
| 86 | 87 |
else \ |
| 87 | 88 |
echo; \ |
| 88 | 89 |
echo "Doxygen not found."; \ |
| 89 | 90 |
echo; \ |
| 90 | 91 |
exit 1; \ |
| 91 | 92 |
fi |
| 92 | 93 |
|
| 93 | 94 |
clean-local: |
| 94 | 95 |
-rm -rf doc/html |
| 95 | 96 |
-rm -f doc/doxygen.log |
| 96 | 97 |
-rm -f $(DOC_PNG_IMAGES) |
| 97 | 98 |
-rm -rf doc/gen-images |
| 98 | 99 |
|
| 99 | 100 |
update-external-tags: |
| 100 | 101 |
wget -O doc/libstdc++.tag.tmp http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/libstdc++.tag && \ |
| 101 | 102 |
mv doc/libstdc++.tag.tmp doc/libstdc++.tag || \ |
| 102 | 103 |
rm doc/libstdc++.tag.tmp |
| 103 | 104 |
|
| 104 | 105 |
install-html-local: doc/html |
| 105 | 106 |
@$(NORMAL_INSTALL) |
| 106 | 107 |
$(mkinstalldirs) $(DESTDIR)$(htmldir)/html |
| 107 | 108 |
for p in doc/html/*.{html,css,png,map,gif,tag} ; do \
|
| 108 | 109 |
f="`echo $$p | sed -e 's|^.*/||'`"; \ |
| 109 | 110 |
echo " $(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/html/$$f"; \ |
| 110 | 111 |
$(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/html/$$f; \ |
| 111 | 112 |
done |
| 112 | 113 |
|
| 113 | 114 |
uninstall-local: |
| 114 | 115 |
@$(NORMAL_UNINSTALL) |
| 115 | 116 |
for p in doc/html/*.{html,css,png,map,gif,tag} ; do \
|
| 116 | 117 |
f="`echo $$p | sed -e 's|^.*/||'`"; \ |
| 117 | 118 |
echo " rm -f $(DESTDIR)$(htmldir)/html/$$f"; \ |
| 118 | 119 |
rm -f $(DESTDIR)$(htmldir)/html/$$f; \ |
| 119 | 120 |
done |
| 120 | 121 |
|
| 121 | 122 |
.PHONY: update-external-tags |
| ... | ... |
@@ -78,192 +78,197 @@ |
| 78 | 78 |
static Value plus(const Value& left, const Value& right) {
|
| 79 | 79 |
if (left == infinity() || right == infinity()) return infinity(); |
| 80 | 80 |
return left + right; |
| 81 | 81 |
} |
| 82 | 82 |
static bool less(const Value& left, const Value& right) {
|
| 83 | 83 |
return left < right; |
| 84 | 84 |
} |
| 85 | 85 |
}; |
| 86 | 86 |
|
| 87 | 87 |
/// \brief Default traits class of BellmanFord class. |
| 88 | 88 |
/// |
| 89 | 89 |
/// Default traits class of BellmanFord class. |
| 90 | 90 |
/// \param GR The type of the digraph. |
| 91 | 91 |
/// \param LEN The type of the length map. |
| 92 | 92 |
template<typename GR, typename LEN> |
| 93 | 93 |
struct BellmanFordDefaultTraits {
|
| 94 | 94 |
/// The type of the digraph the algorithm runs on. |
| 95 | 95 |
typedef GR Digraph; |
| 96 | 96 |
|
| 97 | 97 |
/// \brief The type of the map that stores the arc lengths. |
| 98 | 98 |
/// |
| 99 | 99 |
/// The type of the map that stores the arc lengths. |
| 100 | 100 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 101 | 101 |
typedef LEN LengthMap; |
| 102 | 102 |
|
| 103 | 103 |
/// The type of the arc lengths. |
| 104 | 104 |
typedef typename LEN::Value Value; |
| 105 | 105 |
|
| 106 | 106 |
/// \brief Operation traits for Bellman-Ford algorithm. |
| 107 | 107 |
/// |
| 108 | 108 |
/// It defines the used operations and the infinity value for the |
| 109 | 109 |
/// given \c Value type. |
| 110 | 110 |
/// \see BellmanFordDefaultOperationTraits |
| 111 | 111 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
| 112 | 112 |
|
| 113 | 113 |
/// \brief The type of the map that stores the last arcs of the |
| 114 | 114 |
/// shortest paths. |
| 115 | 115 |
/// |
| 116 | 116 |
/// The type of the map that stores the last |
| 117 | 117 |
/// arcs of the shortest paths. |
| 118 | 118 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 119 | 119 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
| 120 | 120 |
|
| 121 | 121 |
/// \brief Instantiates a \c PredMap. |
| 122 | 122 |
/// |
| 123 | 123 |
/// This function instantiates a \ref PredMap. |
| 124 | 124 |
/// \param g is the digraph to which we would like to define the |
| 125 | 125 |
/// \ref PredMap. |
| 126 | 126 |
static PredMap *createPredMap(const GR& g) {
|
| 127 | 127 |
return new PredMap(g); |
| 128 | 128 |
} |
| 129 | 129 |
|
| 130 | 130 |
/// \brief The type of the map that stores the distances of the nodes. |
| 131 | 131 |
/// |
| 132 | 132 |
/// The type of the map that stores the distances of the nodes. |
| 133 | 133 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 134 | 134 |
typedef typename GR::template NodeMap<typename LEN::Value> DistMap; |
| 135 | 135 |
|
| 136 | 136 |
/// \brief Instantiates a \c DistMap. |
| 137 | 137 |
/// |
| 138 | 138 |
/// This function instantiates a \ref DistMap. |
| 139 | 139 |
/// \param g is the digraph to which we would like to define the |
| 140 | 140 |
/// \ref DistMap. |
| 141 | 141 |
static DistMap *createDistMap(const GR& g) {
|
| 142 | 142 |
return new DistMap(g); |
| 143 | 143 |
} |
| 144 | 144 |
|
| 145 | 145 |
}; |
| 146 | 146 |
|
| 147 | 147 |
/// \brief %BellmanFord algorithm class. |
| 148 | 148 |
/// |
| 149 | 149 |
/// \ingroup shortest_path |
| 150 | 150 |
/// This class provides an efficient implementation of the Bellman-Ford |
| 151 | 151 |
/// algorithm. The maximum time complexity of the algorithm is |
| 152 | 152 |
/// <tt>O(ne)</tt>. |
| 153 | 153 |
/// |
| 154 | 154 |
/// The Bellman-Ford algorithm solves the single-source shortest path |
| 155 | 155 |
/// problem when the arcs can have negative lengths, but the digraph |
| 156 | 156 |
/// should not contain directed cycles with negative total length. |
| 157 | 157 |
/// If all arc costs are non-negative, consider to use the Dijkstra |
| 158 | 158 |
/// algorithm instead, since it is more efficient. |
| 159 | 159 |
/// |
| 160 | 160 |
/// The arc lengths are passed to the algorithm using a |
| 161 | 161 |
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
| 162 | 162 |
/// kind of length. The type of the length values is determined by the |
| 163 | 163 |
/// \ref concepts::ReadMap::Value "Value" type of the length map. |
| 164 | 164 |
/// |
| 165 | 165 |
/// There is also a \ref bellmanFord() "function-type interface" for the |
| 166 | 166 |
/// Bellman-Ford algorithm, which is convenient in the simplier cases and |
| 167 | 167 |
/// it can be used easier. |
| 168 | 168 |
/// |
| 169 | 169 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 170 | 170 |
/// The default type is \ref ListDigraph. |
| 171 | 171 |
/// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
| 172 | 172 |
/// the lengths of the arcs. The default map type is |
| 173 | 173 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 174 |
/// \tparam TR The traits class that defines various types used by the |
|
| 175 |
/// algorithm. By default, it is \ref BellmanFordDefaultTraits |
|
| 176 |
/// "BellmanFordDefaultTraits<GR, LEN>". |
|
| 177 |
/// In most cases, this parameter should not be set directly, |
|
| 178 |
/// consider to use the named template parameters instead. |
|
| 174 | 179 |
#ifdef DOXYGEN |
| 175 | 180 |
template <typename GR, typename LEN, typename TR> |
| 176 | 181 |
#else |
| 177 | 182 |
template <typename GR=ListDigraph, |
| 178 | 183 |
typename LEN=typename GR::template ArcMap<int>, |
| 179 | 184 |
typename TR=BellmanFordDefaultTraits<GR,LEN> > |
| 180 | 185 |
#endif |
| 181 | 186 |
class BellmanFord {
|
| 182 | 187 |
public: |
| 183 | 188 |
|
| 184 | 189 |
///The type of the underlying digraph. |
| 185 | 190 |
typedef typename TR::Digraph Digraph; |
| 186 | 191 |
|
| 187 | 192 |
/// \brief The type of the arc lengths. |
| 188 | 193 |
typedef typename TR::LengthMap::Value Value; |
| 189 | 194 |
/// \brief The type of the map that stores the arc lengths. |
| 190 | 195 |
typedef typename TR::LengthMap LengthMap; |
| 191 | 196 |
/// \brief The type of the map that stores the last |
| 192 | 197 |
/// arcs of the shortest paths. |
| 193 | 198 |
typedef typename TR::PredMap PredMap; |
| 194 | 199 |
/// \brief The type of the map that stores the distances of the nodes. |
| 195 | 200 |
typedef typename TR::DistMap DistMap; |
| 196 | 201 |
/// The type of the paths. |
| 197 | 202 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 198 | 203 |
///\brief The \ref BellmanFordDefaultOperationTraits |
| 199 | 204 |
/// "operation traits class" of the algorithm. |
| 200 | 205 |
typedef typename TR::OperationTraits OperationTraits; |
| 201 | 206 |
|
| 202 | 207 |
///The \ref BellmanFordDefaultTraits "traits class" of the algorithm. |
| 203 | 208 |
typedef TR Traits; |
| 204 | 209 |
|
| 205 | 210 |
private: |
| 206 | 211 |
|
| 207 | 212 |
typedef typename Digraph::Node Node; |
| 208 | 213 |
typedef typename Digraph::NodeIt NodeIt; |
| 209 | 214 |
typedef typename Digraph::Arc Arc; |
| 210 | 215 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 211 | 216 |
|
| 212 | 217 |
// Pointer to the underlying digraph. |
| 213 | 218 |
const Digraph *_gr; |
| 214 | 219 |
// Pointer to the length map |
| 215 | 220 |
const LengthMap *_length; |
| 216 | 221 |
// Pointer to the map of predecessors arcs. |
| 217 | 222 |
PredMap *_pred; |
| 218 | 223 |
// Indicates if _pred is locally allocated (true) or not. |
| 219 | 224 |
bool _local_pred; |
| 220 | 225 |
// Pointer to the map of distances. |
| 221 | 226 |
DistMap *_dist; |
| 222 | 227 |
// Indicates if _dist is locally allocated (true) or not. |
| 223 | 228 |
bool _local_dist; |
| 224 | 229 |
|
| 225 | 230 |
typedef typename Digraph::template NodeMap<bool> MaskMap; |
| 226 | 231 |
MaskMap *_mask; |
| 227 | 232 |
|
| 228 | 233 |
std::vector<Node> _process; |
| 229 | 234 |
|
| 230 | 235 |
// Creates the maps if necessary. |
| 231 | 236 |
void create_maps() {
|
| 232 | 237 |
if(!_pred) {
|
| 233 | 238 |
_local_pred = true; |
| 234 | 239 |
_pred = Traits::createPredMap(*_gr); |
| 235 | 240 |
} |
| 236 | 241 |
if(!_dist) {
|
| 237 | 242 |
_local_dist = true; |
| 238 | 243 |
_dist = Traits::createDistMap(*_gr); |
| 239 | 244 |
} |
| 240 | 245 |
if(!_mask) {
|
| 241 | 246 |
_mask = new MaskMap(*_gr); |
| 242 | 247 |
} |
| 243 | 248 |
} |
| 244 | 249 |
|
| 245 | 250 |
public : |
| 246 | 251 |
|
| 247 | 252 |
typedef BellmanFord Create; |
| 248 | 253 |
|
| 249 | 254 |
/// \name Named Template Parameters |
| 250 | 255 |
|
| 251 | 256 |
///@{
|
| 252 | 257 |
|
| 253 | 258 |
template <class T> |
| 254 | 259 |
struct SetPredMapTraits : public Traits {
|
| 255 | 260 |
typedef T PredMap; |
| 256 | 261 |
static PredMap *createPredMap(const Digraph&) {
|
| 257 | 262 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 258 | 263 |
return 0; // ignore warnings |
| 259 | 264 |
} |
| 260 | 265 |
}; |
| 261 | 266 |
|
| 262 | 267 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 263 | 268 |
/// \c PredMap type. |
| 264 | 269 |
/// |
| 265 | 270 |
/// \ref named-templ-param "Named parameter" for setting |
| 266 | 271 |
/// \c PredMap type. |
| 267 | 272 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 268 | 273 |
template <class T> |
| 269 | 274 |
struct SetPredMap |
| ... | ... |
@@ -840,192 +845,195 @@ |
| 840 | 845 |
/// |
| 841 | 846 |
/// The type of the map that stores the last arcs of the shortest paths. |
| 842 | 847 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 843 | 848 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
| 844 | 849 |
|
| 845 | 850 |
/// \brief Instantiates a \c PredMap. |
| 846 | 851 |
/// |
| 847 | 852 |
/// This function instantiates a \ref PredMap. |
| 848 | 853 |
/// \param g is the digraph to which we would like to define the |
| 849 | 854 |
/// \ref PredMap. |
| 850 | 855 |
static PredMap *createPredMap(const GR &g) {
|
| 851 | 856 |
return new PredMap(g); |
| 852 | 857 |
} |
| 853 | 858 |
|
| 854 | 859 |
/// \brief The type of the map that stores the distances of the nodes. |
| 855 | 860 |
/// |
| 856 | 861 |
/// The type of the map that stores the distances of the nodes. |
| 857 | 862 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 858 | 863 |
typedef typename GR::template NodeMap<Value> DistMap; |
| 859 | 864 |
|
| 860 | 865 |
/// \brief Instantiates a \c DistMap. |
| 861 | 866 |
/// |
| 862 | 867 |
/// This function instantiates a \ref DistMap. |
| 863 | 868 |
/// \param g is the digraph to which we would like to define the |
| 864 | 869 |
/// \ref DistMap. |
| 865 | 870 |
static DistMap *createDistMap(const GR &g) {
|
| 866 | 871 |
return new DistMap(g); |
| 867 | 872 |
} |
| 868 | 873 |
|
| 869 | 874 |
///The type of the shortest paths. |
| 870 | 875 |
|
| 871 | 876 |
///The type of the shortest paths. |
| 872 | 877 |
///It must meet the \ref concepts::Path "Path" concept. |
| 873 | 878 |
typedef lemon::Path<Digraph> Path; |
| 874 | 879 |
}; |
| 875 | 880 |
|
| 876 | 881 |
/// \brief Default traits class used by BellmanFordWizard. |
| 877 | 882 |
/// |
| 878 | 883 |
/// Default traits class used by BellmanFordWizard. |
| 879 | 884 |
/// \tparam GR The type of the digraph. |
| 880 | 885 |
/// \tparam LEN The type of the length map. |
| 881 | 886 |
template <typename GR, typename LEN> |
| 882 | 887 |
class BellmanFordWizardBase |
| 883 | 888 |
: public BellmanFordWizardDefaultTraits<GR, LEN> {
|
| 884 | 889 |
|
| 885 | 890 |
typedef BellmanFordWizardDefaultTraits<GR, LEN> Base; |
| 886 | 891 |
protected: |
| 887 | 892 |
// Type of the nodes in the digraph. |
| 888 | 893 |
typedef typename Base::Digraph::Node Node; |
| 889 | 894 |
|
| 890 | 895 |
// Pointer to the underlying digraph. |
| 891 | 896 |
void *_graph; |
| 892 | 897 |
// Pointer to the length map |
| 893 | 898 |
void *_length; |
| 894 | 899 |
// Pointer to the map of predecessors arcs. |
| 895 | 900 |
void *_pred; |
| 896 | 901 |
// Pointer to the map of distances. |
| 897 | 902 |
void *_dist; |
| 898 | 903 |
//Pointer to the shortest path to the target node. |
| 899 | 904 |
void *_path; |
| 900 | 905 |
//Pointer to the distance of the target node. |
| 901 | 906 |
void *_di; |
| 902 | 907 |
|
| 903 | 908 |
public: |
| 904 | 909 |
/// Constructor. |
| 905 | 910 |
|
| 906 | 911 |
/// This constructor does not require parameters, it initiates |
| 907 | 912 |
/// all of the attributes to default values \c 0. |
| 908 | 913 |
BellmanFordWizardBase() : |
| 909 | 914 |
_graph(0), _length(0), _pred(0), _dist(0), _path(0), _di(0) {}
|
| 910 | 915 |
|
| 911 | 916 |
/// Constructor. |
| 912 | 917 |
|
| 913 | 918 |
/// This constructor requires two parameters, |
| 914 | 919 |
/// others are initiated to \c 0. |
| 915 | 920 |
/// \param gr The digraph the algorithm runs on. |
| 916 | 921 |
/// \param len The length map. |
| 917 | 922 |
BellmanFordWizardBase(const GR& gr, |
| 918 | 923 |
const LEN& len) : |
| 919 | 924 |
_graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))), |
| 920 | 925 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&len))), |
| 921 | 926 |
_pred(0), _dist(0), _path(0), _di(0) {}
|
| 922 | 927 |
|
| 923 | 928 |
}; |
| 924 | 929 |
|
| 925 | 930 |
/// \brief Auxiliary class for the function-type interface of the |
| 926 | 931 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
| 927 | 932 |
/// |
| 928 | 933 |
/// This auxiliary class is created to implement the |
| 929 | 934 |
/// \ref bellmanFord() "function-type interface" of the |
| 930 | 935 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
| 931 | 936 |
/// It does not have own \ref run() method, it uses the |
| 932 | 937 |
/// functions and features of the plain \ref BellmanFord. |
| 933 | 938 |
/// |
| 934 | 939 |
/// This class should only be used through the \ref bellmanFord() |
| 935 | 940 |
/// function, which makes it easier to use the algorithm. |
| 941 |
/// |
|
| 942 |
/// \tparam TR The traits class that defines various types used by the |
|
| 943 |
/// algorithm. |
|
| 936 | 944 |
template<class TR> |
| 937 | 945 |
class BellmanFordWizard : public TR {
|
| 938 | 946 |
typedef TR Base; |
| 939 | 947 |
|
| 940 | 948 |
typedef typename TR::Digraph Digraph; |
| 941 | 949 |
|
| 942 | 950 |
typedef typename Digraph::Node Node; |
| 943 | 951 |
typedef typename Digraph::NodeIt NodeIt; |
| 944 | 952 |
typedef typename Digraph::Arc Arc; |
| 945 | 953 |
typedef typename Digraph::OutArcIt ArcIt; |
| 946 | 954 |
|
| 947 | 955 |
typedef typename TR::LengthMap LengthMap; |
| 948 | 956 |
typedef typename LengthMap::Value Value; |
| 949 | 957 |
typedef typename TR::PredMap PredMap; |
| 950 | 958 |
typedef typename TR::DistMap DistMap; |
| 951 | 959 |
typedef typename TR::Path Path; |
| 952 | 960 |
|
| 953 | 961 |
public: |
| 954 | 962 |
/// Constructor. |
| 955 | 963 |
BellmanFordWizard() : TR() {}
|
| 956 | 964 |
|
| 957 | 965 |
/// \brief Constructor that requires parameters. |
| 958 | 966 |
/// |
| 959 | 967 |
/// Constructor that requires parameters. |
| 960 | 968 |
/// These parameters will be the default values for the traits class. |
| 961 | 969 |
/// \param gr The digraph the algorithm runs on. |
| 962 | 970 |
/// \param len The length map. |
| 963 | 971 |
BellmanFordWizard(const Digraph& gr, const LengthMap& len) |
| 964 | 972 |
: TR(gr, len) {}
|
| 965 | 973 |
|
| 966 | 974 |
/// \brief Copy constructor |
| 967 | 975 |
BellmanFordWizard(const TR &b) : TR(b) {}
|
| 968 | 976 |
|
| 969 | 977 |
~BellmanFordWizard() {}
|
| 970 | 978 |
|
| 971 | 979 |
/// \brief Runs the Bellman-Ford algorithm from the given source node. |
| 972 | 980 |
/// |
| 973 | 981 |
/// This method runs the Bellman-Ford algorithm from the given source |
| 974 | 982 |
/// node in order to compute the shortest path to each node. |
| 975 | 983 |
void run(Node s) {
|
| 976 | 984 |
BellmanFord<Digraph,LengthMap,TR> |
| 977 | 985 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
| 978 | 986 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
| 979 | 987 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 980 | 988 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 981 | 989 |
bf.run(s); |
| 982 | 990 |
} |
| 983 | 991 |
|
| 984 | 992 |
/// \brief Runs the Bellman-Ford algorithm to find the shortest path |
| 985 | 993 |
/// between \c s and \c t. |
| 986 | 994 |
/// |
| 987 | 995 |
/// This method runs the Bellman-Ford algorithm from node \c s |
| 988 | 996 |
/// in order to compute the shortest path to node \c t. |
| 989 | 997 |
/// Actually, it computes the shortest path to each node, but using |
| 990 | 998 |
/// this function you can retrieve the distance and the shortest path |
| 991 | 999 |
/// for a single target node easier. |
| 992 | 1000 |
/// |
| 993 | 1001 |
/// \return \c true if \c t is reachable form \c s. |
| 994 | 1002 |
bool run(Node s, Node t) {
|
| 995 | 1003 |
BellmanFord<Digraph,LengthMap,TR> |
| 996 | 1004 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
| 997 | 1005 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
| 998 | 1006 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 999 | 1007 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1000 | 1008 |
bf.run(s); |
| 1001 | 1009 |
if (Base::_path) *reinterpret_cast<Path*>(Base::_path) = bf.path(t); |
| 1002 | 1010 |
if (Base::_di) *reinterpret_cast<Value*>(Base::_di) = bf.dist(t); |
| 1003 | 1011 |
return bf.reached(t); |
| 1004 | 1012 |
} |
| 1005 | 1013 |
|
| 1006 | 1014 |
template<class T> |
| 1007 | 1015 |
struct SetPredMapBase : public Base {
|
| 1008 | 1016 |
typedef T PredMap; |
| 1009 | 1017 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
| 1010 | 1018 |
SetPredMapBase(const TR &b) : TR(b) {}
|
| 1011 | 1019 |
}; |
| 1012 | 1020 |
|
| 1013 | 1021 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1014 | 1022 |
/// the predecessor map. |
| 1015 | 1023 |
/// |
| 1016 | 1024 |
/// \ref named-templ-param "Named parameter" for setting |
| 1017 | 1025 |
/// the map that stores the predecessor arcs of the nodes. |
| 1018 | 1026 |
template<class T> |
| 1019 | 1027 |
BellmanFordWizard<SetPredMapBase<T> > predMap(const T &t) {
|
| 1020 | 1028 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1021 | 1029 |
return BellmanFordWizard<SetPredMapBase<T> >(*this); |
| 1022 | 1030 |
} |
| 1023 | 1031 |
|
| 1024 | 1032 |
template<class T> |
| 1025 | 1033 |
struct SetDistMapBase : public Base {
|
| 1026 | 1034 |
typedef T DistMap; |
| 1027 | 1035 |
static DistMap *createDistMap(const Digraph &) { return 0; };
|
| 1028 | 1036 |
SetDistMapBase(const TR &b) : TR(b) {}
|
| 1029 | 1037 |
}; |
| 1030 | 1038 |
|
| 1031 | 1039 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| ... | ... |
@@ -28,192 +28,197 @@ |
| 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 conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 52 | 52 |
///Instantiates a \c PredMap. |
| 53 | 53 |
|
| 54 | 54 |
///This function instantiates a \ref PredMap. |
| 55 | 55 |
///\param g is the digraph, to which we would like to define the |
| 56 | 56 |
///\ref 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 conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 66 | 66 |
///By default, it is a NullMap. |
| 67 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 68 | 68 |
///Instantiates a \c ProcessedMap. |
| 69 | 69 |
|
| 70 | 70 |
///This function instantiates a \ref ProcessedMap. |
| 71 | 71 |
///\param g is the digraph, to which |
| 72 | 72 |
///we would like to define the \ref ProcessedMap |
| 73 | 73 |
#ifdef DOXYGEN |
| 74 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 75 | 75 |
#else |
| 76 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 77 | 77 |
#endif |
| 78 | 78 |
{
|
| 79 | 79 |
return new ProcessedMap(); |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 | 82 |
///The type of the map that indicates which nodes are reached. |
| 83 | 83 |
|
| 84 | 84 |
///The type of the map that indicates which nodes are reached. |
| 85 | 85 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 86 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 87 | 87 |
///Instantiates a \c ReachedMap. |
| 88 | 88 |
|
| 89 | 89 |
///This function instantiates a \ref ReachedMap. |
| 90 | 90 |
///\param g is the digraph, to which |
| 91 | 91 |
///we would like to define the \ref ReachedMap. |
| 92 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 93 | 93 |
{
|
| 94 | 94 |
return new ReachedMap(g); |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 | 97 |
///The type of the map that stores the distances of the nodes. |
| 98 | 98 |
|
| 99 | 99 |
///The type of the map that stores the distances of the nodes. |
| 100 | 100 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 101 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 102 | 102 |
///Instantiates a \c DistMap. |
| 103 | 103 |
|
| 104 | 104 |
///This function instantiates a \ref DistMap. |
| 105 | 105 |
///\param g is the digraph, to which we would like to define the |
| 106 | 106 |
///\ref DistMap. |
| 107 | 107 |
static DistMap *createDistMap(const Digraph &g) |
| 108 | 108 |
{
|
| 109 | 109 |
return new DistMap(g); |
| 110 | 110 |
} |
| 111 | 111 |
}; |
| 112 | 112 |
|
| 113 | 113 |
///%BFS algorithm class. |
| 114 | 114 |
|
| 115 | 115 |
///\ingroup search |
| 116 | 116 |
///This class provides an efficient implementation of the %BFS algorithm. |
| 117 | 117 |
/// |
| 118 | 118 |
///There is also a \ref bfs() "function-type interface" for the BFS |
| 119 | 119 |
///algorithm, which is convenient in the simplier cases and it can be |
| 120 | 120 |
///used easier. |
| 121 | 121 |
/// |
| 122 | 122 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 123 | 123 |
///The default type is \ref ListDigraph. |
| 124 |
///\tparam TR The traits class that defines various types used by the |
|
| 125 |
///algorithm. By default, it is \ref BfsDefaultTraits |
|
| 126 |
///"BfsDefaultTraits<GR>". |
|
| 127 |
///In most cases, this parameter should not be set directly, |
|
| 128 |
///consider to use the named template parameters instead. |
|
| 124 | 129 |
#ifdef DOXYGEN |
| 125 | 130 |
template <typename GR, |
| 126 | 131 |
typename TR> |
| 127 | 132 |
#else |
| 128 | 133 |
template <typename GR=ListDigraph, |
| 129 | 134 |
typename TR=BfsDefaultTraits<GR> > |
| 130 | 135 |
#endif |
| 131 | 136 |
class Bfs {
|
| 132 | 137 |
public: |
| 133 | 138 |
|
| 134 | 139 |
///The type of the digraph the algorithm runs on. |
| 135 | 140 |
typedef typename TR::Digraph Digraph; |
| 136 | 141 |
|
| 137 | 142 |
///\brief The type of the map that stores the predecessor arcs of the |
| 138 | 143 |
///shortest paths. |
| 139 | 144 |
typedef typename TR::PredMap PredMap; |
| 140 | 145 |
///The type of the map that stores the distances of the nodes. |
| 141 | 146 |
typedef typename TR::DistMap DistMap; |
| 142 | 147 |
///The type of the map that indicates which nodes are reached. |
| 143 | 148 |
typedef typename TR::ReachedMap ReachedMap; |
| 144 | 149 |
///The type of the map that indicates which nodes are processed. |
| 145 | 150 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 146 | 151 |
///The type of the paths. |
| 147 | 152 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 148 | 153 |
|
| 149 | 154 |
///The \ref BfsDefaultTraits "traits class" of the algorithm. |
| 150 | 155 |
typedef TR Traits; |
| 151 | 156 |
|
| 152 | 157 |
private: |
| 153 | 158 |
|
| 154 | 159 |
typedef typename Digraph::Node Node; |
| 155 | 160 |
typedef typename Digraph::NodeIt NodeIt; |
| 156 | 161 |
typedef typename Digraph::Arc Arc; |
| 157 | 162 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 158 | 163 |
|
| 159 | 164 |
//Pointer to the underlying digraph. |
| 160 | 165 |
const Digraph *G; |
| 161 | 166 |
//Pointer to the map of predecessor arcs. |
| 162 | 167 |
PredMap *_pred; |
| 163 | 168 |
//Indicates if _pred is locally allocated (true) or not. |
| 164 | 169 |
bool local_pred; |
| 165 | 170 |
//Pointer to the map of distances. |
| 166 | 171 |
DistMap *_dist; |
| 167 | 172 |
//Indicates if _dist is locally allocated (true) or not. |
| 168 | 173 |
bool local_dist; |
| 169 | 174 |
//Pointer to the map of reached status of the nodes. |
| 170 | 175 |
ReachedMap *_reached; |
| 171 | 176 |
//Indicates if _reached is locally allocated (true) or not. |
| 172 | 177 |
bool local_reached; |
| 173 | 178 |
//Pointer to the map of processed status of the nodes. |
| 174 | 179 |
ProcessedMap *_processed; |
| 175 | 180 |
//Indicates if _processed is locally allocated (true) or not. |
| 176 | 181 |
bool local_processed; |
| 177 | 182 |
|
| 178 | 183 |
std::vector<typename Digraph::Node> _queue; |
| 179 | 184 |
int _queue_head,_queue_tail,_queue_next_dist; |
| 180 | 185 |
int _curr_dist; |
| 181 | 186 |
|
| 182 | 187 |
//Creates the maps if necessary. |
| 183 | 188 |
void create_maps() |
| 184 | 189 |
{
|
| 185 | 190 |
if(!_pred) {
|
| 186 | 191 |
local_pred = true; |
| 187 | 192 |
_pred = Traits::createPredMap(*G); |
| 188 | 193 |
} |
| 189 | 194 |
if(!_dist) {
|
| 190 | 195 |
local_dist = true; |
| 191 | 196 |
_dist = Traits::createDistMap(*G); |
| 192 | 197 |
} |
| 193 | 198 |
if(!_reached) {
|
| 194 | 199 |
local_reached = true; |
| 195 | 200 |
_reached = Traits::createReachedMap(*G); |
| 196 | 201 |
} |
| 197 | 202 |
if(!_processed) {
|
| 198 | 203 |
local_processed = true; |
| 199 | 204 |
_processed = Traits::createProcessedMap(*G); |
| 200 | 205 |
} |
| 201 | 206 |
} |
| 202 | 207 |
|
| 203 | 208 |
protected: |
| 204 | 209 |
|
| 205 | 210 |
Bfs() {}
|
| 206 | 211 |
|
| 207 | 212 |
public: |
| 208 | 213 |
|
| 209 | 214 |
typedef Bfs Create; |
| 210 | 215 |
|
| 211 | 216 |
///\name Named Template Parameters |
| 212 | 217 |
|
| 213 | 218 |
///@{
|
| 214 | 219 |
|
| 215 | 220 |
template <class T> |
| 216 | 221 |
struct SetPredMapTraits : public Traits {
|
| 217 | 222 |
typedef T PredMap; |
| 218 | 223 |
static PredMap *createPredMap(const Digraph &) |
| 219 | 224 |
{
|
| ... | ... |
@@ -864,192 +869,195 @@ |
| 864 | 869 |
return new ProcessedMap(); |
| 865 | 870 |
} |
| 866 | 871 |
|
| 867 | 872 |
///The type of the map that indicates which nodes are reached. |
| 868 | 873 |
|
| 869 | 874 |
///The type of the map that indicates which nodes are reached. |
| 870 | 875 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 871 | 876 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 872 | 877 |
///Instantiates a ReachedMap. |
| 873 | 878 |
|
| 874 | 879 |
///This function instantiates a ReachedMap. |
| 875 | 880 |
///\param g is the digraph, to which |
| 876 | 881 |
///we would like to define the ReachedMap. |
| 877 | 882 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 878 | 883 |
{
|
| 879 | 884 |
return new ReachedMap(g); |
| 880 | 885 |
} |
| 881 | 886 |
|
| 882 | 887 |
///The type of the map that stores the distances of the nodes. |
| 883 | 888 |
|
| 884 | 889 |
///The type of the map that stores the distances of the nodes. |
| 885 | 890 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 886 | 891 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 887 | 892 |
///Instantiates a DistMap. |
| 888 | 893 |
|
| 889 | 894 |
///This function instantiates a DistMap. |
| 890 | 895 |
///\param g is the digraph, to which we would like to define |
| 891 | 896 |
///the DistMap |
| 892 | 897 |
static DistMap *createDistMap(const Digraph &g) |
| 893 | 898 |
{
|
| 894 | 899 |
return new DistMap(g); |
| 895 | 900 |
} |
| 896 | 901 |
|
| 897 | 902 |
///The type of the shortest paths. |
| 898 | 903 |
|
| 899 | 904 |
///The type of the shortest paths. |
| 900 | 905 |
///It must conform to the \ref concepts::Path "Path" concept. |
| 901 | 906 |
typedef lemon::Path<Digraph> Path; |
| 902 | 907 |
}; |
| 903 | 908 |
|
| 904 | 909 |
/// Default traits class used by BfsWizard |
| 905 | 910 |
|
| 906 | 911 |
/// Default traits class used by BfsWizard. |
| 907 | 912 |
/// \tparam GR The type of the digraph. |
| 908 | 913 |
template<class GR> |
| 909 | 914 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
| 910 | 915 |
{
|
| 911 | 916 |
|
| 912 | 917 |
typedef BfsWizardDefaultTraits<GR> Base; |
| 913 | 918 |
protected: |
| 914 | 919 |
//The type of the nodes in the digraph. |
| 915 | 920 |
typedef typename Base::Digraph::Node Node; |
| 916 | 921 |
|
| 917 | 922 |
//Pointer to the digraph the algorithm runs on. |
| 918 | 923 |
void *_g; |
| 919 | 924 |
//Pointer to the map of reached nodes. |
| 920 | 925 |
void *_reached; |
| 921 | 926 |
//Pointer to the map of processed nodes. |
| 922 | 927 |
void *_processed; |
| 923 | 928 |
//Pointer to the map of predecessors arcs. |
| 924 | 929 |
void *_pred; |
| 925 | 930 |
//Pointer to the map of distances. |
| 926 | 931 |
void *_dist; |
| 927 | 932 |
//Pointer to the shortest path to the target node. |
| 928 | 933 |
void *_path; |
| 929 | 934 |
//Pointer to the distance of the target node. |
| 930 | 935 |
int *_di; |
| 931 | 936 |
|
| 932 | 937 |
public: |
| 933 | 938 |
/// Constructor. |
| 934 | 939 |
|
| 935 | 940 |
/// This constructor does not require parameters, it initiates |
| 936 | 941 |
/// all of the attributes to \c 0. |
| 937 | 942 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
| 938 | 943 |
_dist(0), _path(0), _di(0) {}
|
| 939 | 944 |
|
| 940 | 945 |
/// Constructor. |
| 941 | 946 |
|
| 942 | 947 |
/// This constructor requires one parameter, |
| 943 | 948 |
/// others are initiated to \c 0. |
| 944 | 949 |
/// \param g The digraph the algorithm runs on. |
| 945 | 950 |
BfsWizardBase(const GR &g) : |
| 946 | 951 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
| 947 | 952 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
|
| 948 | 953 |
|
| 949 | 954 |
}; |
| 950 | 955 |
|
| 951 | 956 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
| 952 | 957 |
|
| 953 | 958 |
/// This auxiliary class is created to implement the |
| 954 | 959 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
| 955 | 960 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
| 956 | 961 |
/// functions and features of the plain \ref Bfs. |
| 957 | 962 |
/// |
| 958 | 963 |
/// This class should only be used through the \ref bfs() function, |
| 959 | 964 |
/// which makes it easier to use the algorithm. |
| 965 |
/// |
|
| 966 |
/// \tparam TR The traits class that defines various types used by the |
|
| 967 |
/// algorithm. |
|
| 960 | 968 |
template<class TR> |
| 961 | 969 |
class BfsWizard : public TR |
| 962 | 970 |
{
|
| 963 | 971 |
typedef TR Base; |
| 964 | 972 |
|
| 965 | 973 |
typedef typename TR::Digraph Digraph; |
| 966 | 974 |
|
| 967 | 975 |
typedef typename Digraph::Node Node; |
| 968 | 976 |
typedef typename Digraph::NodeIt NodeIt; |
| 969 | 977 |
typedef typename Digraph::Arc Arc; |
| 970 | 978 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 971 | 979 |
|
| 972 | 980 |
typedef typename TR::PredMap PredMap; |
| 973 | 981 |
typedef typename TR::DistMap DistMap; |
| 974 | 982 |
typedef typename TR::ReachedMap ReachedMap; |
| 975 | 983 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 976 | 984 |
typedef typename TR::Path Path; |
| 977 | 985 |
|
| 978 | 986 |
public: |
| 979 | 987 |
|
| 980 | 988 |
/// Constructor. |
| 981 | 989 |
BfsWizard() : TR() {}
|
| 982 | 990 |
|
| 983 | 991 |
/// Constructor that requires parameters. |
| 984 | 992 |
|
| 985 | 993 |
/// Constructor that requires parameters. |
| 986 | 994 |
/// These parameters will be the default values for the traits class. |
| 987 | 995 |
/// \param g The digraph the algorithm runs on. |
| 988 | 996 |
BfsWizard(const Digraph &g) : |
| 989 | 997 |
TR(g) {}
|
| 990 | 998 |
|
| 991 | 999 |
///Copy constructor |
| 992 | 1000 |
BfsWizard(const TR &b) : TR(b) {}
|
| 993 | 1001 |
|
| 994 | 1002 |
~BfsWizard() {}
|
| 995 | 1003 |
|
| 996 | 1004 |
///Runs BFS algorithm from the given source node. |
| 997 | 1005 |
|
| 998 | 1006 |
///This method runs BFS algorithm from node \c s |
| 999 | 1007 |
///in order to compute the shortest path to each node. |
| 1000 | 1008 |
void run(Node s) |
| 1001 | 1009 |
{
|
| 1002 | 1010 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 1003 | 1011 |
if (Base::_pred) |
| 1004 | 1012 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1005 | 1013 |
if (Base::_dist) |
| 1006 | 1014 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1007 | 1015 |
if (Base::_reached) |
| 1008 | 1016 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 1009 | 1017 |
if (Base::_processed) |
| 1010 | 1018 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 1011 | 1019 |
if (s!=INVALID) |
| 1012 | 1020 |
alg.run(s); |
| 1013 | 1021 |
else |
| 1014 | 1022 |
alg.run(); |
| 1015 | 1023 |
} |
| 1016 | 1024 |
|
| 1017 | 1025 |
///Finds the shortest path between \c s and \c t. |
| 1018 | 1026 |
|
| 1019 | 1027 |
///This method runs BFS algorithm from node \c s |
| 1020 | 1028 |
///in order to compute the shortest path to node \c t |
| 1021 | 1029 |
///(it stops searching when \c t is processed). |
| 1022 | 1030 |
/// |
| 1023 | 1031 |
///\return \c true if \c t is reachable form \c s. |
| 1024 | 1032 |
bool run(Node s, Node t) |
| 1025 | 1033 |
{
|
| 1026 | 1034 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 1027 | 1035 |
if (Base::_pred) |
| 1028 | 1036 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1029 | 1037 |
if (Base::_dist) |
| 1030 | 1038 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1031 | 1039 |
if (Base::_reached) |
| 1032 | 1040 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 1033 | 1041 |
if (Base::_processed) |
| 1034 | 1042 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 1035 | 1043 |
alg.run(s,t); |
| 1036 | 1044 |
if (Base::_path) |
| 1037 | 1045 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
| 1038 | 1046 |
if (Base::_di) |
| 1039 | 1047 |
*Base::_di = alg.dist(t); |
| 1040 | 1048 |
return alg.reached(t); |
| 1041 | 1049 |
} |
| 1042 | 1050 |
|
| 1043 | 1051 |
///Runs BFS algorithm to visit all nodes in the digraph. |
| 1044 | 1052 |
|
| 1045 | 1053 |
///This method runs BFS algorithm in order to visit all nodes |
| 1046 | 1054 |
///in the digraph. |
| 1047 | 1055 |
void run() |
| 1048 | 1056 |
{
|
| 1049 | 1057 |
run(INVALID); |
| 1050 | 1058 |
} |
| 1051 | 1059 |
|
| 1052 | 1060 |
template<class T> |
| 1053 | 1061 |
struct SetPredMapBase : public Base {
|
| 1054 | 1062 |
typedef T PredMap; |
| 1055 | 1063 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
| ... | ... |
@@ -1202,197 +1210,197 @@ |
| 1202 | 1210 |
void reach(const Node& node) {}
|
| 1203 | 1211 |
/// \brief Called when a node is processed. |
| 1204 | 1212 |
/// |
| 1205 | 1213 |
/// This function is called when a node is processed. |
| 1206 | 1214 |
void process(const Node& node) {}
|
| 1207 | 1215 |
/// \brief Called when an arc reaches a new node. |
| 1208 | 1216 |
/// |
| 1209 | 1217 |
/// This function is called when the BFS finds an arc whose target node |
| 1210 | 1218 |
/// is not reached yet. |
| 1211 | 1219 |
void discover(const Arc& arc) {}
|
| 1212 | 1220 |
/// \brief Called when an arc is examined but its target node is |
| 1213 | 1221 |
/// already discovered. |
| 1214 | 1222 |
/// |
| 1215 | 1223 |
/// This function is called when an arc is examined but its target node is |
| 1216 | 1224 |
/// already discovered. |
| 1217 | 1225 |
void examine(const Arc& arc) {}
|
| 1218 | 1226 |
}; |
| 1219 | 1227 |
#else |
| 1220 | 1228 |
template <typename GR> |
| 1221 | 1229 |
struct BfsVisitor {
|
| 1222 | 1230 |
typedef GR Digraph; |
| 1223 | 1231 |
typedef typename Digraph::Arc Arc; |
| 1224 | 1232 |
typedef typename Digraph::Node Node; |
| 1225 | 1233 |
void start(const Node&) {}
|
| 1226 | 1234 |
void reach(const Node&) {}
|
| 1227 | 1235 |
void process(const Node&) {}
|
| 1228 | 1236 |
void discover(const Arc&) {}
|
| 1229 | 1237 |
void examine(const Arc&) {}
|
| 1230 | 1238 |
|
| 1231 | 1239 |
template <typename _Visitor> |
| 1232 | 1240 |
struct Constraints {
|
| 1233 | 1241 |
void constraints() {
|
| 1234 | 1242 |
Arc arc; |
| 1235 | 1243 |
Node node; |
| 1236 | 1244 |
visitor.start(node); |
| 1237 | 1245 |
visitor.reach(node); |
| 1238 | 1246 |
visitor.process(node); |
| 1239 | 1247 |
visitor.discover(arc); |
| 1240 | 1248 |
visitor.examine(arc); |
| 1241 | 1249 |
} |
| 1242 | 1250 |
_Visitor& visitor; |
| 1243 | 1251 |
}; |
| 1244 | 1252 |
}; |
| 1245 | 1253 |
#endif |
| 1246 | 1254 |
|
| 1247 | 1255 |
/// \brief Default traits class of BfsVisit class. |
| 1248 | 1256 |
/// |
| 1249 | 1257 |
/// Default traits class of BfsVisit class. |
| 1250 | 1258 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 1251 | 1259 |
template<class GR> |
| 1252 | 1260 |
struct BfsVisitDefaultTraits {
|
| 1253 | 1261 |
|
| 1254 | 1262 |
/// \brief The type of the digraph the algorithm runs on. |
| 1255 | 1263 |
typedef GR Digraph; |
| 1256 | 1264 |
|
| 1257 | 1265 |
/// \brief The type of the map that indicates which nodes are reached. |
| 1258 | 1266 |
/// |
| 1259 | 1267 |
/// The type of the map that indicates which nodes are reached. |
| 1260 | 1268 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 1261 | 1269 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 1262 | 1270 |
|
| 1263 | 1271 |
/// \brief Instantiates a ReachedMap. |
| 1264 | 1272 |
/// |
| 1265 | 1273 |
/// This function instantiates a ReachedMap. |
| 1266 | 1274 |
/// \param digraph is the digraph, to which |
| 1267 | 1275 |
/// we would like to define the ReachedMap. |
| 1268 | 1276 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1269 | 1277 |
return new ReachedMap(digraph); |
| 1270 | 1278 |
} |
| 1271 | 1279 |
|
| 1272 | 1280 |
}; |
| 1273 | 1281 |
|
| 1274 | 1282 |
/// \ingroup search |
| 1275 | 1283 |
/// |
| 1276 | 1284 |
/// \brief BFS algorithm class with visitor interface. |
| 1277 | 1285 |
/// |
| 1278 | 1286 |
/// This class provides an efficient implementation of the BFS algorithm |
| 1279 | 1287 |
/// with visitor interface. |
| 1280 | 1288 |
/// |
| 1281 | 1289 |
/// The BfsVisit class provides an alternative interface to the Bfs |
| 1282 | 1290 |
/// class. It works with callback mechanism, the BfsVisit object calls |
| 1283 | 1291 |
/// the member functions of the \c Visitor class on every BFS event. |
| 1284 | 1292 |
/// |
| 1285 | 1293 |
/// This interface of the BFS algorithm should be used in special cases |
| 1286 | 1294 |
/// when extra actions have to be performed in connection with certain |
| 1287 | 1295 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
| 1288 | 1296 |
/// instead. |
| 1289 | 1297 |
/// |
| 1290 | 1298 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 1291 | 1299 |
/// The default type is \ref ListDigraph. |
| 1292 | 1300 |
/// The value of GR is not used directly by \ref BfsVisit, |
| 1293 | 1301 |
/// it is only passed to \ref BfsVisitDefaultTraits. |
| 1294 | 1302 |
/// \tparam VS The Visitor type that is used by the algorithm. |
| 1295 | 1303 |
/// \ref BfsVisitor "BfsVisitor<GR>" is an empty visitor, which |
| 1296 | 1304 |
/// does not observe the BFS events. If you want to observe the BFS |
| 1297 | 1305 |
/// events, you should implement your own visitor class. |
| 1298 |
/// \tparam TR Traits class to set various data types used by the |
|
| 1299 |
/// algorithm. The default traits class is |
|
| 1300 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<GR>". |
|
| 1301 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
|
| 1302 |
/// |
|
| 1306 |
/// \tparam TR The traits class that defines various types used by the |
|
| 1307 |
/// algorithm. By default, it is \ref BfsVisitDefaultTraits |
|
| 1308 |
/// "BfsVisitDefaultTraits<GR>". |
|
| 1309 |
/// In most cases, this parameter should not be set directly, |
|
| 1310 |
/// consider to use the named template parameters instead. |
|
| 1303 | 1311 |
#ifdef DOXYGEN |
| 1304 | 1312 |
template <typename GR, typename VS, typename TR> |
| 1305 | 1313 |
#else |
| 1306 | 1314 |
template <typename GR = ListDigraph, |
| 1307 | 1315 |
typename VS = BfsVisitor<GR>, |
| 1308 | 1316 |
typename TR = BfsVisitDefaultTraits<GR> > |
| 1309 | 1317 |
#endif |
| 1310 | 1318 |
class BfsVisit {
|
| 1311 | 1319 |
public: |
| 1312 | 1320 |
|
| 1313 | 1321 |
///The traits class. |
| 1314 | 1322 |
typedef TR Traits; |
| 1315 | 1323 |
|
| 1316 | 1324 |
///The type of the digraph the algorithm runs on. |
| 1317 | 1325 |
typedef typename Traits::Digraph Digraph; |
| 1318 | 1326 |
|
| 1319 | 1327 |
///The visitor type used by the algorithm. |
| 1320 | 1328 |
typedef VS Visitor; |
| 1321 | 1329 |
|
| 1322 | 1330 |
///The type of the map that indicates which nodes are reached. |
| 1323 | 1331 |
typedef typename Traits::ReachedMap ReachedMap; |
| 1324 | 1332 |
|
| 1325 | 1333 |
private: |
| 1326 | 1334 |
|
| 1327 | 1335 |
typedef typename Digraph::Node Node; |
| 1328 | 1336 |
typedef typename Digraph::NodeIt NodeIt; |
| 1329 | 1337 |
typedef typename Digraph::Arc Arc; |
| 1330 | 1338 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1331 | 1339 |
|
| 1332 | 1340 |
//Pointer to the underlying digraph. |
| 1333 | 1341 |
const Digraph *_digraph; |
| 1334 | 1342 |
//Pointer to the visitor object. |
| 1335 | 1343 |
Visitor *_visitor; |
| 1336 | 1344 |
//Pointer to the map of reached status of the nodes. |
| 1337 | 1345 |
ReachedMap *_reached; |
| 1338 | 1346 |
//Indicates if _reached is locally allocated (true) or not. |
| 1339 | 1347 |
bool local_reached; |
| 1340 | 1348 |
|
| 1341 | 1349 |
std::vector<typename Digraph::Node> _list; |
| 1342 | 1350 |
int _list_front, _list_back; |
| 1343 | 1351 |
|
| 1344 | 1352 |
//Creates the maps if necessary. |
| 1345 | 1353 |
void create_maps() {
|
| 1346 | 1354 |
if(!_reached) {
|
| 1347 | 1355 |
local_reached = true; |
| 1348 | 1356 |
_reached = Traits::createReachedMap(*_digraph); |
| 1349 | 1357 |
} |
| 1350 | 1358 |
} |
| 1351 | 1359 |
|
| 1352 | 1360 |
protected: |
| 1353 | 1361 |
|
| 1354 | 1362 |
BfsVisit() {}
|
| 1355 | 1363 |
|
| 1356 | 1364 |
public: |
| 1357 | 1365 |
|
| 1358 | 1366 |
typedef BfsVisit Create; |
| 1359 | 1367 |
|
| 1360 | 1368 |
/// \name Named Template Parameters |
| 1361 | 1369 |
|
| 1362 | 1370 |
///@{
|
| 1363 | 1371 |
template <class T> |
| 1364 | 1372 |
struct SetReachedMapTraits : public Traits {
|
| 1365 | 1373 |
typedef T ReachedMap; |
| 1366 | 1374 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1367 | 1375 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 1368 | 1376 |
return 0; // ignore warnings |
| 1369 | 1377 |
} |
| 1370 | 1378 |
}; |
| 1371 | 1379 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1372 | 1380 |
/// ReachedMap type. |
| 1373 | 1381 |
/// |
| 1374 | 1382 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
| 1375 | 1383 |
template <class T> |
| 1376 | 1384 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
| 1377 | 1385 |
SetReachedMapTraits<T> > {
|
| 1378 | 1386 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
| 1379 | 1387 |
}; |
| 1380 | 1388 |
///@} |
| 1381 | 1389 |
|
| 1382 | 1390 |
public: |
| 1383 | 1391 |
|
| 1384 | 1392 |
/// \brief Constructor. |
| 1385 | 1393 |
/// |
| 1386 | 1394 |
/// Constructor. |
| 1387 | 1395 |
/// |
| 1388 | 1396 |
/// \param digraph The digraph the algorithm runs on. |
| 1389 | 1397 |
/// \param visitor The visitor object of the algorithm. |
| 1390 | 1398 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
| 1391 | 1399 |
: _digraph(&digraph), _visitor(&visitor), |
| 1392 | 1400 |
_reached(0), local_reached(false) {}
|
| 1393 | 1401 |
|
| 1394 | 1402 |
/// \brief Destructor. |
| 1395 | 1403 |
~BfsVisit() {
|
| 1396 | 1404 |
if(local_reached) delete _reached; |
| 1397 | 1405 |
} |
| 1398 | 1406 |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 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_CAPACITY_SCALING_H |
| 20 | 20 |
#define LEMON_CAPACITY_SCALING_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_cost_flow_algs |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Capacity Scaling algorithm for finding a minimum cost flow. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/bin_heap.h> |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
/// \brief Default traits class of CapacityScaling algorithm. |
| 35 | 35 |
/// |
| 36 | 36 |
/// Default traits class of CapacityScaling algorithm. |
| 37 | 37 |
/// \tparam GR Digraph type. |
| 38 | 38 |
/// \tparam V The number type used for flow amounts, capacity bounds |
| 39 | 39 |
/// and supply values. By default it is \c int. |
| 40 | 40 |
/// \tparam C The number type used for costs and potentials. |
| 41 | 41 |
/// By default it is the same as \c V. |
| 42 | 42 |
template <typename GR, typename V = int, typename C = V> |
| 43 | 43 |
struct CapacityScalingDefaultTraits |
| 44 | 44 |
{
|
| 45 | 45 |
/// The type of the digraph |
| 46 | 46 |
typedef GR Digraph; |
| 47 | 47 |
/// The type of the flow amounts, capacity bounds and supply values |
| 48 | 48 |
typedef V Value; |
| 49 | 49 |
/// The type of the arc costs |
| 50 | 50 |
typedef C Cost; |
| 51 | 51 |
|
| 52 | 52 |
/// \brief The type of the heap used for internal Dijkstra computations. |
| 53 | 53 |
/// |
| 54 | 54 |
/// The type of the heap used for internal Dijkstra computations. |
| 55 | 55 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
| 56 | 56 |
/// its priority type must be \c Cost and its cross reference type |
| 57 | 57 |
/// must be \ref RangeMap "RangeMap<int>". |
| 58 | 58 |
typedef BinHeap<Cost, RangeMap<int> > Heap; |
| 59 | 59 |
}; |
| 60 | 60 |
|
| 61 | 61 |
/// \addtogroup min_cost_flow_algs |
| 62 | 62 |
/// @{
|
| 63 | 63 |
|
| 64 | 64 |
/// \brief Implementation of the Capacity Scaling algorithm for |
| 65 | 65 |
/// finding a \ref min_cost_flow "minimum cost flow". |
| 66 | 66 |
/// |
| 67 | 67 |
/// \ref CapacityScaling implements the capacity scaling version |
| 68 | 68 |
/// of the successive shortest path algorithm for finding a |
| 69 | 69 |
/// \ref min_cost_flow "minimum cost flow" \ref amo93networkflows, |
| 70 | 70 |
/// \ref edmondskarp72theoretical. It is an efficient dual |
| 71 | 71 |
/// solution method. |
| 72 | 72 |
/// |
| 73 | 73 |
/// Most of the parameters of the problem (except for the digraph) |
| 74 | 74 |
/// can be given using separate functions, and the algorithm can be |
| 75 | 75 |
/// executed using the \ref run() function. If some parameters are not |
| 76 | 76 |
/// specified, then default values will be used. |
| 77 | 77 |
/// |
| 78 | 78 |
/// \tparam GR The digraph type the algorithm runs on. |
| 79 | 79 |
/// \tparam V The number type used for flow amounts, capacity bounds |
| 80 |
/// and supply values in the algorithm. By default it is \c int. |
|
| 80 |
/// and supply values in the algorithm. By default, it is \c int. |
|
| 81 | 81 |
/// \tparam C The number type used for costs and potentials in the |
| 82 |
/// algorithm. By default it is the same as \c V. |
|
| 82 |
/// algorithm. By default, it is the same as \c V. |
|
| 83 |
/// \tparam TR The traits class that defines various types used by the |
|
| 84 |
/// algorithm. By default, it is \ref CapacityScalingDefaultTraits |
|
| 85 |
/// "CapacityScalingDefaultTraits<GR, V, C>". |
|
| 86 |
/// In most cases, this parameter should not be set directly, |
|
| 87 |
/// consider to use the named template parameters instead. |
|
| 83 | 88 |
/// |
| 84 | 89 |
/// \warning Both number types must be signed and all input data must |
| 85 | 90 |
/// be integer. |
| 86 | 91 |
/// \warning This algorithm does not support negative costs for such |
| 87 | 92 |
/// arcs that have infinite upper bound. |
| 88 | 93 |
#ifdef DOXYGEN |
| 89 | 94 |
template <typename GR, typename V, typename C, typename TR> |
| 90 | 95 |
#else |
| 91 | 96 |
template < typename GR, typename V = int, typename C = V, |
| 92 | 97 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
| 93 | 98 |
#endif |
| 94 | 99 |
class CapacityScaling |
| 95 | 100 |
{
|
| 96 | 101 |
public: |
| 97 | 102 |
|
| 98 | 103 |
/// The type of the digraph |
| 99 | 104 |
typedef typename TR::Digraph Digraph; |
| 100 | 105 |
/// The type of the flow amounts, capacity bounds and supply values |
| 101 | 106 |
typedef typename TR::Value Value; |
| 102 | 107 |
/// The type of the arc costs |
| 103 | 108 |
typedef typename TR::Cost Cost; |
| 104 | 109 |
|
| 105 | 110 |
/// The type of the heap used for internal Dijkstra computations |
| 106 | 111 |
typedef typename TR::Heap Heap; |
| 107 | 112 |
|
| 108 | 113 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
| 109 | 114 |
typedef TR Traits; |
| 110 | 115 |
|
| 111 | 116 |
public: |
| 112 | 117 |
|
| 113 | 118 |
/// \brief Problem type constants for the \c run() function. |
| 114 | 119 |
/// |
| 115 | 120 |
/// Enum type containing the problem type constants that can be |
| 116 | 121 |
/// returned by the \ref run() function of the algorithm. |
| 117 | 122 |
enum ProblemType {
|
| 118 | 123 |
/// The problem has no feasible solution (flow). |
| 119 | 124 |
INFEASIBLE, |
| 120 | 125 |
/// The problem has optimal solution (i.e. it is feasible and |
| 121 | 126 |
/// bounded), and the algorithm has found optimal flow and node |
| 122 | 127 |
/// potentials (primal and dual solutions). |
| 123 | 128 |
OPTIMAL, |
| 124 | 129 |
/// The digraph contains an arc of negative cost and infinite |
| 125 | 130 |
/// upper bound. It means that the objective function is unbounded |
| 126 | 131 |
/// on that arc, however, note that it could actually be bounded |
| 127 | 132 |
/// over the feasible flows, but this algroithm cannot handle |
| 128 | 133 |
/// these cases. |
| 129 | 134 |
UNBOUNDED |
| 130 | 135 |
}; |
| 131 | 136 |
|
| 132 | 137 |
private: |
| 133 | 138 |
|
| 134 | 139 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 135 | 140 |
|
| 136 | 141 |
typedef std::vector<int> IntVector; |
| 137 | 142 |
typedef std::vector<char> BoolVector; |
| 138 | 143 |
typedef std::vector<Value> ValueVector; |
| 139 | 144 |
typedef std::vector<Cost> CostVector; |
| 140 | 145 |
|
| 141 | 146 |
private: |
| 142 | 147 |
|
| 143 | 148 |
// Data related to the underlying digraph |
| 144 | 149 |
const GR &_graph; |
| 145 | 150 |
int _node_num; |
| 146 | 151 |
int _arc_num; |
| 147 | 152 |
int _res_arc_num; |
| 148 | 153 |
int _root; |
| 149 | 154 |
|
| 150 | 155 |
// Parameters of the problem |
| 151 | 156 |
bool _have_lower; |
| 152 | 157 |
Value _sum_supply; |
| 153 | 158 |
|
| 154 | 159 |
// Data structures for storing the digraph |
| 155 | 160 |
IntNodeMap _node_id; |
| 156 | 161 |
IntArcMap _arc_idf; |
| 157 | 162 |
IntArcMap _arc_idb; |
| 158 | 163 |
IntVector _first_out; |
| 159 | 164 |
BoolVector _forward; |
| 160 | 165 |
IntVector _source; |
| 161 | 166 |
IntVector _target; |
| 162 | 167 |
IntVector _reverse; |
| 163 | 168 |
|
| 164 | 169 |
// Node and arc data |
| 165 | 170 |
ValueVector _lower; |
| 166 | 171 |
ValueVector _upper; |
| 167 | 172 |
CostVector _cost; |
| 168 | 173 |
ValueVector _supply; |
| 169 | 174 |
|
| 170 | 175 |
ValueVector _res_cap; |
| 171 | 176 |
CostVector _pi; |
| 172 | 177 |
ValueVector _excess; |
| 173 | 178 |
IntVector _excess_nodes; |
| 174 | 179 |
IntVector _deficit_nodes; |
| 175 | 180 |
|
| 176 | 181 |
Value _delta; |
| 177 | 182 |
int _factor; |
| 178 | 183 |
IntVector _pred; |
| ... | ... |
@@ -80,192 +80,197 @@ |
| 80 | 80 |
|
| 81 | 81 |
/// \brief Instantiates a FlowMap. |
| 82 | 82 |
/// |
| 83 | 83 |
/// This function instantiates a \ref FlowMap. |
| 84 | 84 |
/// \param digraph The digraph for which we would like to define |
| 85 | 85 |
/// the flow map. |
| 86 | 86 |
static FlowMap* createFlowMap(const Digraph& digraph) {
|
| 87 | 87 |
return new FlowMap(digraph); |
| 88 | 88 |
} |
| 89 | 89 |
|
| 90 | 90 |
/// \brief The elevator type used by the algorithm. |
| 91 | 91 |
/// |
| 92 | 92 |
/// The elevator type used by the algorithm. |
| 93 | 93 |
/// |
| 94 | 94 |
/// \sa Elevator, LinkedElevator |
| 95 | 95 |
#ifdef DOXYGEN |
| 96 | 96 |
typedef lemon::Elevator<GR, GR::Node> Elevator; |
| 97 | 97 |
#else |
| 98 | 98 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
| 99 | 99 |
#endif |
| 100 | 100 |
|
| 101 | 101 |
/// \brief Instantiates an Elevator. |
| 102 | 102 |
/// |
| 103 | 103 |
/// This function instantiates an \ref Elevator. |
| 104 | 104 |
/// \param digraph The digraph for which we would like to define |
| 105 | 105 |
/// the elevator. |
| 106 | 106 |
/// \param max_level The maximum level of the elevator. |
| 107 | 107 |
static Elevator* createElevator(const Digraph& digraph, int max_level) {
|
| 108 | 108 |
return new Elevator(digraph, max_level); |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 | 111 |
/// \brief The tolerance used by the algorithm |
| 112 | 112 |
/// |
| 113 | 113 |
/// The tolerance used by the algorithm to handle inexact computation. |
| 114 | 114 |
typedef lemon::Tolerance<Value> Tolerance; |
| 115 | 115 |
|
| 116 | 116 |
}; |
| 117 | 117 |
|
| 118 | 118 |
/** |
| 119 | 119 |
\brief Push-relabel algorithm for the network circulation problem. |
| 120 | 120 |
|
| 121 | 121 |
\ingroup max_flow |
| 122 | 122 |
This class implements a push-relabel algorithm for the \e network |
| 123 | 123 |
\e circulation problem. |
| 124 | 124 |
It is to find a feasible circulation when lower and upper bounds |
| 125 | 125 |
are given for the flow values on the arcs and lower bounds are |
| 126 | 126 |
given for the difference between the outgoing and incoming flow |
| 127 | 127 |
at the nodes. |
| 128 | 128 |
|
| 129 | 129 |
The exact formulation of this problem is the following. |
| 130 | 130 |
Let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$
|
| 131 | 131 |
\f$upper: A\rightarrow\mathbf{R}\cup\{\infty\}\f$ denote the lower and
|
| 132 | 132 |
upper bounds on the arcs, for which \f$lower(uv) \leq upper(uv)\f$ |
| 133 | 133 |
holds for all \f$uv\in A\f$, and \f$sup: V\rightarrow\mathbf{R}\f$
|
| 134 | 134 |
denotes the signed supply values of the nodes. |
| 135 | 135 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
| 136 | 136 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
| 137 | 137 |
\f$-sup(u)\f$ demand. |
| 138 | 138 |
A feasible circulation is an \f$f: A\rightarrow\mathbf{R}\f$
|
| 139 | 139 |
solution of the following problem. |
| 140 | 140 |
|
| 141 | 141 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu)
|
| 142 | 142 |
\geq sup(u) \quad \forall u\in V, \f] |
| 143 | 143 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A. \f] |
| 144 | 144 |
|
| 145 | 145 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be
|
| 146 | 146 |
zero or negative in order to have a feasible solution (since the sum |
| 147 | 147 |
of the expressions on the left-hand side of the inequalities is zero). |
| 148 | 148 |
It means that the total demand must be greater or equal to the total |
| 149 | 149 |
supply and all the supplies have to be carried out from the supply nodes, |
| 150 | 150 |
but there could be demands that are not satisfied. |
| 151 | 151 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand
|
| 152 | 152 |
constraints have to be satisfied with equality, i.e. all demands |
| 153 | 153 |
have to be satisfied and all supplies have to be used. |
| 154 | 154 |
|
| 155 | 155 |
If you need the opposite inequalities in the supply/demand constraints |
| 156 | 156 |
(i.e. the total demand is less than the total supply and all the demands |
| 157 | 157 |
have to be satisfied while there could be supplies that are not used), |
| 158 | 158 |
then you could easily transform the problem to the above form by reversing |
| 159 | 159 |
the direction of the arcs and taking the negative of the supply values |
| 160 | 160 |
(e.g. using \ref ReverseDigraph and \ref NegMap adaptors). |
| 161 | 161 |
|
| 162 | 162 |
This algorithm either calculates a feasible circulation, or provides |
| 163 | 163 |
a \ref barrier() "barrier", which prooves that a feasible soultion |
| 164 | 164 |
cannot exist. |
| 165 | 165 |
|
| 166 | 166 |
Note that this algorithm also provides a feasible solution for the |
| 167 | 167 |
\ref min_cost_flow "minimum cost flow problem". |
| 168 | 168 |
|
| 169 | 169 |
\tparam GR The type of the digraph the algorithm runs on. |
| 170 | 170 |
\tparam LM The type of the lower bound map. The default |
| 171 | 171 |
map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 172 | 172 |
\tparam UM The type of the upper bound (capacity) map. |
| 173 | 173 |
The default map type is \c LM. |
| 174 | 174 |
\tparam SM The type of the supply map. The default map type is |
| 175 | 175 |
\ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>". |
| 176 |
\tparam TR The traits class that defines various types used by the |
|
| 177 |
algorithm. By default, it is \ref CirculationDefaultTraits |
|
| 178 |
"CirculationDefaultTraits<GR, LM, UM, SM>". |
|
| 179 |
In most cases, this parameter should not be set directly, |
|
| 180 |
consider to use the named template parameters instead. |
|
| 176 | 181 |
*/ |
| 177 | 182 |
#ifdef DOXYGEN |
| 178 | 183 |
template< typename GR, |
| 179 | 184 |
typename LM, |
| 180 | 185 |
typename UM, |
| 181 | 186 |
typename SM, |
| 182 | 187 |
typename TR > |
| 183 | 188 |
#else |
| 184 | 189 |
template< typename GR, |
| 185 | 190 |
typename LM = typename GR::template ArcMap<int>, |
| 186 | 191 |
typename UM = LM, |
| 187 | 192 |
typename SM = typename GR::template NodeMap<typename UM::Value>, |
| 188 | 193 |
typename TR = CirculationDefaultTraits<GR, LM, UM, SM> > |
| 189 | 194 |
#endif |
| 190 | 195 |
class Circulation {
|
| 191 | 196 |
public: |
| 192 | 197 |
|
| 193 | 198 |
///The \ref CirculationDefaultTraits "traits class" of the algorithm. |
| 194 | 199 |
typedef TR Traits; |
| 195 | 200 |
///The type of the digraph the algorithm runs on. |
| 196 | 201 |
typedef typename Traits::Digraph Digraph; |
| 197 | 202 |
///The type of the flow and supply values. |
| 198 | 203 |
typedef typename Traits::Value Value; |
| 199 | 204 |
|
| 200 | 205 |
///The type of the lower bound map. |
| 201 | 206 |
typedef typename Traits::LowerMap LowerMap; |
| 202 | 207 |
///The type of the upper bound (capacity) map. |
| 203 | 208 |
typedef typename Traits::UpperMap UpperMap; |
| 204 | 209 |
///The type of the supply map. |
| 205 | 210 |
typedef typename Traits::SupplyMap SupplyMap; |
| 206 | 211 |
///The type of the flow map. |
| 207 | 212 |
typedef typename Traits::FlowMap FlowMap; |
| 208 | 213 |
|
| 209 | 214 |
///The type of the elevator. |
| 210 | 215 |
typedef typename Traits::Elevator Elevator; |
| 211 | 216 |
///The type of the tolerance. |
| 212 | 217 |
typedef typename Traits::Tolerance Tolerance; |
| 213 | 218 |
|
| 214 | 219 |
private: |
| 215 | 220 |
|
| 216 | 221 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 217 | 222 |
|
| 218 | 223 |
const Digraph &_g; |
| 219 | 224 |
int _node_num; |
| 220 | 225 |
|
| 221 | 226 |
const LowerMap *_lo; |
| 222 | 227 |
const UpperMap *_up; |
| 223 | 228 |
const SupplyMap *_supply; |
| 224 | 229 |
|
| 225 | 230 |
FlowMap *_flow; |
| 226 | 231 |
bool _local_flow; |
| 227 | 232 |
|
| 228 | 233 |
Elevator* _level; |
| 229 | 234 |
bool _local_level; |
| 230 | 235 |
|
| 231 | 236 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
| 232 | 237 |
ExcessMap* _excess; |
| 233 | 238 |
|
| 234 | 239 |
Tolerance _tol; |
| 235 | 240 |
int _el; |
| 236 | 241 |
|
| 237 | 242 |
public: |
| 238 | 243 |
|
| 239 | 244 |
typedef Circulation Create; |
| 240 | 245 |
|
| 241 | 246 |
///\name Named Template Parameters |
| 242 | 247 |
|
| 243 | 248 |
///@{
|
| 244 | 249 |
|
| 245 | 250 |
template <typename T> |
| 246 | 251 |
struct SetFlowMapTraits : public Traits {
|
| 247 | 252 |
typedef T FlowMap; |
| 248 | 253 |
static FlowMap *createFlowMap(const Digraph&) {
|
| 249 | 254 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
| 250 | 255 |
return 0; // ignore warnings |
| 251 | 256 |
} |
| 252 | 257 |
}; |
| 253 | 258 |
|
| 254 | 259 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 255 | 260 |
/// FlowMap type |
| 256 | 261 |
/// |
| 257 | 262 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
| 258 | 263 |
/// type. |
| 259 | 264 |
template <typename T> |
| 260 | 265 |
struct SetFlowMap |
| 261 | 266 |
: public Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
| 262 | 267 |
SetFlowMapTraits<T> > {
|
| 263 | 268 |
typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
| 264 | 269 |
SetFlowMapTraits<T> > Create; |
| 265 | 270 |
}; |
| 266 | 271 |
|
| 267 | 272 |
template <typename T> |
| 268 | 273 |
struct SetElevatorTraits : public Traits {
|
| 269 | 274 |
typedef T Elevator; |
| 270 | 275 |
static Elevator *createElevator(const Digraph&, int) {
|
| 271 | 276 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
| ... | ... |
@@ -11,226 +11,230 @@ |
| 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_COST_SCALING_H |
| 20 | 20 |
#define LEMON_COST_SCALING_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_cost_flow_algs |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Cost scaling algorithm for finding a minimum cost flow. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
#include <deque> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
|
| 30 | 30 |
#include <lemon/core.h> |
| 31 | 31 |
#include <lemon/maps.h> |
| 32 | 32 |
#include <lemon/math.h> |
| 33 | 33 |
#include <lemon/static_graph.h> |
| 34 | 34 |
#include <lemon/circulation.h> |
| 35 | 35 |
#include <lemon/bellman_ford.h> |
| 36 | 36 |
|
| 37 | 37 |
namespace lemon {
|
| 38 | 38 |
|
| 39 | 39 |
/// \brief Default traits class of CostScaling algorithm. |
| 40 | 40 |
/// |
| 41 | 41 |
/// Default traits class of CostScaling algorithm. |
| 42 | 42 |
/// \tparam GR Digraph type. |
| 43 | 43 |
/// \tparam V The number type used for flow amounts, capacity bounds |
| 44 | 44 |
/// and supply values. By default it is \c int. |
| 45 | 45 |
/// \tparam C The number type used for costs and potentials. |
| 46 | 46 |
/// By default it is the same as \c V. |
| 47 | 47 |
#ifdef DOXYGEN |
| 48 | 48 |
template <typename GR, typename V = int, typename C = V> |
| 49 | 49 |
#else |
| 50 | 50 |
template < typename GR, typename V = int, typename C = V, |
| 51 | 51 |
bool integer = std::numeric_limits<C>::is_integer > |
| 52 | 52 |
#endif |
| 53 | 53 |
struct CostScalingDefaultTraits |
| 54 | 54 |
{
|
| 55 | 55 |
/// The type of the digraph |
| 56 | 56 |
typedef GR Digraph; |
| 57 | 57 |
/// The type of the flow amounts, capacity bounds and supply values |
| 58 | 58 |
typedef V Value; |
| 59 | 59 |
/// The type of the arc costs |
| 60 | 60 |
typedef C Cost; |
| 61 | 61 |
|
| 62 | 62 |
/// \brief The large cost type used for internal computations |
| 63 | 63 |
/// |
| 64 | 64 |
/// The large cost type used for internal computations. |
| 65 | 65 |
/// It is \c long \c long if the \c Cost type is integer, |
| 66 | 66 |
/// otherwise it is \c double. |
| 67 | 67 |
/// \c Cost must be convertible to \c LargeCost. |
| 68 | 68 |
typedef double LargeCost; |
| 69 | 69 |
}; |
| 70 | 70 |
|
| 71 | 71 |
// Default traits class for integer cost types |
| 72 | 72 |
template <typename GR, typename V, typename C> |
| 73 | 73 |
struct CostScalingDefaultTraits<GR, V, C, true> |
| 74 | 74 |
{
|
| 75 | 75 |
typedef GR Digraph; |
| 76 | 76 |
typedef V Value; |
| 77 | 77 |
typedef C Cost; |
| 78 | 78 |
#ifdef LEMON_HAVE_LONG_LONG |
| 79 | 79 |
typedef long long LargeCost; |
| 80 | 80 |
#else |
| 81 | 81 |
typedef long LargeCost; |
| 82 | 82 |
#endif |
| 83 | 83 |
}; |
| 84 | 84 |
|
| 85 | 85 |
|
| 86 | 86 |
/// \addtogroup min_cost_flow_algs |
| 87 | 87 |
/// @{
|
| 88 | 88 |
|
| 89 | 89 |
/// \brief Implementation of the Cost Scaling algorithm for |
| 90 | 90 |
/// finding a \ref min_cost_flow "minimum cost flow". |
| 91 | 91 |
/// |
| 92 | 92 |
/// \ref CostScaling implements a cost scaling algorithm that performs |
| 93 | 93 |
/// push/augment and relabel operations for finding a \ref min_cost_flow |
| 94 | 94 |
/// "minimum cost flow" \ref amo93networkflows, \ref goldberg90approximation, |
| 95 | 95 |
/// \ref goldberg97efficient, \ref bunnagel98efficient. |
| 96 | 96 |
/// It is a highly efficient primal-dual solution method, which |
| 97 | 97 |
/// can be viewed as the generalization of the \ref Preflow |
| 98 | 98 |
/// "preflow push-relabel" algorithm for the maximum flow problem. |
| 99 | 99 |
/// |
| 100 | 100 |
/// Most of the parameters of the problem (except for the digraph) |
| 101 | 101 |
/// can be given using separate functions, and the algorithm can be |
| 102 | 102 |
/// executed using the \ref run() function. If some parameters are not |
| 103 | 103 |
/// specified, then default values will be used. |
| 104 | 104 |
/// |
| 105 | 105 |
/// \tparam GR The digraph type the algorithm runs on. |
| 106 | 106 |
/// \tparam V The number type used for flow amounts, capacity bounds |
| 107 |
/// and supply values in the algorithm. By default it is \c int. |
|
| 107 |
/// and supply values in the algorithm. By default, it is \c int. |
|
| 108 | 108 |
/// \tparam C The number type used for costs and potentials in the |
| 109 |
/// algorithm. By default it is the same as \c V. |
|
| 109 |
/// algorithm. By default, it is the same as \c V. |
|
| 110 |
/// \tparam TR The traits class that defines various types used by the |
|
| 111 |
/// algorithm. By default, it is \ref CostScalingDefaultTraits |
|
| 112 |
/// "CostScalingDefaultTraits<GR, V, C>". |
|
| 113 |
/// In most cases, this parameter should not be set directly, |
|
| 114 |
/// consider to use the named template parameters instead. |
|
| 110 | 115 |
/// |
| 111 | 116 |
/// \warning Both number types must be signed and all input data must |
| 112 | 117 |
/// be integer. |
| 113 | 118 |
/// \warning This algorithm does not support negative costs for such |
| 114 | 119 |
/// arcs that have infinite upper bound. |
| 115 | 120 |
/// |
| 116 | 121 |
/// \note %CostScaling provides three different internal methods, |
| 117 | 122 |
/// from which the most efficient one is used by default. |
| 118 | 123 |
/// For more information, see \ref Method. |
| 119 | 124 |
#ifdef DOXYGEN |
| 120 | 125 |
template <typename GR, typename V, typename C, typename TR> |
| 121 | 126 |
#else |
| 122 | 127 |
template < typename GR, typename V = int, typename C = V, |
| 123 | 128 |
typename TR = CostScalingDefaultTraits<GR, V, C> > |
| 124 | 129 |
#endif |
| 125 | 130 |
class CostScaling |
| 126 | 131 |
{
|
| 127 | 132 |
public: |
| 128 | 133 |
|
| 129 | 134 |
/// The type of the digraph |
| 130 | 135 |
typedef typename TR::Digraph Digraph; |
| 131 | 136 |
/// The type of the flow amounts, capacity bounds and supply values |
| 132 | 137 |
typedef typename TR::Value Value; |
| 133 | 138 |
/// The type of the arc costs |
| 134 | 139 |
typedef typename TR::Cost Cost; |
| 135 | 140 |
|
| 136 | 141 |
/// \brief The large cost type |
| 137 | 142 |
/// |
| 138 | 143 |
/// The large cost type used for internal computations. |
| 139 |
/// Using the \ref CostScalingDefaultTraits "default traits class", |
|
| 140 |
/// it is \c long \c long if the \c Cost type is integer, |
|
| 144 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
|
| 141 | 145 |
/// otherwise it is \c double. |
| 142 | 146 |
typedef typename TR::LargeCost LargeCost; |
| 143 | 147 |
|
| 144 | 148 |
/// The \ref CostScalingDefaultTraits "traits class" of the algorithm |
| 145 | 149 |
typedef TR Traits; |
| 146 | 150 |
|
| 147 | 151 |
public: |
| 148 | 152 |
|
| 149 | 153 |
/// \brief Problem type constants for the \c run() function. |
| 150 | 154 |
/// |
| 151 | 155 |
/// Enum type containing the problem type constants that can be |
| 152 | 156 |
/// returned by the \ref run() function of the algorithm. |
| 153 | 157 |
enum ProblemType {
|
| 154 | 158 |
/// The problem has no feasible solution (flow). |
| 155 | 159 |
INFEASIBLE, |
| 156 | 160 |
/// The problem has optimal solution (i.e. it is feasible and |
| 157 | 161 |
/// bounded), and the algorithm has found optimal flow and node |
| 158 | 162 |
/// potentials (primal and dual solutions). |
| 159 | 163 |
OPTIMAL, |
| 160 | 164 |
/// The digraph contains an arc of negative cost and infinite |
| 161 | 165 |
/// upper bound. It means that the objective function is unbounded |
| 162 | 166 |
/// on that arc, however, note that it could actually be bounded |
| 163 | 167 |
/// over the feasible flows, but this algroithm cannot handle |
| 164 | 168 |
/// these cases. |
| 165 | 169 |
UNBOUNDED |
| 166 | 170 |
}; |
| 167 | 171 |
|
| 168 | 172 |
/// \brief Constants for selecting the internal method. |
| 169 | 173 |
/// |
| 170 | 174 |
/// Enum type containing constants for selecting the internal method |
| 171 | 175 |
/// for the \ref run() function. |
| 172 | 176 |
/// |
| 173 | 177 |
/// \ref CostScaling provides three internal methods that differ mainly |
| 174 | 178 |
/// in their base operations, which are used in conjunction with the |
| 175 | 179 |
/// relabel operation. |
| 176 | 180 |
/// By default, the so called \ref PARTIAL_AUGMENT |
| 177 | 181 |
/// "Partial Augment-Relabel" method is used, which proved to be |
| 178 | 182 |
/// the most efficient and the most robust on various test inputs. |
| 179 | 183 |
/// However, the other methods can be selected using the \ref run() |
| 180 | 184 |
/// function with the proper parameter. |
| 181 | 185 |
enum Method {
|
| 182 | 186 |
/// Local push operations are used, i.e. flow is moved only on one |
| 183 | 187 |
/// admissible arc at once. |
| 184 | 188 |
PUSH, |
| 185 | 189 |
/// Augment operations are used, i.e. flow is moved on admissible |
| 186 | 190 |
/// paths from a node with excess to a node with deficit. |
| 187 | 191 |
AUGMENT, |
| 188 | 192 |
/// Partial augment operations are used, i.e. flow is moved on |
| 189 | 193 |
/// admissible paths started from a node with excess, but the |
| 190 | 194 |
/// lengths of these paths are limited. This method can be viewed |
| 191 | 195 |
/// as a combined version of the previous two operations. |
| 192 | 196 |
PARTIAL_AUGMENT |
| 193 | 197 |
}; |
| 194 | 198 |
|
| 195 | 199 |
private: |
| 196 | 200 |
|
| 197 | 201 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 198 | 202 |
|
| 199 | 203 |
typedef std::vector<int> IntVector; |
| 200 | 204 |
typedef std::vector<char> BoolVector; |
| 201 | 205 |
typedef std::vector<Value> ValueVector; |
| 202 | 206 |
typedef std::vector<Cost> CostVector; |
| 203 | 207 |
typedef std::vector<LargeCost> LargeCostVector; |
| 204 | 208 |
|
| 205 | 209 |
private: |
| 206 | 210 |
|
| 207 | 211 |
template <typename KT, typename VT> |
| 208 | 212 |
class StaticVectorMap {
|
| 209 | 213 |
public: |
| 210 | 214 |
typedef KT Key; |
| 211 | 215 |
typedef VT Value; |
| 212 | 216 |
|
| 213 | 217 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {}
|
| 214 | 218 |
|
| 215 | 219 |
const Value& operator[](const Key& key) const {
|
| 216 | 220 |
return _v[StaticDigraph::id(key)]; |
| 217 | 221 |
} |
| 218 | 222 |
|
| 219 | 223 |
Value& operator[](const Key& key) {
|
| 220 | 224 |
return _v[StaticDigraph::id(key)]; |
| 221 | 225 |
} |
| 222 | 226 |
|
| 223 | 227 |
void set(const Key& key, const Value& val) {
|
| 224 | 228 |
_v[StaticDigraph::id(key)] = val; |
| 225 | 229 |
} |
| 226 | 230 |
|
| 227 | 231 |
private: |
| 228 | 232 |
std::vector<Value>& _v; |
| 229 | 233 |
}; |
| 230 | 234 |
|
| 231 | 235 |
typedef StaticVectorMap<StaticDigraph::Node, LargeCost> LargeCostNodeMap; |
| 232 | 236 |
typedef StaticVectorMap<StaticDigraph::Arc, LargeCost> LargeCostArcMap; |
| 233 | 237 |
|
| 234 | 238 |
private: |
| 235 | 239 |
|
| 236 | 240 |
// Data related to the underlying digraph |
| ... | ... |
@@ -28,192 +28,197 @@ |
| 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 conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 52 | 52 |
///Instantiates a \c PredMap. |
| 53 | 53 |
|
| 54 | 54 |
///This function instantiates a \ref PredMap. |
| 55 | 55 |
///\param g is the digraph, to which we would like to define the |
| 56 | 56 |
///\ref 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 conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 66 | 66 |
///By default, it is a NullMap. |
| 67 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 68 | 68 |
///Instantiates a \c ProcessedMap. |
| 69 | 69 |
|
| 70 | 70 |
///This function instantiates a \ref ProcessedMap. |
| 71 | 71 |
///\param g is the digraph, to which |
| 72 | 72 |
///we would like to define the \ref ProcessedMap. |
| 73 | 73 |
#ifdef DOXYGEN |
| 74 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 75 | 75 |
#else |
| 76 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 77 | 77 |
#endif |
| 78 | 78 |
{
|
| 79 | 79 |
return new ProcessedMap(); |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 | 82 |
///The type of the map that indicates which nodes are reached. |
| 83 | 83 |
|
| 84 | 84 |
///The type of the map that indicates which nodes are reached. |
| 85 | 85 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 86 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 87 | 87 |
///Instantiates a \c ReachedMap. |
| 88 | 88 |
|
| 89 | 89 |
///This function instantiates a \ref ReachedMap. |
| 90 | 90 |
///\param g is the digraph, to which |
| 91 | 91 |
///we would like to define the \ref ReachedMap. |
| 92 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 93 | 93 |
{
|
| 94 | 94 |
return new ReachedMap(g); |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 | 97 |
///The type of the map that stores the distances of the nodes. |
| 98 | 98 |
|
| 99 | 99 |
///The type of the map that stores the distances of the nodes. |
| 100 | 100 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 101 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 102 | 102 |
///Instantiates a \c DistMap. |
| 103 | 103 |
|
| 104 | 104 |
///This function instantiates a \ref DistMap. |
| 105 | 105 |
///\param g is the digraph, to which we would like to define the |
| 106 | 106 |
///\ref DistMap. |
| 107 | 107 |
static DistMap *createDistMap(const Digraph &g) |
| 108 | 108 |
{
|
| 109 | 109 |
return new DistMap(g); |
| 110 | 110 |
} |
| 111 | 111 |
}; |
| 112 | 112 |
|
| 113 | 113 |
///%DFS algorithm class. |
| 114 | 114 |
|
| 115 | 115 |
///\ingroup search |
| 116 | 116 |
///This class provides an efficient implementation of the %DFS algorithm. |
| 117 | 117 |
/// |
| 118 | 118 |
///There is also a \ref dfs() "function-type interface" for the DFS |
| 119 | 119 |
///algorithm, which is convenient in the simplier cases and it can be |
| 120 | 120 |
///used easier. |
| 121 | 121 |
/// |
| 122 | 122 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 123 | 123 |
///The default type is \ref ListDigraph. |
| 124 |
///\tparam TR The traits class that defines various types used by the |
|
| 125 |
///algorithm. By default, it is \ref DfsDefaultTraits |
|
| 126 |
///"DfsDefaultTraits<GR>". |
|
| 127 |
///In most cases, this parameter should not be set directly, |
|
| 128 |
///consider to use the named template parameters instead. |
|
| 124 | 129 |
#ifdef DOXYGEN |
| 125 | 130 |
template <typename GR, |
| 126 | 131 |
typename TR> |
| 127 | 132 |
#else |
| 128 | 133 |
template <typename GR=ListDigraph, |
| 129 | 134 |
typename TR=DfsDefaultTraits<GR> > |
| 130 | 135 |
#endif |
| 131 | 136 |
class Dfs {
|
| 132 | 137 |
public: |
| 133 | 138 |
|
| 134 | 139 |
///The type of the digraph the algorithm runs on. |
| 135 | 140 |
typedef typename TR::Digraph Digraph; |
| 136 | 141 |
|
| 137 | 142 |
///\brief The type of the map that stores the predecessor arcs of the |
| 138 | 143 |
///DFS paths. |
| 139 | 144 |
typedef typename TR::PredMap PredMap; |
| 140 | 145 |
///The type of the map that stores the distances of the nodes. |
| 141 | 146 |
typedef typename TR::DistMap DistMap; |
| 142 | 147 |
///The type of the map that indicates which nodes are reached. |
| 143 | 148 |
typedef typename TR::ReachedMap ReachedMap; |
| 144 | 149 |
///The type of the map that indicates which nodes are processed. |
| 145 | 150 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 146 | 151 |
///The type of the paths. |
| 147 | 152 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 148 | 153 |
|
| 149 | 154 |
///The \ref DfsDefaultTraits "traits class" of the algorithm. |
| 150 | 155 |
typedef TR Traits; |
| 151 | 156 |
|
| 152 | 157 |
private: |
| 153 | 158 |
|
| 154 | 159 |
typedef typename Digraph::Node Node; |
| 155 | 160 |
typedef typename Digraph::NodeIt NodeIt; |
| 156 | 161 |
typedef typename Digraph::Arc Arc; |
| 157 | 162 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 158 | 163 |
|
| 159 | 164 |
//Pointer to the underlying digraph. |
| 160 | 165 |
const Digraph *G; |
| 161 | 166 |
//Pointer to the map of predecessor arcs. |
| 162 | 167 |
PredMap *_pred; |
| 163 | 168 |
//Indicates if _pred is locally allocated (true) or not. |
| 164 | 169 |
bool local_pred; |
| 165 | 170 |
//Pointer to the map of distances. |
| 166 | 171 |
DistMap *_dist; |
| 167 | 172 |
//Indicates if _dist is locally allocated (true) or not. |
| 168 | 173 |
bool local_dist; |
| 169 | 174 |
//Pointer to the map of reached status of the nodes. |
| 170 | 175 |
ReachedMap *_reached; |
| 171 | 176 |
//Indicates if _reached is locally allocated (true) or not. |
| 172 | 177 |
bool local_reached; |
| 173 | 178 |
//Pointer to the map of processed status of the nodes. |
| 174 | 179 |
ProcessedMap *_processed; |
| 175 | 180 |
//Indicates if _processed is locally allocated (true) or not. |
| 176 | 181 |
bool local_processed; |
| 177 | 182 |
|
| 178 | 183 |
std::vector<typename Digraph::OutArcIt> _stack; |
| 179 | 184 |
int _stack_head; |
| 180 | 185 |
|
| 181 | 186 |
//Creates the maps if necessary. |
| 182 | 187 |
void create_maps() |
| 183 | 188 |
{
|
| 184 | 189 |
if(!_pred) {
|
| 185 | 190 |
local_pred = true; |
| 186 | 191 |
_pred = Traits::createPredMap(*G); |
| 187 | 192 |
} |
| 188 | 193 |
if(!_dist) {
|
| 189 | 194 |
local_dist = true; |
| 190 | 195 |
_dist = Traits::createDistMap(*G); |
| 191 | 196 |
} |
| 192 | 197 |
if(!_reached) {
|
| 193 | 198 |
local_reached = true; |
| 194 | 199 |
_reached = Traits::createReachedMap(*G); |
| 195 | 200 |
} |
| 196 | 201 |
if(!_processed) {
|
| 197 | 202 |
local_processed = true; |
| 198 | 203 |
_processed = Traits::createProcessedMap(*G); |
| 199 | 204 |
} |
| 200 | 205 |
} |
| 201 | 206 |
|
| 202 | 207 |
protected: |
| 203 | 208 |
|
| 204 | 209 |
Dfs() {}
|
| 205 | 210 |
|
| 206 | 211 |
public: |
| 207 | 212 |
|
| 208 | 213 |
typedef Dfs Create; |
| 209 | 214 |
|
| 210 | 215 |
///\name Named Template Parameters |
| 211 | 216 |
|
| 212 | 217 |
///@{
|
| 213 | 218 |
|
| 214 | 219 |
template <class T> |
| 215 | 220 |
struct SetPredMapTraits : public Traits {
|
| 216 | 221 |
typedef T PredMap; |
| 217 | 222 |
static PredMap *createPredMap(const Digraph &) |
| 218 | 223 |
{
|
| 219 | 224 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| ... | ... |
@@ -794,192 +799,195 @@ |
| 794 | 799 |
return new ProcessedMap(); |
| 795 | 800 |
} |
| 796 | 801 |
|
| 797 | 802 |
///The type of the map that indicates which nodes are reached. |
| 798 | 803 |
|
| 799 | 804 |
///The type of the map that indicates which nodes are reached. |
| 800 | 805 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 801 | 806 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 802 | 807 |
///Instantiates a ReachedMap. |
| 803 | 808 |
|
| 804 | 809 |
///This function instantiates a ReachedMap. |
| 805 | 810 |
///\param g is the digraph, to which |
| 806 | 811 |
///we would like to define the ReachedMap. |
| 807 | 812 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 808 | 813 |
{
|
| 809 | 814 |
return new ReachedMap(g); |
| 810 | 815 |
} |
| 811 | 816 |
|
| 812 | 817 |
///The type of the map that stores the distances of the nodes. |
| 813 | 818 |
|
| 814 | 819 |
///The type of the map that stores the distances of the nodes. |
| 815 | 820 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 816 | 821 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 817 | 822 |
///Instantiates a DistMap. |
| 818 | 823 |
|
| 819 | 824 |
///This function instantiates a DistMap. |
| 820 | 825 |
///\param g is the digraph, to which we would like to define |
| 821 | 826 |
///the DistMap |
| 822 | 827 |
static DistMap *createDistMap(const Digraph &g) |
| 823 | 828 |
{
|
| 824 | 829 |
return new DistMap(g); |
| 825 | 830 |
} |
| 826 | 831 |
|
| 827 | 832 |
///The type of the DFS paths. |
| 828 | 833 |
|
| 829 | 834 |
///The type of the DFS paths. |
| 830 | 835 |
///It must conform to the \ref concepts::Path "Path" concept. |
| 831 | 836 |
typedef lemon::Path<Digraph> Path; |
| 832 | 837 |
}; |
| 833 | 838 |
|
| 834 | 839 |
/// Default traits class used by DfsWizard |
| 835 | 840 |
|
| 836 | 841 |
/// Default traits class used by DfsWizard. |
| 837 | 842 |
/// \tparam GR The type of the digraph. |
| 838 | 843 |
template<class GR> |
| 839 | 844 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
| 840 | 845 |
{
|
| 841 | 846 |
|
| 842 | 847 |
typedef DfsWizardDefaultTraits<GR> Base; |
| 843 | 848 |
protected: |
| 844 | 849 |
//The type of the nodes in the digraph. |
| 845 | 850 |
typedef typename Base::Digraph::Node Node; |
| 846 | 851 |
|
| 847 | 852 |
//Pointer to the digraph the algorithm runs on. |
| 848 | 853 |
void *_g; |
| 849 | 854 |
//Pointer to the map of reached nodes. |
| 850 | 855 |
void *_reached; |
| 851 | 856 |
//Pointer to the map of processed nodes. |
| 852 | 857 |
void *_processed; |
| 853 | 858 |
//Pointer to the map of predecessors arcs. |
| 854 | 859 |
void *_pred; |
| 855 | 860 |
//Pointer to the map of distances. |
| 856 | 861 |
void *_dist; |
| 857 | 862 |
//Pointer to the DFS path to the target node. |
| 858 | 863 |
void *_path; |
| 859 | 864 |
//Pointer to the distance of the target node. |
| 860 | 865 |
int *_di; |
| 861 | 866 |
|
| 862 | 867 |
public: |
| 863 | 868 |
/// Constructor. |
| 864 | 869 |
|
| 865 | 870 |
/// This constructor does not require parameters, it initiates |
| 866 | 871 |
/// all of the attributes to \c 0. |
| 867 | 872 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
| 868 | 873 |
_dist(0), _path(0), _di(0) {}
|
| 869 | 874 |
|
| 870 | 875 |
/// Constructor. |
| 871 | 876 |
|
| 872 | 877 |
/// This constructor requires one parameter, |
| 873 | 878 |
/// others are initiated to \c 0. |
| 874 | 879 |
/// \param g The digraph the algorithm runs on. |
| 875 | 880 |
DfsWizardBase(const GR &g) : |
| 876 | 881 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
| 877 | 882 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
|
| 878 | 883 |
|
| 879 | 884 |
}; |
| 880 | 885 |
|
| 881 | 886 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
| 882 | 887 |
|
| 883 | 888 |
/// This auxiliary class is created to implement the |
| 884 | 889 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
| 885 | 890 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
| 886 | 891 |
/// functions and features of the plain \ref Dfs. |
| 887 | 892 |
/// |
| 888 | 893 |
/// This class should only be used through the \ref dfs() function, |
| 889 | 894 |
/// which makes it easier to use the algorithm. |
| 895 |
/// |
|
| 896 |
/// \tparam TR The traits class that defines various types used by the |
|
| 897 |
/// algorithm. |
|
| 890 | 898 |
template<class TR> |
| 891 | 899 |
class DfsWizard : public TR |
| 892 | 900 |
{
|
| 893 | 901 |
typedef TR Base; |
| 894 | 902 |
|
| 895 | 903 |
typedef typename TR::Digraph Digraph; |
| 896 | 904 |
|
| 897 | 905 |
typedef typename Digraph::Node Node; |
| 898 | 906 |
typedef typename Digraph::NodeIt NodeIt; |
| 899 | 907 |
typedef typename Digraph::Arc Arc; |
| 900 | 908 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 901 | 909 |
|
| 902 | 910 |
typedef typename TR::PredMap PredMap; |
| 903 | 911 |
typedef typename TR::DistMap DistMap; |
| 904 | 912 |
typedef typename TR::ReachedMap ReachedMap; |
| 905 | 913 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 906 | 914 |
typedef typename TR::Path Path; |
| 907 | 915 |
|
| 908 | 916 |
public: |
| 909 | 917 |
|
| 910 | 918 |
/// Constructor. |
| 911 | 919 |
DfsWizard() : TR() {}
|
| 912 | 920 |
|
| 913 | 921 |
/// Constructor that requires parameters. |
| 914 | 922 |
|
| 915 | 923 |
/// Constructor that requires parameters. |
| 916 | 924 |
/// These parameters will be the default values for the traits class. |
| 917 | 925 |
/// \param g The digraph the algorithm runs on. |
| 918 | 926 |
DfsWizard(const Digraph &g) : |
| 919 | 927 |
TR(g) {}
|
| 920 | 928 |
|
| 921 | 929 |
///Copy constructor |
| 922 | 930 |
DfsWizard(const TR &b) : TR(b) {}
|
| 923 | 931 |
|
| 924 | 932 |
~DfsWizard() {}
|
| 925 | 933 |
|
| 926 | 934 |
///Runs DFS algorithm from the given source node. |
| 927 | 935 |
|
| 928 | 936 |
///This method runs DFS algorithm from node \c s |
| 929 | 937 |
///in order to compute the DFS path to each node. |
| 930 | 938 |
void run(Node s) |
| 931 | 939 |
{
|
| 932 | 940 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 933 | 941 |
if (Base::_pred) |
| 934 | 942 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 935 | 943 |
if (Base::_dist) |
| 936 | 944 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 937 | 945 |
if (Base::_reached) |
| 938 | 946 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 939 | 947 |
if (Base::_processed) |
| 940 | 948 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 941 | 949 |
if (s!=INVALID) |
| 942 | 950 |
alg.run(s); |
| 943 | 951 |
else |
| 944 | 952 |
alg.run(); |
| 945 | 953 |
} |
| 946 | 954 |
|
| 947 | 955 |
///Finds the DFS path between \c s and \c t. |
| 948 | 956 |
|
| 949 | 957 |
///This method runs DFS algorithm from node \c s |
| 950 | 958 |
///in order to compute the DFS path to node \c t |
| 951 | 959 |
///(it stops searching when \c t is processed). |
| 952 | 960 |
/// |
| 953 | 961 |
///\return \c true if \c t is reachable form \c s. |
| 954 | 962 |
bool run(Node s, Node t) |
| 955 | 963 |
{
|
| 956 | 964 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 957 | 965 |
if (Base::_pred) |
| 958 | 966 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 959 | 967 |
if (Base::_dist) |
| 960 | 968 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 961 | 969 |
if (Base::_reached) |
| 962 | 970 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 963 | 971 |
if (Base::_processed) |
| 964 | 972 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 965 | 973 |
alg.run(s,t); |
| 966 | 974 |
if (Base::_path) |
| 967 | 975 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
| 968 | 976 |
if (Base::_di) |
| 969 | 977 |
*Base::_di = alg.dist(t); |
| 970 | 978 |
return alg.reached(t); |
| 971 | 979 |
} |
| 972 | 980 |
|
| 973 | 981 |
///Runs DFS algorithm to visit all nodes in the digraph. |
| 974 | 982 |
|
| 975 | 983 |
///This method runs DFS algorithm in order to visit all nodes |
| 976 | 984 |
///in the digraph. |
| 977 | 985 |
void run() |
| 978 | 986 |
{
|
| 979 | 987 |
run(INVALID); |
| 980 | 988 |
} |
| 981 | 989 |
|
| 982 | 990 |
template<class T> |
| 983 | 991 |
struct SetPredMapBase : public Base {
|
| 984 | 992 |
typedef T PredMap; |
| 985 | 993 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
| ... | ... |
@@ -1144,197 +1152,197 @@ |
| 1144 | 1152 |
/// |
| 1145 | 1153 |
/// This function is called when an arc is examined but its target node is |
| 1146 | 1154 |
/// already discovered. |
| 1147 | 1155 |
void examine(const Arc& arc) {}
|
| 1148 | 1156 |
/// \brief Called when the DFS steps back from a node. |
| 1149 | 1157 |
/// |
| 1150 | 1158 |
/// This function is called when the DFS steps back from a node. |
| 1151 | 1159 |
void leave(const Node& node) {}
|
| 1152 | 1160 |
/// \brief Called when the DFS steps back on an arc. |
| 1153 | 1161 |
/// |
| 1154 | 1162 |
/// This function is called when the DFS steps back on an arc. |
| 1155 | 1163 |
void backtrack(const Arc& arc) {}
|
| 1156 | 1164 |
}; |
| 1157 | 1165 |
#else |
| 1158 | 1166 |
template <typename GR> |
| 1159 | 1167 |
struct DfsVisitor {
|
| 1160 | 1168 |
typedef GR Digraph; |
| 1161 | 1169 |
typedef typename Digraph::Arc Arc; |
| 1162 | 1170 |
typedef typename Digraph::Node Node; |
| 1163 | 1171 |
void start(const Node&) {}
|
| 1164 | 1172 |
void stop(const Node&) {}
|
| 1165 | 1173 |
void reach(const Node&) {}
|
| 1166 | 1174 |
void discover(const Arc&) {}
|
| 1167 | 1175 |
void examine(const Arc&) {}
|
| 1168 | 1176 |
void leave(const Node&) {}
|
| 1169 | 1177 |
void backtrack(const Arc&) {}
|
| 1170 | 1178 |
|
| 1171 | 1179 |
template <typename _Visitor> |
| 1172 | 1180 |
struct Constraints {
|
| 1173 | 1181 |
void constraints() {
|
| 1174 | 1182 |
Arc arc; |
| 1175 | 1183 |
Node node; |
| 1176 | 1184 |
visitor.start(node); |
| 1177 | 1185 |
visitor.stop(arc); |
| 1178 | 1186 |
visitor.reach(node); |
| 1179 | 1187 |
visitor.discover(arc); |
| 1180 | 1188 |
visitor.examine(arc); |
| 1181 | 1189 |
visitor.leave(node); |
| 1182 | 1190 |
visitor.backtrack(arc); |
| 1183 | 1191 |
} |
| 1184 | 1192 |
_Visitor& visitor; |
| 1185 | 1193 |
}; |
| 1186 | 1194 |
}; |
| 1187 | 1195 |
#endif |
| 1188 | 1196 |
|
| 1189 | 1197 |
/// \brief Default traits class of DfsVisit class. |
| 1190 | 1198 |
/// |
| 1191 | 1199 |
/// Default traits class of DfsVisit class. |
| 1192 | 1200 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 1193 | 1201 |
template<class GR> |
| 1194 | 1202 |
struct DfsVisitDefaultTraits {
|
| 1195 | 1203 |
|
| 1196 | 1204 |
/// \brief The type of the digraph the algorithm runs on. |
| 1197 | 1205 |
typedef GR Digraph; |
| 1198 | 1206 |
|
| 1199 | 1207 |
/// \brief The type of the map that indicates which nodes are reached. |
| 1200 | 1208 |
/// |
| 1201 | 1209 |
/// The type of the map that indicates which nodes are reached. |
| 1202 | 1210 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 1203 | 1211 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 1204 | 1212 |
|
| 1205 | 1213 |
/// \brief Instantiates a ReachedMap. |
| 1206 | 1214 |
/// |
| 1207 | 1215 |
/// This function instantiates a ReachedMap. |
| 1208 | 1216 |
/// \param digraph is the digraph, to which |
| 1209 | 1217 |
/// we would like to define the ReachedMap. |
| 1210 | 1218 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1211 | 1219 |
return new ReachedMap(digraph); |
| 1212 | 1220 |
} |
| 1213 | 1221 |
|
| 1214 | 1222 |
}; |
| 1215 | 1223 |
|
| 1216 | 1224 |
/// \ingroup search |
| 1217 | 1225 |
/// |
| 1218 | 1226 |
/// \brief DFS algorithm class with visitor interface. |
| 1219 | 1227 |
/// |
| 1220 | 1228 |
/// This class provides an efficient implementation of the DFS algorithm |
| 1221 | 1229 |
/// with visitor interface. |
| 1222 | 1230 |
/// |
| 1223 | 1231 |
/// The DfsVisit class provides an alternative interface to the Dfs |
| 1224 | 1232 |
/// class. It works with callback mechanism, the DfsVisit object calls |
| 1225 | 1233 |
/// the member functions of the \c Visitor class on every DFS event. |
| 1226 | 1234 |
/// |
| 1227 | 1235 |
/// This interface of the DFS algorithm should be used in special cases |
| 1228 | 1236 |
/// when extra actions have to be performed in connection with certain |
| 1229 | 1237 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
| 1230 | 1238 |
/// instead. |
| 1231 | 1239 |
/// |
| 1232 | 1240 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 1233 | 1241 |
/// The default type is \ref ListDigraph. |
| 1234 | 1242 |
/// The value of GR is not used directly by \ref DfsVisit, |
| 1235 | 1243 |
/// it is only passed to \ref DfsVisitDefaultTraits. |
| 1236 | 1244 |
/// \tparam VS The Visitor type that is used by the algorithm. |
| 1237 | 1245 |
/// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which |
| 1238 | 1246 |
/// does not observe the DFS events. If you want to observe the DFS |
| 1239 | 1247 |
/// events, you should implement your own visitor class. |
| 1240 |
/// \tparam TR Traits class to set various data types used by the |
|
| 1241 |
/// algorithm. The default traits class is |
|
| 1242 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<GR>". |
|
| 1243 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
|
| 1244 |
/// |
|
| 1248 |
/// \tparam TR The traits class that defines various types used by the |
|
| 1249 |
/// algorithm. By default, it is \ref DfsVisitDefaultTraits |
|
| 1250 |
/// "DfsVisitDefaultTraits<GR>". |
|
| 1251 |
/// In most cases, this parameter should not be set directly, |
|
| 1252 |
/// consider to use the named template parameters instead. |
|
| 1245 | 1253 |
#ifdef DOXYGEN |
| 1246 | 1254 |
template <typename GR, typename VS, typename TR> |
| 1247 | 1255 |
#else |
| 1248 | 1256 |
template <typename GR = ListDigraph, |
| 1249 | 1257 |
typename VS = DfsVisitor<GR>, |
| 1250 | 1258 |
typename TR = DfsVisitDefaultTraits<GR> > |
| 1251 | 1259 |
#endif |
| 1252 | 1260 |
class DfsVisit {
|
| 1253 | 1261 |
public: |
| 1254 | 1262 |
|
| 1255 | 1263 |
///The traits class. |
| 1256 | 1264 |
typedef TR Traits; |
| 1257 | 1265 |
|
| 1258 | 1266 |
///The type of the digraph the algorithm runs on. |
| 1259 | 1267 |
typedef typename Traits::Digraph Digraph; |
| 1260 | 1268 |
|
| 1261 | 1269 |
///The visitor type used by the algorithm. |
| 1262 | 1270 |
typedef VS Visitor; |
| 1263 | 1271 |
|
| 1264 | 1272 |
///The type of the map that indicates which nodes are reached. |
| 1265 | 1273 |
typedef typename Traits::ReachedMap ReachedMap; |
| 1266 | 1274 |
|
| 1267 | 1275 |
private: |
| 1268 | 1276 |
|
| 1269 | 1277 |
typedef typename Digraph::Node Node; |
| 1270 | 1278 |
typedef typename Digraph::NodeIt NodeIt; |
| 1271 | 1279 |
typedef typename Digraph::Arc Arc; |
| 1272 | 1280 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1273 | 1281 |
|
| 1274 | 1282 |
//Pointer to the underlying digraph. |
| 1275 | 1283 |
const Digraph *_digraph; |
| 1276 | 1284 |
//Pointer to the visitor object. |
| 1277 | 1285 |
Visitor *_visitor; |
| 1278 | 1286 |
//Pointer to the map of reached status of the nodes. |
| 1279 | 1287 |
ReachedMap *_reached; |
| 1280 | 1288 |
//Indicates if _reached is locally allocated (true) or not. |
| 1281 | 1289 |
bool local_reached; |
| 1282 | 1290 |
|
| 1283 | 1291 |
std::vector<typename Digraph::Arc> _stack; |
| 1284 | 1292 |
int _stack_head; |
| 1285 | 1293 |
|
| 1286 | 1294 |
//Creates the maps if necessary. |
| 1287 | 1295 |
void create_maps() {
|
| 1288 | 1296 |
if(!_reached) {
|
| 1289 | 1297 |
local_reached = true; |
| 1290 | 1298 |
_reached = Traits::createReachedMap(*_digraph); |
| 1291 | 1299 |
} |
| 1292 | 1300 |
} |
| 1293 | 1301 |
|
| 1294 | 1302 |
protected: |
| 1295 | 1303 |
|
| 1296 | 1304 |
DfsVisit() {}
|
| 1297 | 1305 |
|
| 1298 | 1306 |
public: |
| 1299 | 1307 |
|
| 1300 | 1308 |
typedef DfsVisit Create; |
| 1301 | 1309 |
|
| 1302 | 1310 |
/// \name Named Template Parameters |
| 1303 | 1311 |
|
| 1304 | 1312 |
///@{
|
| 1305 | 1313 |
template <class T> |
| 1306 | 1314 |
struct SetReachedMapTraits : public Traits {
|
| 1307 | 1315 |
typedef T ReachedMap; |
| 1308 | 1316 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1309 | 1317 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 1310 | 1318 |
return 0; // ignore warnings |
| 1311 | 1319 |
} |
| 1312 | 1320 |
}; |
| 1313 | 1321 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1314 | 1322 |
/// ReachedMap type. |
| 1315 | 1323 |
/// |
| 1316 | 1324 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
| 1317 | 1325 |
template <class T> |
| 1318 | 1326 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
| 1319 | 1327 |
SetReachedMapTraits<T> > {
|
| 1320 | 1328 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
| 1321 | 1329 |
}; |
| 1322 | 1330 |
///@} |
| 1323 | 1331 |
|
| 1324 | 1332 |
public: |
| 1325 | 1333 |
|
| 1326 | 1334 |
/// \brief Constructor. |
| 1327 | 1335 |
/// |
| 1328 | 1336 |
/// Constructor. |
| 1329 | 1337 |
/// |
| 1330 | 1338 |
/// \param digraph The digraph the algorithm runs on. |
| 1331 | 1339 |
/// \param visitor The visitor object of the algorithm. |
| 1332 | 1340 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
| 1333 | 1341 |
: _digraph(&digraph), _visitor(&visitor), |
| 1334 | 1342 |
_reached(0), local_reached(false) {}
|
| 1335 | 1343 |
|
| 1336 | 1344 |
/// \brief Destructor. |
| 1337 | 1345 |
~DfsVisit() {
|
| 1338 | 1346 |
if(local_reached) delete _reached; |
| 1339 | 1347 |
} |
| 1340 | 1348 |
| ... | ... |
@@ -99,192 +99,197 @@ |
| 99 | 99 |
///The heap type used by the %Dijkstra algorithm. |
| 100 | 100 |
|
| 101 | 101 |
///The heap type used by the Dijkstra algorithm. |
| 102 | 102 |
/// |
| 103 | 103 |
///\sa BinHeap |
| 104 | 104 |
///\sa Dijkstra |
| 105 | 105 |
typedef BinHeap<typename LEN::Value, HeapCrossRef, std::less<Value> > Heap; |
| 106 | 106 |
///Instantiates a \c Heap. |
| 107 | 107 |
|
| 108 | 108 |
///This function instantiates a \ref Heap. |
| 109 | 109 |
static Heap *createHeap(HeapCrossRef& r) |
| 110 | 110 |
{
|
| 111 | 111 |
return new Heap(r); |
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 | 114 |
///\brief The type of the map that stores the predecessor |
| 115 | 115 |
///arcs of the shortest paths. |
| 116 | 116 |
/// |
| 117 | 117 |
///The type of the map that stores the predecessor |
| 118 | 118 |
///arcs of the shortest paths. |
| 119 | 119 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 120 | 120 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 121 | 121 |
///Instantiates a \c PredMap. |
| 122 | 122 |
|
| 123 | 123 |
///This function instantiates a \ref PredMap. |
| 124 | 124 |
///\param g is the digraph, to which we would like to define the |
| 125 | 125 |
///\ref PredMap. |
| 126 | 126 |
static PredMap *createPredMap(const Digraph &g) |
| 127 | 127 |
{
|
| 128 | 128 |
return new PredMap(g); |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
///The type of the map that indicates which nodes are processed. |
| 132 | 132 |
|
| 133 | 133 |
///The type of the map that indicates which nodes are processed. |
| 134 | 134 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 135 | 135 |
///By default, it is a NullMap. |
| 136 | 136 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 137 | 137 |
///Instantiates a \c ProcessedMap. |
| 138 | 138 |
|
| 139 | 139 |
///This function instantiates a \ref ProcessedMap. |
| 140 | 140 |
///\param g is the digraph, to which |
| 141 | 141 |
///we would like to define the \ref ProcessedMap. |
| 142 | 142 |
#ifdef DOXYGEN |
| 143 | 143 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 144 | 144 |
#else |
| 145 | 145 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 146 | 146 |
#endif |
| 147 | 147 |
{
|
| 148 | 148 |
return new ProcessedMap(); |
| 149 | 149 |
} |
| 150 | 150 |
|
| 151 | 151 |
///The type of the map that stores the distances of the nodes. |
| 152 | 152 |
|
| 153 | 153 |
///The type of the map that stores the distances of the nodes. |
| 154 | 154 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 155 | 155 |
typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap; |
| 156 | 156 |
///Instantiates a \c DistMap. |
| 157 | 157 |
|
| 158 | 158 |
///This function instantiates a \ref DistMap. |
| 159 | 159 |
///\param g is the digraph, to which we would like to define |
| 160 | 160 |
///the \ref DistMap. |
| 161 | 161 |
static DistMap *createDistMap(const Digraph &g) |
| 162 | 162 |
{
|
| 163 | 163 |
return new DistMap(g); |
| 164 | 164 |
} |
| 165 | 165 |
}; |
| 166 | 166 |
|
| 167 | 167 |
///%Dijkstra algorithm class. |
| 168 | 168 |
|
| 169 | 169 |
/// \ingroup shortest_path |
| 170 | 170 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
| 171 | 171 |
/// |
| 172 | 172 |
///The %Dijkstra algorithm solves the single-source shortest path problem |
| 173 | 173 |
///when all arc lengths are non-negative. If there are negative lengths, |
| 174 | 174 |
///the BellmanFord algorithm should be used instead. |
| 175 | 175 |
/// |
| 176 | 176 |
///The arc lengths are passed to the algorithm using a |
| 177 | 177 |
///\ref concepts::ReadMap "ReadMap", |
| 178 | 178 |
///so it is easy to change it to any kind of length. |
| 179 | 179 |
///The type of the length is determined by the |
| 180 | 180 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
| 181 | 181 |
///It is also possible to change the underlying priority heap. |
| 182 | 182 |
/// |
| 183 | 183 |
///There is also a \ref dijkstra() "function-type interface" for the |
| 184 | 184 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
| 185 | 185 |
///it can be used easier. |
| 186 | 186 |
/// |
| 187 | 187 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 188 | 188 |
///The default type is \ref ListDigraph. |
| 189 | 189 |
///\tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
| 190 | 190 |
///the lengths of the arcs. |
| 191 | 191 |
///It is read once for each arc, so the map may involve in |
| 192 | 192 |
///relatively time consuming process to compute the arc lengths if |
| 193 | 193 |
///it is necessary. The default map type is \ref |
| 194 | 194 |
///concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 195 |
///\tparam TR The traits class that defines various types used by the |
|
| 196 |
///algorithm. By default, it is \ref DijkstraDefaultTraits |
|
| 197 |
///"DijkstraDefaultTraits<GR, LEN>". |
|
| 198 |
///In most cases, this parameter should not be set directly, |
|
| 199 |
///consider to use the named template parameters instead. |
|
| 195 | 200 |
#ifdef DOXYGEN |
| 196 | 201 |
template <typename GR, typename LEN, typename TR> |
| 197 | 202 |
#else |
| 198 | 203 |
template <typename GR=ListDigraph, |
| 199 | 204 |
typename LEN=typename GR::template ArcMap<int>, |
| 200 | 205 |
typename TR=DijkstraDefaultTraits<GR,LEN> > |
| 201 | 206 |
#endif |
| 202 | 207 |
class Dijkstra {
|
| 203 | 208 |
public: |
| 204 | 209 |
|
| 205 | 210 |
///The type of the digraph the algorithm runs on. |
| 206 | 211 |
typedef typename TR::Digraph Digraph; |
| 207 | 212 |
|
| 208 | 213 |
///The type of the arc lengths. |
| 209 | 214 |
typedef typename TR::Value Value; |
| 210 | 215 |
///The type of the map that stores the arc lengths. |
| 211 | 216 |
typedef typename TR::LengthMap LengthMap; |
| 212 | 217 |
///\brief The type of the map that stores the predecessor arcs of the |
| 213 | 218 |
///shortest paths. |
| 214 | 219 |
typedef typename TR::PredMap PredMap; |
| 215 | 220 |
///The type of the map that stores the distances of the nodes. |
| 216 | 221 |
typedef typename TR::DistMap DistMap; |
| 217 | 222 |
///The type of the map that indicates which nodes are processed. |
| 218 | 223 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 219 | 224 |
///The type of the paths. |
| 220 | 225 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 221 | 226 |
///The cross reference type used for the current heap. |
| 222 | 227 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
| 223 | 228 |
///The heap type used by the algorithm. |
| 224 | 229 |
typedef typename TR::Heap Heap; |
| 225 | 230 |
///\brief The \ref DijkstraDefaultOperationTraits "operation traits class" |
| 226 | 231 |
///of the algorithm. |
| 227 | 232 |
typedef typename TR::OperationTraits OperationTraits; |
| 228 | 233 |
|
| 229 | 234 |
///The \ref DijkstraDefaultTraits "traits class" of the algorithm. |
| 230 | 235 |
typedef TR Traits; |
| 231 | 236 |
|
| 232 | 237 |
private: |
| 233 | 238 |
|
| 234 | 239 |
typedef typename Digraph::Node Node; |
| 235 | 240 |
typedef typename Digraph::NodeIt NodeIt; |
| 236 | 241 |
typedef typename Digraph::Arc Arc; |
| 237 | 242 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 238 | 243 |
|
| 239 | 244 |
//Pointer to the underlying digraph. |
| 240 | 245 |
const Digraph *G; |
| 241 | 246 |
//Pointer to the length map. |
| 242 | 247 |
const LengthMap *_length; |
| 243 | 248 |
//Pointer to the map of predecessors arcs. |
| 244 | 249 |
PredMap *_pred; |
| 245 | 250 |
//Indicates if _pred is locally allocated (true) or not. |
| 246 | 251 |
bool local_pred; |
| 247 | 252 |
//Pointer to the map of distances. |
| 248 | 253 |
DistMap *_dist; |
| 249 | 254 |
//Indicates if _dist is locally allocated (true) or not. |
| 250 | 255 |
bool local_dist; |
| 251 | 256 |
//Pointer to the map of processed status of the nodes. |
| 252 | 257 |
ProcessedMap *_processed; |
| 253 | 258 |
//Indicates if _processed is locally allocated (true) or not. |
| 254 | 259 |
bool local_processed; |
| 255 | 260 |
//Pointer to the heap cross references. |
| 256 | 261 |
HeapCrossRef *_heap_cross_ref; |
| 257 | 262 |
//Indicates if _heap_cross_ref is locally allocated (true) or not. |
| 258 | 263 |
bool local_heap_cross_ref; |
| 259 | 264 |
//Pointer to the heap. |
| 260 | 265 |
Heap *_heap; |
| 261 | 266 |
//Indicates if _heap is locally allocated (true) or not. |
| 262 | 267 |
bool local_heap; |
| 263 | 268 |
|
| 264 | 269 |
//Creates the maps if necessary. |
| 265 | 270 |
void create_maps() |
| 266 | 271 |
{
|
| 267 | 272 |
if(!_pred) {
|
| 268 | 273 |
local_pred = true; |
| 269 | 274 |
_pred = Traits::createPredMap(*G); |
| 270 | 275 |
} |
| 271 | 276 |
if(!_dist) {
|
| 272 | 277 |
local_dist = true; |
| 273 | 278 |
_dist = Traits::createDistMap(*G); |
| 274 | 279 |
} |
| 275 | 280 |
if(!_processed) {
|
| 276 | 281 |
local_processed = true; |
| 277 | 282 |
_processed = Traits::createProcessedMap(*G); |
| 278 | 283 |
} |
| 279 | 284 |
if (!_heap_cross_ref) {
|
| 280 | 285 |
local_heap_cross_ref = true; |
| 281 | 286 |
_heap_cross_ref = Traits::createHeapCrossRef(*G); |
| 282 | 287 |
} |
| 283 | 288 |
if (!_heap) {
|
| 284 | 289 |
local_heap = true; |
| 285 | 290 |
_heap = Traits::createHeap(*_heap_cross_ref); |
| 286 | 291 |
} |
| 287 | 292 |
} |
| 288 | 293 |
|
| 289 | 294 |
public: |
| 290 | 295 |
|
| ... | ... |
@@ -999,192 +1004,195 @@ |
| 999 | 1004 |
///By default, it is a NullMap. |
| 1000 | 1005 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 1001 | 1006 |
///Instantiates a ProcessedMap. |
| 1002 | 1007 |
|
| 1003 | 1008 |
///This function instantiates a ProcessedMap. |
| 1004 | 1009 |
///\param g is the digraph, to which |
| 1005 | 1010 |
///we would like to define the ProcessedMap. |
| 1006 | 1011 |
#ifdef DOXYGEN |
| 1007 | 1012 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 1008 | 1013 |
#else |
| 1009 | 1014 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 1010 | 1015 |
#endif |
| 1011 | 1016 |
{
|
| 1012 | 1017 |
return new ProcessedMap(); |
| 1013 | 1018 |
} |
| 1014 | 1019 |
|
| 1015 | 1020 |
///The type of the map that stores the distances of the nodes. |
| 1016 | 1021 |
|
| 1017 | 1022 |
///The type of the map that stores the distances of the nodes. |
| 1018 | 1023 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 1019 | 1024 |
typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap; |
| 1020 | 1025 |
///Instantiates a DistMap. |
| 1021 | 1026 |
|
| 1022 | 1027 |
///This function instantiates a DistMap. |
| 1023 | 1028 |
///\param g is the digraph, to which we would like to define |
| 1024 | 1029 |
///the DistMap |
| 1025 | 1030 |
static DistMap *createDistMap(const Digraph &g) |
| 1026 | 1031 |
{
|
| 1027 | 1032 |
return new DistMap(g); |
| 1028 | 1033 |
} |
| 1029 | 1034 |
|
| 1030 | 1035 |
///The type of the shortest paths. |
| 1031 | 1036 |
|
| 1032 | 1037 |
///The type of the shortest paths. |
| 1033 | 1038 |
///It must conform to the \ref concepts::Path "Path" concept. |
| 1034 | 1039 |
typedef lemon::Path<Digraph> Path; |
| 1035 | 1040 |
}; |
| 1036 | 1041 |
|
| 1037 | 1042 |
/// Default traits class used by DijkstraWizard |
| 1038 | 1043 |
|
| 1039 | 1044 |
/// Default traits class used by DijkstraWizard. |
| 1040 | 1045 |
/// \tparam GR The type of the digraph. |
| 1041 | 1046 |
/// \tparam LEN The type of the length map. |
| 1042 | 1047 |
template<typename GR, typename LEN> |
| 1043 | 1048 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LEN> |
| 1044 | 1049 |
{
|
| 1045 | 1050 |
typedef DijkstraWizardDefaultTraits<GR,LEN> Base; |
| 1046 | 1051 |
protected: |
| 1047 | 1052 |
//The type of the nodes in the digraph. |
| 1048 | 1053 |
typedef typename Base::Digraph::Node Node; |
| 1049 | 1054 |
|
| 1050 | 1055 |
//Pointer to the digraph the algorithm runs on. |
| 1051 | 1056 |
void *_g; |
| 1052 | 1057 |
//Pointer to the length map. |
| 1053 | 1058 |
void *_length; |
| 1054 | 1059 |
//Pointer to the map of processed nodes. |
| 1055 | 1060 |
void *_processed; |
| 1056 | 1061 |
//Pointer to the map of predecessors arcs. |
| 1057 | 1062 |
void *_pred; |
| 1058 | 1063 |
//Pointer to the map of distances. |
| 1059 | 1064 |
void *_dist; |
| 1060 | 1065 |
//Pointer to the shortest path to the target node. |
| 1061 | 1066 |
void *_path; |
| 1062 | 1067 |
//Pointer to the distance of the target node. |
| 1063 | 1068 |
void *_di; |
| 1064 | 1069 |
|
| 1065 | 1070 |
public: |
| 1066 | 1071 |
/// Constructor. |
| 1067 | 1072 |
|
| 1068 | 1073 |
/// This constructor does not require parameters, therefore it initiates |
| 1069 | 1074 |
/// all of the attributes to \c 0. |
| 1070 | 1075 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
| 1071 | 1076 |
_dist(0), _path(0), _di(0) {}
|
| 1072 | 1077 |
|
| 1073 | 1078 |
/// Constructor. |
| 1074 | 1079 |
|
| 1075 | 1080 |
/// This constructor requires two parameters, |
| 1076 | 1081 |
/// others are initiated to \c 0. |
| 1077 | 1082 |
/// \param g The digraph the algorithm runs on. |
| 1078 | 1083 |
/// \param l The length map. |
| 1079 | 1084 |
DijkstraWizardBase(const GR &g,const LEN &l) : |
| 1080 | 1085 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
| 1081 | 1086 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&l))), |
| 1082 | 1087 |
_processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
|
| 1083 | 1088 |
|
| 1084 | 1089 |
}; |
| 1085 | 1090 |
|
| 1086 | 1091 |
/// Auxiliary class for the function-type interface of Dijkstra algorithm. |
| 1087 | 1092 |
|
| 1088 | 1093 |
/// This auxiliary class is created to implement the |
| 1089 | 1094 |
/// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm. |
| 1090 | 1095 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
| 1091 | 1096 |
/// functions and features of the plain \ref Dijkstra. |
| 1092 | 1097 |
/// |
| 1093 | 1098 |
/// This class should only be used through the \ref dijkstra() function, |
| 1094 | 1099 |
/// which makes it easier to use the algorithm. |
| 1100 |
/// |
|
| 1101 |
/// \tparam TR The traits class that defines various types used by the |
|
| 1102 |
/// algorithm. |
|
| 1095 | 1103 |
template<class TR> |
| 1096 | 1104 |
class DijkstraWizard : public TR |
| 1097 | 1105 |
{
|
| 1098 | 1106 |
typedef TR Base; |
| 1099 | 1107 |
|
| 1100 | 1108 |
typedef typename TR::Digraph Digraph; |
| 1101 | 1109 |
|
| 1102 | 1110 |
typedef typename Digraph::Node Node; |
| 1103 | 1111 |
typedef typename Digraph::NodeIt NodeIt; |
| 1104 | 1112 |
typedef typename Digraph::Arc Arc; |
| 1105 | 1113 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1106 | 1114 |
|
| 1107 | 1115 |
typedef typename TR::LengthMap LengthMap; |
| 1108 | 1116 |
typedef typename LengthMap::Value Value; |
| 1109 | 1117 |
typedef typename TR::PredMap PredMap; |
| 1110 | 1118 |
typedef typename TR::DistMap DistMap; |
| 1111 | 1119 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 1112 | 1120 |
typedef typename TR::Path Path; |
| 1113 | 1121 |
typedef typename TR::Heap Heap; |
| 1114 | 1122 |
|
| 1115 | 1123 |
public: |
| 1116 | 1124 |
|
| 1117 | 1125 |
/// Constructor. |
| 1118 | 1126 |
DijkstraWizard() : TR() {}
|
| 1119 | 1127 |
|
| 1120 | 1128 |
/// Constructor that requires parameters. |
| 1121 | 1129 |
|
| 1122 | 1130 |
/// Constructor that requires parameters. |
| 1123 | 1131 |
/// These parameters will be the default values for the traits class. |
| 1124 | 1132 |
/// \param g The digraph the algorithm runs on. |
| 1125 | 1133 |
/// \param l The length map. |
| 1126 | 1134 |
DijkstraWizard(const Digraph &g, const LengthMap &l) : |
| 1127 | 1135 |
TR(g,l) {}
|
| 1128 | 1136 |
|
| 1129 | 1137 |
///Copy constructor |
| 1130 | 1138 |
DijkstraWizard(const TR &b) : TR(b) {}
|
| 1131 | 1139 |
|
| 1132 | 1140 |
~DijkstraWizard() {}
|
| 1133 | 1141 |
|
| 1134 | 1142 |
///Runs Dijkstra algorithm from the given source node. |
| 1135 | 1143 |
|
| 1136 | 1144 |
///This method runs %Dijkstra algorithm from the given source node |
| 1137 | 1145 |
///in order to compute the shortest path to each node. |
| 1138 | 1146 |
void run(Node s) |
| 1139 | 1147 |
{
|
| 1140 | 1148 |
Dijkstra<Digraph,LengthMap,TR> |
| 1141 | 1149 |
dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
| 1142 | 1150 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
| 1143 | 1151 |
if (Base::_pred) |
| 1144 | 1152 |
dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1145 | 1153 |
if (Base::_dist) |
| 1146 | 1154 |
dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1147 | 1155 |
if (Base::_processed) |
| 1148 | 1156 |
dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 1149 | 1157 |
dijk.run(s); |
| 1150 | 1158 |
} |
| 1151 | 1159 |
|
| 1152 | 1160 |
///Finds the shortest path between \c s and \c t. |
| 1153 | 1161 |
|
| 1154 | 1162 |
///This method runs the %Dijkstra algorithm from node \c s |
| 1155 | 1163 |
///in order to compute the shortest path to node \c t |
| 1156 | 1164 |
///(it stops searching when \c t is processed). |
| 1157 | 1165 |
/// |
| 1158 | 1166 |
///\return \c true if \c t is reachable form \c s. |
| 1159 | 1167 |
bool run(Node s, Node t) |
| 1160 | 1168 |
{
|
| 1161 | 1169 |
Dijkstra<Digraph,LengthMap,TR> |
| 1162 | 1170 |
dijk(*reinterpret_cast<const Digraph*>(Base::_g), |
| 1163 | 1171 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
| 1164 | 1172 |
if (Base::_pred) |
| 1165 | 1173 |
dijk.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1166 | 1174 |
if (Base::_dist) |
| 1167 | 1175 |
dijk.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1168 | 1176 |
if (Base::_processed) |
| 1169 | 1177 |
dijk.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 1170 | 1178 |
dijk.run(s,t); |
| 1171 | 1179 |
if (Base::_path) |
| 1172 | 1180 |
*reinterpret_cast<Path*>(Base::_path) = dijk.path(t); |
| 1173 | 1181 |
if (Base::_di) |
| 1174 | 1182 |
*reinterpret_cast<Value*>(Base::_di) = dijk.dist(t); |
| 1175 | 1183 |
return dijk.reached(t); |
| 1176 | 1184 |
} |
| 1177 | 1185 |
|
| 1178 | 1186 |
template<class T> |
| 1179 | 1187 |
struct SetPredMapBase : public Base {
|
| 1180 | 1188 |
typedef T PredMap; |
| 1181 | 1189 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
| 1182 | 1190 |
SetPredMapBase(const TR &b) : TR(b) {}
|
| 1183 | 1191 |
}; |
| 1184 | 1192 |
|
| 1185 | 1193 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 1186 | 1194 |
///the predecessor map. |
| 1187 | 1195 |
/// |
| 1188 | 1196 |
///\ref named-templ-param "Named parameter" function for setting |
| 1189 | 1197 |
///the map that stores the predecessor arcs of the nodes. |
| 1190 | 1198 |
template<class T> |
| ... | ... |
@@ -13,215 +13,219 @@ |
| 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_HARTMANN_ORLIN_H |
| 20 | 20 |
#define LEMON_HARTMANN_ORLIN_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_mean_cycle |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Hartmann-Orlin's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/connectivity.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Default traits class of HartmannOrlin algorithm. |
| 37 | 37 |
/// |
| 38 | 38 |
/// Default traits class of HartmannOrlin algorithm. |
| 39 | 39 |
/// \tparam GR The type of the digraph. |
| 40 | 40 |
/// \tparam LEN The type of the length map. |
| 41 | 41 |
/// It must conform to the \ref concepts::Rea_data "Rea_data" concept. |
| 42 | 42 |
#ifdef DOXYGEN |
| 43 | 43 |
template <typename GR, typename LEN> |
| 44 | 44 |
#else |
| 45 | 45 |
template <typename GR, typename LEN, |
| 46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
| 47 | 47 |
#endif |
| 48 | 48 |
struct HartmannOrlinDefaultTraits |
| 49 | 49 |
{
|
| 50 | 50 |
/// The type of the digraph |
| 51 | 51 |
typedef GR Digraph; |
| 52 | 52 |
/// The type of the length map |
| 53 | 53 |
typedef LEN LengthMap; |
| 54 | 54 |
/// The type of the arc lengths |
| 55 | 55 |
typedef typename LengthMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The large value type used for internal computations |
| 58 | 58 |
/// |
| 59 | 59 |
/// The large value type used for internal computations. |
| 60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
| 61 | 61 |
/// otherwise it is \c double. |
| 62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
| 63 | 63 |
typedef double LargeValue; |
| 64 | 64 |
|
| 65 | 65 |
/// The tolerance type used for internal computations |
| 66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 67 | 67 |
|
| 68 | 68 |
/// \brief The path type of the found cycles |
| 69 | 69 |
/// |
| 70 | 70 |
/// The path type of the found cycles. |
| 71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 72 | 72 |
/// and it must have an \c addFront() function. |
| 73 | 73 |
typedef lemon::Path<Digraph> Path; |
| 74 | 74 |
}; |
| 75 | 75 |
|
| 76 | 76 |
// Default traits class for integer value types |
| 77 | 77 |
template <typename GR, typename LEN> |
| 78 | 78 |
struct HartmannOrlinDefaultTraits<GR, LEN, true> |
| 79 | 79 |
{
|
| 80 | 80 |
typedef GR Digraph; |
| 81 | 81 |
typedef LEN LengthMap; |
| 82 | 82 |
typedef typename LengthMap::Value Value; |
| 83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
| 84 | 84 |
typedef long long LargeValue; |
| 85 | 85 |
#else |
| 86 | 86 |
typedef long LargeValue; |
| 87 | 87 |
#endif |
| 88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 89 | 89 |
typedef lemon::Path<Digraph> Path; |
| 90 | 90 |
}; |
| 91 | 91 |
|
| 92 | 92 |
|
| 93 | 93 |
/// \addtogroup min_mean_cycle |
| 94 | 94 |
/// @{
|
| 95 | 95 |
|
| 96 | 96 |
/// \brief Implementation of the Hartmann-Orlin algorithm for finding |
| 97 | 97 |
/// a minimum mean cycle. |
| 98 | 98 |
/// |
| 99 | 99 |
/// This class implements the Hartmann-Orlin algorithm for finding |
| 100 | 100 |
/// a directed cycle of minimum mean length (cost) in a digraph |
| 101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
| 102 | 102 |
/// It is an improved version of \ref Karp "Karp"'s original algorithm, |
| 103 | 103 |
/// it applies an efficient early termination scheme. |
| 104 | 104 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
| 105 | 105 |
/// |
| 106 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 107 | 107 |
/// \tparam LEN The type of the length map. The default |
| 108 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 109 |
/// \tparam TR The traits class that defines various types used by the |
|
| 110 |
/// algorithm. By default, it is \ref HartmannOrlinDefaultTraits |
|
| 111 |
/// "HartmannOrlinDefaultTraits<GR, LEN>". |
|
| 112 |
/// In most cases, this parameter should not be set directly, |
|
| 113 |
/// consider to use the named template parameters instead. |
|
| 109 | 114 |
#ifdef DOXYGEN |
| 110 | 115 |
template <typename GR, typename LEN, typename TR> |
| 111 | 116 |
#else |
| 112 | 117 |
template < typename GR, |
| 113 | 118 |
typename LEN = typename GR::template ArcMap<int>, |
| 114 | 119 |
typename TR = HartmannOrlinDefaultTraits<GR, LEN> > |
| 115 | 120 |
#endif |
| 116 | 121 |
class HartmannOrlin |
| 117 | 122 |
{
|
| 118 | 123 |
public: |
| 119 | 124 |
|
| 120 | 125 |
/// The type of the digraph |
| 121 | 126 |
typedef typename TR::Digraph Digraph; |
| 122 | 127 |
/// The type of the length map |
| 123 | 128 |
typedef typename TR::LengthMap LengthMap; |
| 124 | 129 |
/// The type of the arc lengths |
| 125 | 130 |
typedef typename TR::Value Value; |
| 126 | 131 |
|
| 127 | 132 |
/// \brief The large value type |
| 128 | 133 |
/// |
| 129 | 134 |
/// The large value type used for internal computations. |
| 130 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
|
| 131 |
/// it is \c long \c long if the \c Value type is integer, |
|
| 135 |
/// By default, it is \c long \c long if the \c Value type is integer, |
|
| 132 | 136 |
/// otherwise it is \c double. |
| 133 | 137 |
typedef typename TR::LargeValue LargeValue; |
| 134 | 138 |
|
| 135 | 139 |
/// The tolerance type |
| 136 | 140 |
typedef typename TR::Tolerance Tolerance; |
| 137 | 141 |
|
| 138 | 142 |
/// \brief The path type of the found cycles |
| 139 | 143 |
/// |
| 140 | 144 |
/// The path type of the found cycles. |
| 141 | 145 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
| 142 | 146 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 143 | 147 |
typedef typename TR::Path Path; |
| 144 | 148 |
|
| 145 | 149 |
/// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm |
| 146 | 150 |
typedef TR Traits; |
| 147 | 151 |
|
| 148 | 152 |
private: |
| 149 | 153 |
|
| 150 | 154 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 151 | 155 |
|
| 152 | 156 |
// Data sturcture for path data |
| 153 | 157 |
struct PathData |
| 154 | 158 |
{
|
| 155 | 159 |
LargeValue dist; |
| 156 | 160 |
Arc pred; |
| 157 | 161 |
PathData(LargeValue d, Arc p = INVALID) : |
| 158 | 162 |
dist(d), pred(p) {}
|
| 159 | 163 |
}; |
| 160 | 164 |
|
| 161 | 165 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
| 162 | 166 |
PathDataNodeMap; |
| 163 | 167 |
|
| 164 | 168 |
private: |
| 165 | 169 |
|
| 166 | 170 |
// The digraph the algorithm runs on |
| 167 | 171 |
const Digraph &_gr; |
| 168 | 172 |
// The length of the arcs |
| 169 | 173 |
const LengthMap &_length; |
| 170 | 174 |
|
| 171 | 175 |
// Data for storing the strongly connected components |
| 172 | 176 |
int _comp_num; |
| 173 | 177 |
typename Digraph::template NodeMap<int> _comp; |
| 174 | 178 |
std::vector<std::vector<Node> > _comp_nodes; |
| 175 | 179 |
std::vector<Node>* _nodes; |
| 176 | 180 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
| 177 | 181 |
|
| 178 | 182 |
// Data for the found cycles |
| 179 | 183 |
bool _curr_found, _best_found; |
| 180 | 184 |
LargeValue _curr_length, _best_length; |
| 181 | 185 |
int _curr_size, _best_size; |
| 182 | 186 |
Node _curr_node, _best_node; |
| 183 | 187 |
int _curr_level, _best_level; |
| 184 | 188 |
|
| 185 | 189 |
Path *_cycle_path; |
| 186 | 190 |
bool _local_path; |
| 187 | 191 |
|
| 188 | 192 |
// Node map for storing path data |
| 189 | 193 |
PathDataNodeMap _data; |
| 190 | 194 |
// The processed nodes in the last round |
| 191 | 195 |
std::vector<Node> _process; |
| 192 | 196 |
|
| 193 | 197 |
Tolerance _tolerance; |
| 194 | 198 |
|
| 195 | 199 |
// Infinite constant |
| 196 | 200 |
const LargeValue INF; |
| 197 | 201 |
|
| 198 | 202 |
public: |
| 199 | 203 |
|
| 200 | 204 |
/// \name Named Template Parameters |
| 201 | 205 |
/// @{
|
| 202 | 206 |
|
| 203 | 207 |
template <typename T> |
| 204 | 208 |
struct SetLargeValueTraits : public Traits {
|
| 205 | 209 |
typedef T LargeValue; |
| 206 | 210 |
typedef lemon::Tolerance<T> Tolerance; |
| 207 | 211 |
}; |
| 208 | 212 |
|
| 209 | 213 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 210 | 214 |
/// \c LargeValue type. |
| 211 | 215 |
/// |
| 212 | 216 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
| 213 | 217 |
/// type. It is used for internal computations in the algorithm. |
| 214 | 218 |
template <typename T> |
| 215 | 219 |
struct SetLargeValue |
| 216 | 220 |
: public HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > {
|
| 217 | 221 |
typedef HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > Create; |
| 218 | 222 |
}; |
| 219 | 223 |
|
| 220 | 224 |
template <typename T> |
| 221 | 225 |
struct SetPathTraits : public Traits {
|
| 222 | 226 |
typedef T Path; |
| 223 | 227 |
}; |
| 224 | 228 |
|
| 225 | 229 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 226 | 230 |
/// \c %Path type. |
| 227 | 231 |
/// |
| ... | ... |
@@ -13,215 +13,219 @@ |
| 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_HOWARD_H |
| 20 | 20 |
#define LEMON_HOWARD_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_mean_cycle |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Howard's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/connectivity.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Default traits class of Howard class. |
| 37 | 37 |
/// |
| 38 | 38 |
/// Default traits class of Howard class. |
| 39 | 39 |
/// \tparam GR The type of the digraph. |
| 40 | 40 |
/// \tparam LEN The type of the length map. |
| 41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 42 | 42 |
#ifdef DOXYGEN |
| 43 | 43 |
template <typename GR, typename LEN> |
| 44 | 44 |
#else |
| 45 | 45 |
template <typename GR, typename LEN, |
| 46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
| 47 | 47 |
#endif |
| 48 | 48 |
struct HowardDefaultTraits |
| 49 | 49 |
{
|
| 50 | 50 |
/// The type of the digraph |
| 51 | 51 |
typedef GR Digraph; |
| 52 | 52 |
/// The type of the length map |
| 53 | 53 |
typedef LEN LengthMap; |
| 54 | 54 |
/// The type of the arc lengths |
| 55 | 55 |
typedef typename LengthMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The large value type used for internal computations |
| 58 | 58 |
/// |
| 59 | 59 |
/// The large value type used for internal computations. |
| 60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
| 61 | 61 |
/// otherwise it is \c double. |
| 62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
| 63 | 63 |
typedef double LargeValue; |
| 64 | 64 |
|
| 65 | 65 |
/// The tolerance type used for internal computations |
| 66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 67 | 67 |
|
| 68 | 68 |
/// \brief The path type of the found cycles |
| 69 | 69 |
/// |
| 70 | 70 |
/// The path type of the found cycles. |
| 71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 72 | 72 |
/// and it must have an \c addBack() function. |
| 73 | 73 |
typedef lemon::Path<Digraph> Path; |
| 74 | 74 |
}; |
| 75 | 75 |
|
| 76 | 76 |
// Default traits class for integer value types |
| 77 | 77 |
template <typename GR, typename LEN> |
| 78 | 78 |
struct HowardDefaultTraits<GR, LEN, true> |
| 79 | 79 |
{
|
| 80 | 80 |
typedef GR Digraph; |
| 81 | 81 |
typedef LEN LengthMap; |
| 82 | 82 |
typedef typename LengthMap::Value Value; |
| 83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
| 84 | 84 |
typedef long long LargeValue; |
| 85 | 85 |
#else |
| 86 | 86 |
typedef long LargeValue; |
| 87 | 87 |
#endif |
| 88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 89 | 89 |
typedef lemon::Path<Digraph> Path; |
| 90 | 90 |
}; |
| 91 | 91 |
|
| 92 | 92 |
|
| 93 | 93 |
/// \addtogroup min_mean_cycle |
| 94 | 94 |
/// @{
|
| 95 | 95 |
|
| 96 | 96 |
/// \brief Implementation of Howard's algorithm for finding a minimum |
| 97 | 97 |
/// mean cycle. |
| 98 | 98 |
/// |
| 99 | 99 |
/// This class implements Howard's policy iteration algorithm for finding |
| 100 | 100 |
/// a directed cycle of minimum mean length (cost) in a digraph |
| 101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
| 102 | 102 |
/// This class provides the most efficient algorithm for the |
| 103 | 103 |
/// minimum mean cycle problem, though the best known theoretical |
| 104 | 104 |
/// bound on its running time is exponential. |
| 105 | 105 |
/// |
| 106 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 107 | 107 |
/// \tparam LEN The type of the length map. The default |
| 108 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 109 |
/// \tparam TR The traits class that defines various types used by the |
|
| 110 |
/// algorithm. By default, it is \ref HowardDefaultTraits |
|
| 111 |
/// "HowardDefaultTraits<GR, LEN>". |
|
| 112 |
/// In most cases, this parameter should not be set directly, |
|
| 113 |
/// consider to use the named template parameters instead. |
|
| 109 | 114 |
#ifdef DOXYGEN |
| 110 | 115 |
template <typename GR, typename LEN, typename TR> |
| 111 | 116 |
#else |
| 112 | 117 |
template < typename GR, |
| 113 | 118 |
typename LEN = typename GR::template ArcMap<int>, |
| 114 | 119 |
typename TR = HowardDefaultTraits<GR, LEN> > |
| 115 | 120 |
#endif |
| 116 | 121 |
class Howard |
| 117 | 122 |
{
|
| 118 | 123 |
public: |
| 119 | 124 |
|
| 120 | 125 |
/// The type of the digraph |
| 121 | 126 |
typedef typename TR::Digraph Digraph; |
| 122 | 127 |
/// The type of the length map |
| 123 | 128 |
typedef typename TR::LengthMap LengthMap; |
| 124 | 129 |
/// The type of the arc lengths |
| 125 | 130 |
typedef typename TR::Value Value; |
| 126 | 131 |
|
| 127 | 132 |
/// \brief The large value type |
| 128 | 133 |
/// |
| 129 | 134 |
/// The large value type used for internal computations. |
| 130 |
/// Using the \ref HowardDefaultTraits "default traits class", |
|
| 131 |
/// it is \c long \c long if the \c Value type is integer, |
|
| 135 |
/// By default, it is \c long \c long if the \c Value type is integer, |
|
| 132 | 136 |
/// otherwise it is \c double. |
| 133 | 137 |
typedef typename TR::LargeValue LargeValue; |
| 134 | 138 |
|
| 135 | 139 |
/// The tolerance type |
| 136 | 140 |
typedef typename TR::Tolerance Tolerance; |
| 137 | 141 |
|
| 138 | 142 |
/// \brief The path type of the found cycles |
| 139 | 143 |
/// |
| 140 | 144 |
/// The path type of the found cycles. |
| 141 | 145 |
/// Using the \ref HowardDefaultTraits "default traits class", |
| 142 | 146 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 143 | 147 |
typedef typename TR::Path Path; |
| 144 | 148 |
|
| 145 | 149 |
/// The \ref HowardDefaultTraits "traits class" of the algorithm |
| 146 | 150 |
typedef TR Traits; |
| 147 | 151 |
|
| 148 | 152 |
private: |
| 149 | 153 |
|
| 150 | 154 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 151 | 155 |
|
| 152 | 156 |
// The digraph the algorithm runs on |
| 153 | 157 |
const Digraph &_gr; |
| 154 | 158 |
// The length of the arcs |
| 155 | 159 |
const LengthMap &_length; |
| 156 | 160 |
|
| 157 | 161 |
// Data for the found cycles |
| 158 | 162 |
bool _curr_found, _best_found; |
| 159 | 163 |
LargeValue _curr_length, _best_length; |
| 160 | 164 |
int _curr_size, _best_size; |
| 161 | 165 |
Node _curr_node, _best_node; |
| 162 | 166 |
|
| 163 | 167 |
Path *_cycle_path; |
| 164 | 168 |
bool _local_path; |
| 165 | 169 |
|
| 166 | 170 |
// Internal data used by the algorithm |
| 167 | 171 |
typename Digraph::template NodeMap<Arc> _policy; |
| 168 | 172 |
typename Digraph::template NodeMap<bool> _reached; |
| 169 | 173 |
typename Digraph::template NodeMap<int> _level; |
| 170 | 174 |
typename Digraph::template NodeMap<LargeValue> _dist; |
| 171 | 175 |
|
| 172 | 176 |
// Data for storing the strongly connected components |
| 173 | 177 |
int _comp_num; |
| 174 | 178 |
typename Digraph::template NodeMap<int> _comp; |
| 175 | 179 |
std::vector<std::vector<Node> > _comp_nodes; |
| 176 | 180 |
std::vector<Node>* _nodes; |
| 177 | 181 |
typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs; |
| 178 | 182 |
|
| 179 | 183 |
// Queue used for BFS search |
| 180 | 184 |
std::vector<Node> _queue; |
| 181 | 185 |
int _qfront, _qback; |
| 182 | 186 |
|
| 183 | 187 |
Tolerance _tolerance; |
| 184 | 188 |
|
| 185 | 189 |
// Infinite constant |
| 186 | 190 |
const LargeValue INF; |
| 187 | 191 |
|
| 188 | 192 |
public: |
| 189 | 193 |
|
| 190 | 194 |
/// \name Named Template Parameters |
| 191 | 195 |
/// @{
|
| 192 | 196 |
|
| 193 | 197 |
template <typename T> |
| 194 | 198 |
struct SetLargeValueTraits : public Traits {
|
| 195 | 199 |
typedef T LargeValue; |
| 196 | 200 |
typedef lemon::Tolerance<T> Tolerance; |
| 197 | 201 |
}; |
| 198 | 202 |
|
| 199 | 203 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 200 | 204 |
/// \c LargeValue type. |
| 201 | 205 |
/// |
| 202 | 206 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
| 203 | 207 |
/// type. It is used for internal computations in the algorithm. |
| 204 | 208 |
template <typename T> |
| 205 | 209 |
struct SetLargeValue |
| 206 | 210 |
: public Howard<GR, LEN, SetLargeValueTraits<T> > {
|
| 207 | 211 |
typedef Howard<GR, LEN, SetLargeValueTraits<T> > Create; |
| 208 | 212 |
}; |
| 209 | 213 |
|
| 210 | 214 |
template <typename T> |
| 211 | 215 |
struct SetPathTraits : public Traits {
|
| 212 | 216 |
typedef T Path; |
| 213 | 217 |
}; |
| 214 | 218 |
|
| 215 | 219 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 216 | 220 |
/// \c %Path type. |
| 217 | 221 |
/// |
| 218 | 222 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
| 219 | 223 |
/// type of the found cycles. |
| 220 | 224 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 221 | 225 |
/// and it must have an \c addBack() function. |
| 222 | 226 |
template <typename T> |
| 223 | 227 |
struct SetPath |
| 224 | 228 |
: public Howard<GR, LEN, SetPathTraits<T> > {
|
| 225 | 229 |
typedef Howard<GR, LEN, SetPathTraits<T> > Create; |
| 226 | 230 |
}; |
| 227 | 231 |
|
| ... | ... |
@@ -11,215 +11,219 @@ |
| 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_KARP_H |
| 20 | 20 |
#define LEMON_KARP_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_mean_cycle |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Karp's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/connectivity.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Default traits class of Karp algorithm. |
| 37 | 37 |
/// |
| 38 | 38 |
/// Default traits class of Karp algorithm. |
| 39 | 39 |
/// \tparam GR The type of the digraph. |
| 40 | 40 |
/// \tparam LEN The type of the length map. |
| 41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 42 | 42 |
#ifdef DOXYGEN |
| 43 | 43 |
template <typename GR, typename LEN> |
| 44 | 44 |
#else |
| 45 | 45 |
template <typename GR, typename LEN, |
| 46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
| 47 | 47 |
#endif |
| 48 | 48 |
struct KarpDefaultTraits |
| 49 | 49 |
{
|
| 50 | 50 |
/// The type of the digraph |
| 51 | 51 |
typedef GR Digraph; |
| 52 | 52 |
/// The type of the length map |
| 53 | 53 |
typedef LEN LengthMap; |
| 54 | 54 |
/// The type of the arc lengths |
| 55 | 55 |
typedef typename LengthMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The large value type used for internal computations |
| 58 | 58 |
/// |
| 59 | 59 |
/// The large value type used for internal computations. |
| 60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
| 61 | 61 |
/// otherwise it is \c double. |
| 62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
| 63 | 63 |
typedef double LargeValue; |
| 64 | 64 |
|
| 65 | 65 |
/// The tolerance type used for internal computations |
| 66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 67 | 67 |
|
| 68 | 68 |
/// \brief The path type of the found cycles |
| 69 | 69 |
/// |
| 70 | 70 |
/// The path type of the found cycles. |
| 71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 72 | 72 |
/// and it must have an \c addFront() function. |
| 73 | 73 |
typedef lemon::Path<Digraph> Path; |
| 74 | 74 |
}; |
| 75 | 75 |
|
| 76 | 76 |
// Default traits class for integer value types |
| 77 | 77 |
template <typename GR, typename LEN> |
| 78 | 78 |
struct KarpDefaultTraits<GR, LEN, true> |
| 79 | 79 |
{
|
| 80 | 80 |
typedef GR Digraph; |
| 81 | 81 |
typedef LEN LengthMap; |
| 82 | 82 |
typedef typename LengthMap::Value Value; |
| 83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
| 84 | 84 |
typedef long long LargeValue; |
| 85 | 85 |
#else |
| 86 | 86 |
typedef long LargeValue; |
| 87 | 87 |
#endif |
| 88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 89 | 89 |
typedef lemon::Path<Digraph> Path; |
| 90 | 90 |
}; |
| 91 | 91 |
|
| 92 | 92 |
|
| 93 | 93 |
/// \addtogroup min_mean_cycle |
| 94 | 94 |
/// @{
|
| 95 | 95 |
|
| 96 | 96 |
/// \brief Implementation of Karp's algorithm for finding a minimum |
| 97 | 97 |
/// mean cycle. |
| 98 | 98 |
/// |
| 99 | 99 |
/// This class implements Karp's algorithm for finding a directed |
| 100 | 100 |
/// cycle of minimum mean length (cost) in a digraph |
| 101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
| 102 | 102 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
| 103 | 103 |
/// |
| 104 | 104 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 105 | 105 |
/// \tparam LEN The type of the length map. The default |
| 106 | 106 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 107 |
/// \tparam TR The traits class that defines various types used by the |
|
| 108 |
/// algorithm. By default, it is \ref KarpDefaultTraits |
|
| 109 |
/// "KarpDefaultTraits<GR, LEN>". |
|
| 110 |
/// In most cases, this parameter should not be set directly, |
|
| 111 |
/// consider to use the named template parameters instead. |
|
| 107 | 112 |
#ifdef DOXYGEN |
| 108 | 113 |
template <typename GR, typename LEN, typename TR> |
| 109 | 114 |
#else |
| 110 | 115 |
template < typename GR, |
| 111 | 116 |
typename LEN = typename GR::template ArcMap<int>, |
| 112 | 117 |
typename TR = KarpDefaultTraits<GR, LEN> > |
| 113 | 118 |
#endif |
| 114 | 119 |
class Karp |
| 115 | 120 |
{
|
| 116 | 121 |
public: |
| 117 | 122 |
|
| 118 | 123 |
/// The type of the digraph |
| 119 | 124 |
typedef typename TR::Digraph Digraph; |
| 120 | 125 |
/// The type of the length map |
| 121 | 126 |
typedef typename TR::LengthMap LengthMap; |
| 122 | 127 |
/// The type of the arc lengths |
| 123 | 128 |
typedef typename TR::Value Value; |
| 124 | 129 |
|
| 125 | 130 |
/// \brief The large value type |
| 126 | 131 |
/// |
| 127 | 132 |
/// The large value type used for internal computations. |
| 128 |
/// Using the \ref KarpDefaultTraits "default traits class", |
|
| 129 |
/// it is \c long \c long if the \c Value type is integer, |
|
| 133 |
/// By default, it is \c long \c long if the \c Value type is integer, |
|
| 130 | 134 |
/// otherwise it is \c double. |
| 131 | 135 |
typedef typename TR::LargeValue LargeValue; |
| 132 | 136 |
|
| 133 | 137 |
/// The tolerance type |
| 134 | 138 |
typedef typename TR::Tolerance Tolerance; |
| 135 | 139 |
|
| 136 | 140 |
/// \brief The path type of the found cycles |
| 137 | 141 |
/// |
| 138 | 142 |
/// The path type of the found cycles. |
| 139 | 143 |
/// Using the \ref KarpDefaultTraits "default traits class", |
| 140 | 144 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 141 | 145 |
typedef typename TR::Path Path; |
| 142 | 146 |
|
| 143 | 147 |
/// The \ref KarpDefaultTraits "traits class" of the algorithm |
| 144 | 148 |
typedef TR Traits; |
| 145 | 149 |
|
| 146 | 150 |
private: |
| 147 | 151 |
|
| 148 | 152 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 149 | 153 |
|
| 150 | 154 |
// Data sturcture for path data |
| 151 | 155 |
struct PathData |
| 152 | 156 |
{
|
| 153 | 157 |
LargeValue dist; |
| 154 | 158 |
Arc pred; |
| 155 | 159 |
PathData(LargeValue d, Arc p = INVALID) : |
| 156 | 160 |
dist(d), pred(p) {}
|
| 157 | 161 |
}; |
| 158 | 162 |
|
| 159 | 163 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
| 160 | 164 |
PathDataNodeMap; |
| 161 | 165 |
|
| 162 | 166 |
private: |
| 163 | 167 |
|
| 164 | 168 |
// The digraph the algorithm runs on |
| 165 | 169 |
const Digraph &_gr; |
| 166 | 170 |
// The length of the arcs |
| 167 | 171 |
const LengthMap &_length; |
| 168 | 172 |
|
| 169 | 173 |
// Data for storing the strongly connected components |
| 170 | 174 |
int _comp_num; |
| 171 | 175 |
typename Digraph::template NodeMap<int> _comp; |
| 172 | 176 |
std::vector<std::vector<Node> > _comp_nodes; |
| 173 | 177 |
std::vector<Node>* _nodes; |
| 174 | 178 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
| 175 | 179 |
|
| 176 | 180 |
// Data for the found cycle |
| 177 | 181 |
LargeValue _cycle_length; |
| 178 | 182 |
int _cycle_size; |
| 179 | 183 |
Node _cycle_node; |
| 180 | 184 |
|
| 181 | 185 |
Path *_cycle_path; |
| 182 | 186 |
bool _local_path; |
| 183 | 187 |
|
| 184 | 188 |
// Node map for storing path data |
| 185 | 189 |
PathDataNodeMap _data; |
| 186 | 190 |
// The processed nodes in the last round |
| 187 | 191 |
std::vector<Node> _process; |
| 188 | 192 |
|
| 189 | 193 |
Tolerance _tolerance; |
| 190 | 194 |
|
| 191 | 195 |
// Infinite constant |
| 192 | 196 |
const LargeValue INF; |
| 193 | 197 |
|
| 194 | 198 |
public: |
| 195 | 199 |
|
| 196 | 200 |
/// \name Named Template Parameters |
| 197 | 201 |
/// @{
|
| 198 | 202 |
|
| 199 | 203 |
template <typename T> |
| 200 | 204 |
struct SetLargeValueTraits : public Traits {
|
| 201 | 205 |
typedef T LargeValue; |
| 202 | 206 |
typedef lemon::Tolerance<T> Tolerance; |
| 203 | 207 |
}; |
| 204 | 208 |
|
| 205 | 209 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 206 | 210 |
/// \c LargeValue type. |
| 207 | 211 |
/// |
| 208 | 212 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
| 209 | 213 |
/// type. It is used for internal computations in the algorithm. |
| 210 | 214 |
template <typename T> |
| 211 | 215 |
struct SetLargeValue |
| 212 | 216 |
: public Karp<GR, LEN, SetLargeValueTraits<T> > {
|
| 213 | 217 |
typedef Karp<GR, LEN, SetLargeValueTraits<T> > Create; |
| 214 | 218 |
}; |
| 215 | 219 |
|
| 216 | 220 |
template <typename T> |
| 217 | 221 |
struct SetPathTraits : public Traits {
|
| 218 | 222 |
typedef T Path; |
| 219 | 223 |
}; |
| 220 | 224 |
|
| 221 | 225 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 222 | 226 |
/// \c %Path type. |
| 223 | 227 |
/// |
| 224 | 228 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
| 225 | 229 |
/// type of the found cycles. |
| ... | ... |
@@ -19,203 +19,204 @@ |
| 19 | 19 |
#ifndef LEMON_MIN_COST_ARBORESCENCE_H |
| 20 | 20 |
#define LEMON_MIN_COST_ARBORESCENCE_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup spantree |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Minimum Cost Arborescence algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/list_graph.h> |
| 29 | 29 |
#include <lemon/bin_heap.h> |
| 30 | 30 |
#include <lemon/assert.h> |
| 31 | 31 |
|
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
|
| 35 | 35 |
/// \brief Default traits class for MinCostArborescence class. |
| 36 | 36 |
/// |
| 37 | 37 |
/// Default traits class for MinCostArborescence class. |
| 38 | 38 |
/// \param GR Digraph type. |
| 39 | 39 |
/// \param CM Type of the cost map. |
| 40 | 40 |
template <class GR, class CM> |
| 41 | 41 |
struct MinCostArborescenceDefaultTraits{
|
| 42 | 42 |
|
| 43 | 43 |
/// \brief The digraph type the algorithm runs on. |
| 44 | 44 |
typedef GR Digraph; |
| 45 | 45 |
|
| 46 | 46 |
/// \brief The type of the map that stores the arc costs. |
| 47 | 47 |
/// |
| 48 | 48 |
/// The type of the map that stores the arc costs. |
| 49 | 49 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 50 | 50 |
typedef CM CostMap; |
| 51 | 51 |
|
| 52 | 52 |
/// \brief The value type of the costs. |
| 53 | 53 |
/// |
| 54 | 54 |
/// The value type of the costs. |
| 55 | 55 |
typedef typename CostMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The type of the map that stores which arcs are in the |
| 58 | 58 |
/// arborescence. |
| 59 | 59 |
/// |
| 60 | 60 |
/// The type of the map that stores which arcs are in the |
| 61 | 61 |
/// arborescence. It must conform to the \ref concepts::WriteMap |
| 62 | 62 |
/// "WriteMap" concept, and its value type must be \c bool |
| 63 | 63 |
/// (or convertible). Initially it will be set to \c false on each |
| 64 | 64 |
/// arc, then it will be set on each arborescence arc once. |
| 65 | 65 |
typedef typename Digraph::template ArcMap<bool> ArborescenceMap; |
| 66 | 66 |
|
| 67 | 67 |
/// \brief Instantiates a \c ArborescenceMap. |
| 68 | 68 |
/// |
| 69 | 69 |
/// This function instantiates a \c ArborescenceMap. |
| 70 | 70 |
/// \param digraph The digraph to which we would like to calculate |
| 71 | 71 |
/// the \c ArborescenceMap. |
| 72 | 72 |
static ArborescenceMap *createArborescenceMap(const Digraph &digraph){
|
| 73 | 73 |
return new ArborescenceMap(digraph); |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 | 76 |
/// \brief The type of the \c PredMap |
| 77 | 77 |
/// |
| 78 | 78 |
/// The type of the \c PredMap. It must confrom to the |
| 79 | 79 |
/// \ref concepts::WriteMap "WriteMap" concept, and its value type |
| 80 | 80 |
/// must be the \c Arc type of the digraph. |
| 81 | 81 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 82 | 82 |
|
| 83 | 83 |
/// \brief Instantiates a \c PredMap. |
| 84 | 84 |
/// |
| 85 | 85 |
/// This function instantiates a \c PredMap. |
| 86 | 86 |
/// \param digraph The digraph to which we would like to define the |
| 87 | 87 |
/// \c PredMap. |
| 88 | 88 |
static PredMap *createPredMap(const Digraph &digraph){
|
| 89 | 89 |
return new PredMap(digraph); |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 | 92 |
}; |
| 93 | 93 |
|
| 94 | 94 |
/// \ingroup spantree |
| 95 | 95 |
/// |
| 96 | 96 |
/// \brief Minimum Cost Arborescence algorithm class. |
| 97 | 97 |
/// |
| 98 | 98 |
/// This class provides an efficient implementation of the |
| 99 | 99 |
/// Minimum Cost Arborescence algorithm. The arborescence is a tree |
| 100 | 100 |
/// which is directed from a given source node of the digraph. One or |
| 101 | 101 |
/// more sources should be given to the algorithm and it will calculate |
| 102 | 102 |
/// the minimum cost subgraph that is the union of arborescences with the |
| 103 | 103 |
/// given sources and spans all the nodes which are reachable from the |
| 104 | 104 |
/// sources. The time complexity of the algorithm is O(n<sup>2</sup>+e). |
| 105 | 105 |
/// |
| 106 | 106 |
/// The algorithm also provides an optimal dual solution, therefore |
| 107 | 107 |
/// the optimality of the solution can be checked. |
| 108 | 108 |
/// |
| 109 | 109 |
/// \param GR The digraph type the algorithm runs on. |
| 110 | 110 |
/// \param CM A read-only arc map storing the costs of the |
| 111 | 111 |
/// arcs. It is read once for each arc, so the map may involve in |
| 112 | 112 |
/// relatively time consuming process to compute the arc costs if |
| 113 | 113 |
/// it is necessary. The default map type is \ref |
| 114 | 114 |
/// concepts::Digraph::ArcMap "Digraph::ArcMap<int>". |
| 115 |
/// \param TR Traits class to set various data types used |
|
| 116 |
/// by the algorithm. The default traits class is |
|
| 117 |
/// \ |
|
| 115 |
/// \tparam TR The traits class that defines various types used by the |
|
| 116 |
/// algorithm. By default, it is \ref MinCostArborescenceDefaultTraits |
|
| 118 | 117 |
/// "MinCostArborescenceDefaultTraits<GR, CM>". |
| 118 |
/// In most cases, this parameter should not be set directly, |
|
| 119 |
/// consider to use the named template parameters instead. |
|
| 119 | 120 |
#ifndef DOXYGEN |
| 120 | 121 |
template <typename GR, |
| 121 | 122 |
typename CM = typename GR::template ArcMap<int>, |
| 122 | 123 |
typename TR = |
| 123 | 124 |
MinCostArborescenceDefaultTraits<GR, CM> > |
| 124 | 125 |
#else |
| 125 |
template <typename GR, typename CM, |
|
| 126 |
template <typename GR, typename CM, typename TR> |
|
| 126 | 127 |
#endif |
| 127 | 128 |
class MinCostArborescence {
|
| 128 | 129 |
public: |
| 129 | 130 |
|
| 130 | 131 |
/// \brief The \ref MinCostArborescenceDefaultTraits "traits class" |
| 131 | 132 |
/// of the algorithm. |
| 132 | 133 |
typedef TR Traits; |
| 133 | 134 |
/// The type of the underlying digraph. |
| 134 | 135 |
typedef typename Traits::Digraph Digraph; |
| 135 | 136 |
/// The type of the map that stores the arc costs. |
| 136 | 137 |
typedef typename Traits::CostMap CostMap; |
| 137 | 138 |
///The type of the costs of the arcs. |
| 138 | 139 |
typedef typename Traits::Value Value; |
| 139 | 140 |
///The type of the predecessor map. |
| 140 | 141 |
typedef typename Traits::PredMap PredMap; |
| 141 | 142 |
///The type of the map that stores which arcs are in the arborescence. |
| 142 | 143 |
typedef typename Traits::ArborescenceMap ArborescenceMap; |
| 143 | 144 |
|
| 144 | 145 |
typedef MinCostArborescence Create; |
| 145 | 146 |
|
| 146 | 147 |
private: |
| 147 | 148 |
|
| 148 | 149 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 149 | 150 |
|
| 150 | 151 |
struct CostArc {
|
| 151 | 152 |
|
| 152 | 153 |
Arc arc; |
| 153 | 154 |
Value value; |
| 154 | 155 |
|
| 155 | 156 |
CostArc() {}
|
| 156 | 157 |
CostArc(Arc _arc, Value _value) : arc(_arc), value(_value) {}
|
| 157 | 158 |
|
| 158 | 159 |
}; |
| 159 | 160 |
|
| 160 | 161 |
const Digraph *_digraph; |
| 161 | 162 |
const CostMap *_cost; |
| 162 | 163 |
|
| 163 | 164 |
PredMap *_pred; |
| 164 | 165 |
bool local_pred; |
| 165 | 166 |
|
| 166 | 167 |
ArborescenceMap *_arborescence; |
| 167 | 168 |
bool local_arborescence; |
| 168 | 169 |
|
| 169 | 170 |
typedef typename Digraph::template ArcMap<int> ArcOrder; |
| 170 | 171 |
ArcOrder *_arc_order; |
| 171 | 172 |
|
| 172 | 173 |
typedef typename Digraph::template NodeMap<int> NodeOrder; |
| 173 | 174 |
NodeOrder *_node_order; |
| 174 | 175 |
|
| 175 | 176 |
typedef typename Digraph::template NodeMap<CostArc> CostArcMap; |
| 176 | 177 |
CostArcMap *_cost_arcs; |
| 177 | 178 |
|
| 178 | 179 |
struct StackLevel {
|
| 179 | 180 |
|
| 180 | 181 |
std::vector<CostArc> arcs; |
| 181 | 182 |
int node_level; |
| 182 | 183 |
|
| 183 | 184 |
}; |
| 184 | 185 |
|
| 185 | 186 |
std::vector<StackLevel> level_stack; |
| 186 | 187 |
std::vector<Node> queue; |
| 187 | 188 |
|
| 188 | 189 |
typedef std::vector<typename Digraph::Node> DualNodeList; |
| 189 | 190 |
|
| 190 | 191 |
DualNodeList _dual_node_list; |
| 191 | 192 |
|
| 192 | 193 |
struct DualVariable {
|
| 193 | 194 |
int begin, end; |
| 194 | 195 |
Value value; |
| 195 | 196 |
|
| 196 | 197 |
DualVariable(int _begin, int _end, Value _value) |
| 197 | 198 |
: begin(_begin), end(_end), value(_value) {}
|
| 198 | 199 |
|
| 199 | 200 |
}; |
| 200 | 201 |
|
| 201 | 202 |
typedef std::vector<DualVariable> DualVariables; |
| 202 | 203 |
|
| 203 | 204 |
DualVariables _dual_variables; |
| 204 | 205 |
|
| 205 | 206 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
| 206 | 207 |
|
| 207 | 208 |
HeapCrossRef *_heap_cross_ref; |
| 208 | 209 |
|
| 209 | 210 |
typedef BinHeap<int, HeapCrossRef> Heap; |
| 210 | 211 |
|
| 211 | 212 |
Heap *_heap; |
| 212 | 213 |
|
| 213 | 214 |
protected: |
| 214 | 215 |
|
| 215 | 216 |
MinCostArborescence() {}
|
| 216 | 217 |
|
| 217 | 218 |
private: |
| 218 | 219 |
|
| 219 | 220 |
void createStructures() {
|
| 220 | 221 |
if (!_pred) {
|
| 221 | 222 |
local_pred = true; |
| ... | ... |
@@ -425,397 +425,404 @@ |
| 425 | 425 |
|
| 426 | 426 |
int rn = merge_roots[node].front(); |
| 427 | 427 |
|
| 428 | 428 |
int xn = node_data[rn].next; |
| 429 | 429 |
Node xnode = order_list[xn]; |
| 430 | 430 |
|
| 431 | 431 |
int yn = node_data[rn].prev; |
| 432 | 432 |
Node ynode = order_list[yn]; |
| 433 | 433 |
|
| 434 | 434 |
bool rd; |
| 435 | 435 |
if (!external(xnode, rorder, child_lists, |
| 436 | 436 |
ancestor_map, low_map)) {
|
| 437 | 437 |
rd = true; |
| 438 | 438 |
} else if (!external(ynode, rorder, child_lists, |
| 439 | 439 |
ancestor_map, low_map)) {
|
| 440 | 440 |
rd = false; |
| 441 | 441 |
} else if (pertinent(xnode, embed_arc, merge_roots)) {
|
| 442 | 442 |
rd = true; |
| 443 | 443 |
} else {
|
| 444 | 444 |
rd = false; |
| 445 | 445 |
} |
| 446 | 446 |
|
| 447 | 447 |
merge_stack.push_back(std::make_pair(rn, rd)); |
| 448 | 448 |
|
| 449 | 449 |
pn = rn; |
| 450 | 450 |
n = rd ? xn : yn; |
| 451 | 451 |
|
| 452 | 452 |
} else if (!external(node, rorder, child_lists, |
| 453 | 453 |
ancestor_map, low_map)) {
|
| 454 | 454 |
int nn = (node_data[n].next != pn ? |
| 455 | 455 |
node_data[n].next : node_data[n].prev); |
| 456 | 456 |
|
| 457 | 457 |
bool nd = n == node_data[nn].prev; |
| 458 | 458 |
|
| 459 | 459 |
if (nd) node_data[nn].prev = pn; |
| 460 | 460 |
else node_data[nn].next = pn; |
| 461 | 461 |
|
| 462 | 462 |
if (n == node_data[pn].prev) node_data[pn].prev = nn; |
| 463 | 463 |
else node_data[pn].next = nn; |
| 464 | 464 |
|
| 465 | 465 |
node_data[nn].inverted = |
| 466 | 466 |
(node_data[nn].prev == node_data[nn].next && nd != rd); |
| 467 | 467 |
|
| 468 | 468 |
n = nn; |
| 469 | 469 |
} |
| 470 | 470 |
else break; |
| 471 | 471 |
|
| 472 | 472 |
} |
| 473 | 473 |
|
| 474 | 474 |
if (!merge_stack.empty() || n == rn) {
|
| 475 | 475 |
break; |
| 476 | 476 |
} |
| 477 | 477 |
} |
| 478 | 478 |
} |
| 479 | 479 |
|
| 480 | 480 |
void initFace(const Node& node, NodeData& node_data, |
| 481 | 481 |
const OrderMap& order_map, const OrderList& order_list) {
|
| 482 | 482 |
int n = order_map[node]; |
| 483 | 483 |
int rn = n + order_list.size(); |
| 484 | 484 |
|
| 485 | 485 |
node_data[n].next = node_data[n].prev = rn; |
| 486 | 486 |
node_data[rn].next = node_data[rn].prev = n; |
| 487 | 487 |
|
| 488 | 488 |
node_data[n].visited = order_list.size(); |
| 489 | 489 |
node_data[rn].visited = order_list.size(); |
| 490 | 490 |
|
| 491 | 491 |
} |
| 492 | 492 |
|
| 493 | 493 |
bool external(const Node& node, int rorder, |
| 494 | 494 |
ChildLists& child_lists, AncestorMap& ancestor_map, |
| 495 | 495 |
LowMap& low_map) {
|
| 496 | 496 |
Node child = child_lists[node].first; |
| 497 | 497 |
|
| 498 | 498 |
if (child != INVALID) {
|
| 499 | 499 |
if (low_map[child] < rorder) return true; |
| 500 | 500 |
} |
| 501 | 501 |
|
| 502 | 502 |
if (ancestor_map[node] < rorder) return true; |
| 503 | 503 |
|
| 504 | 504 |
return false; |
| 505 | 505 |
} |
| 506 | 506 |
|
| 507 | 507 |
bool pertinent(const Node& node, const EmbedArc& embed_arc, |
| 508 | 508 |
const MergeRoots& merge_roots) {
|
| 509 | 509 |
return !merge_roots[node].empty() || embed_arc[node]; |
| 510 | 510 |
} |
| 511 | 511 |
|
| 512 | 512 |
}; |
| 513 | 513 |
|
| 514 | 514 |
} |
| 515 | 515 |
|
| 516 | 516 |
/// \ingroup planar |
| 517 | 517 |
/// |
| 518 | 518 |
/// \brief Planarity checking of an undirected simple graph |
| 519 | 519 |
/// |
| 520 | 520 |
/// This function implements the Boyer-Myrvold algorithm for |
| 521 |
/// planarity checking of an undirected graph. It is a simplified |
|
| 521 |
/// planarity checking of an undirected simple graph. It is a simplified |
|
| 522 | 522 |
/// version of the PlanarEmbedding algorithm class because neither |
| 523 |
/// the embedding nor the |
|
| 523 |
/// the embedding nor the Kuratowski subdivisons are computed. |
|
| 524 | 524 |
template <typename GR> |
| 525 | 525 |
bool checkPlanarity(const GR& graph) {
|
| 526 | 526 |
_planarity_bits::PlanarityChecking<GR> pc(graph); |
| 527 | 527 |
return pc.run(); |
| 528 | 528 |
} |
| 529 | 529 |
|
| 530 | 530 |
/// \ingroup planar |
| 531 | 531 |
/// |
| 532 | 532 |
/// \brief Planar embedding of an undirected simple graph |
| 533 | 533 |
/// |
| 534 | 534 |
/// This class implements the Boyer-Myrvold algorithm for planar |
| 535 |
/// embedding of an undirected graph. The planar embedding is an |
|
| 535 |
/// embedding of an undirected simple graph. The planar embedding is an |
|
| 536 | 536 |
/// ordering of the outgoing edges of the nodes, which is a possible |
| 537 | 537 |
/// configuration to draw the graph in the plane. If there is not |
| 538 |
/// such ordering then the graph contains a \f$ K_5 \f$ (full graph |
|
| 539 |
/// with 5 nodes) or a \f$ K_{3,3} \f$ (complete bipartite graph on
|
|
| 540 |
/// |
|
| 538 |
/// such ordering then the graph contains a K<sub>5</sub> (full graph |
|
| 539 |
/// with 5 nodes) or a K<sub>3,3</sub> (complete bipartite graph on |
|
| 540 |
/// 3 Red and 3 Blue nodes) subdivision. |
|
| 541 | 541 |
/// |
| 542 | 542 |
/// The current implementation calculates either an embedding or a |
| 543 |
/// Kuratowski subdivision. The running time of the algorithm is |
|
| 544 |
/// \f$ O(n) \f$. |
|
| 543 |
/// Kuratowski subdivision. The running time of the algorithm is O(n). |
|
| 544 |
/// |
|
| 545 |
/// \see PlanarDrawing, checkPlanarity() |
|
| 545 | 546 |
template <typename Graph> |
| 546 | 547 |
class PlanarEmbedding {
|
| 547 | 548 |
private: |
| 548 | 549 |
|
| 549 | 550 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 550 | 551 |
|
| 551 | 552 |
const Graph& _graph; |
| 552 | 553 |
typename Graph::template ArcMap<Arc> _embedding; |
| 553 | 554 |
|
| 554 | 555 |
typename Graph::template EdgeMap<bool> _kuratowski; |
| 555 | 556 |
|
| 556 | 557 |
private: |
| 557 | 558 |
|
| 558 | 559 |
typedef typename Graph::template NodeMap<Arc> PredMap; |
| 559 | 560 |
|
| 560 | 561 |
typedef typename Graph::template EdgeMap<bool> TreeMap; |
| 561 | 562 |
|
| 562 | 563 |
typedef typename Graph::template NodeMap<int> OrderMap; |
| 563 | 564 |
typedef std::vector<Node> OrderList; |
| 564 | 565 |
|
| 565 | 566 |
typedef typename Graph::template NodeMap<int> LowMap; |
| 566 | 567 |
typedef typename Graph::template NodeMap<int> AncestorMap; |
| 567 | 568 |
|
| 568 | 569 |
typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode; |
| 569 | 570 |
typedef std::vector<NodeDataNode> NodeData; |
| 570 | 571 |
|
| 571 | 572 |
typedef _planarity_bits::ChildListNode<Graph> ChildListNode; |
| 572 | 573 |
typedef typename Graph::template NodeMap<ChildListNode> ChildLists; |
| 573 | 574 |
|
| 574 | 575 |
typedef typename Graph::template NodeMap<std::list<int> > MergeRoots; |
| 575 | 576 |
|
| 576 | 577 |
typedef typename Graph::template NodeMap<Arc> EmbedArc; |
| 577 | 578 |
|
| 578 | 579 |
typedef _planarity_bits::ArcListNode<Graph> ArcListNode; |
| 579 | 580 |
typedef typename Graph::template ArcMap<ArcListNode> ArcLists; |
| 580 | 581 |
|
| 581 | 582 |
typedef typename Graph::template NodeMap<bool> FlipMap; |
| 582 | 583 |
|
| 583 | 584 |
typedef typename Graph::template NodeMap<int> TypeMap; |
| 584 | 585 |
|
| 585 | 586 |
enum IsolatorNodeType {
|
| 586 | 587 |
HIGHX = 6, LOWX = 7, |
| 587 | 588 |
HIGHY = 8, LOWY = 9, |
| 588 | 589 |
ROOT = 10, PERTINENT = 11, |
| 589 | 590 |
INTERNAL = 12 |
| 590 | 591 |
}; |
| 591 | 592 |
|
| 592 | 593 |
public: |
| 593 | 594 |
|
| 594 |
/// \brief The map for |
|
| 595 |
/// \brief The map type for storing the embedding |
|
| 596 |
/// |
|
| 597 |
/// The map type for storing the embedding. |
|
| 598 |
/// \see embeddingMap() |
|
| 595 | 599 |
typedef typename Graph::template ArcMap<Arc> EmbeddingMap; |
| 596 | 600 |
|
| 597 | 601 |
/// \brief Constructor |
| 598 | 602 |
/// |
| 599 |
/// \note The graph should be simple, i.e. parallel and loop arc |
|
| 600 |
/// free. |
|
| 603 |
/// Constructor. |
|
| 604 |
/// \pre The graph must be simple, i.e. it should not |
|
| 605 |
/// contain parallel or loop arcs. |
|
| 601 | 606 |
PlanarEmbedding(const Graph& graph) |
| 602 | 607 |
: _graph(graph), _embedding(_graph), _kuratowski(graph, false) {}
|
| 603 | 608 |
|
| 604 |
/// \brief |
|
| 609 |
/// \brief Run the algorithm. |
|
| 605 | 610 |
/// |
| 606 |
/// Runs the algorithm. |
|
| 607 |
/// \param kuratowski If the parameter is false, then the |
|
| 611 |
/// This function runs the algorithm. |
|
| 612 |
/// \param kuratowski If this parameter is set to \c false, then the |
|
| 608 | 613 |
/// algorithm does not compute a Kuratowski subdivision. |
| 609 |
///\return |
|
| 614 |
/// \return \c true if the graph is planar. |
|
| 610 | 615 |
bool run(bool kuratowski = true) {
|
| 611 | 616 |
typedef _planarity_bits::PlanarityVisitor<Graph> Visitor; |
| 612 | 617 |
|
| 613 | 618 |
PredMap pred_map(_graph, INVALID); |
| 614 | 619 |
TreeMap tree_map(_graph, false); |
| 615 | 620 |
|
| 616 | 621 |
OrderMap order_map(_graph, -1); |
| 617 | 622 |
OrderList order_list; |
| 618 | 623 |
|
| 619 | 624 |
AncestorMap ancestor_map(_graph, -1); |
| 620 | 625 |
LowMap low_map(_graph, -1); |
| 621 | 626 |
|
| 622 | 627 |
Visitor visitor(_graph, pred_map, tree_map, |
| 623 | 628 |
order_map, order_list, ancestor_map, low_map); |
| 624 | 629 |
DfsVisit<Graph, Visitor> visit(_graph, visitor); |
| 625 | 630 |
visit.run(); |
| 626 | 631 |
|
| 627 | 632 |
ChildLists child_lists(_graph); |
| 628 | 633 |
createChildLists(tree_map, order_map, low_map, child_lists); |
| 629 | 634 |
|
| 630 | 635 |
NodeData node_data(2 * order_list.size()); |
| 631 | 636 |
|
| 632 | 637 |
EmbedArc embed_arc(_graph, INVALID); |
| 633 | 638 |
|
| 634 | 639 |
MergeRoots merge_roots(_graph); |
| 635 | 640 |
|
| 636 | 641 |
ArcLists arc_lists(_graph); |
| 637 | 642 |
|
| 638 | 643 |
FlipMap flip_map(_graph, false); |
| 639 | 644 |
|
| 640 | 645 |
for (int i = order_list.size() - 1; i >= 0; --i) {
|
| 641 | 646 |
|
| 642 | 647 |
Node node = order_list[i]; |
| 643 | 648 |
|
| 644 | 649 |
node_data[i].first = INVALID; |
| 645 | 650 |
|
| 646 | 651 |
Node source = node; |
| 647 | 652 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
| 648 | 653 |
Node target = _graph.target(e); |
| 649 | 654 |
|
| 650 | 655 |
if (order_map[source] < order_map[target] && tree_map[e]) {
|
| 651 | 656 |
initFace(target, arc_lists, node_data, |
| 652 | 657 |
pred_map, order_map, order_list); |
| 653 | 658 |
} |
| 654 | 659 |
} |
| 655 | 660 |
|
| 656 | 661 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
| 657 | 662 |
Node target = _graph.target(e); |
| 658 | 663 |
|
| 659 | 664 |
if (order_map[source] < order_map[target] && !tree_map[e]) {
|
| 660 | 665 |
embed_arc[target] = e; |
| 661 | 666 |
walkUp(target, source, i, pred_map, low_map, |
| 662 | 667 |
order_map, order_list, node_data, merge_roots); |
| 663 | 668 |
} |
| 664 | 669 |
} |
| 665 | 670 |
|
| 666 | 671 |
for (typename MergeRoots::Value::iterator it = |
| 667 | 672 |
merge_roots[node].begin(); it != merge_roots[node].end(); ++it) {
|
| 668 | 673 |
int rn = *it; |
| 669 | 674 |
walkDown(rn, i, node_data, arc_lists, flip_map, order_list, |
| 670 | 675 |
child_lists, ancestor_map, low_map, embed_arc, merge_roots); |
| 671 | 676 |
} |
| 672 | 677 |
merge_roots[node].clear(); |
| 673 | 678 |
|
| 674 | 679 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
| 675 | 680 |
Node target = _graph.target(e); |
| 676 | 681 |
|
| 677 | 682 |
if (order_map[source] < order_map[target] && !tree_map[e]) {
|
| 678 | 683 |
if (embed_arc[target] != INVALID) {
|
| 679 | 684 |
if (kuratowski) {
|
| 680 | 685 |
isolateKuratowski(e, node_data, arc_lists, flip_map, |
| 681 | 686 |
order_map, order_list, pred_map, child_lists, |
| 682 | 687 |
ancestor_map, low_map, |
| 683 | 688 |
embed_arc, merge_roots); |
| 684 | 689 |
} |
| 685 | 690 |
return false; |
| 686 | 691 |
} |
| 687 | 692 |
} |
| 688 | 693 |
} |
| 689 | 694 |
} |
| 690 | 695 |
|
| 691 | 696 |
for (int i = 0; i < int(order_list.size()); ++i) {
|
| 692 | 697 |
|
| 693 | 698 |
mergeRemainingFaces(order_list[i], node_data, order_list, order_map, |
| 694 | 699 |
child_lists, arc_lists); |
| 695 | 700 |
storeEmbedding(order_list[i], node_data, order_map, pred_map, |
| 696 | 701 |
arc_lists, flip_map); |
| 697 | 702 |
} |
| 698 | 703 |
|
| 699 | 704 |
return true; |
| 700 | 705 |
} |
| 701 | 706 |
|
| 702 |
/// \brief |
|
| 707 |
/// \brief Give back the successor of an arc |
|
| 703 | 708 |
/// |
| 704 |
/// |
|
| 709 |
/// This function gives back the successor of an arc. It makes |
|
| 705 | 710 |
/// possible to query the cyclic order of the outgoing arcs from |
| 706 | 711 |
/// a node. |
| 707 | 712 |
Arc next(const Arc& arc) const {
|
| 708 | 713 |
return _embedding[arc]; |
| 709 | 714 |
} |
| 710 | 715 |
|
| 711 |
/// \brief |
|
| 716 |
/// \brief Give back the calculated embedding map |
|
| 712 | 717 |
/// |
| 713 |
/// The returned map contains the successor of each arc in the |
|
| 714 |
/// graph. |
|
| 718 |
/// This function gives back the calculated embedding map, which |
|
| 719 |
/// contains the successor of each arc in the cyclic order of the |
|
| 720 |
/// outgoing arcs of its source node. |
|
| 715 | 721 |
const EmbeddingMap& embeddingMap() const {
|
| 716 | 722 |
return _embedding; |
| 717 | 723 |
} |
| 718 | 724 |
|
| 719 |
/// \brief Gives back true if the undirected arc is in the |
|
| 720 |
/// kuratowski subdivision |
|
| 725 |
/// \brief Give back \c true if the given edge is in the Kuratowski |
|
| 726 |
/// subdivision |
|
| 721 | 727 |
/// |
| 722 |
/// Gives back true if the undirected arc is in the kuratowski |
|
| 723 |
/// subdivision |
|
| 724 |
/// \note The \c run() had to be called with true value. |
|
| 725 |
bool kuratowski(const Edge& edge) {
|
|
| 728 |
/// This function gives back \c true if the given edge is in the found |
|
| 729 |
/// Kuratowski subdivision. |
|
| 730 |
/// \pre The \c run() function must be called with \c true parameter |
|
| 731 |
/// before using this function. |
|
| 732 |
bool kuratowski(const Edge& edge) const {
|
|
| 726 | 733 |
return _kuratowski[edge]; |
| 727 | 734 |
} |
| 728 | 735 |
|
| 729 | 736 |
private: |
| 730 | 737 |
|
| 731 | 738 |
void createChildLists(const TreeMap& tree_map, const OrderMap& order_map, |
| 732 | 739 |
const LowMap& low_map, ChildLists& child_lists) {
|
| 733 | 740 |
|
| 734 | 741 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 735 | 742 |
Node source = n; |
| 736 | 743 |
|
| 737 | 744 |
std::vector<Node> targets; |
| 738 | 745 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
| 739 | 746 |
Node target = _graph.target(e); |
| 740 | 747 |
|
| 741 | 748 |
if (order_map[source] < order_map[target] && tree_map[e]) {
|
| 742 | 749 |
targets.push_back(target); |
| 743 | 750 |
} |
| 744 | 751 |
} |
| 745 | 752 |
|
| 746 | 753 |
if (targets.size() == 0) {
|
| 747 | 754 |
child_lists[source].first = INVALID; |
| 748 | 755 |
} else if (targets.size() == 1) {
|
| 749 | 756 |
child_lists[source].first = targets[0]; |
| 750 | 757 |
child_lists[targets[0]].prev = INVALID; |
| 751 | 758 |
child_lists[targets[0]].next = INVALID; |
| 752 | 759 |
} else {
|
| 753 | 760 |
radixSort(targets.begin(), targets.end(), mapToFunctor(low_map)); |
| 754 | 761 |
for (int i = 1; i < int(targets.size()); ++i) {
|
| 755 | 762 |
child_lists[targets[i]].prev = targets[i - 1]; |
| 756 | 763 |
child_lists[targets[i - 1]].next = targets[i]; |
| 757 | 764 |
} |
| 758 | 765 |
child_lists[targets.back()].next = INVALID; |
| 759 | 766 |
child_lists[targets.front()].prev = INVALID; |
| 760 | 767 |
child_lists[source].first = targets.front(); |
| 761 | 768 |
} |
| 762 | 769 |
} |
| 763 | 770 |
} |
| 764 | 771 |
|
| 765 | 772 |
void walkUp(const Node& node, Node root, int rorder, |
| 766 | 773 |
const PredMap& pred_map, const LowMap& low_map, |
| 767 | 774 |
const OrderMap& order_map, const OrderList& order_list, |
| 768 | 775 |
NodeData& node_data, MergeRoots& merge_roots) {
|
| 769 | 776 |
|
| 770 | 777 |
int na, nb; |
| 771 | 778 |
bool da, db; |
| 772 | 779 |
|
| 773 | 780 |
na = nb = order_map[node]; |
| 774 | 781 |
da = true; db = false; |
| 775 | 782 |
|
| 776 | 783 |
while (true) {
|
| 777 | 784 |
|
| 778 | 785 |
if (node_data[na].visited == rorder) break; |
| 779 | 786 |
if (node_data[nb].visited == rorder) break; |
| 780 | 787 |
|
| 781 | 788 |
node_data[na].visited = rorder; |
| 782 | 789 |
node_data[nb].visited = rorder; |
| 783 | 790 |
|
| 784 | 791 |
int rn = -1; |
| 785 | 792 |
|
| 786 | 793 |
if (na >= int(order_list.size())) {
|
| 787 | 794 |
rn = na; |
| 788 | 795 |
} else if (nb >= int(order_list.size())) {
|
| 789 | 796 |
rn = nb; |
| 790 | 797 |
} |
| 791 | 798 |
|
| 792 | 799 |
if (rn == -1) {
|
| 793 | 800 |
int nn; |
| 794 | 801 |
|
| 795 | 802 |
nn = da ? node_data[na].prev : node_data[na].next; |
| 796 | 803 |
da = node_data[nn].prev != na; |
| 797 | 804 |
na = nn; |
| 798 | 805 |
|
| 799 | 806 |
nn = db ? node_data[nb].prev : node_data[nb].next; |
| 800 | 807 |
db = node_data[nn].prev != nb; |
| 801 | 808 |
nb = nn; |
| 802 | 809 |
|
| 803 | 810 |
} else {
|
| 804 | 811 |
|
| 805 | 812 |
Node rep = order_list[rn - order_list.size()]; |
| 806 | 813 |
Node parent = _graph.source(pred_map[rep]); |
| 807 | 814 |
|
| 808 | 815 |
if (low_map[rep] < rorder) {
|
| 809 | 816 |
merge_roots[parent].push_back(rn); |
| 810 | 817 |
} else {
|
| 811 | 818 |
merge_roots[parent].push_front(rn); |
| 812 | 819 |
} |
| 813 | 820 |
|
| 814 | 821 |
if (parent != root) {
|
| 815 | 822 |
na = nb = order_map[parent]; |
| 816 | 823 |
da = true; db = false; |
| 817 | 824 |
} else {
|
| 818 | 825 |
break; |
| 819 | 826 |
} |
| 820 | 827 |
} |
| 821 | 828 |
} |
| ... | ... |
@@ -1966,215 +1973,218 @@ |
| 1966 | 1973 |
for (typename Graph::OutArcIt e(graph, s); e != INVALID; ++e) {
|
| 1967 | 1974 |
visited.set(graph.target(e), true); |
| 1968 | 1975 |
} |
| 1969 | 1976 |
|
| 1970 | 1977 |
typename Graph::Arc oppe = INVALID; |
| 1971 | 1978 |
|
| 1972 | 1979 |
e = embedding[graph.oppositeArc(mine)]; |
| 1973 | 1980 |
e = embedding[graph.oppositeArc(e)]; |
| 1974 | 1981 |
while (graph.target(e) != s) {
|
| 1975 | 1982 |
if (visited[graph.source(e)]) {
|
| 1976 | 1983 |
oppe = e; |
| 1977 | 1984 |
break; |
| 1978 | 1985 |
} |
| 1979 | 1986 |
e = embedding[graph.oppositeArc(e)]; |
| 1980 | 1987 |
} |
| 1981 | 1988 |
visited.setAll(false); |
| 1982 | 1989 |
|
| 1983 | 1990 |
if (oppe == INVALID) {
|
| 1984 | 1991 |
|
| 1985 | 1992 |
e = embedding[graph.oppositeArc(mine)]; |
| 1986 | 1993 |
typename Graph::Arc pn = mine, p = e; |
| 1987 | 1994 |
|
| 1988 | 1995 |
e = embedding[graph.oppositeArc(e)]; |
| 1989 | 1996 |
while (graph.target(e) != s) {
|
| 1990 | 1997 |
typename Graph::Arc n = |
| 1991 | 1998 |
graph.direct(graph.addEdge(s, graph.source(e)), true); |
| 1992 | 1999 |
|
| 1993 | 2000 |
embedding[n] = pn; |
| 1994 | 2001 |
embedding[graph.oppositeArc(n)] = e; |
| 1995 | 2002 |
embedding[graph.oppositeArc(p)] = graph.oppositeArc(n); |
| 1996 | 2003 |
|
| 1997 | 2004 |
pn = n; |
| 1998 | 2005 |
|
| 1999 | 2006 |
p = e; |
| 2000 | 2007 |
e = embedding[graph.oppositeArc(e)]; |
| 2001 | 2008 |
} |
| 2002 | 2009 |
|
| 2003 | 2010 |
embedding[graph.oppositeArc(e)] = pn; |
| 2004 | 2011 |
|
| 2005 | 2012 |
} else {
|
| 2006 | 2013 |
|
| 2007 | 2014 |
mine = embedding[graph.oppositeArc(mine)]; |
| 2008 | 2015 |
s = graph.source(mine); |
| 2009 | 2016 |
oppe = embedding[graph.oppositeArc(oppe)]; |
| 2010 | 2017 |
typename Graph::Node t = graph.source(oppe); |
| 2011 | 2018 |
|
| 2012 | 2019 |
typename Graph::Arc ce = graph.direct(graph.addEdge(s, t), true); |
| 2013 | 2020 |
embedding[ce] = mine; |
| 2014 | 2021 |
embedding[graph.oppositeArc(ce)] = oppe; |
| 2015 | 2022 |
|
| 2016 | 2023 |
typename Graph::Arc pn = ce, p = oppe; |
| 2017 | 2024 |
e = embedding[graph.oppositeArc(oppe)]; |
| 2018 | 2025 |
while (graph.target(e) != s) {
|
| 2019 | 2026 |
typename Graph::Arc n = |
| 2020 | 2027 |
graph.direct(graph.addEdge(s, graph.source(e)), true); |
| 2021 | 2028 |
|
| 2022 | 2029 |
embedding[n] = pn; |
| 2023 | 2030 |
embedding[graph.oppositeArc(n)] = e; |
| 2024 | 2031 |
embedding[graph.oppositeArc(p)] = graph.oppositeArc(n); |
| 2025 | 2032 |
|
| 2026 | 2033 |
pn = n; |
| 2027 | 2034 |
|
| 2028 | 2035 |
p = e; |
| 2029 | 2036 |
e = embedding[graph.oppositeArc(e)]; |
| 2030 | 2037 |
|
| 2031 | 2038 |
} |
| 2032 | 2039 |
embedding[graph.oppositeArc(e)] = pn; |
| 2033 | 2040 |
|
| 2034 | 2041 |
pn = graph.oppositeArc(ce), p = mine; |
| 2035 | 2042 |
e = embedding[graph.oppositeArc(mine)]; |
| 2036 | 2043 |
while (graph.target(e) != t) {
|
| 2037 | 2044 |
typename Graph::Arc n = |
| 2038 | 2045 |
graph.direct(graph.addEdge(t, graph.source(e)), true); |
| 2039 | 2046 |
|
| 2040 | 2047 |
embedding[n] = pn; |
| 2041 | 2048 |
embedding[graph.oppositeArc(n)] = e; |
| 2042 | 2049 |
embedding[graph.oppositeArc(p)] = graph.oppositeArc(n); |
| 2043 | 2050 |
|
| 2044 | 2051 |
pn = n; |
| 2045 | 2052 |
|
| 2046 | 2053 |
p = e; |
| 2047 | 2054 |
e = embedding[graph.oppositeArc(e)]; |
| 2048 | 2055 |
|
| 2049 | 2056 |
} |
| 2050 | 2057 |
embedding[graph.oppositeArc(e)] = pn; |
| 2051 | 2058 |
} |
| 2052 | 2059 |
} |
| 2053 | 2060 |
} |
| 2054 | 2061 |
|
| 2055 | 2062 |
} |
| 2056 | 2063 |
|
| 2057 | 2064 |
/// \ingroup planar |
| 2058 | 2065 |
/// |
| 2059 | 2066 |
/// \brief Schnyder's planar drawing algorithm |
| 2060 | 2067 |
/// |
| 2061 | 2068 |
/// The planar drawing algorithm calculates positions for the nodes |
| 2062 |
/// in the plane which coordinates satisfy that if the arcs are |
|
| 2063 |
/// represented with straight lines then they will not intersect |
|
| 2069 |
/// in the plane. These coordinates satisfy that if the edges are |
|
| 2070 |
/// represented with straight lines, then they will not intersect |
|
| 2064 | 2071 |
/// each other. |
| 2065 | 2072 |
/// |
| 2066 |
/// Scnyder's algorithm embeds the graph on \c (n-2,n-2) size grid, |
|
| 2067 |
/// i.e. each node will be located in the \c [0,n-2]x[0,n-2] square. |
|
| 2073 |
/// Scnyder's algorithm embeds the graph on an \c (n-2)x(n-2) size grid, |
|
| 2074 |
/// i.e. each node will be located in the \c [0..n-2]x[0..n-2] square. |
|
| 2068 | 2075 |
/// The time complexity of the algorithm is O(n). |
| 2076 |
/// |
|
| 2077 |
/// \see PlanarEmbedding |
|
| 2069 | 2078 |
template <typename Graph> |
| 2070 | 2079 |
class PlanarDrawing {
|
| 2071 | 2080 |
public: |
| 2072 | 2081 |
|
| 2073 | 2082 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 2074 | 2083 |
|
| 2075 |
/// \brief The point type for |
|
| 2084 |
/// \brief The point type for storing coordinates |
|
| 2076 | 2085 |
typedef dim2::Point<int> Point; |
| 2077 |
/// \brief The map type for |
|
| 2086 |
/// \brief The map type for storing the coordinates of the nodes |
|
| 2078 | 2087 |
typedef typename Graph::template NodeMap<Point> PointMap; |
| 2079 | 2088 |
|
| 2080 | 2089 |
|
| 2081 | 2090 |
/// \brief Constructor |
| 2082 | 2091 |
/// |
| 2083 | 2092 |
/// Constructor |
| 2084 |
/// \pre The graph |
|
| 2093 |
/// \pre The graph must be simple, i.e. it should not |
|
| 2094 |
/// contain parallel or loop arcs. |
|
| 2085 | 2095 |
PlanarDrawing(const Graph& graph) |
| 2086 | 2096 |
: _graph(graph), _point_map(graph) {}
|
| 2087 | 2097 |
|
| 2088 | 2098 |
private: |
| 2089 | 2099 |
|
| 2090 | 2100 |
template <typename AuxGraph, typename AuxEmbeddingMap> |
| 2091 | 2101 |
void drawing(const AuxGraph& graph, |
| 2092 | 2102 |
const AuxEmbeddingMap& next, |
| 2093 | 2103 |
PointMap& point_map) {
|
| 2094 | 2104 |
TEMPLATE_GRAPH_TYPEDEFS(AuxGraph); |
| 2095 | 2105 |
|
| 2096 | 2106 |
typename AuxGraph::template ArcMap<Arc> prev(graph); |
| 2097 | 2107 |
|
| 2098 | 2108 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
| 2099 | 2109 |
Arc e = OutArcIt(graph, n); |
| 2100 | 2110 |
|
| 2101 | 2111 |
Arc p = e, l = e; |
| 2102 | 2112 |
|
| 2103 | 2113 |
e = next[e]; |
| 2104 | 2114 |
while (e != l) {
|
| 2105 | 2115 |
prev[e] = p; |
| 2106 | 2116 |
p = e; |
| 2107 | 2117 |
e = next[e]; |
| 2108 | 2118 |
} |
| 2109 | 2119 |
prev[e] = p; |
| 2110 | 2120 |
} |
| 2111 | 2121 |
|
| 2112 | 2122 |
Node anode, bnode, cnode; |
| 2113 | 2123 |
|
| 2114 | 2124 |
{
|
| 2115 | 2125 |
Arc e = ArcIt(graph); |
| 2116 | 2126 |
anode = graph.source(e); |
| 2117 | 2127 |
bnode = graph.target(e); |
| 2118 | 2128 |
cnode = graph.target(next[graph.oppositeArc(e)]); |
| 2119 | 2129 |
} |
| 2120 | 2130 |
|
| 2121 | 2131 |
IterableBoolMap<AuxGraph, Node> proper(graph, false); |
| 2122 | 2132 |
typename AuxGraph::template NodeMap<int> conn(graph, -1); |
| 2123 | 2133 |
|
| 2124 | 2134 |
conn[anode] = conn[bnode] = -2; |
| 2125 | 2135 |
{
|
| 2126 | 2136 |
for (OutArcIt e(graph, anode); e != INVALID; ++e) {
|
| 2127 | 2137 |
Node m = graph.target(e); |
| 2128 | 2138 |
if (conn[m] == -1) {
|
| 2129 | 2139 |
conn[m] = 1; |
| 2130 | 2140 |
} |
| 2131 | 2141 |
} |
| 2132 | 2142 |
conn[cnode] = 2; |
| 2133 | 2143 |
|
| 2134 | 2144 |
for (OutArcIt e(graph, bnode); e != INVALID; ++e) {
|
| 2135 | 2145 |
Node m = graph.target(e); |
| 2136 | 2146 |
if (conn[m] == -1) {
|
| 2137 | 2147 |
conn[m] = 1; |
| 2138 | 2148 |
} else if (conn[m] != -2) {
|
| 2139 | 2149 |
conn[m] += 1; |
| 2140 | 2150 |
Arc pe = graph.oppositeArc(e); |
| 2141 | 2151 |
if (conn[graph.target(next[pe])] == -2) {
|
| 2142 | 2152 |
conn[m] -= 1; |
| 2143 | 2153 |
} |
| 2144 | 2154 |
if (conn[graph.target(prev[pe])] == -2) {
|
| 2145 | 2155 |
conn[m] -= 1; |
| 2146 | 2156 |
} |
| 2147 | 2157 |
|
| 2148 | 2158 |
proper.set(m, conn[m] == 1); |
| 2149 | 2159 |
} |
| 2150 | 2160 |
} |
| 2151 | 2161 |
} |
| 2152 | 2162 |
|
| 2153 | 2163 |
|
| 2154 | 2164 |
typename AuxGraph::template ArcMap<int> angle(graph, -1); |
| 2155 | 2165 |
|
| 2156 | 2166 |
while (proper.trueNum() != 0) {
|
| 2157 | 2167 |
Node n = typename IterableBoolMap<AuxGraph, Node>::TrueIt(proper); |
| 2158 | 2168 |
proper.set(n, false); |
| 2159 | 2169 |
conn[n] = -2; |
| 2160 | 2170 |
|
| 2161 | 2171 |
for (OutArcIt e(graph, n); e != INVALID; ++e) {
|
| 2162 | 2172 |
Node m = graph.target(e); |
| 2163 | 2173 |
if (conn[m] == -1) {
|
| 2164 | 2174 |
conn[m] = 1; |
| 2165 | 2175 |
} else if (conn[m] != -2) {
|
| 2166 | 2176 |
conn[m] += 1; |
| 2167 | 2177 |
Arc pe = graph.oppositeArc(e); |
| 2168 | 2178 |
if (conn[graph.target(next[pe])] == -2) {
|
| 2169 | 2179 |
conn[m] -= 1; |
| 2170 | 2180 |
} |
| 2171 | 2181 |
if (conn[graph.target(prev[pe])] == -2) {
|
| 2172 | 2182 |
conn[m] -= 1; |
| 2173 | 2183 |
} |
| 2174 | 2184 |
|
| 2175 | 2185 |
proper.set(m, conn[m] == 1); |
| 2176 | 2186 |
} |
| 2177 | 2187 |
} |
| 2178 | 2188 |
|
| 2179 | 2189 |
{
|
| 2180 | 2190 |
Arc e = OutArcIt(graph, n); |
| ... | ... |
@@ -2273,465 +2283,473 @@ |
| 2273 | 2283 |
processed[m] = true; |
| 2274 | 2284 |
m = bpred[m]; |
| 2275 | 2285 |
} |
| 2276 | 2286 |
while (!st.empty()) {
|
| 2277 | 2287 |
border.push_back(st.back()); |
| 2278 | 2288 |
st.pop_back(); |
| 2279 | 2289 |
} |
| 2280 | 2290 |
} |
| 2281 | 2291 |
} |
| 2282 | 2292 |
} |
| 2283 | 2293 |
|
| 2284 | 2294 |
{
|
| 2285 | 2295 |
typename AuxGraph::template NodeMap<bool> processed(graph, false); |
| 2286 | 2296 |
std::vector<Node> st; |
| 2287 | 2297 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
| 2288 | 2298 |
if (!processed[n] && n != anode && n != bnode) {
|
| 2289 | 2299 |
st.push_back(n); |
| 2290 | 2300 |
processed[n] = true; |
| 2291 | 2301 |
Node m = cpred[n]; |
| 2292 | 2302 |
while (m != INVALID && !processed[m]) {
|
| 2293 | 2303 |
st.push_back(m); |
| 2294 | 2304 |
processed[m] = true; |
| 2295 | 2305 |
m = cpred[m]; |
| 2296 | 2306 |
} |
| 2297 | 2307 |
while (!st.empty()) {
|
| 2298 | 2308 |
corder.push_back(st.back()); |
| 2299 | 2309 |
st.pop_back(); |
| 2300 | 2310 |
} |
| 2301 | 2311 |
} |
| 2302 | 2312 |
} |
| 2303 | 2313 |
} |
| 2304 | 2314 |
|
| 2305 | 2315 |
typename AuxGraph::template NodeMap<int> atree(graph, 0); |
| 2306 | 2316 |
for (int i = aorder.size() - 1; i >= 0; --i) {
|
| 2307 | 2317 |
Node n = aorder[i]; |
| 2308 | 2318 |
atree[n] = 1; |
| 2309 | 2319 |
for (OutArcIt e(graph, n); e != INVALID; ++e) {
|
| 2310 | 2320 |
if (apred[graph.target(e)] == n) {
|
| 2311 | 2321 |
atree[n] += atree[graph.target(e)]; |
| 2312 | 2322 |
} |
| 2313 | 2323 |
} |
| 2314 | 2324 |
} |
| 2315 | 2325 |
|
| 2316 | 2326 |
typename AuxGraph::template NodeMap<int> btree(graph, 0); |
| 2317 | 2327 |
for (int i = border.size() - 1; i >= 0; --i) {
|
| 2318 | 2328 |
Node n = border[i]; |
| 2319 | 2329 |
btree[n] = 1; |
| 2320 | 2330 |
for (OutArcIt e(graph, n); e != INVALID; ++e) {
|
| 2321 | 2331 |
if (bpred[graph.target(e)] == n) {
|
| 2322 | 2332 |
btree[n] += btree[graph.target(e)]; |
| 2323 | 2333 |
} |
| 2324 | 2334 |
} |
| 2325 | 2335 |
} |
| 2326 | 2336 |
|
| 2327 | 2337 |
typename AuxGraph::template NodeMap<int> apath(graph, 0); |
| 2328 | 2338 |
apath[bnode] = apath[cnode] = 1; |
| 2329 | 2339 |
typename AuxGraph::template NodeMap<int> apath_btree(graph, 0); |
| 2330 | 2340 |
apath_btree[bnode] = btree[bnode]; |
| 2331 | 2341 |
for (int i = 1; i < int(aorder.size()); ++i) {
|
| 2332 | 2342 |
Node n = aorder[i]; |
| 2333 | 2343 |
apath[n] = apath[apred[n]] + 1; |
| 2334 | 2344 |
apath_btree[n] = btree[n] + apath_btree[apred[n]]; |
| 2335 | 2345 |
} |
| 2336 | 2346 |
|
| 2337 | 2347 |
typename AuxGraph::template NodeMap<int> bpath_atree(graph, 0); |
| 2338 | 2348 |
bpath_atree[anode] = atree[anode]; |
| 2339 | 2349 |
for (int i = 1; i < int(border.size()); ++i) {
|
| 2340 | 2350 |
Node n = border[i]; |
| 2341 | 2351 |
bpath_atree[n] = atree[n] + bpath_atree[bpred[n]]; |
| 2342 | 2352 |
} |
| 2343 | 2353 |
|
| 2344 | 2354 |
typename AuxGraph::template NodeMap<int> cpath(graph, 0); |
| 2345 | 2355 |
cpath[anode] = cpath[bnode] = 1; |
| 2346 | 2356 |
typename AuxGraph::template NodeMap<int> cpath_atree(graph, 0); |
| 2347 | 2357 |
cpath_atree[anode] = atree[anode]; |
| 2348 | 2358 |
typename AuxGraph::template NodeMap<int> cpath_btree(graph, 0); |
| 2349 | 2359 |
cpath_btree[bnode] = btree[bnode]; |
| 2350 | 2360 |
for (int i = 1; i < int(corder.size()); ++i) {
|
| 2351 | 2361 |
Node n = corder[i]; |
| 2352 | 2362 |
cpath[n] = cpath[cpred[n]] + 1; |
| 2353 | 2363 |
cpath_atree[n] = atree[n] + cpath_atree[cpred[n]]; |
| 2354 | 2364 |
cpath_btree[n] = btree[n] + cpath_btree[cpred[n]]; |
| 2355 | 2365 |
} |
| 2356 | 2366 |
|
| 2357 | 2367 |
typename AuxGraph::template NodeMap<int> third(graph); |
| 2358 | 2368 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
| 2359 | 2369 |
point_map[n].x = |
| 2360 | 2370 |
bpath_atree[n] + cpath_atree[n] - atree[n] - cpath[n] + 1; |
| 2361 | 2371 |
point_map[n].y = |
| 2362 | 2372 |
cpath_btree[n] + apath_btree[n] - btree[n] - apath[n] + 1; |
| 2363 | 2373 |
} |
| 2364 | 2374 |
|
| 2365 | 2375 |
} |
| 2366 | 2376 |
|
| 2367 | 2377 |
public: |
| 2368 | 2378 |
|
| 2369 |
/// \brief |
|
| 2379 |
/// \brief Calculate the node positions |
|
| 2370 | 2380 |
/// |
| 2371 |
/// This function calculates the node positions. |
|
| 2372 |
/// \return %True if the graph is planar. |
|
| 2381 |
/// This function calculates the node positions on the plane. |
|
| 2382 |
/// \return \c true if the graph is planar. |
|
| 2373 | 2383 |
bool run() {
|
| 2374 | 2384 |
PlanarEmbedding<Graph> pe(_graph); |
| 2375 | 2385 |
if (!pe.run()) return false; |
| 2376 | 2386 |
|
| 2377 | 2387 |
run(pe); |
| 2378 | 2388 |
return true; |
| 2379 | 2389 |
} |
| 2380 | 2390 |
|
| 2381 |
/// \brief |
|
| 2391 |
/// \brief Calculate the node positions according to a |
|
| 2382 | 2392 |
/// combinatorical embedding |
| 2383 | 2393 |
/// |
| 2384 |
/// This function calculates the node locations. The \c embedding |
|
| 2385 |
/// parameter should contain a valid combinatorical embedding, i.e. |
|
| 2386 |
/// |
|
| 2394 |
/// This function calculates the node positions on the plane. |
|
| 2395 |
/// The given \c embedding map should contain a valid combinatorical |
|
| 2396 |
/// embedding, i.e. a valid cyclic order of the arcs. |
|
| 2397 |
/// It can be computed using PlanarEmbedding. |
|
| 2387 | 2398 |
template <typename EmbeddingMap> |
| 2388 | 2399 |
void run(const EmbeddingMap& embedding) {
|
| 2389 | 2400 |
typedef SmartEdgeSet<Graph> AuxGraph; |
| 2390 | 2401 |
|
| 2391 | 2402 |
if (3 * countNodes(_graph) - 6 == countEdges(_graph)) {
|
| 2392 | 2403 |
drawing(_graph, embedding, _point_map); |
| 2393 | 2404 |
return; |
| 2394 | 2405 |
} |
| 2395 | 2406 |
|
| 2396 | 2407 |
AuxGraph aux_graph(_graph); |
| 2397 | 2408 |
typename AuxGraph::template ArcMap<typename AuxGraph::Arc> |
| 2398 | 2409 |
aux_embedding(aux_graph); |
| 2399 | 2410 |
|
| 2400 | 2411 |
{
|
| 2401 | 2412 |
|
| 2402 | 2413 |
typename Graph::template EdgeMap<typename AuxGraph::Edge> |
| 2403 | 2414 |
ref(_graph); |
| 2404 | 2415 |
|
| 2405 | 2416 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 2406 | 2417 |
ref[e] = aux_graph.addEdge(_graph.u(e), _graph.v(e)); |
| 2407 | 2418 |
} |
| 2408 | 2419 |
|
| 2409 | 2420 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 2410 | 2421 |
Arc ee = embedding[_graph.direct(e, true)]; |
| 2411 | 2422 |
aux_embedding[aux_graph.direct(ref[e], true)] = |
| 2412 | 2423 |
aux_graph.direct(ref[ee], _graph.direction(ee)); |
| 2413 | 2424 |
ee = embedding[_graph.direct(e, false)]; |
| 2414 | 2425 |
aux_embedding[aux_graph.direct(ref[e], false)] = |
| 2415 | 2426 |
aux_graph.direct(ref[ee], _graph.direction(ee)); |
| 2416 | 2427 |
} |
| 2417 | 2428 |
} |
| 2418 | 2429 |
_planarity_bits::makeConnected(aux_graph, aux_embedding); |
| 2419 | 2430 |
_planarity_bits::makeBiNodeConnected(aux_graph, aux_embedding); |
| 2420 | 2431 |
_planarity_bits::makeMaxPlanar(aux_graph, aux_embedding); |
| 2421 | 2432 |
drawing(aux_graph, aux_embedding, _point_map); |
| 2422 | 2433 |
} |
| 2423 | 2434 |
|
| 2424 | 2435 |
/// \brief The coordinate of the given node |
| 2425 | 2436 |
/// |
| 2426 |
/// |
|
| 2437 |
/// This function returns the coordinate of the given node. |
|
| 2427 | 2438 |
Point operator[](const Node& node) const {
|
| 2428 | 2439 |
return _point_map[node]; |
| 2429 | 2440 |
} |
| 2430 | 2441 |
|
| 2431 |
/// \brief |
|
| 2442 |
/// \brief Return the grid embedding in a node map |
|
| 2432 | 2443 |
/// |
| 2433 |
/// |
|
| 2444 |
/// This function returns the grid embedding in a node map of |
|
| 2445 |
/// \c dim2::Point<int> coordinates. |
|
| 2434 | 2446 |
const PointMap& coords() const {
|
| 2435 | 2447 |
return _point_map; |
| 2436 | 2448 |
} |
| 2437 | 2449 |
|
| 2438 | 2450 |
private: |
| 2439 | 2451 |
|
| 2440 | 2452 |
const Graph& _graph; |
| 2441 | 2453 |
PointMap _point_map; |
| 2442 | 2454 |
|
| 2443 | 2455 |
}; |
| 2444 | 2456 |
|
| 2445 | 2457 |
namespace _planarity_bits {
|
| 2446 | 2458 |
|
| 2447 | 2459 |
template <typename ColorMap> |
| 2448 | 2460 |
class KempeFilter {
|
| 2449 | 2461 |
public: |
| 2450 | 2462 |
typedef typename ColorMap::Key Key; |
| 2451 | 2463 |
typedef bool Value; |
| 2452 | 2464 |
|
| 2453 | 2465 |
KempeFilter(const ColorMap& color_map, |
| 2454 | 2466 |
const typename ColorMap::Value& first, |
| 2455 | 2467 |
const typename ColorMap::Value& second) |
| 2456 | 2468 |
: _color_map(color_map), _first(first), _second(second) {}
|
| 2457 | 2469 |
|
| 2458 | 2470 |
Value operator[](const Key& key) const {
|
| 2459 | 2471 |
return _color_map[key] == _first || _color_map[key] == _second; |
| 2460 | 2472 |
} |
| 2461 | 2473 |
|
| 2462 | 2474 |
private: |
| 2463 | 2475 |
const ColorMap& _color_map; |
| 2464 | 2476 |
typename ColorMap::Value _first, _second; |
| 2465 | 2477 |
}; |
| 2466 | 2478 |
} |
| 2467 | 2479 |
|
| 2468 | 2480 |
/// \ingroup planar |
| 2469 | 2481 |
/// |
| 2470 | 2482 |
/// \brief Coloring planar graphs |
| 2471 | 2483 |
/// |
| 2472 | 2484 |
/// The graph coloring problem is the coloring of the graph nodes |
| 2473 |
/// that there are not adjacent nodes with the same color. The |
|
| 2474 |
/// planar graphs can be always colored with four colors, it is |
|
| 2475 |
/// |
|
| 2485 |
/// so that there are no adjacent nodes with the same color. The |
|
| 2486 |
/// planar graphs can always be colored with four colors, which is |
|
| 2487 |
/// proved by Appel and Haken. Their proofs provide a quadratic |
|
| 2476 | 2488 |
/// time algorithm for four coloring, but it could not be used to |
| 2477 |
/// implement efficient algorithm. The five and six coloring can be |
|
| 2478 |
/// made in linear time, but in this class the five coloring has |
|
| 2489 |
/// implement an efficient algorithm. The five and six coloring can be |
|
| 2490 |
/// made in linear time, but in this class, the five coloring has |
|
| 2479 | 2491 |
/// quadratic worst case time complexity. The two coloring (if |
| 2480 | 2492 |
/// possible) is solvable with a graph search algorithm and it is |
| 2481 | 2493 |
/// implemented in \ref bipartitePartitions() function in LEMON. To |
| 2482 |
/// decide whether the planar graph is three colorable is |
|
| 2483 |
/// NP-complete. |
|
| 2494 |
/// decide whether a planar graph is three colorable is NP-complete. |
|
| 2484 | 2495 |
/// |
| 2485 | 2496 |
/// This class contains member functions for calculate colorings |
| 2486 | 2497 |
/// with five and six colors. The six coloring algorithm is a simple |
| 2487 | 2498 |
/// greedy coloring on the backward minimum outgoing order of nodes. |
| 2488 |
/// This order can be computed as in each phase the node with least |
|
| 2489 |
/// outgoing arcs to unprocessed nodes is chosen. This order |
|
| 2499 |
/// This order can be computed by selecting the node with least |
|
| 2500 |
/// outgoing arcs to unprocessed nodes in each phase. This order |
|
| 2490 | 2501 |
/// guarantees that when a node is chosen for coloring it has at |
| 2491 | 2502 |
/// most five already colored adjacents. The five coloring algorithm |
| 2492 | 2503 |
/// use the same method, but if the greedy approach fails to color |
| 2493 | 2504 |
/// with five colors, i.e. the node has five already different |
| 2494 | 2505 |
/// colored neighbours, it swaps the colors in one of the connected |
| 2495 | 2506 |
/// two colored sets with the Kempe recoloring method. |
| 2496 | 2507 |
template <typename Graph> |
| 2497 | 2508 |
class PlanarColoring {
|
| 2498 | 2509 |
public: |
| 2499 | 2510 |
|
| 2500 | 2511 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 2501 | 2512 |
|
| 2502 |
/// \brief The map type for |
|
| 2513 |
/// \brief The map type for storing color indices |
|
| 2503 | 2514 |
typedef typename Graph::template NodeMap<int> IndexMap; |
| 2504 |
/// \brief The map type for |
|
| 2515 |
/// \brief The map type for storing colors |
|
| 2516 |
/// |
|
| 2517 |
/// The map type for storing colors. |
|
| 2518 |
/// \see Palette, Color |
|
| 2505 | 2519 |
typedef ComposeMap<Palette, IndexMap> ColorMap; |
| 2506 | 2520 |
|
| 2507 | 2521 |
/// \brief Constructor |
| 2508 | 2522 |
/// |
| 2509 |
/// Constructor |
|
| 2510 |
/// \pre The graph should be simple, i.e. loop and parallel arc free. |
|
| 2523 |
/// Constructor. |
|
| 2524 |
/// \pre The graph must be simple, i.e. it should not |
|
| 2525 |
/// contain parallel or loop arcs. |
|
| 2511 | 2526 |
PlanarColoring(const Graph& graph) |
| 2512 | 2527 |
: _graph(graph), _color_map(graph), _palette(0) {
|
| 2513 | 2528 |
_palette.add(Color(1,0,0)); |
| 2514 | 2529 |
_palette.add(Color(0,1,0)); |
| 2515 | 2530 |
_palette.add(Color(0,0,1)); |
| 2516 | 2531 |
_palette.add(Color(1,1,0)); |
| 2517 | 2532 |
_palette.add(Color(1,0,1)); |
| 2518 | 2533 |
_palette.add(Color(0,1,1)); |
| 2519 | 2534 |
} |
| 2520 | 2535 |
|
| 2521 |
/// \brief |
|
| 2536 |
/// \brief Return the node map of color indices |
|
| 2522 | 2537 |
/// |
| 2523 |
/// Returns the \e NodeMap of color indexes. The values are in the |
|
| 2524 |
/// range \c [0..4] or \c [0..5] according to the coloring method. |
|
| 2538 |
/// This function returns the node map of color indices. The values are |
|
| 2539 |
/// in the range \c [0..4] or \c [0..5] according to the coloring method. |
|
| 2525 | 2540 |
IndexMap colorIndexMap() const {
|
| 2526 | 2541 |
return _color_map; |
| 2527 | 2542 |
} |
| 2528 | 2543 |
|
| 2529 |
/// \brief |
|
| 2544 |
/// \brief Return the node map of colors |
|
| 2530 | 2545 |
/// |
| 2531 |
/// Returns the \e NodeMap of colors. The values are five or six |
|
| 2532 |
/// distinct \ref lemon::Color "colors". |
|
| 2546 |
/// This function returns the node map of colors. The values are among |
|
| 2547 |
/// five or six distinct \ref lemon::Color "colors". |
|
| 2533 | 2548 |
ColorMap colorMap() const {
|
| 2534 | 2549 |
return composeMap(_palette, _color_map); |
| 2535 | 2550 |
} |
| 2536 | 2551 |
|
| 2537 |
/// \brief |
|
| 2552 |
/// \brief Return the color index of the node |
|
| 2538 | 2553 |
/// |
| 2539 |
/// Returns the color index of the node. The values are in the |
|
| 2540 |
/// range \c [0..4] or \c [0..5] according to the coloring method. |
|
| 2554 |
/// This function returns the color index of the given node. The value is |
|
| 2555 |
/// in the range \c [0..4] or \c [0..5] according to the coloring method. |
|
| 2541 | 2556 |
int colorIndex(const Node& node) const {
|
| 2542 | 2557 |
return _color_map[node]; |
| 2543 | 2558 |
} |
| 2544 | 2559 |
|
| 2545 |
/// \brief |
|
| 2560 |
/// \brief Return the color of the node |
|
| 2546 | 2561 |
/// |
| 2547 |
/// Returns the color of the node. The values are five or six |
|
| 2548 |
/// distinct \ref lemon::Color "colors". |
|
| 2562 |
/// This function returns the color of the given node. The value is among |
|
| 2563 |
/// five or six distinct \ref lemon::Color "colors". |
|
| 2549 | 2564 |
Color color(const Node& node) const {
|
| 2550 | 2565 |
return _palette[_color_map[node]]; |
| 2551 | 2566 |
} |
| 2552 | 2567 |
|
| 2553 | 2568 |
|
| 2554 |
/// \brief |
|
| 2569 |
/// \brief Calculate a coloring with at most six colors |
|
| 2555 | 2570 |
/// |
| 2556 | 2571 |
/// This function calculates a coloring with at most six colors. The time |
| 2557 | 2572 |
/// complexity of this variant is linear in the size of the graph. |
| 2558 |
/// \return %True when the algorithm could color the graph with six color. |
|
| 2559 |
/// If the algorithm fails, then the graph could not be planar. |
|
| 2560 |
/// \note This function can return true if the graph is not |
|
| 2561 |
/// planar but it can be colored with 6 colors. |
|
| 2573 |
/// \return \c true if the algorithm could color the graph with six colors. |
|
| 2574 |
/// If the algorithm fails, then the graph is not planar. |
|
| 2575 |
/// \note This function can return \c true if the graph is not |
|
| 2576 |
/// planar, but it can be colored with at most six colors. |
|
| 2562 | 2577 |
bool runSixColoring() {
|
| 2563 | 2578 |
|
| 2564 | 2579 |
typename Graph::template NodeMap<int> heap_index(_graph, -1); |
| 2565 | 2580 |
BucketHeap<typename Graph::template NodeMap<int> > heap(heap_index); |
| 2566 | 2581 |
|
| 2567 | 2582 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 2568 | 2583 |
_color_map[n] = -2; |
| 2569 | 2584 |
heap.push(n, countOutArcs(_graph, n)); |
| 2570 | 2585 |
} |
| 2571 | 2586 |
|
| 2572 | 2587 |
std::vector<Node> order; |
| 2573 | 2588 |
|
| 2574 | 2589 |
while (!heap.empty()) {
|
| 2575 | 2590 |
Node n = heap.top(); |
| 2576 | 2591 |
heap.pop(); |
| 2577 | 2592 |
_color_map[n] = -1; |
| 2578 | 2593 |
order.push_back(n); |
| 2579 | 2594 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
| 2580 | 2595 |
Node t = _graph.runningNode(e); |
| 2581 | 2596 |
if (_color_map[t] == -2) {
|
| 2582 | 2597 |
heap.decrease(t, heap[t] - 1); |
| 2583 | 2598 |
} |
| 2584 | 2599 |
} |
| 2585 | 2600 |
} |
| 2586 | 2601 |
|
| 2587 | 2602 |
for (int i = order.size() - 1; i >= 0; --i) {
|
| 2588 | 2603 |
std::vector<bool> forbidden(6, false); |
| 2589 | 2604 |
for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) {
|
| 2590 | 2605 |
Node t = _graph.runningNode(e); |
| 2591 | 2606 |
if (_color_map[t] != -1) {
|
| 2592 | 2607 |
forbidden[_color_map[t]] = true; |
| 2593 | 2608 |
} |
| 2594 | 2609 |
} |
| 2595 | 2610 |
for (int k = 0; k < 6; ++k) {
|
| 2596 | 2611 |
if (!forbidden[k]) {
|
| 2597 | 2612 |
_color_map[order[i]] = k; |
| 2598 | 2613 |
break; |
| 2599 | 2614 |
} |
| 2600 | 2615 |
} |
| 2601 | 2616 |
if (_color_map[order[i]] == -1) {
|
| 2602 | 2617 |
return false; |
| 2603 | 2618 |
} |
| 2604 | 2619 |
} |
| 2605 | 2620 |
return true; |
| 2606 | 2621 |
} |
| 2607 | 2622 |
|
| 2608 | 2623 |
private: |
| 2609 | 2624 |
|
| 2610 | 2625 |
bool recolor(const Node& u, const Node& v) {
|
| 2611 | 2626 |
int ucolor = _color_map[u]; |
| 2612 | 2627 |
int vcolor = _color_map[v]; |
| 2613 | 2628 |
typedef _planarity_bits::KempeFilter<IndexMap> KempeFilter; |
| 2614 | 2629 |
KempeFilter filter(_color_map, ucolor, vcolor); |
| 2615 | 2630 |
|
| 2616 | 2631 |
typedef FilterNodes<const Graph, const KempeFilter> KempeGraph; |
| 2617 | 2632 |
KempeGraph kempe_graph(_graph, filter); |
| 2618 | 2633 |
|
| 2619 | 2634 |
std::vector<Node> comp; |
| 2620 | 2635 |
Bfs<KempeGraph> bfs(kempe_graph); |
| 2621 | 2636 |
bfs.init(); |
| 2622 | 2637 |
bfs.addSource(u); |
| 2623 | 2638 |
while (!bfs.emptyQueue()) {
|
| 2624 | 2639 |
Node n = bfs.nextNode(); |
| 2625 | 2640 |
if (n == v) return false; |
| 2626 | 2641 |
comp.push_back(n); |
| 2627 | 2642 |
bfs.processNextNode(); |
| 2628 | 2643 |
} |
| 2629 | 2644 |
|
| 2630 | 2645 |
int scolor = ucolor + vcolor; |
| 2631 | 2646 |
for (int i = 0; i < static_cast<int>(comp.size()); ++i) {
|
| 2632 | 2647 |
_color_map[comp[i]] = scolor - _color_map[comp[i]]; |
| 2633 | 2648 |
} |
| 2634 | 2649 |
|
| 2635 | 2650 |
return true; |
| 2636 | 2651 |
} |
| 2637 | 2652 |
|
| 2638 | 2653 |
template <typename EmbeddingMap> |
| 2639 | 2654 |
void kempeRecoloring(const Node& node, const EmbeddingMap& embedding) {
|
| 2640 | 2655 |
std::vector<Node> nodes; |
| 2641 | 2656 |
nodes.reserve(4); |
| 2642 | 2657 |
|
| 2643 | 2658 |
for (Arc e = OutArcIt(_graph, node); e != INVALID; e = embedding[e]) {
|
| 2644 | 2659 |
Node t = _graph.target(e); |
| 2645 | 2660 |
if (_color_map[t] != -1) {
|
| 2646 | 2661 |
nodes.push_back(t); |
| 2647 | 2662 |
if (nodes.size() == 4) break; |
| 2648 | 2663 |
} |
| 2649 | 2664 |
} |
| 2650 | 2665 |
|
| 2651 | 2666 |
int color = _color_map[nodes[0]]; |
| 2652 | 2667 |
if (recolor(nodes[0], nodes[2])) {
|
| 2653 | 2668 |
_color_map[node] = color; |
| 2654 | 2669 |
} else {
|
| 2655 | 2670 |
color = _color_map[nodes[1]]; |
| 2656 | 2671 |
recolor(nodes[1], nodes[3]); |
| 2657 | 2672 |
_color_map[node] = color; |
| 2658 | 2673 |
} |
| 2659 | 2674 |
} |
| 2660 | 2675 |
|
| 2661 | 2676 |
public: |
| 2662 | 2677 |
|
| 2663 |
/// \brief |
|
| 2678 |
/// \brief Calculate a coloring with at most five colors |
|
| 2664 | 2679 |
/// |
| 2665 | 2680 |
/// This function calculates a coloring with at most five |
| 2666 | 2681 |
/// colors. The worst case time complexity of this variant is |
| 2667 | 2682 |
/// quadratic in the size of the graph. |
| 2683 |
/// \param embedding This map should contain a valid combinatorical |
|
| 2684 |
/// embedding, i.e. a valid cyclic order of the arcs. |
|
| 2685 |
/// It can be computed using PlanarEmbedding. |
|
| 2668 | 2686 |
template <typename EmbeddingMap> |
| 2669 | 2687 |
void runFiveColoring(const EmbeddingMap& embedding) {
|
| 2670 | 2688 |
|
| 2671 | 2689 |
typename Graph::template NodeMap<int> heap_index(_graph, -1); |
| 2672 | 2690 |
BucketHeap<typename Graph::template NodeMap<int> > heap(heap_index); |
| 2673 | 2691 |
|
| 2674 | 2692 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 2675 | 2693 |
_color_map[n] = -2; |
| 2676 | 2694 |
heap.push(n, countOutArcs(_graph, n)); |
| 2677 | 2695 |
} |
| 2678 | 2696 |
|
| 2679 | 2697 |
std::vector<Node> order; |
| 2680 | 2698 |
|
| 2681 | 2699 |
while (!heap.empty()) {
|
| 2682 | 2700 |
Node n = heap.top(); |
| 2683 | 2701 |
heap.pop(); |
| 2684 | 2702 |
_color_map[n] = -1; |
| 2685 | 2703 |
order.push_back(n); |
| 2686 | 2704 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
| 2687 | 2705 |
Node t = _graph.runningNode(e); |
| 2688 | 2706 |
if (_color_map[t] == -2) {
|
| 2689 | 2707 |
heap.decrease(t, heap[t] - 1); |
| 2690 | 2708 |
} |
| 2691 | 2709 |
} |
| 2692 | 2710 |
} |
| 2693 | 2711 |
|
| 2694 | 2712 |
for (int i = order.size() - 1; i >= 0; --i) {
|
| 2695 | 2713 |
std::vector<bool> forbidden(5, false); |
| 2696 | 2714 |
for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) {
|
| 2697 | 2715 |
Node t = _graph.runningNode(e); |
| 2698 | 2716 |
if (_color_map[t] != -1) {
|
| 2699 | 2717 |
forbidden[_color_map[t]] = true; |
| 2700 | 2718 |
} |
| 2701 | 2719 |
} |
| 2702 | 2720 |
for (int k = 0; k < 5; ++k) {
|
| 2703 | 2721 |
if (!forbidden[k]) {
|
| 2704 | 2722 |
_color_map[order[i]] = k; |
| 2705 | 2723 |
break; |
| 2706 | 2724 |
} |
| 2707 | 2725 |
} |
| 2708 | 2726 |
if (_color_map[order[i]] == -1) {
|
| 2709 | 2727 |
kempeRecoloring(order[i], embedding); |
| 2710 | 2728 |
} |
| 2711 | 2729 |
} |
| 2712 | 2730 |
} |
| 2713 | 2731 |
|
| 2714 |
/// \brief |
|
| 2732 |
/// \brief Calculate a coloring with at most five colors |
|
| 2715 | 2733 |
/// |
| 2716 | 2734 |
/// This function calculates a coloring with at most five |
| 2717 | 2735 |
/// colors. The worst case time complexity of this variant is |
| 2718 | 2736 |
/// quadratic in the size of the graph. |
| 2719 |
/// \return |
|
| 2737 |
/// \return \c true if the graph is planar. |
|
| 2720 | 2738 |
bool runFiveColoring() {
|
| 2721 | 2739 |
PlanarEmbedding<Graph> pe(_graph); |
| 2722 | 2740 |
if (!pe.run()) return false; |
| 2723 | 2741 |
|
| 2724 | 2742 |
runFiveColoring(pe.embeddingMap()); |
| 2725 | 2743 |
return true; |
| 2726 | 2744 |
} |
| 2727 | 2745 |
|
| 2728 | 2746 |
private: |
| 2729 | 2747 |
|
| 2730 | 2748 |
const Graph& _graph; |
| 2731 | 2749 |
IndexMap _color_map; |
| 2732 | 2750 |
Palette _palette; |
| 2733 | 2751 |
}; |
| 2734 | 2752 |
|
| 2735 | 2753 |
} |
| 2736 | 2754 |
|
| 2737 | 2755 |
#endif |
| ... | ... |
@@ -26,192 +26,197 @@ |
| 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 GR Digraph type. |
| 35 | 35 |
/// \tparam CAP Capacity map type. |
| 36 | 36 |
template <typename GR, typename CAP> |
| 37 | 37 |
struct PreflowDefaultTraits {
|
| 38 | 38 |
|
| 39 | 39 |
/// \brief The type of the digraph the algorithm runs on. |
| 40 | 40 |
typedef GR 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 CAP 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 |
#ifdef DOXYGEN |
| 56 | 56 |
typedef GR::ArcMap<Value> FlowMap; |
| 57 | 57 |
#else |
| 58 | 58 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
| 59 | 59 |
#endif |
| 60 | 60 |
|
| 61 | 61 |
/// \brief Instantiates a FlowMap. |
| 62 | 62 |
/// |
| 63 | 63 |
/// This function instantiates a \ref FlowMap. |
| 64 | 64 |
/// \param digraph The digraph for which we would like to define |
| 65 | 65 |
/// the flow map. |
| 66 | 66 |
static FlowMap* createFlowMap(const Digraph& digraph) {
|
| 67 | 67 |
return new FlowMap(digraph); |
| 68 | 68 |
} |
| 69 | 69 |
|
| 70 | 70 |
/// \brief The elevator type used by Preflow algorithm. |
| 71 | 71 |
/// |
| 72 | 72 |
/// The elevator type used by Preflow algorithm. |
| 73 | 73 |
/// |
| 74 | 74 |
/// \sa Elevator, LinkedElevator |
| 75 | 75 |
#ifdef DOXYGEN |
| 76 | 76 |
typedef lemon::Elevator<GR, GR::Node> Elevator; |
| 77 | 77 |
#else |
| 78 | 78 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
| 79 | 79 |
#endif |
| 80 | 80 |
|
| 81 | 81 |
/// \brief Instantiates an Elevator. |
| 82 | 82 |
/// |
| 83 | 83 |
/// This function instantiates an \ref Elevator. |
| 84 | 84 |
/// \param digraph The digraph for which we would like to define |
| 85 | 85 |
/// the elevator. |
| 86 | 86 |
/// \param max_level The maximum level of the elevator. |
| 87 | 87 |
static Elevator* createElevator(const Digraph& digraph, int max_level) {
|
| 88 | 88 |
return new Elevator(digraph, max_level); |
| 89 | 89 |
} |
| 90 | 90 |
|
| 91 | 91 |
/// \brief The tolerance used by the algorithm |
| 92 | 92 |
/// |
| 93 | 93 |
/// The tolerance used by the algorithm to handle inexact computation. |
| 94 | 94 |
typedef lemon::Tolerance<Value> Tolerance; |
| 95 | 95 |
|
| 96 | 96 |
}; |
| 97 | 97 |
|
| 98 | 98 |
|
| 99 | 99 |
/// \ingroup max_flow |
| 100 | 100 |
/// |
| 101 | 101 |
/// \brief %Preflow algorithm class. |
| 102 | 102 |
/// |
| 103 | 103 |
/// This class provides an implementation of Goldberg-Tarjan's \e preflow |
| 104 | 104 |
/// \e push-relabel algorithm producing a \ref max_flow |
| 105 | 105 |
/// "flow of maximum value" in a digraph \ref clrs01algorithms, |
| 106 | 106 |
/// \ref amo93networkflows, \ref goldberg88newapproach. |
| 107 | 107 |
/// The preflow algorithms are the fastest known maximum |
| 108 | 108 |
/// flow algorithms. The current implementation uses a mixture of the |
| 109 | 109 |
/// \e "highest label" and the \e "bound decrease" heuristics. |
| 110 | 110 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
|
| 111 | 111 |
/// |
| 112 | 112 |
/// The algorithm consists of two phases. After the first phase |
| 113 | 113 |
/// the maximum flow value and the minimum cut is obtained. The |
| 114 | 114 |
/// second phase constructs a feasible maximum flow on each arc. |
| 115 | 115 |
/// |
| 116 | 116 |
/// \warning This implementation cannot handle infinite or very large |
| 117 | 117 |
/// capacities (e.g. the maximum value of \c CAP::Value). |
| 118 | 118 |
/// |
| 119 | 119 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 120 | 120 |
/// \tparam CAP The type of the capacity map. The default map |
| 121 | 121 |
/// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 122 |
/// \tparam TR The traits class that defines various types used by the |
|
| 123 |
/// algorithm. By default, it is \ref PreflowDefaultTraits |
|
| 124 |
/// "PreflowDefaultTraits<GR, CAP>". |
|
| 125 |
/// In most cases, this parameter should not be set directly, |
|
| 126 |
/// consider to use the named template parameters instead. |
|
| 122 | 127 |
#ifdef DOXYGEN |
| 123 | 128 |
template <typename GR, typename CAP, typename TR> |
| 124 | 129 |
#else |
| 125 | 130 |
template <typename GR, |
| 126 | 131 |
typename CAP = typename GR::template ArcMap<int>, |
| 127 | 132 |
typename TR = PreflowDefaultTraits<GR, CAP> > |
| 128 | 133 |
#endif |
| 129 | 134 |
class Preflow {
|
| 130 | 135 |
public: |
| 131 | 136 |
|
| 132 | 137 |
///The \ref PreflowDefaultTraits "traits class" of the algorithm. |
| 133 | 138 |
typedef TR Traits; |
| 134 | 139 |
///The type of the digraph the algorithm runs on. |
| 135 | 140 |
typedef typename Traits::Digraph Digraph; |
| 136 | 141 |
///The type of the capacity map. |
| 137 | 142 |
typedef typename Traits::CapacityMap CapacityMap; |
| 138 | 143 |
///The type of the flow values. |
| 139 | 144 |
typedef typename Traits::Value Value; |
| 140 | 145 |
|
| 141 | 146 |
///The type of the flow map. |
| 142 | 147 |
typedef typename Traits::FlowMap FlowMap; |
| 143 | 148 |
///The type of the elevator. |
| 144 | 149 |
typedef typename Traits::Elevator Elevator; |
| 145 | 150 |
///The type of the tolerance. |
| 146 | 151 |
typedef typename Traits::Tolerance Tolerance; |
| 147 | 152 |
|
| 148 | 153 |
private: |
| 149 | 154 |
|
| 150 | 155 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 151 | 156 |
|
| 152 | 157 |
const Digraph& _graph; |
| 153 | 158 |
const CapacityMap* _capacity; |
| 154 | 159 |
|
| 155 | 160 |
int _node_num; |
| 156 | 161 |
|
| 157 | 162 |
Node _source, _target; |
| 158 | 163 |
|
| 159 | 164 |
FlowMap* _flow; |
| 160 | 165 |
bool _local_flow; |
| 161 | 166 |
|
| 162 | 167 |
Elevator* _level; |
| 163 | 168 |
bool _local_level; |
| 164 | 169 |
|
| 165 | 170 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
| 166 | 171 |
ExcessMap* _excess; |
| 167 | 172 |
|
| 168 | 173 |
Tolerance _tolerance; |
| 169 | 174 |
|
| 170 | 175 |
bool _phase; |
| 171 | 176 |
|
| 172 | 177 |
|
| 173 | 178 |
void createStructures() {
|
| 174 | 179 |
_node_num = countNodes(_graph); |
| 175 | 180 |
|
| 176 | 181 |
if (!_flow) {
|
| 177 | 182 |
_flow = Traits::createFlowMap(_graph); |
| 178 | 183 |
_local_flow = true; |
| 179 | 184 |
} |
| 180 | 185 |
if (!_level) {
|
| 181 | 186 |
_level = Traits::createElevator(_graph, _node_num); |
| 182 | 187 |
_local_level = true; |
| 183 | 188 |
} |
| 184 | 189 |
if (!_excess) {
|
| 185 | 190 |
_excess = new ExcessMap(_graph); |
| 186 | 191 |
} |
| 187 | 192 |
} |
| 188 | 193 |
|
| 189 | 194 |
void destroyStructures() {
|
| 190 | 195 |
if (_local_flow) {
|
| 191 | 196 |
delete _flow; |
| 192 | 197 |
} |
| 193 | 198 |
if (_local_level) {
|
| 194 | 199 |
delete _level; |
| 195 | 200 |
} |
| 196 | 201 |
if (_excess) {
|
| 197 | 202 |
delete _excess; |
| 198 | 203 |
} |
| 199 | 204 |
} |
| 200 | 205 |
|
| 201 | 206 |
public: |
| 202 | 207 |
|
| 203 | 208 |
typedef Preflow Create; |
| 204 | 209 |
|
| 205 | 210 |
///\name Named Template Parameters |
| 206 | 211 |
|
| 207 | 212 |
///@{
|
| 208 | 213 |
|
| 209 | 214 |
template <typename T> |
| 210 | 215 |
struct SetFlowMapTraits : public Traits {
|
| 211 | 216 |
typedef T FlowMap; |
| 212 | 217 |
static FlowMap *createFlowMap(const Digraph&) {
|
| 213 | 218 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
| 214 | 219 |
return 0; // ignore warnings |
| 215 | 220 |
} |
| 216 | 221 |
}; |
| 217 | 222 |
|
0 comments (0 inline)