| ... | ... |
@@ -952,194 +952,194 @@ |
| 952 | 952 |
/// The \ref run() function must be called before using them. |
| 953 | 953 |
|
| 954 | 954 |
/// @{
|
| 955 | 955 |
|
| 956 | 956 |
/// \brief Return the total cost of the found flow. |
| 957 | 957 |
/// |
| 958 | 958 |
/// This function returns the total cost of the found flow. |
| 959 | 959 |
/// Its complexity is O(e). |
| 960 | 960 |
/// |
| 961 | 961 |
/// \note The return type of the function can be specified as a |
| 962 | 962 |
/// template parameter. For example, |
| 963 | 963 |
/// \code |
| 964 | 964 |
/// ns.totalCost<double>(); |
| 965 | 965 |
/// \endcode |
| 966 | 966 |
/// It is useful if the total cost cannot be stored in the \c Cost |
| 967 | 967 |
/// type of the algorithm, which is the default return type of the |
| 968 | 968 |
/// function. |
| 969 | 969 |
/// |
| 970 | 970 |
/// \pre \ref run() must be called before using this function. |
| 971 | 971 |
template <typename Number> |
| 972 | 972 |
Number totalCost() const {
|
| 973 | 973 |
Number c = 0; |
| 974 | 974 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 975 | 975 |
int i = _arc_id[a]; |
| 976 | 976 |
c += Number(_flow[i]) * Number(_cost[i]); |
| 977 | 977 |
} |
| 978 | 978 |
return c; |
| 979 | 979 |
} |
| 980 | 980 |
|
| 981 | 981 |
#ifndef DOXYGEN |
| 982 | 982 |
Cost totalCost() const {
|
| 983 | 983 |
return totalCost<Cost>(); |
| 984 | 984 |
} |
| 985 | 985 |
#endif |
| 986 | 986 |
|
| 987 | 987 |
/// \brief Return the flow on the given arc. |
| 988 | 988 |
/// |
| 989 | 989 |
/// This function returns the flow on the given arc. |
| 990 | 990 |
/// |
| 991 | 991 |
/// \pre \ref run() must be called before using this function. |
| 992 | 992 |
Value flow(const Arc& a) const {
|
| 993 | 993 |
return _flow[_arc_id[a]]; |
| 994 | 994 |
} |
| 995 | 995 |
|
| 996 | 996 |
/// \brief Return the flow map (the primal solution). |
| 997 | 997 |
/// |
| 998 | 998 |
/// This function copies the flow value on each arc into the given |
| 999 | 999 |
/// map. The \c Value type of the algorithm must be convertible to |
| 1000 | 1000 |
/// the \c Value type of the map. |
| 1001 | 1001 |
/// |
| 1002 | 1002 |
/// \pre \ref run() must be called before using this function. |
| 1003 | 1003 |
template <typename FlowMap> |
| 1004 | 1004 |
void flowMap(FlowMap &map) const {
|
| 1005 | 1005 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 1006 | 1006 |
map.set(a, _flow[_arc_id[a]]); |
| 1007 | 1007 |
} |
| 1008 | 1008 |
} |
| 1009 | 1009 |
|
| 1010 | 1010 |
/// \brief Return the potential (dual value) of the given node. |
| 1011 | 1011 |
/// |
| 1012 | 1012 |
/// This function returns the potential (dual value) of the |
| 1013 | 1013 |
/// given node. |
| 1014 | 1014 |
/// |
| 1015 | 1015 |
/// \pre \ref run() must be called before using this function. |
| 1016 | 1016 |
Cost potential(const Node& n) const {
|
| 1017 | 1017 |
return _pi[_node_id[n]]; |
| 1018 | 1018 |
} |
| 1019 | 1019 |
|
| 1020 | 1020 |
/// \brief Return the potential map (the dual solution). |
| 1021 | 1021 |
/// |
| 1022 | 1022 |
/// This function copies the potential (dual value) of each node |
| 1023 | 1023 |
/// into the given map. |
| 1024 | 1024 |
/// The \c Cost type of the algorithm must be convertible to the |
| 1025 | 1025 |
/// \c Value type of the map. |
| 1026 | 1026 |
/// |
| 1027 | 1027 |
/// \pre \ref run() must be called before using this function. |
| 1028 | 1028 |
template <typename PotentialMap> |
| 1029 | 1029 |
void potentialMap(PotentialMap &map) const {
|
| 1030 | 1030 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1031 | 1031 |
map.set(n, _pi[_node_id[n]]); |
| 1032 | 1032 |
} |
| 1033 | 1033 |
} |
| 1034 | 1034 |
|
| 1035 | 1035 |
/// @} |
| 1036 | 1036 |
|
| 1037 | 1037 |
private: |
| 1038 | 1038 |
|
| 1039 | 1039 |
// Initialize internal data structures |
| 1040 | 1040 |
bool init() {
|
| 1041 | 1041 |
if (_node_num == 0) return false; |
| 1042 | 1042 |
|
| 1043 | 1043 |
// Check the sum of supply values |
| 1044 | 1044 |
_sum_supply = 0; |
| 1045 | 1045 |
for (int i = 0; i != _node_num; ++i) {
|
| 1046 | 1046 |
_sum_supply += _supply[i]; |
| 1047 | 1047 |
} |
| 1048 |
if ( !(_stype == GEQ && _sum_supply <= 0 || |
|
| 1049 |
_stype == LEQ && _sum_supply >= 0) ) return false; |
|
| 1048 |
if ( !((_stype == GEQ && _sum_supply <= 0) || |
|
| 1049 |
(_stype == LEQ && _sum_supply >= 0)) ) return false; |
|
| 1050 | 1050 |
|
| 1051 | 1051 |
// Remove non-zero lower bounds |
| 1052 | 1052 |
if (_have_lower) {
|
| 1053 | 1053 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1054 | 1054 |
Value c = _lower[i]; |
| 1055 | 1055 |
if (c >= 0) {
|
| 1056 | 1056 |
_cap[i] = _upper[i] < INF ? _upper[i] - c : INF; |
| 1057 | 1057 |
} else {
|
| 1058 | 1058 |
_cap[i] = _upper[i] < INF + c ? _upper[i] - c : INF; |
| 1059 | 1059 |
} |
| 1060 | 1060 |
_supply[_source[i]] -= c; |
| 1061 | 1061 |
_supply[_target[i]] += c; |
| 1062 | 1062 |
} |
| 1063 | 1063 |
} else {
|
| 1064 | 1064 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1065 | 1065 |
_cap[i] = _upper[i]; |
| 1066 | 1066 |
} |
| 1067 | 1067 |
} |
| 1068 | 1068 |
|
| 1069 | 1069 |
// Initialize artifical cost |
| 1070 | 1070 |
Cost ART_COST; |
| 1071 | 1071 |
if (std::numeric_limits<Cost>::is_exact) {
|
| 1072 | 1072 |
ART_COST = std::numeric_limits<Cost>::max() / 4 + 1; |
| 1073 | 1073 |
} else {
|
| 1074 | 1074 |
ART_COST = std::numeric_limits<Cost>::min(); |
| 1075 | 1075 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1076 | 1076 |
if (_cost[i] > ART_COST) ART_COST = _cost[i]; |
| 1077 | 1077 |
} |
| 1078 | 1078 |
ART_COST = (ART_COST + 1) * _node_num; |
| 1079 | 1079 |
} |
| 1080 | 1080 |
|
| 1081 | 1081 |
// Initialize arc maps |
| 1082 | 1082 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1083 | 1083 |
_flow[i] = 0; |
| 1084 | 1084 |
_state[i] = STATE_LOWER; |
| 1085 | 1085 |
} |
| 1086 | 1086 |
|
| 1087 | 1087 |
// Set data for the artificial root node |
| 1088 | 1088 |
_root = _node_num; |
| 1089 | 1089 |
_parent[_root] = -1; |
| 1090 | 1090 |
_pred[_root] = -1; |
| 1091 | 1091 |
_thread[_root] = 0; |
| 1092 | 1092 |
_rev_thread[0] = _root; |
| 1093 | 1093 |
_succ_num[_root] = _node_num + 1; |
| 1094 | 1094 |
_last_succ[_root] = _root - 1; |
| 1095 | 1095 |
_supply[_root] = -_sum_supply; |
| 1096 | 1096 |
_pi[_root] = _sum_supply < 0 ? -ART_COST : ART_COST; |
| 1097 | 1097 |
|
| 1098 | 1098 |
// Add artificial arcs and initialize the spanning tree data structure |
| 1099 | 1099 |
for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) {
|
| 1100 | 1100 |
_parent[u] = _root; |
| 1101 | 1101 |
_pred[u] = e; |
| 1102 | 1102 |
_thread[u] = u + 1; |
| 1103 | 1103 |
_rev_thread[u + 1] = u; |
| 1104 | 1104 |
_succ_num[u] = 1; |
| 1105 | 1105 |
_last_succ[u] = u; |
| 1106 | 1106 |
_cost[e] = ART_COST; |
| 1107 | 1107 |
_cap[e] = INF; |
| 1108 | 1108 |
_state[e] = STATE_TREE; |
| 1109 | 1109 |
if (_supply[u] > 0 || (_supply[u] == 0 && _sum_supply <= 0)) {
|
| 1110 | 1110 |
_flow[e] = _supply[u]; |
| 1111 | 1111 |
_forward[u] = true; |
| 1112 | 1112 |
_pi[u] = -ART_COST + _pi[_root]; |
| 1113 | 1113 |
} else {
|
| 1114 | 1114 |
_flow[e] = -_supply[u]; |
| 1115 | 1115 |
_forward[u] = false; |
| 1116 | 1116 |
_pi[u] = ART_COST + _pi[_root]; |
| 1117 | 1117 |
} |
| 1118 | 1118 |
} |
| 1119 | 1119 |
|
| 1120 | 1120 |
return true; |
| 1121 | 1121 |
} |
| 1122 | 1122 |
|
| 1123 | 1123 |
// Find the join node |
| 1124 | 1124 |
void findJoinNode() {
|
| 1125 | 1125 |
int u = _source[in_arc]; |
| 1126 | 1126 |
int v = _target[in_arc]; |
| 1127 | 1127 |
while (u != v) {
|
| 1128 | 1128 |
if (_succ_num[u] < _succ_num[v]) {
|
| 1129 | 1129 |
u = _parent[u]; |
| 1130 | 1130 |
} else {
|
| 1131 | 1131 |
v = _parent[v]; |
| 1132 | 1132 |
} |
| 1133 | 1133 |
} |
| 1134 | 1134 |
join = u; |
| 1135 | 1135 |
} |
| 1136 | 1136 |
|
| 1137 | 1137 |
// Find the leaving arc of the cycle and returns true if the |
| 1138 | 1138 |
// leaving arc is not the same as the entering arc |
| 1139 | 1139 |
bool findLeavingArc() {
|
| 1140 | 1140 |
// Initialize first and second nodes according to the direction |
| 1141 | 1141 |
// of the cycle |
| 1142 | 1142 |
if (_state[in_arc] == STATE_LOWER) {
|
| 1143 | 1143 |
first = _source[in_arc]; |
| 1144 | 1144 |
second = _target[in_arc]; |
| 1145 | 1145 |
} else {
|
0 comments (0 inline)