0
6
0
| ... | ... |
@@ -11,101 +11,101 @@ |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_GRAPH_EXTENDER_H |
| 20 | 20 |
#define LEMON_BITS_GRAPH_EXTENDER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/bits/map_extender.h> |
| 25 | 25 |
#include <lemon/bits/default_map.h> |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/concept_check.h> |
| 28 | 28 |
#include <lemon/concepts/maps.h> |
| 29 | 29 |
|
| 30 | 30 |
//\ingroup graphbits |
| 31 | 31 |
//\file |
| 32 | 32 |
//\brief Extenders for the graph types |
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
// \ingroup graphbits |
| 36 | 36 |
// |
| 37 | 37 |
// \brief Extender for the digraph implementations |
| 38 | 38 |
template <typename Base> |
| 39 | 39 |
class DigraphExtender : public Base {
|
| 40 | 40 |
typedef Base Parent; |
| 41 | 41 |
|
| 42 | 42 |
public: |
| 43 | 43 |
|
| 44 | 44 |
typedef DigraphExtender Digraph; |
| 45 | 45 |
|
| 46 | 46 |
// Base extensions |
| 47 | 47 |
|
| 48 | 48 |
typedef typename Parent::Node Node; |
| 49 | 49 |
typedef typename Parent::Arc Arc; |
| 50 | 50 |
|
| 51 | 51 |
int maxId(Node) const {
|
| 52 | 52 |
return Parent::maxNodeId(); |
| 53 | 53 |
} |
| 54 | 54 |
|
| 55 | 55 |
int maxId(Arc) const {
|
| 56 | 56 |
return Parent::maxArcId(); |
| 57 | 57 |
} |
| 58 | 58 |
|
| 59 |
Node fromId(int id, Node) |
|
| 59 |
static Node fromId(int id, Node) {
|
|
| 60 | 60 |
return Parent::nodeFromId(id); |
| 61 | 61 |
} |
| 62 | 62 |
|
| 63 |
Arc fromId(int id, Arc) |
|
| 63 |
static Arc fromId(int id, Arc) {
|
|
| 64 | 64 |
return Parent::arcFromId(id); |
| 65 | 65 |
} |
| 66 | 66 |
|
| 67 | 67 |
Node oppositeNode(const Node &node, const Arc &arc) const {
|
| 68 | 68 |
if (node == Parent::source(arc)) |
| 69 | 69 |
return Parent::target(arc); |
| 70 | 70 |
else if(node == Parent::target(arc)) |
| 71 | 71 |
return Parent::source(arc); |
| 72 | 72 |
else |
| 73 | 73 |
return INVALID; |
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 | 76 |
// Alterable extension |
| 77 | 77 |
|
| 78 | 78 |
typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier; |
| 79 | 79 |
typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier; |
| 80 | 80 |
|
| 81 | 81 |
|
| 82 | 82 |
protected: |
| 83 | 83 |
|
| 84 | 84 |
mutable NodeNotifier node_notifier; |
| 85 | 85 |
mutable ArcNotifier arc_notifier; |
| 86 | 86 |
|
| 87 | 87 |
public: |
| 88 | 88 |
|
| 89 | 89 |
NodeNotifier& notifier(Node) const {
|
| 90 | 90 |
return node_notifier; |
| 91 | 91 |
} |
| 92 | 92 |
|
| 93 | 93 |
ArcNotifier& notifier(Arc) const {
|
| 94 | 94 |
return arc_notifier; |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 | 97 |
class NodeIt : public Node {
|
| 98 | 98 |
const Digraph* _digraph; |
| 99 | 99 |
public: |
| 100 | 100 |
|
| 101 | 101 |
NodeIt() {}
|
| 102 | 102 |
|
| 103 | 103 |
NodeIt(Invalid i) : Node(i) { }
|
| 104 | 104 |
|
| 105 | 105 |
explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) {
|
| 106 | 106 |
_digraph->first(static_cast<Node&>(*this)); |
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 | 109 |
NodeIt(const Digraph& digraph, const Node& node) |
| 110 | 110 |
: Node(node), _digraph(&digraph) {}
|
| 111 | 111 |
|
| ... | ... |
@@ -310,105 +310,105 @@ |
| 310 | 310 |
void erase(const Arc& arc) {
|
| 311 | 311 |
notifier(Arc()).erase(arc); |
| 312 | 312 |
Parent::erase(arc); |
| 313 | 313 |
} |
| 314 | 314 |
|
| 315 | 315 |
DigraphExtender() {
|
| 316 | 316 |
node_notifier.setContainer(*this); |
| 317 | 317 |
arc_notifier.setContainer(*this); |
| 318 | 318 |
} |
| 319 | 319 |
|
| 320 | 320 |
|
| 321 | 321 |
~DigraphExtender() {
|
| 322 | 322 |
arc_notifier.clear(); |
| 323 | 323 |
node_notifier.clear(); |
| 324 | 324 |
} |
| 325 | 325 |
}; |
| 326 | 326 |
|
| 327 | 327 |
// \ingroup _graphbits |
| 328 | 328 |
// |
| 329 | 329 |
// \brief Extender for the Graphs |
| 330 | 330 |
template <typename Base> |
| 331 | 331 |
class GraphExtender : public Base {
|
| 332 | 332 |
typedef Base Parent; |
| 333 | 333 |
|
| 334 | 334 |
public: |
| 335 | 335 |
|
| 336 | 336 |
typedef GraphExtender Graph; |
| 337 | 337 |
|
| 338 | 338 |
typedef True UndirectedTag; |
| 339 | 339 |
|
| 340 | 340 |
typedef typename Parent::Node Node; |
| 341 | 341 |
typedef typename Parent::Arc Arc; |
| 342 | 342 |
typedef typename Parent::Edge Edge; |
| 343 | 343 |
|
| 344 | 344 |
// Graph extension |
| 345 | 345 |
|
| 346 | 346 |
int maxId(Node) const {
|
| 347 | 347 |
return Parent::maxNodeId(); |
| 348 | 348 |
} |
| 349 | 349 |
|
| 350 | 350 |
int maxId(Arc) const {
|
| 351 | 351 |
return Parent::maxArcId(); |
| 352 | 352 |
} |
| 353 | 353 |
|
| 354 | 354 |
int maxId(Edge) const {
|
| 355 | 355 |
return Parent::maxEdgeId(); |
| 356 | 356 |
} |
| 357 | 357 |
|
| 358 |
Node fromId(int id, Node) |
|
| 358 |
static Node fromId(int id, Node) {
|
|
| 359 | 359 |
return Parent::nodeFromId(id); |
| 360 | 360 |
} |
| 361 | 361 |
|
| 362 |
Arc fromId(int id, Arc) |
|
| 362 |
static Arc fromId(int id, Arc) {
|
|
| 363 | 363 |
return Parent::arcFromId(id); |
| 364 | 364 |
} |
| 365 | 365 |
|
| 366 |
Edge fromId(int id, Edge) |
|
| 366 |
static Edge fromId(int id, Edge) {
|
|
| 367 | 367 |
return Parent::edgeFromId(id); |
| 368 | 368 |
} |
| 369 | 369 |
|
| 370 | 370 |
Node oppositeNode(const Node &n, const Edge &e) const {
|
| 371 | 371 |
if( n == Parent::u(e)) |
| 372 | 372 |
return Parent::v(e); |
| 373 | 373 |
else if( n == Parent::v(e)) |
| 374 | 374 |
return Parent::u(e); |
| 375 | 375 |
else |
| 376 | 376 |
return INVALID; |
| 377 | 377 |
} |
| 378 | 378 |
|
| 379 | 379 |
Arc oppositeArc(const Arc &arc) const {
|
| 380 | 380 |
return Parent::direct(arc, !Parent::direction(arc)); |
| 381 | 381 |
} |
| 382 | 382 |
|
| 383 | 383 |
using Parent::direct; |
| 384 | 384 |
Arc direct(const Edge &edge, const Node &node) const {
|
| 385 | 385 |
return Parent::direct(edge, Parent::u(edge) == node); |
| 386 | 386 |
} |
| 387 | 387 |
|
| 388 | 388 |
// Alterable extension |
| 389 | 389 |
|
| 390 | 390 |
typedef AlterationNotifier<GraphExtender, Node> NodeNotifier; |
| 391 | 391 |
typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier; |
| 392 | 392 |
typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier; |
| 393 | 393 |
|
| 394 | 394 |
|
| 395 | 395 |
protected: |
| 396 | 396 |
|
| 397 | 397 |
mutable NodeNotifier node_notifier; |
| 398 | 398 |
mutable ArcNotifier arc_notifier; |
| 399 | 399 |
mutable EdgeNotifier edge_notifier; |
| 400 | 400 |
|
| 401 | 401 |
public: |
| 402 | 402 |
|
| 403 | 403 |
NodeNotifier& notifier(Node) const {
|
| 404 | 404 |
return node_notifier; |
| 405 | 405 |
} |
| 406 | 406 |
|
| 407 | 407 |
ArcNotifier& notifier(Arc) const {
|
| 408 | 408 |
return arc_notifier; |
| 409 | 409 |
} |
| 410 | 410 |
|
| 411 | 411 |
EdgeNotifier& notifier(Edge) const {
|
| 412 | 412 |
return edge_notifier; |
| 413 | 413 |
} |
| 414 | 414 |
| ... | ... |
@@ -822,97 +822,97 @@ |
| 822 | 822 |
Arc() {}
|
| 823 | 823 |
Arc(Invalid) : id(-1) {}
|
| 824 | 824 |
bool operator==(const Arc& arc) const { return id == arc.id; }
|
| 825 | 825 |
bool operator!=(const Arc& arc) const { return id != arc.id; }
|
| 826 | 826 |
bool operator<(const Arc& arc) const { return id < arc.id; }
|
| 827 | 827 |
}; |
| 828 | 828 |
|
| 829 | 829 |
SmartArcSetBase() {}
|
| 830 | 830 |
|
| 831 | 831 |
Node addNode() {
|
| 832 | 832 |
LEMON_ASSERT(false, |
| 833 | 833 |
"This graph structure does not support node insertion"); |
| 834 | 834 |
return INVALID; // avoid warning |
| 835 | 835 |
} |
| 836 | 836 |
|
| 837 | 837 |
Arc addArc(const Node& u, const Node& v) {
|
| 838 | 838 |
int n = arcs.size(); |
| 839 | 839 |
arcs.push_back(ArcT()); |
| 840 | 840 |
arcs[n].next_in = (*_nodes)[v].first_in; |
| 841 | 841 |
(*_nodes)[v].first_in = n; |
| 842 | 842 |
arcs[n].next_out = (*_nodes)[u].first_out; |
| 843 | 843 |
(*_nodes)[u].first_out = n; |
| 844 | 844 |
arcs[n].source = u; |
| 845 | 845 |
arcs[n].target = v; |
| 846 | 846 |
return Arc(n); |
| 847 | 847 |
} |
| 848 | 848 |
|
| 849 | 849 |
void clear() {
|
| 850 | 850 |
Node node; |
| 851 | 851 |
for (first(node); node != INVALID; next(node)) {
|
| 852 | 852 |
(*_nodes)[node].first_in = -1; |
| 853 | 853 |
(*_nodes)[node].first_out = -1; |
| 854 | 854 |
} |
| 855 | 855 |
arcs.clear(); |
| 856 | 856 |
} |
| 857 | 857 |
|
| 858 | 858 |
void first(Node& node) const {
|
| 859 | 859 |
_graph->first(node); |
| 860 | 860 |
} |
| 861 | 861 |
|
| 862 | 862 |
void next(Node& node) const {
|
| 863 | 863 |
_graph->next(node); |
| 864 | 864 |
} |
| 865 | 865 |
|
| 866 | 866 |
void first(Arc& arc) const {
|
| 867 | 867 |
arc.id = arcs.size() - 1; |
| 868 | 868 |
} |
| 869 | 869 |
|
| 870 |
void next(Arc& arc) |
|
| 870 |
static void next(Arc& arc) {
|
|
| 871 | 871 |
--arc.id; |
| 872 | 872 |
} |
| 873 | 873 |
|
| 874 | 874 |
void firstOut(Arc& arc, const Node& node) const {
|
| 875 | 875 |
arc.id = (*_nodes)[node].first_out; |
| 876 | 876 |
} |
| 877 | 877 |
|
| 878 | 878 |
void nextOut(Arc& arc) const {
|
| 879 | 879 |
arc.id = arcs[arc.id].next_out; |
| 880 | 880 |
} |
| 881 | 881 |
|
| 882 | 882 |
void firstIn(Arc& arc, const Node& node) const {
|
| 883 | 883 |
arc.id = (*_nodes)[node].first_in; |
| 884 | 884 |
} |
| 885 | 885 |
|
| 886 | 886 |
void nextIn(Arc& arc) const {
|
| 887 | 887 |
arc.id = arcs[arc.id].next_in; |
| 888 | 888 |
} |
| 889 | 889 |
|
| 890 | 890 |
int id(const Node& node) const { return _graph->id(node); }
|
| 891 | 891 |
int id(const Arc& arc) const { return arc.id; }
|
| 892 | 892 |
|
| 893 | 893 |
Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
|
| 894 | 894 |
Arc arcFromId(int ix) const { return Arc(ix); }
|
| 895 | 895 |
|
| 896 | 896 |
int maxNodeId() const { return _graph->maxNodeId(); };
|
| 897 | 897 |
int maxArcId() const { return arcs.size() - 1; }
|
| 898 | 898 |
|
| 899 | 899 |
Node source(const Arc& arc) const { return arcs[arc.id].source;}
|
| 900 | 900 |
Node target(const Arc& arc) const { return arcs[arc.id].target;}
|
| 901 | 901 |
|
| 902 | 902 |
typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
| 903 | 903 |
|
| 904 | 904 |
NodeNotifier& notifier(Node) const {
|
| 905 | 905 |
return _graph->notifier(Node()); |
| 906 | 906 |
} |
| 907 | 907 |
|
| 908 | 908 |
template <typename V> |
| 909 | 909 |
class NodeMap : public GR::template NodeMap<V> {
|
| 910 | 910 |
typedef typename GR::template NodeMap<V> Parent; |
| 911 | 911 |
|
| 912 | 912 |
public: |
| 913 | 913 |
|
| 914 | 914 |
explicit NodeMap(const SmartArcSetBase<GR>& arcset) |
| 915 | 915 |
: Parent(*arcset._graph) { }
|
| 916 | 916 |
|
| 917 | 917 |
NodeMap(const SmartArcSetBase<GR>& arcset, const V& value) |
| 918 | 918 |
: Parent(*arcset._graph, value) { }
|
| ... | ... |
@@ -1128,105 +1128,105 @@ |
| 1128 | 1128 |
bool operator<(const Arc& arc) const { return id < arc.id; }
|
| 1129 | 1129 |
}; |
| 1130 | 1130 |
|
| 1131 | 1131 |
SmartEdgeSetBase() {}
|
| 1132 | 1132 |
|
| 1133 | 1133 |
Node addNode() {
|
| 1134 | 1134 |
LEMON_ASSERT(false, |
| 1135 | 1135 |
"This graph structure does not support node insertion"); |
| 1136 | 1136 |
return INVALID; // avoid warning |
| 1137 | 1137 |
} |
| 1138 | 1138 |
|
| 1139 | 1139 |
Edge addEdge(const Node& u, const Node& v) {
|
| 1140 | 1140 |
int n = arcs.size(); |
| 1141 | 1141 |
arcs.push_back(ArcT()); |
| 1142 | 1142 |
arcs.push_back(ArcT()); |
| 1143 | 1143 |
|
| 1144 | 1144 |
arcs[n].target = u; |
| 1145 | 1145 |
arcs[n | 1].target = v; |
| 1146 | 1146 |
|
| 1147 | 1147 |
arcs[n].next_out = (*_nodes)[v].first_out; |
| 1148 | 1148 |
(*_nodes)[v].first_out = n; |
| 1149 | 1149 |
|
| 1150 | 1150 |
arcs[n | 1].next_out = (*_nodes)[u].first_out; |
| 1151 | 1151 |
(*_nodes)[u].first_out = (n | 1); |
| 1152 | 1152 |
|
| 1153 | 1153 |
return Edge(n / 2); |
| 1154 | 1154 |
} |
| 1155 | 1155 |
|
| 1156 | 1156 |
void clear() {
|
| 1157 | 1157 |
Node node; |
| 1158 | 1158 |
for (first(node); node != INVALID; next(node)) {
|
| 1159 | 1159 |
(*_nodes)[node].first_out = -1; |
| 1160 | 1160 |
} |
| 1161 | 1161 |
arcs.clear(); |
| 1162 | 1162 |
} |
| 1163 | 1163 |
|
| 1164 | 1164 |
void first(Node& node) const {
|
| 1165 | 1165 |
_graph->first(node); |
| 1166 | 1166 |
} |
| 1167 | 1167 |
|
| 1168 | 1168 |
void next(Node& node) const {
|
| 1169 | 1169 |
_graph->next(node); |
| 1170 | 1170 |
} |
| 1171 | 1171 |
|
| 1172 | 1172 |
void first(Arc& arc) const {
|
| 1173 | 1173 |
arc.id = arcs.size() - 1; |
| 1174 | 1174 |
} |
| 1175 | 1175 |
|
| 1176 |
void next(Arc& arc) |
|
| 1176 |
static void next(Arc& arc) {
|
|
| 1177 | 1177 |
--arc.id; |
| 1178 | 1178 |
} |
| 1179 | 1179 |
|
| 1180 | 1180 |
void first(Edge& arc) const {
|
| 1181 | 1181 |
arc.id = arcs.size() / 2 - 1; |
| 1182 | 1182 |
} |
| 1183 | 1183 |
|
| 1184 |
void next(Edge& arc) |
|
| 1184 |
static void next(Edge& arc) {
|
|
| 1185 | 1185 |
--arc.id; |
| 1186 | 1186 |
} |
| 1187 | 1187 |
|
| 1188 | 1188 |
void firstOut(Arc& arc, const Node& node) const {
|
| 1189 | 1189 |
arc.id = (*_nodes)[node].first_out; |
| 1190 | 1190 |
} |
| 1191 | 1191 |
|
| 1192 | 1192 |
void nextOut(Arc& arc) const {
|
| 1193 | 1193 |
arc.id = arcs[arc.id].next_out; |
| 1194 | 1194 |
} |
| 1195 | 1195 |
|
| 1196 | 1196 |
void firstIn(Arc& arc, const Node& node) const {
|
| 1197 | 1197 |
arc.id = (((*_nodes)[node].first_out) ^ 1); |
| 1198 | 1198 |
if (arc.id == -2) arc.id = -1; |
| 1199 | 1199 |
} |
| 1200 | 1200 |
|
| 1201 | 1201 |
void nextIn(Arc& arc) const {
|
| 1202 | 1202 |
arc.id = ((arcs[arc.id ^ 1].next_out) ^ 1); |
| 1203 | 1203 |
if (arc.id == -2) arc.id = -1; |
| 1204 | 1204 |
} |
| 1205 | 1205 |
|
| 1206 | 1206 |
void firstInc(Edge &arc, bool& dir, const Node& node) const {
|
| 1207 | 1207 |
int de = (*_nodes)[node].first_out; |
| 1208 | 1208 |
if (de != -1 ) {
|
| 1209 | 1209 |
arc.id = de / 2; |
| 1210 | 1210 |
dir = ((de & 1) == 1); |
| 1211 | 1211 |
} else {
|
| 1212 | 1212 |
arc.id = -1; |
| 1213 | 1213 |
dir = true; |
| 1214 | 1214 |
} |
| 1215 | 1215 |
} |
| 1216 | 1216 |
void nextInc(Edge &arc, bool& dir) const {
|
| 1217 | 1217 |
int de = (arcs[(arc.id * 2) | (dir ? 1 : 0)].next_out); |
| 1218 | 1218 |
if (de != -1 ) {
|
| 1219 | 1219 |
arc.id = de / 2; |
| 1220 | 1220 |
dir = ((de & 1) == 1); |
| 1221 | 1221 |
} else {
|
| 1222 | 1222 |
arc.id = -1; |
| 1223 | 1223 |
dir = true; |
| 1224 | 1224 |
} |
| 1225 | 1225 |
} |
| 1226 | 1226 |
|
| 1227 | 1227 |
static bool direction(Arc arc) {
|
| 1228 | 1228 |
return (arc.id & 1) == 1; |
| 1229 | 1229 |
} |
| 1230 | 1230 |
|
| 1231 | 1231 |
static Arc direct(Edge edge, bool dir) {
|
| 1232 | 1232 |
return Arc(edge.id * 2 + (dir ? 1 : 0)); |
| ... | ... |
@@ -6,97 +6,97 @@ |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_FULL_GRAPH_H |
| 20 | 20 |
#define LEMON_FULL_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/bits/graph_extender.h> |
| 24 | 24 |
|
| 25 | 25 |
///\ingroup graphs |
| 26 | 26 |
///\file |
| 27 | 27 |
///\brief FullGraph and FullDigraph classes. |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
class FullDigraphBase {
|
| 32 | 32 |
public: |
| 33 | 33 |
|
| 34 | 34 |
typedef FullDigraphBase Digraph; |
| 35 | 35 |
|
| 36 | 36 |
class Node; |
| 37 | 37 |
class Arc; |
| 38 | 38 |
|
| 39 | 39 |
protected: |
| 40 | 40 |
|
| 41 | 41 |
int _node_num; |
| 42 | 42 |
int _arc_num; |
| 43 | 43 |
|
| 44 | 44 |
FullDigraphBase() {}
|
| 45 | 45 |
|
| 46 | 46 |
void construct(int n) { _node_num = n; _arc_num = n * n; }
|
| 47 | 47 |
|
| 48 | 48 |
public: |
| 49 | 49 |
|
| 50 | 50 |
typedef True NodeNumTag; |
| 51 | 51 |
typedef True ArcNumTag; |
| 52 | 52 |
|
| 53 | 53 |
Node operator()(int ix) const { return Node(ix); }
|
| 54 |
int index(const Node& node) |
|
| 54 |
static int index(const Node& node) { return node._id; }
|
|
| 55 | 55 |
|
| 56 | 56 |
Arc arc(const Node& s, const Node& t) const {
|
| 57 | 57 |
return Arc(s._id * _node_num + t._id); |
| 58 | 58 |
} |
| 59 | 59 |
|
| 60 | 60 |
int nodeNum() const { return _node_num; }
|
| 61 | 61 |
int arcNum() const { return _arc_num; }
|
| 62 | 62 |
|
| 63 | 63 |
int maxNodeId() const { return _node_num - 1; }
|
| 64 | 64 |
int maxArcId() const { return _arc_num - 1; }
|
| 65 | 65 |
|
| 66 | 66 |
Node source(Arc arc) const { return arc._id / _node_num; }
|
| 67 | 67 |
Node target(Arc arc) const { return arc._id % _node_num; }
|
| 68 | 68 |
|
| 69 | 69 |
static int id(Node node) { return node._id; }
|
| 70 | 70 |
static int id(Arc arc) { return arc._id; }
|
| 71 | 71 |
|
| 72 | 72 |
static Node nodeFromId(int id) { return Node(id);}
|
| 73 | 73 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 74 | 74 |
|
| 75 | 75 |
typedef True FindArcTag; |
| 76 | 76 |
|
| 77 | 77 |
Arc findArc(Node s, Node t, Arc prev = INVALID) const {
|
| 78 | 78 |
return prev == INVALID ? arc(s, t) : INVALID; |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
class Node {
|
| 82 | 82 |
friend class FullDigraphBase; |
| 83 | 83 |
|
| 84 | 84 |
protected: |
| 85 | 85 |
int _id; |
| 86 | 86 |
Node(int id) : _id(id) {}
|
| 87 | 87 |
public: |
| 88 | 88 |
Node() {}
|
| 89 | 89 |
Node (Invalid) : _id(-1) {}
|
| 90 | 90 |
bool operator==(const Node node) const {return _id == node._id;}
|
| 91 | 91 |
bool operator!=(const Node node) const {return _id != node._id;}
|
| 92 | 92 |
bool operator<(const Node node) const {return _id < node._id;}
|
| 93 | 93 |
}; |
| 94 | 94 |
|
| 95 | 95 |
class Arc {
|
| 96 | 96 |
friend class FullDigraphBase; |
| 97 | 97 |
|
| 98 | 98 |
protected: |
| 99 | 99 |
int _id; // _node_num * source + target; |
| 100 | 100 |
|
| 101 | 101 |
Arc(int id) : _id(id) {}
|
| 102 | 102 |
|
| ... | ... |
@@ -164,171 +164,171 @@ |
| 164 | 164 |
/// but there are two differences. While this class conforms only |
| 165 | 165 |
/// to the \ref concepts::Digraph "Digraph" concept, the \c FullGraph |
| 166 | 166 |
/// class conforms to the \ref concepts::Graph "Graph" concept, |
| 167 | 167 |
/// moreover \c FullGraph does not contain a loop arc for each |
| 168 | 168 |
/// node as \c FullDigraph does. |
| 169 | 169 |
/// |
| 170 | 170 |
/// \sa FullGraph |
| 171 | 171 |
class FullDigraph : public ExtendedFullDigraphBase {
|
| 172 | 172 |
typedef ExtendedFullDigraphBase Parent; |
| 173 | 173 |
|
| 174 | 174 |
public: |
| 175 | 175 |
|
| 176 | 176 |
/// \brief Constructor |
| 177 | 177 |
FullDigraph() { construct(0); }
|
| 178 | 178 |
|
| 179 | 179 |
/// \brief Constructor |
| 180 | 180 |
/// |
| 181 | 181 |
/// Constructor. |
| 182 | 182 |
/// \param n The number of the nodes. |
| 183 | 183 |
FullDigraph(int n) { construct(n); }
|
| 184 | 184 |
|
| 185 | 185 |
/// \brief Resizes the digraph |
| 186 | 186 |
/// |
| 187 | 187 |
/// Resizes the digraph. The function will fully destroy and |
| 188 | 188 |
/// rebuild the digraph. This cause that the maps of the digraph will |
| 189 | 189 |
/// reallocated automatically and the previous values will be lost. |
| 190 | 190 |
void resize(int n) {
|
| 191 | 191 |
Parent::notifier(Arc()).clear(); |
| 192 | 192 |
Parent::notifier(Node()).clear(); |
| 193 | 193 |
construct(n); |
| 194 | 194 |
Parent::notifier(Node()).build(); |
| 195 | 195 |
Parent::notifier(Arc()).build(); |
| 196 | 196 |
} |
| 197 | 197 |
|
| 198 | 198 |
/// \brief Returns the node with the given index. |
| 199 | 199 |
/// |
| 200 | 200 |
/// Returns the node with the given index. Since it is a static |
| 201 | 201 |
/// digraph its nodes can be indexed with integers from the range |
| 202 | 202 |
/// <tt>[0..nodeNum()-1]</tt>. |
| 203 | 203 |
/// \sa index() |
| 204 | 204 |
Node operator()(int ix) const { return Parent::operator()(ix); }
|
| 205 | 205 |
|
| 206 | 206 |
/// \brief Returns the index of the given node. |
| 207 | 207 |
/// |
| 208 | 208 |
/// Returns the index of the given node. Since it is a static |
| 209 | 209 |
/// digraph its nodes can be indexed with integers from the range |
| 210 | 210 |
/// <tt>[0..nodeNum()-1]</tt>. |
| 211 | 211 |
/// \sa operator() |
| 212 |
int index(const Node& node) |
|
| 212 |
static int index(const Node& node) { return Parent::index(node); }
|
|
| 213 | 213 |
|
| 214 | 214 |
/// \brief Returns the arc connecting the given nodes. |
| 215 | 215 |
/// |
| 216 | 216 |
/// Returns the arc connecting the given nodes. |
| 217 | 217 |
Arc arc(const Node& u, const Node& v) const {
|
| 218 | 218 |
return Parent::arc(u, v); |
| 219 | 219 |
} |
| 220 | 220 |
|
| 221 | 221 |
/// \brief Number of nodes. |
| 222 | 222 |
int nodeNum() const { return Parent::nodeNum(); }
|
| 223 | 223 |
/// \brief Number of arcs. |
| 224 | 224 |
int arcNum() const { return Parent::arcNum(); }
|
| 225 | 225 |
}; |
| 226 | 226 |
|
| 227 | 227 |
|
| 228 | 228 |
class FullGraphBase {
|
| 229 | 229 |
public: |
| 230 | 230 |
|
| 231 | 231 |
typedef FullGraphBase Graph; |
| 232 | 232 |
|
| 233 | 233 |
class Node; |
| 234 | 234 |
class Arc; |
| 235 | 235 |
class Edge; |
| 236 | 236 |
|
| 237 | 237 |
protected: |
| 238 | 238 |
|
| 239 | 239 |
int _node_num; |
| 240 | 240 |
int _edge_num; |
| 241 | 241 |
|
| 242 | 242 |
FullGraphBase() {}
|
| 243 | 243 |
|
| 244 | 244 |
void construct(int n) { _node_num = n; _edge_num = n * (n - 1) / 2; }
|
| 245 | 245 |
|
| 246 | 246 |
int _uid(int e) const {
|
| 247 | 247 |
int u = e / _node_num; |
| 248 | 248 |
int v = e % _node_num; |
| 249 | 249 |
return u < v ? u : _node_num - 2 - u; |
| 250 | 250 |
} |
| 251 | 251 |
|
| 252 | 252 |
int _vid(int e) const {
|
| 253 | 253 |
int u = e / _node_num; |
| 254 | 254 |
int v = e % _node_num; |
| 255 | 255 |
return u < v ? v : _node_num - 1 - v; |
| 256 | 256 |
} |
| 257 | 257 |
|
| 258 | 258 |
void _uvid(int e, int& u, int& v) const {
|
| 259 | 259 |
u = e / _node_num; |
| 260 | 260 |
v = e % _node_num; |
| 261 | 261 |
if (u >= v) {
|
| 262 | 262 |
u = _node_num - 2 - u; |
| 263 | 263 |
v = _node_num - 1 - v; |
| 264 | 264 |
} |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 | 267 |
void _stid(int a, int& s, int& t) const {
|
| 268 | 268 |
if ((a & 1) == 1) {
|
| 269 | 269 |
_uvid(a >> 1, s, t); |
| 270 | 270 |
} else {
|
| 271 | 271 |
_uvid(a >> 1, t, s); |
| 272 | 272 |
} |
| 273 | 273 |
} |
| 274 | 274 |
|
| 275 | 275 |
int _eid(int u, int v) const {
|
| 276 | 276 |
if (u < (_node_num - 1) / 2) {
|
| 277 | 277 |
return u * _node_num + v; |
| 278 | 278 |
} else {
|
| 279 | 279 |
return (_node_num - 1 - u) * _node_num - v - 1; |
| 280 | 280 |
} |
| 281 | 281 |
} |
| 282 | 282 |
|
| 283 | 283 |
public: |
| 284 | 284 |
|
| 285 | 285 |
Node operator()(int ix) const { return Node(ix); }
|
| 286 |
int index(const Node& node) |
|
| 286 |
static int index(const Node& node) { return node._id; }
|
|
| 287 | 287 |
|
| 288 | 288 |
Edge edge(const Node& u, const Node& v) const {
|
| 289 | 289 |
if (u._id < v._id) {
|
| 290 | 290 |
return Edge(_eid(u._id, v._id)); |
| 291 | 291 |
} else if (u._id != v._id) {
|
| 292 | 292 |
return Edge(_eid(v._id, u._id)); |
| 293 | 293 |
} else {
|
| 294 | 294 |
return INVALID; |
| 295 | 295 |
} |
| 296 | 296 |
} |
| 297 | 297 |
|
| 298 | 298 |
Arc arc(const Node& s, const Node& t) const {
|
| 299 | 299 |
if (s._id < t._id) {
|
| 300 | 300 |
return Arc((_eid(s._id, t._id) << 1) | 1); |
| 301 | 301 |
} else if (s._id != t._id) {
|
| 302 | 302 |
return Arc(_eid(t._id, s._id) << 1); |
| 303 | 303 |
} else {
|
| 304 | 304 |
return INVALID; |
| 305 | 305 |
} |
| 306 | 306 |
} |
| 307 | 307 |
|
| 308 | 308 |
typedef True NodeNumTag; |
| 309 | 309 |
typedef True ArcNumTag; |
| 310 | 310 |
typedef True EdgeNumTag; |
| 311 | 311 |
|
| 312 | 312 |
int nodeNum() const { return _node_num; }
|
| 313 | 313 |
int arcNum() const { return 2 * _edge_num; }
|
| 314 | 314 |
int edgeNum() const { return _edge_num; }
|
| 315 | 315 |
|
| 316 | 316 |
static int id(Node node) { return node._id; }
|
| 317 | 317 |
static int id(Arc arc) { return arc._id; }
|
| 318 | 318 |
static int id(Edge edge) { return edge._id; }
|
| 319 | 319 |
|
| 320 | 320 |
int maxNodeId() const { return _node_num-1; }
|
| 321 | 321 |
int maxArcId() const { return 2 * _edge_num-1; }
|
| 322 | 322 |
int maxEdgeId() const { return _edge_num-1; }
|
| 323 | 323 |
|
| 324 | 324 |
static Node nodeFromId(int id) { return Node(id);}
|
| 325 | 325 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 326 | 326 |
static Edge edgeFromId(int id) { return Edge(id);}
|
| 327 | 327 |
|
| 328 | 328 |
Node u(Edge edge) const {
|
| 329 | 329 |
return Node(_uid(edge._id)); |
| 330 | 330 |
} |
| 331 | 331 |
|
| 332 | 332 |
Node v(Edge edge) const {
|
| 333 | 333 |
return Node(_vid(edge._id)); |
| 334 | 334 |
} |
| ... | ... |
@@ -535,78 +535,78 @@ |
| 535 | 535 |
/// this class conforms to the \ref concepts::Graph "Graph" concept, |
| 536 | 536 |
/// moreover \c FullGraph does not contain a loop arc for each |
| 537 | 537 |
/// node as \c FullDigraph does. |
| 538 | 538 |
/// |
| 539 | 539 |
/// \sa FullDigraph |
| 540 | 540 |
class FullGraph : public ExtendedFullGraphBase {
|
| 541 | 541 |
typedef ExtendedFullGraphBase Parent; |
| 542 | 542 |
|
| 543 | 543 |
public: |
| 544 | 544 |
|
| 545 | 545 |
/// \brief Constructor |
| 546 | 546 |
FullGraph() { construct(0); }
|
| 547 | 547 |
|
| 548 | 548 |
/// \brief Constructor |
| 549 | 549 |
/// |
| 550 | 550 |
/// Constructor. |
| 551 | 551 |
/// \param n The number of the nodes. |
| 552 | 552 |
FullGraph(int n) { construct(n); }
|
| 553 | 553 |
|
| 554 | 554 |
/// \brief Resizes the graph |
| 555 | 555 |
/// |
| 556 | 556 |
/// Resizes the graph. The function will fully destroy and |
| 557 | 557 |
/// rebuild the graph. This cause that the maps of the graph will |
| 558 | 558 |
/// reallocated automatically and the previous values will be lost. |
| 559 | 559 |
void resize(int n) {
|
| 560 | 560 |
Parent::notifier(Arc()).clear(); |
| 561 | 561 |
Parent::notifier(Edge()).clear(); |
| 562 | 562 |
Parent::notifier(Node()).clear(); |
| 563 | 563 |
construct(n); |
| 564 | 564 |
Parent::notifier(Node()).build(); |
| 565 | 565 |
Parent::notifier(Edge()).build(); |
| 566 | 566 |
Parent::notifier(Arc()).build(); |
| 567 | 567 |
} |
| 568 | 568 |
|
| 569 | 569 |
/// \brief Returns the node with the given index. |
| 570 | 570 |
/// |
| 571 | 571 |
/// Returns the node with the given index. Since it is a static |
| 572 | 572 |
/// graph its nodes can be indexed with integers from the range |
| 573 | 573 |
/// <tt>[0..nodeNum()-1]</tt>. |
| 574 | 574 |
/// \sa index() |
| 575 | 575 |
Node operator()(int ix) const { return Parent::operator()(ix); }
|
| 576 | 576 |
|
| 577 | 577 |
/// \brief Returns the index of the given node. |
| 578 | 578 |
/// |
| 579 | 579 |
/// Returns the index of the given node. Since it is a static |
| 580 | 580 |
/// graph its nodes can be indexed with integers from the range |
| 581 | 581 |
/// <tt>[0..nodeNum()-1]</tt>. |
| 582 | 582 |
/// \sa operator() |
| 583 |
int index(const Node& node) |
|
| 583 |
static int index(const Node& node) { return Parent::index(node); }
|
|
| 584 | 584 |
|
| 585 | 585 |
/// \brief Returns the arc connecting the given nodes. |
| 586 | 586 |
/// |
| 587 | 587 |
/// Returns the arc connecting the given nodes. |
| 588 | 588 |
Arc arc(const Node& s, const Node& t) const {
|
| 589 | 589 |
return Parent::arc(s, t); |
| 590 | 590 |
} |
| 591 | 591 |
|
| 592 | 592 |
/// \brief Returns the edge connects the given nodes. |
| 593 | 593 |
/// |
| 594 | 594 |
/// Returns the edge connects the given nodes. |
| 595 | 595 |
Edge edge(const Node& u, const Node& v) const {
|
| 596 | 596 |
return Parent::edge(u, v); |
| 597 | 597 |
} |
| 598 | 598 |
|
| 599 | 599 |
/// \brief Number of nodes. |
| 600 | 600 |
int nodeNum() const { return Parent::nodeNum(); }
|
| 601 | 601 |
/// \brief Number of arcs. |
| 602 | 602 |
int arcNum() const { return Parent::arcNum(); }
|
| 603 | 603 |
/// \brief Number of edges. |
| 604 | 604 |
int edgeNum() const { return Parent::edgeNum(); }
|
| 605 | 605 |
|
| 606 | 606 |
}; |
| 607 | 607 |
|
| 608 | 608 |
|
| 609 | 609 |
} //namespace lemon |
| 610 | 610 |
|
| 611 | 611 |
|
| 612 | 612 |
#endif //LEMON_FULL_GRAPH_H |
| ... | ... |
@@ -217,172 +217,172 @@ |
| 217 | 217 |
arc._id = (k << (_dim-1)) | |
| 218 | 218 |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1)); |
| 219 | 219 |
arc._id = (arc._id << 1) | (~(n._id >> k) & 1); |
| 220 | 220 |
} else {
|
| 221 | 221 |
arc._id = -1; |
| 222 | 222 |
} |
| 223 | 223 |
} |
| 224 | 224 |
|
| 225 | 225 |
void firstIn(Arc& arc, const Node& node) const {
|
| 226 | 226 |
arc._id = ((node._id >> 1) << 1) | (node._id & 1); |
| 227 | 227 |
} |
| 228 | 228 |
|
| 229 | 229 |
void nextIn(Arc& arc) const {
|
| 230 | 230 |
Node n = (arc._id & 1) == 1 ? v(arc) : u(arc); |
| 231 | 231 |
int k = (arc._id >> _dim) + 1; |
| 232 | 232 |
if (k < _dim) {
|
| 233 | 233 |
arc._id = (k << (_dim-1)) | |
| 234 | 234 |
((n._id >> (k+1)) << k) | (n._id & ((1 << k) - 1)); |
| 235 | 235 |
arc._id = (arc._id << 1) | ((n._id >> k) & 1); |
| 236 | 236 |
} else {
|
| 237 | 237 |
arc._id = -1; |
| 238 | 238 |
} |
| 239 | 239 |
} |
| 240 | 240 |
|
| 241 | 241 |
static bool direction(Arc arc) {
|
| 242 | 242 |
return (arc._id & 1) == 1; |
| 243 | 243 |
} |
| 244 | 244 |
|
| 245 | 245 |
static Arc direct(Edge edge, bool dir) {
|
| 246 | 246 |
return Arc((edge._id << 1) | (dir ? 1 : 0)); |
| 247 | 247 |
} |
| 248 | 248 |
|
| 249 | 249 |
int dimension() const {
|
| 250 | 250 |
return _dim; |
| 251 | 251 |
} |
| 252 | 252 |
|
| 253 | 253 |
bool projection(Node node, int n) const {
|
| 254 | 254 |
return static_cast<bool>(node._id & (1 << n)); |
| 255 | 255 |
} |
| 256 | 256 |
|
| 257 | 257 |
int dimension(Edge edge) const {
|
| 258 | 258 |
return edge._id >> (_dim-1); |
| 259 | 259 |
} |
| 260 | 260 |
|
| 261 | 261 |
int dimension(Arc arc) const {
|
| 262 | 262 |
return arc._id >> _dim; |
| 263 | 263 |
} |
| 264 | 264 |
|
| 265 |
int index(Node node) |
|
| 265 |
static int index(Node node) {
|
|
| 266 | 266 |
return node._id; |
| 267 | 267 |
} |
| 268 | 268 |
|
| 269 | 269 |
Node operator()(int ix) const {
|
| 270 | 270 |
return Node(ix); |
| 271 | 271 |
} |
| 272 | 272 |
|
| 273 | 273 |
private: |
| 274 | 274 |
int _dim; |
| 275 | 275 |
int _node_num, _edge_num; |
| 276 | 276 |
}; |
| 277 | 277 |
|
| 278 | 278 |
|
| 279 | 279 |
typedef GraphExtender<HypercubeGraphBase> ExtendedHypercubeGraphBase; |
| 280 | 280 |
|
| 281 | 281 |
/// \ingroup graphs |
| 282 | 282 |
/// |
| 283 | 283 |
/// \brief Hypercube graph class |
| 284 | 284 |
/// |
| 285 | 285 |
/// This class implements a special graph type. The nodes of the graph |
| 286 | 286 |
/// are indiced with integers with at most \c dim binary digits. |
| 287 | 287 |
/// Two nodes are connected in the graph if and only if their indices |
| 288 | 288 |
/// differ only on one position in the binary form. |
| 289 | 289 |
/// |
| 290 | 290 |
/// \note The type of the indices is chosen to \c int for efficiency |
| 291 | 291 |
/// reasons. Thus the maximum dimension of this implementation is 26 |
| 292 | 292 |
/// (assuming that the size of \c int is 32 bit). |
| 293 | 293 |
/// |
| 294 | 294 |
/// This graph type fully conforms to the \ref concepts::Graph |
| 295 | 295 |
/// "Graph concept". |
| 296 | 296 |
class HypercubeGraph : public ExtendedHypercubeGraphBase {
|
| 297 | 297 |
typedef ExtendedHypercubeGraphBase Parent; |
| 298 | 298 |
|
| 299 | 299 |
public: |
| 300 | 300 |
|
| 301 | 301 |
/// \brief Constructs a hypercube graph with \c dim dimensions. |
| 302 | 302 |
/// |
| 303 | 303 |
/// Constructs a hypercube graph with \c dim dimensions. |
| 304 | 304 |
HypercubeGraph(int dim) { construct(dim); }
|
| 305 | 305 |
|
| 306 | 306 |
/// \brief The number of dimensions. |
| 307 | 307 |
/// |
| 308 | 308 |
/// Gives back the number of dimensions. |
| 309 | 309 |
int dimension() const {
|
| 310 | 310 |
return Parent::dimension(); |
| 311 | 311 |
} |
| 312 | 312 |
|
| 313 | 313 |
/// \brief Returns \c true if the n'th bit of the node is one. |
| 314 | 314 |
/// |
| 315 | 315 |
/// Returns \c true if the n'th bit of the node is one. |
| 316 | 316 |
bool projection(Node node, int n) const {
|
| 317 | 317 |
return Parent::projection(node, n); |
| 318 | 318 |
} |
| 319 | 319 |
|
| 320 | 320 |
/// \brief The dimension id of an edge. |
| 321 | 321 |
/// |
| 322 | 322 |
/// Gives back the dimension id of the given edge. |
| 323 | 323 |
/// It is in the [0..dim-1] range. |
| 324 | 324 |
int dimension(Edge edge) const {
|
| 325 | 325 |
return Parent::dimension(edge); |
| 326 | 326 |
} |
| 327 | 327 |
|
| 328 | 328 |
/// \brief The dimension id of an arc. |
| 329 | 329 |
/// |
| 330 | 330 |
/// Gives back the dimension id of the given arc. |
| 331 | 331 |
/// It is in the [0..dim-1] range. |
| 332 | 332 |
int dimension(Arc arc) const {
|
| 333 | 333 |
return Parent::dimension(arc); |
| 334 | 334 |
} |
| 335 | 335 |
|
| 336 | 336 |
/// \brief The index of a node. |
| 337 | 337 |
/// |
| 338 | 338 |
/// Gives back the index of the given node. |
| 339 | 339 |
/// The lower bits of the integer describes the node. |
| 340 |
int index(Node node) |
|
| 340 |
static int index(Node node) {
|
|
| 341 | 341 |
return Parent::index(node); |
| 342 | 342 |
} |
| 343 | 343 |
|
| 344 | 344 |
/// \brief Gives back a node by its index. |
| 345 | 345 |
/// |
| 346 | 346 |
/// Gives back a node by its index. |
| 347 | 347 |
Node operator()(int ix) const {
|
| 348 | 348 |
return Parent::operator()(ix); |
| 349 | 349 |
} |
| 350 | 350 |
|
| 351 | 351 |
/// \brief Number of nodes. |
| 352 | 352 |
int nodeNum() const { return Parent::nodeNum(); }
|
| 353 | 353 |
/// \brief Number of edges. |
| 354 | 354 |
int edgeNum() const { return Parent::edgeNum(); }
|
| 355 | 355 |
/// \brief Number of arcs. |
| 356 | 356 |
int arcNum() const { return Parent::arcNum(); }
|
| 357 | 357 |
|
| 358 | 358 |
/// \brief Linear combination map. |
| 359 | 359 |
/// |
| 360 | 360 |
/// This map makes possible to give back a linear combination |
| 361 | 361 |
/// for each node. It works like the \c std::accumulate function, |
| 362 | 362 |
/// so it accumulates the \c bf binary function with the \c fv first |
| 363 | 363 |
/// value. The map accumulates only on that positions (dimensions) |
| 364 | 364 |
/// where the index of the node is one. The values that have to be |
| 365 | 365 |
/// accumulated should be given by the \c begin and \c end iterators |
| 366 | 366 |
/// and the length of this range should be equal to the dimension |
| 367 | 367 |
/// number of the graph. |
| 368 | 368 |
/// |
| 369 | 369 |
///\code |
| 370 | 370 |
/// const int DIM = 3; |
| 371 | 371 |
/// HypercubeGraph graph(DIM); |
| 372 | 372 |
/// dim2::Point<double> base[DIM]; |
| 373 | 373 |
/// for (int k = 0; k < DIM; ++k) {
|
| 374 | 374 |
/// base[k].x = rnd(); |
| 375 | 375 |
/// base[k].y = rnd(); |
| 376 | 376 |
/// } |
| 377 | 377 |
/// HypercubeGraph::HyperMap<dim2::Point<double> > |
| 378 | 378 |
/// pos(graph, base, base + DIM, dim2::Point<double>(0.0, 0.0)); |
| 379 | 379 |
///\endcode |
| 380 | 380 |
/// |
| 381 | 381 |
/// \see HypercubeGraph |
| 382 | 382 |
template <typename T, typename BF = std::plus<T> > |
| 383 | 383 |
class HyperMap {
|
| 384 | 384 |
public: |
| 385 | 385 |
|
| 386 | 386 |
/// \brief The key type of the map |
| 387 | 387 |
typedef Node Key; |
| 388 | 388 |
/// \brief The value type of the map |
| ... | ... |
@@ -463,113 +463,113 @@ |
| 463 | 463 |
|
| 464 | 464 |
public: |
| 465 | 465 |
operator Edge() const {
|
| 466 | 466 |
return _id != -1 ? edgeFromId(_id / 2) : INVALID; |
| 467 | 467 |
} |
| 468 | 468 |
|
| 469 | 469 |
Arc() {}
|
| 470 | 470 |
Arc (Invalid) { _id = -1; }
|
| 471 | 471 |
bool operator==(const Arc& arc) const {return _id == arc._id;}
|
| 472 | 472 |
bool operator!=(const Arc& arc) const {return _id != arc._id;}
|
| 473 | 473 |
bool operator<(const Arc& arc) const {return _id < arc._id;}
|
| 474 | 474 |
}; |
| 475 | 475 |
|
| 476 | 476 |
|
| 477 | 477 |
|
| 478 | 478 |
SmartGraphBase() |
| 479 | 479 |
: nodes(), arcs() {}
|
| 480 | 480 |
|
| 481 | 481 |
typedef True NodeNumTag; |
| 482 | 482 |
typedef True EdgeNumTag; |
| 483 | 483 |
typedef True ArcNumTag; |
| 484 | 484 |
|
| 485 | 485 |
int nodeNum() const { return nodes.size(); }
|
| 486 | 486 |
int edgeNum() const { return arcs.size() / 2; }
|
| 487 | 487 |
int arcNum() const { return arcs.size(); }
|
| 488 | 488 |
|
| 489 | 489 |
int maxNodeId() const { return nodes.size()-1; }
|
| 490 | 490 |
int maxEdgeId() const { return arcs.size() / 2 - 1; }
|
| 491 | 491 |
int maxArcId() const { return arcs.size()-1; }
|
| 492 | 492 |
|
| 493 | 493 |
Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
|
| 494 | 494 |
Node target(Arc e) const { return Node(arcs[e._id].target); }
|
| 495 | 495 |
|
| 496 | 496 |
Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
|
| 497 | 497 |
Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
|
| 498 | 498 |
|
| 499 | 499 |
static bool direction(Arc e) {
|
| 500 | 500 |
return (e._id & 1) == 1; |
| 501 | 501 |
} |
| 502 | 502 |
|
| 503 | 503 |
static Arc direct(Edge e, bool d) {
|
| 504 | 504 |
return Arc(e._id * 2 + (d ? 1 : 0)); |
| 505 | 505 |
} |
| 506 | 506 |
|
| 507 | 507 |
void first(Node& node) const {
|
| 508 | 508 |
node._id = nodes.size() - 1; |
| 509 | 509 |
} |
| 510 | 510 |
|
| 511 |
void next(Node& node) |
|
| 511 |
static void next(Node& node) {
|
|
| 512 | 512 |
--node._id; |
| 513 | 513 |
} |
| 514 | 514 |
|
| 515 | 515 |
void first(Arc& arc) const {
|
| 516 | 516 |
arc._id = arcs.size() - 1; |
| 517 | 517 |
} |
| 518 | 518 |
|
| 519 |
void next(Arc& arc) |
|
| 519 |
static void next(Arc& arc) {
|
|
| 520 | 520 |
--arc._id; |
| 521 | 521 |
} |
| 522 | 522 |
|
| 523 | 523 |
void first(Edge& arc) const {
|
| 524 | 524 |
arc._id = arcs.size() / 2 - 1; |
| 525 | 525 |
} |
| 526 | 526 |
|
| 527 |
void next(Edge& arc) |
|
| 527 |
static void next(Edge& arc) {
|
|
| 528 | 528 |
--arc._id; |
| 529 | 529 |
} |
| 530 | 530 |
|
| 531 | 531 |
void firstOut(Arc &arc, const Node& v) const {
|
| 532 | 532 |
arc._id = nodes[v._id].first_out; |
| 533 | 533 |
} |
| 534 | 534 |
void nextOut(Arc &arc) const {
|
| 535 | 535 |
arc._id = arcs[arc._id].next_out; |
| 536 | 536 |
} |
| 537 | 537 |
|
| 538 | 538 |
void firstIn(Arc &arc, const Node& v) const {
|
| 539 | 539 |
arc._id = ((nodes[v._id].first_out) ^ 1); |
| 540 | 540 |
if (arc._id == -2) arc._id = -1; |
| 541 | 541 |
} |
| 542 | 542 |
void nextIn(Arc &arc) const {
|
| 543 | 543 |
arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1); |
| 544 | 544 |
if (arc._id == -2) arc._id = -1; |
| 545 | 545 |
} |
| 546 | 546 |
|
| 547 | 547 |
void firstInc(Edge &arc, bool& d, const Node& v) const {
|
| 548 | 548 |
int de = nodes[v._id].first_out; |
| 549 | 549 |
if (de != -1) {
|
| 550 | 550 |
arc._id = de / 2; |
| 551 | 551 |
d = ((de & 1) == 1); |
| 552 | 552 |
} else {
|
| 553 | 553 |
arc._id = -1; |
| 554 | 554 |
d = true; |
| 555 | 555 |
} |
| 556 | 556 |
} |
| 557 | 557 |
void nextInc(Edge &arc, bool& d) const {
|
| 558 | 558 |
int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out); |
| 559 | 559 |
if (de != -1) {
|
| 560 | 560 |
arc._id = de / 2; |
| 561 | 561 |
d = ((de & 1) == 1); |
| 562 | 562 |
} else {
|
| 563 | 563 |
arc._id = -1; |
| 564 | 564 |
d = true; |
| 565 | 565 |
} |
| 566 | 566 |
} |
| 567 | 567 |
|
| 568 | 568 |
static int id(Node v) { return v._id; }
|
| 569 | 569 |
static int id(Arc e) { return e._id; }
|
| 570 | 570 |
static int id(Edge e) { return e._id; }
|
| 571 | 571 |
|
| 572 | 572 |
static Node nodeFromId(int id) { return Node(id);}
|
| 573 | 573 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 574 | 574 |
static Edge edgeFromId(int id) { return Edge(id);}
|
| 575 | 575 |
| ... | ... |
@@ -47,102 +47,102 @@ |
| 47 | 47 |
delete[] arc_next_in; |
| 48 | 48 |
} |
| 49 | 49 |
} |
| 50 | 50 |
|
| 51 | 51 |
class Node {
|
| 52 | 52 |
friend class StaticDigraphBase; |
| 53 | 53 |
protected: |
| 54 | 54 |
int id; |
| 55 | 55 |
Node(int _id) : id(_id) {}
|
| 56 | 56 |
public: |
| 57 | 57 |
Node() {}
|
| 58 | 58 |
Node (Invalid) : id(-1) {}
|
| 59 | 59 |
bool operator==(const Node& node) const { return id == node.id; }
|
| 60 | 60 |
bool operator!=(const Node& node) const { return id != node.id; }
|
| 61 | 61 |
bool operator<(const Node& node) const { return id < node.id; }
|
| 62 | 62 |
}; |
| 63 | 63 |
|
| 64 | 64 |
class Arc {
|
| 65 | 65 |
friend class StaticDigraphBase; |
| 66 | 66 |
protected: |
| 67 | 67 |
int id; |
| 68 | 68 |
Arc(int _id) : id(_id) {}
|
| 69 | 69 |
public: |
| 70 | 70 |
Arc() { }
|
| 71 | 71 |
Arc (Invalid) : id(-1) {}
|
| 72 | 72 |
bool operator==(const Arc& arc) const { return id == arc.id; }
|
| 73 | 73 |
bool operator!=(const Arc& arc) const { return id != arc.id; }
|
| 74 | 74 |
bool operator<(const Arc& arc) const { return id < arc.id; }
|
| 75 | 75 |
}; |
| 76 | 76 |
|
| 77 | 77 |
Node source(const Arc& e) const { return Node(arc_source[e.id]); }
|
| 78 | 78 |
Node target(const Arc& e) const { return Node(arc_target[e.id]); }
|
| 79 | 79 |
|
| 80 | 80 |
void first(Node& n) const { n.id = node_num - 1; }
|
| 81 | 81 |
static void next(Node& n) { --n.id; }
|
| 82 | 82 |
|
| 83 | 83 |
void first(Arc& e) const { e.id = arc_num - 1; }
|
| 84 | 84 |
static void next(Arc& e) { --e.id; }
|
| 85 | 85 |
|
| 86 | 86 |
void firstOut(Arc& e, const Node& n) const {
|
| 87 | 87 |
e.id = node_first_out[n.id] != node_first_out[n.id + 1] ? |
| 88 | 88 |
node_first_out[n.id] : -1; |
| 89 | 89 |
} |
| 90 | 90 |
void nextOut(Arc& e) const { e.id = arc_next_out[e.id]; }
|
| 91 | 91 |
|
| 92 | 92 |
void firstIn(Arc& e, const Node& n) const { e.id = node_first_in[n.id]; }
|
| 93 | 93 |
void nextIn(Arc& e) const { e.id = arc_next_in[e.id]; }
|
| 94 | 94 |
|
| 95 |
int id(const Node& n) const { return n.id; }
|
|
| 96 |
Node nodeFromId(int id) const { return Node(id); }
|
|
| 95 |
static int id(const Node& n) { return n.id; }
|
|
| 96 |
static Node nodeFromId(int id) { return Node(id); }
|
|
| 97 | 97 |
int maxNodeId() const { return node_num - 1; }
|
| 98 | 98 |
|
| 99 |
int id(const Arc& e) const { return e.id; }
|
|
| 100 |
Arc arcFromId(int id) const { return Arc(id); }
|
|
| 99 |
static int id(const Arc& e) { return e.id; }
|
|
| 100 |
static Arc arcFromId(int id) { return Arc(id); }
|
|
| 101 | 101 |
int maxArcId() const { return arc_num - 1; }
|
| 102 | 102 |
|
| 103 | 103 |
typedef True NodeNumTag; |
| 104 | 104 |
typedef True ArcNumTag; |
| 105 | 105 |
|
| 106 | 106 |
int nodeNum() const { return node_num; }
|
| 107 | 107 |
int arcNum() const { return arc_num; }
|
| 108 | 108 |
|
| 109 | 109 |
private: |
| 110 | 110 |
|
| 111 | 111 |
template <typename Digraph, typename NodeRefMap> |
| 112 | 112 |
class ArcLess {
|
| 113 | 113 |
public: |
| 114 | 114 |
typedef typename Digraph::Arc Arc; |
| 115 | 115 |
|
| 116 | 116 |
ArcLess(const Digraph &_graph, const NodeRefMap& _nodeRef) |
| 117 | 117 |
: digraph(_graph), nodeRef(_nodeRef) {}
|
| 118 | 118 |
|
| 119 | 119 |
bool operator()(const Arc& left, const Arc& right) const {
|
| 120 | 120 |
return nodeRef[digraph.target(left)] < nodeRef[digraph.target(right)]; |
| 121 | 121 |
} |
| 122 | 122 |
private: |
| 123 | 123 |
const Digraph& digraph; |
| 124 | 124 |
const NodeRefMap& nodeRef; |
| 125 | 125 |
}; |
| 126 | 126 |
|
| 127 | 127 |
public: |
| 128 | 128 |
|
| 129 | 129 |
typedef True BuildTag; |
| 130 | 130 |
|
| 131 | 131 |
void clear() {
|
| 132 | 132 |
if (built) {
|
| 133 | 133 |
delete[] node_first_out; |
| 134 | 134 |
delete[] node_first_in; |
| 135 | 135 |
delete[] arc_source; |
| 136 | 136 |
delete[] arc_target; |
| 137 | 137 |
delete[] arc_next_out; |
| 138 | 138 |
delete[] arc_next_in; |
| 139 | 139 |
} |
| 140 | 140 |
built = false; |
| 141 | 141 |
node_num = 0; |
| 142 | 142 |
arc_num = 0; |
| 143 | 143 |
} |
| 144 | 144 |
|
| 145 | 145 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap> |
| 146 | 146 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
|
| 147 | 147 |
typedef typename Digraph::Node GNode; |
| 148 | 148 |
typedef typename Digraph::Arc GArc; |
| ... | ... |
@@ -223,115 +223,115 @@ |
| 223 | 223 |
int *arc_next_out; |
| 224 | 224 |
}; |
| 225 | 225 |
|
| 226 | 226 |
typedef DigraphExtender<StaticDigraphBase> ExtendedStaticDigraphBase; |
| 227 | 227 |
|
| 228 | 228 |
|
| 229 | 229 |
/// \ingroup graphs |
| 230 | 230 |
/// |
| 231 | 231 |
/// \brief A static directed graph class. |
| 232 | 232 |
/// |
| 233 | 233 |
/// \ref StaticDigraph is a highly efficient digraph implementation, |
| 234 | 234 |
/// but it is fully static. |
| 235 | 235 |
/// It stores only two \c int values for each node and only four \c int |
| 236 | 236 |
/// values for each arc. Moreover it provides faster item iteration than |
| 237 | 237 |
/// \ref ListDigraph and \ref SmartDigraph, especially using \c OutArcIt |
| 238 | 238 |
/// iterators, since its arcs are stored in an appropriate order. |
| 239 | 239 |
/// However it only provides build() and clear() functions and does not |
| 240 | 240 |
/// support any other modification of the digraph. |
| 241 | 241 |
/// |
| 242 | 242 |
/// Since this digraph structure is completely static, its nodes and arcs |
| 243 | 243 |
/// can be indexed with integers from the ranges <tt>[0..nodeNum()-1]</tt> |
| 244 | 244 |
/// and <tt>[0..arcNum()-1]</tt>, respectively. |
| 245 | 245 |
/// The index of an item is the same as its ID, it can be obtained |
| 246 | 246 |
/// using the corresponding \ref index() or \ref concepts::Digraph::id() |
| 247 | 247 |
/// "id()" function. A node or arc with a certain index can be obtained |
| 248 | 248 |
/// using node() or arc(). |
| 249 | 249 |
/// |
| 250 | 250 |
/// This type fully conforms to the \ref concepts::Digraph "Digraph concept". |
| 251 | 251 |
/// Most of its member functions and nested classes are documented |
| 252 | 252 |
/// only in the concept class. |
| 253 | 253 |
/// |
| 254 | 254 |
/// \sa concepts::Digraph |
| 255 | 255 |
class StaticDigraph : public ExtendedStaticDigraphBase {
|
| 256 | 256 |
public: |
| 257 | 257 |
|
| 258 | 258 |
typedef ExtendedStaticDigraphBase Parent; |
| 259 | 259 |
|
| 260 | 260 |
public: |
| 261 | 261 |
|
| 262 | 262 |
/// \brief Constructor |
| 263 | 263 |
/// |
| 264 | 264 |
/// Default constructor. |
| 265 | 265 |
StaticDigraph() : Parent() {}
|
| 266 | 266 |
|
| 267 | 267 |
/// \brief The node with the given index. |
| 268 | 268 |
/// |
| 269 | 269 |
/// This function returns the node with the given index. |
| 270 | 270 |
/// \sa index() |
| 271 |
Node node(int ix) |
|
| 271 |
static Node node(int ix) { return Parent::nodeFromId(ix); }
|
|
| 272 | 272 |
|
| 273 | 273 |
/// \brief The arc with the given index. |
| 274 | 274 |
/// |
| 275 | 275 |
/// This function returns the arc with the given index. |
| 276 | 276 |
/// \sa index() |
| 277 |
Arc arc(int ix) |
|
| 277 |
static Arc arc(int ix) { return Parent::arcFromId(ix); }
|
|
| 278 | 278 |
|
| 279 | 279 |
/// \brief The index of the given node. |
| 280 | 280 |
/// |
| 281 | 281 |
/// This function returns the index of the the given node. |
| 282 | 282 |
/// \sa node() |
| 283 |
int index(Node node) |
|
| 283 |
static int index(Node node) { return Parent::id(node); }
|
|
| 284 | 284 |
|
| 285 | 285 |
/// \brief The index of the given arc. |
| 286 | 286 |
/// |
| 287 | 287 |
/// This function returns the index of the the given arc. |
| 288 | 288 |
/// \sa arc() |
| 289 |
int index(Arc arc) |
|
| 289 |
static int index(Arc arc) { return Parent::id(arc); }
|
|
| 290 | 290 |
|
| 291 | 291 |
/// \brief Number of nodes. |
| 292 | 292 |
/// |
| 293 | 293 |
/// This function returns the number of nodes. |
| 294 | 294 |
int nodeNum() const { return node_num; }
|
| 295 | 295 |
|
| 296 | 296 |
/// \brief Number of arcs. |
| 297 | 297 |
/// |
| 298 | 298 |
/// This function returns the number of arcs. |
| 299 | 299 |
int arcNum() const { return arc_num; }
|
| 300 | 300 |
|
| 301 | 301 |
/// \brief Build the digraph copying another digraph. |
| 302 | 302 |
/// |
| 303 | 303 |
/// This function builds the digraph copying another digraph of any |
| 304 | 304 |
/// kind. It can be called more than once, but in such case, the whole |
| 305 | 305 |
/// structure and all maps will be cleared and rebuilt. |
| 306 | 306 |
/// |
| 307 | 307 |
/// This method also makes possible to copy a digraph to a StaticDigraph |
| 308 | 308 |
/// structure using \ref DigraphCopy. |
| 309 | 309 |
/// |
| 310 | 310 |
/// \param digraph An existing digraph to be copied. |
| 311 | 311 |
/// \param nodeRef The node references will be copied into this map. |
| 312 | 312 |
/// Its key type must be \c Digraph::Node and its value type must be |
| 313 | 313 |
/// \c StaticDigraph::Node. |
| 314 | 314 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" |
| 315 | 315 |
/// concept. |
| 316 | 316 |
/// \param arcRef The arc references will be copied into this map. |
| 317 | 317 |
/// Its key type must be \c Digraph::Arc and its value type must be |
| 318 | 318 |
/// \c StaticDigraph::Arc. |
| 319 | 319 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 320 | 320 |
/// |
| 321 | 321 |
/// \note If you do not need the arc references, then you could use |
| 322 | 322 |
/// \ref NullMap for the last parameter. However the node references |
| 323 | 323 |
/// are required by the function itself, thus they must be readable |
| 324 | 324 |
/// from the map. |
| 325 | 325 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap> |
| 326 | 326 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
|
| 327 | 327 |
if (built) Parent::clear(); |
| 328 | 328 |
Parent::build(digraph, nodeRef, arcRef); |
| 329 | 329 |
} |
| 330 | 330 |
|
| 331 | 331 |
/// \brief Clear the digraph. |
| 332 | 332 |
/// |
| 333 | 333 |
/// This function erases all nodes and arcs from the digraph. |
| 334 | 334 |
void clear() {
|
| 335 | 335 |
Parent::clear(); |
| 336 | 336 |
} |
| 337 | 337 |
|
0 comments (0 inline)