1
21
2
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2009 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
namespace lemon { |
|
20 |
|
|
21 |
/** |
|
22 |
\page min_cost_flow Minimum Cost Flow Problem |
|
23 |
|
|
24 |
\section mcf_def Definition (GEQ form) |
|
25 |
|
|
26 |
The \e minimum \e cost \e flow \e problem is to find a feasible flow of |
|
27 |
minimum total cost from a set of supply nodes to a set of demand nodes |
|
28 |
in a network with capacity constraints (lower and upper bounds) |
|
29 |
and arc costs. |
|
30 |
|
|
31 |
Formally, let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$, |
|
32 |
\f$upper: A\rightarrow\mathbf{R}\cup\{+\infty\}\f$ denote the lower and |
|
33 |
upper bounds for the flow values on the arcs, for which |
|
34 |
\f$lower(uv) \leq upper(uv)\f$ must hold for all \f$uv\in A\f$, |
|
35 |
\f$cost: A\rightarrow\mathbf{R}\f$ denotes the cost per unit flow |
|
36 |
on the arcs and \f$sup: V\rightarrow\mathbf{R}\f$ denotes the |
|
37 |
signed supply values of the nodes. |
|
38 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
|
39 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
|
40 |
\f$-sup(u)\f$ demand. |
|
41 |
A minimum cost flow is an \f$f: A\rightarrow\mathbf{R}\f$ solution |
|
42 |
of the following optimization problem. |
|
43 |
|
|
44 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
|
45 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \geq |
|
46 |
sup(u) \quad \forall u\in V \f] |
|
47 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
|
48 |
|
|
49 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be |
|
50 |
zero or negative in order to have a feasible solution (since the sum |
|
51 |
of the expressions on the left-hand side of the inequalities is zero). |
|
52 |
It means that the total demand must be greater or equal to the total |
|
53 |
supply and all the supplies have to be carried out from the supply nodes, |
|
54 |
but there could be demands that are not satisfied. |
|
55 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand |
|
56 |
constraints have to be satisfied with equality, i.e. all demands |
|
57 |
have to be satisfied and all supplies have to be used. |
|
58 |
|
|
59 |
|
|
60 |
\section mcf_algs Algorithms |
|
61 |
|
|
62 |
LEMON contains several algorithms for solving this problem, for more |
|
63 |
information see \ref min_cost_flow_algs "Minimum Cost Flow Algorithms". |
|
64 |
|
|
65 |
A feasible solution for this problem can be found using \ref Circulation. |
|
66 |
|
|
67 |
|
|
68 |
\section mcf_dual Dual Solution |
|
69 |
|
|
70 |
The dual solution of the minimum cost flow problem is represented by |
|
71 |
node potentials \f$\pi: V\rightarrow\mathbf{R}\f$. |
|
72 |
An \f$f: A\rightarrow\mathbf{R}\f$ primal feasible solution is optimal |
|
73 |
if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ node potentials |
|
74 |
the following \e complementary \e slackness optimality conditions hold. |
|
75 |
|
|
76 |
- For all \f$uv\in A\f$ arcs: |
|
77 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
|
78 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
|
79 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
|
80 |
- For all \f$u\in V\f$ nodes: |
|
81 |
- \f$\pi(u)<=0\f$; |
|
82 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$, |
|
83 |
then \f$\pi(u)=0\f$. |
|
84 |
|
|
85 |
Here \f$cost^\pi(uv)\f$ denotes the \e reduced \e cost of the arc |
|
86 |
\f$uv\in A\f$ with respect to the potential function \f$\pi\f$, i.e. |
|
87 |
\f[ cost^\pi(uv) = cost(uv) + \pi(u) - \pi(v).\f] |
|
88 |
|
|
89 |
All algorithms provide dual solution (node potentials), as well, |
|
90 |
if an optimal flow is found. |
|
91 |
|
|
92 |
|
|
93 |
\section mcf_eq Equality Form |
|
94 |
|
|
95 |
The above \ref mcf_def "definition" is actually more general than the |
|
96 |
usual formulation of the minimum cost flow problem, in which strict |
|
97 |
equalities are required in the supply/demand contraints. |
|
98 |
|
|
99 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
|
100 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) = |
|
101 |
sup(u) \quad \forall u\in V \f] |
|
102 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
|
103 |
|
|
104 |
However if the sum of the supply values is zero, then these two problems |
|
105 |
are equivalent. |
|
106 |
The \ref min_cost_flow_algs "algorithms" in LEMON support the general |
|
107 |
form, so if you need the equality form, you have to ensure this additional |
|
108 |
contraint manually. |
|
109 |
|
|
110 |
|
|
111 |
\section mcf_leq Opposite Inequalites (LEQ Form) |
|
112 |
|
|
113 |
Another possible definition of the minimum cost flow problem is |
|
114 |
when there are <em>"less or equal"</em> (LEQ) supply/demand constraints, |
|
115 |
instead of the <em>"greater or equal"</em> (GEQ) constraints. |
|
116 |
|
|
117 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
|
118 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \leq |
|
119 |
sup(u) \quad \forall u\in V \f] |
|
120 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
|
121 |
|
|
122 |
It means that the total demand must be less or equal to the |
|
123 |
total supply (i.e. \f$\sum_{u\in V} sup(u)\f$ must be zero or |
|
124 |
positive) and all the demands have to be satisfied, but there |
|
125 |
could be supplies that are not carried out from the supply |
|
126 |
nodes. |
|
127 |
The equality form is also a special case of this form, of course. |
|
128 |
|
|
129 |
You could easily transform this case to the \ref mcf_def "GEQ form" |
|
130 |
of the problem by reversing the direction of the arcs and taking the |
|
131 |
negative of the supply values (e.g. using \ref ReverseDigraph and |
|
132 |
\ref NegMap adaptors). |
|
133 |
However \ref NetworkSimplex algorithm also supports this form directly |
|
134 |
for the sake of convenience. |
|
135 |
|
|
136 |
Note that the optimality conditions for this supply constraint type are |
|
137 |
slightly differ from the conditions that are discussed for the GEQ form, |
|
138 |
namely the potentials have to be non-negative instead of non-positive. |
|
139 |
An \f$f: A\rightarrow\mathbf{R}\f$ feasible solution of this problem |
|
140 |
is optimal if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ |
|
141 |
node potentials the following conditions hold. |
|
142 |
|
|
143 |
- For all \f$uv\in A\f$ arcs: |
|
144 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
|
145 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
|
146 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
|
147 |
- For all \f$u\in V\f$ nodes: |
|
148 |
- \f$\pi(u)>=0\f$; |
|
149 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$, |
|
150 |
then \f$\pi(u)=0\f$. |
|
151 |
|
|
152 |
*/ |
|
153 |
} |
1 | 1 |
CMAKE_MINIMUM_REQUIRED(VERSION 2.6) |
2 | 2 |
|
3 | 3 |
IF(EXISTS ${CMAKE_SOURCE_DIR}/cmake/version.cmake) |
4 | 4 |
INCLUDE(${CMAKE_SOURCE_DIR}/cmake/version.cmake) |
5 | 5 |
ELSE(EXISTS ${CMAKE_SOURCE_DIR}/cmake/version.cmake) |
6 | 6 |
SET(PROJECT_NAME "LEMON") |
7 | 7 |
SET(PROJECT_VERSION "hg-tip" CACHE STRING "LEMON version string.") |
8 | 8 |
ENDIF(EXISTS ${CMAKE_SOURCE_DIR}/cmake/version.cmake) |
9 | 9 |
|
10 | 10 |
PROJECT(${PROJECT_NAME}) |
11 | 11 |
|
12 | 12 |
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) |
13 | 13 |
|
14 | 14 |
INCLUDE(FindDoxygen) |
15 | 15 |
INCLUDE(FindGhostscript) |
16 | 16 |
FIND_PACKAGE(GLPK 4.33) |
17 | 17 |
FIND_PACKAGE(CPLEX) |
18 | 18 |
FIND_PACKAGE(COIN) |
19 | 19 |
|
20 | 20 |
IF(MSVC) |
21 | 21 |
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4250 /wd4355 /wd4800 /wd4996") |
22 | 22 |
# Suppressed warnings: |
23 | 23 |
# C4250: 'class1' : inherits 'class2::member' via dominance |
24 | 24 |
# C4355: 'this' : used in base member initializer list |
25 | 25 |
# C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning) |
26 | 26 |
# C4996: 'function': was declared deprecated |
27 | 27 |
ENDIF(MSVC) |
28 | 28 |
|
29 | 29 |
INCLUDE(CheckTypeSize) |
30 | 30 |
CHECK_TYPE_SIZE("long long" LEMON_LONG_LONG) |
31 | 31 |
|
32 | 32 |
ENABLE_TESTING() |
33 | 33 |
|
34 | 34 |
ADD_SUBDIRECTORY(lemon) |
35 | 35 |
IF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR}) |
36 | 36 |
ADD_SUBDIRECTORY(demo) |
37 | 37 |
ADD_SUBDIRECTORY(tools) |
38 | 38 |
ADD_SUBDIRECTORY(doc) |
39 | 39 |
ADD_SUBDIRECTORY(test) |
40 | 40 |
ENDIF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR}) |
41 | 41 |
|
42 | 42 |
IF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR}) |
43 | 43 |
IF(WIN32) |
44 | 44 |
SET(CPACK_PACKAGE_NAME ${PROJECT_NAME}) |
45 | 45 |
SET(CPACK_PACKAGE_VENDOR "EGRES") |
46 | 46 |
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY |
47 |
"LEMON - Library |
|
47 |
"LEMON - Library for Efficient Modeling and Optimization in Networks") |
|
48 | 48 |
SET(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE") |
49 | 49 |
|
50 | 50 |
SET(CPACK_PACKAGE_VERSION ${PROJECT_VERSION}) |
51 | 51 |
|
52 | 52 |
SET(CPACK_PACKAGE_INSTALL_DIRECTORY |
53 | 53 |
"${PROJECT_NAME} ${PROJECT_VERSION}") |
54 | 54 |
SET(CPACK_PACKAGE_INSTALL_REGISTRY_KEY |
55 | 55 |
"${PROJECT_NAME} ${PROJECT_VERSION}") |
56 | 56 |
|
57 | 57 |
SET(CPACK_COMPONENTS_ALL headers library html_documentation bin) |
58 | 58 |
|
59 | 59 |
SET(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "C++ headers") |
60 | 60 |
SET(CPACK_COMPONENT_LIBRARY_DISPLAY_NAME "Dynamic-link library") |
61 | 61 |
SET(CPACK_COMPONENT_BIN_DISPLAY_NAME "Command line utilities") |
62 | 62 |
SET(CPACK_COMPONENT_HTML_DOCUMENTATION_DISPLAY_NAME "HTML documentation") |
63 | 63 |
|
64 | 64 |
SET(CPACK_COMPONENT_HEADERS_DESCRIPTION |
65 | 65 |
"C++ header files") |
66 | 66 |
SET(CPACK_COMPONENT_LIBRARY_DESCRIPTION |
67 | 67 |
"DLL and import library") |
68 | 68 |
SET(CPACK_COMPONENT_BIN_DESCRIPTION |
69 | 69 |
"Command line utilities") |
70 | 70 |
SET(CPACK_COMPONENT_HTML_DOCUMENTATION_DESCRIPTION |
71 | 71 |
"Doxygen generated documentation") |
72 | 72 |
|
73 | 73 |
SET(CPACK_COMPONENT_HEADERS_DEPENDS library) |
74 | 74 |
|
75 | 75 |
SET(CPACK_COMPONENT_HEADERS_GROUP "Development") |
76 | 76 |
SET(CPACK_COMPONENT_LIBRARY_GROUP "Development") |
77 | 77 |
SET(CPACK_COMPONENT_HTML_DOCUMENTATION_GROUP "Documentation") |
78 | 78 |
|
79 | 79 |
SET(CPACK_COMPONENT_GROUP_DEVELOPMENT_DESCRIPTION |
80 | 80 |
"Components needed to develop software using LEMON") |
81 | 81 |
SET(CPACK_COMPONENT_GROUP_DOCUMENTATION_DESCRIPTION |
82 | 82 |
"Documentation of LEMON") |
83 | 83 |
|
84 | 84 |
SET(CPACK_ALL_INSTALL_TYPES Full Developer) |
85 | 85 |
|
86 | 86 |
SET(CPACK_COMPONENT_HEADERS_INSTALL_TYPES Developer Full) |
87 | 87 |
SET(CPACK_COMPONENT_LIBRARY_INSTALL_TYPES Developer Full) |
88 | 88 |
SET(CPACK_COMPONENT_HTML_DOCUMENTATION_INSTALL_TYPES Full) |
89 | 89 |
|
90 | 90 |
SET(CPACK_GENERATOR "NSIS") |
91 | 91 |
SET(CPACK_NSIS_MUI_ICON "${PROJECT_SOURCE_DIR}/cmake/nsis/lemon.ico") |
92 | 92 |
SET(CPACK_NSIS_MUI_UNIICON "${PROJECT_SOURCE_DIR}/cmake/nsis/uninstall.ico") |
93 | 93 |
#SET(CPACK_PACKAGE_ICON "${PROJECT_SOURCE_DIR}/cmake/nsis\\\\installer.bmp") |
94 | 94 |
SET(CPACK_NSIS_INSTALLED_ICON_NAME "bin\\\\lemon.ico") |
95 | 95 |
SET(CPACK_NSIS_DISPLAY_NAME "${CPACK_PACKAGE_INSTALL_DIRECTORY} ${PROJECT_NAME}") |
96 | 96 |
SET(CPACK_NSIS_HELP_LINK "http:\\\\\\\\lemon.cs.elte.hu") |
97 | 97 |
SET(CPACK_NSIS_URL_INFO_ABOUT "http:\\\\\\\\lemon.cs.elte.hu") |
98 | 98 |
SET(CPACK_NSIS_CONTACT "lemon-user@lemon.cs.elte.hu") |
99 | 99 |
SET(CPACK_NSIS_CREATE_ICONS_EXTRA " |
100 | 100 |
CreateShortCut \\\"$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\Documentation.lnk\\\" \\\"$INSTDIR\\\\share\\\\doc\\\\index.html\\\" |
101 | 101 |
") |
102 | 102 |
SET(CPACK_NSIS_DELETE_ICONS_EXTRA " |
103 | 103 |
!insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP |
104 | 104 |
Delete \\\"$SMPROGRAMS\\\\$MUI_TEMP\\\\Documentation.lnk\\\" |
105 | 105 |
") |
106 | 106 |
|
107 | 107 |
INCLUDE(CPack) |
108 | 108 |
ENDIF(WIN32) |
109 | 109 |
ENDIF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR}) |
1 |
2009-05-13 Version 1.1 released |
|
2 |
|
|
3 |
This is the second stable release of the 1.x series. It |
|
4 |
features a better coverage of the tools available in the 0.x |
|
5 |
series, a thoroughly reworked LP/MIP interface plus various |
|
6 |
improvements in the existing tools. |
|
7 |
|
|
8 |
* Much improved M$ Windows support |
|
9 |
* Various improvements in the CMAKE build system |
|
10 |
* Compilation warnings are fixed/suppressed |
|
11 |
* Support IBM xlC compiler |
|
12 |
* New algorithms |
|
13 |
* Connectivity related algorithms (#61) |
|
14 |
* Euler walks (#65) |
|
15 |
* Preflow push-relabel max. flow algorithm (#176) |
|
16 |
* Circulation algorithm (push-relabel based) (#175) |
|
17 |
* Suurballe algorithm (#47) |
|
18 |
* Gomory-Hu algorithm (#66) |
|
19 |
* Hao-Orlin algorithm (#58) |
|
20 |
* Edmond's maximum cardinality and weighted matching algorithms |
|
21 |
in general graphs (#48,#265) |
|
22 |
* Minimum cost arborescence/branching (#60) |
|
23 |
* Network Simplex min. cost flow algorithm (#234) |
|
24 |
* New data structures |
|
25 |
* Full graph structure (#57) |
|
26 |
* Grid graph structure (#57) |
|
27 |
* Hypercube graph structure (#57) |
|
28 |
* Graph adaptors (#67) |
|
29 |
* ArcSet and EdgeSet classes (#67) |
|
30 |
* Elevator class (#174) |
|
31 |
* Other new tools |
|
32 |
* LP/MIP interface (#44) |
|
33 |
* Support for GLPK, CPLEX, Soplex, COIN-OR CLP and CBC |
|
34 |
* Reader for the Nauty file format (#55) |
|
35 |
* DIMACS readers (#167) |
|
36 |
* Radix sort algorithms (#72) |
|
37 |
* RangeIdMap and CrossRefMap (#160) |
|
38 |
* New command line tools |
|
39 |
* DIMACS to LGF converter (#182) |
|
40 |
* lgf-gen - a graph generator (#45) |
|
41 |
* DIMACS solver utility (#226) |
|
42 |
* Other code improvements |
|
43 |
* Lognormal distribution added to Random (#102) |
|
44 |
* Better (i.e. O(1) time) item counting in SmartGraph (#3) |
|
45 |
* The standard maps of graphs are guaranteed to be |
|
46 |
reference maps (#190) |
|
47 |
* Miscellaneous |
|
48 |
* Various doc improvements |
|
49 |
* Improved 0.x -> 1.x converter script |
|
50 |
|
|
51 |
* Several bugfixes (compared to release 1.0): |
|
52 |
#170: Bugfix SmartDigraph::split() |
|
53 |
#171: Bugfix in SmartGraph::restoreSnapshot() |
|
54 |
#172: Extended test cases for graphs and digraphs |
|
55 |
#173: Bugfix in Random |
|
56 |
* operator()s always return a double now |
|
57 |
* the faulty real<Num>(Num) and real<Num>(Num,Num) |
|
58 |
have been removed |
|
59 |
#187: Remove DijkstraWidestPathOperationTraits |
|
60 |
#61: Bugfix in DfsVisit |
|
61 |
#193: Bugfix in GraphReader::skipSection() |
|
62 |
#195: Bugfix in ConEdgeIt() |
|
63 |
#197: Bugfix in heap unionfind |
|
64 |
* This bug affects Edmond's general matching algorithms |
|
65 |
#207: Fix 'make install' without 'make html' using CMAKE |
|
66 |
#208: Suppress or fix VS2008 compilation warnings |
|
67 |
----: Update the LEMON icon |
|
68 |
----: Enable the component-based installer |
|
69 |
(in installers made by CPACK) |
|
70 |
----: Set the proper version for CMAKE in the tarballs |
|
71 |
(made by autotools) |
|
72 |
----: Minor clarification in the LICENSE file |
|
73 |
----: Add missing unistd.h include to time_measure.h |
|
74 |
#204: Compilation bug fixed in graph_to_eps.h with VS2005 |
|
75 |
#214,#215: windows.h should never be included by lemon headers |
|
76 |
#230: Build systems check the availability of 'long long' type |
|
77 |
#229: Default implementation of Tolerance<> is used for integer types |
|
78 |
#211,#212: Various fixes for compiling on AIX |
|
79 |
----: Improvements in CMAKE config |
|
80 |
- docs is installed in share/doc/ |
|
81 |
- detects newer versions of Ghostscript |
|
82 |
#239: Fix missing 'inline' specifier in time_measure.h |
|
83 |
#274,#280: Install lemon/config.h |
|
84 |
#275: Prefix macro names with LEMON_ in lemon/config.h |
|
85 |
----: Small script for making the release tarballs added |
|
86 |
----: Minor improvement in unify-sources.sh (a76f55d7d397) |
|
87 |
|
|
1 | 88 |
2009-03-27 LEMON joins to the COIN-OR initiative |
2 | 89 |
|
3 | 90 |
COIN-OR (Computational Infrastructure for Operations Research, |
4 | 91 |
http://www.coin-or.org) project is an initiative to spur the |
5 | 92 |
development of open-source software for the operations research |
6 | 93 |
community. |
7 | 94 |
|
8 | 95 |
2008-10-13 Version 1.0 released |
9 | 96 |
|
10 | 97 |
This is the first stable release of LEMON. Compared to the 0.x |
11 | 98 |
release series, it features a considerably smaller but more |
12 | 99 |
matured set of tools. The API has also completely revised and |
13 | 100 |
changed in several places. |
14 | 101 |
|
15 | 102 |
* The major name changes compared to the 0.x series (see the |
16 | 103 |
Migration Guide in the doc for more details) |
17 | 104 |
* Graph -> Digraph, UGraph -> Graph |
18 | 105 |
* Edge -> Arc, UEdge -> Edge |
19 | 106 |
* source(UEdge)/target(UEdge) -> u(Edge)/v(Edge) |
20 | 107 |
* Other improvements |
21 | 108 |
* Better documentation |
22 | 109 |
* Reviewed and cleaned up codebase |
23 | 110 |
* CMake based build system (along with the autotools based one) |
24 | 111 |
* Contents of the library (ported from 0.x) |
25 | 112 |
* Algorithms |
26 | 113 |
* breadth-first search (bfs.h) |
27 | 114 |
* depth-first search (dfs.h) |
28 | 115 |
* Dijkstra's algorithm (dijkstra.h) |
29 | 116 |
* Kruskal's algorithm (kruskal.h) |
30 | 117 |
* Data structures |
31 | 118 |
* graph data structures (list_graph.h, smart_graph.h) |
32 | 119 |
* path data structures (path.h) |
33 | 120 |
* binary heap data structure (bin_heap.h) |
34 | 121 |
* union-find data structures (unionfind.h) |
35 | 122 |
* miscellaneous property maps (maps.h) |
36 | 123 |
* two dimensional vector and bounding box (dim2.h) |
37 | 124 |
* Concepts |
38 | 125 |
* graph structure concepts (concepts/digraph.h, concepts/graph.h, |
39 | 126 |
concepts/graph_components.h) |
40 | 127 |
* concepts for other structures (concepts/heap.h, concepts/maps.h, |
41 | 128 |
concepts/path.h) |
42 | 129 |
* Tools |
43 | 130 |
* Mersenne twister random number generator (random.h) |
44 | 131 |
* tools for measuring cpu and wall clock time (time_measure.h) |
45 | 132 |
* tools for counting steps and events (counter.h) |
46 | 133 |
* tool for parsing command line arguments (arg_parser.h) |
47 | 134 |
* tool for visualizing graphs (graph_to_eps.h) |
48 | 135 |
* tools for reading and writing data in LEMON Graph Format |
49 | 136 |
(lgf_reader.h, lgf_writer.h) |
50 | 137 |
* tools to handle the anomalies of calculations with |
51 | 138 |
floating point numbers (tolerance.h) |
52 | 139 |
* tools to manage RGB colors (color.h) |
53 | 140 |
* Infrastructure |
54 | 141 |
* extended assertion handling (assert.h) |
55 | 142 |
* exception classes and error handling (error.h) |
56 | 143 |
* concept checking (concept_check.h) |
57 | 144 |
* commonly used mathematical constants (math.h) |
1 |
================================================================== |
|
2 |
LEMON - a Library of Efficient Models and Optimization in Networks |
|
3 |
================================================================== |
|
1 |
===================================================================== |
|
2 |
LEMON - a Library for Efficient Modeling and Optimization in Networks |
|
3 |
===================================================================== |
|
4 | 4 |
|
5 | 5 |
LEMON is an open source library written in C++. It provides |
6 | 6 |
easy-to-use implementations of common data structures and algorithms |
7 | 7 |
in the area of optimization and helps implementing new ones. The main |
8 | 8 |
focus is on graphs and graph algorithms, thus it is especially |
9 | 9 |
suitable for solving design and optimization problems of |
10 | 10 |
telecommunication networks. To achieve wide usability its data |
11 | 11 |
structures and algorithms provide generic interfaces. |
12 | 12 |
|
13 | 13 |
Contents |
14 | 14 |
======== |
15 | 15 |
|
16 | 16 |
LICENSE |
17 | 17 |
|
18 | 18 |
Copying, distribution and modification conditions and terms. |
19 | 19 |
|
20 | 20 |
INSTALL |
21 | 21 |
|
22 | 22 |
General building and installation instructions. |
23 | 23 |
|
24 | 24 |
lemon/ |
25 | 25 |
|
26 | 26 |
Source code of LEMON library. |
27 | 27 |
|
28 | 28 |
doc/ |
29 | 29 |
|
30 | 30 |
Documentation of LEMON. The starting page is doc/html/index.html. |
31 | 31 |
|
32 | 32 |
demo/ |
33 | 33 |
|
34 | 34 |
Some example programs to make you easier to get familiar with LEMON. |
35 | 35 |
|
36 | 36 |
test/ |
37 | 37 |
|
38 | 38 |
Programs to check the integrity and correctness of LEMON. |
39 | 39 |
|
40 | 40 |
tools/ |
41 | 41 |
|
42 | 42 |
Various utilities related to LEMON. |
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 |
doc/min_cost_flow.dox \ |
|
11 | 12 |
doc/named-param.dox \ |
12 | 13 |
doc/namespaces.dox \ |
13 | 14 |
doc/html \ |
14 | 15 |
doc/CMakeLists.txt |
15 | 16 |
|
16 | 17 |
DOC_EPS_IMAGES18 = \ |
17 | 18 |
grid_graph.eps \ |
18 | 19 |
nodeshape_0.eps \ |
19 | 20 |
nodeshape_1.eps \ |
20 | 21 |
nodeshape_2.eps \ |
21 | 22 |
nodeshape_3.eps \ |
22 | 23 |
nodeshape_4.eps |
23 | 24 |
|
24 | 25 |
DOC_EPS_IMAGES27 = \ |
25 | 26 |
bipartite_matching.eps \ |
26 | 27 |
bipartite_partitions.eps \ |
27 | 28 |
connected_components.eps \ |
28 | 29 |
edge_biconnected_components.eps \ |
29 | 30 |
node_biconnected_components.eps \ |
30 | 31 |
strongly_connected_components.eps |
31 | 32 |
|
32 | 33 |
DOC_EPS_IMAGES = \ |
33 | 34 |
$(DOC_EPS_IMAGES18) \ |
34 | 35 |
$(DOC_EPS_IMAGES27) |
35 | 36 |
|
36 | 37 |
DOC_PNG_IMAGES = \ |
37 | 38 |
$(DOC_EPS_IMAGES:%.eps=doc/gen-images/%.png) |
38 | 39 |
|
39 | 40 |
EXTRA_DIST += $(DOC_EPS_IMAGES:%=doc/images/%) |
40 | 41 |
|
41 | 42 |
doc/html: |
42 | 43 |
$(MAKE) $(AM_MAKEFLAGS) html |
43 | 44 |
|
44 | 45 |
GS_COMMAND=gs -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 |
45 | 46 |
|
46 | 47 |
$(DOC_EPS_IMAGES18:%.eps=doc/gen-images/%.png): doc/gen-images/%.png: doc/images/%.eps |
47 | 48 |
-mkdir doc/gen-images |
48 | 49 |
if test ${gs_found} = yes; then \ |
49 | 50 |
$(GS_COMMAND) -sDEVICE=pngalpha -r18 -sOutputFile=$@ $<; \ |
50 | 51 |
else \ |
51 | 52 |
echo; \ |
52 | 53 |
echo "Ghostscript not found."; \ |
53 | 54 |
echo; \ |
54 | 55 |
exit 1; \ |
55 | 56 |
fi |
56 | 57 |
|
57 | 58 |
$(DOC_EPS_IMAGES27:%.eps=doc/gen-images/%.png): doc/gen-images/%.png: doc/images/%.eps |
58 | 59 |
-mkdir doc/gen-images |
59 | 60 |
if test ${gs_found} = yes; then \ |
60 | 61 |
$(GS_COMMAND) -sDEVICE=pngalpha -r27 -sOutputFile=$@ $<; \ |
61 | 62 |
else \ |
62 | 63 |
echo; \ |
63 | 64 |
echo "Ghostscript not found."; \ |
64 | 65 |
echo; \ |
65 | 66 |
exit 1; \ |
66 | 67 |
fi |
67 | 68 |
|
68 | 69 |
html-local: $(DOC_PNG_IMAGES) |
69 | 70 |
if test ${doxygen_found} = yes; then \ |
70 | 71 |
cd doc; \ |
71 | 72 |
doxygen Doxyfile; \ |
72 | 73 |
cd ..; \ |
73 | 74 |
else \ |
74 | 75 |
echo; \ |
75 | 76 |
echo "Doxygen not found."; \ |
76 | 77 |
echo; \ |
77 | 78 |
exit 1; \ |
78 | 79 |
fi |
79 | 80 |
|
80 | 81 |
clean-local: |
81 | 82 |
-rm -rf doc/html |
82 | 83 |
-rm -f doc/doxygen.log |
83 | 84 |
-rm -f $(DOC_PNG_IMAGES) |
84 | 85 |
-rm -rf doc/gen-images |
85 | 86 |
|
86 | 87 |
update-external-tags: |
87 | 88 |
wget -O doc/libstdc++.tag.tmp http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/libstdc++.tag && \ |
88 | 89 |
mv doc/libstdc++.tag.tmp doc/libstdc++.tag || \ |
89 | 90 |
rm doc/libstdc++.tag.tmp |
90 | 91 |
|
91 | 92 |
install-html-local: doc/html |
92 | 93 |
@$(NORMAL_INSTALL) |
93 | 94 |
$(mkinstalldirs) $(DESTDIR)$(htmldir)/docs |
94 | 95 |
for p in doc/html/*.{html,css,png,map,gif,tag} ; do \ |
95 | 96 |
f="`echo $$p | sed -e 's|^.*/||'`"; \ |
96 | 97 |
echo " $(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/docs/$$f"; \ |
97 | 98 |
$(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/docs/$$f; \ |
98 | 99 |
done |
99 | 100 |
|
100 | 101 |
uninstall-local: |
101 | 102 |
@$(NORMAL_UNINSTALL) |
102 | 103 |
for p in doc/html/*.{html,css,png,map,gif,tag} ; do \ |
103 | 104 |
f="`echo $$p | sed -e 's|^.*/||'`"; \ |
104 | 105 |
echo " rm -f $(DESTDIR)$(htmldir)/docs/$$f"; \ |
105 | 106 |
rm -f $(DESTDIR)$(htmldir)/docs/$$f; \ |
106 | 107 |
done |
107 | 108 |
|
108 | 109 |
.PHONY: update-external-tags |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
namespace lemon { |
20 | 20 |
|
21 | 21 |
/** |
22 | 22 |
@defgroup datas Data Structures |
23 | 23 |
This group contains the several data structures implemented in LEMON. |
24 | 24 |
*/ |
25 | 25 |
|
26 | 26 |
/** |
27 | 27 |
@defgroup graphs Graph Structures |
28 | 28 |
@ingroup datas |
29 | 29 |
\brief Graph structures implemented in LEMON. |
30 | 30 |
|
31 | 31 |
The implementation of combinatorial algorithms heavily relies on |
32 | 32 |
efficient graph implementations. LEMON offers data structures which are |
33 | 33 |
planned to be easily used in an experimental phase of implementation studies, |
34 | 34 |
and thereafter the program code can be made efficient by small modifications. |
35 | 35 |
|
36 | 36 |
The most efficient implementation of diverse applications require the |
37 | 37 |
usage of different physical graph implementations. These differences |
38 | 38 |
appear in the size of graph we require to handle, memory or time usage |
39 | 39 |
limitations or in the set of operations through which the graph can be |
40 | 40 |
accessed. LEMON provides several physical graph structures to meet |
41 | 41 |
the diverging requirements of the possible users. In order to save on |
42 | 42 |
running time or on memory usage, some structures may fail to provide |
43 | 43 |
some graph features like arc/edge or node deletion. |
44 | 44 |
|
45 | 45 |
Alteration of standard containers need a very limited number of |
46 | 46 |
operations, these together satisfy the everyday requirements. |
47 | 47 |
In the case of graph structures, different operations are needed which do |
48 | 48 |
not alter the physical graph, but gives another view. If some nodes or |
49 | 49 |
arcs have to be hidden or the reverse oriented graph have to be used, then |
50 | 50 |
this is the case. It also may happen that in a flow implementation |
51 | 51 |
the residual graph can be accessed by another algorithm, or a node-set |
52 | 52 |
is to be shrunk for another algorithm. |
53 | 53 |
LEMON also provides a variety of graphs for these requirements called |
54 | 54 |
\ref graph_adaptors "graph adaptors". Adaptors cannot be used alone but only |
55 | 55 |
in conjunction with other graph representations. |
56 | 56 |
|
57 | 57 |
You are free to use the graph structure that fit your requirements |
58 | 58 |
the best, most graph algorithms and auxiliary data structures can be used |
59 | 59 |
with any graph structure. |
60 | 60 |
|
61 | 61 |
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts". |
62 | 62 |
*/ |
63 | 63 |
|
64 | 64 |
/** |
65 | 65 |
@defgroup graph_adaptors Adaptor Classes for Graphs |
66 | 66 |
@ingroup graphs |
67 | 67 |
\brief Adaptor classes for digraphs and graphs |
68 | 68 |
|
69 | 69 |
This group contains several useful adaptor classes for digraphs and graphs. |
70 | 70 |
|
71 | 71 |
The main parts of LEMON are the different graph structures, generic |
72 | 72 |
graph algorithms, graph concepts, which couple them, and graph |
73 | 73 |
adaptors. While the previous notions are more or less clear, the |
74 | 74 |
latter one needs further explanation. Graph adaptors are graph classes |
75 | 75 |
which serve for considering graph structures in different ways. |
76 | 76 |
|
77 | 77 |
A short example makes this much clearer. Suppose that we have an |
78 | 78 |
instance \c g of a directed graph type, say ListDigraph and an algorithm |
79 | 79 |
\code |
80 | 80 |
template <typename Digraph> |
81 | 81 |
int algorithm(const Digraph&); |
82 | 82 |
\endcode |
83 | 83 |
is needed to run on the reverse oriented graph. It may be expensive |
84 | 84 |
(in time or in memory usage) to copy \c g with the reversed |
85 | 85 |
arcs. In this case, an adaptor class is used, which (according |
86 | 86 |
to LEMON \ref concepts::Digraph "digraph concepts") works as a digraph. |
87 | 87 |
The adaptor uses the original digraph structure and digraph operations when |
88 | 88 |
methods of the reversed oriented graph are called. This means that the adaptor |
89 | 89 |
have minor memory usage, and do not perform sophisticated algorithmic |
90 | 90 |
actions. The purpose of it is to give a tool for the cases when a |
91 | 91 |
graph have to be used in a specific alteration. If this alteration is |
92 | 92 |
obtained by a usual construction like filtering the node or the arc set or |
93 | 93 |
considering a new orientation, then an adaptor is worthwhile to use. |
94 | 94 |
To come back to the reverse oriented graph, in this situation |
95 | 95 |
\code |
96 | 96 |
template<typename Digraph> class ReverseDigraph; |
97 | 97 |
\endcode |
98 | 98 |
template class can be used. The code looks as follows |
99 | 99 |
\code |
100 | 100 |
ListDigraph g; |
101 | 101 |
ReverseDigraph<ListDigraph> rg(g); |
102 | 102 |
int result = algorithm(rg); |
103 | 103 |
\endcode |
104 | 104 |
During running the algorithm, the original digraph \c g is untouched. |
105 | 105 |
This techniques give rise to an elegant code, and based on stable |
106 | 106 |
graph adaptors, complex algorithms can be implemented easily. |
107 | 107 |
|
108 | 108 |
In flow, circulation and matching problems, the residual |
109 | 109 |
graph is of particular importance. Combining an adaptor implementing |
110 | 110 |
this with shortest path algorithms or minimum mean cycle algorithms, |
111 | 111 |
a range of weighted and cardinality optimization algorithms can be |
112 | 112 |
obtained. For other examples, the interested user is referred to the |
113 | 113 |
detailed documentation of particular adaptors. |
114 | 114 |
|
115 | 115 |
The behavior of graph adaptors can be very different. Some of them keep |
116 | 116 |
capabilities of the original graph while in other cases this would be |
117 | 117 |
meaningless. This means that the concepts that they meet depend |
118 | 118 |
on the graph adaptor, and the wrapped graph. |
119 | 119 |
For example, if an arc of a reversed digraph is deleted, this is carried |
120 | 120 |
out by deleting the corresponding arc of the original digraph, thus the |
121 | 121 |
adaptor modifies the original digraph. |
122 | 122 |
However in case of a residual digraph, this operation has no sense. |
123 | 123 |
|
124 | 124 |
Let us stand one more example here to simplify your work. |
125 | 125 |
ReverseDigraph has constructor |
126 | 126 |
\code |
127 | 127 |
ReverseDigraph(Digraph& digraph); |
128 | 128 |
\endcode |
129 | 129 |
This means that in a situation, when a <tt>const %ListDigraph&</tt> |
130 | 130 |
reference to a graph is given, then it have to be instantiated with |
131 | 131 |
<tt>Digraph=const %ListDigraph</tt>. |
132 | 132 |
\code |
133 | 133 |
int algorithm1(const ListDigraph& g) { |
134 | 134 |
ReverseDigraph<const ListDigraph> rg(g); |
135 | 135 |
return algorithm2(rg); |
136 | 136 |
} |
137 | 137 |
\endcode |
138 | 138 |
*/ |
139 | 139 |
|
140 | 140 |
/** |
141 |
@defgroup semi_adaptors Semi-Adaptor Classes for Graphs |
|
142 |
@ingroup graphs |
|
143 |
\brief Graph types between real graphs and graph adaptors. |
|
144 |
|
|
145 |
This group contains some graph types between real graphs and graph adaptors. |
|
146 |
These classes wrap graphs to give new functionality as the adaptors do it. |
|
147 |
On the other hand they are not light-weight structures as the adaptors. |
|
148 |
*/ |
|
149 |
|
|
150 |
/** |
|
151 | 141 |
@defgroup maps Maps |
152 | 142 |
@ingroup datas |
153 | 143 |
\brief Map structures implemented in LEMON. |
154 | 144 |
|
155 | 145 |
This group contains the map structures implemented in LEMON. |
156 | 146 |
|
157 | 147 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
158 | 148 |
new maps from existing ones. |
159 | 149 |
|
160 | 150 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
161 | 151 |
*/ |
162 | 152 |
|
163 | 153 |
/** |
164 | 154 |
@defgroup graph_maps Graph Maps |
165 | 155 |
@ingroup maps |
166 | 156 |
\brief Special graph-related maps. |
167 | 157 |
|
168 | 158 |
This group contains maps that are specifically designed to assign |
169 | 159 |
values to the nodes and arcs/edges of graphs. |
170 | 160 |
|
171 | 161 |
If you are looking for the standard graph maps (\c NodeMap, \c ArcMap, |
172 | 162 |
\c EdgeMap), see the \ref graph_concepts "Graph Structure Concepts". |
173 | 163 |
*/ |
174 | 164 |
|
175 | 165 |
/** |
176 | 166 |
\defgroup map_adaptors Map Adaptors |
177 | 167 |
\ingroup maps |
178 | 168 |
\brief Tools to create new maps from existing ones |
179 | 169 |
|
180 | 170 |
This group contains map adaptors that are used to create "implicit" |
181 | 171 |
maps from other maps. |
182 | 172 |
|
183 | 173 |
Most of them are \ref concepts::ReadMap "read-only maps". |
184 | 174 |
They can make arithmetic and logical operations between one or two maps |
185 | 175 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
186 | 176 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
187 | 177 |
|
188 | 178 |
The typical usage of this classes is passing implicit maps to |
189 | 179 |
algorithms. If a function type algorithm is called then the function |
190 | 180 |
type map adaptors can be used comfortable. For example let's see the |
191 | 181 |
usage of map adaptors with the \c graphToEps() function. |
192 | 182 |
\code |
193 | 183 |
Color nodeColor(int deg) { |
194 | 184 |
if (deg >= 2) { |
195 | 185 |
return Color(0.5, 0.0, 0.5); |
196 | 186 |
} else if (deg == 1) { |
197 | 187 |
return Color(1.0, 0.5, 1.0); |
198 | 188 |
} else { |
199 | 189 |
return Color(0.0, 0.0, 0.0); |
200 | 190 |
} |
201 | 191 |
} |
202 | 192 |
|
203 | 193 |
Digraph::NodeMap<int> degree_map(graph); |
204 | 194 |
|
205 | 195 |
graphToEps(graph, "graph.eps") |
206 | 196 |
.coords(coords).scaleToA4().undirected() |
207 | 197 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
208 | 198 |
.run(); |
209 | 199 |
\endcode |
210 | 200 |
The \c functorToMap() function makes an \c int to \c Color map from the |
211 | 201 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
212 | 202 |
and the previously created map. The composed map is a proper function to |
213 | 203 |
get the color of each node. |
214 | 204 |
|
215 | 205 |
The usage with class type algorithms is little bit harder. In this |
216 | 206 |
case the function type map adaptors can not be used, because the |
217 | 207 |
function map adaptors give back temporary objects. |
218 | 208 |
\code |
219 | 209 |
Digraph graph; |
220 | 210 |
|
221 | 211 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
222 | 212 |
DoubleArcMap length(graph); |
223 | 213 |
DoubleArcMap speed(graph); |
224 | 214 |
|
225 | 215 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
226 | 216 |
TimeMap time(length, speed); |
227 | 217 |
|
228 | 218 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
229 | 219 |
dijkstra.run(source, target); |
230 | 220 |
\endcode |
231 | 221 |
We have a length map and a maximum speed map on the arcs of a digraph. |
232 | 222 |
The minimum time to pass the arc can be calculated as the division of |
233 | 223 |
the two maps which can be done implicitly with the \c DivMap template |
234 | 224 |
class. We use the implicit minimum time map as the length map of the |
235 | 225 |
\c Dijkstra algorithm. |
236 | 226 |
*/ |
237 | 227 |
|
238 | 228 |
/** |
239 | 229 |
@defgroup paths Path Structures |
240 | 230 |
@ingroup datas |
241 | 231 |
\brief %Path structures implemented in LEMON. |
242 | 232 |
|
243 | 233 |
This group contains the path structures implemented in LEMON. |
244 | 234 |
|
245 | 235 |
LEMON provides flexible data structures to work with paths. |
246 | 236 |
All of them have similar interfaces and they can be copied easily with |
247 | 237 |
assignment operators and copy constructors. This makes it easy and |
248 | 238 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
249 | 239 |
any kind of path structure. |
250 | 240 |
|
251 | 241 |
\sa lemon::concepts::Path |
252 | 242 |
*/ |
253 | 243 |
|
254 | 244 |
/** |
255 | 245 |
@defgroup auxdat Auxiliary Data Structures |
256 | 246 |
@ingroup datas |
257 | 247 |
\brief Auxiliary data structures implemented in LEMON. |
258 | 248 |
|
259 | 249 |
This group contains some data structures implemented in LEMON in |
260 | 250 |
order to make it easier to implement combinatorial algorithms. |
261 | 251 |
*/ |
262 | 252 |
|
263 | 253 |
/** |
264 | 254 |
@defgroup algs Algorithms |
265 | 255 |
\brief This group contains the several algorithms |
266 | 256 |
implemented in LEMON. |
267 | 257 |
|
268 | 258 |
This group contains the several algorithms |
269 | 259 |
implemented in LEMON. |
270 | 260 |
*/ |
271 | 261 |
|
272 | 262 |
/** |
273 | 263 |
@defgroup search Graph Search |
274 | 264 |
@ingroup algs |
275 | 265 |
\brief Common graph search algorithms. |
276 | 266 |
|
277 | 267 |
This group contains the common graph search algorithms, namely |
278 | 268 |
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS). |
279 | 269 |
*/ |
280 | 270 |
|
281 | 271 |
/** |
282 | 272 |
@defgroup shortest_path Shortest Path Algorithms |
283 | 273 |
@ingroup algs |
284 | 274 |
\brief Algorithms for finding shortest paths. |
285 | 275 |
|
286 | 276 |
This group contains the algorithms for finding shortest paths in digraphs. |
287 | 277 |
|
288 | 278 |
- \ref Dijkstra Dijkstra's algorithm for finding shortest paths from a |
289 | 279 |
source node when all arc lengths are non-negative. |
290 | 280 |
- \ref Suurballe A successive shortest path algorithm for finding |
291 | 281 |
arc-disjoint paths between two nodes having minimum total length. |
292 | 282 |
*/ |
293 | 283 |
|
294 | 284 |
/** |
295 | 285 |
@defgroup max_flow Maximum Flow Algorithms |
296 | 286 |
@ingroup algs |
297 | 287 |
\brief Algorithms for finding maximum flows. |
298 | 288 |
|
299 | 289 |
This group contains the algorithms for finding maximum flows and |
300 | 290 |
feasible circulations. |
301 | 291 |
|
302 | 292 |
The \e maximum \e flow \e problem is to find a flow of maximum value between |
303 | 293 |
a single source and a single target. Formally, there is a \f$G=(V,A)\f$ |
304 | 294 |
digraph, a \f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function and |
305 | 295 |
\f$s, t \in V\f$ source and target nodes. |
306 | 296 |
A maximum flow is an \f$f: A\rightarrow\mathbf{R}^+_0\f$ solution of the |
307 | 297 |
following optimization problem. |
308 | 298 |
|
309 | 299 |
\f[ \max\sum_{sv\in A} f(sv) - \sum_{vs\in A} f(vs) \f] |
310 | 300 |
\f[ \sum_{uv\in A} f(uv) = \sum_{vu\in A} f(vu) |
311 | 301 |
\quad \forall u\in V\setminus\{s,t\} \f] |
312 | 302 |
\f[ 0 \leq f(uv) \leq cap(uv) \quad \forall uv\in A \f] |
313 | 303 |
|
314 | 304 |
\ref Preflow implements the preflow push-relabel algorithm of Goldberg and |
315 | 305 |
Tarjan for solving this problem. It also provides functions to query the |
316 | 306 |
minimum cut, which is the dual problem of maximum flow. |
317 | 307 |
|
308 |
|
|
318 | 309 |
\ref Circulation is a preflow push-relabel algorithm implemented directly |
319 | 310 |
for finding feasible circulations, which is a somewhat different problem, |
320 | 311 |
but it is strongly related to maximum flow. |
321 | 312 |
For more information, see \ref Circulation. |
322 | 313 |
*/ |
323 | 314 |
|
324 | 315 |
/** |
325 |
@defgroup |
|
316 |
@defgroup min_cost_flow_algs Minimum Cost Flow Algorithms |
|
326 | 317 |
@ingroup algs |
327 | 318 |
|
328 | 319 |
\brief Algorithms for finding minimum cost flows and circulations. |
329 | 320 |
|
330 | 321 |
This group contains the algorithms for finding minimum cost flows and |
331 |
circulations. |
|
332 |
|
|
333 |
The \e minimum \e cost \e flow \e problem is to find a feasible flow of |
|
334 |
minimum total cost from a set of supply nodes to a set of demand nodes |
|
335 |
in a network with capacity constraints (lower and upper bounds) |
|
336 |
and arc costs. |
|
337 |
Formally, let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{Z}\f$, |
|
338 |
\f$upper: A\rightarrow\mathbf{Z}\cup\{+\infty\}\f$ denote the lower and |
|
339 |
upper bounds for the flow values on the arcs, for which |
|
340 |
\f$lower(uv) \leq upper(uv)\f$ must hold for all \f$uv\in A\f$, |
|
341 |
\f$cost: A\rightarrow\mathbf{Z}\f$ denotes the cost per unit flow |
|
342 |
on the arcs and \f$sup: V\rightarrow\mathbf{Z}\f$ denotes the |
|
343 |
signed supply values of the nodes. |
|
344 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
|
345 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
|
346 |
\f$-sup(u)\f$ demand. |
|
347 |
A minimum cost flow is an \f$f: A\rightarrow\mathbf{Z}\f$ solution |
|
348 |
of the following optimization problem. |
|
349 |
|
|
350 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
|
351 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \geq |
|
352 |
sup(u) \quad \forall u\in V \f] |
|
353 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
|
354 |
|
|
355 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be |
|
356 |
zero or negative in order to have a feasible solution (since the sum |
|
357 |
of the expressions on the left-hand side of the inequalities is zero). |
|
358 |
It means that the total demand must be greater or equal to the total |
|
359 |
supply and all the supplies have to be carried out from the supply nodes, |
|
360 |
but there could be demands that are not satisfied. |
|
361 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand |
|
362 |
constraints have to be satisfied with equality, i.e. all demands |
|
363 |
have to be satisfied and all supplies have to be used. |
|
364 |
|
|
365 |
If you need the opposite inequalities in the supply/demand constraints |
|
366 |
(i.e. the total demand is less than the total supply and all the demands |
|
367 |
have to be satisfied while there could be supplies that are not used), |
|
368 |
then you could easily transform the problem to the above form by reversing |
|
369 |
the direction of the arcs and taking the negative of the supply values |
|
370 |
(e.g. using \ref ReverseDigraph and \ref NegMap adaptors). |
|
371 |
However \ref NetworkSimplex algorithm also supports this form directly |
|
372 |
for the sake of convenience. |
|
373 |
|
|
374 |
A feasible solution for this problem can be found using \ref Circulation. |
|
375 |
|
|
376 |
Note that the above formulation is actually more general than the usual |
|
377 |
definition of the minimum cost flow problem, in which strict equalities |
|
378 |
are required in the supply/demand contraints, i.e. |
|
379 |
|
|
380 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) = |
|
381 |
sup(u) \quad \forall u\in V. \f] |
|
382 |
|
|
383 |
However if the sum of the supply values is zero, then these two problems |
|
384 |
are equivalent. So if you need the equality form, you have to ensure this |
|
385 |
additional contraint for the algorithms. |
|
386 |
|
|
387 |
The dual solution of the minimum cost flow problem is represented by node |
|
388 |
potentials \f$\pi: V\rightarrow\mathbf{Z}\f$. |
|
389 |
An \f$f: A\rightarrow\mathbf{Z}\f$ feasible solution of the problem |
|
390 |
is optimal if and only if for some \f$\pi: V\rightarrow\mathbf{Z}\f$ |
|
391 |
node potentials the following \e complementary \e slackness optimality |
|
392 |
conditions hold. |
|
393 |
|
|
394 |
- For all \f$uv\in A\f$ arcs: |
|
395 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
|
396 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
|
397 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
|
398 |
- For all \f$u\in V\f$ nodes: |
|
399 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$, |
|
400 |
then \f$\pi(u)=0\f$. |
|
401 |
|
|
402 |
Here \f$cost^\pi(uv)\f$ denotes the \e reduced \e cost of the arc |
|
403 |
\f$uv\in A\f$ with respect to the potential function \f$\pi\f$, i.e. |
|
404 |
\f[ cost^\pi(uv) = cost(uv) + \pi(u) - \pi(v).\f] |
|
322 |
circulations. For more information about this problem and its dual |
|
323 |
solution see \ref min_cost_flow "Minimum Cost Flow Problem". |
|
405 | 324 |
|
406 | 325 |
\ref NetworkSimplex is an efficient implementation of the primal Network |
407 | 326 |
Simplex algorithm for finding minimum cost flows. It also provides dual |
408 | 327 |
solution (node potentials), if an optimal flow is found. |
409 | 328 |
*/ |
410 | 329 |
|
411 | 330 |
/** |
412 | 331 |
@defgroup min_cut Minimum Cut Algorithms |
413 | 332 |
@ingroup algs |
414 | 333 |
|
415 | 334 |
\brief Algorithms for finding minimum cut in graphs. |
416 | 335 |
|
417 | 336 |
This group contains the algorithms for finding minimum cut in graphs. |
418 | 337 |
|
419 | 338 |
The \e minimum \e cut \e problem is to find a non-empty and non-complete |
420 | 339 |
\f$X\f$ subset of the nodes with minimum overall capacity on |
421 | 340 |
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a |
422 | 341 |
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum |
423 | 342 |
cut is the \f$X\f$ solution of the next optimization problem: |
424 | 343 |
|
425 | 344 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}} |
426 | 345 |
\sum_{uv\in A, u\in X, v\not\in X}cap(uv) \f] |
427 | 346 |
|
428 | 347 |
LEMON contains several algorithms related to minimum cut problems: |
429 | 348 |
|
430 | 349 |
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut |
431 | 350 |
in directed graphs. |
432 | 351 |
- \ref GomoryHu "Gomory-Hu tree computation" for calculating |
433 | 352 |
all-pairs minimum cut in undirected graphs. |
434 | 353 |
|
435 | 354 |
If you want to find minimum cut just between two distinict nodes, |
436 | 355 |
see the \ref max_flow "maximum flow problem". |
437 | 356 |
*/ |
438 | 357 |
|
439 | 358 |
/** |
440 | 359 |
@defgroup graph_properties Connectivity and Other Graph Properties |
441 | 360 |
@ingroup algs |
442 | 361 |
\brief Algorithms for discovering the graph properties |
443 | 362 |
|
444 | 363 |
This group contains the algorithms for discovering the graph properties |
445 | 364 |
like connectivity, bipartiteness, euler property, simplicity etc. |
446 | 365 |
|
447 | 366 |
\image html edge_biconnected_components.png |
448 | 367 |
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
449 | 368 |
*/ |
450 | 369 |
|
451 | 370 |
/** |
452 | 371 |
@defgroup matching Matching Algorithms |
453 | 372 |
@ingroup algs |
454 | 373 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
455 | 374 |
|
456 | 375 |
This group contains the algorithms for calculating matchings in graphs. |
457 | 376 |
The general matching problem is finding a subset of the edges for which |
458 | 377 |
each node has at most one incident edge. |
459 | 378 |
|
460 | 379 |
There are several different algorithms for calculate matchings in |
461 | 380 |
graphs. The goal of the matching optimization |
462 | 381 |
can be finding maximum cardinality, maximum weight or minimum cost |
463 | 382 |
matching. The search can be constrained to find perfect or |
464 | 383 |
maximum cardinality matching. |
465 | 384 |
|
466 | 385 |
The matching algorithms implemented in LEMON: |
467 | 386 |
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating |
468 | 387 |
maximum cardinality matching in general graphs. |
469 | 388 |
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating |
470 | 389 |
maximum weighted matching in general graphs. |
471 | 390 |
- \ref MaxWeightedPerfectMatching |
472 | 391 |
Edmond's blossom shrinking algorithm for calculating maximum weighted |
473 | 392 |
perfect matching in general graphs. |
474 | 393 |
|
475 | 394 |
\image html bipartite_matching.png |
476 | 395 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
477 | 396 |
*/ |
478 | 397 |
|
479 | 398 |
/** |
480 | 399 |
@defgroup spantree Minimum Spanning Tree Algorithms |
481 | 400 |
@ingroup algs |
482 |
\brief Algorithms for finding |
|
401 |
\brief Algorithms for finding minimum cost spanning trees and arborescences. |
|
483 | 402 |
|
484 |
This group contains the algorithms for finding a minimum cost spanning |
|
485 |
tree in a graph. |
|
403 |
This group contains the algorithms for finding minimum cost spanning |
|
404 |
trees and arborescences. |
|
486 | 405 |
*/ |
487 | 406 |
|
488 | 407 |
/** |
489 | 408 |
@defgroup auxalg Auxiliary Algorithms |
490 | 409 |
@ingroup algs |
491 | 410 |
\brief Auxiliary algorithms implemented in LEMON. |
492 | 411 |
|
493 | 412 |
This group contains some algorithms implemented in LEMON |
494 | 413 |
in order to make it easier to implement complex algorithms. |
495 | 414 |
*/ |
496 | 415 |
|
497 | 416 |
/** |
498 | 417 |
@defgroup gen_opt_group General Optimization Tools |
499 | 418 |
\brief This group contains some general optimization frameworks |
500 | 419 |
implemented in LEMON. |
501 | 420 |
|
502 | 421 |
This group contains some general optimization frameworks |
503 | 422 |
implemented in LEMON. |
504 | 423 |
*/ |
505 | 424 |
|
506 | 425 |
/** |
507 | 426 |
@defgroup lp_group Lp and Mip Solvers |
508 | 427 |
@ingroup gen_opt_group |
509 | 428 |
\brief Lp and Mip solver interfaces for LEMON. |
510 | 429 |
|
511 | 430 |
This group contains Lp and Mip solver interfaces for LEMON. The |
512 | 431 |
various LP solvers could be used in the same manner with this |
513 | 432 |
interface. |
514 | 433 |
*/ |
515 | 434 |
|
516 | 435 |
/** |
517 | 436 |
@defgroup utils Tools and Utilities |
518 | 437 |
\brief Tools and utilities for programming in LEMON |
519 | 438 |
|
520 | 439 |
Tools and utilities for programming in LEMON. |
521 | 440 |
*/ |
522 | 441 |
|
523 | 442 |
/** |
524 | 443 |
@defgroup gutils Basic Graph Utilities |
525 | 444 |
@ingroup utils |
526 | 445 |
\brief Simple basic graph utilities. |
527 | 446 |
|
528 | 447 |
This group contains some simple basic graph utilities. |
529 | 448 |
*/ |
530 | 449 |
|
531 | 450 |
/** |
532 | 451 |
@defgroup misc Miscellaneous Tools |
533 | 452 |
@ingroup utils |
534 | 453 |
\brief Tools for development, debugging and testing. |
535 | 454 |
|
536 | 455 |
This group contains several useful tools for development, |
537 | 456 |
debugging and testing. |
538 | 457 |
*/ |
539 | 458 |
|
540 | 459 |
/** |
541 | 460 |
@defgroup timecount Time Measuring and Counting |
542 | 461 |
@ingroup misc |
543 | 462 |
\brief Simple tools for measuring the performance of algorithms. |
544 | 463 |
|
545 | 464 |
This group contains simple tools for measuring the performance |
546 | 465 |
of algorithms. |
547 | 466 |
*/ |
548 | 467 |
|
549 | 468 |
/** |
550 | 469 |
@defgroup exceptions Exceptions |
551 | 470 |
@ingroup utils |
552 | 471 |
\brief Exceptions defined in LEMON. |
553 | 472 |
|
554 | 473 |
This group contains the exceptions defined in LEMON. |
555 | 474 |
*/ |
556 | 475 |
|
557 | 476 |
/** |
558 | 477 |
@defgroup io_group Input-Output |
559 | 478 |
\brief Graph Input-Output methods |
560 | 479 |
|
561 | 480 |
This group contains the tools for importing and exporting graphs |
562 | 481 |
and graph related data. Now it supports the \ref lgf-format |
563 | 482 |
"LEMON Graph Format", the \c DIMACS format and the encapsulated |
564 | 483 |
postscript (EPS) format. |
565 | 484 |
*/ |
566 | 485 |
|
567 | 486 |
/** |
568 | 487 |
@defgroup lemon_io LEMON Graph Format |
569 | 488 |
@ingroup io_group |
570 | 489 |
\brief Reading and writing LEMON Graph Format. |
571 | 490 |
|
572 | 491 |
This group contains methods for reading and writing |
573 | 492 |
\ref lgf-format "LEMON Graph Format". |
574 | 493 |
*/ |
575 | 494 |
|
576 | 495 |
/** |
577 | 496 |
@defgroup eps_io Postscript Exporting |
578 | 497 |
@ingroup io_group |
579 | 498 |
\brief General \c EPS drawer and graph exporter |
580 | 499 |
|
581 | 500 |
This group contains general \c EPS drawing methods and special |
582 | 501 |
graph exporting tools. |
583 | 502 |
*/ |
584 | 503 |
|
585 | 504 |
/** |
586 | 505 |
@defgroup dimacs_group DIMACS format |
587 | 506 |
@ingroup io_group |
588 | 507 |
\brief Read and write files in DIMACS format |
589 | 508 |
|
590 | 509 |
Tools to read a digraph from or write it to a file in DIMACS format data. |
591 | 510 |
*/ |
592 | 511 |
|
593 | 512 |
/** |
594 | 513 |
@defgroup nauty_group NAUTY Format |
595 | 514 |
@ingroup io_group |
596 | 515 |
\brief Read \e Nauty format |
597 | 516 |
|
598 | 517 |
Tool to read graphs from \e Nauty format data. |
599 | 518 |
*/ |
600 | 519 |
|
601 | 520 |
/** |
602 | 521 |
@defgroup concept Concepts |
603 | 522 |
\brief Skeleton classes and concept checking classes |
604 | 523 |
|
605 | 524 |
This group contains the data/algorithm skeletons and concept checking |
606 | 525 |
classes implemented in LEMON. |
607 | 526 |
|
608 | 527 |
The purpose of the classes in this group is fourfold. |
609 | 528 |
|
610 | 529 |
- These classes contain the documentations of the %concepts. In order |
611 | 530 |
to avoid document multiplications, an implementation of a concept |
612 | 531 |
simply refers to the corresponding concept class. |
613 | 532 |
|
614 | 533 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
615 | 534 |
implementation of the %concepts should provide, however completely |
616 | 535 |
without implementations and real data structures behind the |
617 | 536 |
interface. On the other hand they should provide nothing else. All |
618 | 537 |
the algorithms working on a data structure meeting a certain concept |
619 | 538 |
should compile with these classes. (Though it will not run properly, |
620 | 539 |
of course.) In this way it is easily to check if an algorithm |
621 | 540 |
doesn't use any extra feature of a certain implementation. |
622 | 541 |
|
623 | 542 |
- The concept descriptor classes also provide a <em>checker class</em> |
624 | 543 |
that makes it possible to check whether a certain implementation of a |
625 | 544 |
concept indeed provides all the required features. |
626 | 545 |
|
627 | 546 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
628 | 547 |
*/ |
629 | 548 |
|
630 | 549 |
/** |
631 | 550 |
@defgroup graph_concepts Graph Structure Concepts |
632 | 551 |
@ingroup concept |
633 | 552 |
\brief Skeleton and concept checking classes for graph structures |
634 | 553 |
|
635 | 554 |
This group contains the skeletons and concept checking classes of LEMON's |
636 | 555 |
graph structures and helper classes used to implement these. |
637 | 556 |
*/ |
638 | 557 |
|
639 | 558 |
/** |
640 | 559 |
@defgroup map_concepts Map Concepts |
641 | 560 |
@ingroup concept |
642 | 561 |
\brief Skeleton and concept checking classes for maps |
643 | 562 |
|
644 | 563 |
This group contains the skeletons and concept checking classes of maps. |
645 | 564 |
*/ |
646 | 565 |
|
647 | 566 |
/** |
648 | 567 |
\anchor demoprograms |
649 | 568 |
|
650 | 569 |
@defgroup demos Demo Programs |
651 | 570 |
|
652 | 571 |
Some demo programs are listed here. Their full source codes can be found in |
653 | 572 |
the \c demo subdirectory of the source tree. |
654 | 573 |
|
655 | 574 |
In order to compile them, use the <tt>make demo</tt> or the |
656 | 575 |
<tt>make check</tt> commands. |
657 | 576 |
*/ |
658 | 577 |
|
659 | 578 |
/** |
660 | 579 |
@defgroup tools Standalone Utility Applications |
661 | 580 |
|
662 | 581 |
Some utility applications are listed here. |
663 | 582 |
|
664 | 583 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
665 | 584 |
them, as well. |
666 | 585 |
*/ |
667 | 586 |
|
668 | 587 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
/** |
20 | 20 |
\mainpage LEMON Documentation |
21 | 21 |
|
22 | 22 |
\section intro Introduction |
23 | 23 |
|
24 | 24 |
\subsection whatis What is LEMON |
25 | 25 |
|
26 |
LEMON stands for |
|
27 |
<b>L</b>ibrary of <b>E</b>fficient <b>M</b>odels |
|
26 |
LEMON stands for <b>L</b>ibrary for <b>E</b>fficient <b>M</b>odeling |
|
28 | 27 |
and <b>O</b>ptimization in <b>N</b>etworks. |
29 | 28 |
It is a C++ template |
30 | 29 |
library aimed at combinatorial optimization tasks which |
31 | 30 |
often involve in working |
32 | 31 |
with graphs. |
33 | 32 |
|
34 | 33 |
<b> |
35 | 34 |
LEMON is an <a class="el" href="http://opensource.org/">open source</a> |
36 | 35 |
project. |
37 | 36 |
You are free to use it in your commercial or |
38 | 37 |
non-commercial applications under very permissive |
39 | 38 |
\ref license "license terms". |
40 | 39 |
</b> |
41 | 40 |
|
42 | 41 |
\subsection howtoread How to read the documentation |
43 | 42 |
|
44 |
If you want to get a quick start and see the most important features then |
|
45 |
take a look at our \ref quicktour |
|
46 |
"Quick Tour to LEMON" which will guide you along. |
|
47 |
|
|
48 |
If you |
|
43 |
If you would like to get to know the library, see |
|
49 | 44 |
<a class="el" href="http://lemon.cs.elte.hu/pub/tutorial/">LEMON Tutorial</a>. |
50 | 45 |
|
51 |
If you know what you are looking for then try to find it under the |
|
46 |
If you know what you are looking for, then try to find it under the |
|
52 | 47 |
<a class="el" href="modules.html">Modules</a> section. |
53 | 48 |
|
54 | 49 |
If you are a user of the old (0.x) series of LEMON, please check out the |
55 | 50 |
\ref migration "Migration Guide" for the backward incompatibilities. |
56 | 51 |
*/ |
1 | 1 |
EXTRA_DIST += \ |
2 | 2 |
lemon/lemon.pc.in \ |
3 | 3 |
lemon/CMakeLists.txt |
4 | 4 |
|
5 | 5 |
pkgconfig_DATA += lemon/lemon.pc |
6 | 6 |
|
7 | 7 |
lib_LTLIBRARIES += lemon/libemon.la |
8 | 8 |
|
9 | 9 |
lemon_libemon_la_SOURCES = \ |
10 | 10 |
lemon/arg_parser.cc \ |
11 | 11 |
lemon/base.cc \ |
12 | 12 |
lemon/color.cc \ |
13 | 13 |
lemon/lp_base.cc \ |
14 | 14 |
lemon/lp_skeleton.cc \ |
15 | 15 |
lemon/random.cc \ |
16 | 16 |
lemon/bits/windows.cc |
17 | 17 |
|
18 | 18 |
nodist_lemon_HEADERS = lemon/config.h |
19 | 19 |
|
20 | 20 |
lemon_libemon_la_CXXFLAGS = \ |
21 | 21 |
$(AM_CXXFLAGS) \ |
22 | 22 |
$(GLPK_CFLAGS) \ |
23 | 23 |
$(CPLEX_CFLAGS) \ |
24 | 24 |
$(SOPLEX_CXXFLAGS) \ |
25 | 25 |
$(CLP_CXXFLAGS) \ |
26 | 26 |
$(CBC_CXXFLAGS) |
27 | 27 |
|
28 | 28 |
lemon_libemon_la_LDFLAGS = \ |
29 | 29 |
$(GLPK_LIBS) \ |
30 | 30 |
$(CPLEX_LIBS) \ |
31 | 31 |
$(SOPLEX_LIBS) \ |
32 | 32 |
$(CLP_LIBS) \ |
33 | 33 |
$(CBC_LIBS) |
34 | 34 |
|
35 | 35 |
if HAVE_GLPK |
36 | 36 |
lemon_libemon_la_SOURCES += lemon/glpk.cc |
37 | 37 |
endif |
38 | 38 |
|
39 | 39 |
if HAVE_CPLEX |
40 | 40 |
lemon_libemon_la_SOURCES += lemon/cplex.cc |
41 | 41 |
endif |
42 | 42 |
|
43 | 43 |
if HAVE_SOPLEX |
44 | 44 |
lemon_libemon_la_SOURCES += lemon/soplex.cc |
45 | 45 |
endif |
46 | 46 |
|
47 | 47 |
if HAVE_CLP |
48 | 48 |
lemon_libemon_la_SOURCES += lemon/clp.cc |
49 | 49 |
endif |
50 | 50 |
|
51 | 51 |
if HAVE_CBC |
52 | 52 |
lemon_libemon_la_SOURCES += lemon/cbc.cc |
53 | 53 |
endif |
54 | 54 |
|
55 | 55 |
lemon_HEADERS += \ |
56 | 56 |
lemon/adaptors.h \ |
57 | 57 |
lemon/arg_parser.h \ |
58 | 58 |
lemon/assert.h \ |
59 | 59 |
lemon/bfs.h \ |
60 | 60 |
lemon/bin_heap.h \ |
61 | 61 |
lemon/cbc.h \ |
62 | 62 |
lemon/circulation.h \ |
63 | 63 |
lemon/clp.h \ |
64 | 64 |
lemon/color.h \ |
65 | 65 |
lemon/concept_check.h \ |
66 | 66 |
lemon/connectivity.h \ |
67 | 67 |
lemon/counter.h \ |
68 | 68 |
lemon/core.h \ |
69 | 69 |
lemon/cplex.h \ |
70 | 70 |
lemon/dfs.h \ |
71 | 71 |
lemon/dijkstra.h \ |
72 | 72 |
lemon/dim2.h \ |
73 | 73 |
lemon/dimacs.h \ |
74 | 74 |
lemon/edge_set.h \ |
75 | 75 |
lemon/elevator.h \ |
76 | 76 |
lemon/error.h \ |
77 | 77 |
lemon/euler.h \ |
78 | 78 |
lemon/full_graph.h \ |
79 | 79 |
lemon/glpk.h \ |
80 | 80 |
lemon/gomory_hu.h \ |
81 | 81 |
lemon/graph_to_eps.h \ |
82 | 82 |
lemon/grid_graph.h \ |
83 | 83 |
lemon/hypercube_graph.h \ |
84 | 84 |
lemon/kruskal.h \ |
85 | 85 |
lemon/hao_orlin.h \ |
86 | 86 |
lemon/lgf_reader.h \ |
87 | 87 |
lemon/lgf_writer.h \ |
88 | 88 |
lemon/list_graph.h \ |
89 | 89 |
lemon/lp.h \ |
90 | 90 |
lemon/lp_base.h \ |
91 | 91 |
lemon/lp_skeleton.h \ |
92 | 92 |
lemon/list_graph.h \ |
93 | 93 |
lemon/maps.h \ |
94 | 94 |
lemon/matching.h \ |
95 | 95 |
lemon/math.h \ |
96 | 96 |
lemon/min_cost_arborescence.h \ |
97 | 97 |
lemon/nauty_reader.h \ |
98 | 98 |
lemon/network_simplex.h \ |
99 | 99 |
lemon/path.h \ |
100 | 100 |
lemon/preflow.h \ |
101 | 101 |
lemon/radix_sort.h \ |
102 | 102 |
lemon/random.h \ |
103 | 103 |
lemon/smart_graph.h \ |
104 | 104 |
lemon/soplex.h \ |
105 | 105 |
lemon/suurballe.h \ |
106 | 106 |
lemon/time_measure.h \ |
107 | 107 |
lemon/tolerance.h \ |
108 | 108 |
lemon/unionfind.h \ |
109 | 109 |
lemon/bits/windows.h |
110 | 110 |
|
111 | 111 |
bits_HEADERS += \ |
112 | 112 |
lemon/bits/alteration_notifier.h \ |
113 | 113 |
lemon/bits/array_map.h \ |
114 |
lemon/bits/base_extender.h \ |
|
115 | 114 |
lemon/bits/bezier.h \ |
116 | 115 |
lemon/bits/default_map.h \ |
117 | 116 |
lemon/bits/edge_set_extender.h \ |
118 | 117 |
lemon/bits/enable_if.h \ |
119 | 118 |
lemon/bits/graph_adaptor_extender.h \ |
120 | 119 |
lemon/bits/graph_extender.h \ |
121 | 120 |
lemon/bits/map_extender.h \ |
122 | 121 |
lemon/bits/path_dump.h \ |
123 | 122 |
lemon/bits/solver_bits.h \ |
124 | 123 |
lemon/bits/traits.h \ |
125 | 124 |
lemon/bits/variant.h \ |
126 | 125 |
lemon/bits/vector_map.h |
127 | 126 |
|
128 | 127 |
concept_HEADERS += \ |
129 | 128 |
lemon/concepts/digraph.h \ |
130 | 129 |
lemon/concepts/graph.h \ |
131 | 130 |
lemon/concepts/graph_components.h \ |
132 | 131 |
lemon/concepts/heap.h \ |
133 | 132 |
lemon/concepts/maps.h \ |
134 | 133 |
lemon/concepts/path.h |
... | ... |
@@ -1074,1682 +1074,1679 @@ |
1074 | 1074 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
1075 | 1075 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> { |
1076 | 1076 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
1077 | 1077 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
1078 | 1078 |
|
1079 | 1079 |
public: |
1080 | 1080 |
typedef V Value; |
1081 | 1081 |
|
1082 | 1082 |
EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
1083 | 1083 |
: Parent(adaptor) {} |
1084 | 1084 |
|
1085 | 1085 |
EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
1086 | 1086 |
: Parent(adaptor, value) {} |
1087 | 1087 |
|
1088 | 1088 |
private: |
1089 | 1089 |
EdgeMap& operator=(const EdgeMap& cmap) { |
1090 | 1090 |
return operator=<EdgeMap>(cmap); |
1091 | 1091 |
} |
1092 | 1092 |
|
1093 | 1093 |
template <typename CMap> |
1094 | 1094 |
EdgeMap& operator=(const CMap& cmap) { |
1095 | 1095 |
Parent::operator=(cmap); |
1096 | 1096 |
return *this; |
1097 | 1097 |
} |
1098 | 1098 |
}; |
1099 | 1099 |
|
1100 | 1100 |
}; |
1101 | 1101 |
|
1102 | 1102 |
template <typename GR, typename NF, typename EF> |
1103 | 1103 |
class SubGraphBase<GR, NF, EF, false> |
1104 | 1104 |
: public GraphAdaptorBase<GR> { |
1105 | 1105 |
typedef GraphAdaptorBase<GR> Parent; |
1106 | 1106 |
public: |
1107 | 1107 |
typedef GR Graph; |
1108 | 1108 |
typedef NF NodeFilterMap; |
1109 | 1109 |
typedef EF EdgeFilterMap; |
1110 | 1110 |
|
1111 | 1111 |
typedef SubGraphBase Adaptor; |
1112 | 1112 |
protected: |
1113 | 1113 |
NF* _node_filter; |
1114 | 1114 |
EF* _edge_filter; |
1115 | 1115 |
SubGraphBase() |
1116 | 1116 |
: Parent(), _node_filter(0), _edge_filter(0) { } |
1117 | 1117 |
|
1118 | 1118 |
void initialize(GR& graph, NF& node_filter, EF& edge_filter) { |
1119 | 1119 |
Parent::initialize(graph); |
1120 | 1120 |
_node_filter = &node_filter; |
1121 | 1121 |
_edge_filter = &edge_filter; |
1122 | 1122 |
} |
1123 | 1123 |
|
1124 | 1124 |
public: |
1125 | 1125 |
|
1126 | 1126 |
typedef typename Parent::Node Node; |
1127 | 1127 |
typedef typename Parent::Arc Arc; |
1128 | 1128 |
typedef typename Parent::Edge Edge; |
1129 | 1129 |
|
1130 | 1130 |
void first(Node& i) const { |
1131 | 1131 |
Parent::first(i); |
1132 | 1132 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
1133 | 1133 |
} |
1134 | 1134 |
|
1135 | 1135 |
void first(Arc& i) const { |
1136 | 1136 |
Parent::first(i); |
1137 | 1137 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
1138 | 1138 |
} |
1139 | 1139 |
|
1140 | 1140 |
void first(Edge& i) const { |
1141 | 1141 |
Parent::first(i); |
1142 | 1142 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
1143 | 1143 |
} |
1144 | 1144 |
|
1145 | 1145 |
void firstIn(Arc& i, const Node& n) const { |
1146 | 1146 |
Parent::firstIn(i, n); |
1147 | 1147 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i); |
1148 | 1148 |
} |
1149 | 1149 |
|
1150 | 1150 |
void firstOut(Arc& i, const Node& n) const { |
1151 | 1151 |
Parent::firstOut(i, n); |
1152 | 1152 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i); |
1153 | 1153 |
} |
1154 | 1154 |
|
1155 | 1155 |
void firstInc(Edge& i, bool& d, const Node& n) const { |
1156 | 1156 |
Parent::firstInc(i, d, n); |
1157 | 1157 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d); |
1158 | 1158 |
} |
1159 | 1159 |
|
1160 | 1160 |
void next(Node& i) const { |
1161 | 1161 |
Parent::next(i); |
1162 | 1162 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
1163 | 1163 |
} |
1164 | 1164 |
void next(Arc& i) const { |
1165 | 1165 |
Parent::next(i); |
1166 | 1166 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
1167 | 1167 |
} |
1168 | 1168 |
void next(Edge& i) const { |
1169 | 1169 |
Parent::next(i); |
1170 | 1170 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
1171 | 1171 |
} |
1172 | 1172 |
void nextIn(Arc& i) const { |
1173 | 1173 |
Parent::nextIn(i); |
1174 | 1174 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i); |
1175 | 1175 |
} |
1176 | 1176 |
|
1177 | 1177 |
void nextOut(Arc& i) const { |
1178 | 1178 |
Parent::nextOut(i); |
1179 | 1179 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i); |
1180 | 1180 |
} |
1181 | 1181 |
void nextInc(Edge& i, bool& d) const { |
1182 | 1182 |
Parent::nextInc(i, d); |
1183 | 1183 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d); |
1184 | 1184 |
} |
1185 | 1185 |
|
1186 | 1186 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
1187 | 1187 |
void status(const Edge& e, bool v) const { _edge_filter->set(e, v); } |
1188 | 1188 |
|
1189 | 1189 |
bool status(const Node& n) const { return (*_node_filter)[n]; } |
1190 | 1190 |
bool status(const Edge& e) const { return (*_edge_filter)[e]; } |
1191 | 1191 |
|
1192 | 1192 |
typedef False NodeNumTag; |
1193 | 1193 |
typedef False ArcNumTag; |
1194 | 1194 |
typedef False EdgeNumTag; |
1195 | 1195 |
|
1196 | 1196 |
typedef FindArcTagIndicator<Graph> FindArcTag; |
1197 | 1197 |
Arc findArc(const Node& u, const Node& v, |
1198 | 1198 |
const Arc& prev = INVALID) const { |
1199 | 1199 |
Arc arc = Parent::findArc(u, v, prev); |
1200 | 1200 |
while (arc != INVALID && !(*_edge_filter)[arc]) { |
1201 | 1201 |
arc = Parent::findArc(u, v, arc); |
1202 | 1202 |
} |
1203 | 1203 |
return arc; |
1204 | 1204 |
} |
1205 | 1205 |
|
1206 | 1206 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
1207 | 1207 |
Edge findEdge(const Node& u, const Node& v, |
1208 | 1208 |
const Edge& prev = INVALID) const { |
1209 | 1209 |
Edge edge = Parent::findEdge(u, v, prev); |
1210 | 1210 |
while (edge != INVALID && !(*_edge_filter)[edge]) { |
1211 | 1211 |
edge = Parent::findEdge(u, v, edge); |
1212 | 1212 |
} |
1213 | 1213 |
return edge; |
1214 | 1214 |
} |
1215 | 1215 |
|
1216 | 1216 |
template <typename V> |
1217 | 1217 |
class NodeMap |
1218 | 1218 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1219 | 1219 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> { |
1220 | 1220 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1221 | 1221 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
1222 | 1222 |
|
1223 | 1223 |
public: |
1224 | 1224 |
typedef V Value; |
1225 | 1225 |
|
1226 | 1226 |
NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
1227 | 1227 |
: Parent(adaptor) {} |
1228 | 1228 |
NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
1229 | 1229 |
: Parent(adaptor, value) {} |
1230 | 1230 |
|
1231 | 1231 |
private: |
1232 | 1232 |
NodeMap& operator=(const NodeMap& cmap) { |
1233 | 1233 |
return operator=<NodeMap>(cmap); |
1234 | 1234 |
} |
1235 | 1235 |
|
1236 | 1236 |
template <typename CMap> |
1237 | 1237 |
NodeMap& operator=(const CMap& cmap) { |
1238 | 1238 |
Parent::operator=(cmap); |
1239 | 1239 |
return *this; |
1240 | 1240 |
} |
1241 | 1241 |
}; |
1242 | 1242 |
|
1243 | 1243 |
template <typename V> |
1244 | 1244 |
class ArcMap |
1245 | 1245 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1246 | 1246 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> { |
1247 | 1247 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1248 | 1248 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
1249 | 1249 |
|
1250 | 1250 |
public: |
1251 | 1251 |
typedef V Value; |
1252 | 1252 |
|
1253 | 1253 |
ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
1254 | 1254 |
: Parent(adaptor) {} |
1255 | 1255 |
ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
1256 | 1256 |
: Parent(adaptor, value) {} |
1257 | 1257 |
|
1258 | 1258 |
private: |
1259 | 1259 |
ArcMap& operator=(const ArcMap& cmap) { |
1260 | 1260 |
return operator=<ArcMap>(cmap); |
1261 | 1261 |
} |
1262 | 1262 |
|
1263 | 1263 |
template <typename CMap> |
1264 | 1264 |
ArcMap& operator=(const CMap& cmap) { |
1265 | 1265 |
Parent::operator=(cmap); |
1266 | 1266 |
return *this; |
1267 | 1267 |
} |
1268 | 1268 |
}; |
1269 | 1269 |
|
1270 | 1270 |
template <typename V> |
1271 | 1271 |
class EdgeMap |
1272 | 1272 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1273 | 1273 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> { |
1274 | 1274 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1275 | 1275 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
1276 | 1276 |
|
1277 | 1277 |
public: |
1278 | 1278 |
typedef V Value; |
1279 | 1279 |
|
1280 | 1280 |
EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
1281 | 1281 |
: Parent(adaptor) {} |
1282 | 1282 |
|
1283 | 1283 |
EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
1284 | 1284 |
: Parent(adaptor, value) {} |
1285 | 1285 |
|
1286 | 1286 |
private: |
1287 | 1287 |
EdgeMap& operator=(const EdgeMap& cmap) { |
1288 | 1288 |
return operator=<EdgeMap>(cmap); |
1289 | 1289 |
} |
1290 | 1290 |
|
1291 | 1291 |
template <typename CMap> |
1292 | 1292 |
EdgeMap& operator=(const CMap& cmap) { |
1293 | 1293 |
Parent::operator=(cmap); |
1294 | 1294 |
return *this; |
1295 | 1295 |
} |
1296 | 1296 |
}; |
1297 | 1297 |
|
1298 | 1298 |
}; |
1299 | 1299 |
|
1300 | 1300 |
/// \ingroup graph_adaptors |
1301 | 1301 |
/// |
1302 | 1302 |
/// \brief Adaptor class for hiding nodes and edges in an undirected |
1303 | 1303 |
/// graph. |
1304 | 1304 |
/// |
1305 | 1305 |
/// SubGraph can be used for hiding nodes and edges in a graph. |
1306 | 1306 |
/// A \c bool node map and a \c bool edge map must be specified, which |
1307 | 1307 |
/// define the filters for nodes and edges. |
1308 | 1308 |
/// Only the nodes and edges with \c true filter value are |
1309 | 1309 |
/// shown in the subgraph. The edges that are incident to hidden |
1310 | 1310 |
/// nodes are also filtered out. |
1311 | 1311 |
/// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
1312 | 1312 |
/// |
1313 | 1313 |
/// The adapted graph can also be modified through this adaptor |
1314 | 1314 |
/// by adding or removing nodes or edges, unless the \c GR template |
1315 | 1315 |
/// parameter is set to be \c const. |
1316 | 1316 |
/// |
1317 | 1317 |
/// \tparam GR The type of the adapted graph. |
1318 | 1318 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
1319 | 1319 |
/// It can also be specified to be \c const. |
1320 | 1320 |
/// \tparam NF The type of the node filter map. |
1321 | 1321 |
/// It must be a \c bool (or convertible) node map of the |
1322 | 1322 |
/// adapted graph. The default type is |
1323 | 1323 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
1324 | 1324 |
/// \tparam EF The type of the edge filter map. |
1325 | 1325 |
/// It must be a \c bool (or convertible) edge map of the |
1326 | 1326 |
/// adapted graph. The default type is |
1327 | 1327 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
1328 | 1328 |
/// |
1329 | 1329 |
/// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
1330 | 1330 |
/// adapted graph are convertible to each other. |
1331 | 1331 |
/// |
1332 | 1332 |
/// \see FilterNodes |
1333 | 1333 |
/// \see FilterEdges |
1334 | 1334 |
#ifdef DOXYGEN |
1335 | 1335 |
template<typename GR, typename NF, typename EF> |
1336 | 1336 |
class SubGraph { |
1337 | 1337 |
#else |
1338 | 1338 |
template<typename GR, |
1339 | 1339 |
typename NF = typename GR::template NodeMap<bool>, |
1340 | 1340 |
typename EF = typename GR::template EdgeMap<bool> > |
1341 | 1341 |
class SubGraph : |
1342 | 1342 |
public GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > { |
1343 | 1343 |
#endif |
1344 | 1344 |
public: |
1345 | 1345 |
/// The type of the adapted graph. |
1346 | 1346 |
typedef GR Graph; |
1347 | 1347 |
/// The type of the node filter map. |
1348 | 1348 |
typedef NF NodeFilterMap; |
1349 | 1349 |
/// The type of the edge filter map. |
1350 | 1350 |
typedef EF EdgeFilterMap; |
1351 | 1351 |
|
1352 | 1352 |
typedef GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > |
1353 | 1353 |
Parent; |
1354 | 1354 |
|
1355 | 1355 |
typedef typename Parent::Node Node; |
1356 | 1356 |
typedef typename Parent::Edge Edge; |
1357 | 1357 |
|
1358 | 1358 |
protected: |
1359 | 1359 |
SubGraph() { } |
1360 | 1360 |
public: |
1361 | 1361 |
|
1362 | 1362 |
/// \brief Constructor |
1363 | 1363 |
/// |
1364 | 1364 |
/// Creates a subgraph for the given graph with the given node |
1365 | 1365 |
/// and edge filter maps. |
1366 | 1366 |
SubGraph(GR& graph, NF& node_filter, EF& edge_filter) { |
1367 | 1367 |
initialize(graph, node_filter, edge_filter); |
1368 | 1368 |
} |
1369 | 1369 |
|
1370 | 1370 |
/// \brief Sets the status of the given node |
1371 | 1371 |
/// |
1372 | 1372 |
/// This function sets the status of the given node. |
1373 | 1373 |
/// It is done by simply setting the assigned value of \c n |
1374 | 1374 |
/// to \c v in the node filter map. |
1375 | 1375 |
void status(const Node& n, bool v) const { Parent::status(n, v); } |
1376 | 1376 |
|
1377 | 1377 |
/// \brief Sets the status of the given edge |
1378 | 1378 |
/// |
1379 | 1379 |
/// This function sets the status of the given edge. |
1380 | 1380 |
/// It is done by simply setting the assigned value of \c e |
1381 | 1381 |
/// to \c v in the edge filter map. |
1382 | 1382 |
void status(const Edge& e, bool v) const { Parent::status(e, v); } |
1383 | 1383 |
|
1384 | 1384 |
/// \brief Returns the status of the given node |
1385 | 1385 |
/// |
1386 | 1386 |
/// This function returns the status of the given node. |
1387 | 1387 |
/// It is \c true if the given node is enabled (i.e. not hidden). |
1388 | 1388 |
bool status(const Node& n) const { return Parent::status(n); } |
1389 | 1389 |
|
1390 | 1390 |
/// \brief Returns the status of the given edge |
1391 | 1391 |
/// |
1392 | 1392 |
/// This function returns the status of the given edge. |
1393 | 1393 |
/// It is \c true if the given edge is enabled (i.e. not hidden). |
1394 | 1394 |
bool status(const Edge& e) const { return Parent::status(e); } |
1395 | 1395 |
|
1396 | 1396 |
/// \brief Disables the given node |
1397 | 1397 |
/// |
1398 | 1398 |
/// This function disables the given node in the subdigraph, |
1399 | 1399 |
/// so the iteration jumps over it. |
1400 | 1400 |
/// It is the same as \ref status() "status(n, false)". |
1401 | 1401 |
void disable(const Node& n) const { Parent::status(n, false); } |
1402 | 1402 |
|
1403 | 1403 |
/// \brief Disables the given edge |
1404 | 1404 |
/// |
1405 | 1405 |
/// This function disables the given edge in the subgraph, |
1406 | 1406 |
/// so the iteration jumps over it. |
1407 | 1407 |
/// It is the same as \ref status() "status(e, false)". |
1408 | 1408 |
void disable(const Edge& e) const { Parent::status(e, false); } |
1409 | 1409 |
|
1410 | 1410 |
/// \brief Enables the given node |
1411 | 1411 |
/// |
1412 | 1412 |
/// This function enables the given node in the subdigraph. |
1413 | 1413 |
/// It is the same as \ref status() "status(n, true)". |
1414 | 1414 |
void enable(const Node& n) const { Parent::status(n, true); } |
1415 | 1415 |
|
1416 | 1416 |
/// \brief Enables the given edge |
1417 | 1417 |
/// |
1418 | 1418 |
/// This function enables the given edge in the subgraph. |
1419 | 1419 |
/// It is the same as \ref status() "status(e, true)". |
1420 | 1420 |
void enable(const Edge& e) const { Parent::status(e, true); } |
1421 | 1421 |
|
1422 | 1422 |
}; |
1423 | 1423 |
|
1424 | 1424 |
/// \brief Returns a read-only SubGraph adaptor |
1425 | 1425 |
/// |
1426 | 1426 |
/// This function just returns a read-only \ref SubGraph adaptor. |
1427 | 1427 |
/// \ingroup graph_adaptors |
1428 | 1428 |
/// \relates SubGraph |
1429 | 1429 |
template<typename GR, typename NF, typename EF> |
1430 | 1430 |
SubGraph<const GR, NF, EF> |
1431 | 1431 |
subGraph(const GR& graph, NF& node_filter, EF& edge_filter) { |
1432 | 1432 |
return SubGraph<const GR, NF, EF> |
1433 | 1433 |
(graph, node_filter, edge_filter); |
1434 | 1434 |
} |
1435 | 1435 |
|
1436 | 1436 |
template<typename GR, typename NF, typename EF> |
1437 | 1437 |
SubGraph<const GR, const NF, EF> |
1438 | 1438 |
subGraph(const GR& graph, const NF& node_filter, EF& edge_filter) { |
1439 | 1439 |
return SubGraph<const GR, const NF, EF> |
1440 | 1440 |
(graph, node_filter, edge_filter); |
1441 | 1441 |
} |
1442 | 1442 |
|
1443 | 1443 |
template<typename GR, typename NF, typename EF> |
1444 | 1444 |
SubGraph<const GR, NF, const EF> |
1445 | 1445 |
subGraph(const GR& graph, NF& node_filter, const EF& edge_filter) { |
1446 | 1446 |
return SubGraph<const GR, NF, const EF> |
1447 | 1447 |
(graph, node_filter, edge_filter); |
1448 | 1448 |
} |
1449 | 1449 |
|
1450 | 1450 |
template<typename GR, typename NF, typename EF> |
1451 | 1451 |
SubGraph<const GR, const NF, const EF> |
1452 | 1452 |
subGraph(const GR& graph, const NF& node_filter, const EF& edge_filter) { |
1453 | 1453 |
return SubGraph<const GR, const NF, const EF> |
1454 | 1454 |
(graph, node_filter, edge_filter); |
1455 | 1455 |
} |
1456 | 1456 |
|
1457 | 1457 |
|
1458 | 1458 |
/// \ingroup graph_adaptors |
1459 | 1459 |
/// |
1460 | 1460 |
/// \brief Adaptor class for hiding nodes in a digraph or a graph. |
1461 | 1461 |
/// |
1462 | 1462 |
/// FilterNodes adaptor can be used for hiding nodes in a digraph or a |
1463 | 1463 |
/// graph. A \c bool node map must be specified, which defines the filter |
1464 | 1464 |
/// for the nodes. Only the nodes with \c true filter value and the |
1465 | 1465 |
/// arcs/edges incident to nodes both with \c true filter value are shown |
1466 | 1466 |
/// in the subgraph. This adaptor conforms to the \ref concepts::Digraph |
1467 | 1467 |
/// "Digraph" concept or the \ref concepts::Graph "Graph" concept |
1468 | 1468 |
/// depending on the \c GR template parameter. |
1469 | 1469 |
/// |
1470 | 1470 |
/// The adapted (di)graph can also be modified through this adaptor |
1471 | 1471 |
/// by adding or removing nodes or arcs/edges, unless the \c GR template |
1472 | 1472 |
/// parameter is set to be \c const. |
1473 | 1473 |
/// |
1474 | 1474 |
/// \tparam GR The type of the adapted digraph or graph. |
1475 | 1475 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept |
1476 | 1476 |
/// or the \ref concepts::Graph "Graph" concept. |
1477 | 1477 |
/// It can also be specified to be \c const. |
1478 | 1478 |
/// \tparam NF The type of the node filter map. |
1479 | 1479 |
/// It must be a \c bool (or convertible) node map of the |
1480 | 1480 |
/// adapted (di)graph. The default type is |
1481 | 1481 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
1482 | 1482 |
/// |
1483 | 1483 |
/// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the |
1484 | 1484 |
/// adapted (di)graph are convertible to each other. |
1485 | 1485 |
#ifdef DOXYGEN |
1486 | 1486 |
template<typename GR, typename NF> |
1487 | 1487 |
class FilterNodes { |
1488 | 1488 |
#else |
1489 | 1489 |
template<typename GR, |
1490 | 1490 |
typename NF = typename GR::template NodeMap<bool>, |
1491 | 1491 |
typename Enable = void> |
1492 | 1492 |
class FilterNodes : |
1493 | 1493 |
public DigraphAdaptorExtender< |
1494 | 1494 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
1495 | 1495 |
true> > { |
1496 | 1496 |
#endif |
1497 | 1497 |
typedef DigraphAdaptorExtender< |
1498 | 1498 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
1499 | 1499 |
true> > Parent; |
1500 | 1500 |
|
1501 | 1501 |
public: |
1502 | 1502 |
|
1503 | 1503 |
typedef GR Digraph; |
1504 | 1504 |
typedef NF NodeFilterMap; |
1505 | 1505 |
|
1506 | 1506 |
typedef typename Parent::Node Node; |
1507 | 1507 |
|
1508 | 1508 |
protected: |
1509 | 1509 |
ConstMap<typename Digraph::Arc, Const<bool, true> > const_true_map; |
1510 | 1510 |
|
1511 | 1511 |
FilterNodes() : const_true_map() {} |
1512 | 1512 |
|
1513 | 1513 |
public: |
1514 | 1514 |
|
1515 | 1515 |
/// \brief Constructor |
1516 | 1516 |
/// |
1517 | 1517 |
/// Creates a subgraph for the given digraph or graph with the |
1518 | 1518 |
/// given node filter map. |
1519 | 1519 |
FilterNodes(GR& graph, NF& node_filter) |
1520 | 1520 |
: Parent(), const_true_map() |
1521 | 1521 |
{ |
1522 | 1522 |
Parent::initialize(graph, node_filter, const_true_map); |
1523 | 1523 |
} |
1524 | 1524 |
|
1525 | 1525 |
/// \brief Sets the status of the given node |
1526 | 1526 |
/// |
1527 | 1527 |
/// This function sets the status of the given node. |
1528 | 1528 |
/// It is done by simply setting the assigned value of \c n |
1529 | 1529 |
/// to \c v in the node filter map. |
1530 | 1530 |
void status(const Node& n, bool v) const { Parent::status(n, v); } |
1531 | 1531 |
|
1532 | 1532 |
/// \brief Returns the status of the given node |
1533 | 1533 |
/// |
1534 | 1534 |
/// This function returns the status of the given node. |
1535 | 1535 |
/// It is \c true if the given node is enabled (i.e. not hidden). |
1536 | 1536 |
bool status(const Node& n) const { return Parent::status(n); } |
1537 | 1537 |
|
1538 | 1538 |
/// \brief Disables the given node |
1539 | 1539 |
/// |
1540 | 1540 |
/// This function disables the given node, so the iteration |
1541 | 1541 |
/// jumps over it. |
1542 | 1542 |
/// It is the same as \ref status() "status(n, false)". |
1543 | 1543 |
void disable(const Node& n) const { Parent::status(n, false); } |
1544 | 1544 |
|
1545 | 1545 |
/// \brief Enables the given node |
1546 | 1546 |
/// |
1547 | 1547 |
/// This function enables the given node. |
1548 | 1548 |
/// It is the same as \ref status() "status(n, true)". |
1549 | 1549 |
void enable(const Node& n) const { Parent::status(n, true); } |
1550 | 1550 |
|
1551 | 1551 |
}; |
1552 | 1552 |
|
1553 | 1553 |
template<typename GR, typename NF> |
1554 | 1554 |
class FilterNodes<GR, NF, |
1555 | 1555 |
typename enable_if<UndirectedTagIndicator<GR> >::type> : |
1556 | 1556 |
public GraphAdaptorExtender< |
1557 | 1557 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
1558 | 1558 |
true> > { |
1559 | 1559 |
|
1560 | 1560 |
typedef GraphAdaptorExtender< |
1561 | 1561 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
1562 | 1562 |
true> > Parent; |
1563 | 1563 |
|
1564 | 1564 |
public: |
1565 | 1565 |
|
1566 | 1566 |
typedef GR Graph; |
1567 | 1567 |
typedef NF NodeFilterMap; |
1568 | 1568 |
|
1569 | 1569 |
typedef typename Parent::Node Node; |
1570 | 1570 |
|
1571 | 1571 |
protected: |
1572 | 1572 |
ConstMap<typename GR::Edge, Const<bool, true> > const_true_map; |
1573 | 1573 |
|
1574 | 1574 |
FilterNodes() : const_true_map() {} |
1575 | 1575 |
|
1576 | 1576 |
public: |
1577 | 1577 |
|
1578 | 1578 |
FilterNodes(GR& graph, NodeFilterMap& node_filter) : |
1579 | 1579 |
Parent(), const_true_map() { |
1580 | 1580 |
Parent::initialize(graph, node_filter, const_true_map); |
1581 | 1581 |
} |
1582 | 1582 |
|
1583 | 1583 |
void status(const Node& n, bool v) const { Parent::status(n, v); } |
1584 | 1584 |
bool status(const Node& n) const { return Parent::status(n); } |
1585 | 1585 |
void disable(const Node& n) const { Parent::status(n, false); } |
1586 | 1586 |
void enable(const Node& n) const { Parent::status(n, true); } |
1587 | 1587 |
|
1588 | 1588 |
}; |
1589 | 1589 |
|
1590 | 1590 |
|
1591 | 1591 |
/// \brief Returns a read-only FilterNodes adaptor |
1592 | 1592 |
/// |
1593 | 1593 |
/// This function just returns a read-only \ref FilterNodes adaptor. |
1594 | 1594 |
/// \ingroup graph_adaptors |
1595 | 1595 |
/// \relates FilterNodes |
1596 | 1596 |
template<typename GR, typename NF> |
1597 | 1597 |
FilterNodes<const GR, NF> |
1598 | 1598 |
filterNodes(const GR& graph, NF& node_filter) { |
1599 | 1599 |
return FilterNodes<const GR, NF>(graph, node_filter); |
1600 | 1600 |
} |
1601 | 1601 |
|
1602 | 1602 |
template<typename GR, typename NF> |
1603 | 1603 |
FilterNodes<const GR, const NF> |
1604 | 1604 |
filterNodes(const GR& graph, const NF& node_filter) { |
1605 | 1605 |
return FilterNodes<const GR, const NF>(graph, node_filter); |
1606 | 1606 |
} |
1607 | 1607 |
|
1608 | 1608 |
/// \ingroup graph_adaptors |
1609 | 1609 |
/// |
1610 | 1610 |
/// \brief Adaptor class for hiding arcs in a digraph. |
1611 | 1611 |
/// |
1612 | 1612 |
/// FilterArcs adaptor can be used for hiding arcs in a digraph. |
1613 | 1613 |
/// A \c bool arc map must be specified, which defines the filter for |
1614 | 1614 |
/// the arcs. Only the arcs with \c true filter value are shown in the |
1615 | 1615 |
/// subdigraph. This adaptor conforms to the \ref concepts::Digraph |
1616 | 1616 |
/// "Digraph" concept. |
1617 | 1617 |
/// |
1618 | 1618 |
/// The adapted digraph can also be modified through this adaptor |
1619 | 1619 |
/// by adding or removing nodes or arcs, unless the \c GR template |
1620 | 1620 |
/// parameter is set to be \c const. |
1621 | 1621 |
/// |
1622 | 1622 |
/// \tparam DGR The type of the adapted digraph. |
1623 | 1623 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
1624 | 1624 |
/// It can also be specified to be \c const. |
1625 | 1625 |
/// \tparam AF The type of the arc filter map. |
1626 | 1626 |
/// It must be a \c bool (or convertible) arc map of the |
1627 | 1627 |
/// adapted digraph. The default type is |
1628 | 1628 |
/// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
1629 | 1629 |
/// |
1630 | 1630 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
1631 | 1631 |
/// digraph are convertible to each other. |
1632 | 1632 |
#ifdef DOXYGEN |
1633 | 1633 |
template<typename DGR, |
1634 | 1634 |
typename AF> |
1635 | 1635 |
class FilterArcs { |
1636 | 1636 |
#else |
1637 | 1637 |
template<typename DGR, |
1638 | 1638 |
typename AF = typename DGR::template ArcMap<bool> > |
1639 | 1639 |
class FilterArcs : |
1640 | 1640 |
public DigraphAdaptorExtender< |
1641 | 1641 |
SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
1642 | 1642 |
AF, false> > { |
1643 | 1643 |
#endif |
1644 | 1644 |
typedef DigraphAdaptorExtender< |
1645 | 1645 |
SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
1646 | 1646 |
AF, false> > Parent; |
1647 | 1647 |
|
1648 | 1648 |
public: |
1649 | 1649 |
|
1650 | 1650 |
/// The type of the adapted digraph. |
1651 | 1651 |
typedef DGR Digraph; |
1652 | 1652 |
/// The type of the arc filter map. |
1653 | 1653 |
typedef AF ArcFilterMap; |
1654 | 1654 |
|
1655 | 1655 |
typedef typename Parent::Arc Arc; |
1656 | 1656 |
|
1657 | 1657 |
protected: |
1658 | 1658 |
ConstMap<typename DGR::Node, Const<bool, true> > const_true_map; |
1659 | 1659 |
|
1660 | 1660 |
FilterArcs() : const_true_map() {} |
1661 | 1661 |
|
1662 | 1662 |
public: |
1663 | 1663 |
|
1664 | 1664 |
/// \brief Constructor |
1665 | 1665 |
/// |
1666 | 1666 |
/// Creates a subdigraph for the given digraph with the given arc |
1667 | 1667 |
/// filter map. |
1668 | 1668 |
FilterArcs(DGR& digraph, ArcFilterMap& arc_filter) |
1669 | 1669 |
: Parent(), const_true_map() { |
1670 | 1670 |
Parent::initialize(digraph, const_true_map, arc_filter); |
1671 | 1671 |
} |
1672 | 1672 |
|
1673 | 1673 |
/// \brief Sets the status of the given arc |
1674 | 1674 |
/// |
1675 | 1675 |
/// This function sets the status of the given arc. |
1676 | 1676 |
/// It is done by simply setting the assigned value of \c a |
1677 | 1677 |
/// to \c v in the arc filter map. |
1678 | 1678 |
void status(const Arc& a, bool v) const { Parent::status(a, v); } |
1679 | 1679 |
|
1680 | 1680 |
/// \brief Returns the status of the given arc |
1681 | 1681 |
/// |
1682 | 1682 |
/// This function returns the status of the given arc. |
1683 | 1683 |
/// It is \c true if the given arc is enabled (i.e. not hidden). |
1684 | 1684 |
bool status(const Arc& a) const { return Parent::status(a); } |
1685 | 1685 |
|
1686 | 1686 |
/// \brief Disables the given arc |
1687 | 1687 |
/// |
1688 | 1688 |
/// This function disables the given arc in the subdigraph, |
1689 | 1689 |
/// so the iteration jumps over it. |
1690 | 1690 |
/// It is the same as \ref status() "status(a, false)". |
1691 | 1691 |
void disable(const Arc& a) const { Parent::status(a, false); } |
1692 | 1692 |
|
1693 | 1693 |
/// \brief Enables the given arc |
1694 | 1694 |
/// |
1695 | 1695 |
/// This function enables the given arc in the subdigraph. |
1696 | 1696 |
/// It is the same as \ref status() "status(a, true)". |
1697 | 1697 |
void enable(const Arc& a) const { Parent::status(a, true); } |
1698 | 1698 |
|
1699 | 1699 |
}; |
1700 | 1700 |
|
1701 | 1701 |
/// \brief Returns a read-only FilterArcs adaptor |
1702 | 1702 |
/// |
1703 | 1703 |
/// This function just returns a read-only \ref FilterArcs adaptor. |
1704 | 1704 |
/// \ingroup graph_adaptors |
1705 | 1705 |
/// \relates FilterArcs |
1706 | 1706 |
template<typename DGR, typename AF> |
1707 | 1707 |
FilterArcs<const DGR, AF> |
1708 | 1708 |
filterArcs(const DGR& digraph, AF& arc_filter) { |
1709 | 1709 |
return FilterArcs<const DGR, AF>(digraph, arc_filter); |
1710 | 1710 |
} |
1711 | 1711 |
|
1712 | 1712 |
template<typename DGR, typename AF> |
1713 | 1713 |
FilterArcs<const DGR, const AF> |
1714 | 1714 |
filterArcs(const DGR& digraph, const AF& arc_filter) { |
1715 | 1715 |
return FilterArcs<const DGR, const AF>(digraph, arc_filter); |
1716 | 1716 |
} |
1717 | 1717 |
|
1718 | 1718 |
/// \ingroup graph_adaptors |
1719 | 1719 |
/// |
1720 | 1720 |
/// \brief Adaptor class for hiding edges in a graph. |
1721 | 1721 |
/// |
1722 | 1722 |
/// FilterEdges adaptor can be used for hiding edges in a graph. |
1723 | 1723 |
/// A \c bool edge map must be specified, which defines the filter for |
1724 | 1724 |
/// the edges. Only the edges with \c true filter value are shown in the |
1725 | 1725 |
/// subgraph. This adaptor conforms to the \ref concepts::Graph |
1726 | 1726 |
/// "Graph" concept. |
1727 | 1727 |
/// |
1728 | 1728 |
/// The adapted graph can also be modified through this adaptor |
1729 | 1729 |
/// by adding or removing nodes or edges, unless the \c GR template |
1730 | 1730 |
/// parameter is set to be \c const. |
1731 | 1731 |
/// |
1732 | 1732 |
/// \tparam GR The type of the adapted graph. |
1733 | 1733 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
1734 | 1734 |
/// It can also be specified to be \c const. |
1735 | 1735 |
/// \tparam EF The type of the edge filter map. |
1736 | 1736 |
/// It must be a \c bool (or convertible) edge map of the |
1737 | 1737 |
/// adapted graph. The default type is |
1738 | 1738 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
1739 | 1739 |
/// |
1740 | 1740 |
/// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
1741 | 1741 |
/// adapted graph are convertible to each other. |
1742 | 1742 |
#ifdef DOXYGEN |
1743 | 1743 |
template<typename GR, |
1744 | 1744 |
typename EF> |
1745 | 1745 |
class FilterEdges { |
1746 | 1746 |
#else |
1747 | 1747 |
template<typename GR, |
1748 | 1748 |
typename EF = typename GR::template EdgeMap<bool> > |
1749 | 1749 |
class FilterEdges : |
1750 | 1750 |
public GraphAdaptorExtender< |
1751 | 1751 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >, |
1752 | 1752 |
EF, false> > { |
1753 | 1753 |
#endif |
1754 | 1754 |
typedef GraphAdaptorExtender< |
1755 | 1755 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >, |
1756 | 1756 |
EF, false> > Parent; |
1757 | 1757 |
|
1758 | 1758 |
public: |
1759 | 1759 |
|
1760 | 1760 |
/// The type of the adapted graph. |
1761 | 1761 |
typedef GR Graph; |
1762 | 1762 |
/// The type of the edge filter map. |
1763 | 1763 |
typedef EF EdgeFilterMap; |
1764 | 1764 |
|
1765 | 1765 |
typedef typename Parent::Edge Edge; |
1766 | 1766 |
|
1767 | 1767 |
protected: |
1768 | 1768 |
ConstMap<typename GR::Node, Const<bool, true> > const_true_map; |
1769 | 1769 |
|
1770 | 1770 |
FilterEdges() : const_true_map(true) { |
1771 | 1771 |
Parent::setNodeFilterMap(const_true_map); |
1772 | 1772 |
} |
1773 | 1773 |
|
1774 | 1774 |
public: |
1775 | 1775 |
|
1776 | 1776 |
/// \brief Constructor |
1777 | 1777 |
/// |
1778 | 1778 |
/// Creates a subgraph for the given graph with the given edge |
1779 | 1779 |
/// filter map. |
1780 | 1780 |
FilterEdges(GR& graph, EF& edge_filter) |
1781 | 1781 |
: Parent(), const_true_map() { |
1782 | 1782 |
Parent::initialize(graph, const_true_map, edge_filter); |
1783 | 1783 |
} |
1784 | 1784 |
|
1785 | 1785 |
/// \brief Sets the status of the given edge |
1786 | 1786 |
/// |
1787 | 1787 |
/// This function sets the status of the given edge. |
1788 | 1788 |
/// It is done by simply setting the assigned value of \c e |
1789 | 1789 |
/// to \c v in the edge filter map. |
1790 | 1790 |
void status(const Edge& e, bool v) const { Parent::status(e, v); } |
1791 | 1791 |
|
1792 | 1792 |
/// \brief Returns the status of the given edge |
1793 | 1793 |
/// |
1794 | 1794 |
/// This function returns the status of the given edge. |
1795 | 1795 |
/// It is \c true if the given edge is enabled (i.e. not hidden). |
1796 | 1796 |
bool status(const Edge& e) const { return Parent::status(e); } |
1797 | 1797 |
|
1798 | 1798 |
/// \brief Disables the given edge |
1799 | 1799 |
/// |
1800 | 1800 |
/// This function disables the given edge in the subgraph, |
1801 | 1801 |
/// so the iteration jumps over it. |
1802 | 1802 |
/// It is the same as \ref status() "status(e, false)". |
1803 | 1803 |
void disable(const Edge& e) const { Parent::status(e, false); } |
1804 | 1804 |
|
1805 | 1805 |
/// \brief Enables the given edge |
1806 | 1806 |
/// |
1807 | 1807 |
/// This function enables the given edge in the subgraph. |
1808 | 1808 |
/// It is the same as \ref status() "status(e, true)". |
1809 | 1809 |
void enable(const Edge& e) const { Parent::status(e, true); } |
1810 | 1810 |
|
1811 | 1811 |
}; |
1812 | 1812 |
|
1813 | 1813 |
/// \brief Returns a read-only FilterEdges adaptor |
1814 | 1814 |
/// |
1815 | 1815 |
/// This function just returns a read-only \ref FilterEdges adaptor. |
1816 | 1816 |
/// \ingroup graph_adaptors |
1817 | 1817 |
/// \relates FilterEdges |
1818 | 1818 |
template<typename GR, typename EF> |
1819 | 1819 |
FilterEdges<const GR, EF> |
1820 | 1820 |
filterEdges(const GR& graph, EF& edge_filter) { |
1821 | 1821 |
return FilterEdges<const GR, EF>(graph, edge_filter); |
1822 | 1822 |
} |
1823 | 1823 |
|
1824 | 1824 |
template<typename GR, typename EF> |
1825 | 1825 |
FilterEdges<const GR, const EF> |
1826 | 1826 |
filterEdges(const GR& graph, const EF& edge_filter) { |
1827 | 1827 |
return FilterEdges<const GR, const EF>(graph, edge_filter); |
1828 | 1828 |
} |
1829 | 1829 |
|
1830 | 1830 |
|
1831 | 1831 |
template <typename DGR> |
1832 | 1832 |
class UndirectorBase { |
1833 | 1833 |
public: |
1834 | 1834 |
typedef DGR Digraph; |
1835 | 1835 |
typedef UndirectorBase Adaptor; |
1836 | 1836 |
|
1837 | 1837 |
typedef True UndirectedTag; |
1838 | 1838 |
|
1839 | 1839 |
typedef typename Digraph::Arc Edge; |
1840 | 1840 |
typedef typename Digraph::Node Node; |
1841 | 1841 |
|
1842 |
class Arc |
|
1842 |
class Arc { |
|
1843 | 1843 |
friend class UndirectorBase; |
1844 | 1844 |
protected: |
1845 |
Edge _edge; |
|
1845 | 1846 |
bool _forward; |
1846 | 1847 |
|
1847 |
Arc(const Edge& edge, bool forward) : |
|
1848 |
Edge(edge), _forward(forward) {} |
|
1848 |
Arc(const Edge& edge, bool forward) |
|
1849 |
: _edge(edge), _forward(forward) {} |
|
1849 | 1850 |
|
1850 | 1851 |
public: |
1851 | 1852 |
Arc() {} |
1852 | 1853 |
|
1853 |
Arc(Invalid) : |
|
1854 |
Arc(Invalid) : _edge(INVALID), _forward(true) {} |
|
1855 |
|
|
1856 |
operator const Edge&() const { return _edge; } |
|
1854 | 1857 |
|
1855 | 1858 |
bool operator==(const Arc &other) const { |
1856 |
return _forward == other._forward && |
|
1857 |
static_cast<const Edge&>(*this) == static_cast<const Edge&>(other); |
|
1859 |
return _forward == other._forward && _edge == other._edge; |
|
1858 | 1860 |
} |
1859 | 1861 |
bool operator!=(const Arc &other) const { |
1860 |
return _forward != other._forward || |
|
1861 |
static_cast<const Edge&>(*this) != static_cast<const Edge&>(other); |
|
1862 |
return _forward != other._forward || _edge != other._edge; |
|
1862 | 1863 |
} |
1863 | 1864 |
bool operator<(const Arc &other) const { |
1864 | 1865 |
return _forward < other._forward || |
1865 |
(_forward == other._forward && |
|
1866 |
static_cast<const Edge&>(*this) < static_cast<const Edge&>(other)); |
|
1866 |
(_forward == other._forward && _edge < other._edge); |
|
1867 | 1867 |
} |
1868 | 1868 |
}; |
1869 | 1869 |
|
1870 | 1870 |
void first(Node& n) const { |
1871 | 1871 |
_digraph->first(n); |
1872 | 1872 |
} |
1873 | 1873 |
|
1874 | 1874 |
void next(Node& n) const { |
1875 | 1875 |
_digraph->next(n); |
1876 | 1876 |
} |
1877 | 1877 |
|
1878 | 1878 |
void first(Arc& a) const { |
1879 |
_digraph->first(a); |
|
1879 |
_digraph->first(a._edge); |
|
1880 | 1880 |
a._forward = true; |
1881 | 1881 |
} |
1882 | 1882 |
|
1883 | 1883 |
void next(Arc& a) const { |
1884 | 1884 |
if (a._forward) { |
1885 | 1885 |
a._forward = false; |
1886 | 1886 |
} else { |
1887 |
_digraph->next(a); |
|
1887 |
_digraph->next(a._edge); |
|
1888 | 1888 |
a._forward = true; |
1889 | 1889 |
} |
1890 | 1890 |
} |
1891 | 1891 |
|
1892 | 1892 |
void first(Edge& e) const { |
1893 | 1893 |
_digraph->first(e); |
1894 | 1894 |
} |
1895 | 1895 |
|
1896 | 1896 |
void next(Edge& e) const { |
1897 | 1897 |
_digraph->next(e); |
1898 | 1898 |
} |
1899 | 1899 |
|
1900 | 1900 |
void firstOut(Arc& a, const Node& n) const { |
1901 |
_digraph->firstIn(a, n); |
|
1902 |
if( static_cast<const Edge&>(a) != INVALID ) { |
|
1901 |
_digraph->firstIn(a._edge, n); |
|
1902 |
if (a._edge != INVALID ) { |
|
1903 | 1903 |
a._forward = false; |
1904 | 1904 |
} else { |
1905 |
_digraph->firstOut(a, n); |
|
1905 |
_digraph->firstOut(a._edge, n); |
|
1906 | 1906 |
a._forward = true; |
1907 | 1907 |
} |
1908 | 1908 |
} |
1909 | 1909 |
void nextOut(Arc &a) const { |
1910 | 1910 |
if (!a._forward) { |
1911 |
Node n = _digraph->target(a); |
|
1912 |
_digraph->nextIn(a); |
|
1913 |
if (static_cast<const Edge&>(a) == INVALID ) { |
|
1914 |
_digraph->firstOut(a, n); |
|
1911 |
Node n = _digraph->target(a._edge); |
|
1912 |
_digraph->nextIn(a._edge); |
|
1913 |
if (a._edge == INVALID) { |
|
1914 |
_digraph->firstOut(a._edge, n); |
|
1915 | 1915 |
a._forward = true; |
1916 | 1916 |
} |
1917 | 1917 |
} |
1918 | 1918 |
else { |
1919 |
_digraph->nextOut(a); |
|
1919 |
_digraph->nextOut(a._edge); |
|
1920 | 1920 |
} |
1921 | 1921 |
} |
1922 | 1922 |
|
1923 | 1923 |
void firstIn(Arc &a, const Node &n) const { |
1924 |
_digraph->firstOut(a, n); |
|
1925 |
if (static_cast<const Edge&>(a) != INVALID ) { |
|
1924 |
_digraph->firstOut(a._edge, n); |
|
1925 |
if (a._edge != INVALID ) { |
|
1926 | 1926 |
a._forward = false; |
1927 | 1927 |
} else { |
1928 |
_digraph->firstIn(a, n); |
|
1928 |
_digraph->firstIn(a._edge, n); |
|
1929 | 1929 |
a._forward = true; |
1930 | 1930 |
} |
1931 | 1931 |
} |
1932 | 1932 |
void nextIn(Arc &a) const { |
1933 | 1933 |
if (!a._forward) { |
1934 |
Node n = _digraph->source(a); |
|
1935 |
_digraph->nextOut(a); |
|
1936 |
if( static_cast<const Edge&>(a) == INVALID ) { |
|
1937 |
_digraph->firstIn(a, n); |
|
1934 |
Node n = _digraph->source(a._edge); |
|
1935 |
_digraph->nextOut(a._edge); |
|
1936 |
if (a._edge == INVALID ) { |
|
1937 |
_digraph->firstIn(a._edge, n); |
|
1938 | 1938 |
a._forward = true; |
1939 | 1939 |
} |
1940 | 1940 |
} |
1941 | 1941 |
else { |
1942 |
_digraph->nextIn(a); |
|
1942 |
_digraph->nextIn(a._edge); |
|
1943 | 1943 |
} |
1944 | 1944 |
} |
1945 | 1945 |
|
1946 | 1946 |
void firstInc(Edge &e, bool &d, const Node &n) const { |
1947 | 1947 |
d = true; |
1948 | 1948 |
_digraph->firstOut(e, n); |
1949 | 1949 |
if (e != INVALID) return; |
1950 | 1950 |
d = false; |
1951 | 1951 |
_digraph->firstIn(e, n); |
1952 | 1952 |
} |
1953 | 1953 |
|
1954 | 1954 |
void nextInc(Edge &e, bool &d) const { |
1955 | 1955 |
if (d) { |
1956 | 1956 |
Node s = _digraph->source(e); |
1957 | 1957 |
_digraph->nextOut(e); |
1958 | 1958 |
if (e != INVALID) return; |
1959 | 1959 |
d = false; |
1960 | 1960 |
_digraph->firstIn(e, s); |
1961 | 1961 |
} else { |
1962 | 1962 |
_digraph->nextIn(e); |
1963 | 1963 |
} |
1964 | 1964 |
} |
1965 | 1965 |
|
1966 | 1966 |
Node u(const Edge& e) const { |
1967 | 1967 |
return _digraph->source(e); |
1968 | 1968 |
} |
1969 | 1969 |
|
1970 | 1970 |
Node v(const Edge& e) const { |
1971 | 1971 |
return _digraph->target(e); |
1972 | 1972 |
} |
1973 | 1973 |
|
1974 | 1974 |
Node source(const Arc &a) const { |
1975 |
return a._forward ? _digraph->source(a) : _digraph->target(a); |
|
1975 |
return a._forward ? _digraph->source(a._edge) : _digraph->target(a._edge); |
|
1976 | 1976 |
} |
1977 | 1977 |
|
1978 | 1978 |
Node target(const Arc &a) const { |
1979 |
return a._forward ? _digraph->target(a) : _digraph->source(a); |
|
1979 |
return a._forward ? _digraph->target(a._edge) : _digraph->source(a._edge); |
|
1980 | 1980 |
} |
1981 | 1981 |
|
1982 | 1982 |
static Arc direct(const Edge &e, bool d) { |
1983 | 1983 |
return Arc(e, d); |
1984 | 1984 |
} |
1985 |
Arc direct(const Edge &e, const Node& n) const { |
|
1986 |
return Arc(e, _digraph->source(e) == n); |
|
1987 |
} |
|
1988 | 1985 |
|
1989 | 1986 |
static bool direction(const Arc &a) { return a._forward; } |
1990 | 1987 |
|
1991 | 1988 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
1992 | 1989 |
Arc arcFromId(int ix) const { |
1993 | 1990 |
return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1)); |
1994 | 1991 |
} |
1995 | 1992 |
Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); } |
1996 | 1993 |
|
1997 | 1994 |
int id(const Node &n) const { return _digraph->id(n); } |
1998 | 1995 |
int id(const Arc &a) const { |
1999 | 1996 |
return (_digraph->id(a) << 1) | (a._forward ? 1 : 0); |
2000 | 1997 |
} |
2001 | 1998 |
int id(const Edge &e) const { return _digraph->id(e); } |
2002 | 1999 |
|
2003 | 2000 |
int maxNodeId() const { return _digraph->maxNodeId(); } |
2004 | 2001 |
int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; } |
2005 | 2002 |
int maxEdgeId() const { return _digraph->maxArcId(); } |
2006 | 2003 |
|
2007 | 2004 |
Node addNode() { return _digraph->addNode(); } |
2008 | 2005 |
Edge addEdge(const Node& u, const Node& v) { |
2009 | 2006 |
return _digraph->addArc(u, v); |
2010 | 2007 |
} |
2011 | 2008 |
|
2012 | 2009 |
void erase(const Node& i) { _digraph->erase(i); } |
2013 | 2010 |
void erase(const Edge& i) { _digraph->erase(i); } |
2014 | 2011 |
|
2015 | 2012 |
void clear() { _digraph->clear(); } |
2016 | 2013 |
|
2017 | 2014 |
typedef NodeNumTagIndicator<Digraph> NodeNumTag; |
2018 | 2015 |
int nodeNum() const { return _digraph->nodeNum(); } |
2019 | 2016 |
|
2020 | 2017 |
typedef ArcNumTagIndicator<Digraph> ArcNumTag; |
2021 | 2018 |
int arcNum() const { return 2 * _digraph->arcNum(); } |
2022 | 2019 |
|
2023 | 2020 |
typedef ArcNumTag EdgeNumTag; |
2024 | 2021 |
int edgeNum() const { return _digraph->arcNum(); } |
2025 | 2022 |
|
2026 | 2023 |
typedef FindArcTagIndicator<Digraph> FindArcTag; |
2027 | 2024 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
2028 | 2025 |
if (p == INVALID) { |
2029 | 2026 |
Edge arc = _digraph->findArc(s, t); |
2030 | 2027 |
if (arc != INVALID) return direct(arc, true); |
2031 | 2028 |
arc = _digraph->findArc(t, s); |
2032 | 2029 |
if (arc != INVALID) return direct(arc, false); |
2033 | 2030 |
} else if (direction(p)) { |
2034 | 2031 |
Edge arc = _digraph->findArc(s, t, p); |
2035 | 2032 |
if (arc != INVALID) return direct(arc, true); |
2036 | 2033 |
arc = _digraph->findArc(t, s); |
2037 | 2034 |
if (arc != INVALID) return direct(arc, false); |
2038 | 2035 |
} else { |
2039 | 2036 |
Edge arc = _digraph->findArc(t, s, p); |
2040 | 2037 |
if (arc != INVALID) return direct(arc, false); |
2041 | 2038 |
} |
2042 | 2039 |
return INVALID; |
2043 | 2040 |
} |
2044 | 2041 |
|
2045 | 2042 |
typedef FindArcTag FindEdgeTag; |
2046 | 2043 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
2047 | 2044 |
if (s != t) { |
2048 | 2045 |
if (p == INVALID) { |
2049 | 2046 |
Edge arc = _digraph->findArc(s, t); |
2050 | 2047 |
if (arc != INVALID) return arc; |
2051 | 2048 |
arc = _digraph->findArc(t, s); |
2052 | 2049 |
if (arc != INVALID) return arc; |
2053 | 2050 |
} else if (_digraph->source(p) == s) { |
2054 | 2051 |
Edge arc = _digraph->findArc(s, t, p); |
2055 | 2052 |
if (arc != INVALID) return arc; |
2056 | 2053 |
arc = _digraph->findArc(t, s); |
2057 | 2054 |
if (arc != INVALID) return arc; |
2058 | 2055 |
} else { |
2059 | 2056 |
Edge arc = _digraph->findArc(t, s, p); |
2060 | 2057 |
if (arc != INVALID) return arc; |
2061 | 2058 |
} |
2062 | 2059 |
} else { |
2063 | 2060 |
return _digraph->findArc(s, t, p); |
2064 | 2061 |
} |
2065 | 2062 |
return INVALID; |
2066 | 2063 |
} |
2067 | 2064 |
|
2068 | 2065 |
private: |
2069 | 2066 |
|
2070 | 2067 |
template <typename V> |
2071 | 2068 |
class ArcMapBase { |
2072 | 2069 |
private: |
2073 | 2070 |
|
2074 | 2071 |
typedef typename DGR::template ArcMap<V> MapImpl; |
2075 | 2072 |
|
2076 | 2073 |
public: |
2077 | 2074 |
|
2078 | 2075 |
typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag; |
2079 | 2076 |
|
2080 | 2077 |
typedef V Value; |
2081 | 2078 |
typedef Arc Key; |
2082 | 2079 |
typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue; |
2083 | 2080 |
typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue; |
2084 | 2081 |
typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference; |
2085 | 2082 |
typedef typename MapTraits<MapImpl>::ReturnValue Reference; |
2086 | 2083 |
|
2087 | 2084 |
ArcMapBase(const UndirectorBase<DGR>& adaptor) : |
2088 | 2085 |
_forward(*adaptor._digraph), _backward(*adaptor._digraph) {} |
2089 | 2086 |
|
2090 | 2087 |
ArcMapBase(const UndirectorBase<DGR>& adaptor, const V& value) |
2091 | 2088 |
: _forward(*adaptor._digraph, value), |
2092 | 2089 |
_backward(*adaptor._digraph, value) {} |
2093 | 2090 |
|
2094 | 2091 |
void set(const Arc& a, const V& value) { |
2095 | 2092 |
if (direction(a)) { |
2096 | 2093 |
_forward.set(a, value); |
2097 | 2094 |
} else { |
2098 | 2095 |
_backward.set(a, value); |
2099 | 2096 |
} |
2100 | 2097 |
} |
2101 | 2098 |
|
2102 | 2099 |
ConstReturnValue operator[](const Arc& a) const { |
2103 | 2100 |
if (direction(a)) { |
2104 | 2101 |
return _forward[a]; |
2105 | 2102 |
} else { |
2106 | 2103 |
return _backward[a]; |
2107 | 2104 |
} |
2108 | 2105 |
} |
2109 | 2106 |
|
2110 | 2107 |
ReturnValue operator[](const Arc& a) { |
2111 | 2108 |
if (direction(a)) { |
2112 | 2109 |
return _forward[a]; |
2113 | 2110 |
} else { |
2114 | 2111 |
return _backward[a]; |
2115 | 2112 |
} |
2116 | 2113 |
} |
2117 | 2114 |
|
2118 | 2115 |
protected: |
2119 | 2116 |
|
2120 | 2117 |
MapImpl _forward, _backward; |
2121 | 2118 |
|
2122 | 2119 |
}; |
2123 | 2120 |
|
2124 | 2121 |
public: |
2125 | 2122 |
|
2126 | 2123 |
template <typename V> |
2127 | 2124 |
class NodeMap : public DGR::template NodeMap<V> { |
2128 | 2125 |
typedef typename DGR::template NodeMap<V> Parent; |
2129 | 2126 |
|
2130 | 2127 |
public: |
2131 | 2128 |
typedef V Value; |
2132 | 2129 |
|
2133 | 2130 |
explicit NodeMap(const UndirectorBase<DGR>& adaptor) |
2134 | 2131 |
: Parent(*adaptor._digraph) {} |
2135 | 2132 |
|
2136 | 2133 |
NodeMap(const UndirectorBase<DGR>& adaptor, const V& value) |
2137 | 2134 |
: Parent(*adaptor._digraph, value) { } |
2138 | 2135 |
|
2139 | 2136 |
private: |
2140 | 2137 |
NodeMap& operator=(const NodeMap& cmap) { |
2141 | 2138 |
return operator=<NodeMap>(cmap); |
2142 | 2139 |
} |
2143 | 2140 |
|
2144 | 2141 |
template <typename CMap> |
2145 | 2142 |
NodeMap& operator=(const CMap& cmap) { |
2146 | 2143 |
Parent::operator=(cmap); |
2147 | 2144 |
return *this; |
2148 | 2145 |
} |
2149 | 2146 |
|
2150 | 2147 |
}; |
2151 | 2148 |
|
2152 | 2149 |
template <typename V> |
2153 | 2150 |
class ArcMap |
2154 | 2151 |
: public SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> > { |
2155 | 2152 |
typedef SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> > Parent; |
2156 | 2153 |
|
2157 | 2154 |
public: |
2158 | 2155 |
typedef V Value; |
2159 | 2156 |
|
2160 | 2157 |
explicit ArcMap(const UndirectorBase<DGR>& adaptor) |
2161 | 2158 |
: Parent(adaptor) {} |
2162 | 2159 |
|
2163 | 2160 |
ArcMap(const UndirectorBase<DGR>& adaptor, const V& value) |
2164 | 2161 |
: Parent(adaptor, value) {} |
2165 | 2162 |
|
2166 | 2163 |
private: |
2167 | 2164 |
ArcMap& operator=(const ArcMap& cmap) { |
2168 | 2165 |
return operator=<ArcMap>(cmap); |
2169 | 2166 |
} |
2170 | 2167 |
|
2171 | 2168 |
template <typename CMap> |
2172 | 2169 |
ArcMap& operator=(const CMap& cmap) { |
2173 | 2170 |
Parent::operator=(cmap); |
2174 | 2171 |
return *this; |
2175 | 2172 |
} |
2176 | 2173 |
}; |
2177 | 2174 |
|
2178 | 2175 |
template <typename V> |
2179 | 2176 |
class EdgeMap : public Digraph::template ArcMap<V> { |
2180 | 2177 |
typedef typename Digraph::template ArcMap<V> Parent; |
2181 | 2178 |
|
2182 | 2179 |
public: |
2183 | 2180 |
typedef V Value; |
2184 | 2181 |
|
2185 | 2182 |
explicit EdgeMap(const UndirectorBase<DGR>& adaptor) |
2186 | 2183 |
: Parent(*adaptor._digraph) {} |
2187 | 2184 |
|
2188 | 2185 |
EdgeMap(const UndirectorBase<DGR>& adaptor, const V& value) |
2189 | 2186 |
: Parent(*adaptor._digraph, value) {} |
2190 | 2187 |
|
2191 | 2188 |
private: |
2192 | 2189 |
EdgeMap& operator=(const EdgeMap& cmap) { |
2193 | 2190 |
return operator=<EdgeMap>(cmap); |
2194 | 2191 |
} |
2195 | 2192 |
|
2196 | 2193 |
template <typename CMap> |
2197 | 2194 |
EdgeMap& operator=(const CMap& cmap) { |
2198 | 2195 |
Parent::operator=(cmap); |
2199 | 2196 |
return *this; |
2200 | 2197 |
} |
2201 | 2198 |
|
2202 | 2199 |
}; |
2203 | 2200 |
|
2204 | 2201 |
typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
2205 | 2202 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
2206 | 2203 |
|
2207 | 2204 |
typedef typename ItemSetTraits<DGR, Edge>::ItemNotifier EdgeNotifier; |
2208 | 2205 |
EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); } |
2209 | 2206 |
|
2210 | 2207 |
typedef EdgeNotifier ArcNotifier; |
2211 | 2208 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Edge()); } |
2212 | 2209 |
|
2213 | 2210 |
protected: |
2214 | 2211 |
|
2215 | 2212 |
UndirectorBase() : _digraph(0) {} |
2216 | 2213 |
|
2217 | 2214 |
DGR* _digraph; |
2218 | 2215 |
|
2219 | 2216 |
void initialize(DGR& digraph) { |
2220 | 2217 |
_digraph = &digraph; |
2221 | 2218 |
} |
2222 | 2219 |
|
2223 | 2220 |
}; |
2224 | 2221 |
|
2225 | 2222 |
/// \ingroup graph_adaptors |
2226 | 2223 |
/// |
2227 | 2224 |
/// \brief Adaptor class for viewing a digraph as an undirected graph. |
2228 | 2225 |
/// |
2229 | 2226 |
/// Undirector adaptor can be used for viewing a digraph as an undirected |
2230 | 2227 |
/// graph. All arcs of the underlying digraph are showed in the |
2231 | 2228 |
/// adaptor as an edge (and also as a pair of arcs, of course). |
2232 | 2229 |
/// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
2233 | 2230 |
/// |
2234 | 2231 |
/// The adapted digraph can also be modified through this adaptor |
2235 | 2232 |
/// by adding or removing nodes or edges, unless the \c GR template |
2236 | 2233 |
/// parameter is set to be \c const. |
2237 | 2234 |
/// |
2238 | 2235 |
/// \tparam DGR The type of the adapted digraph. |
2239 | 2236 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
2240 | 2237 |
/// It can also be specified to be \c const. |
2241 | 2238 |
/// |
2242 | 2239 |
/// \note The \c Node type of this adaptor and the adapted digraph are |
2243 | 2240 |
/// convertible to each other, moreover the \c Edge type of the adaptor |
2244 | 2241 |
/// and the \c Arc type of the adapted digraph are also convertible to |
2245 | 2242 |
/// each other. |
2246 | 2243 |
/// (Thus the \c Arc type of the adaptor is convertible to the \c Arc type |
2247 | 2244 |
/// of the adapted digraph.) |
2248 | 2245 |
template<typename DGR> |
2249 | 2246 |
#ifdef DOXYGEN |
2250 | 2247 |
class Undirector { |
2251 | 2248 |
#else |
2252 | 2249 |
class Undirector : |
2253 | 2250 |
public GraphAdaptorExtender<UndirectorBase<DGR> > { |
2254 | 2251 |
#endif |
2255 | 2252 |
typedef GraphAdaptorExtender<UndirectorBase<DGR> > Parent; |
2256 | 2253 |
public: |
2257 | 2254 |
/// The type of the adapted digraph. |
2258 | 2255 |
typedef DGR Digraph; |
2259 | 2256 |
protected: |
2260 | 2257 |
Undirector() { } |
2261 | 2258 |
public: |
2262 | 2259 |
|
2263 | 2260 |
/// \brief Constructor |
2264 | 2261 |
/// |
2265 | 2262 |
/// Creates an undirected graph from the given digraph. |
2266 | 2263 |
Undirector(DGR& digraph) { |
2267 | 2264 |
initialize(digraph); |
2268 | 2265 |
} |
2269 | 2266 |
|
2270 | 2267 |
/// \brief Arc map combined from two original arc maps |
2271 | 2268 |
/// |
2272 | 2269 |
/// This map adaptor class adapts two arc maps of the underlying |
2273 | 2270 |
/// digraph to get an arc map of the undirected graph. |
2274 | 2271 |
/// Its value type is inherited from the first arc map type (\c FW). |
2275 | 2272 |
/// \tparam FW The type of the "foward" arc map. |
2276 | 2273 |
/// \tparam BK The type of the "backward" arc map. |
2277 | 2274 |
template <typename FW, typename BK> |
2278 | 2275 |
class CombinedArcMap { |
2279 | 2276 |
public: |
2280 | 2277 |
|
2281 | 2278 |
/// The key type of the map |
2282 | 2279 |
typedef typename Parent::Arc Key; |
2283 | 2280 |
/// The value type of the map |
2284 | 2281 |
typedef typename FW::Value Value; |
2285 | 2282 |
|
2286 | 2283 |
typedef typename MapTraits<FW>::ReferenceMapTag ReferenceMapTag; |
2287 | 2284 |
|
2288 | 2285 |
typedef typename MapTraits<FW>::ReturnValue ReturnValue; |
2289 | 2286 |
typedef typename MapTraits<FW>::ConstReturnValue ConstReturnValue; |
2290 | 2287 |
typedef typename MapTraits<FW>::ReturnValue Reference; |
2291 | 2288 |
typedef typename MapTraits<FW>::ConstReturnValue ConstReference; |
2292 | 2289 |
|
2293 | 2290 |
/// Constructor |
2294 | 2291 |
CombinedArcMap(FW& forward, BK& backward) |
2295 | 2292 |
: _forward(&forward), _backward(&backward) {} |
2296 | 2293 |
|
2297 | 2294 |
/// Sets the value associated with the given key. |
2298 | 2295 |
void set(const Key& e, const Value& a) { |
2299 | 2296 |
if (Parent::direction(e)) { |
2300 | 2297 |
_forward->set(e, a); |
2301 | 2298 |
} else { |
2302 | 2299 |
_backward->set(e, a); |
2303 | 2300 |
} |
2304 | 2301 |
} |
2305 | 2302 |
|
2306 | 2303 |
/// Returns the value associated with the given key. |
2307 | 2304 |
ConstReturnValue operator[](const Key& e) const { |
2308 | 2305 |
if (Parent::direction(e)) { |
2309 | 2306 |
return (*_forward)[e]; |
2310 | 2307 |
} else { |
2311 | 2308 |
return (*_backward)[e]; |
2312 | 2309 |
} |
2313 | 2310 |
} |
2314 | 2311 |
|
2315 | 2312 |
/// Returns a reference to the value associated with the given key. |
2316 | 2313 |
ReturnValue operator[](const Key& e) { |
2317 | 2314 |
if (Parent::direction(e)) { |
2318 | 2315 |
return (*_forward)[e]; |
2319 | 2316 |
} else { |
2320 | 2317 |
return (*_backward)[e]; |
2321 | 2318 |
} |
2322 | 2319 |
} |
2323 | 2320 |
|
2324 | 2321 |
protected: |
2325 | 2322 |
|
2326 | 2323 |
FW* _forward; |
2327 | 2324 |
BK* _backward; |
2328 | 2325 |
|
2329 | 2326 |
}; |
2330 | 2327 |
|
2331 | 2328 |
/// \brief Returns a combined arc map |
2332 | 2329 |
/// |
2333 | 2330 |
/// This function just returns a combined arc map. |
2334 | 2331 |
template <typename FW, typename BK> |
2335 | 2332 |
static CombinedArcMap<FW, BK> |
2336 | 2333 |
combinedArcMap(FW& forward, BK& backward) { |
2337 | 2334 |
return CombinedArcMap<FW, BK>(forward, backward); |
2338 | 2335 |
} |
2339 | 2336 |
|
2340 | 2337 |
template <typename FW, typename BK> |
2341 | 2338 |
static CombinedArcMap<const FW, BK> |
2342 | 2339 |
combinedArcMap(const FW& forward, BK& backward) { |
2343 | 2340 |
return CombinedArcMap<const FW, BK>(forward, backward); |
2344 | 2341 |
} |
2345 | 2342 |
|
2346 | 2343 |
template <typename FW, typename BK> |
2347 | 2344 |
static CombinedArcMap<FW, const BK> |
2348 | 2345 |
combinedArcMap(FW& forward, const BK& backward) { |
2349 | 2346 |
return CombinedArcMap<FW, const BK>(forward, backward); |
2350 | 2347 |
} |
2351 | 2348 |
|
2352 | 2349 |
template <typename FW, typename BK> |
2353 | 2350 |
static CombinedArcMap<const FW, const BK> |
2354 | 2351 |
combinedArcMap(const FW& forward, const BK& backward) { |
2355 | 2352 |
return CombinedArcMap<const FW, const BK>(forward, backward); |
2356 | 2353 |
} |
2357 | 2354 |
|
2358 | 2355 |
}; |
2359 | 2356 |
|
2360 | 2357 |
/// \brief Returns a read-only Undirector adaptor |
2361 | 2358 |
/// |
2362 | 2359 |
/// This function just returns a read-only \ref Undirector adaptor. |
2363 | 2360 |
/// \ingroup graph_adaptors |
2364 | 2361 |
/// \relates Undirector |
2365 | 2362 |
template<typename DGR> |
2366 | 2363 |
Undirector<const DGR> undirector(const DGR& digraph) { |
2367 | 2364 |
return Undirector<const DGR>(digraph); |
2368 | 2365 |
} |
2369 | 2366 |
|
2370 | 2367 |
|
2371 | 2368 |
template <typename GR, typename DM> |
2372 | 2369 |
class OrienterBase { |
2373 | 2370 |
public: |
2374 | 2371 |
|
2375 | 2372 |
typedef GR Graph; |
2376 | 2373 |
typedef DM DirectionMap; |
2377 | 2374 |
|
2378 | 2375 |
typedef typename GR::Node Node; |
2379 | 2376 |
typedef typename GR::Edge Arc; |
2380 | 2377 |
|
2381 | 2378 |
void reverseArc(const Arc& arc) { |
2382 | 2379 |
_direction->set(arc, !(*_direction)[arc]); |
2383 | 2380 |
} |
2384 | 2381 |
|
2385 | 2382 |
void first(Node& i) const { _graph->first(i); } |
2386 | 2383 |
void first(Arc& i) const { _graph->first(i); } |
2387 | 2384 |
void firstIn(Arc& i, const Node& n) const { |
2388 | 2385 |
bool d = true; |
2389 | 2386 |
_graph->firstInc(i, d, n); |
2390 | 2387 |
while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); |
2391 | 2388 |
} |
2392 | 2389 |
void firstOut(Arc& i, const Node& n ) const { |
2393 | 2390 |
bool d = true; |
2394 | 2391 |
_graph->firstInc(i, d, n); |
2395 | 2392 |
while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); |
2396 | 2393 |
} |
2397 | 2394 |
|
2398 | 2395 |
void next(Node& i) const { _graph->next(i); } |
2399 | 2396 |
void next(Arc& i) const { _graph->next(i); } |
2400 | 2397 |
void nextIn(Arc& i) const { |
2401 | 2398 |
bool d = !(*_direction)[i]; |
2402 | 2399 |
_graph->nextInc(i, d); |
2403 | 2400 |
while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); |
2404 | 2401 |
} |
2405 | 2402 |
void nextOut(Arc& i) const { |
2406 | 2403 |
bool d = (*_direction)[i]; |
2407 | 2404 |
_graph->nextInc(i, d); |
2408 | 2405 |
while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); |
2409 | 2406 |
} |
2410 | 2407 |
|
2411 | 2408 |
Node source(const Arc& e) const { |
2412 | 2409 |
return (*_direction)[e] ? _graph->u(e) : _graph->v(e); |
2413 | 2410 |
} |
2414 | 2411 |
Node target(const Arc& e) const { |
2415 | 2412 |
return (*_direction)[e] ? _graph->v(e) : _graph->u(e); |
2416 | 2413 |
} |
2417 | 2414 |
|
2418 | 2415 |
typedef NodeNumTagIndicator<Graph> NodeNumTag; |
2419 | 2416 |
int nodeNum() const { return _graph->nodeNum(); } |
2420 | 2417 |
|
2421 | 2418 |
typedef EdgeNumTagIndicator<Graph> ArcNumTag; |
2422 | 2419 |
int arcNum() const { return _graph->edgeNum(); } |
2423 | 2420 |
|
2424 | 2421 |
typedef FindEdgeTagIndicator<Graph> FindArcTag; |
2425 | 2422 |
Arc findArc(const Node& u, const Node& v, |
2426 | 2423 |
const Arc& prev = INVALID) const { |
2427 | 2424 |
Arc arc = _graph->findEdge(u, v, prev); |
2428 | 2425 |
while (arc != INVALID && source(arc) != u) { |
2429 | 2426 |
arc = _graph->findEdge(u, v, arc); |
2430 | 2427 |
} |
2431 | 2428 |
return arc; |
2432 | 2429 |
} |
2433 | 2430 |
|
2434 | 2431 |
Node addNode() { |
2435 | 2432 |
return Node(_graph->addNode()); |
2436 | 2433 |
} |
2437 | 2434 |
|
2438 | 2435 |
Arc addArc(const Node& u, const Node& v) { |
2439 | 2436 |
Arc arc = _graph->addEdge(u, v); |
2440 | 2437 |
_direction->set(arc, _graph->u(arc) == u); |
2441 | 2438 |
return arc; |
2442 | 2439 |
} |
2443 | 2440 |
|
2444 | 2441 |
void erase(const Node& i) { _graph->erase(i); } |
2445 | 2442 |
void erase(const Arc& i) { _graph->erase(i); } |
2446 | 2443 |
|
2447 | 2444 |
void clear() { _graph->clear(); } |
2448 | 2445 |
|
2449 | 2446 |
int id(const Node& v) const { return _graph->id(v); } |
2450 | 2447 |
int id(const Arc& e) const { return _graph->id(e); } |
2451 | 2448 |
|
2452 | 2449 |
Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); } |
2453 | 2450 |
Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); } |
2454 | 2451 |
|
2455 | 2452 |
int maxNodeId() const { return _graph->maxNodeId(); } |
2456 | 2453 |
int maxArcId() const { return _graph->maxEdgeId(); } |
2457 | 2454 |
|
2458 | 2455 |
typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
2459 | 2456 |
NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
2460 | 2457 |
|
2461 | 2458 |
typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier; |
2462 | 2459 |
ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
2463 | 2460 |
|
2464 | 2461 |
template <typename V> |
2465 | 2462 |
class NodeMap : public GR::template NodeMap<V> { |
2466 | 2463 |
typedef typename GR::template NodeMap<V> Parent; |
2467 | 2464 |
|
2468 | 2465 |
public: |
2469 | 2466 |
|
2470 | 2467 |
explicit NodeMap(const OrienterBase<GR, DM>& adapter) |
2471 | 2468 |
: Parent(*adapter._graph) {} |
2472 | 2469 |
|
2473 | 2470 |
NodeMap(const OrienterBase<GR, DM>& adapter, const V& value) |
2474 | 2471 |
: Parent(*adapter._graph, value) {} |
2475 | 2472 |
|
2476 | 2473 |
private: |
2477 | 2474 |
NodeMap& operator=(const NodeMap& cmap) { |
2478 | 2475 |
return operator=<NodeMap>(cmap); |
2479 | 2476 |
} |
2480 | 2477 |
|
2481 | 2478 |
template <typename CMap> |
2482 | 2479 |
NodeMap& operator=(const CMap& cmap) { |
2483 | 2480 |
Parent::operator=(cmap); |
2484 | 2481 |
return *this; |
2485 | 2482 |
} |
2486 | 2483 |
|
2487 | 2484 |
}; |
2488 | 2485 |
|
2489 | 2486 |
template <typename V> |
2490 | 2487 |
class ArcMap : public GR::template EdgeMap<V> { |
2491 | 2488 |
typedef typename Graph::template EdgeMap<V> Parent; |
2492 | 2489 |
|
2493 | 2490 |
public: |
2494 | 2491 |
|
2495 | 2492 |
explicit ArcMap(const OrienterBase<GR, DM>& adapter) |
2496 | 2493 |
: Parent(*adapter._graph) { } |
2497 | 2494 |
|
2498 | 2495 |
ArcMap(const OrienterBase<GR, DM>& adapter, const V& value) |
2499 | 2496 |
: Parent(*adapter._graph, value) { } |
2500 | 2497 |
|
2501 | 2498 |
private: |
2502 | 2499 |
ArcMap& operator=(const ArcMap& cmap) { |
2503 | 2500 |
return operator=<ArcMap>(cmap); |
2504 | 2501 |
} |
2505 | 2502 |
|
2506 | 2503 |
template <typename CMap> |
2507 | 2504 |
ArcMap& operator=(const CMap& cmap) { |
2508 | 2505 |
Parent::operator=(cmap); |
2509 | 2506 |
return *this; |
2510 | 2507 |
} |
2511 | 2508 |
}; |
2512 | 2509 |
|
2513 | 2510 |
|
2514 | 2511 |
|
2515 | 2512 |
protected: |
2516 | 2513 |
Graph* _graph; |
2517 | 2514 |
DM* _direction; |
2518 | 2515 |
|
2519 | 2516 |
void initialize(GR& graph, DM& direction) { |
2520 | 2517 |
_graph = &graph; |
2521 | 2518 |
_direction = &direction; |
2522 | 2519 |
} |
2523 | 2520 |
|
2524 | 2521 |
}; |
2525 | 2522 |
|
2526 | 2523 |
/// \ingroup graph_adaptors |
2527 | 2524 |
/// |
2528 | 2525 |
/// \brief Adaptor class for orienting the edges of a graph to get a digraph |
2529 | 2526 |
/// |
2530 | 2527 |
/// Orienter adaptor can be used for orienting the edges of a graph to |
2531 | 2528 |
/// get a digraph. A \c bool edge map of the underlying graph must be |
2532 | 2529 |
/// specified, which define the direction of the arcs in the adaptor. |
2533 | 2530 |
/// The arcs can be easily reversed by the \c reverseArc() member function |
2534 | 2531 |
/// of the adaptor. |
2535 | 2532 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
2536 | 2533 |
/// |
2537 | 2534 |
/// The adapted graph can also be modified through this adaptor |
2538 | 2535 |
/// by adding or removing nodes or arcs, unless the \c GR template |
2539 | 2536 |
/// parameter is set to be \c const. |
2540 | 2537 |
/// |
2541 | 2538 |
/// \tparam GR The type of the adapted graph. |
2542 | 2539 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
2543 | 2540 |
/// It can also be specified to be \c const. |
2544 | 2541 |
/// \tparam DM The type of the direction map. |
2545 | 2542 |
/// It must be a \c bool (or convertible) edge map of the |
2546 | 2543 |
/// adapted graph. The default type is |
2547 | 2544 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
2548 | 2545 |
/// |
2549 | 2546 |
/// \note The \c Node type of this adaptor and the adapted graph are |
2550 | 2547 |
/// convertible to each other, moreover the \c Arc type of the adaptor |
2551 | 2548 |
/// and the \c Edge type of the adapted graph are also convertible to |
2552 | 2549 |
/// each other. |
2553 | 2550 |
#ifdef DOXYGEN |
2554 | 2551 |
template<typename GR, |
2555 | 2552 |
typename DM> |
2556 | 2553 |
class Orienter { |
2557 | 2554 |
#else |
2558 | 2555 |
template<typename GR, |
2559 | 2556 |
typename DM = typename GR::template EdgeMap<bool> > |
2560 | 2557 |
class Orienter : |
2561 | 2558 |
public DigraphAdaptorExtender<OrienterBase<GR, DM> > { |
2562 | 2559 |
#endif |
2563 | 2560 |
typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent; |
2564 | 2561 |
public: |
2565 | 2562 |
|
2566 | 2563 |
/// The type of the adapted graph. |
2567 | 2564 |
typedef GR Graph; |
2568 | 2565 |
/// The type of the direction edge map. |
2569 | 2566 |
typedef DM DirectionMap; |
2570 | 2567 |
|
2571 | 2568 |
typedef typename Parent::Arc Arc; |
2572 | 2569 |
|
2573 | 2570 |
protected: |
2574 | 2571 |
Orienter() { } |
2575 | 2572 |
|
2576 | 2573 |
public: |
2577 | 2574 |
|
2578 | 2575 |
/// \brief Constructor |
2579 | 2576 |
/// |
2580 | 2577 |
/// Constructor of the adaptor. |
2581 | 2578 |
Orienter(GR& graph, DM& direction) { |
2582 | 2579 |
Parent::initialize(graph, direction); |
2583 | 2580 |
} |
2584 | 2581 |
|
2585 | 2582 |
/// \brief Reverses the given arc |
2586 | 2583 |
/// |
2587 | 2584 |
/// This function reverses the given arc. |
2588 | 2585 |
/// It is done by simply negate the assigned value of \c a |
2589 | 2586 |
/// in the direction map. |
2590 | 2587 |
void reverseArc(const Arc& a) { |
2591 | 2588 |
Parent::reverseArc(a); |
2592 | 2589 |
} |
2593 | 2590 |
}; |
2594 | 2591 |
|
2595 | 2592 |
/// \brief Returns a read-only Orienter adaptor |
2596 | 2593 |
/// |
2597 | 2594 |
/// This function just returns a read-only \ref Orienter adaptor. |
2598 | 2595 |
/// \ingroup graph_adaptors |
2599 | 2596 |
/// \relates Orienter |
2600 | 2597 |
template<typename GR, typename DM> |
2601 | 2598 |
Orienter<const GR, DM> |
2602 | 2599 |
orienter(const GR& graph, DM& direction) { |
2603 | 2600 |
return Orienter<const GR, DM>(graph, direction); |
2604 | 2601 |
} |
2605 | 2602 |
|
2606 | 2603 |
template<typename GR, typename DM> |
2607 | 2604 |
Orienter<const GR, const DM> |
2608 | 2605 |
orienter(const GR& graph, const DM& direction) { |
2609 | 2606 |
return Orienter<const GR, const DM>(graph, direction); |
2610 | 2607 |
} |
2611 | 2608 |
|
2612 | 2609 |
namespace _adaptor_bits { |
2613 | 2610 |
|
2614 | 2611 |
template <typename DGR, typename CM, typename FM, typename TL> |
2615 | 2612 |
class ResForwardFilter { |
2616 | 2613 |
public: |
2617 | 2614 |
|
2618 | 2615 |
typedef typename DGR::Arc Key; |
2619 | 2616 |
typedef bool Value; |
2620 | 2617 |
|
2621 | 2618 |
private: |
2622 | 2619 |
|
2623 | 2620 |
const CM* _capacity; |
2624 | 2621 |
const FM* _flow; |
2625 | 2622 |
TL _tolerance; |
2626 | 2623 |
|
2627 | 2624 |
public: |
2628 | 2625 |
|
2629 | 2626 |
ResForwardFilter(const CM& capacity, const FM& flow, |
2630 | 2627 |
const TL& tolerance = TL()) |
2631 | 2628 |
: _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
2632 | 2629 |
|
2633 | 2630 |
bool operator[](const typename DGR::Arc& a) const { |
2634 | 2631 |
return _tolerance.positive((*_capacity)[a] - (*_flow)[a]); |
2635 | 2632 |
} |
2636 | 2633 |
}; |
2637 | 2634 |
|
2638 | 2635 |
template<typename DGR,typename CM, typename FM, typename TL> |
2639 | 2636 |
class ResBackwardFilter { |
2640 | 2637 |
public: |
2641 | 2638 |
|
2642 | 2639 |
typedef typename DGR::Arc Key; |
2643 | 2640 |
typedef bool Value; |
2644 | 2641 |
|
2645 | 2642 |
private: |
2646 | 2643 |
|
2647 | 2644 |
const CM* _capacity; |
2648 | 2645 |
const FM* _flow; |
2649 | 2646 |
TL _tolerance; |
2650 | 2647 |
|
2651 | 2648 |
public: |
2652 | 2649 |
|
2653 | 2650 |
ResBackwardFilter(const CM& capacity, const FM& flow, |
2654 | 2651 |
const TL& tolerance = TL()) |
2655 | 2652 |
: _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
2656 | 2653 |
|
2657 | 2654 |
bool operator[](const typename DGR::Arc& a) const { |
2658 | 2655 |
return _tolerance.positive((*_flow)[a]); |
2659 | 2656 |
} |
2660 | 2657 |
}; |
2661 | 2658 |
|
2662 | 2659 |
} |
2663 | 2660 |
|
2664 | 2661 |
/// \ingroup graph_adaptors |
2665 | 2662 |
/// |
2666 | 2663 |
/// \brief Adaptor class for composing the residual digraph for directed |
2667 | 2664 |
/// flow and circulation problems. |
2668 | 2665 |
/// |
2669 | 2666 |
/// ResidualDigraph can be used for composing the \e residual digraph |
2670 | 2667 |
/// for directed flow and circulation problems. Let \f$ G=(V, A) \f$ |
2671 | 2668 |
/// be a directed graph and let \f$ F \f$ be a number type. |
2672 | 2669 |
/// Let \f$ flow, cap: A\to F \f$ be functions on the arcs. |
2673 | 2670 |
/// This adaptor implements a digraph structure with node set \f$ V \f$ |
2674 | 2671 |
/// and arc set \f$ A_{forward}\cup A_{backward} \f$, |
2675 | 2672 |
/// where \f$ A_{forward}=\{uv : uv\in A, flow(uv)<cap(uv)\} \f$ and |
2676 | 2673 |
/// \f$ A_{backward}=\{vu : uv\in A, flow(uv)>0\} \f$, i.e. the so |
2677 | 2674 |
/// called residual digraph. |
2678 | 2675 |
/// When the union \f$ A_{forward}\cup A_{backward} \f$ is taken, |
2679 | 2676 |
/// multiplicities are counted, i.e. the adaptor has exactly |
2680 | 2677 |
/// \f$ |A_{forward}| + |A_{backward}|\f$ arcs (it may have parallel |
2681 | 2678 |
/// arcs). |
2682 | 2679 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
2683 | 2680 |
/// |
2684 | 2681 |
/// \tparam DGR The type of the adapted digraph. |
2685 | 2682 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
2686 | 2683 |
/// It is implicitly \c const. |
2687 | 2684 |
/// \tparam CM The type of the capacity map. |
2688 | 2685 |
/// It must be an arc map of some numerical type, which defines |
2689 | 2686 |
/// the capacities in the flow problem. It is implicitly \c const. |
2690 | 2687 |
/// The default type is |
2691 | 2688 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
2692 | 2689 |
/// \tparam FM The type of the flow map. |
2693 | 2690 |
/// It must be an arc map of some numerical type, which defines |
2694 | 2691 |
/// the flow values in the flow problem. The default type is \c CM. |
2695 | 2692 |
/// \tparam TL The tolerance type for handling inexact computation. |
2696 | 2693 |
/// The default tolerance type depends on the value type of the |
2697 | 2694 |
/// capacity map. |
2698 | 2695 |
/// |
2699 | 2696 |
/// \note This adaptor is implemented using Undirector and FilterArcs |
2700 | 2697 |
/// adaptors. |
2701 | 2698 |
/// |
2702 | 2699 |
/// \note The \c Node type of this adaptor and the adapted digraph are |
2703 | 2700 |
/// convertible to each other, moreover the \c Arc type of the adaptor |
2704 | 2701 |
/// is convertible to the \c Arc type of the adapted digraph. |
2705 | 2702 |
#ifdef DOXYGEN |
2706 | 2703 |
template<typename DGR, typename CM, typename FM, typename TL> |
2707 | 2704 |
class ResidualDigraph |
2708 | 2705 |
#else |
2709 | 2706 |
template<typename DGR, |
2710 | 2707 |
typename CM = typename DGR::template ArcMap<int>, |
2711 | 2708 |
typename FM = CM, |
2712 | 2709 |
typename TL = Tolerance<typename CM::Value> > |
2713 | 2710 |
class ResidualDigraph |
2714 | 2711 |
: public SubDigraph< |
2715 | 2712 |
Undirector<const DGR>, |
2716 | 2713 |
ConstMap<typename DGR::Node, Const<bool, true> >, |
2717 | 2714 |
typename Undirector<const DGR>::template CombinedArcMap< |
2718 | 2715 |
_adaptor_bits::ResForwardFilter<const DGR, CM, FM, TL>, |
2719 | 2716 |
_adaptor_bits::ResBackwardFilter<const DGR, CM, FM, TL> > > |
2720 | 2717 |
#endif |
2721 | 2718 |
{ |
2722 | 2719 |
public: |
2723 | 2720 |
|
2724 | 2721 |
/// The type of the underlying digraph. |
2725 | 2722 |
typedef DGR Digraph; |
2726 | 2723 |
/// The type of the capacity map. |
2727 | 2724 |
typedef CM CapacityMap; |
2728 | 2725 |
/// The type of the flow map. |
2729 | 2726 |
typedef FM FlowMap; |
2730 | 2727 |
/// The tolerance type. |
2731 | 2728 |
typedef TL Tolerance; |
2732 | 2729 |
|
2733 | 2730 |
typedef typename CapacityMap::Value Value; |
2734 | 2731 |
typedef ResidualDigraph Adaptor; |
2735 | 2732 |
|
2736 | 2733 |
protected: |
2737 | 2734 |
|
2738 | 2735 |
typedef Undirector<const Digraph> Undirected; |
2739 | 2736 |
|
2740 | 2737 |
typedef ConstMap<typename DGR::Node, Const<bool, true> > NodeFilter; |
2741 | 2738 |
|
2742 | 2739 |
typedef _adaptor_bits::ResForwardFilter<const DGR, CM, |
2743 | 2740 |
FM, TL> ForwardFilter; |
2744 | 2741 |
|
2745 | 2742 |
typedef _adaptor_bits::ResBackwardFilter<const DGR, CM, |
2746 | 2743 |
FM, TL> BackwardFilter; |
2747 | 2744 |
|
2748 | 2745 |
typedef typename Undirected:: |
2749 | 2746 |
template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter; |
2750 | 2747 |
|
2751 | 2748 |
typedef SubDigraph<Undirected, NodeFilter, ArcFilter> Parent; |
2752 | 2749 |
|
2753 | 2750 |
const CapacityMap* _capacity; |
2754 | 2751 |
FlowMap* _flow; |
2755 | 2752 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup graph_concepts |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of Undirected Graphs. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPTS_GRAPH_H |
24 | 24 |
#define LEMON_CONCEPTS_GRAPH_H |
25 | 25 |
|
26 | 26 |
#include <lemon/concepts/graph_components.h> |
27 | 27 |
#include <lemon/core.h> |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
namespace concepts { |
31 | 31 |
|
32 | 32 |
/// \ingroup graph_concepts |
33 | 33 |
/// |
34 | 34 |
/// \brief Class describing the concept of Undirected Graphs. |
35 | 35 |
/// |
36 | 36 |
/// This class describes the common interface of all Undirected |
37 | 37 |
/// Graphs. |
38 | 38 |
/// |
39 | 39 |
/// As all concept describing classes it provides only interface |
40 | 40 |
/// without any sensible implementation. So any algorithm for |
41 | 41 |
/// undirected graph should compile with this class, but it will not |
42 | 42 |
/// run properly, of course. |
43 | 43 |
/// |
44 | 44 |
/// The LEMON undirected graphs also fulfill the concept of |
45 | 45 |
/// directed graphs (\ref lemon::concepts::Digraph "Digraph |
46 | 46 |
/// Concept"). Each edges can be seen as two opposite |
47 | 47 |
/// directed arc and consequently the undirected graph can be |
48 | 48 |
/// seen as the direceted graph of these directed arcs. The |
49 | 49 |
/// Graph has the Edge inner class for the edges and |
50 | 50 |
/// the Arc type for the directed arcs. The Arc type is |
51 | 51 |
/// convertible to Edge or inherited from it so from a directed |
52 | 52 |
/// arc we can get the represented edge. |
53 | 53 |
/// |
54 | 54 |
/// In the sense of the LEMON each edge has a default |
55 | 55 |
/// direction (it should be in every computer implementation, |
56 | 56 |
/// because the order of edge's nodes defines an |
57 | 57 |
/// orientation). With the default orientation we can define that |
58 | 58 |
/// the directed arc is forward or backward directed. With the \c |
59 | 59 |
/// direction() and \c direct() function we can get the direction |
60 | 60 |
/// of the directed arc and we can direct an edge. |
61 | 61 |
/// |
62 | 62 |
/// The EdgeIt is an iterator for the edges. We can use |
63 | 63 |
/// the EdgeMap to map values for the edges. The InArcIt and |
64 | 64 |
/// OutArcIt iterates on the same edges but with opposite |
65 | 65 |
/// direction. The IncEdgeIt iterates also on the same edges |
66 | 66 |
/// as the OutArcIt and InArcIt but it is not convertible to Arc just |
67 | 67 |
/// to Edge. |
68 | 68 |
class Graph { |
69 | 69 |
public: |
70 | 70 |
/// \brief The undirected graph should be tagged by the |
71 | 71 |
/// UndirectedTag. |
72 | 72 |
/// |
73 | 73 |
/// The undirected graph should be tagged by the UndirectedTag. This |
74 | 74 |
/// tag helps the enable_if technics to make compile time |
75 | 75 |
/// specializations for undirected graphs. |
76 | 76 |
typedef True UndirectedTag; |
77 | 77 |
|
78 | 78 |
/// \brief The base type of node iterators, |
79 | 79 |
/// or in other words, the trivial node iterator. |
80 | 80 |
/// |
81 | 81 |
/// This is the base type of each node iterator, |
82 | 82 |
/// thus each kind of node iterator converts to this. |
83 | 83 |
/// More precisely each kind of node iterator should be inherited |
84 | 84 |
/// from the trivial node iterator. |
85 | 85 |
class Node { |
86 | 86 |
public: |
87 | 87 |
/// Default constructor |
88 | 88 |
|
89 | 89 |
/// @warning The default constructor sets the iterator |
90 | 90 |
/// to an undefined value. |
91 | 91 |
Node() { } |
92 | 92 |
/// Copy constructor. |
93 | 93 |
|
94 | 94 |
/// Copy constructor. |
95 | 95 |
/// |
96 | 96 |
Node(const Node&) { } |
97 | 97 |
|
98 | 98 |
/// Invalid constructor \& conversion. |
99 | 99 |
|
100 | 100 |
/// This constructor initializes the iterator to be invalid. |
101 | 101 |
/// \sa Invalid for more details. |
102 | 102 |
Node(Invalid) { } |
103 | 103 |
/// Equality operator |
104 | 104 |
|
105 | 105 |
/// Two iterators are equal if and only if they point to the |
106 | 106 |
/// same object or both are invalid. |
107 | 107 |
bool operator==(Node) const { return true; } |
108 | 108 |
|
109 | 109 |
/// Inequality operator |
110 | 110 |
|
111 | 111 |
/// \sa operator==(Node n) |
112 | 112 |
/// |
113 | 113 |
bool operator!=(Node) const { return true; } |
114 | 114 |
|
115 | 115 |
/// Artificial ordering operator. |
116 | 116 |
|
117 | 117 |
/// To allow the use of graph descriptors as key type in std::map or |
118 | 118 |
/// similar associative container we require this. |
119 | 119 |
/// |
120 | 120 |
/// \note This operator only have to define some strict ordering of |
121 | 121 |
/// the items; this order has nothing to do with the iteration |
122 | 122 |
/// ordering of the items. |
123 | 123 |
bool operator<(Node) const { return false; } |
124 | 124 |
|
125 | 125 |
}; |
126 | 126 |
|
127 | 127 |
/// This iterator goes through each node. |
128 | 128 |
|
129 | 129 |
/// This iterator goes through each node. |
130 | 130 |
/// Its usage is quite simple, for example you can count the number |
131 | 131 |
/// of nodes in graph \c g of type \c Graph like this: |
132 | 132 |
///\code |
133 | 133 |
/// int count=0; |
134 | 134 |
/// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count; |
135 | 135 |
///\endcode |
136 | 136 |
class NodeIt : public Node { |
137 | 137 |
public: |
138 | 138 |
/// Default constructor |
139 | 139 |
|
140 | 140 |
/// @warning The default constructor sets the iterator |
141 | 141 |
/// to an undefined value. |
142 | 142 |
NodeIt() { } |
143 | 143 |
/// Copy constructor. |
144 | 144 |
|
145 | 145 |
/// Copy constructor. |
146 | 146 |
/// |
147 | 147 |
NodeIt(const NodeIt& n) : Node(n) { } |
148 | 148 |
/// Invalid constructor \& conversion. |
149 | 149 |
|
150 | 150 |
/// Initialize the iterator to be invalid. |
151 | 151 |
/// \sa Invalid for more details. |
152 | 152 |
NodeIt(Invalid) { } |
153 | 153 |
/// Sets the iterator to the first node. |
154 | 154 |
|
155 | 155 |
/// Sets the iterator to the first node of \c g. |
156 | 156 |
/// |
157 | 157 |
NodeIt(const Graph&) { } |
158 | 158 |
/// Node -> NodeIt conversion. |
159 | 159 |
|
160 | 160 |
/// Sets the iterator to the node of \c the graph pointed by |
161 | 161 |
/// the trivial iterator. |
162 | 162 |
/// This feature necessitates that each time we |
163 | 163 |
/// iterate the arc-set, the iteration order is the same. |
164 | 164 |
NodeIt(const Graph&, const Node&) { } |
165 | 165 |
/// Next node. |
166 | 166 |
|
167 | 167 |
/// Assign the iterator to the next node. |
168 | 168 |
/// |
169 | 169 |
NodeIt& operator++() { return *this; } |
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
|
173 | 173 |
/// The base type of the edge iterators. |
174 | 174 |
|
175 | 175 |
/// The base type of the edge iterators. |
176 | 176 |
/// |
177 | 177 |
class Edge { |
178 | 178 |
public: |
179 | 179 |
/// Default constructor |
180 | 180 |
|
181 | 181 |
/// @warning The default constructor sets the iterator |
182 | 182 |
/// to an undefined value. |
183 | 183 |
Edge() { } |
184 | 184 |
/// Copy constructor. |
185 | 185 |
|
186 | 186 |
/// Copy constructor. |
187 | 187 |
/// |
188 | 188 |
Edge(const Edge&) { } |
189 | 189 |
/// Initialize the iterator to be invalid. |
190 | 190 |
|
191 | 191 |
/// Initialize the iterator to be invalid. |
192 | 192 |
/// |
193 | 193 |
Edge(Invalid) { } |
194 | 194 |
/// Equality operator |
195 | 195 |
|
196 | 196 |
/// Two iterators are equal if and only if they point to the |
197 | 197 |
/// same object or both are invalid. |
198 | 198 |
bool operator==(Edge) const { return true; } |
199 | 199 |
/// Inequality operator |
200 | 200 |
|
201 | 201 |
/// \sa operator==(Edge n) |
202 | 202 |
/// |
203 | 203 |
bool operator!=(Edge) const { return true; } |
204 | 204 |
|
205 | 205 |
/// Artificial ordering operator. |
206 | 206 |
|
207 | 207 |
/// To allow the use of graph descriptors as key type in std::map or |
208 | 208 |
/// similar associative container we require this. |
209 | 209 |
/// |
210 | 210 |
/// \note This operator only have to define some strict ordering of |
211 | 211 |
/// the items; this order has nothing to do with the iteration |
212 | 212 |
/// ordering of the items. |
213 | 213 |
bool operator<(Edge) const { return false; } |
214 | 214 |
}; |
215 | 215 |
|
216 | 216 |
/// This iterator goes through each edge. |
217 | 217 |
|
218 | 218 |
/// This iterator goes through each edge of a graph. |
219 | 219 |
/// Its usage is quite simple, for example you can count the number |
220 | 220 |
/// of edges in a graph \c g of type \c Graph as follows: |
221 | 221 |
///\code |
222 | 222 |
/// int count=0; |
223 | 223 |
/// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count; |
224 | 224 |
///\endcode |
225 | 225 |
class EdgeIt : public Edge { |
226 | 226 |
public: |
227 | 227 |
/// Default constructor |
228 | 228 |
|
229 | 229 |
/// @warning The default constructor sets the iterator |
230 | 230 |
/// to an undefined value. |
231 | 231 |
EdgeIt() { } |
232 | 232 |
/// Copy constructor. |
233 | 233 |
|
234 | 234 |
/// Copy constructor. |
235 | 235 |
/// |
236 | 236 |
EdgeIt(const EdgeIt& e) : Edge(e) { } |
237 | 237 |
/// Initialize the iterator to be invalid. |
238 | 238 |
|
239 | 239 |
/// Initialize the iterator to be invalid. |
240 | 240 |
/// |
241 | 241 |
EdgeIt(Invalid) { } |
242 | 242 |
/// This constructor sets the iterator to the first edge. |
243 | 243 |
|
244 | 244 |
/// This constructor sets the iterator to the first edge. |
245 | 245 |
EdgeIt(const Graph&) { } |
246 | 246 |
/// Edge -> EdgeIt conversion |
247 | 247 |
|
248 | 248 |
/// Sets the iterator to the value of the trivial iterator. |
249 | 249 |
/// This feature necessitates that each time we |
250 | 250 |
/// iterate the edge-set, the iteration order is the |
251 | 251 |
/// same. |
252 | 252 |
EdgeIt(const Graph&, const Edge&) { } |
253 | 253 |
/// Next edge |
254 | 254 |
|
255 | 255 |
/// Assign the iterator to the next edge. |
256 | 256 |
EdgeIt& operator++() { return *this; } |
257 | 257 |
}; |
258 | 258 |
|
259 | 259 |
/// \brief This iterator goes trough the incident undirected |
260 | 260 |
/// arcs of a node. |
261 | 261 |
/// |
262 | 262 |
/// This iterator goes trough the incident edges |
263 | 263 |
/// of a certain node of a graph. You should assume that the |
264 | 264 |
/// loop arcs will be iterated twice. |
265 | 265 |
/// |
266 | 266 |
/// Its usage is quite simple, for example you can compute the |
267 | 267 |
/// degree (i.e. count the number of incident arcs of a node \c n |
268 | 268 |
/// in graph \c g of type \c Graph as follows. |
269 | 269 |
/// |
270 | 270 |
///\code |
271 | 271 |
/// int count=0; |
272 | 272 |
/// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count; |
273 | 273 |
///\endcode |
274 | 274 |
class IncEdgeIt : public Edge { |
275 | 275 |
public: |
276 | 276 |
/// Default constructor |
277 | 277 |
|
278 | 278 |
/// @warning The default constructor sets the iterator |
279 | 279 |
/// to an undefined value. |
280 | 280 |
IncEdgeIt() { } |
281 | 281 |
/// Copy constructor. |
282 | 282 |
|
283 | 283 |
/// Copy constructor. |
284 | 284 |
/// |
285 | 285 |
IncEdgeIt(const IncEdgeIt& e) : Edge(e) { } |
286 | 286 |
/// Initialize the iterator to be invalid. |
287 | 287 |
|
288 | 288 |
/// Initialize the iterator to be invalid. |
289 | 289 |
/// |
290 | 290 |
IncEdgeIt(Invalid) { } |
291 | 291 |
/// This constructor sets the iterator to first incident arc. |
292 | 292 |
|
293 | 293 |
/// This constructor set the iterator to the first incident arc of |
294 | 294 |
/// the node. |
295 | 295 |
IncEdgeIt(const Graph&, const Node&) { } |
296 | 296 |
/// Edge -> IncEdgeIt conversion |
297 | 297 |
|
298 | 298 |
/// Sets the iterator to the value of the trivial iterator \c e. |
299 | 299 |
/// This feature necessitates that each time we |
300 | 300 |
/// iterate the arc-set, the iteration order is the same. |
301 | 301 |
IncEdgeIt(const Graph&, const Edge&) { } |
302 | 302 |
/// Next incident arc |
303 | 303 |
|
304 | 304 |
/// Assign the iterator to the next incident arc |
305 | 305 |
/// of the corresponding node. |
306 | 306 |
IncEdgeIt& operator++() { return *this; } |
307 | 307 |
}; |
308 | 308 |
|
309 | 309 |
/// The directed arc type. |
310 | 310 |
|
311 | 311 |
/// The directed arc type. It can be converted to the |
312 | 312 |
/// edge or it should be inherited from the undirected |
313 |
/// arc. |
|
314 |
class Arc : public Edge { |
|
313 |
/// edge. |
|
314 |
class Arc { |
|
315 | 315 |
public: |
316 | 316 |
/// Default constructor |
317 | 317 |
|
318 | 318 |
/// @warning The default constructor sets the iterator |
319 | 319 |
/// to an undefined value. |
320 | 320 |
Arc() { } |
321 | 321 |
/// Copy constructor. |
322 | 322 |
|
323 | 323 |
/// Copy constructor. |
324 | 324 |
/// |
325 |
Arc(const Arc& |
|
325 |
Arc(const Arc&) { } |
|
326 | 326 |
/// Initialize the iterator to be invalid. |
327 | 327 |
|
328 | 328 |
/// Initialize the iterator to be invalid. |
329 | 329 |
/// |
330 | 330 |
Arc(Invalid) { } |
331 | 331 |
/// Equality operator |
332 | 332 |
|
333 | 333 |
/// Two iterators are equal if and only if they point to the |
334 | 334 |
/// same object or both are invalid. |
335 | 335 |
bool operator==(Arc) const { return true; } |
336 | 336 |
/// Inequality operator |
337 | 337 |
|
338 | 338 |
/// \sa operator==(Arc n) |
339 | 339 |
/// |
340 | 340 |
bool operator!=(Arc) const { return true; } |
341 | 341 |
|
342 | 342 |
/// Artificial ordering operator. |
343 | 343 |
|
344 | 344 |
/// To allow the use of graph descriptors as key type in std::map or |
345 | 345 |
/// similar associative container we require this. |
346 | 346 |
/// |
347 | 347 |
/// \note This operator only have to define some strict ordering of |
348 | 348 |
/// the items; this order has nothing to do with the iteration |
349 | 349 |
/// ordering of the items. |
350 | 350 |
bool operator<(Arc) const { return false; } |
351 | 351 |
|
352 |
/// Converison to Edge |
|
353 |
operator Edge() const { return Edge(); } |
|
352 | 354 |
}; |
353 | 355 |
/// This iterator goes through each directed arc. |
354 | 356 |
|
355 | 357 |
/// This iterator goes through each arc of a graph. |
356 | 358 |
/// Its usage is quite simple, for example you can count the number |
357 | 359 |
/// of arcs in a graph \c g of type \c Graph as follows: |
358 | 360 |
///\code |
359 | 361 |
/// int count=0; |
360 | 362 |
/// for(Graph::ArcIt e(g); e!=INVALID; ++e) ++count; |
361 | 363 |
///\endcode |
362 | 364 |
class ArcIt : public Arc { |
363 | 365 |
public: |
364 | 366 |
/// Default constructor |
365 | 367 |
|
366 | 368 |
/// @warning The default constructor sets the iterator |
367 | 369 |
/// to an undefined value. |
368 | 370 |
ArcIt() { } |
369 | 371 |
/// Copy constructor. |
370 | 372 |
|
371 | 373 |
/// Copy constructor. |
372 | 374 |
/// |
373 | 375 |
ArcIt(const ArcIt& e) : Arc(e) { } |
374 | 376 |
/// Initialize the iterator to be invalid. |
375 | 377 |
|
376 | 378 |
/// Initialize the iterator to be invalid. |
377 | 379 |
/// |
378 | 380 |
ArcIt(Invalid) { } |
379 | 381 |
/// This constructor sets the iterator to the first arc. |
380 | 382 |
|
381 | 383 |
/// This constructor sets the iterator to the first arc of \c g. |
382 | 384 |
///@param g the graph |
383 | 385 |
ArcIt(const Graph &g) { ignore_unused_variable_warning(g); } |
384 | 386 |
/// Arc -> ArcIt conversion |
385 | 387 |
|
386 | 388 |
/// Sets the iterator to the value of the trivial iterator \c e. |
387 | 389 |
/// This feature necessitates that each time we |
388 | 390 |
/// iterate the arc-set, the iteration order is the same. |
389 | 391 |
ArcIt(const Graph&, const Arc&) { } |
390 | 392 |
///Next arc |
391 | 393 |
|
392 | 394 |
/// Assign the iterator to the next arc. |
393 | 395 |
ArcIt& operator++() { return *this; } |
394 | 396 |
}; |
395 | 397 |
|
396 | 398 |
/// This iterator goes trough the outgoing directed arcs of a node. |
397 | 399 |
|
398 | 400 |
/// This iterator goes trough the \e outgoing arcs of a certain node |
399 | 401 |
/// of a graph. |
400 | 402 |
/// Its usage is quite simple, for example you can count the number |
401 | 403 |
/// of outgoing arcs of a node \c n |
402 | 404 |
/// in graph \c g of type \c Graph as follows. |
403 | 405 |
///\code |
404 | 406 |
/// int count=0; |
405 | 407 |
/// for (Graph::OutArcIt e(g, n); e!=INVALID; ++e) ++count; |
406 | 408 |
///\endcode |
407 | 409 |
|
408 | 410 |
class OutArcIt : public Arc { |
409 | 411 |
public: |
410 | 412 |
/// Default constructor |
411 | 413 |
|
412 | 414 |
/// @warning The default constructor sets the iterator |
413 | 415 |
/// to an undefined value. |
414 | 416 |
OutArcIt() { } |
415 | 417 |
/// Copy constructor. |
416 | 418 |
|
417 | 419 |
/// Copy constructor. |
418 | 420 |
/// |
419 | 421 |
OutArcIt(const OutArcIt& e) : Arc(e) { } |
420 | 422 |
/// Initialize the iterator to be invalid. |
421 | 423 |
|
422 | 424 |
/// Initialize the iterator to be invalid. |
423 | 425 |
/// |
424 | 426 |
OutArcIt(Invalid) { } |
425 | 427 |
/// This constructor sets the iterator to the first outgoing arc. |
426 | 428 |
|
427 | 429 |
/// This constructor sets the iterator to the first outgoing arc of |
428 | 430 |
/// the node. |
429 | 431 |
///@param n the node |
430 | 432 |
///@param g the graph |
431 | 433 |
OutArcIt(const Graph& n, const Node& g) { |
432 | 434 |
ignore_unused_variable_warning(n); |
433 | 435 |
ignore_unused_variable_warning(g); |
434 | 436 |
} |
435 | 437 |
/// Arc -> OutArcIt conversion |
436 | 438 |
|
437 | 439 |
/// Sets the iterator to the value of the trivial iterator. |
438 | 440 |
/// This feature necessitates that each time we |
439 | 441 |
/// iterate the arc-set, the iteration order is the same. |
440 | 442 |
OutArcIt(const Graph&, const Arc&) { } |
441 | 443 |
///Next outgoing arc |
442 | 444 |
|
443 | 445 |
/// Assign the iterator to the next |
444 | 446 |
/// outgoing arc of the corresponding node. |
445 | 447 |
OutArcIt& operator++() { return *this; } |
446 | 448 |
}; |
447 | 449 |
|
448 | 450 |
/// This iterator goes trough the incoming directed arcs of a node. |
449 | 451 |
|
450 | 452 |
/// This iterator goes trough the \e incoming arcs of a certain node |
451 | 453 |
/// of a graph. |
452 | 454 |
/// Its usage is quite simple, for example you can count the number |
453 | 455 |
/// of outgoing arcs of a node \c n |
454 | 456 |
/// in graph \c g of type \c Graph as follows. |
455 | 457 |
///\code |
456 | 458 |
/// int count=0; |
457 | 459 |
/// for(Graph::InArcIt e(g, n); e!=INVALID; ++e) ++count; |
458 | 460 |
///\endcode |
459 | 461 |
|
460 | 462 |
class InArcIt : public Arc { |
461 | 463 |
public: |
462 | 464 |
/// Default constructor |
463 | 465 |
|
464 | 466 |
/// @warning The default constructor sets the iterator |
465 | 467 |
/// to an undefined value. |
466 | 468 |
InArcIt() { } |
467 | 469 |
/// Copy constructor. |
468 | 470 |
|
469 | 471 |
/// Copy constructor. |
470 | 472 |
/// |
471 | 473 |
InArcIt(const InArcIt& e) : Arc(e) { } |
472 | 474 |
/// Initialize the iterator to be invalid. |
473 | 475 |
|
474 | 476 |
/// Initialize the iterator to be invalid. |
475 | 477 |
/// |
476 | 478 |
InArcIt(Invalid) { } |
477 | 479 |
/// This constructor sets the iterator to first incoming arc. |
478 | 480 |
|
479 | 481 |
/// This constructor set the iterator to the first incoming arc of |
480 | 482 |
/// the node. |
481 | 483 |
///@param n the node |
482 | 484 |
///@param g the graph |
483 | 485 |
InArcIt(const Graph& g, const Node& n) { |
484 | 486 |
ignore_unused_variable_warning(n); |
485 | 487 |
ignore_unused_variable_warning(g); |
486 | 488 |
} |
487 | 489 |
/// Arc -> InArcIt conversion |
488 | 490 |
|
489 | 491 |
/// Sets the iterator to the value of the trivial iterator \c e. |
490 | 492 |
/// This feature necessitates that each time we |
491 | 493 |
/// iterate the arc-set, the iteration order is the same. |
492 | 494 |
InArcIt(const Graph&, const Arc&) { } |
493 | 495 |
/// Next incoming arc |
494 | 496 |
|
495 | 497 |
/// Assign the iterator to the next inarc of the corresponding node. |
496 | 498 |
/// |
497 | 499 |
InArcIt& operator++() { return *this; } |
498 | 500 |
}; |
499 | 501 |
|
500 | 502 |
/// \brief Reference map of the nodes to type \c T. |
501 | 503 |
/// |
502 | 504 |
/// Reference map of the nodes to type \c T. |
503 | 505 |
template<class T> |
504 | 506 |
class NodeMap : public ReferenceMap<Node, T, T&, const T&> |
505 | 507 |
{ |
506 | 508 |
public: |
507 | 509 |
|
508 | 510 |
///\e |
509 | 511 |
NodeMap(const Graph&) { } |
510 | 512 |
///\e |
511 | 513 |
NodeMap(const Graph&, T) { } |
512 | 514 |
|
513 | 515 |
private: |
514 | 516 |
///Copy constructor |
515 | 517 |
NodeMap(const NodeMap& nm) : |
516 | 518 |
ReferenceMap<Node, T, T&, const T&>(nm) { } |
517 | 519 |
///Assignment operator |
518 | 520 |
template <typename CMap> |
519 | 521 |
NodeMap& operator=(const CMap&) { |
520 | 522 |
checkConcept<ReadMap<Node, T>, CMap>(); |
521 | 523 |
return *this; |
522 | 524 |
} |
523 | 525 |
}; |
524 | 526 |
|
525 | 527 |
/// \brief Reference map of the arcs to type \c T. |
526 | 528 |
/// |
527 | 529 |
/// Reference map of the arcs to type \c T. |
528 | 530 |
template<class T> |
529 | 531 |
class ArcMap : public ReferenceMap<Arc, T, T&, const T&> |
530 | 532 |
{ |
531 | 533 |
public: |
532 | 534 |
|
533 | 535 |
///\e |
534 | 536 |
ArcMap(const Graph&) { } |
535 | 537 |
///\e |
536 | 538 |
ArcMap(const Graph&, T) { } |
537 | 539 |
private: |
538 | 540 |
///Copy constructor |
539 | 541 |
ArcMap(const ArcMap& em) : |
540 | 542 |
ReferenceMap<Arc, T, T&, const T&>(em) { } |
541 | 543 |
///Assignment operator |
542 | 544 |
template <typename CMap> |
543 | 545 |
ArcMap& operator=(const CMap&) { |
544 | 546 |
checkConcept<ReadMap<Arc, T>, CMap>(); |
545 | 547 |
return *this; |
546 | 548 |
} |
547 | 549 |
}; |
548 | 550 |
|
549 | 551 |
/// Reference map of the edges to type \c T. |
550 | 552 |
|
551 | 553 |
/// Reference map of the edges to type \c T. |
552 | 554 |
template<class T> |
553 | 555 |
class EdgeMap : public ReferenceMap<Edge, T, T&, const T&> |
554 | 556 |
{ |
555 | 557 |
public: |
556 | 558 |
|
557 | 559 |
///\e |
558 | 560 |
EdgeMap(const Graph&) { } |
559 | 561 |
///\e |
560 | 562 |
EdgeMap(const Graph&, T) { } |
561 | 563 |
private: |
562 | 564 |
///Copy constructor |
563 | 565 |
EdgeMap(const EdgeMap& em) : |
564 | 566 |
ReferenceMap<Edge, T, T&, const T&>(em) {} |
565 | 567 |
///Assignment operator |
566 | 568 |
template <typename CMap> |
567 | 569 |
EdgeMap& operator=(const CMap&) { |
568 | 570 |
checkConcept<ReadMap<Edge, T>, CMap>(); |
569 | 571 |
return *this; |
570 | 572 |
} |
571 | 573 |
}; |
572 | 574 |
|
573 | 575 |
/// \brief Direct the given edge. |
574 | 576 |
/// |
575 | 577 |
/// Direct the given edge. The returned arc source |
576 | 578 |
/// will be the given node. |
577 | 579 |
Arc direct(const Edge&, const Node&) const { |
578 | 580 |
return INVALID; |
579 | 581 |
} |
580 | 582 |
|
581 | 583 |
/// \brief Direct the given edge. |
582 | 584 |
/// |
583 | 585 |
/// Direct the given edge. The returned arc |
584 | 586 |
/// represents the given edge and the direction comes |
585 | 587 |
/// from the bool parameter. The source of the edge and |
586 | 588 |
/// the directed arc is the same when the given bool is true. |
587 | 589 |
Arc direct(const Edge&, bool) const { |
588 | 590 |
return INVALID; |
589 | 591 |
} |
590 | 592 |
|
591 | 593 |
/// \brief Returns true if the arc has default orientation. |
592 | 594 |
/// |
593 | 595 |
/// Returns whether the given directed arc is same orientation as |
594 | 596 |
/// the corresponding edge's default orientation. |
595 | 597 |
bool direction(Arc) const { return true; } |
596 | 598 |
|
597 | 599 |
/// \brief Returns the opposite directed arc. |
598 | 600 |
/// |
599 | 601 |
/// Returns the opposite directed arc. |
600 | 602 |
Arc oppositeArc(Arc) const { return INVALID; } |
601 | 603 |
|
602 | 604 |
/// \brief Opposite node on an arc |
603 | 605 |
/// |
604 | 606 |
/// \return The opposite of the given node on the given edge. |
605 | 607 |
Node oppositeNode(Node, Edge) const { return INVALID; } |
606 | 608 |
|
607 | 609 |
/// \brief First node of the edge. |
608 | 610 |
/// |
609 | 611 |
/// \return The first node of the given edge. |
610 | 612 |
/// |
611 | 613 |
/// Naturally edges don't have direction and thus |
612 | 614 |
/// don't have source and target node. However we use \c u() and \c v() |
613 | 615 |
/// methods to query the two nodes of the arc. The direction of the |
614 | 616 |
/// arc which arises this way is called the inherent direction of the |
615 | 617 |
/// edge, and is used to define the "default" direction |
616 | 618 |
/// of the directed versions of the arcs. |
617 | 619 |
/// \sa v() |
618 | 620 |
/// \sa direction() |
619 | 621 |
Node u(Edge) const { return INVALID; } |
620 | 622 |
|
621 | 623 |
/// \brief Second node of the edge. |
622 | 624 |
/// |
623 | 625 |
/// \return The second node of the given edge. |
624 | 626 |
/// |
625 | 627 |
/// Naturally edges don't have direction and thus |
626 | 628 |
/// don't have source and target node. However we use \c u() and \c v() |
627 | 629 |
/// methods to query the two nodes of the arc. The direction of the |
628 | 630 |
/// arc which arises this way is called the inherent direction of the |
629 | 631 |
/// edge, and is used to define the "default" direction |
630 | 632 |
/// of the directed versions of the arcs. |
631 | 633 |
/// \sa u() |
632 | 634 |
/// \sa direction() |
633 | 635 |
Node v(Edge) const { return INVALID; } |
634 | 636 |
|
635 | 637 |
/// \brief Source node of the directed arc. |
636 | 638 |
Node source(Arc) const { return INVALID; } |
637 | 639 |
|
638 | 640 |
/// \brief Target node of the directed arc. |
639 | 641 |
Node target(Arc) const { return INVALID; } |
640 | 642 |
|
641 | 643 |
/// \brief Returns the id of the node. |
642 | 644 |
int id(Node) const { return -1; } |
643 | 645 |
|
644 | 646 |
/// \brief Returns the id of the edge. |
645 | 647 |
int id(Edge) const { return -1; } |
646 | 648 |
|
647 | 649 |
/// \brief Returns the id of the arc. |
648 | 650 |
int id(Arc) const { return -1; } |
649 | 651 |
|
650 | 652 |
/// \brief Returns the node with the given id. |
651 | 653 |
/// |
652 | 654 |
/// \pre The argument should be a valid node id in the graph. |
653 | 655 |
Node nodeFromId(int) const { return INVALID; } |
654 | 656 |
|
655 | 657 |
/// \brief Returns the edge with the given id. |
656 | 658 |
/// |
657 | 659 |
/// \pre The argument should be a valid edge id in the graph. |
658 | 660 |
Edge edgeFromId(int) const { return INVALID; } |
659 | 661 |
|
660 | 662 |
/// \brief Returns the arc with the given id. |
661 | 663 |
/// |
662 | 664 |
/// \pre The argument should be a valid arc id in the graph. |
663 | 665 |
Arc arcFromId(int) const { return INVALID; } |
664 | 666 |
|
665 | 667 |
/// \brief Returns an upper bound on the node IDs. |
666 | 668 |
int maxNodeId() const { return -1; } |
667 | 669 |
|
668 | 670 |
/// \brief Returns an upper bound on the edge IDs. |
669 | 671 |
int maxEdgeId() const { return -1; } |
670 | 672 |
|
671 | 673 |
/// \brief Returns an upper bound on the arc IDs. |
672 | 674 |
int maxArcId() const { return -1; } |
673 | 675 |
|
674 | 676 |
void first(Node&) const {} |
675 | 677 |
void next(Node&) const {} |
676 | 678 |
|
677 | 679 |
void first(Edge&) const {} |
678 | 680 |
void next(Edge&) const {} |
679 | 681 |
|
680 | 682 |
void first(Arc&) const {} |
681 | 683 |
void next(Arc&) const {} |
682 | 684 |
|
683 | 685 |
void firstOut(Arc&, Node) const {} |
684 | 686 |
void nextOut(Arc&) const {} |
685 | 687 |
|
686 | 688 |
void firstIn(Arc&, Node) const {} |
687 | 689 |
void nextIn(Arc&) const {} |
688 | 690 |
|
689 | 691 |
void firstInc(Edge &, bool &, const Node &) const {} |
690 | 692 |
void nextInc(Edge &, bool &) const {} |
691 | 693 |
|
692 | 694 |
// The second parameter is dummy. |
693 | 695 |
Node fromId(int, Node) const { return INVALID; } |
694 | 696 |
// The second parameter is dummy. |
695 | 697 |
Edge fromId(int, Edge) const { return INVALID; } |
696 | 698 |
// The second parameter is dummy. |
697 | 699 |
Arc fromId(int, Arc) const { return INVALID; } |
698 | 700 |
|
699 | 701 |
// Dummy parameter. |
700 | 702 |
int maxId(Node) const { return -1; } |
701 | 703 |
// Dummy parameter. |
702 | 704 |
int maxId(Edge) const { return -1; } |
703 | 705 |
// Dummy parameter. |
704 | 706 |
int maxId(Arc) const { return -1; } |
705 | 707 |
|
706 | 708 |
/// \brief Base node of the iterator |
707 | 709 |
/// |
708 | 710 |
/// Returns the base node (the source in this case) of the iterator |
709 | 711 |
Node baseNode(OutArcIt e) const { |
710 | 712 |
return source(e); |
711 | 713 |
} |
712 | 714 |
/// \brief Running node of the iterator |
713 | 715 |
/// |
714 | 716 |
/// Returns the running node (the target in this case) of the |
715 | 717 |
/// iterator |
716 | 718 |
Node runningNode(OutArcIt e) const { |
717 | 719 |
return target(e); |
718 | 720 |
} |
719 | 721 |
|
720 | 722 |
/// \brief Base node of the iterator |
721 | 723 |
/// |
722 | 724 |
/// Returns the base node (the target in this case) of the iterator |
723 | 725 |
Node baseNode(InArcIt e) const { |
724 | 726 |
return target(e); |
725 | 727 |
} |
726 | 728 |
/// \brief Running node of the iterator |
727 | 729 |
/// |
728 | 730 |
/// Returns the running node (the source in this case) of the |
729 | 731 |
/// iterator |
730 | 732 |
Node runningNode(InArcIt e) const { |
731 | 733 |
return source(e); |
732 | 734 |
} |
733 | 735 |
|
734 | 736 |
/// \brief Base node of the iterator |
735 | 737 |
/// |
736 | 738 |
/// Returns the base node of the iterator |
737 | 739 |
Node baseNode(IncEdgeIt) const { |
738 | 740 |
return INVALID; |
739 | 741 |
} |
740 | 742 |
|
741 | 743 |
/// \brief Running node of the iterator |
742 | 744 |
/// |
743 | 745 |
/// Returns the running node of the iterator |
744 | 746 |
Node runningNode(IncEdgeIt) const { |
745 | 747 |
return INVALID; |
746 | 748 |
} |
747 | 749 |
|
748 | 750 |
template <typename _Graph> |
749 | 751 |
struct Constraints { |
750 | 752 |
void constraints() { |
751 | 753 |
checkConcept<BaseGraphComponent, _Graph>(); |
752 | 754 |
checkConcept<IterableGraphComponent<>, _Graph>(); |
753 | 755 |
checkConcept<IDableGraphComponent<>, _Graph>(); |
754 | 756 |
checkConcept<MappableGraphComponent<>, _Graph>(); |
755 | 757 |
} |
756 | 758 |
}; |
757 | 759 |
|
758 | 760 |
}; |
759 | 761 |
|
760 | 762 |
} |
761 | 763 |
|
762 | 764 |
} |
763 | 765 |
|
764 | 766 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CONNECTIVITY_H |
20 | 20 |
#define LEMON_CONNECTIVITY_H |
21 | 21 |
|
22 | 22 |
#include <lemon/dfs.h> |
23 | 23 |
#include <lemon/bfs.h> |
24 | 24 |
#include <lemon/core.h> |
25 | 25 |
#include <lemon/maps.h> |
26 | 26 |
#include <lemon/adaptors.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concepts/digraph.h> |
29 | 29 |
#include <lemon/concepts/graph.h> |
30 | 30 |
#include <lemon/concept_check.h> |
31 | 31 |
|
32 | 32 |
#include <stack> |
33 | 33 |
#include <functional> |
34 | 34 |
|
35 | 35 |
/// \ingroup graph_properties |
36 | 36 |
/// \file |
37 | 37 |
/// \brief Connectivity algorithms |
38 | 38 |
/// |
39 | 39 |
/// Connectivity algorithms |
40 | 40 |
|
41 | 41 |
namespace lemon { |
42 | 42 |
|
43 | 43 |
/// \ingroup graph_properties |
44 | 44 |
/// |
45 |
/// \brief Check whether |
|
45 |
/// \brief Check whether an undirected graph is connected. |
|
46 | 46 |
/// |
47 |
/// Check whether the given undirected graph is connected. |
|
48 |
/// \param graph The undirected graph. |
|
49 |
/// |
|
47 |
/// This function checks whether the given undirected graph is connected, |
|
48 |
/// i.e. there is a path between any two nodes in the graph. |
|
49 |
/// |
|
50 |
/// \return \c true if the graph is connected. |
|
50 | 51 |
/// \note By definition, the empty graph is connected. |
52 |
/// |
|
53 |
/// \see countConnectedComponents(), connectedComponents() |
|
54 |
/// \see stronglyConnected() |
|
51 | 55 |
template <typename Graph> |
52 | 56 |
bool connected(const Graph& graph) { |
53 | 57 |
checkConcept<concepts::Graph, Graph>(); |
54 | 58 |
typedef typename Graph::NodeIt NodeIt; |
55 | 59 |
if (NodeIt(graph) == INVALID) return true; |
56 | 60 |
Dfs<Graph> dfs(graph); |
57 | 61 |
dfs.run(NodeIt(graph)); |
58 | 62 |
for (NodeIt it(graph); it != INVALID; ++it) { |
59 | 63 |
if (!dfs.reached(it)) { |
60 | 64 |
return false; |
61 | 65 |
} |
62 | 66 |
} |
63 | 67 |
return true; |
64 | 68 |
} |
65 | 69 |
|
66 | 70 |
/// \ingroup graph_properties |
67 | 71 |
/// |
68 | 72 |
/// \brief Count the number of connected components of an undirected graph |
69 | 73 |
/// |
70 |
/// |
|
74 |
/// This function counts the number of connected components of the given |
|
75 |
/// undirected graph. |
|
71 | 76 |
/// |
72 |
/// \param graph The graph. It must be undirected. |
|
73 |
/// \return The number of components |
|
77 |
/// The connected components are the classes of an equivalence relation |
|
78 |
/// on the nodes of an undirected graph. Two nodes are in the same class |
|
79 |
/// if they are connected with a path. |
|
80 |
/// |
|
81 |
/// \return The number of connected components. |
|
74 | 82 |
/// \note By definition, the empty graph consists |
75 | 83 |
/// of zero connected components. |
84 |
/// |
|
85 |
/// \see connected(), connectedComponents() |
|
76 | 86 |
template <typename Graph> |
77 | 87 |
int countConnectedComponents(const Graph &graph) { |
78 | 88 |
checkConcept<concepts::Graph, Graph>(); |
79 | 89 |
typedef typename Graph::Node Node; |
80 | 90 |
typedef typename Graph::Arc Arc; |
81 | 91 |
|
82 | 92 |
typedef NullMap<Node, Arc> PredMap; |
83 | 93 |
typedef NullMap<Node, int> DistMap; |
84 | 94 |
|
85 | 95 |
int compNum = 0; |
86 | 96 |
typename Bfs<Graph>:: |
87 | 97 |
template SetPredMap<PredMap>:: |
88 | 98 |
template SetDistMap<DistMap>:: |
89 | 99 |
Create bfs(graph); |
90 | 100 |
|
91 | 101 |
PredMap predMap; |
92 | 102 |
bfs.predMap(predMap); |
93 | 103 |
|
94 | 104 |
DistMap distMap; |
95 | 105 |
bfs.distMap(distMap); |
96 | 106 |
|
97 | 107 |
bfs.init(); |
98 | 108 |
for(typename Graph::NodeIt n(graph); n != INVALID; ++n) { |
99 | 109 |
if (!bfs.reached(n)) { |
100 | 110 |
bfs.addSource(n); |
101 | 111 |
bfs.start(); |
102 | 112 |
++compNum; |
103 | 113 |
} |
104 | 114 |
} |
105 | 115 |
return compNum; |
106 | 116 |
} |
107 | 117 |
|
108 | 118 |
/// \ingroup graph_properties |
109 | 119 |
/// |
110 | 120 |
/// \brief Find the connected components of an undirected graph |
111 | 121 |
/// |
112 |
/// |
|
122 |
/// This function finds the connected components of the given undirected |
|
123 |
/// graph. |
|
124 |
/// |
|
125 |
/// The connected components are the classes of an equivalence relation |
|
126 |
/// on the nodes of an undirected graph. Two nodes are in the same class |
|
127 |
/// if they are connected with a path. |
|
113 | 128 |
/// |
114 | 129 |
/// \image html connected_components.png |
115 | 130 |
/// \image latex connected_components.eps "Connected components" width=\textwidth |
116 | 131 |
/// |
117 |
/// \param graph The |
|
132 |
/// \param graph The undirected graph. |
|
118 | 133 |
/// \retval compMap A writable node map. The values will be set from 0 to |
119 |
/// the number of the connected components minus one. Each values of the map |
|
120 |
/// will be set exactly once, the values of a certain component will be |
|
134 |
/// the number of the connected components minus one. Each value of the map |
|
135 |
/// will be set exactly once, and the values of a certain component will be |
|
121 | 136 |
/// set continuously. |
122 |
/// \return The number of components |
|
137 |
/// \return The number of connected components. |
|
138 |
/// \note By definition, the empty graph consists |
|
139 |
/// of zero connected components. |
|
140 |
/// |
|
141 |
/// \see connected(), countConnectedComponents() |
|
123 | 142 |
template <class Graph, class NodeMap> |
124 | 143 |
int connectedComponents(const Graph &graph, NodeMap &compMap) { |
125 | 144 |
checkConcept<concepts::Graph, Graph>(); |
126 | 145 |
typedef typename Graph::Node Node; |
127 | 146 |
typedef typename Graph::Arc Arc; |
128 | 147 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>(); |
129 | 148 |
|
130 | 149 |
typedef NullMap<Node, Arc> PredMap; |
131 | 150 |
typedef NullMap<Node, int> DistMap; |
132 | 151 |
|
133 | 152 |
int compNum = 0; |
134 | 153 |
typename Bfs<Graph>:: |
135 | 154 |
template SetPredMap<PredMap>:: |
136 | 155 |
template SetDistMap<DistMap>:: |
137 | 156 |
Create bfs(graph); |
138 | 157 |
|
139 | 158 |
PredMap predMap; |
140 | 159 |
bfs.predMap(predMap); |
141 | 160 |
|
142 | 161 |
DistMap distMap; |
143 | 162 |
bfs.distMap(distMap); |
144 | 163 |
|
145 | 164 |
bfs.init(); |
146 | 165 |
for(typename Graph::NodeIt n(graph); n != INVALID; ++n) { |
147 | 166 |
if(!bfs.reached(n)) { |
148 | 167 |
bfs.addSource(n); |
149 | 168 |
while (!bfs.emptyQueue()) { |
150 | 169 |
compMap.set(bfs.nextNode(), compNum); |
151 | 170 |
bfs.processNextNode(); |
152 | 171 |
} |
153 | 172 |
++compNum; |
154 | 173 |
} |
155 | 174 |
} |
156 | 175 |
return compNum; |
157 | 176 |
} |
158 | 177 |
|
159 | 178 |
namespace _connectivity_bits { |
160 | 179 |
|
161 | 180 |
template <typename Digraph, typename Iterator > |
162 | 181 |
struct LeaveOrderVisitor : public DfsVisitor<Digraph> { |
163 | 182 |
public: |
164 | 183 |
typedef typename Digraph::Node Node; |
165 | 184 |
LeaveOrderVisitor(Iterator it) : _it(it) {} |
166 | 185 |
|
167 | 186 |
void leave(const Node& node) { |
168 | 187 |
*(_it++) = node; |
169 | 188 |
} |
170 | 189 |
|
171 | 190 |
private: |
172 | 191 |
Iterator _it; |
173 | 192 |
}; |
174 | 193 |
|
175 | 194 |
template <typename Digraph, typename Map> |
176 | 195 |
struct FillMapVisitor : public DfsVisitor<Digraph> { |
177 | 196 |
public: |
178 | 197 |
typedef typename Digraph::Node Node; |
179 | 198 |
typedef typename Map::Value Value; |
180 | 199 |
|
181 | 200 |
FillMapVisitor(Map& map, Value& value) |
182 | 201 |
: _map(map), _value(value) {} |
183 | 202 |
|
184 | 203 |
void reach(const Node& node) { |
185 | 204 |
_map.set(node, _value); |
186 | 205 |
} |
187 | 206 |
private: |
188 | 207 |
Map& _map; |
189 | 208 |
Value& _value; |
190 | 209 |
}; |
191 | 210 |
|
192 | 211 |
template <typename Digraph, typename ArcMap> |
193 | 212 |
struct StronglyConnectedCutArcsVisitor : public DfsVisitor<Digraph> { |
194 | 213 |
public: |
195 | 214 |
typedef typename Digraph::Node Node; |
196 | 215 |
typedef typename Digraph::Arc Arc; |
197 | 216 |
|
198 | 217 |
StronglyConnectedCutArcsVisitor(const Digraph& digraph, |
199 | 218 |
ArcMap& cutMap, |
200 | 219 |
int& cutNum) |
201 | 220 |
: _digraph(digraph), _cutMap(cutMap), _cutNum(cutNum), |
202 | 221 |
_compMap(digraph, -1), _num(-1) { |
203 | 222 |
} |
204 | 223 |
|
205 | 224 |
void start(const Node&) { |
206 | 225 |
++_num; |
207 | 226 |
} |
208 | 227 |
|
209 | 228 |
void reach(const Node& node) { |
210 | 229 |
_compMap.set(node, _num); |
211 | 230 |
} |
212 | 231 |
|
213 | 232 |
void examine(const Arc& arc) { |
214 | 233 |
if (_compMap[_digraph.source(arc)] != |
215 | 234 |
_compMap[_digraph.target(arc)]) { |
216 | 235 |
_cutMap.set(arc, true); |
217 | 236 |
++_cutNum; |
218 | 237 |
} |
219 | 238 |
} |
220 | 239 |
private: |
221 | 240 |
const Digraph& _digraph; |
222 | 241 |
ArcMap& _cutMap; |
223 | 242 |
int& _cutNum; |
224 | 243 |
|
225 | 244 |
typename Digraph::template NodeMap<int> _compMap; |
226 | 245 |
int _num; |
227 | 246 |
}; |
228 | 247 |
|
229 | 248 |
} |
230 | 249 |
|
231 | 250 |
|
232 | 251 |
/// \ingroup graph_properties |
233 | 252 |
/// |
234 |
/// \brief Check whether |
|
253 |
/// \brief Check whether a directed graph is strongly connected. |
|
235 | 254 |
/// |
236 |
/// Check whether the given directed graph is strongly connected. The |
|
237 |
/// graph is strongly connected when any two nodes of the graph are |
|
255 |
/// This function checks whether the given directed graph is strongly |
|
256 |
/// connected, i.e. any two nodes of the digraph are |
|
238 | 257 |
/// connected with directed paths in both direction. |
239 |
/// \return \c false when the graph is not strongly connected. |
|
240 |
/// \see connected |
|
241 | 258 |
/// |
242 |
/// \ |
|
259 |
/// \return \c true if the digraph is strongly connected. |
|
260 |
/// \note By definition, the empty digraph is strongly connected. |
|
261 |
/// |
|
262 |
/// \see countStronglyConnectedComponents(), stronglyConnectedComponents() |
|
263 |
/// \see connected() |
|
243 | 264 |
template <typename Digraph> |
244 | 265 |
bool stronglyConnected(const Digraph& digraph) { |
245 | 266 |
checkConcept<concepts::Digraph, Digraph>(); |
246 | 267 |
|
247 | 268 |
typedef typename Digraph::Node Node; |
248 | 269 |
typedef typename Digraph::NodeIt NodeIt; |
249 | 270 |
|
250 | 271 |
typename Digraph::Node source = NodeIt(digraph); |
251 | 272 |
if (source == INVALID) return true; |
252 | 273 |
|
253 | 274 |
using namespace _connectivity_bits; |
254 | 275 |
|
255 | 276 |
typedef DfsVisitor<Digraph> Visitor; |
256 | 277 |
Visitor visitor; |
257 | 278 |
|
258 | 279 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
259 | 280 |
dfs.init(); |
260 | 281 |
dfs.addSource(source); |
261 | 282 |
dfs.start(); |
262 | 283 |
|
263 | 284 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
264 | 285 |
if (!dfs.reached(it)) { |
265 | 286 |
return false; |
266 | 287 |
} |
267 | 288 |
} |
268 | 289 |
|
269 | 290 |
typedef ReverseDigraph<const Digraph> RDigraph; |
270 | 291 |
typedef typename RDigraph::NodeIt RNodeIt; |
271 | 292 |
RDigraph rdigraph(digraph); |
272 | 293 |
|
273 |
typedef DfsVisitor< |
|
294 |
typedef DfsVisitor<RDigraph> RVisitor; |
|
274 | 295 |
RVisitor rvisitor; |
275 | 296 |
|
276 | 297 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
277 | 298 |
rdfs.init(); |
278 | 299 |
rdfs.addSource(source); |
279 | 300 |
rdfs.start(); |
280 | 301 |
|
281 | 302 |
for (RNodeIt it(rdigraph); it != INVALID; ++it) { |
282 | 303 |
if (!rdfs.reached(it)) { |
283 | 304 |
return false; |
284 | 305 |
} |
285 | 306 |
} |
286 | 307 |
|
287 | 308 |
return true; |
288 | 309 |
} |
289 | 310 |
|
290 | 311 |
/// \ingroup graph_properties |
291 | 312 |
/// |
292 |
/// \brief Count the strongly connected components of a |
|
313 |
/// \brief Count the number of strongly connected components of a |
|
314 |
/// directed graph |
|
293 | 315 |
/// |
294 |
/// |
|
316 |
/// This function counts the number of strongly connected components of |
|
317 |
/// the given directed graph. |
|
318 |
/// |
|
295 | 319 |
/// The strongly connected components are the classes of an |
296 |
/// equivalence relation on the nodes of |
|
320 |
/// equivalence relation on the nodes of a digraph. Two nodes are in |
|
297 | 321 |
/// the same class if they are connected with directed paths in both |
298 | 322 |
/// direction. |
299 | 323 |
/// |
300 |
/// \param digraph The graph. |
|
301 |
/// \return The number of components |
|
302 |
/// \ |
|
324 |
/// \return The number of strongly connected components. |
|
325 |
/// \note By definition, the empty digraph has zero |
|
303 | 326 |
/// strongly connected components. |
327 |
/// |
|
328 |
/// \see stronglyConnected(), stronglyConnectedComponents() |
|
304 | 329 |
template <typename Digraph> |
305 | 330 |
int countStronglyConnectedComponents(const Digraph& digraph) { |
306 | 331 |
checkConcept<concepts::Digraph, Digraph>(); |
307 | 332 |
|
308 | 333 |
using namespace _connectivity_bits; |
309 | 334 |
|
310 | 335 |
typedef typename Digraph::Node Node; |
311 | 336 |
typedef typename Digraph::Arc Arc; |
312 | 337 |
typedef typename Digraph::NodeIt NodeIt; |
313 | 338 |
typedef typename Digraph::ArcIt ArcIt; |
314 | 339 |
|
315 | 340 |
typedef std::vector<Node> Container; |
316 | 341 |
typedef typename Container::iterator Iterator; |
317 | 342 |
|
318 | 343 |
Container nodes(countNodes(digraph)); |
319 | 344 |
typedef LeaveOrderVisitor<Digraph, Iterator> Visitor; |
320 | 345 |
Visitor visitor(nodes.begin()); |
321 | 346 |
|
322 | 347 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
323 | 348 |
dfs.init(); |
324 | 349 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
325 | 350 |
if (!dfs.reached(it)) { |
326 | 351 |
dfs.addSource(it); |
327 | 352 |
dfs.start(); |
328 | 353 |
} |
329 | 354 |
} |
330 | 355 |
|
331 | 356 |
typedef typename Container::reverse_iterator RIterator; |
332 | 357 |
typedef ReverseDigraph<const Digraph> RDigraph; |
333 | 358 |
|
334 | 359 |
RDigraph rdigraph(digraph); |
335 | 360 |
|
336 | 361 |
typedef DfsVisitor<Digraph> RVisitor; |
337 | 362 |
RVisitor rvisitor; |
338 | 363 |
|
339 | 364 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
340 | 365 |
|
341 | 366 |
int compNum = 0; |
342 | 367 |
|
343 | 368 |
rdfs.init(); |
344 | 369 |
for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) { |
345 | 370 |
if (!rdfs.reached(*it)) { |
346 | 371 |
rdfs.addSource(*it); |
347 | 372 |
rdfs.start(); |
348 | 373 |
++compNum; |
349 | 374 |
} |
350 | 375 |
} |
351 | 376 |
return compNum; |
352 | 377 |
} |
353 | 378 |
|
354 | 379 |
/// \ingroup graph_properties |
355 | 380 |
/// |
356 | 381 |
/// \brief Find the strongly connected components of a directed graph |
357 | 382 |
/// |
358 |
/// Find the strongly connected components of a directed graph. The |
|
359 |
/// strongly connected components are the classes of an equivalence |
|
360 |
/// relation on the nodes of the graph. Two nodes are in |
|
361 |
/// relationship when there are directed paths between them in both |
|
362 |
/// direction. In addition, the numbering of components will satisfy |
|
363 |
/// that there is no arc going from a higher numbered component to |
|
364 |
/// |
|
383 |
/// This function finds the strongly connected components of the given |
|
384 |
/// directed graph. In addition, the numbering of the components will |
|
385 |
/// satisfy that there is no arc going from a higher numbered component |
|
386 |
/// to a lower one (i.e. it provides a topological order of the components). |
|
387 |
/// |
|
388 |
/// The strongly connected components are the classes of an |
|
389 |
/// equivalence relation on the nodes of a digraph. Two nodes are in |
|
390 |
/// the same class if they are connected with directed paths in both |
|
391 |
/// direction. |
|
365 | 392 |
/// |
366 | 393 |
/// \image html strongly_connected_components.png |
367 | 394 |
/// \image latex strongly_connected_components.eps "Strongly connected components" width=\textwidth |
368 | 395 |
/// |
369 | 396 |
/// \param digraph The digraph. |
370 | 397 |
/// \retval compMap A writable node map. The values will be set from 0 to |
371 | 398 |
/// the number of the strongly connected components minus one. Each value |
372 |
/// of the map will be set exactly once, the values of a certain component |
|
373 |
/// will be set continuously. |
|
374 |
/// |
|
399 |
/// of the map will be set exactly once, and the values of a certain |
|
400 |
/// component will be set continuously. |
|
401 |
/// \return The number of strongly connected components. |
|
402 |
/// \note By definition, the empty digraph has zero |
|
403 |
/// strongly connected components. |
|
404 |
/// |
|
405 |
/// \see stronglyConnected(), countStronglyConnectedComponents() |
|
375 | 406 |
template <typename Digraph, typename NodeMap> |
376 | 407 |
int stronglyConnectedComponents(const Digraph& digraph, NodeMap& compMap) { |
377 | 408 |
checkConcept<concepts::Digraph, Digraph>(); |
378 | 409 |
typedef typename Digraph::Node Node; |
379 | 410 |
typedef typename Digraph::NodeIt NodeIt; |
380 | 411 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>(); |
381 | 412 |
|
382 | 413 |
using namespace _connectivity_bits; |
383 | 414 |
|
384 | 415 |
typedef std::vector<Node> Container; |
385 | 416 |
typedef typename Container::iterator Iterator; |
386 | 417 |
|
387 | 418 |
Container nodes(countNodes(digraph)); |
388 | 419 |
typedef LeaveOrderVisitor<Digraph, Iterator> Visitor; |
389 | 420 |
Visitor visitor(nodes.begin()); |
390 | 421 |
|
391 | 422 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
392 | 423 |
dfs.init(); |
393 | 424 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
394 | 425 |
if (!dfs.reached(it)) { |
395 | 426 |
dfs.addSource(it); |
396 | 427 |
dfs.start(); |
397 | 428 |
} |
398 | 429 |
} |
399 | 430 |
|
400 | 431 |
typedef typename Container::reverse_iterator RIterator; |
401 | 432 |
typedef ReverseDigraph<const Digraph> RDigraph; |
402 | 433 |
|
403 | 434 |
RDigraph rdigraph(digraph); |
404 | 435 |
|
405 | 436 |
int compNum = 0; |
406 | 437 |
|
407 | 438 |
typedef FillMapVisitor<RDigraph, NodeMap> RVisitor; |
408 | 439 |
RVisitor rvisitor(compMap, compNum); |
409 | 440 |
|
410 | 441 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
411 | 442 |
|
412 | 443 |
rdfs.init(); |
413 | 444 |
for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) { |
414 | 445 |
if (!rdfs.reached(*it)) { |
415 | 446 |
rdfs.addSource(*it); |
416 | 447 |
rdfs.start(); |
417 | 448 |
++compNum; |
418 | 449 |
} |
419 | 450 |
} |
420 | 451 |
return compNum; |
421 | 452 |
} |
422 | 453 |
|
423 | 454 |
/// \ingroup graph_properties |
424 | 455 |
/// |
425 | 456 |
/// \brief Find the cut arcs of the strongly connected components. |
426 | 457 |
/// |
427 |
/// Find the cut arcs of the strongly connected components. |
|
428 |
/// The strongly connected components are the classes of an equivalence |
|
429 |
/// relation on the nodes of the graph. Two nodes are in relationship |
|
430 |
/// when there are directed paths between them in both direction. |
|
458 |
/// This function finds the cut arcs of the strongly connected components |
|
459 |
/// of the given digraph. |
|
460 |
/// |
|
461 |
/// The strongly connected components are the classes of an |
|
462 |
/// equivalence relation on the nodes of a digraph. Two nodes are in |
|
463 |
/// the same class if they are connected with directed paths in both |
|
464 |
/// direction. |
|
431 | 465 |
/// The strongly connected components are separated by the cut arcs. |
432 | 466 |
/// |
433 |
/// \param graph The graph. |
|
434 |
/// \retval cutMap A writable node map. The values will be set true when the |
|
435 |
/// |
|
467 |
/// \param digraph The digraph. |
|
468 |
/// \retval cutMap A writable arc map. The values will be set to \c true |
|
469 |
/// for the cut arcs (exactly once for each cut arc), and will not be |
|
470 |
/// changed for other arcs. |
|
471 |
/// \return The number of cut arcs. |
|
436 | 472 |
/// |
437 |
/// \ |
|
473 |
/// \see stronglyConnected(), stronglyConnectedComponents() |
|
438 | 474 |
template <typename Digraph, typename ArcMap> |
439 |
int stronglyConnectedCutArcs(const Digraph& |
|
475 |
int stronglyConnectedCutArcs(const Digraph& digraph, ArcMap& cutMap) { |
|
440 | 476 |
checkConcept<concepts::Digraph, Digraph>(); |
441 | 477 |
typedef typename Digraph::Node Node; |
442 | 478 |
typedef typename Digraph::Arc Arc; |
443 | 479 |
typedef typename Digraph::NodeIt NodeIt; |
444 | 480 |
checkConcept<concepts::WriteMap<Arc, bool>, ArcMap>(); |
445 | 481 |
|
446 | 482 |
using namespace _connectivity_bits; |
447 | 483 |
|
448 | 484 |
typedef std::vector<Node> Container; |
449 | 485 |
typedef typename Container::iterator Iterator; |
450 | 486 |
|
451 |
Container nodes(countNodes( |
|
487 |
Container nodes(countNodes(digraph)); |
|
452 | 488 |
typedef LeaveOrderVisitor<Digraph, Iterator> Visitor; |
453 | 489 |
Visitor visitor(nodes.begin()); |
454 | 490 |
|
455 |
DfsVisit<Digraph, Visitor> dfs( |
|
491 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
|
456 | 492 |
dfs.init(); |
457 |
for (NodeIt it( |
|
493 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
|
458 | 494 |
if (!dfs.reached(it)) { |
459 | 495 |
dfs.addSource(it); |
460 | 496 |
dfs.start(); |
461 | 497 |
} |
462 | 498 |
} |
463 | 499 |
|
464 | 500 |
typedef typename Container::reverse_iterator RIterator; |
465 | 501 |
typedef ReverseDigraph<const Digraph> RDigraph; |
466 | 502 |
|
467 |
RDigraph |
|
503 |
RDigraph rdigraph(digraph); |
|
468 | 504 |
|
469 | 505 |
int cutNum = 0; |
470 | 506 |
|
471 | 507 |
typedef StronglyConnectedCutArcsVisitor<RDigraph, ArcMap> RVisitor; |
472 |
RVisitor rvisitor( |
|
508 |
RVisitor rvisitor(rdigraph, cutMap, cutNum); |
|
473 | 509 |
|
474 |
DfsVisit<RDigraph, RVisitor> rdfs( |
|
510 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
|
475 | 511 |
|
476 | 512 |
rdfs.init(); |
477 | 513 |
for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) { |
478 | 514 |
if (!rdfs.reached(*it)) { |
479 | 515 |
rdfs.addSource(*it); |
480 | 516 |
rdfs.start(); |
481 | 517 |
} |
482 | 518 |
} |
483 | 519 |
return cutNum; |
484 | 520 |
} |
485 | 521 |
|
486 | 522 |
namespace _connectivity_bits { |
487 | 523 |
|
488 | 524 |
template <typename Digraph> |
489 | 525 |
class CountBiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> { |
490 | 526 |
public: |
491 | 527 |
typedef typename Digraph::Node Node; |
492 | 528 |
typedef typename Digraph::Arc Arc; |
493 | 529 |
typedef typename Digraph::Edge Edge; |
494 | 530 |
|
495 | 531 |
CountBiNodeConnectedComponentsVisitor(const Digraph& graph, int &compNum) |
496 | 532 |
: _graph(graph), _compNum(compNum), |
497 | 533 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
498 | 534 |
|
499 | 535 |
void start(const Node& node) { |
500 | 536 |
_predMap.set(node, INVALID); |
501 | 537 |
} |
502 | 538 |
|
503 | 539 |
void reach(const Node& node) { |
504 | 540 |
_numMap.set(node, _num); |
505 | 541 |
_retMap.set(node, _num); |
506 | 542 |
++_num; |
507 | 543 |
} |
508 | 544 |
|
509 | 545 |
void discover(const Arc& edge) { |
510 | 546 |
_predMap.set(_graph.target(edge), _graph.source(edge)); |
511 | 547 |
} |
512 | 548 |
|
513 | 549 |
void examine(const Arc& edge) { |
514 | 550 |
if (_graph.source(edge) == _graph.target(edge) && |
515 | 551 |
_graph.direction(edge)) { |
516 | 552 |
++_compNum; |
517 | 553 |
return; |
518 | 554 |
} |
519 | 555 |
if (_predMap[_graph.source(edge)] == _graph.target(edge)) { |
520 | 556 |
return; |
521 | 557 |
} |
522 | 558 |
if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) { |
523 | 559 |
_retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]); |
524 | 560 |
} |
525 | 561 |
} |
526 | 562 |
|
527 | 563 |
void backtrack(const Arc& edge) { |
528 | 564 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
529 | 565 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
530 | 566 |
} |
531 | 567 |
if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) { |
532 | 568 |
++_compNum; |
533 | 569 |
} |
534 | 570 |
} |
535 | 571 |
|
536 | 572 |
private: |
537 | 573 |
const Digraph& _graph; |
538 | 574 |
int& _compNum; |
539 | 575 |
|
540 | 576 |
typename Digraph::template NodeMap<int> _numMap; |
541 | 577 |
typename Digraph::template NodeMap<int> _retMap; |
542 | 578 |
typename Digraph::template NodeMap<Node> _predMap; |
543 | 579 |
int _num; |
544 | 580 |
}; |
545 | 581 |
|
546 | 582 |
template <typename Digraph, typename ArcMap> |
547 | 583 |
class BiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> { |
548 | 584 |
public: |
549 | 585 |
typedef typename Digraph::Node Node; |
550 | 586 |
typedef typename Digraph::Arc Arc; |
551 | 587 |
typedef typename Digraph::Edge Edge; |
552 | 588 |
|
553 | 589 |
BiNodeConnectedComponentsVisitor(const Digraph& graph, |
554 | 590 |
ArcMap& compMap, int &compNum) |
555 | 591 |
: _graph(graph), _compMap(compMap), _compNum(compNum), |
556 | 592 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
557 | 593 |
|
558 | 594 |
void start(const Node& node) { |
559 | 595 |
_predMap.set(node, INVALID); |
560 | 596 |
} |
561 | 597 |
|
562 | 598 |
void reach(const Node& node) { |
563 | 599 |
_numMap.set(node, _num); |
564 | 600 |
_retMap.set(node, _num); |
565 | 601 |
++_num; |
566 | 602 |
} |
567 | 603 |
|
568 | 604 |
void discover(const Arc& edge) { |
569 | 605 |
Node target = _graph.target(edge); |
570 | 606 |
_predMap.set(target, edge); |
571 | 607 |
_edgeStack.push(edge); |
572 | 608 |
} |
573 | 609 |
|
574 | 610 |
void examine(const Arc& edge) { |
575 | 611 |
Node source = _graph.source(edge); |
576 | 612 |
Node target = _graph.target(edge); |
577 | 613 |
if (source == target && _graph.direction(edge)) { |
578 | 614 |
_compMap.set(edge, _compNum); |
579 | 615 |
++_compNum; |
580 | 616 |
return; |
581 | 617 |
} |
582 | 618 |
if (_numMap[target] < _numMap[source]) { |
583 | 619 |
if (_predMap[source] != _graph.oppositeArc(edge)) { |
584 | 620 |
_edgeStack.push(edge); |
585 | 621 |
} |
586 | 622 |
} |
587 | 623 |
if (_predMap[source] != INVALID && |
588 | 624 |
target == _graph.source(_predMap[source])) { |
589 | 625 |
return; |
590 | 626 |
} |
591 | 627 |
if (_retMap[source] > _numMap[target]) { |
592 | 628 |
_retMap.set(source, _numMap[target]); |
593 | 629 |
} |
594 | 630 |
} |
595 | 631 |
|
596 | 632 |
void backtrack(const Arc& edge) { |
597 | 633 |
Node source = _graph.source(edge); |
598 | 634 |
Node target = _graph.target(edge); |
599 | 635 |
if (_retMap[source] > _retMap[target]) { |
600 | 636 |
_retMap.set(source, _retMap[target]); |
601 | 637 |
} |
602 | 638 |
if (_numMap[source] <= _retMap[target]) { |
603 | 639 |
while (_edgeStack.top() != edge) { |
604 | 640 |
_compMap.set(_edgeStack.top(), _compNum); |
605 | 641 |
_edgeStack.pop(); |
606 | 642 |
} |
607 | 643 |
_compMap.set(edge, _compNum); |
608 | 644 |
_edgeStack.pop(); |
609 | 645 |
++_compNum; |
610 | 646 |
} |
611 | 647 |
} |
612 | 648 |
|
613 | 649 |
private: |
614 | 650 |
const Digraph& _graph; |
615 | 651 |
ArcMap& _compMap; |
616 | 652 |
int& _compNum; |
617 | 653 |
|
618 | 654 |
typename Digraph::template NodeMap<int> _numMap; |
619 | 655 |
typename Digraph::template NodeMap<int> _retMap; |
620 | 656 |
typename Digraph::template NodeMap<Arc> _predMap; |
621 | 657 |
std::stack<Edge> _edgeStack; |
622 | 658 |
int _num; |
623 | 659 |
}; |
624 | 660 |
|
625 | 661 |
|
626 | 662 |
template <typename Digraph, typename NodeMap> |
627 | 663 |
class BiNodeConnectedCutNodesVisitor : public DfsVisitor<Digraph> { |
628 | 664 |
public: |
629 | 665 |
typedef typename Digraph::Node Node; |
630 | 666 |
typedef typename Digraph::Arc Arc; |
631 | 667 |
typedef typename Digraph::Edge Edge; |
632 | 668 |
|
633 | 669 |
BiNodeConnectedCutNodesVisitor(const Digraph& graph, NodeMap& cutMap, |
634 | 670 |
int& cutNum) |
635 | 671 |
: _graph(graph), _cutMap(cutMap), _cutNum(cutNum), |
636 | 672 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
637 | 673 |
|
638 | 674 |
void start(const Node& node) { |
639 | 675 |
_predMap.set(node, INVALID); |
640 | 676 |
rootCut = false; |
641 | 677 |
} |
642 | 678 |
|
643 | 679 |
void reach(const Node& node) { |
644 | 680 |
_numMap.set(node, _num); |
645 | 681 |
_retMap.set(node, _num); |
646 | 682 |
++_num; |
647 | 683 |
} |
648 | 684 |
|
649 | 685 |
void discover(const Arc& edge) { |
650 | 686 |
_predMap.set(_graph.target(edge), _graph.source(edge)); |
651 | 687 |
} |
652 | 688 |
|
653 | 689 |
void examine(const Arc& edge) { |
654 | 690 |
if (_graph.source(edge) == _graph.target(edge) && |
655 | 691 |
_graph.direction(edge)) { |
656 | 692 |
if (!_cutMap[_graph.source(edge)]) { |
657 | 693 |
_cutMap.set(_graph.source(edge), true); |
658 | 694 |
++_cutNum; |
659 | 695 |
} |
660 | 696 |
return; |
661 | 697 |
} |
662 | 698 |
if (_predMap[_graph.source(edge)] == _graph.target(edge)) return; |
663 | 699 |
if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) { |
664 | 700 |
_retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]); |
665 | 701 |
} |
666 | 702 |
} |
667 | 703 |
|
668 | 704 |
void backtrack(const Arc& edge) { |
669 | 705 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
670 | 706 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
671 | 707 |
} |
672 | 708 |
if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) { |
673 | 709 |
if (_predMap[_graph.source(edge)] != INVALID) { |
674 | 710 |
if (!_cutMap[_graph.source(edge)]) { |
675 | 711 |
_cutMap.set(_graph.source(edge), true); |
676 | 712 |
++_cutNum; |
677 | 713 |
} |
678 | 714 |
} else if (rootCut) { |
679 | 715 |
if (!_cutMap[_graph.source(edge)]) { |
680 | 716 |
_cutMap.set(_graph.source(edge), true); |
681 | 717 |
++_cutNum; |
682 | 718 |
} |
683 | 719 |
} else { |
684 | 720 |
rootCut = true; |
685 | 721 |
} |
686 | 722 |
} |
687 | 723 |
} |
688 | 724 |
|
689 | 725 |
private: |
690 | 726 |
const Digraph& _graph; |
691 | 727 |
NodeMap& _cutMap; |
692 | 728 |
int& _cutNum; |
693 | 729 |
|
694 | 730 |
typename Digraph::template NodeMap<int> _numMap; |
695 | 731 |
typename Digraph::template NodeMap<int> _retMap; |
696 | 732 |
typename Digraph::template NodeMap<Node> _predMap; |
697 | 733 |
std::stack<Edge> _edgeStack; |
698 | 734 |
int _num; |
699 | 735 |
bool rootCut; |
700 | 736 |
}; |
701 | 737 |
|
702 | 738 |
} |
703 | 739 |
|
704 | 740 |
template <typename Graph> |
705 | 741 |
int countBiNodeConnectedComponents(const Graph& graph); |
706 | 742 |
|
707 | 743 |
/// \ingroup graph_properties |
708 | 744 |
/// |
709 |
/// \brief |
|
745 |
/// \brief Check whether an undirected graph is bi-node-connected. |
|
710 | 746 |
/// |
711 |
/// This function checks that the undirected graph is bi-node-connected |
|
712 |
/// graph. The graph is bi-node-connected if any two undirected edge is |
|
713 |
/// |
|
747 |
/// This function checks whether the given undirected graph is |
|
748 |
/// bi-node-connected, i.e. any two edges are on same circle. |
|
714 | 749 |
/// |
715 |
/// \param graph The graph. |
|
716 |
/// \return \c true when the graph bi-node-connected. |
|
750 |
/// \return \c true if the graph bi-node-connected. |
|
751 |
/// \note By definition, the empty graph is bi-node-connected. |
|
752 |
/// |
|
753 |
/// \see countBiNodeConnectedComponents(), biNodeConnectedComponents() |
|
717 | 754 |
template <typename Graph> |
718 | 755 |
bool biNodeConnected(const Graph& graph) { |
719 | 756 |
return countBiNodeConnectedComponents(graph) <= 1; |
720 | 757 |
} |
721 | 758 |
|
722 | 759 |
/// \ingroup graph_properties |
723 | 760 |
/// |
724 |
/// \brief Count the |
|
761 |
/// \brief Count the number of bi-node-connected components of an |
|
762 |
/// undirected graph. |
|
725 | 763 |
/// |
726 |
/// This function finds the bi-node-connected components in an undirected |
|
727 |
/// graph. The biconnected components are the classes of an equivalence |
|
728 |
/// relation on the undirected edges. Two undirected edge is in relationship |
|
729 |
/// when they are on same circle. |
|
764 |
/// This function counts the number of bi-node-connected components of |
|
765 |
/// the given undirected graph. |
|
730 | 766 |
/// |
731 |
/// \param graph The graph. |
|
732 |
/// \return The number of components. |
|
767 |
/// The bi-node-connected components are the classes of an equivalence |
|
768 |
/// relation on the edges of a undirected graph. Two edges are in the |
|
769 |
/// same class if they are on same circle. |
|
770 |
/// |
|
771 |
/// \return The number of bi-node-connected components. |
|
772 |
/// |
|
773 |
/// \see biNodeConnected(), biNodeConnectedComponents() |
|
733 | 774 |
template <typename Graph> |
734 | 775 |
int countBiNodeConnectedComponents(const Graph& graph) { |
735 | 776 |
checkConcept<concepts::Graph, Graph>(); |
736 | 777 |
typedef typename Graph::NodeIt NodeIt; |
737 | 778 |
|
738 | 779 |
using namespace _connectivity_bits; |
739 | 780 |
|
740 | 781 |
typedef CountBiNodeConnectedComponentsVisitor<Graph> Visitor; |
741 | 782 |
|
742 | 783 |
int compNum = 0; |
743 | 784 |
Visitor visitor(graph, compNum); |
744 | 785 |
|
745 | 786 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
746 | 787 |
dfs.init(); |
747 | 788 |
|
748 | 789 |
for (NodeIt it(graph); it != INVALID; ++it) { |
749 | 790 |
if (!dfs.reached(it)) { |
750 | 791 |
dfs.addSource(it); |
751 | 792 |
dfs.start(); |
752 | 793 |
} |
753 | 794 |
} |
754 | 795 |
return compNum; |
755 | 796 |
} |
756 | 797 |
|
757 | 798 |
/// \ingroup graph_properties |
758 | 799 |
/// |
759 |
/// \brief Find the bi-node-connected components. |
|
800 |
/// \brief Find the bi-node-connected components of an undirected graph. |
|
760 | 801 |
/// |
761 |
/// This function finds the bi-node-connected components in an undirected |
|
762 |
/// graph. The bi-node-connected components are the classes of an equivalence |
|
763 |
/// relation on the undirected edges. Two undirected edge are in relationship |
|
764 |
/// when they are on same circle. |
|
802 |
/// This function finds the bi-node-connected components of the given |
|
803 |
/// undirected graph. |
|
804 |
/// |
|
805 |
/// The bi-node-connected components are the classes of an equivalence |
|
806 |
/// relation on the edges of a undirected graph. Two edges are in the |
|
807 |
/// same class if they are on same circle. |
|
765 | 808 |
/// |
766 | 809 |
/// \image html node_biconnected_components.png |
767 | 810 |
/// \image latex node_biconnected_components.eps "bi-node-connected components" width=\textwidth |
768 | 811 |
/// |
769 |
/// \param graph The graph. |
|
770 |
/// \retval compMap A writable uedge map. The values will be set from 0 |
|
771 |
/// to the number of the biconnected components minus one. Each values |
|
772 |
/// of the map will be set exactly once, the values of a certain component |
|
773 |
/// will be set continuously. |
|
774 |
/// \return The number of components. |
|
812 |
/// \param graph The undirected graph. |
|
813 |
/// \retval compMap A writable edge map. The values will be set from 0 |
|
814 |
/// to the number of the bi-node-connected components minus one. Each |
|
815 |
/// value of the map will be set exactly once, and the values of a |
|
816 |
/// certain component will be set continuously. |
|
817 |
/// \return The number of bi-node-connected components. |
|
818 |
/// |
|
819 |
/// \see biNodeConnected(), countBiNodeConnectedComponents() |
|
775 | 820 |
template <typename Graph, typename EdgeMap> |
776 | 821 |
int biNodeConnectedComponents(const Graph& graph, |
777 | 822 |
EdgeMap& compMap) { |
778 | 823 |
checkConcept<concepts::Graph, Graph>(); |
779 | 824 |
typedef typename Graph::NodeIt NodeIt; |
780 | 825 |
typedef typename Graph::Edge Edge; |
781 | 826 |
checkConcept<concepts::WriteMap<Edge, int>, EdgeMap>(); |
782 | 827 |
|
783 | 828 |
using namespace _connectivity_bits; |
784 | 829 |
|
785 | 830 |
typedef BiNodeConnectedComponentsVisitor<Graph, EdgeMap> Visitor; |
786 | 831 |
|
787 | 832 |
int compNum = 0; |
788 | 833 |
Visitor visitor(graph, compMap, compNum); |
789 | 834 |
|
790 | 835 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
791 | 836 |
dfs.init(); |
792 | 837 |
|
793 | 838 |
for (NodeIt it(graph); it != INVALID; ++it) { |
794 | 839 |
if (!dfs.reached(it)) { |
795 | 840 |
dfs.addSource(it); |
796 | 841 |
dfs.start(); |
797 | 842 |
} |
798 | 843 |
} |
799 | 844 |
return compNum; |
800 | 845 |
} |
801 | 846 |
|
802 | 847 |
/// \ingroup graph_properties |
803 | 848 |
/// |
804 |
/// \brief Find the bi-node-connected cut nodes. |
|
849 |
/// \brief Find the bi-node-connected cut nodes in an undirected graph. |
|
805 | 850 |
/// |
806 |
/// This function finds the bi-node-connected cut nodes in an undirected |
|
807 |
/// graph. The bi-node-connected components are the classes of an equivalence |
|
808 |
/// relation on the undirected edges. Two undirected edges are in |
|
809 |
/// relationship when they are on same circle. The biconnected components |
|
810 |
/// |
|
851 |
/// This function finds the bi-node-connected cut nodes in the given |
|
852 |
/// undirected graph. |
|
811 | 853 |
/// |
812 |
/// \param graph The graph. |
|
813 |
/// \retval cutMap A writable edge map. The values will be set true when |
|
814 |
/// |
|
854 |
/// The bi-node-connected components are the classes of an equivalence |
|
855 |
/// relation on the edges of a undirected graph. Two edges are in the |
|
856 |
/// same class if they are on same circle. |
|
857 |
/// The bi-node-connected components are separted by the cut nodes of |
|
858 |
/// the components. |
|
859 |
/// |
|
860 |
/// \param graph The undirected graph. |
|
861 |
/// \retval cutMap A writable node map. The values will be set to |
|
862 |
/// \c true for the nodes that separate two or more components |
|
863 |
/// (exactly once for each cut node), and will not be changed for |
|
864 |
/// other nodes. |
|
815 | 865 |
/// \return The number of the cut nodes. |
866 |
/// |
|
867 |
/// \see biNodeConnected(), biNodeConnectedComponents() |
|
816 | 868 |
template <typename Graph, typename NodeMap> |
817 | 869 |
int biNodeConnectedCutNodes(const Graph& graph, NodeMap& cutMap) { |
818 | 870 |
checkConcept<concepts::Graph, Graph>(); |
819 | 871 |
typedef typename Graph::Node Node; |
820 | 872 |
typedef typename Graph::NodeIt NodeIt; |
821 | 873 |
checkConcept<concepts::WriteMap<Node, bool>, NodeMap>(); |
822 | 874 |
|
823 | 875 |
using namespace _connectivity_bits; |
824 | 876 |
|
825 | 877 |
typedef BiNodeConnectedCutNodesVisitor<Graph, NodeMap> Visitor; |
826 | 878 |
|
827 | 879 |
int cutNum = 0; |
828 | 880 |
Visitor visitor(graph, cutMap, cutNum); |
829 | 881 |
|
830 | 882 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
831 | 883 |
dfs.init(); |
832 | 884 |
|
833 | 885 |
for (NodeIt it(graph); it != INVALID; ++it) { |
834 | 886 |
if (!dfs.reached(it)) { |
835 | 887 |
dfs.addSource(it); |
836 | 888 |
dfs.start(); |
837 | 889 |
} |
838 | 890 |
} |
839 | 891 |
return cutNum; |
840 | 892 |
} |
841 | 893 |
|
842 | 894 |
namespace _connectivity_bits { |
843 | 895 |
|
844 | 896 |
template <typename Digraph> |
845 | 897 |
class CountBiEdgeConnectedComponentsVisitor : public DfsVisitor<Digraph> { |
846 | 898 |
public: |
847 | 899 |
typedef typename Digraph::Node Node; |
848 | 900 |
typedef typename Digraph::Arc Arc; |
849 | 901 |
typedef typename Digraph::Edge Edge; |
850 | 902 |
|
851 | 903 |
CountBiEdgeConnectedComponentsVisitor(const Digraph& graph, int &compNum) |
852 | 904 |
: _graph(graph), _compNum(compNum), |
853 | 905 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
854 | 906 |
|
855 | 907 |
void start(const Node& node) { |
856 | 908 |
_predMap.set(node, INVALID); |
857 | 909 |
} |
858 | 910 |
|
859 | 911 |
void reach(const Node& node) { |
860 | 912 |
_numMap.set(node, _num); |
861 | 913 |
_retMap.set(node, _num); |
862 | 914 |
++_num; |
863 | 915 |
} |
864 | 916 |
|
865 | 917 |
void leave(const Node& node) { |
866 | 918 |
if (_numMap[node] <= _retMap[node]) { |
867 | 919 |
++_compNum; |
868 | 920 |
} |
869 | 921 |
} |
870 | 922 |
|
871 | 923 |
void discover(const Arc& edge) { |
872 | 924 |
_predMap.set(_graph.target(edge), edge); |
873 | 925 |
} |
874 | 926 |
|
875 | 927 |
void examine(const Arc& edge) { |
876 | 928 |
if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) { |
877 | 929 |
return; |
878 | 930 |
} |
879 | 931 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
880 | 932 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
881 | 933 |
} |
882 | 934 |
} |
883 | 935 |
|
884 | 936 |
void backtrack(const Arc& edge) { |
885 | 937 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
886 | 938 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
887 | 939 |
} |
888 | 940 |
} |
889 | 941 |
|
890 | 942 |
private: |
891 | 943 |
const Digraph& _graph; |
892 | 944 |
int& _compNum; |
893 | 945 |
|
894 | 946 |
typename Digraph::template NodeMap<int> _numMap; |
895 | 947 |
typename Digraph::template NodeMap<int> _retMap; |
896 | 948 |
typename Digraph::template NodeMap<Arc> _predMap; |
897 | 949 |
int _num; |
898 | 950 |
}; |
899 | 951 |
|
900 | 952 |
template <typename Digraph, typename NodeMap> |
901 | 953 |
class BiEdgeConnectedComponentsVisitor : public DfsVisitor<Digraph> { |
902 | 954 |
public: |
903 | 955 |
typedef typename Digraph::Node Node; |
904 | 956 |
typedef typename Digraph::Arc Arc; |
905 | 957 |
typedef typename Digraph::Edge Edge; |
906 | 958 |
|
907 | 959 |
BiEdgeConnectedComponentsVisitor(const Digraph& graph, |
908 | 960 |
NodeMap& compMap, int &compNum) |
909 | 961 |
: _graph(graph), _compMap(compMap), _compNum(compNum), |
910 | 962 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
911 | 963 |
|
912 | 964 |
void start(const Node& node) { |
913 | 965 |
_predMap.set(node, INVALID); |
914 | 966 |
} |
915 | 967 |
|
916 | 968 |
void reach(const Node& node) { |
917 | 969 |
_numMap.set(node, _num); |
918 | 970 |
_retMap.set(node, _num); |
919 | 971 |
_nodeStack.push(node); |
920 | 972 |
++_num; |
921 | 973 |
} |
922 | 974 |
|
923 | 975 |
void leave(const Node& node) { |
924 | 976 |
if (_numMap[node] <= _retMap[node]) { |
925 | 977 |
while (_nodeStack.top() != node) { |
926 | 978 |
_compMap.set(_nodeStack.top(), _compNum); |
927 | 979 |
_nodeStack.pop(); |
928 | 980 |
} |
929 | 981 |
_compMap.set(node, _compNum); |
930 | 982 |
_nodeStack.pop(); |
931 | 983 |
++_compNum; |
932 | 984 |
} |
933 | 985 |
} |
934 | 986 |
|
935 | 987 |
void discover(const Arc& edge) { |
936 | 988 |
_predMap.set(_graph.target(edge), edge); |
937 | 989 |
} |
938 | 990 |
|
939 | 991 |
void examine(const Arc& edge) { |
940 | 992 |
if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) { |
941 | 993 |
return; |
942 | 994 |
} |
943 | 995 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
944 | 996 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
945 | 997 |
} |
946 | 998 |
} |
947 | 999 |
|
948 | 1000 |
void backtrack(const Arc& edge) { |
949 | 1001 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
950 | 1002 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
951 | 1003 |
} |
952 | 1004 |
} |
953 | 1005 |
|
954 | 1006 |
private: |
955 | 1007 |
const Digraph& _graph; |
956 | 1008 |
NodeMap& _compMap; |
957 | 1009 |
int& _compNum; |
958 | 1010 |
|
959 | 1011 |
typename Digraph::template NodeMap<int> _numMap; |
960 | 1012 |
typename Digraph::template NodeMap<int> _retMap; |
961 | 1013 |
typename Digraph::template NodeMap<Arc> _predMap; |
962 | 1014 |
std::stack<Node> _nodeStack; |
963 | 1015 |
int _num; |
964 | 1016 |
}; |
965 | 1017 |
|
966 | 1018 |
|
967 | 1019 |
template <typename Digraph, typename ArcMap> |
968 | 1020 |
class BiEdgeConnectedCutEdgesVisitor : public DfsVisitor<Digraph> { |
969 | 1021 |
public: |
970 | 1022 |
typedef typename Digraph::Node Node; |
971 | 1023 |
typedef typename Digraph::Arc Arc; |
972 | 1024 |
typedef typename Digraph::Edge Edge; |
973 | 1025 |
|
974 | 1026 |
BiEdgeConnectedCutEdgesVisitor(const Digraph& graph, |
975 | 1027 |
ArcMap& cutMap, int &cutNum) |
976 | 1028 |
: _graph(graph), _cutMap(cutMap), _cutNum(cutNum), |
977 | 1029 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
978 | 1030 |
|
979 | 1031 |
void start(const Node& node) { |
980 | 1032 |
_predMap[node] = INVALID; |
981 | 1033 |
} |
982 | 1034 |
|
983 | 1035 |
void reach(const Node& node) { |
984 | 1036 |
_numMap.set(node, _num); |
985 | 1037 |
_retMap.set(node, _num); |
986 | 1038 |
++_num; |
987 | 1039 |
} |
988 | 1040 |
|
989 | 1041 |
void leave(const Node& node) { |
990 | 1042 |
if (_numMap[node] <= _retMap[node]) { |
991 | 1043 |
if (_predMap[node] != INVALID) { |
992 | 1044 |
_cutMap.set(_predMap[node], true); |
993 | 1045 |
++_cutNum; |
994 | 1046 |
} |
995 | 1047 |
} |
996 | 1048 |
} |
997 | 1049 |
|
998 | 1050 |
void discover(const Arc& edge) { |
999 | 1051 |
_predMap.set(_graph.target(edge), edge); |
1000 | 1052 |
} |
1001 | 1053 |
|
1002 | 1054 |
void examine(const Arc& edge) { |
1003 | 1055 |
if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) { |
1004 | 1056 |
return; |
1005 | 1057 |
} |
1006 | 1058 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
1007 | 1059 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
1008 | 1060 |
} |
1009 | 1061 |
} |
1010 | 1062 |
|
1011 | 1063 |
void backtrack(const Arc& edge) { |
1012 | 1064 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
1013 | 1065 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
1014 | 1066 |
} |
1015 | 1067 |
} |
1016 | 1068 |
|
1017 | 1069 |
private: |
1018 | 1070 |
const Digraph& _graph; |
1019 | 1071 |
ArcMap& _cutMap; |
1020 | 1072 |
int& _cutNum; |
1021 | 1073 |
|
1022 | 1074 |
typename Digraph::template NodeMap<int> _numMap; |
1023 | 1075 |
typename Digraph::template NodeMap<int> _retMap; |
1024 | 1076 |
typename Digraph::template NodeMap<Arc> _predMap; |
1025 | 1077 |
int _num; |
1026 | 1078 |
}; |
1027 | 1079 |
} |
1028 | 1080 |
|
1029 | 1081 |
template <typename Graph> |
1030 | 1082 |
int countBiEdgeConnectedComponents(const Graph& graph); |
1031 | 1083 |
|
1032 | 1084 |
/// \ingroup graph_properties |
1033 | 1085 |
/// |
1034 |
/// \brief |
|
1086 |
/// \brief Check whether an undirected graph is bi-edge-connected. |
|
1035 | 1087 |
/// |
1036 |
/// This function checks that the graph is bi-edge-connected. The undirected |
|
1037 |
/// graph is bi-edge-connected when any two nodes are connected with two |
|
1038 |
/// |
|
1088 |
/// This function checks whether the given undirected graph is |
|
1089 |
/// bi-edge-connected, i.e. any two nodes are connected with at least |
|
1090 |
/// two edge-disjoint paths. |
|
1039 | 1091 |
/// |
1040 |
/// \param graph The undirected graph. |
|
1041 |
/// \return The number of components. |
|
1092 |
/// \return \c true if the graph is bi-edge-connected. |
|
1093 |
/// \note By definition, the empty graph is bi-edge-connected. |
|
1094 |
/// |
|
1095 |
/// \see countBiEdgeConnectedComponents(), biEdgeConnectedComponents() |
|
1042 | 1096 |
template <typename Graph> |
1043 | 1097 |
bool biEdgeConnected(const Graph& graph) { |
1044 | 1098 |
return countBiEdgeConnectedComponents(graph) <= 1; |
1045 | 1099 |
} |
1046 | 1100 |
|
1047 | 1101 |
/// \ingroup graph_properties |
1048 | 1102 |
/// |
1049 |
/// \brief Count the bi-edge-connected components |
|
1103 |
/// \brief Count the number of bi-edge-connected components of an |
|
1104 |
/// undirected graph. |
|
1050 | 1105 |
/// |
1051 |
/// This function count the bi-edge-connected components in an undirected |
|
1052 |
/// graph. The bi-edge-connected components are the classes of an equivalence |
|
1053 |
/// relation on the nodes. Two nodes are in relationship when they are |
|
1054 |
/// connected with at least two edge-disjoint paths. |
|
1106 |
/// This function counts the number of bi-edge-connected components of |
|
1107 |
/// the given undirected graph. |
|
1055 | 1108 |
/// |
1056 |
/// \param graph The undirected graph. |
|
1057 |
/// \return The number of components. |
|
1109 |
/// The bi-edge-connected components are the classes of an equivalence |
|
1110 |
/// relation on the nodes of an undirected graph. Two nodes are in the |
|
1111 |
/// same class if they are connected with at least two edge-disjoint |
|
1112 |
/// paths. |
|
1113 |
/// |
|
1114 |
/// \return The number of bi-edge-connected components. |
|
1115 |
/// |
|
1116 |
/// \see biEdgeConnected(), biEdgeConnectedComponents() |
|
1058 | 1117 |
template <typename Graph> |
1059 | 1118 |
int countBiEdgeConnectedComponents(const Graph& graph) { |
1060 | 1119 |
checkConcept<concepts::Graph, Graph>(); |
1061 | 1120 |
typedef typename Graph::NodeIt NodeIt; |
1062 | 1121 |
|
1063 | 1122 |
using namespace _connectivity_bits; |
1064 | 1123 |
|
1065 | 1124 |
typedef CountBiEdgeConnectedComponentsVisitor<Graph> Visitor; |
1066 | 1125 |
|
1067 | 1126 |
int compNum = 0; |
1068 | 1127 |
Visitor visitor(graph, compNum); |
1069 | 1128 |
|
1070 | 1129 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
1071 | 1130 |
dfs.init(); |
1072 | 1131 |
|
1073 | 1132 |
for (NodeIt it(graph); it != INVALID; ++it) { |
1074 | 1133 |
if (!dfs.reached(it)) { |
1075 | 1134 |
dfs.addSource(it); |
1076 | 1135 |
dfs.start(); |
1077 | 1136 |
} |
1078 | 1137 |
} |
1079 | 1138 |
return compNum; |
1080 | 1139 |
} |
1081 | 1140 |
|
1082 | 1141 |
/// \ingroup graph_properties |
1083 | 1142 |
/// |
1084 |
/// \brief Find the bi-edge-connected components. |
|
1143 |
/// \brief Find the bi-edge-connected components of an undirected graph. |
|
1085 | 1144 |
/// |
1086 |
/// This function finds the bi-edge-connected components in an undirected |
|
1087 |
/// graph. The bi-edge-connected components are the classes of an equivalence |
|
1088 |
/// relation on the nodes. Two nodes are in relationship when they are |
|
1089 |
/// connected at least two edge-disjoint paths. |
|
1145 |
/// This function finds the bi-edge-connected components of the given |
|
1146 |
/// undirected graph. |
|
1147 |
/// |
|
1148 |
/// The bi-edge-connected components are the classes of an equivalence |
|
1149 |
/// relation on the nodes of an undirected graph. Two nodes are in the |
|
1150 |
/// same class if they are connected with at least two edge-disjoint |
|
1151 |
/// paths. |
|
1090 | 1152 |
/// |
1091 | 1153 |
/// \image html edge_biconnected_components.png |
1092 | 1154 |
/// \image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
1093 | 1155 |
/// |
1094 |
/// \param graph The graph. |
|
1156 |
/// \param graph The undirected graph. |
|
1095 | 1157 |
/// \retval compMap A writable node map. The values will be set from 0 to |
1096 |
/// the number of the biconnected components minus one. Each values |
|
1097 |
/// of the map will be set exactly once, the values of a certain component |
|
1098 |
/// will be set continuously. |
|
1099 |
/// \return The number of components. |
|
1158 |
/// the number of the bi-edge-connected components minus one. Each value |
|
1159 |
/// of the map will be set exactly once, and the values of a certain |
|
1160 |
/// component will be set continuously. |
|
1161 |
/// \return The number of bi-edge-connected components. |
|
1162 |
/// |
|
1163 |
/// \see biEdgeConnected(), countBiEdgeConnectedComponents() |
|
1100 | 1164 |
template <typename Graph, typename NodeMap> |
1101 | 1165 |
int biEdgeConnectedComponents(const Graph& graph, NodeMap& compMap) { |
1102 | 1166 |
checkConcept<concepts::Graph, Graph>(); |
1103 | 1167 |
typedef typename Graph::NodeIt NodeIt; |
1104 | 1168 |
typedef typename Graph::Node Node; |
1105 | 1169 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>(); |
1106 | 1170 |
|
1107 | 1171 |
using namespace _connectivity_bits; |
1108 | 1172 |
|
1109 | 1173 |
typedef BiEdgeConnectedComponentsVisitor<Graph, NodeMap> Visitor; |
1110 | 1174 |
|
1111 | 1175 |
int compNum = 0; |
1112 | 1176 |
Visitor visitor(graph, compMap, compNum); |
1113 | 1177 |
|
1114 | 1178 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
1115 | 1179 |
dfs.init(); |
1116 | 1180 |
|
1117 | 1181 |
for (NodeIt it(graph); it != INVALID; ++it) { |
1118 | 1182 |
if (!dfs.reached(it)) { |
1119 | 1183 |
dfs.addSource(it); |
1120 | 1184 |
dfs.start(); |
1121 | 1185 |
} |
1122 | 1186 |
} |
1123 | 1187 |
return compNum; |
1124 | 1188 |
} |
1125 | 1189 |
|
1126 | 1190 |
/// \ingroup graph_properties |
1127 | 1191 |
/// |
1128 |
/// \brief Find the bi-edge-connected cut edges. |
|
1192 |
/// \brief Find the bi-edge-connected cut edges in an undirected graph. |
|
1129 | 1193 |
/// |
1130 |
/// This function finds the bi-edge-connected components in an undirected |
|
1131 |
/// graph. The bi-edge-connected components are the classes of an equivalence |
|
1132 |
/// relation on the nodes. Two nodes are in relationship when they are |
|
1133 |
/// connected with at least two edge-disjoint paths. The bi-edge-connected |
|
1134 |
/// components are separted by edges which are the cut edges of the |
|
1135 |
/// components. |
|
1194 |
/// This function finds the bi-edge-connected cut edges in the given |
|
1195 |
/// undirected graph. |
|
1136 | 1196 |
/// |
1137 |
/// \param graph The graph. |
|
1138 |
/// \retval cutMap A writable node map. The values will be set true when the |
|
1139 |
/// edge |
|
1197 |
/// The bi-edge-connected components are the classes of an equivalence |
|
1198 |
/// relation on the nodes of an undirected graph. Two nodes are in the |
|
1199 |
/// same class if they are connected with at least two edge-disjoint |
|
1200 |
/// paths. |
|
1201 |
/// The bi-edge-connected components are separted by the cut edges of |
|
1202 |
/// the components. |
|
1203 |
/// |
|
1204 |
/// \param graph The undirected graph. |
|
1205 |
/// \retval cutMap A writable edge map. The values will be set to \c true |
|
1206 |
/// for the cut edges (exactly once for each cut edge), and will not be |
|
1207 |
/// changed for other edges. |
|
1140 | 1208 |
/// \return The number of cut edges. |
1209 |
/// |
|
1210 |
/// \see biEdgeConnected(), biEdgeConnectedComponents() |
|
1141 | 1211 |
template <typename Graph, typename EdgeMap> |
1142 | 1212 |
int biEdgeConnectedCutEdges(const Graph& graph, EdgeMap& cutMap) { |
1143 | 1213 |
checkConcept<concepts::Graph, Graph>(); |
1144 | 1214 |
typedef typename Graph::NodeIt NodeIt; |
1145 | 1215 |
typedef typename Graph::Edge Edge; |
1146 | 1216 |
checkConcept<concepts::WriteMap<Edge, bool>, EdgeMap>(); |
1147 | 1217 |
|
1148 | 1218 |
using namespace _connectivity_bits; |
1149 | 1219 |
|
1150 | 1220 |
typedef BiEdgeConnectedCutEdgesVisitor<Graph, EdgeMap> Visitor; |
1151 | 1221 |
|
1152 | 1222 |
int cutNum = 0; |
1153 | 1223 |
Visitor visitor(graph, cutMap, cutNum); |
1154 | 1224 |
|
1155 | 1225 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
1156 | 1226 |
dfs.init(); |
1157 | 1227 |
|
1158 | 1228 |
for (NodeIt it(graph); it != INVALID; ++it) { |
1159 | 1229 |
if (!dfs.reached(it)) { |
1160 | 1230 |
dfs.addSource(it); |
1161 | 1231 |
dfs.start(); |
1162 | 1232 |
} |
1163 | 1233 |
} |
1164 | 1234 |
return cutNum; |
1165 | 1235 |
} |
1166 | 1236 |
|
1167 | 1237 |
|
1168 | 1238 |
namespace _connectivity_bits { |
1169 | 1239 |
|
1170 | 1240 |
template <typename Digraph, typename IntNodeMap> |
1171 | 1241 |
class TopologicalSortVisitor : public DfsVisitor<Digraph> { |
1172 | 1242 |
public: |
1173 | 1243 |
typedef typename Digraph::Node Node; |
1174 | 1244 |
typedef typename Digraph::Arc edge; |
1175 | 1245 |
|
1176 | 1246 |
TopologicalSortVisitor(IntNodeMap& order, int num) |
1177 | 1247 |
: _order(order), _num(num) {} |
1178 | 1248 |
|
1179 | 1249 |
void leave(const Node& node) { |
1180 | 1250 |
_order.set(node, --_num); |
1181 | 1251 |
} |
1182 | 1252 |
|
1183 | 1253 |
private: |
1184 | 1254 |
IntNodeMap& _order; |
1185 | 1255 |
int _num; |
1186 | 1256 |
}; |
1187 | 1257 |
|
1188 | 1258 |
} |
1189 | 1259 |
|
1190 | 1260 |
/// \ingroup graph_properties |
1191 | 1261 |
/// |
1262 |
/// \brief Check whether a digraph is DAG. |
|
1263 |
/// |
|
1264 |
/// This function checks whether the given digraph is DAG, i.e. |
|
1265 |
/// \e Directed \e Acyclic \e Graph. |
|
1266 |
/// \return \c true if there is no directed cycle in the digraph. |
|
1267 |
/// \see acyclic() |
|
1268 |
template <typename Digraph> |
|
1269 |
bool dag(const Digraph& digraph) { |
|
1270 |
|
|
1271 |
checkConcept<concepts::Digraph, Digraph>(); |
|
1272 |
|
|
1273 |
typedef typename Digraph::Node Node; |
|
1274 |
typedef typename Digraph::NodeIt NodeIt; |
|
1275 |
typedef typename Digraph::Arc Arc; |
|
1276 |
|
|
1277 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
|
1278 |
|
|
1279 |
typename Dfs<Digraph>::template SetProcessedMap<ProcessedMap>:: |
|
1280 |
Create dfs(digraph); |
|
1281 |
|
|
1282 |
ProcessedMap processed(digraph); |
|
1283 |
dfs.processedMap(processed); |
|
1284 |
|
|
1285 |
dfs.init(); |
|
1286 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
|
1287 |
if (!dfs.reached(it)) { |
|
1288 |
dfs.addSource(it); |
|
1289 |
while (!dfs.emptyQueue()) { |
|
1290 |
Arc arc = dfs.nextArc(); |
|
1291 |
Node target = digraph.target(arc); |
|
1292 |
if (dfs.reached(target) && !processed[target]) { |
|
1293 |
return false; |
|
1294 |
} |
|
1295 |
dfs.processNextArc(); |
|
1296 |
} |
|
1297 |
} |
|
1298 |
} |
|
1299 |
return true; |
|
1300 |
} |
|
1301 |
|
|
1302 |
/// \ingroup graph_properties |
|
1303 |
/// |
|
1192 | 1304 |
/// \brief Sort the nodes of a DAG into topolgical order. |
1193 | 1305 |
/// |
1194 |
/// |
|
1306 |
/// This function sorts the nodes of the given acyclic digraph (DAG) |
|
1307 |
/// into topolgical order. |
|
1195 | 1308 |
/// |
1196 |
/// \param |
|
1309 |
/// \param digraph The digraph, which must be DAG. |
|
1197 | 1310 |
/// \retval order A writable node map. The values will be set from 0 to |
1198 |
/// the number of the nodes in the graph minus one. Each values of the map |
|
1199 |
/// will be set exactly once, the values will be set descending order. |
|
1311 |
/// the number of the nodes in the digraph minus one. Each value of the |
|
1312 |
/// map will be set exactly once, and the values will be set descending |
|
1313 |
/// order. |
|
1200 | 1314 |
/// |
1201 |
/// \see checkedTopologicalSort |
|
1202 |
/// \see dag |
|
1315 |
/// \see dag(), checkedTopologicalSort() |
|
1203 | 1316 |
template <typename Digraph, typename NodeMap> |
1204 |
void topologicalSort(const Digraph& |
|
1317 |
void topologicalSort(const Digraph& digraph, NodeMap& order) { |
|
1205 | 1318 |
using namespace _connectivity_bits; |
1206 | 1319 |
|
1207 | 1320 |
checkConcept<concepts::Digraph, Digraph>(); |
1208 | 1321 |
checkConcept<concepts::WriteMap<typename Digraph::Node, int>, NodeMap>(); |
1209 | 1322 |
|
1210 | 1323 |
typedef typename Digraph::Node Node; |
1211 | 1324 |
typedef typename Digraph::NodeIt NodeIt; |
1212 | 1325 |
typedef typename Digraph::Arc Arc; |
1213 | 1326 |
|
1214 | 1327 |
TopologicalSortVisitor<Digraph, NodeMap> |
1215 |
visitor(order, countNodes( |
|
1328 |
visitor(order, countNodes(digraph)); |
|
1216 | 1329 |
|
1217 | 1330 |
DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> > |
1218 |
dfs( |
|
1331 |
dfs(digraph, visitor); |
|
1219 | 1332 |
|
1220 | 1333 |
dfs.init(); |
1221 |
for (NodeIt it( |
|
1334 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
|
1222 | 1335 |
if (!dfs.reached(it)) { |
1223 | 1336 |
dfs.addSource(it); |
1224 | 1337 |
dfs.start(); |
1225 | 1338 |
} |
1226 | 1339 |
} |
1227 | 1340 |
} |
1228 | 1341 |
|
1229 | 1342 |
/// \ingroup graph_properties |
1230 | 1343 |
/// |
1231 | 1344 |
/// \brief Sort the nodes of a DAG into topolgical order. |
1232 | 1345 |
/// |
1233 |
/// Sort the nodes of a DAG into topolgical order. It also checks |
|
1234 |
/// that the given graph is DAG. |
|
1346 |
/// This function sorts the nodes of the given acyclic digraph (DAG) |
|
1347 |
/// into topolgical order and also checks whether the given digraph |
|
1348 |
/// is DAG. |
|
1235 | 1349 |
/// |
1236 |
/// \param digraph The graph. It must be directed and acyclic. |
|
1237 |
/// \retval order A readable - writable node map. The values will be set |
|
1238 |
/// from 0 to the number of the nodes in the graph minus one. Each values |
|
1239 |
/// of the map will be set exactly once, the values will be set descending |
|
1240 |
/// order. |
|
1241 |
/// \return \c false when the graph is not DAG. |
|
1350 |
/// \param digraph The digraph. |
|
1351 |
/// \retval order A readable and writable node map. The values will be |
|
1352 |
/// set from 0 to the number of the nodes in the digraph minus one. |
|
1353 |
/// Each value of the map will be set exactly once, and the values will |
|
1354 |
/// be set descending order. |
|
1355 |
/// \return \c false if the digraph is not DAG. |
|
1242 | 1356 |
/// |
1243 |
/// \see topologicalSort |
|
1244 |
/// \see dag |
|
1357 |
/// \see dag(), topologicalSort() |
|
1245 | 1358 |
template <typename Digraph, typename NodeMap> |
1246 | 1359 |
bool checkedTopologicalSort(const Digraph& digraph, NodeMap& order) { |
1247 | 1360 |
using namespace _connectivity_bits; |
1248 | 1361 |
|
1249 | 1362 |
checkConcept<concepts::Digraph, Digraph>(); |
1250 | 1363 |
checkConcept<concepts::ReadWriteMap<typename Digraph::Node, int>, |
1251 | 1364 |
NodeMap>(); |
1252 | 1365 |
|
1253 | 1366 |
typedef typename Digraph::Node Node; |
1254 | 1367 |
typedef typename Digraph::NodeIt NodeIt; |
1255 | 1368 |
typedef typename Digraph::Arc Arc; |
1256 | 1369 |
|
1257 | 1370 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
1258 | 1371 |
order.set(it, -1); |
1259 | 1372 |
} |
1260 | 1373 |
|
1261 | 1374 |
TopologicalSortVisitor<Digraph, NodeMap> |
1262 | 1375 |
visitor(order, countNodes(digraph)); |
1263 | 1376 |
|
1264 | 1377 |
DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> > |
1265 | 1378 |
dfs(digraph, visitor); |
1266 | 1379 |
|
1267 | 1380 |
dfs.init(); |
1268 | 1381 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
1269 | 1382 |
if (!dfs.reached(it)) { |
1270 | 1383 |
dfs.addSource(it); |
1271 | 1384 |
while (!dfs.emptyQueue()) { |
1272 | 1385 |
Arc arc = dfs.nextArc(); |
1273 | 1386 |
Node target = digraph.target(arc); |
1274 | 1387 |
if (dfs.reached(target) && order[target] == -1) { |
1275 | 1388 |
return false; |
1276 | 1389 |
} |
1277 | 1390 |
dfs.processNextArc(); |
1278 | 1391 |
} |
1279 | 1392 |
} |
1280 | 1393 |
} |
1281 | 1394 |
return true; |
1282 | 1395 |
} |
1283 | 1396 |
|
1284 | 1397 |
/// \ingroup graph_properties |
1285 | 1398 |
/// |
1286 |
/// \brief Check |
|
1399 |
/// \brief Check whether an undirected graph is acyclic. |
|
1287 | 1400 |
/// |
1288 |
/// Check that the given directed graph is a DAG. The DAG is |
|
1289 |
/// an Directed Acyclic Digraph. |
|
1290 |
/// \return \c false when the graph is not DAG. |
|
1291 |
/// \see acyclic |
|
1292 |
template <typename Digraph> |
|
1293 |
bool dag(const Digraph& digraph) { |
|
1294 |
|
|
1295 |
checkConcept<concepts::Digraph, Digraph>(); |
|
1296 |
|
|
1297 |
typedef typename Digraph::Node Node; |
|
1298 |
typedef typename Digraph::NodeIt NodeIt; |
|
1299 |
typedef typename Digraph::Arc Arc; |
|
1300 |
|
|
1301 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
|
1302 |
|
|
1303 |
typename Dfs<Digraph>::template SetProcessedMap<ProcessedMap>:: |
|
1304 |
Create dfs(digraph); |
|
1305 |
|
|
1306 |
ProcessedMap processed(digraph); |
|
1307 |
dfs.processedMap(processed); |
|
1308 |
|
|
1309 |
dfs.init(); |
|
1310 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
|
1311 |
if (!dfs.reached(it)) { |
|
1312 |
dfs.addSource(it); |
|
1313 |
while (!dfs.emptyQueue()) { |
|
1314 |
Arc edge = dfs.nextArc(); |
|
1315 |
Node target = digraph.target(edge); |
|
1316 |
if (dfs.reached(target) && !processed[target]) { |
|
1317 |
return false; |
|
1318 |
} |
|
1319 |
dfs.processNextArc(); |
|
1320 |
} |
|
1321 |
} |
|
1322 |
} |
|
1323 |
return true; |
|
1324 |
} |
|
1325 |
|
|
1326 |
/// \ingroup graph_properties |
|
1327 |
/// |
|
1328 |
/// \brief Check that the given undirected graph is acyclic. |
|
1329 |
/// |
|
1330 |
/// Check that the given undirected graph acyclic. |
|
1331 |
/// \param graph The undirected graph. |
|
1332 |
/// \return \c true when there is no circle in the graph. |
|
1333 |
/// \see dag |
|
1401 |
/// This function checks whether the given undirected graph is acyclic. |
|
1402 |
/// \return \c true if there is no cycle in the graph. |
|
1403 |
/// \see dag() |
|
1334 | 1404 |
template <typename Graph> |
1335 | 1405 |
bool acyclic(const Graph& graph) { |
1336 | 1406 |
checkConcept<concepts::Graph, Graph>(); |
1337 | 1407 |
typedef typename Graph::Node Node; |
1338 | 1408 |
typedef typename Graph::NodeIt NodeIt; |
1339 | 1409 |
typedef typename Graph::Arc Arc; |
1340 | 1410 |
Dfs<Graph> dfs(graph); |
1341 | 1411 |
dfs.init(); |
1342 | 1412 |
for (NodeIt it(graph); it != INVALID; ++it) { |
1343 | 1413 |
if (!dfs.reached(it)) { |
1344 | 1414 |
dfs.addSource(it); |
1345 | 1415 |
while (!dfs.emptyQueue()) { |
1346 |
Arc edge = dfs.nextArc(); |
|
1347 |
Node source = graph.source(edge); |
|
1348 |
|
|
1416 |
Arc arc = dfs.nextArc(); |
|
1417 |
Node source = graph.source(arc); |
|
1418 |
Node target = graph.target(arc); |
|
1349 | 1419 |
if (dfs.reached(target) && |
1350 |
dfs.predArc(source) != graph.oppositeArc( |
|
1420 |
dfs.predArc(source) != graph.oppositeArc(arc)) { |
|
1351 | 1421 |
return false; |
1352 | 1422 |
} |
1353 | 1423 |
dfs.processNextArc(); |
1354 | 1424 |
} |
1355 | 1425 |
} |
1356 | 1426 |
} |
1357 | 1427 |
return true; |
1358 | 1428 |
} |
1359 | 1429 |
|
1360 | 1430 |
/// \ingroup graph_properties |
1361 | 1431 |
/// |
1362 |
/// \brief Check |
|
1432 |
/// \brief Check whether an undirected graph is tree. |
|
1363 | 1433 |
/// |
1364 |
/// Check that the given undirected graph is tree. |
|
1365 |
/// \param graph The undirected graph. |
|
1366 |
/// |
|
1434 |
/// This function checks whether the given undirected graph is tree. |
|
1435 |
/// \return \c true if the graph is acyclic and connected. |
|
1436 |
/// \see acyclic(), connected() |
|
1367 | 1437 |
template <typename Graph> |
1368 | 1438 |
bool tree(const Graph& graph) { |
1369 | 1439 |
checkConcept<concepts::Graph, Graph>(); |
1370 | 1440 |
typedef typename Graph::Node Node; |
1371 | 1441 |
typedef typename Graph::NodeIt NodeIt; |
1372 | 1442 |
typedef typename Graph::Arc Arc; |
1443 |
if (NodeIt(graph) == INVALID) return true; |
|
1373 | 1444 |
Dfs<Graph> dfs(graph); |
1374 | 1445 |
dfs.init(); |
1375 | 1446 |
dfs.addSource(NodeIt(graph)); |
1376 | 1447 |
while (!dfs.emptyQueue()) { |
1377 |
Arc edge = dfs.nextArc(); |
|
1378 |
Node source = graph.source(edge); |
|
1379 |
|
|
1448 |
Arc arc = dfs.nextArc(); |
|
1449 |
Node source = graph.source(arc); |
|
1450 |
Node target = graph.target(arc); |
|
1380 | 1451 |
if (dfs.reached(target) && |
1381 |
dfs.predArc(source) != graph.oppositeArc( |
|
1452 |
dfs.predArc(source) != graph.oppositeArc(arc)) { |
|
1382 | 1453 |
return false; |
1383 | 1454 |
} |
1384 | 1455 |
dfs.processNextArc(); |
1385 | 1456 |
} |
1386 | 1457 |
for (NodeIt it(graph); it != INVALID; ++it) { |
1387 | 1458 |
if (!dfs.reached(it)) { |
1388 | 1459 |
return false; |
1389 | 1460 |
} |
1390 | 1461 |
} |
1391 | 1462 |
return true; |
1392 | 1463 |
} |
1393 | 1464 |
|
1394 | 1465 |
namespace _connectivity_bits { |
1395 | 1466 |
|
1396 | 1467 |
template <typename Digraph> |
1397 | 1468 |
class BipartiteVisitor : public BfsVisitor<Digraph> { |
1398 | 1469 |
public: |
1399 | 1470 |
typedef typename Digraph::Arc Arc; |
1400 | 1471 |
typedef typename Digraph::Node Node; |
1401 | 1472 |
|
1402 | 1473 |
BipartiteVisitor(const Digraph& graph, bool& bipartite) |
1403 | 1474 |
: _graph(graph), _part(graph), _bipartite(bipartite) {} |
1404 | 1475 |
|
1405 | 1476 |
void start(const Node& node) { |
1406 | 1477 |
_part[node] = true; |
1407 | 1478 |
} |
1408 | 1479 |
void discover(const Arc& edge) { |
1409 | 1480 |
_part.set(_graph.target(edge), !_part[_graph.source(edge)]); |
1410 | 1481 |
} |
1411 | 1482 |
void examine(const Arc& edge) { |
1412 | 1483 |
_bipartite = _bipartite && |
1413 | 1484 |
_part[_graph.target(edge)] != _part[_graph.source(edge)]; |
1414 | 1485 |
} |
1415 | 1486 |
|
1416 | 1487 |
private: |
1417 | 1488 |
|
1418 | 1489 |
const Digraph& _graph; |
1419 | 1490 |
typename Digraph::template NodeMap<bool> _part; |
1420 | 1491 |
bool& _bipartite; |
1421 | 1492 |
}; |
1422 | 1493 |
|
1423 | 1494 |
template <typename Digraph, typename PartMap> |
1424 | 1495 |
class BipartitePartitionsVisitor : public BfsVisitor<Digraph> { |
1425 | 1496 |
public: |
1426 | 1497 |
typedef typename Digraph::Arc Arc; |
1427 | 1498 |
typedef typename Digraph::Node Node; |
1428 | 1499 |
|
1429 | 1500 |
BipartitePartitionsVisitor(const Digraph& graph, |
1430 | 1501 |
PartMap& part, bool& bipartite) |
1431 | 1502 |
: _graph(graph), _part(part), _bipartite(bipartite) {} |
1432 | 1503 |
|
1433 | 1504 |
void start(const Node& node) { |
1434 | 1505 |
_part.set(node, true); |
1435 | 1506 |
} |
1436 | 1507 |
void discover(const Arc& edge) { |
1437 | 1508 |
_part.set(_graph.target(edge), !_part[_graph.source(edge)]); |
1438 | 1509 |
} |
1439 | 1510 |
void examine(const Arc& edge) { |
1440 | 1511 |
_bipartite = _bipartite && |
1441 | 1512 |
_part[_graph.target(edge)] != _part[_graph.source(edge)]; |
1442 | 1513 |
} |
1443 | 1514 |
|
1444 | 1515 |
private: |
1445 | 1516 |
|
1446 | 1517 |
const Digraph& _graph; |
1447 | 1518 |
PartMap& _part; |
1448 | 1519 |
bool& _bipartite; |
1449 | 1520 |
}; |
1450 | 1521 |
} |
1451 | 1522 |
|
1452 | 1523 |
/// \ingroup graph_properties |
1453 | 1524 |
/// |
1454 |
/// \brief Check |
|
1525 |
/// \brief Check whether an undirected graph is bipartite. |
|
1455 | 1526 |
/// |
1456 |
/// The function checks if the given undirected \c graph graph is bipartite |
|
1457 |
/// or not. The \ref Bfs algorithm is used to calculate the result. |
|
1458 |
/// \param graph The undirected graph. |
|
1459 |
/// \return \c true if \c graph is bipartite, \c false otherwise. |
|
1460 |
/// |
|
1527 |
/// The function checks whether the given undirected graph is bipartite. |
|
1528 |
/// \return \c true if the graph is bipartite. |
|
1529 |
/// |
|
1530 |
/// \see bipartitePartitions() |
|
1461 | 1531 |
template<typename Graph> |
1462 |
|
|
1532 |
bool bipartite(const Graph &graph){ |
|
1463 | 1533 |
using namespace _connectivity_bits; |
1464 | 1534 |
|
1465 | 1535 |
checkConcept<concepts::Graph, Graph>(); |
1466 | 1536 |
|
1467 | 1537 |
typedef typename Graph::NodeIt NodeIt; |
1468 | 1538 |
typedef typename Graph::ArcIt ArcIt; |
1469 | 1539 |
|
1470 | 1540 |
bool bipartite = true; |
1471 | 1541 |
|
1472 | 1542 |
BipartiteVisitor<Graph> |
1473 | 1543 |
visitor(graph, bipartite); |
1474 | 1544 |
BfsVisit<Graph, BipartiteVisitor<Graph> > |
1475 | 1545 |
bfs(graph, visitor); |
1476 | 1546 |
bfs.init(); |
1477 | 1547 |
for(NodeIt it(graph); it != INVALID; ++it) { |
1478 | 1548 |
if(!bfs.reached(it)){ |
1479 | 1549 |
bfs.addSource(it); |
1480 | 1550 |
while (!bfs.emptyQueue()) { |
1481 | 1551 |
bfs.processNextNode(); |
1482 | 1552 |
if (!bipartite) return false; |
1483 | 1553 |
} |
1484 | 1554 |
} |
1485 | 1555 |
} |
1486 | 1556 |
return true; |
1487 | 1557 |
} |
1488 | 1558 |
|
1489 | 1559 |
/// \ingroup graph_properties |
1490 | 1560 |
/// |
1491 |
/// \brief |
|
1561 |
/// \brief Find the bipartite partitions of an undirected graph. |
|
1492 | 1562 |
/// |
1493 |
/// The function checks if the given undirected graph is bipartite |
|
1494 |
/// or not. The \ref Bfs algorithm is used to calculate the result. |
|
1495 |
/// During the execution, the \c partMap will be set as the two |
|
1496 |
/// partitions of the graph. |
|
1563 |
/// This function checks whether the given undirected graph is bipartite |
|
1564 |
/// and gives back the bipartite partitions. |
|
1497 | 1565 |
/// |
1498 | 1566 |
/// \image html bipartite_partitions.png |
1499 | 1567 |
/// \image latex bipartite_partitions.eps "Bipartite partititions" width=\textwidth |
1500 | 1568 |
/// |
1501 | 1569 |
/// \param graph The undirected graph. |
1502 |
/// \retval partMap A writable bool map of nodes. It will be set as the |
|
1503 |
/// two partitions of the graph. |
|
1504 |
/// \ |
|
1570 |
/// \retval partMap A writable node map of \c bool (or convertible) value |
|
1571 |
/// type. The values will be set to \c true for one component and |
|
1572 |
/// \c false for the other one. |
|
1573 |
/// \return \c true if the graph is bipartite, \c false otherwise. |
|
1574 |
/// |
|
1575 |
/// \see bipartite() |
|
1505 | 1576 |
template<typename Graph, typename NodeMap> |
1506 |
|
|
1577 |
bool bipartitePartitions(const Graph &graph, NodeMap &partMap){ |
|
1507 | 1578 |
using namespace _connectivity_bits; |
1508 | 1579 |
|
1509 | 1580 |
checkConcept<concepts::Graph, Graph>(); |
1581 |
checkConcept<concepts::WriteMap<typename Graph::Node, bool>, NodeMap>(); |
|
1510 | 1582 |
|
1511 | 1583 |
typedef typename Graph::Node Node; |
1512 | 1584 |
typedef typename Graph::NodeIt NodeIt; |
1513 | 1585 |
typedef typename Graph::ArcIt ArcIt; |
1514 | 1586 |
|
1515 | 1587 |
bool bipartite = true; |
1516 | 1588 |
|
1517 | 1589 |
BipartitePartitionsVisitor<Graph, NodeMap> |
1518 | 1590 |
visitor(graph, partMap, bipartite); |
1519 | 1591 |
BfsVisit<Graph, BipartitePartitionsVisitor<Graph, NodeMap> > |
1520 | 1592 |
bfs(graph, visitor); |
1521 | 1593 |
bfs.init(); |
1522 | 1594 |
for(NodeIt it(graph); it != INVALID; ++it) { |
1523 | 1595 |
if(!bfs.reached(it)){ |
1524 | 1596 |
bfs.addSource(it); |
1525 | 1597 |
while (!bfs.emptyQueue()) { |
1526 | 1598 |
bfs.processNextNode(); |
1527 | 1599 |
if (!bipartite) return false; |
1528 | 1600 |
} |
1529 | 1601 |
} |
1530 | 1602 |
} |
1531 | 1603 |
return true; |
1532 | 1604 |
} |
1533 | 1605 |
|
1534 |
/// \ |
|
1606 |
/// \ingroup graph_properties |
|
1535 | 1607 |
/// |
1536 |
/// Returns true when there are not loop edges in the graph. |
|
1537 |
template <typename Digraph> |
|
1538 |
bool loopFree(const Digraph& digraph) { |
|
1539 |
for (typename Digraph::ArcIt it(digraph); it != INVALID; ++it) { |
|
1540 |
|
|
1608 |
/// \brief Check whether the given graph contains no loop arcs/edges. |
|
1609 |
/// |
|
1610 |
/// This function returns \c true if there are no loop arcs/edges in |
|
1611 |
/// the given graph. It works for both directed and undirected graphs. |
|
1612 |
template <typename Graph> |
|
1613 |
bool loopFree(const Graph& graph) { |
|
1614 |
for (typename Graph::ArcIt it(graph); it != INVALID; ++it) { |
|
1615 |
if (graph.source(it) == graph.target(it)) return false; |
|
1541 | 1616 |
} |
1542 | 1617 |
return true; |
1543 | 1618 |
} |
1544 | 1619 |
|
1545 |
/// \ |
|
1620 |
/// \ingroup graph_properties |
|
1546 | 1621 |
/// |
1547 |
/// Returns true when there are not parallel edges in the graph. |
|
1548 |
template <typename Digraph> |
|
1549 |
bool parallelFree(const Digraph& digraph) { |
|
1550 |
typename Digraph::template NodeMap<bool> reached(digraph, false); |
|
1551 |
for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) { |
|
1552 |
for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) { |
|
1553 |
if (reached[digraph.target(a)]) return false; |
|
1554 |
reached.set(digraph.target(a), true); |
|
1622 |
/// \brief Check whether the given graph contains no parallel arcs/edges. |
|
1623 |
/// |
|
1624 |
/// This function returns \c true if there are no parallel arcs/edges in |
|
1625 |
/// the given graph. It works for both directed and undirected graphs. |
|
1626 |
template <typename Graph> |
|
1627 |
bool parallelFree(const Graph& graph) { |
|
1628 |
typename Graph::template NodeMap<int> reached(graph, 0); |
|
1629 |
int cnt = 1; |
|
1630 |
for (typename Graph::NodeIt n(graph); n != INVALID; ++n) { |
|
1631 |
for (typename Graph::OutArcIt a(graph, n); a != INVALID; ++a) { |
|
1632 |
if (reached[graph.target(a)] == cnt) return false; |
|
1633 |
reached[graph.target(a)] = cnt; |
|
1555 | 1634 |
} |
1556 |
for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) { |
|
1557 |
reached.set(digraph.target(a), false); |
|
1558 |
|
|
1635 |
++cnt; |
|
1559 | 1636 |
} |
1560 | 1637 |
return true; |
1561 | 1638 |
} |
1562 | 1639 |
|
1563 |
/// \brief Returns true when there are not loop edges and parallel |
|
1564 |
/// edges in the graph. |
|
1640 |
/// \ingroup graph_properties |
|
1565 | 1641 |
/// |
1566 |
/// Returns true when there are not loop edges and parallel edges in |
|
1567 |
/// the graph. |
|
1568 |
template <typename Digraph> |
|
1569 |
bool simpleDigraph(const Digraph& digraph) { |
|
1570 |
typename Digraph::template NodeMap<bool> reached(digraph, false); |
|
1571 |
for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) { |
|
1572 |
reached.set(n, true); |
|
1573 |
for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) { |
|
1574 |
if (reached[digraph.target(a)]) return false; |
|
1575 |
reached.set(digraph.target(a), true); |
|
1642 |
/// \brief Check whether the given graph is simple. |
|
1643 |
/// |
|
1644 |
/// This function returns \c true if the given graph is simple, i.e. |
|
1645 |
/// it contains no loop arcs/edges and no parallel arcs/edges. |
|
1646 |
/// The function works for both directed and undirected graphs. |
|
1647 |
/// \see loopFree(), parallelFree() |
|
1648 |
template <typename Graph> |
|
1649 |
bool simpleGraph(const Graph& graph) { |
|
1650 |
typename Graph::template NodeMap<int> reached(graph, 0); |
|
1651 |
int cnt = 1; |
|
1652 |
for (typename Graph::NodeIt n(graph); n != INVALID; ++n) { |
|
1653 |
reached[n] = cnt; |
|
1654 |
for (typename Graph::OutArcIt a(graph, n); a != INVALID; ++a) { |
|
1655 |
if (reached[graph.target(a)] == cnt) return false; |
|
1656 |
reached[graph.target(a)] = cnt; |
|
1576 | 1657 |
} |
1577 |
for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) { |
|
1578 |
reached.set(digraph.target(a), false); |
|
1579 |
} |
|
1580 |
reached.set(n, false); |
|
1658 |
++cnt; |
|
1581 | 1659 |
} |
1582 | 1660 |
return true; |
1583 | 1661 |
} |
1584 | 1662 |
|
1585 | 1663 |
} //namespace lemon |
1586 | 1664 |
|
1587 | 1665 |
#endif //LEMON_CONNECTIVITY_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 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_EDGE_SET_H |
20 | 20 |
#define LEMON_EDGE_SET_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/bits/edge_set_extender.h> |
24 | 24 |
|
25 |
/// \ingroup |
|
25 |
/// \ingroup graphs |
|
26 | 26 |
/// \file |
27 | 27 |
/// \brief ArcSet and EdgeSet classes. |
28 | 28 |
/// |
29 | 29 |
/// Graphs which use another graph's node-set as own. |
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
template <typename GR> |
33 | 33 |
class ListArcSetBase { |
34 | 34 |
public: |
35 | 35 |
|
36 | 36 |
typedef typename GR::Node Node; |
37 | 37 |
typedef typename GR::NodeIt NodeIt; |
38 | 38 |
|
39 | 39 |
protected: |
40 | 40 |
|
41 | 41 |
struct NodeT { |
42 | 42 |
int first_out, first_in; |
43 | 43 |
NodeT() : first_out(-1), first_in(-1) {} |
44 | 44 |
}; |
45 | 45 |
|
46 | 46 |
typedef typename ItemSetTraits<GR, Node>:: |
47 | 47 |
template Map<NodeT>::Type NodesImplBase; |
48 | 48 |
|
49 | 49 |
NodesImplBase* _nodes; |
50 | 50 |
|
51 | 51 |
struct ArcT { |
52 | 52 |
Node source, target; |
53 | 53 |
int next_out, next_in; |
54 | 54 |
int prev_out, prev_in; |
55 | 55 |
ArcT() : prev_out(-1), prev_in(-1) {} |
56 | 56 |
}; |
57 | 57 |
|
58 | 58 |
std::vector<ArcT> arcs; |
59 | 59 |
|
60 | 60 |
int first_arc; |
61 | 61 |
int first_free_arc; |
62 | 62 |
|
63 | 63 |
const GR* _graph; |
64 | 64 |
|
65 | 65 |
void initalize(const GR& graph, NodesImplBase& nodes) { |
66 | 66 |
_graph = &graph; |
67 | 67 |
_nodes = &nodes; |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
public: |
71 | 71 |
|
72 | 72 |
class Arc { |
73 | 73 |
friend class ListArcSetBase<GR>; |
74 | 74 |
protected: |
75 | 75 |
Arc(int _id) : id(_id) {} |
76 | 76 |
int id; |
77 | 77 |
public: |
78 | 78 |
Arc() {} |
79 | 79 |
Arc(Invalid) : id(-1) {} |
80 | 80 |
bool operator==(const Arc& arc) const { return id == arc.id; } |
81 | 81 |
bool operator!=(const Arc& arc) const { return id != arc.id; } |
82 | 82 |
bool operator<(const Arc& arc) const { return id < arc.id; } |
83 | 83 |
}; |
84 | 84 |
|
85 | 85 |
ListArcSetBase() : first_arc(-1), first_free_arc(-1) {} |
86 | 86 |
|
87 | 87 |
Arc addArc(const Node& u, const Node& v) { |
88 | 88 |
int n; |
89 | 89 |
if (first_free_arc == -1) { |
90 | 90 |
n = arcs.size(); |
91 | 91 |
arcs.push_back(ArcT()); |
92 | 92 |
} else { |
93 | 93 |
n = first_free_arc; |
94 | 94 |
first_free_arc = arcs[first_free_arc].next_in; |
95 | 95 |
} |
96 | 96 |
arcs[n].next_in = (*_nodes)[v].first_in; |
97 | 97 |
if ((*_nodes)[v].first_in != -1) { |
98 | 98 |
arcs[(*_nodes)[v].first_in].prev_in = n; |
99 | 99 |
} |
100 | 100 |
(*_nodes)[v].first_in = n; |
101 | 101 |
arcs[n].next_out = (*_nodes)[u].first_out; |
102 | 102 |
if ((*_nodes)[u].first_out != -1) { |
103 | 103 |
arcs[(*_nodes)[u].first_out].prev_out = n; |
104 | 104 |
} |
105 | 105 |
(*_nodes)[u].first_out = n; |
106 | 106 |
arcs[n].source = u; |
107 | 107 |
arcs[n].target = v; |
108 | 108 |
return Arc(n); |
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
void erase(const Arc& arc) { |
112 | 112 |
int n = arc.id; |
113 | 113 |
if (arcs[n].prev_in != -1) { |
114 | 114 |
arcs[arcs[n].prev_in].next_in = arcs[n].next_in; |
115 | 115 |
} else { |
116 | 116 |
(*_nodes)[arcs[n].target].first_in = arcs[n].next_in; |
117 | 117 |
} |
118 | 118 |
if (arcs[n].next_in != -1) { |
119 | 119 |
arcs[arcs[n].next_in].prev_in = arcs[n].prev_in; |
120 | 120 |
} |
121 | 121 |
|
122 | 122 |
if (arcs[n].prev_out != -1) { |
123 | 123 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
124 | 124 |
} else { |
125 | 125 |
(*_nodes)[arcs[n].source].first_out = arcs[n].next_out; |
126 | 126 |
} |
127 | 127 |
if (arcs[n].next_out != -1) { |
128 | 128 |
arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
} |
132 | 132 |
|
133 | 133 |
void clear() { |
134 | 134 |
Node node; |
135 | 135 |
for (first(node); node != INVALID; next(node)) { |
136 | 136 |
(*_nodes)[node].first_in = -1; |
137 | 137 |
(*_nodes)[node].first_out = -1; |
138 | 138 |
} |
139 | 139 |
arcs.clear(); |
140 | 140 |
first_arc = -1; |
141 | 141 |
first_free_arc = -1; |
142 | 142 |
} |
143 | 143 |
|
144 | 144 |
void first(Node& node) const { |
145 | 145 |
_graph->first(node); |
146 | 146 |
} |
147 | 147 |
|
148 | 148 |
void next(Node& node) const { |
149 | 149 |
_graph->next(node); |
150 | 150 |
} |
151 | 151 |
|
152 | 152 |
void first(Arc& arc) const { |
153 | 153 |
Node node; |
154 | 154 |
first(node); |
155 | 155 |
while (node != INVALID && (*_nodes)[node].first_in == -1) { |
156 | 156 |
next(node); |
157 | 157 |
} |
158 | 158 |
arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in; |
159 | 159 |
} |
160 | 160 |
|
161 | 161 |
void next(Arc& arc) const { |
162 | 162 |
if (arcs[arc.id].next_in != -1) { |
163 | 163 |
arc.id = arcs[arc.id].next_in; |
164 | 164 |
} else { |
165 | 165 |
Node node = arcs[arc.id].target; |
166 | 166 |
next(node); |
167 | 167 |
while (node != INVALID && (*_nodes)[node].first_in == -1) { |
168 | 168 |
next(node); |
169 | 169 |
} |
170 | 170 |
arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_in; |
171 | 171 |
} |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
void firstOut(Arc& arc, const Node& node) const { |
175 | 175 |
arc.id = (*_nodes)[node].first_out; |
176 | 176 |
} |
177 | 177 |
|
178 | 178 |
void nextOut(Arc& arc) const { |
179 | 179 |
arc.id = arcs[arc.id].next_out; |
180 | 180 |
} |
181 | 181 |
|
182 | 182 |
void firstIn(Arc& arc, const Node& node) const { |
183 | 183 |
arc.id = (*_nodes)[node].first_in; |
184 | 184 |
} |
185 | 185 |
|
186 | 186 |
void nextIn(Arc& arc) const { |
187 | 187 |
arc.id = arcs[arc.id].next_in; |
188 | 188 |
} |
189 | 189 |
|
190 | 190 |
int id(const Node& node) const { return _graph->id(node); } |
191 | 191 |
int id(const Arc& arc) const { return arc.id; } |
192 | 192 |
|
193 | 193 |
Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } |
194 | 194 |
Arc arcFromId(int ix) const { return Arc(ix); } |
195 | 195 |
|
196 | 196 |
int maxNodeId() const { return _graph->maxNodeId(); }; |
197 | 197 |
int maxArcId() const { return arcs.size() - 1; } |
198 | 198 |
|
199 | 199 |
Node source(const Arc& arc) const { return arcs[arc.id].source;} |
200 | 200 |
Node target(const Arc& arc) const { return arcs[arc.id].target;} |
201 | 201 |
|
202 | 202 |
typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
203 | 203 |
|
204 | 204 |
NodeNotifier& notifier(Node) const { |
205 | 205 |
return _graph->notifier(Node()); |
206 | 206 |
} |
207 | 207 |
|
208 | 208 |
template <typename V> |
209 | 209 |
class NodeMap : public GR::template NodeMap<V> { |
210 | 210 |
typedef typename GR::template NodeMap<V> Parent; |
211 | 211 |
|
212 | 212 |
public: |
213 | 213 |
|
214 | 214 |
explicit NodeMap(const ListArcSetBase<GR>& arcset) |
215 | 215 |
: Parent(*arcset._graph) {} |
216 | 216 |
|
217 | 217 |
NodeMap(const ListArcSetBase<GR>& arcset, const V& value) |
218 | 218 |
: Parent(*arcset._graph, value) {} |
219 | 219 |
|
220 | 220 |
NodeMap& operator=(const NodeMap& cmap) { |
221 | 221 |
return operator=<NodeMap>(cmap); |
222 | 222 |
} |
223 | 223 |
|
224 | 224 |
template <typename CMap> |
225 | 225 |
NodeMap& operator=(const CMap& cmap) { |
226 | 226 |
Parent::operator=(cmap); |
227 | 227 |
return *this; |
228 | 228 |
} |
229 | 229 |
}; |
230 | 230 |
|
231 | 231 |
}; |
232 | 232 |
|
233 |
/// \ingroup |
|
233 |
/// \ingroup graphs |
|
234 | 234 |
/// |
235 | 235 |
/// \brief Digraph using a node set of another digraph or graph and |
236 | 236 |
/// an own arc set. |
237 | 237 |
/// |
238 | 238 |
/// This structure can be used to establish another directed graph |
239 | 239 |
/// over a node set of an existing one. This class uses the same |
240 | 240 |
/// Node type as the underlying graph, and each valid node of the |
241 | 241 |
/// original graph is valid in this arc set, therefore the node |
242 | 242 |
/// objects of the original graph can be used directly with this |
243 | 243 |
/// class. The node handling functions (id handling, observing, and |
244 | 244 |
/// iterators) works equivalently as in the original graph. |
245 | 245 |
/// |
246 | 246 |
/// This implementation is based on doubly-linked lists, from each |
247 | 247 |
/// node the outgoing and the incoming arcs make up lists, therefore |
248 | 248 |
/// one arc can be erased in constant time. It also makes possible, |
249 | 249 |
/// that node can be removed from the underlying graph, in this case |
250 | 250 |
/// all arcs incident to the given node is erased from the arc set. |
251 | 251 |
/// |
252 | 252 |
/// \param GR The type of the graph which shares its node set with |
253 | 253 |
/// this class. Its interface must conform to the |
254 | 254 |
/// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph" |
255 | 255 |
/// concept. |
256 | 256 |
/// |
257 | 257 |
/// This class fully conforms to the \ref concepts::Digraph |
258 | 258 |
/// "Digraph" concept. |
259 | 259 |
template <typename GR> |
260 | 260 |
class ListArcSet : public ArcSetExtender<ListArcSetBase<GR> > { |
261 | 261 |
typedef ArcSetExtender<ListArcSetBase<GR> > Parent; |
262 | 262 |
|
263 | 263 |
public: |
264 | 264 |
|
265 | 265 |
typedef typename Parent::Node Node; |
266 | 266 |
typedef typename Parent::Arc Arc; |
267 | 267 |
|
268 | 268 |
typedef typename Parent::NodesImplBase NodesImplBase; |
269 | 269 |
|
270 | 270 |
void eraseNode(const Node& node) { |
271 | 271 |
Arc arc; |
272 | 272 |
Parent::firstOut(arc, node); |
273 | 273 |
while (arc != INVALID ) { |
274 | 274 |
erase(arc); |
275 | 275 |
Parent::firstOut(arc, node); |
276 | 276 |
} |
277 | 277 |
|
278 | 278 |
Parent::firstIn(arc, node); |
279 | 279 |
while (arc != INVALID ) { |
280 | 280 |
erase(arc); |
281 | 281 |
Parent::firstIn(arc, node); |
282 | 282 |
} |
283 | 283 |
} |
284 | 284 |
|
285 | 285 |
void clearNodes() { |
286 | 286 |
Parent::clear(); |
287 | 287 |
} |
288 | 288 |
|
289 | 289 |
class NodesImpl : public NodesImplBase { |
290 | 290 |
typedef NodesImplBase Parent; |
291 | 291 |
|
292 | 292 |
public: |
293 | 293 |
NodesImpl(const GR& graph, ListArcSet& arcset) |
294 | 294 |
: Parent(graph), _arcset(arcset) {} |
295 | 295 |
|
296 | 296 |
virtual ~NodesImpl() {} |
297 | 297 |
|
298 | 298 |
protected: |
299 | 299 |
|
300 | 300 |
virtual void erase(const Node& node) { |
301 | 301 |
_arcset.eraseNode(node); |
302 | 302 |
Parent::erase(node); |
303 | 303 |
} |
304 | 304 |
virtual void erase(const std::vector<Node>& nodes) { |
305 | 305 |
for (int i = 0; i < int(nodes.size()); ++i) { |
306 | 306 |
_arcset.eraseNode(nodes[i]); |
307 | 307 |
} |
308 | 308 |
Parent::erase(nodes); |
309 | 309 |
} |
310 | 310 |
virtual void clear() { |
311 | 311 |
_arcset.clearNodes(); |
312 | 312 |
Parent::clear(); |
313 | 313 |
} |
314 | 314 |
|
315 | 315 |
private: |
316 | 316 |
ListArcSet& _arcset; |
317 | 317 |
}; |
318 | 318 |
|
319 | 319 |
NodesImpl _nodes; |
320 | 320 |
|
321 | 321 |
public: |
322 | 322 |
|
323 | 323 |
/// \brief Constructor of the ArcSet. |
324 | 324 |
/// |
325 | 325 |
/// Constructor of the ArcSet. |
326 | 326 |
ListArcSet(const GR& graph) : _nodes(graph, *this) { |
327 | 327 |
Parent::initalize(graph, _nodes); |
328 | 328 |
} |
329 | 329 |
|
330 | 330 |
/// \brief Add a new arc to the digraph. |
331 | 331 |
/// |
332 | 332 |
/// Add a new arc to the digraph with source node \c s |
333 | 333 |
/// and target node \c t. |
334 | 334 |
/// \return The new arc. |
335 | 335 |
Arc addArc(const Node& s, const Node& t) { |
336 | 336 |
return Parent::addArc(s, t); |
337 | 337 |
} |
338 | 338 |
|
339 | 339 |
/// \brief Erase an arc from the digraph. |
340 | 340 |
/// |
341 | 341 |
/// Erase an arc \c a from the digraph. |
342 | 342 |
void erase(const Arc& a) { |
343 | 343 |
return Parent::erase(a); |
344 | 344 |
} |
345 | 345 |
|
346 | 346 |
}; |
347 | 347 |
|
348 | 348 |
template <typename GR> |
349 | 349 |
class ListEdgeSetBase { |
350 | 350 |
public: |
351 | 351 |
|
352 | 352 |
typedef typename GR::Node Node; |
353 | 353 |
typedef typename GR::NodeIt NodeIt; |
354 | 354 |
|
355 | 355 |
protected: |
356 | 356 |
|
357 | 357 |
struct NodeT { |
358 | 358 |
int first_out; |
359 | 359 |
NodeT() : first_out(-1) {} |
360 | 360 |
}; |
361 | 361 |
|
362 | 362 |
typedef typename ItemSetTraits<GR, Node>:: |
363 | 363 |
template Map<NodeT>::Type NodesImplBase; |
364 | 364 |
|
365 | 365 |
NodesImplBase* _nodes; |
366 | 366 |
|
367 | 367 |
struct ArcT { |
368 | 368 |
Node target; |
369 | 369 |
int prev_out, next_out; |
370 | 370 |
ArcT() : prev_out(-1), next_out(-1) {} |
371 | 371 |
}; |
372 | 372 |
|
373 | 373 |
std::vector<ArcT> arcs; |
374 | 374 |
|
375 | 375 |
int first_arc; |
376 | 376 |
int first_free_arc; |
377 | 377 |
|
378 | 378 |
const GR* _graph; |
379 | 379 |
|
380 | 380 |
void initalize(const GR& graph, NodesImplBase& nodes) { |
381 | 381 |
_graph = &graph; |
382 | 382 |
_nodes = &nodes; |
383 | 383 |
} |
384 | 384 |
|
385 | 385 |
public: |
386 | 386 |
|
387 | 387 |
class Edge { |
388 | 388 |
friend class ListEdgeSetBase; |
389 | 389 |
protected: |
390 | 390 |
|
391 | 391 |
int id; |
392 | 392 |
explicit Edge(int _id) { id = _id;} |
393 | 393 |
|
394 | 394 |
public: |
395 | 395 |
Edge() {} |
396 | 396 |
Edge (Invalid) { id = -1; } |
397 | 397 |
bool operator==(const Edge& arc) const {return id == arc.id;} |
398 | 398 |
bool operator!=(const Edge& arc) const {return id != arc.id;} |
399 | 399 |
bool operator<(const Edge& arc) const {return id < arc.id;} |
400 | 400 |
}; |
401 | 401 |
|
402 | 402 |
class Arc { |
403 | 403 |
friend class ListEdgeSetBase; |
404 | 404 |
protected: |
405 | 405 |
Arc(int _id) : id(_id) {} |
406 | 406 |
int id; |
407 | 407 |
public: |
408 | 408 |
operator Edge() const { return edgeFromId(id / 2); } |
409 | 409 |
|
410 | 410 |
Arc() {} |
411 | 411 |
Arc(Invalid) : id(-1) {} |
412 | 412 |
bool operator==(const Arc& arc) const { return id == arc.id; } |
413 | 413 |
bool operator!=(const Arc& arc) const { return id != arc.id; } |
414 | 414 |
bool operator<(const Arc& arc) const { return id < arc.id; } |
415 | 415 |
}; |
416 | 416 |
|
417 | 417 |
ListEdgeSetBase() : first_arc(-1), first_free_arc(-1) {} |
418 | 418 |
|
419 | 419 |
Edge addEdge(const Node& u, const Node& v) { |
420 | 420 |
int n; |
421 | 421 |
|
422 | 422 |
if (first_free_arc == -1) { |
423 | 423 |
n = arcs.size(); |
424 | 424 |
arcs.push_back(ArcT()); |
425 | 425 |
arcs.push_back(ArcT()); |
426 | 426 |
} else { |
427 | 427 |
n = first_free_arc; |
428 | 428 |
first_free_arc = arcs[n].next_out; |
429 | 429 |
} |
430 | 430 |
|
431 | 431 |
arcs[n].target = u; |
432 | 432 |
arcs[n | 1].target = v; |
433 | 433 |
|
434 | 434 |
arcs[n].next_out = (*_nodes)[v].first_out; |
435 | 435 |
if ((*_nodes)[v].first_out != -1) { |
436 | 436 |
arcs[(*_nodes)[v].first_out].prev_out = n; |
437 | 437 |
} |
438 | 438 |
(*_nodes)[v].first_out = n; |
439 | 439 |
arcs[n].prev_out = -1; |
440 | 440 |
|
441 | 441 |
if ((*_nodes)[u].first_out != -1) { |
442 | 442 |
arcs[(*_nodes)[u].first_out].prev_out = (n | 1); |
443 | 443 |
} |
444 | 444 |
arcs[n | 1].next_out = (*_nodes)[u].first_out; |
445 | 445 |
(*_nodes)[u].first_out = (n | 1); |
446 | 446 |
arcs[n | 1].prev_out = -1; |
447 | 447 |
|
448 | 448 |
return Edge(n / 2); |
449 | 449 |
} |
450 | 450 |
|
451 | 451 |
void erase(const Edge& arc) { |
452 | 452 |
int n = arc.id * 2; |
453 | 453 |
|
454 | 454 |
if (arcs[n].next_out != -1) { |
455 | 455 |
arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
456 | 456 |
} |
457 | 457 |
|
458 | 458 |
if (arcs[n].prev_out != -1) { |
459 | 459 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
460 | 460 |
} else { |
461 | 461 |
(*_nodes)[arcs[n | 1].target].first_out = arcs[n].next_out; |
462 | 462 |
} |
463 | 463 |
|
464 | 464 |
if (arcs[n | 1].next_out != -1) { |
465 | 465 |
arcs[arcs[n | 1].next_out].prev_out = arcs[n | 1].prev_out; |
466 | 466 |
} |
467 | 467 |
|
468 | 468 |
if (arcs[n | 1].prev_out != -1) { |
469 | 469 |
arcs[arcs[n | 1].prev_out].next_out = arcs[n | 1].next_out; |
470 | 470 |
} else { |
471 | 471 |
(*_nodes)[arcs[n].target].first_out = arcs[n | 1].next_out; |
472 | 472 |
} |
473 | 473 |
|
474 | 474 |
arcs[n].next_out = first_free_arc; |
475 | 475 |
first_free_arc = n; |
476 | 476 |
|
477 | 477 |
} |
478 | 478 |
|
479 | 479 |
void clear() { |
480 | 480 |
Node node; |
481 | 481 |
for (first(node); node != INVALID; next(node)) { |
482 | 482 |
(*_nodes)[node].first_out = -1; |
483 | 483 |
} |
484 | 484 |
arcs.clear(); |
485 | 485 |
first_arc = -1; |
486 | 486 |
first_free_arc = -1; |
487 | 487 |
} |
488 | 488 |
|
489 | 489 |
void first(Node& node) const { |
490 | 490 |
_graph->first(node); |
491 | 491 |
} |
492 | 492 |
|
493 | 493 |
void next(Node& node) const { |
494 | 494 |
_graph->next(node); |
495 | 495 |
} |
496 | 496 |
|
497 | 497 |
void first(Arc& arc) const { |
498 | 498 |
Node node; |
499 | 499 |
first(node); |
500 | 500 |
while (node != INVALID && (*_nodes)[node].first_out == -1) { |
501 | 501 |
next(node); |
502 | 502 |
} |
503 | 503 |
arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out; |
504 | 504 |
} |
505 | 505 |
|
506 | 506 |
void next(Arc& arc) const { |
507 | 507 |
if (arcs[arc.id].next_out != -1) { |
508 | 508 |
arc.id = arcs[arc.id].next_out; |
509 | 509 |
} else { |
510 | 510 |
Node node = arcs[arc.id ^ 1].target; |
511 | 511 |
next(node); |
512 | 512 |
while(node != INVALID && (*_nodes)[node].first_out == -1) { |
513 | 513 |
next(node); |
514 | 514 |
} |
515 | 515 |
arc.id = (node == INVALID) ? -1 : (*_nodes)[node].first_out; |
516 | 516 |
} |
517 | 517 |
} |
518 | 518 |
|
519 | 519 |
void first(Edge& edge) const { |
520 | 520 |
Node node; |
521 | 521 |
first(node); |
522 | 522 |
while (node != INVALID) { |
523 | 523 |
edge.id = (*_nodes)[node].first_out; |
524 | 524 |
while ((edge.id & 1) != 1) { |
525 | 525 |
edge.id = arcs[edge.id].next_out; |
526 | 526 |
} |
527 | 527 |
if (edge.id != -1) { |
528 | 528 |
edge.id /= 2; |
529 | 529 |
return; |
530 | 530 |
} |
531 | 531 |
next(node); |
532 | 532 |
} |
533 | 533 |
edge.id = -1; |
534 | 534 |
} |
535 | 535 |
|
536 | 536 |
void next(Edge& edge) const { |
537 | 537 |
Node node = arcs[edge.id * 2].target; |
538 | 538 |
edge.id = arcs[(edge.id * 2) | 1].next_out; |
539 | 539 |
while ((edge.id & 1) != 1) { |
540 | 540 |
edge.id = arcs[edge.id].next_out; |
541 | 541 |
} |
542 | 542 |
if (edge.id != -1) { |
543 | 543 |
edge.id /= 2; |
544 | 544 |
return; |
545 | 545 |
} |
546 | 546 |
next(node); |
547 | 547 |
while (node != INVALID) { |
548 | 548 |
edge.id = (*_nodes)[node].first_out; |
549 | 549 |
while ((edge.id & 1) != 1) { |
550 | 550 |
edge.id = arcs[edge.id].next_out; |
551 | 551 |
} |
552 | 552 |
if (edge.id != -1) { |
553 | 553 |
edge.id /= 2; |
554 | 554 |
return; |
555 | 555 |
} |
556 | 556 |
next(node); |
557 | 557 |
} |
558 | 558 |
edge.id = -1; |
559 | 559 |
} |
560 | 560 |
|
561 | 561 |
void firstOut(Arc& arc, const Node& node) const { |
562 | 562 |
arc.id = (*_nodes)[node].first_out; |
563 | 563 |
} |
564 | 564 |
|
565 | 565 |
void nextOut(Arc& arc) const { |
566 | 566 |
arc.id = arcs[arc.id].next_out; |
567 | 567 |
} |
568 | 568 |
|
569 | 569 |
void firstIn(Arc& arc, const Node& node) const { |
570 | 570 |
arc.id = (((*_nodes)[node].first_out) ^ 1); |
571 | 571 |
if (arc.id == -2) arc.id = -1; |
572 | 572 |
} |
573 | 573 |
|
574 | 574 |
void nextIn(Arc& arc) const { |
575 | 575 |
arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1); |
576 | 576 |
if (arc.id == -2) arc.id = -1; |
577 | 577 |
} |
578 | 578 |
|
579 | 579 |
void firstInc(Edge &arc, bool& dir, const Node& node) const { |
580 | 580 |
int de = (*_nodes)[node].first_out; |
581 | 581 |
if (de != -1 ) { |
582 | 582 |
arc.id = de / 2; |
583 | 583 |
dir = ((de & 1) == 1); |
584 | 584 |
} else { |
585 | 585 |
arc.id = -1; |
586 | 586 |
dir = true; |
587 | 587 |
} |
588 | 588 |
} |
589 | 589 |
void nextInc(Edge &arc, bool& dir) const { |
590 | 590 |
int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out); |
591 | 591 |
if (de != -1 ) { |
592 | 592 |
arc.id = de / 2; |
593 | 593 |
dir = ((de & 1) == 1); |
594 | 594 |
} else { |
595 | 595 |
arc.id = -1; |
596 | 596 |
dir = true; |
597 | 597 |
} |
598 | 598 |
} |
599 | 599 |
|
600 | 600 |
static bool direction(Arc arc) { |
601 | 601 |
return (arc.id & 1) == 1; |
602 | 602 |
} |
603 | 603 |
|
604 | 604 |
static Arc direct(Edge edge, bool dir) { |
605 | 605 |
return Arc(edge.id * 2 + (dir ? 1 : 0)); |
606 | 606 |
} |
607 | 607 |
|
608 | 608 |
int id(const Node& node) const { return _graph->id(node); } |
609 | 609 |
static int id(Arc e) { return e.id; } |
610 | 610 |
static int id(Edge e) { return e.id; } |
611 | 611 |
|
612 | 612 |
Node nodeFromId(int id) const { return _graph->nodeFromId(id); } |
613 | 613 |
static Arc arcFromId(int id) { return Arc(id);} |
614 | 614 |
static Edge edgeFromId(int id) { return Edge(id);} |
615 | 615 |
|
616 | 616 |
int maxNodeId() const { return _graph->maxNodeId(); }; |
617 | 617 |
int maxEdgeId() const { return arcs.size() / 2 - 1; } |
618 | 618 |
int maxArcId() const { return arcs.size()-1; } |
619 | 619 |
|
620 | 620 |
Node source(Arc e) const { return arcs[e.id ^ 1].target; } |
621 | 621 |
Node target(Arc e) const { return arcs[e.id].target; } |
622 | 622 |
|
623 | 623 |
Node u(Edge e) const { return arcs[2 * e.id].target; } |
624 | 624 |
Node v(Edge e) const { return arcs[2 * e.id + 1].target; } |
625 | 625 |
|
626 | 626 |
typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
627 | 627 |
|
628 | 628 |
NodeNotifier& notifier(Node) const { |
629 | 629 |
return _graph->notifier(Node()); |
630 | 630 |
} |
631 | 631 |
|
632 | 632 |
template <typename V> |
633 | 633 |
class NodeMap : public GR::template NodeMap<V> { |
634 | 634 |
typedef typename GR::template NodeMap<V> Parent; |
635 | 635 |
|
636 | 636 |
public: |
637 | 637 |
|
638 | 638 |
explicit NodeMap(const ListEdgeSetBase<GR>& arcset) |
639 | 639 |
: Parent(*arcset._graph) {} |
640 | 640 |
|
641 | 641 |
NodeMap(const ListEdgeSetBase<GR>& arcset, const V& value) |
642 | 642 |
: Parent(*arcset._graph, value) {} |
643 | 643 |
|
644 | 644 |
NodeMap& operator=(const NodeMap& cmap) { |
645 | 645 |
return operator=<NodeMap>(cmap); |
646 | 646 |
} |
647 | 647 |
|
648 | 648 |
template <typename CMap> |
649 | 649 |
NodeMap& operator=(const CMap& cmap) { |
650 | 650 |
Parent::operator=(cmap); |
651 | 651 |
return *this; |
652 | 652 |
} |
653 | 653 |
}; |
654 | 654 |
|
655 | 655 |
}; |
656 | 656 |
|
657 |
/// \ingroup |
|
657 |
/// \ingroup graphs |
|
658 | 658 |
/// |
659 | 659 |
/// \brief Graph using a node set of another digraph or graph and an |
660 | 660 |
/// own edge set. |
661 | 661 |
/// |
662 | 662 |
/// This structure can be used to establish another graph over a |
663 | 663 |
/// node set of an existing one. This class uses the same Node type |
664 | 664 |
/// as the underlying graph, and each valid node of the original |
665 | 665 |
/// graph is valid in this arc set, therefore the node objects of |
666 | 666 |
/// the original graph can be used directly with this class. The |
667 | 667 |
/// node handling functions (id handling, observing, and iterators) |
668 | 668 |
/// works equivalently as in the original graph. |
669 | 669 |
/// |
670 | 670 |
/// This implementation is based on doubly-linked lists, from each |
671 | 671 |
/// node the incident edges make up lists, therefore one edge can be |
672 | 672 |
/// erased in constant time. It also makes possible, that node can |
673 | 673 |
/// be removed from the underlying graph, in this case all edges |
674 | 674 |
/// incident to the given node is erased from the arc set. |
675 | 675 |
/// |
676 | 676 |
/// \param GR The type of the graph which shares its node set |
677 | 677 |
/// with this class. Its interface must conform to the |
678 | 678 |
/// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph" |
679 | 679 |
/// concept. |
680 | 680 |
/// |
681 | 681 |
/// This class fully conforms to the \ref concepts::Graph "Graph" |
682 | 682 |
/// concept. |
683 | 683 |
template <typename GR> |
684 | 684 |
class ListEdgeSet : public EdgeSetExtender<ListEdgeSetBase<GR> > { |
685 | 685 |
typedef EdgeSetExtender<ListEdgeSetBase<GR> > Parent; |
686 | 686 |
|
687 | 687 |
public: |
688 | 688 |
|
689 | 689 |
typedef typename Parent::Node Node; |
690 | 690 |
typedef typename Parent::Arc Arc; |
691 | 691 |
typedef typename Parent::Edge Edge; |
692 | 692 |
|
693 | 693 |
typedef typename Parent::NodesImplBase NodesImplBase; |
694 | 694 |
|
695 | 695 |
void eraseNode(const Node& node) { |
696 | 696 |
Arc arc; |
697 | 697 |
Parent::firstOut(arc, node); |
698 | 698 |
while (arc != INVALID ) { |
699 | 699 |
erase(arc); |
700 | 700 |
Parent::firstOut(arc, node); |
701 | 701 |
} |
702 | 702 |
|
703 | 703 |
} |
704 | 704 |
|
705 | 705 |
void clearNodes() { |
706 | 706 |
Parent::clear(); |
707 | 707 |
} |
708 | 708 |
|
709 | 709 |
class NodesImpl : public NodesImplBase { |
710 | 710 |
typedef NodesImplBase Parent; |
711 | 711 |
|
712 | 712 |
public: |
713 | 713 |
NodesImpl(const GR& graph, ListEdgeSet& arcset) |
714 | 714 |
: Parent(graph), _arcset(arcset) {} |
715 | 715 |
|
716 | 716 |
virtual ~NodesImpl() {} |
717 | 717 |
|
718 | 718 |
protected: |
719 | 719 |
|
720 | 720 |
virtual void erase(const Node& node) { |
721 | 721 |
_arcset.eraseNode(node); |
722 | 722 |
Parent::erase(node); |
723 | 723 |
} |
724 | 724 |
virtual void erase(const std::vector<Node>& nodes) { |
725 | 725 |
for (int i = 0; i < int(nodes.size()); ++i) { |
726 | 726 |
_arcset.eraseNode(nodes[i]); |
727 | 727 |
} |
728 | 728 |
Parent::erase(nodes); |
729 | 729 |
} |
730 | 730 |
virtual void clear() { |
731 | 731 |
_arcset.clearNodes(); |
732 | 732 |
Parent::clear(); |
733 | 733 |
} |
734 | 734 |
|
735 | 735 |
private: |
736 | 736 |
ListEdgeSet& _arcset; |
737 | 737 |
}; |
738 | 738 |
|
739 | 739 |
NodesImpl _nodes; |
740 | 740 |
|
741 | 741 |
public: |
742 | 742 |
|
743 | 743 |
/// \brief Constructor of the EdgeSet. |
744 | 744 |
/// |
745 | 745 |
/// Constructor of the EdgeSet. |
746 | 746 |
ListEdgeSet(const GR& graph) : _nodes(graph, *this) { |
747 | 747 |
Parent::initalize(graph, _nodes); |
748 | 748 |
} |
749 | 749 |
|
750 | 750 |
/// \brief Add a new edge to the graph. |
751 | 751 |
/// |
752 | 752 |
/// Add a new edge to the graph with node \c u |
753 | 753 |
/// and node \c v endpoints. |
754 | 754 |
/// \return The new edge. |
755 | 755 |
Edge addEdge(const Node& u, const Node& v) { |
756 | 756 |
return Parent::addEdge(u, v); |
757 | 757 |
} |
758 | 758 |
|
759 | 759 |
/// \brief Erase an edge from the graph. |
760 | 760 |
/// |
761 | 761 |
/// Erase the edge \c e from the graph. |
762 | 762 |
void erase(const Edge& e) { |
763 | 763 |
return Parent::erase(e); |
764 | 764 |
} |
765 | 765 |
|
766 | 766 |
}; |
767 | 767 |
|
768 | 768 |
template <typename GR> |
769 | 769 |
class SmartArcSetBase { |
770 | 770 |
public: |
771 | 771 |
|
772 | 772 |
typedef typename GR::Node Node; |
773 | 773 |
typedef typename GR::NodeIt NodeIt; |
774 | 774 |
|
775 | 775 |
protected: |
776 | 776 |
|
777 | 777 |
struct NodeT { |
778 | 778 |
int first_out, first_in; |
779 | 779 |
NodeT() : first_out(-1), first_in(-1) {} |
780 | 780 |
}; |
781 | 781 |
|
782 | 782 |
typedef typename ItemSetTraits<GR, Node>:: |
783 | 783 |
template Map<NodeT>::Type NodesImplBase; |
784 | 784 |
|
785 | 785 |
NodesImplBase* _nodes; |
786 | 786 |
|
787 | 787 |
struct ArcT { |
788 | 788 |
Node source, target; |
789 | 789 |
int next_out, next_in; |
790 | 790 |
ArcT() {} |
791 | 791 |
}; |
792 | 792 |
|
793 | 793 |
std::vector<ArcT> arcs; |
794 | 794 |
|
795 | 795 |
const GR* _graph; |
796 | 796 |
|
797 | 797 |
void initalize(const GR& graph, NodesImplBase& nodes) { |
798 | 798 |
_graph = &graph; |
799 | 799 |
_nodes = &nodes; |
800 | 800 |
} |
801 | 801 |
|
802 | 802 |
public: |
803 | 803 |
|
804 | 804 |
class Arc { |
805 | 805 |
friend class SmartArcSetBase<GR>; |
806 | 806 |
protected: |
807 | 807 |
Arc(int _id) : id(_id) {} |
808 | 808 |
int id; |
809 | 809 |
public: |
810 | 810 |
Arc() {} |
811 | 811 |
Arc(Invalid) : id(-1) {} |
812 | 812 |
bool operator==(const Arc& arc) const { return id == arc.id; } |
813 | 813 |
bool operator!=(const Arc& arc) const { return id != arc.id; } |
814 | 814 |
bool operator<(const Arc& arc) const { return id < arc.id; } |
815 | 815 |
}; |
816 | 816 |
|
817 | 817 |
SmartArcSetBase() {} |
818 | 818 |
|
819 | 819 |
Arc addArc(const Node& u, const Node& v) { |
820 | 820 |
int n = arcs.size(); |
821 | 821 |
arcs.push_back(ArcT()); |
822 | 822 |
arcs[n].next_in = (*_nodes)[v].first_in; |
823 | 823 |
(*_nodes)[v].first_in = n; |
824 | 824 |
arcs[n].next_out = (*_nodes)[u].first_out; |
825 | 825 |
(*_nodes)[u].first_out = n; |
826 | 826 |
arcs[n].source = u; |
827 | 827 |
arcs[n].target = v; |
828 | 828 |
return Arc(n); |
829 | 829 |
} |
830 | 830 |
|
831 | 831 |
void clear() { |
832 | 832 |
Node node; |
833 | 833 |
for (first(node); node != INVALID; next(node)) { |
834 | 834 |
(*_nodes)[node].first_in = -1; |
835 | 835 |
(*_nodes)[node].first_out = -1; |
836 | 836 |
} |
837 | 837 |
arcs.clear(); |
838 | 838 |
} |
839 | 839 |
|
840 | 840 |
void first(Node& node) const { |
841 | 841 |
_graph->first(node); |
842 | 842 |
} |
843 | 843 |
|
844 | 844 |
void next(Node& node) const { |
845 | 845 |
_graph->next(node); |
846 | 846 |
} |
847 | 847 |
|
848 | 848 |
void first(Arc& arc) const { |
849 | 849 |
arc.id = arcs.size() - 1; |
850 | 850 |
} |
851 | 851 |
|
852 | 852 |
void next(Arc& arc) const { |
853 | 853 |
--arc.id; |
854 | 854 |
} |
855 | 855 |
|
856 | 856 |
void firstOut(Arc& arc, const Node& node) const { |
857 | 857 |
arc.id = (*_nodes)[node].first_out; |
858 | 858 |
} |
859 | 859 |
|
860 | 860 |
void nextOut(Arc& arc) const { |
861 | 861 |
arc.id = arcs[arc.id].next_out; |
862 | 862 |
} |
863 | 863 |
|
864 | 864 |
void firstIn(Arc& arc, const Node& node) const { |
865 | 865 |
arc.id = (*_nodes)[node].first_in; |
866 | 866 |
} |
867 | 867 |
|
868 | 868 |
void nextIn(Arc& arc) const { |
869 | 869 |
arc.id = arcs[arc.id].next_in; |
870 | 870 |
} |
871 | 871 |
|
872 | 872 |
int id(const Node& node) const { return _graph->id(node); } |
873 | 873 |
int id(const Arc& arc) const { return arc.id; } |
874 | 874 |
|
875 | 875 |
Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } |
876 | 876 |
Arc arcFromId(int ix) const { return Arc(ix); } |
877 | 877 |
|
878 | 878 |
int maxNodeId() const { return _graph->maxNodeId(); }; |
879 | 879 |
int maxArcId() const { return arcs.size() - 1; } |
880 | 880 |
|
881 | 881 |
Node source(const Arc& arc) const { return arcs[arc.id].source;} |
882 | 882 |
Node target(const Arc& arc) const { return arcs[arc.id].target;} |
883 | 883 |
|
884 | 884 |
typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
885 | 885 |
|
886 | 886 |
NodeNotifier& notifier(Node) const { |
887 | 887 |
return _graph->notifier(Node()); |
888 | 888 |
} |
889 | 889 |
|
890 | 890 |
template <typename V> |
891 | 891 |
class NodeMap : public GR::template NodeMap<V> { |
892 | 892 |
typedef typename GR::template NodeMap<V> Parent; |
893 | 893 |
|
894 | 894 |
public: |
895 | 895 |
|
896 | 896 |
explicit NodeMap(const SmartArcSetBase<GR>& arcset) |
897 | 897 |
: Parent(*arcset._graph) { } |
898 | 898 |
|
899 | 899 |
NodeMap(const SmartArcSetBase<GR>& arcset, const V& value) |
900 | 900 |
: Parent(*arcset._graph, value) { } |
901 | 901 |
|
902 | 902 |
NodeMap& operator=(const NodeMap& cmap) { |
903 | 903 |
return operator=<NodeMap>(cmap); |
904 | 904 |
} |
905 | 905 |
|
906 | 906 |
template <typename CMap> |
907 | 907 |
NodeMap& operator=(const CMap& cmap) { |
908 | 908 |
Parent::operator=(cmap); |
909 | 909 |
return *this; |
910 | 910 |
} |
911 | 911 |
}; |
912 | 912 |
|
913 | 913 |
}; |
914 | 914 |
|
915 | 915 |
|
916 |
/// \ingroup |
|
916 |
/// \ingroup graphs |
|
917 | 917 |
/// |
918 | 918 |
/// \brief Digraph using a node set of another digraph or graph and |
919 | 919 |
/// an own arc set. |
920 | 920 |
/// |
921 | 921 |
/// This structure can be used to establish another directed graph |
922 | 922 |
/// over a node set of an existing one. This class uses the same |
923 | 923 |
/// Node type as the underlying graph, and each valid node of the |
924 | 924 |
/// original graph is valid in this arc set, therefore the node |
925 | 925 |
/// objects of the original graph can be used directly with this |
926 | 926 |
/// class. The node handling functions (id handling, observing, and |
927 | 927 |
/// iterators) works equivalently as in the original graph. |
928 | 928 |
/// |
929 | 929 |
/// \param GR The type of the graph which shares its node set with |
930 | 930 |
/// this class. Its interface must conform to the |
931 | 931 |
/// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph" |
932 | 932 |
/// concept. |
933 | 933 |
/// |
934 | 934 |
/// This implementation is slightly faster than the \c ListArcSet, |
935 | 935 |
/// because it uses continuous storage for arcs and it uses just |
936 | 936 |
/// single-linked lists for enumerate outgoing and incoming |
937 | 937 |
/// arcs. Therefore the arcs cannot be erased from the arc sets. |
938 | 938 |
/// |
939 | 939 |
/// \warning If a node is erased from the underlying graph and this |
940 | 940 |
/// node is the source or target of one arc in the arc set, then |
941 | 941 |
/// the arc set is invalidated, and it cannot be used anymore. The |
942 | 942 |
/// validity can be checked with the \c valid() member function. |
943 | 943 |
/// |
944 | 944 |
/// This class fully conforms to the \ref concepts::Digraph |
945 | 945 |
/// "Digraph" concept. |
946 | 946 |
template <typename GR> |
947 | 947 |
class SmartArcSet : public ArcSetExtender<SmartArcSetBase<GR> > { |
948 | 948 |
typedef ArcSetExtender<SmartArcSetBase<GR> > Parent; |
949 | 949 |
|
950 | 950 |
public: |
951 | 951 |
|
952 | 952 |
typedef typename Parent::Node Node; |
953 | 953 |
typedef typename Parent::Arc Arc; |
954 | 954 |
|
955 | 955 |
protected: |
956 | 956 |
|
957 | 957 |
typedef typename Parent::NodesImplBase NodesImplBase; |
958 | 958 |
|
959 | 959 |
void eraseNode(const Node& node) { |
960 | 960 |
if (typename Parent::InArcIt(*this, node) == INVALID && |
961 | 961 |
typename Parent::OutArcIt(*this, node) == INVALID) { |
962 | 962 |
return; |
963 | 963 |
} |
964 | 964 |
throw typename NodesImplBase::Notifier::ImmediateDetach(); |
965 | 965 |
} |
966 | 966 |
|
967 | 967 |
void clearNodes() { |
968 | 968 |
Parent::clear(); |
969 | 969 |
} |
970 | 970 |
|
971 | 971 |
class NodesImpl : public NodesImplBase { |
972 | 972 |
typedef NodesImplBase Parent; |
973 | 973 |
|
974 | 974 |
public: |
975 | 975 |
NodesImpl(const GR& graph, SmartArcSet& arcset) |
976 | 976 |
: Parent(graph), _arcset(arcset) {} |
977 | 977 |
|
978 | 978 |
virtual ~NodesImpl() {} |
979 | 979 |
|
980 | 980 |
bool attached() const { |
981 | 981 |
return Parent::attached(); |
982 | 982 |
} |
983 | 983 |
|
984 | 984 |
protected: |
985 | 985 |
|
986 | 986 |
virtual void erase(const Node& node) { |
987 | 987 |
try { |
988 | 988 |
_arcset.eraseNode(node); |
989 | 989 |
Parent::erase(node); |
990 | 990 |
} catch (const typename NodesImplBase::Notifier::ImmediateDetach&) { |
991 | 991 |
Parent::clear(); |
992 | 992 |
throw; |
993 | 993 |
} |
994 | 994 |
} |
995 | 995 |
virtual void erase(const std::vector<Node>& nodes) { |
996 | 996 |
try { |
997 | 997 |
for (int i = 0; i < int(nodes.size()); ++i) { |
998 | 998 |
_arcset.eraseNode(nodes[i]); |
999 | 999 |
} |
1000 | 1000 |
Parent::erase(nodes); |
1001 | 1001 |
} catch (const typename NodesImplBase::Notifier::ImmediateDetach&) { |
1002 | 1002 |
Parent::clear(); |
1003 | 1003 |
throw; |
1004 | 1004 |
} |
1005 | 1005 |
} |
1006 | 1006 |
virtual void clear() { |
1007 | 1007 |
_arcset.clearNodes(); |
1008 | 1008 |
Parent::clear(); |
1009 | 1009 |
} |
1010 | 1010 |
|
1011 | 1011 |
private: |
1012 | 1012 |
SmartArcSet& _arcset; |
1013 | 1013 |
}; |
1014 | 1014 |
|
1015 | 1015 |
NodesImpl _nodes; |
1016 | 1016 |
|
1017 | 1017 |
public: |
1018 | 1018 |
|
1019 | 1019 |
/// \brief Constructor of the ArcSet. |
1020 | 1020 |
/// |
1021 | 1021 |
/// Constructor of the ArcSet. |
1022 | 1022 |
SmartArcSet(const GR& graph) : _nodes(graph, *this) { |
1023 | 1023 |
Parent::initalize(graph, _nodes); |
1024 | 1024 |
} |
1025 | 1025 |
|
1026 | 1026 |
/// \brief Add a new arc to the digraph. |
1027 | 1027 |
/// |
1028 | 1028 |
/// Add a new arc to the digraph with source node \c s |
1029 | 1029 |
/// and target node \c t. |
1030 | 1030 |
/// \return The new arc. |
1031 | 1031 |
Arc addArc(const Node& s, const Node& t) { |
1032 | 1032 |
return Parent::addArc(s, t); |
1033 | 1033 |
} |
1034 | 1034 |
|
1035 | 1035 |
/// \brief Validity check |
1036 | 1036 |
/// |
1037 | 1037 |
/// This functions gives back false if the ArcSet is |
1038 | 1038 |
/// invalidated. It occurs when a node in the underlying graph is |
1039 | 1039 |
/// erased and it is not isolated in the ArcSet. |
1040 | 1040 |
bool valid() const { |
1041 | 1041 |
return _nodes.attached(); |
1042 | 1042 |
} |
1043 | 1043 |
|
1044 | 1044 |
}; |
1045 | 1045 |
|
1046 | 1046 |
|
1047 | 1047 |
template <typename GR> |
1048 | 1048 |
class SmartEdgeSetBase { |
1049 | 1049 |
public: |
1050 | 1050 |
|
1051 | 1051 |
typedef typename GR::Node Node; |
1052 | 1052 |
typedef typename GR::NodeIt NodeIt; |
1053 | 1053 |
|
1054 | 1054 |
protected: |
1055 | 1055 |
|
1056 | 1056 |
struct NodeT { |
1057 | 1057 |
int first_out; |
1058 | 1058 |
NodeT() : first_out(-1) {} |
1059 | 1059 |
}; |
1060 | 1060 |
|
1061 | 1061 |
typedef typename ItemSetTraits<GR, Node>:: |
1062 | 1062 |
template Map<NodeT>::Type NodesImplBase; |
1063 | 1063 |
|
1064 | 1064 |
NodesImplBase* _nodes; |
1065 | 1065 |
|
1066 | 1066 |
struct ArcT { |
1067 | 1067 |
Node target; |
1068 | 1068 |
int next_out; |
1069 | 1069 |
ArcT() {} |
1070 | 1070 |
}; |
1071 | 1071 |
|
1072 | 1072 |
std::vector<ArcT> arcs; |
1073 | 1073 |
|
1074 | 1074 |
const GR* _graph; |
1075 | 1075 |
|
1076 | 1076 |
void initalize(const GR& graph, NodesImplBase& nodes) { |
1077 | 1077 |
_graph = &graph; |
1078 | 1078 |
_nodes = &nodes; |
1079 | 1079 |
} |
1080 | 1080 |
|
1081 | 1081 |
public: |
1082 | 1082 |
|
1083 | 1083 |
class Edge { |
1084 | 1084 |
friend class SmartEdgeSetBase; |
1085 | 1085 |
protected: |
1086 | 1086 |
|
1087 | 1087 |
int id; |
1088 | 1088 |
explicit Edge(int _id) { id = _id;} |
1089 | 1089 |
|
1090 | 1090 |
public: |
1091 | 1091 |
Edge() {} |
1092 | 1092 |
Edge (Invalid) { id = -1; } |
1093 | 1093 |
bool operator==(const Edge& arc) const {return id == arc.id;} |
1094 | 1094 |
bool operator!=(const Edge& arc) const {return id != arc.id;} |
1095 | 1095 |
bool operator<(const Edge& arc) const {return id < arc.id;} |
1096 | 1096 |
}; |
1097 | 1097 |
|
1098 | 1098 |
class Arc { |
1099 | 1099 |
friend class SmartEdgeSetBase; |
1100 | 1100 |
protected: |
1101 | 1101 |
Arc(int _id) : id(_id) {} |
1102 | 1102 |
int id; |
1103 | 1103 |
public: |
1104 | 1104 |
operator Edge() const { return edgeFromId(id / 2); } |
1105 | 1105 |
|
1106 | 1106 |
Arc() {} |
1107 | 1107 |
Arc(Invalid) : id(-1) {} |
1108 | 1108 |
bool operator==(const Arc& arc) const { return id == arc.id; } |
1109 | 1109 |
bool operator!=(const Arc& arc) const { return id != arc.id; } |
1110 | 1110 |
bool operator<(const Arc& arc) const { return id < arc.id; } |
1111 | 1111 |
}; |
1112 | 1112 |
|
1113 | 1113 |
SmartEdgeSetBase() {} |
1114 | 1114 |
|
1115 | 1115 |
Edge addEdge(const Node& u, const Node& v) { |
1116 | 1116 |
int n = arcs.size(); |
1117 | 1117 |
arcs.push_back(ArcT()); |
1118 | 1118 |
arcs.push_back(ArcT()); |
1119 | 1119 |
|
1120 | 1120 |
arcs[n].target = u; |
1121 | 1121 |
arcs[n | 1].target = v; |
1122 | 1122 |
|
1123 | 1123 |
arcs[n].next_out = (*_nodes)[v].first_out; |
1124 | 1124 |
(*_nodes)[v].first_out = n; |
1125 | 1125 |
|
1126 | 1126 |
arcs[n | 1].next_out = (*_nodes)[u].first_out; |
1127 | 1127 |
(*_nodes)[u].first_out = (n | 1); |
1128 | 1128 |
|
1129 | 1129 |
return Edge(n / 2); |
1130 | 1130 |
} |
1131 | 1131 |
|
1132 | 1132 |
void clear() { |
1133 | 1133 |
Node node; |
1134 | 1134 |
for (first(node); node != INVALID; next(node)) { |
1135 | 1135 |
(*_nodes)[node].first_out = -1; |
1136 | 1136 |
} |
1137 | 1137 |
arcs.clear(); |
1138 | 1138 |
} |
1139 | 1139 |
|
1140 | 1140 |
void first(Node& node) const { |
1141 | 1141 |
_graph->first(node); |
1142 | 1142 |
} |
1143 | 1143 |
|
1144 | 1144 |
void next(Node& node) const { |
1145 | 1145 |
_graph->next(node); |
1146 | 1146 |
} |
1147 | 1147 |
|
1148 | 1148 |
void first(Arc& arc) const { |
1149 | 1149 |
arc.id = arcs.size() - 1; |
1150 | 1150 |
} |
1151 | 1151 |
|
1152 | 1152 |
void next(Arc& arc) const { |
1153 | 1153 |
--arc.id; |
1154 | 1154 |
} |
1155 | 1155 |
|
1156 | 1156 |
void first(Edge& arc) const { |
1157 | 1157 |
arc.id = arcs.size() / 2 - 1; |
1158 | 1158 |
} |
1159 | 1159 |
|
1160 | 1160 |
void next(Edge& arc) const { |
1161 | 1161 |
--arc.id; |
1162 | 1162 |
} |
1163 | 1163 |
|
1164 | 1164 |
void firstOut(Arc& arc, const Node& node) const { |
1165 | 1165 |
arc.id = (*_nodes)[node].first_out; |
1166 | 1166 |
} |
1167 | 1167 |
|
1168 | 1168 |
void nextOut(Arc& arc) const { |
1169 | 1169 |
arc.id = arcs[arc.id].next_out; |
1170 | 1170 |
} |
1171 | 1171 |
|
1172 | 1172 |
void firstIn(Arc& arc, const Node& node) const { |
1173 | 1173 |
arc.id = (((*_nodes)[node].first_out) ^ 1); |
1174 | 1174 |
if (arc.id == -2) arc.id = -1; |
1175 | 1175 |
} |
1176 | 1176 |
|
1177 | 1177 |
void nextIn(Arc& arc) const { |
1178 | 1178 |
arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1); |
1179 | 1179 |
if (arc.id == -2) arc.id = -1; |
1180 | 1180 |
} |
1181 | 1181 |
|
1182 | 1182 |
void firstInc(Edge &arc, bool& dir, const Node& node) const { |
1183 | 1183 |
int de = (*_nodes)[node].first_out; |
1184 | 1184 |
if (de != -1 ) { |
1185 | 1185 |
arc.id = de / 2; |
1186 | 1186 |
dir = ((de & 1) == 1); |
1187 | 1187 |
} else { |
1188 | 1188 |
arc.id = -1; |
1189 | 1189 |
dir = true; |
1190 | 1190 |
} |
1191 | 1191 |
} |
1192 | 1192 |
void nextInc(Edge &arc, bool& dir) const { |
1193 | 1193 |
int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out); |
1194 | 1194 |
if (de != -1 ) { |
1195 | 1195 |
arc.id = de / 2; |
1196 | 1196 |
dir = ((de & 1) == 1); |
1197 | 1197 |
} else { |
1198 | 1198 |
arc.id = -1; |
1199 | 1199 |
dir = true; |
1200 | 1200 |
} |
1201 | 1201 |
} |
1202 | 1202 |
|
1203 | 1203 |
static bool direction(Arc arc) { |
1204 | 1204 |
return (arc.id & 1) == 1; |
1205 | 1205 |
} |
1206 | 1206 |
|
1207 | 1207 |
static Arc direct(Edge edge, bool dir) { |
1208 | 1208 |
return Arc(edge.id * 2 + (dir ? 1 : 0)); |
1209 | 1209 |
} |
1210 | 1210 |
|
1211 | 1211 |
int id(Node node) const { return _graph->id(node); } |
1212 | 1212 |
static int id(Arc arc) { return arc.id; } |
1213 | 1213 |
static int id(Edge arc) { return arc.id; } |
1214 | 1214 |
|
1215 | 1215 |
Node nodeFromId(int id) const { return _graph->nodeFromId(id); } |
1216 | 1216 |
static Arc arcFromId(int id) { return Arc(id); } |
1217 | 1217 |
static Edge edgeFromId(int id) { return Edge(id);} |
1218 | 1218 |
|
1219 | 1219 |
int maxNodeId() const { return _graph->maxNodeId(); }; |
1220 | 1220 |
int maxArcId() const { return arcs.size() - 1; } |
1221 | 1221 |
int maxEdgeId() const { return arcs.size() / 2 - 1; } |
1222 | 1222 |
|
1223 | 1223 |
Node source(Arc e) const { return arcs[e.id ^ 1].target; } |
1224 | 1224 |
Node target(Arc e) const { return arcs[e.id].target; } |
1225 | 1225 |
|
1226 | 1226 |
Node u(Edge e) const { return arcs[2 * e.id].target; } |
1227 | 1227 |
Node v(Edge e) const { return arcs[2 * e.id + 1].target; } |
1228 | 1228 |
|
1229 | 1229 |
typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
1230 | 1230 |
|
1231 | 1231 |
NodeNotifier& notifier(Node) const { |
1232 | 1232 |
return _graph->notifier(Node()); |
1233 | 1233 |
} |
1234 | 1234 |
|
1235 | 1235 |
template <typename V> |
1236 | 1236 |
class NodeMap : public GR::template NodeMap<V> { |
1237 | 1237 |
typedef typename GR::template NodeMap<V> Parent; |
1238 | 1238 |
|
1239 | 1239 |
public: |
1240 | 1240 |
|
1241 | 1241 |
explicit NodeMap(const SmartEdgeSetBase<GR>& arcset) |
1242 | 1242 |
: Parent(*arcset._graph) { } |
1243 | 1243 |
|
1244 | 1244 |
NodeMap(const SmartEdgeSetBase<GR>& arcset, const V& value) |
1245 | 1245 |
: Parent(*arcset._graph, value) { } |
1246 | 1246 |
|
1247 | 1247 |
NodeMap& operator=(const NodeMap& cmap) { |
1248 | 1248 |
return operator=<NodeMap>(cmap); |
1249 | 1249 |
} |
1250 | 1250 |
|
1251 | 1251 |
template <typename CMap> |
1252 | 1252 |
NodeMap& operator=(const CMap& cmap) { |
1253 | 1253 |
Parent::operator=(cmap); |
1254 | 1254 |
return *this; |
1255 | 1255 |
} |
1256 | 1256 |
}; |
1257 | 1257 |
|
1258 | 1258 |
}; |
1259 | 1259 |
|
1260 |
/// \ingroup |
|
1260 |
/// \ingroup graphs |
|
1261 | 1261 |
/// |
1262 | 1262 |
/// \brief Graph using a node set of another digraph or graph and an |
1263 | 1263 |
/// own edge set. |
1264 | 1264 |
/// |
1265 | 1265 |
/// This structure can be used to establish another graph over a |
1266 | 1266 |
/// node set of an existing one. This class uses the same Node type |
1267 | 1267 |
/// as the underlying graph, and each valid node of the original |
1268 | 1268 |
/// graph is valid in this arc set, therefore the node objects of |
1269 | 1269 |
/// the original graph can be used directly with this class. The |
1270 | 1270 |
/// node handling functions (id handling, observing, and iterators) |
1271 | 1271 |
/// works equivalently as in the original graph. |
1272 | 1272 |
/// |
1273 | 1273 |
/// \param GR The type of the graph which shares its node set |
1274 | 1274 |
/// with this class. Its interface must conform to the |
1275 | 1275 |
/// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph" |
1276 | 1276 |
/// concept. |
1277 | 1277 |
/// |
1278 | 1278 |
/// This implementation is slightly faster than the \c ListEdgeSet, |
1279 | 1279 |
/// because it uses continuous storage for edges and it uses just |
1280 | 1280 |
/// single-linked lists for enumerate incident edges. Therefore the |
1281 | 1281 |
/// edges cannot be erased from the edge sets. |
1282 | 1282 |
/// |
1283 | 1283 |
/// \warning If a node is erased from the underlying graph and this |
1284 | 1284 |
/// node is incident to one edge in the edge set, then the edge set |
1285 | 1285 |
/// is invalidated, and it cannot be used anymore. The validity can |
1286 | 1286 |
/// be checked with the \c valid() member function. |
1287 | 1287 |
/// |
1288 | 1288 |
/// This class fully conforms to the \ref concepts::Graph |
1289 | 1289 |
/// "Graph" concept. |
1290 | 1290 |
template <typename GR> |
1291 | 1291 |
class SmartEdgeSet : public EdgeSetExtender<SmartEdgeSetBase<GR> > { |
1292 | 1292 |
typedef EdgeSetExtender<SmartEdgeSetBase<GR> > Parent; |
1293 | 1293 |
|
1294 | 1294 |
public: |
1295 | 1295 |
|
1296 | 1296 |
typedef typename Parent::Node Node; |
1297 | 1297 |
typedef typename Parent::Arc Arc; |
1298 | 1298 |
typedef typename Parent::Edge Edge; |
1299 | 1299 |
|
1300 | 1300 |
protected: |
1301 | 1301 |
|
1302 | 1302 |
typedef typename Parent::NodesImplBase NodesImplBase; |
1303 | 1303 |
|
1304 | 1304 |
void eraseNode(const Node& node) { |
1305 | 1305 |
if (typename Parent::IncEdgeIt(*this, node) == INVALID) { |
1306 | 1306 |
return; |
1307 | 1307 |
} |
1308 | 1308 |
throw typename NodesImplBase::Notifier::ImmediateDetach(); |
1309 | 1309 |
} |
1310 | 1310 |
|
1311 | 1311 |
void clearNodes() { |
1312 | 1312 |
Parent::clear(); |
1313 | 1313 |
} |
1314 | 1314 |
|
1315 | 1315 |
class NodesImpl : public NodesImplBase { |
1316 | 1316 |
typedef NodesImplBase Parent; |
1317 | 1317 |
|
1318 | 1318 |
public: |
1319 | 1319 |
NodesImpl(const GR& graph, SmartEdgeSet& arcset) |
1320 | 1320 |
: Parent(graph), _arcset(arcset) {} |
1321 | 1321 |
|
1322 | 1322 |
virtual ~NodesImpl() {} |
1323 | 1323 |
|
1324 | 1324 |
bool attached() const { |
1325 | 1325 |
return Parent::attached(); |
1326 | 1326 |
} |
1327 | 1327 |
|
1328 | 1328 |
protected: |
1329 | 1329 |
|
1330 | 1330 |
virtual void erase(const Node& node) { |
1331 | 1331 |
try { |
1332 | 1332 |
_arcset.eraseNode(node); |
1333 | 1333 |
Parent::erase(node); |
1334 | 1334 |
} catch (const typename NodesImplBase::Notifier::ImmediateDetach&) { |
1335 | 1335 |
Parent::clear(); |
1336 | 1336 |
throw; |
1337 | 1337 |
} |
1338 | 1338 |
} |
1339 | 1339 |
virtual void erase(const std::vector<Node>& nodes) { |
1340 | 1340 |
try { |
1341 | 1341 |
for (int i = 0; i < int(nodes.size()); ++i) { |
1342 | 1342 |
_arcset.eraseNode(nodes[i]); |
1343 | 1343 |
} |
1344 | 1344 |
Parent::erase(nodes); |
1345 | 1345 |
} catch (const typename NodesImplBase::Notifier::ImmediateDetach&) { |
1346 | 1346 |
Parent::clear(); |
1347 | 1347 |
throw; |
1348 | 1348 |
} |
1349 | 1349 |
} |
1350 | 1350 |
virtual void clear() { |
1351 | 1351 |
_arcset.clearNodes(); |
1352 | 1352 |
Parent::clear(); |
1353 | 1353 |
} |
1354 | 1354 |
|
1355 | 1355 |
private: |
1356 | 1356 |
SmartEdgeSet& _arcset; |
1357 | 1357 |
}; |
1358 | 1358 |
|
1359 | 1359 |
NodesImpl _nodes; |
1360 | 1360 |
|
1361 | 1361 |
public: |
1362 | 1362 |
|
1363 | 1363 |
/// \brief Constructor of the EdgeSet. |
1364 | 1364 |
/// |
1365 | 1365 |
/// Constructor of the EdgeSet. |
1366 | 1366 |
SmartEdgeSet(const GR& graph) : _nodes(graph, *this) { |
1367 | 1367 |
Parent::initalize(graph, _nodes); |
1368 | 1368 |
} |
1369 | 1369 |
|
1370 | 1370 |
/// \brief Add a new edge to the graph. |
1371 | 1371 |
/// |
1372 | 1372 |
/// Add a new edge to the graph with node \c u |
1373 | 1373 |
/// and node \c v endpoints. |
1374 | 1374 |
/// \return The new edge. |
1375 | 1375 |
Edge addEdge(const Node& u, const Node& v) { |
1376 | 1376 |
return Parent::addEdge(u, v); |
1377 | 1377 |
} |
1378 | 1378 |
|
1379 | 1379 |
/// \brief Validity check |
1380 | 1380 |
/// |
1381 | 1381 |
/// This functions gives back false if the EdgeSet is |
1382 | 1382 |
/// invalidated. It occurs when a node in the underlying graph is |
1383 | 1383 |
/// erased and it is not isolated in the EdgeSet. |
1384 | 1384 |
bool valid() const { |
1385 | 1385 |
return _nodes.attached(); |
1386 | 1386 |
} |
1387 | 1387 |
|
1388 | 1388 |
}; |
1389 | 1389 |
|
1390 | 1390 |
} |
1391 | 1391 |
|
1392 | 1392 |
#endif |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2009 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_BITS_BASE_EXTENDER_H |
|
20 |
#define LEMON_BITS_BASE_EXTENDER_H |
|
21 |
|
|
22 |
#include <lemon/core.h> |
|
23 |
#include <lemon/error.h> |
|
24 |
|
|
25 |
#include <lemon/bits/map_extender.h> |
|
26 |
#include <lemon/bits/default_map.h> |
|
27 |
|
|
28 |
#include <lemon/concept_check.h> |
|
29 |
#include <lemon/concepts/maps.h> |
|
30 |
|
|
31 |
//\ingroup digraphbits |
|
32 |
//\file |
|
33 |
//\brief Extenders for the graph types |
|
34 |
namespace lemon { |
|
35 |
|
|
36 |
// \ingroup digraphbits |
|
37 |
// |
|
38 |
// \brief BaseDigraph to BaseGraph extender |
|
39 |
template <typename Base> |
|
40 |
class UndirDigraphExtender : public Base { |
|
41 |
typedef Base Parent; |
|
42 |
|
|
43 |
public: |
|
44 |
|
|
45 |
typedef typename Parent::Arc Edge; |
|
46 |
typedef typename Parent::Node Node; |
|
47 |
|
|
48 |
typedef True UndirectedTag; |
|
49 |
|
|
50 |
class Arc : public Edge { |
|
51 |
friend class UndirDigraphExtender; |
|
52 |
|
|
53 |
protected: |
|
54 |
bool forward; |
|
55 |
|
|
56 |
Arc(const Edge &ue, bool _forward) : |
|
57 |
Edge(ue), forward(_forward) {} |
|
58 |
|
|
59 |
public: |
|
60 |
Arc() {} |
|
61 |
|
|
62 |
// Invalid arc constructor |
|
63 |
Arc(Invalid i) : Edge(i), forward(true) {} |
|
64 |
|
|
65 |
bool operator==(const Arc &that) const { |
|
66 |
return forward==that.forward && Edge(*this)==Edge(that); |
|
67 |
} |
|
68 |
bool operator!=(const Arc &that) const { |
|
69 |
return forward!=that.forward || Edge(*this)!=Edge(that); |
|
70 |
} |
|
71 |
bool operator<(const Arc &that) const { |
|
72 |
return forward<that.forward || |
|
73 |
(!(that.forward<forward) && Edge(*this)<Edge(that)); |
|
74 |
} |
|
75 |
}; |
|
76 |
|
|
77 |
// First node of the edge |
|
78 |
Node u(const Edge &e) const { |
|
79 |
return Parent::source(e); |
|
80 |
} |
|
81 |
|
|
82 |
// Source of the given arc |
|
83 |
Node source(const Arc &e) const { |
|
84 |
return e.forward ? Parent::source(e) : Parent::target(e); |
|
85 |
} |
|
86 |
|
|
87 |
// Second node of the edge |
|
88 |
Node v(const Edge &e) const { |
|
89 |
return Parent::target(e); |
|
90 |
} |
|
91 |
|
|
92 |
// Target of the given arc |
|
93 |
Node target(const Arc &e) const { |
|
94 |
return e.forward ? Parent::target(e) : Parent::source(e); |
|
95 |
} |
|
96 |
|
|
97 |
// \brief Directed arc from an edge. |
|
98 |
// |
|
99 |
// Returns a directed arc corresponding to the specified edge. |
|
100 |
// If the given bool is true, the first node of the given edge and |
|
101 |
// the source node of the returned arc are the same. |
|
102 |
static Arc direct(const Edge &e, bool d) { |
|
103 |
return Arc(e, d); |
|
104 |
} |
|
105 |
|
|
106 |
// Returns whether the given directed arc has the same orientation |
|
107 |
// as the corresponding edge. |
|
108 |
static bool direction(const Arc &a) { return a.forward; } |
|
109 |
|
|
110 |
using Parent::first; |
|
111 |
using Parent::next; |
|
112 |
|
|
113 |
void first(Arc &e) const { |
|
114 |
Parent::first(e); |
|
115 |
e.forward=true; |
|
116 |
} |
|
117 |
|
|
118 |
void next(Arc &e) const { |
|
119 |
if( e.forward ) { |
|
120 |
e.forward = false; |
|
121 |
} |
|
122 |
else { |
|
123 |
Parent::next(e); |
|
124 |
e.forward = true; |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
void firstOut(Arc &e, const Node &n) const { |
|
129 |
Parent::firstIn(e,n); |
|
130 |
if( Edge(e) != INVALID ) { |
|
131 |
e.forward = false; |
|
132 |
} |
|
133 |
else { |
|
134 |
Parent::firstOut(e,n); |
|
135 |
e.forward = true; |
|
136 |
} |
|
137 |
} |
|
138 |
void nextOut(Arc &e) const { |
|
139 |
if( ! e.forward ) { |
|
140 |
Node n = Parent::target(e); |
|
141 |
Parent::nextIn(e); |
|
142 |
if( Edge(e) == INVALID ) { |
|
143 |
Parent::firstOut(e, n); |
|
144 |
e.forward = true; |
|
145 |
} |
|
146 |
} |
|
147 |
else { |
|
148 |
Parent::nextOut(e); |
|
149 |
} |
|
150 |
} |
|
151 |
|
|
152 |
void firstIn(Arc &e, const Node &n) const { |
|
153 |
Parent::firstOut(e,n); |
|
154 |
if( Edge(e) != INVALID ) { |
|
155 |
e.forward = false; |
|
156 |
} |
|
157 |
else { |
|
158 |
Parent::firstIn(e,n); |
|
159 |
e.forward = true; |
|
160 |
} |
|
161 |
} |
|
162 |
void nextIn(Arc &e) const { |
|
163 |
if( ! e.forward ) { |
|
164 |
Node n = Parent::source(e); |
|
165 |
Parent::nextOut(e); |
|
166 |
if( Edge(e) == INVALID ) { |
|
167 |
Parent::firstIn(e, n); |
|
168 |
e.forward = true; |
|
169 |
} |
|
170 |
} |
|
171 |
else { |
|
172 |
Parent::nextIn(e); |
|
173 |
} |
|
174 |
} |
|
175 |
|
|
176 |
void firstInc(Edge &e, bool &d, const Node &n) const { |
|
177 |
d = true; |
|
178 |
Parent::firstOut(e, n); |
|
179 |
if (e != INVALID) return; |
|
180 |
d = false; |
|
181 |
Parent::firstIn(e, n); |
|
182 |
} |
|
183 |
|
|
184 |
void nextInc(Edge &e, bool &d) const { |
|
185 |
if (d) { |
|
186 |
Node s = Parent::source(e); |
|
187 |
Parent::nextOut(e); |
|
188 |
if (e != INVALID) return; |
|
189 |
d = false; |
|
190 |
Parent::firstIn(e, s); |
|
191 |
} else { |
|
192 |
Parent::nextIn(e); |
|
193 |
} |
|
194 |
} |
|
195 |
|
|
196 |
Node nodeFromId(int ix) const { |
|
197 |
return Parent::nodeFromId(ix); |
|
198 |
} |
|
199 |
|
|
200 |
Arc arcFromId(int ix) const { |
|
201 |
return direct(Parent::arcFromId(ix >> 1), bool(ix & 1)); |
|
202 |
} |
|
203 |
|
|
204 |
Edge edgeFromId(int ix) const { |
|
205 |
return Parent::arcFromId(ix); |
|
206 |
} |
|
207 |
|
|
208 |
int id(const Node &n) const { |
|
209 |
return Parent::id(n); |
|
210 |
} |
|
211 |
|
|
212 |
int id(const Edge &e) const { |
|
213 |
return Parent::id(e); |
|
214 |
} |
|
215 |
|
|
216 |
int id(const Arc &e) const { |
|
217 |
return 2 * Parent::id(e) + int(e.forward); |
|
218 |
} |
|
219 |
|
|
220 |
int maxNodeId() const { |
|
221 |
return Parent::maxNodeId(); |
|
222 |
} |
|
223 |
|
|
224 |
int maxArcId() const { |
|
225 |
return 2 * Parent::maxArcId() + 1; |
|
226 |
} |
|
227 |
|
|
228 |
int maxEdgeId() const { |
|
229 |
return Parent::maxArcId(); |
|
230 |
} |
|
231 |
|
|
232 |
int arcNum() const { |
|
233 |
return 2 * Parent::arcNum(); |
|
234 |
} |
|
235 |
|
|
236 |
int edgeNum() const { |
|
237 |
return Parent::arcNum(); |
|
238 |
} |
|
239 |
|
|
240 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
|
241 |
if (p == INVALID) { |
|
242 |
Edge arc = Parent::findArc(s, t); |
|
243 |
if (arc != INVALID) return direct(arc, true); |
|
244 |
arc = Parent::findArc(t, s); |
|
245 |
if (arc != INVALID) return direct(arc, false); |
|
246 |
} else if (direction(p)) { |
|
247 |
Edge arc = Parent::findArc(s, t, p); |
|
248 |
if (arc != INVALID) return direct(arc, true); |
|
249 |
arc = Parent::findArc(t, s); |
|
250 |
if (arc != INVALID) return direct(arc, false); |
|
251 |
} else { |
|
252 |
Edge arc = Parent::findArc(t, s, p); |
|
253 |
if (arc != INVALID) return direct(arc, false); |
|
254 |
} |
|
255 |
return INVALID; |
|
256 |
} |
|
257 |
|
|
258 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
|
259 |
if (s != t) { |
|
260 |
if (p == INVALID) { |
|
261 |
Edge arc = Parent::findArc(s, t); |
|
262 |
if (arc != INVALID) return arc; |
|
263 |
arc = Parent::findArc(t, s); |
|
264 |
if (arc != INVALID) return arc; |
|
265 |
} else if (Parent::s(p) == s) { |
|
266 |
Edge arc = Parent::findArc(s, t, p); |
|
267 |
if (arc != INVALID) return arc; |
|
268 |
arc = Parent::findArc(t, s); |
|
269 |
if (arc != INVALID) return arc; |
|
270 |
} else { |
|
271 |
Edge arc = Parent::findArc(t, s, p); |
|
272 |
if (arc != INVALID) return arc; |
|
273 |
} |
|
274 |
} else { |
|
275 |
return Parent::findArc(s, t, p); |
|
276 |
} |
|
277 |
return INVALID; |
|
278 |
} |
|
279 |
}; |
|
280 |
|
|
281 |
template <typename Base> |
|
282 |
class BidirBpGraphExtender : public Base { |
|
283 |
typedef Base Parent; |
|
284 |
|
|
285 |
public: |
|
286 |
typedef BidirBpGraphExtender Digraph; |
|
287 |
|
|
288 |
typedef typename Parent::Node Node; |
|
289 |
typedef typename Parent::Edge Edge; |
|
290 |
|
|
291 |
|
|
292 |
using Parent::first; |
|
293 |
using Parent::next; |
|
294 |
|
|
295 |
using Parent::id; |
|
296 |
|
|
297 |
class Red : public Node { |
|
298 |
friend class BidirBpGraphExtender; |
|
299 |
public: |
|
300 |
Red() {} |
|
301 |
Red(const Node& node) : Node(node) { |
|
302 |
LEMON_DEBUG(Parent::red(node) || node == INVALID, |
|
303 |
typename Parent::NodeSetError()); |
|
304 |
} |
|
305 |
Red& operator=(const Node& node) { |
|
306 |
LEMON_DEBUG(Parent::red(node) || node == INVALID, |
|
307 |
typename Parent::NodeSetError()); |
|
308 |
Node::operator=(node); |
|
309 |
return *this; |
|
310 |
} |
|
311 |
Red(Invalid) : Node(INVALID) {} |
|
312 |
Red& operator=(Invalid) { |
|
313 |
Node::operator=(INVALID); |
|
314 |
return *this; |
|
315 |
} |
|
316 |
}; |
|
317 |
|
|
318 |
void first(Red& node) const { |
|
319 |
Parent::firstRed(static_cast<Node&>(node)); |
|
320 |
} |
|
321 |
void next(Red& node) const { |
|
322 |
Parent::nextRed(static_cast<Node&>(node)); |
|
323 |
} |
|
324 |
|
|
325 |
int id(const Red& node) const { |
|
326 |
return Parent::redId(node); |
|
327 |
} |
|
328 |
|
|
329 |
class Blue : public Node { |
|
330 |
friend class BidirBpGraphExtender; |
|
331 |
public: |
|
332 |
Blue() {} |
|
333 |
Blue(const Node& node) : Node(node) { |
|
334 |
LEMON_DEBUG(Parent::blue(node) || node == INVALID, |
|
335 |
typename Parent::NodeSetError()); |
|
336 |
} |
|
337 |
Blue& operator=(const Node& node) { |
|
338 |
LEMON_DEBUG(Parent::blue(node) || node == INVALID, |
|
339 |
typename Parent::NodeSetError()); |
|
340 |
Node::operator=(node); |
|
341 |
return *this; |
|
342 |
} |
|
343 |
Blue(Invalid) : Node(INVALID) {} |
|
344 |
Blue& operator=(Invalid) { |
|
345 |
Node::operator=(INVALID); |
|
346 |
return *this; |
|
347 |
} |
|
348 |
}; |
|
349 |
|
|
350 |
void first(Blue& node) const { |
|
351 |
Parent::firstBlue(static_cast<Node&>(node)); |
|
352 |
} |
|
353 |
void next(Blue& node) const { |
|
354 |
Parent::nextBlue(static_cast<Node&>(node)); |
|
355 |
} |
|
356 |
|
|
357 |
int id(const Blue& node) const { |
|
358 |
return Parent::redId(node); |
|
359 |
} |
|
360 |
|
|
361 |
Node source(const Edge& arc) const { |
|
362 |
return red(arc); |
|
363 |
} |
|
364 |
Node target(const Edge& arc) const { |
|
365 |
return blue(arc); |
|
366 |
} |
|
367 |
|
|
368 |
void firstInc(Edge& arc, bool& dir, const Node& node) const { |
|
369 |
if (Parent::red(node)) { |
|
370 |
Parent::firstFromRed(arc, node); |
|
371 |
dir = true; |
|
372 |
} else { |
|
373 |
Parent::firstFromBlue(arc, node); |
|
374 |
dir = static_cast<Edge&>(arc) == INVALID; |
|
375 |
} |
|
376 |
} |
|
377 |
void nextInc(Edge& arc, bool& dir) const { |
|
378 |
if (dir) { |
|
379 |
Parent::nextFromRed(arc); |
|
380 |
} else { |
|
381 |
Parent::nextFromBlue(arc); |
|
382 |
if (arc == INVALID) dir = true; |
|
383 |
} |
|
384 |
} |
|
385 |
|
|
386 |
class Arc : public Edge { |
|
387 |
friend class BidirBpGraphExtender; |
|
388 |
protected: |
|
389 |
bool forward; |
|
390 |
|
|
391 |
Arc(const Edge& arc, bool _forward) |
|
392 |
: Edge(arc), forward(_forward) {} |
|
393 |
|
|
394 |
public: |
|
395 |
Arc() {} |
|
396 |
Arc (Invalid) : Edge(INVALID), forward(true) {} |
|
397 |
bool operator==(const Arc& i) const { |
|
398 |
return Edge::operator==(i) && forward == i.forward; |
|
399 |
} |
|
400 |
bool operator!=(const Arc& i) const { |
|
401 |
return Edge::operator!=(i) || forward != i.forward; |
|
402 |
} |
|
403 |
bool operator<(const Arc& i) const { |
|
404 |
return Edge::operator<(i) || |
|
405 |
(!(i.forward<forward) && Edge(*this)<Edge(i)); |
|
406 |
} |
|
407 |
}; |
|
408 |
|
|
409 |
void first(Arc& arc) const { |
|
410 |
Parent::first(static_cast<Edge&>(arc)); |
|
411 |
arc.forward = true; |
|
412 |
} |
|
413 |
|
|
414 |
void next(Arc& arc) const { |
|
415 |
if (!arc.forward) { |
|
416 |
Parent::next(static_cast<Edge&>(arc)); |
|
417 |
} |
|
418 |
arc.forward = !arc.forward; |
|
419 |
} |
|
420 |
|
|
421 |
void firstOut(Arc& arc, const Node& node) const { |
|
422 |
if (Parent::red(node)) { |
|
423 |
Parent::firstFromRed(arc, node); |
|
424 |
arc.forward = true; |
|
425 |
} else { |
|
426 |
Parent::firstFromBlue(arc, node); |
|
427 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
|
428 |
} |
|
429 |
} |
|
430 |
void nextOut(Arc& arc) const { |
|
431 |
if (arc.forward) { |
|
432 |
Parent::nextFromRed(arc); |
|
433 |
} else { |
|
434 |
Parent::nextFromBlue(arc); |
|
435 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
|
436 |
} |
|
437 |
} |
|
438 |
|
|
439 |
void firstIn(Arc& arc, const Node& node) const { |
|
440 |
if (Parent::blue(node)) { |
|
441 |
Parent::firstFromBlue(arc, node); |
|
442 |
arc.forward = true; |
|
443 |
} else { |
|
444 |
Parent::firstFromRed(arc, node); |
|
445 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
|
446 |
} |
|
447 |
} |
|
448 |
void nextIn(Arc& arc) const { |
|
449 |
if (arc.forward) { |
|
450 |
Parent::nextFromBlue(arc); |
|
451 |
} else { |
|
452 |
Parent::nextFromRed(arc); |
|
453 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
|
454 |
} |
|
455 |
} |
|
456 |
|
|
457 |
Node source(const Arc& arc) const { |
|
458 |
return arc.forward ? Parent::red(arc) : Parent::blue(arc); |
|
459 |
} |
|
460 |
Node target(const Arc& arc) const { |
|
461 |
return arc.forward ? Parent::blue(arc) : Parent::red(arc); |
|
462 |
} |
|
463 |
|
|
464 |
int id(const Arc& arc) const { |
|
465 |
return (Parent::id(static_cast<const Edge&>(arc)) << 1) + |
|
466 |
(arc.forward ? 0 : 1); |
|
467 |
} |
|
468 |
Arc arcFromId(int ix) const { |
|
469 |
return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0); |
|
470 |
} |
|
471 |
int maxArcId() const { |
|
472 |
return (Parent::maxEdgeId() << 1) + 1; |
|
473 |
} |
|
474 |
|
|
475 |
bool direction(const Arc& arc) const { |
|
476 |
return arc.forward; |
|
477 |
} |
|
478 |
|
|
479 |
Arc direct(const Edge& arc, bool dir) const { |
|
480 |
return Arc(arc, dir); |
|
481 |
} |
|
482 |
|
|
483 |
int arcNum() const { |
|
484 |
return 2 * Parent::edgeNum(); |
|
485 |
} |
|
486 |
|
|
487 |
int edgeNum() const { |
|
488 |
return Parent::edgeNum(); |
|
489 |
} |
|
490 |
|
|
491 |
|
|
492 |
}; |
|
493 |
} |
|
494 |
|
|
495 |
#endif |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)