gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge bugfix #368 to branch 1.1
0 1 0
merge 1.1
0 files changed with 2 insertions and 2 deletions:
↑ Collapse diff ↑
Ignore white space 512 line context
... ...
@@ -789,701 +789,701 @@
789 789
    /// and the required flow value.
790 790
    /// If neither this function nor \ref supplyMap() is used before
791 791
    /// calling \ref run(), the supply of each node will be set to zero.
792 792
    /// (It makes sense only if non-zero lower bounds are given.)
793 793
    ///
794 794
    /// Using this function has the same effect as using \ref supplyMap()
795 795
    /// with such a map in which \c k is assigned to \c s, \c -k is
796 796
    /// assigned to \c t and all other nodes have zero supply value.
797 797
    ///
798 798
    /// \param s The source node.
799 799
    /// \param t The target node.
800 800
    /// \param k The required amount of flow from node \c s to node \c t
801 801
    /// (i.e. the supply of \c s and the demand of \c t).
802 802
    ///
803 803
    /// \return <tt>(*this)</tt>
804 804
    NetworkSimplex& stSupply(const Node& s, const Node& t, Value k) {
805 805
      for (int i = 0; i != _node_num; ++i) {
806 806
        _supply[i] = 0;
807 807
      }
808 808
      _supply[_node_id[s]] =  k;
809 809
      _supply[_node_id[t]] = -k;
810 810
      return *this;
811 811
    }
812 812
    
813 813
    /// \brief Set the type of the supply constraints.
814 814
    ///
815 815
    /// This function sets the type of the supply/demand constraints.
816 816
    /// If it is not used before calling \ref run(), the \ref GEQ supply
817 817
    /// type will be used.
818 818
    ///
819 819
    /// For more information see \ref SupplyType.
820 820
    ///
821 821
    /// \return <tt>(*this)</tt>
822 822
    NetworkSimplex& supplyType(SupplyType supply_type) {
823 823
      _stype = supply_type;
824 824
      return *this;
825 825
    }
826 826

	
827 827
    /// @}
828 828

	
829 829
    /// \name Execution Control
830 830
    /// The algorithm can be executed using \ref run().
831 831

	
832 832
    /// @{
833 833

	
834 834
    /// \brief Run the algorithm.
835 835
    ///
836 836
    /// This function runs the algorithm.
837 837
    /// The paramters can be specified using functions \ref lowerMap(),
838 838
    /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(), 
839 839
    /// \ref supplyType().
840 840
    /// For example,
841 841
    /// \code
842 842
    ///   NetworkSimplex<ListDigraph> ns(graph);
843 843
    ///   ns.lowerMap(lower).upperMap(upper).costMap(cost)
844 844
    ///     .supplyMap(sup).run();
845 845
    /// \endcode
846 846
    ///
847 847
    /// This function can be called more than once. All the parameters
848 848
    /// that have been given are kept for the next call, unless
849 849
    /// \ref reset() is called, thus only the modified parameters
850 850
    /// have to be set again. See \ref reset() for examples.
851 851
    /// However the underlying digraph must not be modified after this
852 852
    /// class have been constructed, since it copies and extends the graph.
853 853
    ///
854 854
    /// \param pivot_rule The pivot rule that will be used during the
855 855
    /// algorithm. For more information see \ref PivotRule.
856 856
    ///
857 857
    /// \return \c INFEASIBLE if no feasible flow exists,
858 858
    /// \n \c OPTIMAL if the problem has optimal solution
859 859
    /// (i.e. it is feasible and bounded), and the algorithm has found
860 860
    /// optimal flow and node potentials (primal and dual solutions),
861 861
    /// \n \c UNBOUNDED if the objective function of the problem is
862 862
    /// unbounded, i.e. there is a directed cycle having negative total
863 863
    /// cost and infinite upper bound.
864 864
    ///
865 865
    /// \see ProblemType, PivotRule
866 866
    ProblemType run(PivotRule pivot_rule = BLOCK_SEARCH) {
867 867
      if (!init()) return INFEASIBLE;
868 868
      return start(pivot_rule);
869 869
    }
870 870

	
871 871
    /// \brief Reset all the parameters that have been given before.
872 872
    ///
873 873
    /// This function resets all the paramaters that have been given
874 874
    /// before using functions \ref lowerMap(), \ref upperMap(),
875 875
    /// \ref costMap(), \ref supplyMap(), \ref stSupply(), \ref supplyType().
876 876
    ///
877 877
    /// It is useful for multiple run() calls. If this function is not
878 878
    /// used, all the parameters given before are kept for the next
879 879
    /// \ref run() call.
880 880
    /// However the underlying digraph must not be modified after this
881 881
    /// class have been constructed, since it copies and extends the graph.
882 882
    ///
883 883
    /// For example,
884 884
    /// \code
885 885
    ///   NetworkSimplex<ListDigraph> ns(graph);
886 886
    ///
887 887
    ///   // First run
888 888
    ///   ns.lowerMap(lower).upperMap(upper).costMap(cost)
889 889
    ///     .supplyMap(sup).run();
890 890
    ///
891 891
    ///   // Run again with modified cost map (reset() is not called,
892 892
    ///   // so only the cost map have to be set again)
893 893
    ///   cost[e] += 100;
894 894
    ///   ns.costMap(cost).run();
895 895
    ///
896 896
    ///   // Run again from scratch using reset()
897 897
    ///   // (the lower bounds will be set to zero on all arcs)
898 898
    ///   ns.reset();
899 899
    ///   ns.upperMap(capacity).costMap(cost)
900 900
    ///     .supplyMap(sup).run();
901 901
    /// \endcode
902 902
    ///
903 903
    /// \return <tt>(*this)</tt>
904 904
    NetworkSimplex& reset() {
905 905
      for (int i = 0; i != _node_num; ++i) {
906 906
        _supply[i] = 0;
907 907
      }
908 908
      for (int i = 0; i != _arc_num; ++i) {
909 909
        _lower[i] = 0;
910 910
        _upper[i] = INF;
911 911
        _cost[i] = 1;
912 912
      }
913 913
      _have_lower = false;
914 914
      _stype = GEQ;
915 915
      return *this;
916 916
    }
917 917

	
918 918
    /// @}
919 919

	
920 920
    /// \name Query Functions
921 921
    /// The results of the algorithm can be obtained using these
922 922
    /// functions.\n
923 923
    /// The \ref run() function must be called before using them.
924 924

	
925 925
    /// @{
926 926

	
927 927
    /// \brief Return the total cost of the found flow.
928 928
    ///
929 929
    /// This function returns the total cost of the found flow.
930 930
    /// Its complexity is O(e).
931 931
    ///
932 932
    /// \note The return type of the function can be specified as a
933 933
    /// template parameter. For example,
934 934
    /// \code
935 935
    ///   ns.totalCost<double>();
936 936
    /// \endcode
937 937
    /// It is useful if the total cost cannot be stored in the \c Cost
938 938
    /// type of the algorithm, which is the default return type of the
939 939
    /// function.
940 940
    ///
941 941
    /// \pre \ref run() must be called before using this function.
942 942
    template <typename Number>
943 943
    Number totalCost() const {
944 944
      Number c = 0;
945 945
      for (ArcIt a(_graph); a != INVALID; ++a) {
946 946
        int i = _arc_id[a];
947 947
        c += Number(_flow[i]) * Number(_cost[i]);
948 948
      }
949 949
      return c;
950 950
    }
951 951

	
952 952
#ifndef DOXYGEN
953 953
    Cost totalCost() const {
954 954
      return totalCost<Cost>();
955 955
    }
956 956
#endif
957 957

	
958 958
    /// \brief Return the flow on the given arc.
959 959
    ///
960 960
    /// This function returns the flow on the given arc.
961 961
    ///
962 962
    /// \pre \ref run() must be called before using this function.
963 963
    Value flow(const Arc& a) const {
964 964
      return _flow[_arc_id[a]];
965 965
    }
966 966

	
967 967
    /// \brief Return the flow map (the primal solution).
968 968
    ///
969 969
    /// This function copies the flow value on each arc into the given
970 970
    /// map. The \c Value type of the algorithm must be convertible to
971 971
    /// the \c Value type of the map.
972 972
    ///
973 973
    /// \pre \ref run() must be called before using this function.
974 974
    template <typename FlowMap>
975 975
    void flowMap(FlowMap &map) const {
976 976
      for (ArcIt a(_graph); a != INVALID; ++a) {
977 977
        map.set(a, _flow[_arc_id[a]]);
978 978
      }
979 979
    }
980 980

	
981 981
    /// \brief Return the potential (dual value) of the given node.
982 982
    ///
983 983
    /// This function returns the potential (dual value) of the
984 984
    /// given node.
985 985
    ///
986 986
    /// \pre \ref run() must be called before using this function.
987 987
    Cost potential(const Node& n) const {
988 988
      return _pi[_node_id[n]];
989 989
    }
990 990

	
991 991
    /// \brief Return the potential map (the dual solution).
992 992
    ///
993 993
    /// This function copies the potential (dual value) of each node
994 994
    /// into the given map.
995 995
    /// The \c Cost type of the algorithm must be convertible to the
996 996
    /// \c Value type of the map.
997 997
    ///
998 998
    /// \pre \ref run() must be called before using this function.
999 999
    template <typename PotentialMap>
1000 1000
    void potentialMap(PotentialMap &map) const {
1001 1001
      for (NodeIt n(_graph); n != INVALID; ++n) {
1002 1002
        map.set(n, _pi[_node_id[n]]);
1003 1003
      }
1004 1004
    }
1005 1005

	
1006 1006
    /// @}
1007 1007

	
1008 1008
  private:
1009 1009

	
1010 1010
    // Initialize internal data structures
1011 1011
    bool init() {
1012 1012
      if (_node_num == 0) return false;
1013 1013

	
1014 1014
      // Check the sum of supply values
1015 1015
      _sum_supply = 0;
1016 1016
      for (int i = 0; i != _node_num; ++i) {
1017 1017
        _sum_supply += _supply[i];
1018 1018
      }
1019 1019
      if ( !((_stype == GEQ && _sum_supply <= 0) ||
1020 1020
             (_stype == LEQ && _sum_supply >= 0)) ) return false;
1021 1021

	
1022 1022
      // Remove non-zero lower bounds
1023 1023
      if (_have_lower) {
1024 1024
        for (int i = 0; i != _arc_num; ++i) {
1025 1025
          Value c = _lower[i];
1026 1026
          if (c >= 0) {
1027 1027
            _cap[i] = _upper[i] < INF ? _upper[i] - c : INF;
1028 1028
          } else {
1029 1029
            _cap[i] = _upper[i] < INF + c ? _upper[i] - c : INF;
1030 1030
          }
1031 1031
          _supply[_source[i]] -= c;
1032 1032
          _supply[_target[i]] += c;
1033 1033
        }
1034 1034
      } else {
1035 1035
        for (int i = 0; i != _arc_num; ++i) {
1036 1036
          _cap[i] = _upper[i];
1037 1037
        }
1038 1038
      }
1039 1039

	
1040 1040
      // Initialize artifical cost
1041 1041
      Cost ART_COST;
1042 1042
      if (std::numeric_limits<Cost>::is_exact) {
1043 1043
        ART_COST = std::numeric_limits<Cost>::max() / 2 + 1;
1044 1044
      } else {
1045
        ART_COST = std::numeric_limits<Cost>::min();
1045
        ART_COST = 0;
1046 1046
        for (int i = 0; i != _arc_num; ++i) {
1047 1047
          if (_cost[i] > ART_COST) ART_COST = _cost[i];
1048 1048
        }
1049 1049
        ART_COST = (ART_COST + 1) * _node_num;
1050 1050
      }
1051 1051

	
1052 1052
      // Initialize arc maps
1053 1053
      for (int i = 0; i != _arc_num; ++i) {
1054 1054
        _flow[i] = 0;
1055 1055
        _state[i] = STATE_LOWER;
1056 1056
      }
1057 1057
      
1058 1058
      // Set data for the artificial root node
1059 1059
      _root = _node_num;
1060 1060
      _parent[_root] = -1;
1061 1061
      _pred[_root] = -1;
1062 1062
      _thread[_root] = 0;
1063 1063
      _rev_thread[0] = _root;
1064 1064
      _succ_num[_root] = _node_num + 1;
1065 1065
      _last_succ[_root] = _root - 1;
1066 1066
      _supply[_root] = -_sum_supply;
1067 1067
      _pi[_root] = 0;
1068 1068

	
1069 1069
      // Add artificial arcs and initialize the spanning tree data structure
1070 1070
      if (_sum_supply == 0) {
1071 1071
        // EQ supply constraints
1072 1072
        _search_arc_num = _arc_num;
1073 1073
        _all_arc_num = _arc_num + _node_num;
1074 1074
        for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) {
1075 1075
          _parent[u] = _root;
1076 1076
          _pred[u] = e;
1077 1077
          _thread[u] = u + 1;
1078 1078
          _rev_thread[u + 1] = u;
1079 1079
          _succ_num[u] = 1;
1080 1080
          _last_succ[u] = u;
1081 1081
          _cap[e] = INF;
1082 1082
          _state[e] = STATE_TREE;
1083 1083
          if (_supply[u] >= 0) {
1084 1084
            _forward[u] = true;
1085 1085
            _pi[u] = 0;
1086 1086
            _source[e] = u;
1087 1087
            _target[e] = _root;
1088 1088
            _flow[e] = _supply[u];
1089 1089
            _cost[e] = 0;
1090 1090
          } else {
1091 1091
            _forward[u] = false;
1092 1092
            _pi[u] = ART_COST;
1093 1093
            _source[e] = _root;
1094 1094
            _target[e] = u;
1095 1095
            _flow[e] = -_supply[u];
1096 1096
            _cost[e] = ART_COST;
1097 1097
          }
1098 1098
        }
1099 1099
      }
1100 1100
      else if (_sum_supply > 0) {
1101 1101
        // LEQ supply constraints
1102 1102
        _search_arc_num = _arc_num + _node_num;
1103 1103
        int f = _arc_num + _node_num;
1104 1104
        for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) {
1105 1105
          _parent[u] = _root;
1106 1106
          _thread[u] = u + 1;
1107 1107
          _rev_thread[u + 1] = u;
1108 1108
          _succ_num[u] = 1;
1109 1109
          _last_succ[u] = u;
1110 1110
          if (_supply[u] >= 0) {
1111 1111
            _forward[u] = true;
1112 1112
            _pi[u] = 0;
1113 1113
            _pred[u] = e;
1114 1114
            _source[e] = u;
1115 1115
            _target[e] = _root;
1116 1116
            _cap[e] = INF;
1117 1117
            _flow[e] = _supply[u];
1118 1118
            _cost[e] = 0;
1119 1119
            _state[e] = STATE_TREE;
1120 1120
          } else {
1121 1121
            _forward[u] = false;
1122 1122
            _pi[u] = ART_COST;
1123 1123
            _pred[u] = f;
1124 1124
            _source[f] = _root;
1125 1125
            _target[f] = u;
1126 1126
            _cap[f] = INF;
1127 1127
            _flow[f] = -_supply[u];
1128 1128
            _cost[f] = ART_COST;
1129 1129
            _state[f] = STATE_TREE;
1130 1130
            _source[e] = u;
1131 1131
            _target[e] = _root;
1132 1132
            _cap[e] = INF;
1133 1133
            _flow[e] = 0;
1134 1134
            _cost[e] = 0;
1135 1135
            _state[e] = STATE_LOWER;
1136 1136
            ++f;
1137 1137
          }
1138 1138
        }
1139 1139
        _all_arc_num = f;
1140 1140
      }
1141 1141
      else {
1142 1142
        // GEQ supply constraints
1143 1143
        _search_arc_num = _arc_num + _node_num;
1144 1144
        int f = _arc_num + _node_num;
1145 1145
        for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) {
1146 1146
          _parent[u] = _root;
1147 1147
          _thread[u] = u + 1;
1148 1148
          _rev_thread[u + 1] = u;
1149 1149
          _succ_num[u] = 1;
1150 1150
          _last_succ[u] = u;
1151 1151
          if (_supply[u] <= 0) {
1152 1152
            _forward[u] = false;
1153 1153
            _pi[u] = 0;
1154 1154
            _pred[u] = e;
1155 1155
            _source[e] = _root;
1156 1156
            _target[e] = u;
1157 1157
            _cap[e] = INF;
1158 1158
            _flow[e] = -_supply[u];
1159 1159
            _cost[e] = 0;
1160 1160
            _state[e] = STATE_TREE;
1161 1161
          } else {
1162 1162
            _forward[u] = true;
1163 1163
            _pi[u] = -ART_COST;
1164 1164
            _pred[u] = f;
1165 1165
            _source[f] = u;
1166 1166
            _target[f] = _root;
1167 1167
            _cap[f] = INF;
1168 1168
            _flow[f] = _supply[u];
1169 1169
            _state[f] = STATE_TREE;
1170 1170
            _cost[f] = ART_COST;
1171 1171
            _source[e] = _root;
1172 1172
            _target[e] = u;
1173 1173
            _cap[e] = INF;
1174 1174
            _flow[e] = 0;
1175 1175
            _cost[e] = 0;
1176 1176
            _state[e] = STATE_LOWER;
1177 1177
            ++f;
1178 1178
          }
1179 1179
        }
1180 1180
        _all_arc_num = f;
1181 1181
      }
1182 1182

	
1183 1183
      return true;
1184 1184
    }
1185 1185

	
1186 1186
    // Find the join node
1187 1187
    void findJoinNode() {
1188 1188
      int u = _source[in_arc];
1189 1189
      int v = _target[in_arc];
1190 1190
      while (u != v) {
1191 1191
        if (_succ_num[u] < _succ_num[v]) {
1192 1192
          u = _parent[u];
1193 1193
        } else {
1194 1194
          v = _parent[v];
1195 1195
        }
1196 1196
      }
1197 1197
      join = u;
1198 1198
    }
1199 1199

	
1200 1200
    // Find the leaving arc of the cycle and returns true if the
1201 1201
    // leaving arc is not the same as the entering arc
1202 1202
    bool findLeavingArc() {
1203 1203
      // Initialize first and second nodes according to the direction
1204 1204
      // of the cycle
1205 1205
      if (_state[in_arc] == STATE_LOWER) {
1206 1206
        first  = _source[in_arc];
1207 1207
        second = _target[in_arc];
1208 1208
      } else {
1209 1209
        first  = _target[in_arc];
1210 1210
        second = _source[in_arc];
1211 1211
      }
1212 1212
      delta = _cap[in_arc];
1213 1213
      int result = 0;
1214 1214
      Value d;
1215 1215
      int e;
1216 1216

	
1217 1217
      // Search the cycle along the path form the first node to the root
1218 1218
      for (int u = first; u != join; u = _parent[u]) {
1219 1219
        e = _pred[u];
1220 1220
        d = _forward[u] ?
1221 1221
          _flow[e] : (_cap[e] == INF ? INF : _cap[e] - _flow[e]);
1222 1222
        if (d < delta) {
1223 1223
          delta = d;
1224 1224
          u_out = u;
1225 1225
          result = 1;
1226 1226
        }
1227 1227
      }
1228 1228
      // Search the cycle along the path form the second node to the root
1229 1229
      for (int u = second; u != join; u = _parent[u]) {
1230 1230
        e = _pred[u];
1231 1231
        d = _forward[u] ? 
1232 1232
          (_cap[e] == INF ? INF : _cap[e] - _flow[e]) : _flow[e];
1233 1233
        if (d <= delta) {
1234 1234
          delta = d;
1235 1235
          u_out = u;
1236 1236
          result = 2;
1237 1237
        }
1238 1238
      }
1239 1239

	
1240 1240
      if (result == 1) {
1241 1241
        u_in = first;
1242 1242
        v_in = second;
1243 1243
      } else {
1244 1244
        u_in = second;
1245 1245
        v_in = first;
1246 1246
      }
1247 1247
      return result != 0;
1248 1248
    }
1249 1249

	
1250 1250
    // Change _flow and _state vectors
1251 1251
    void changeFlow(bool change) {
1252 1252
      // Augment along the cycle
1253 1253
      if (delta > 0) {
1254 1254
        Value val = _state[in_arc] * delta;
1255 1255
        _flow[in_arc] += val;
1256 1256
        for (int u = _source[in_arc]; u != join; u = _parent[u]) {
1257 1257
          _flow[_pred[u]] += _forward[u] ? -val : val;
1258 1258
        }
1259 1259
        for (int u = _target[in_arc]; u != join; u = _parent[u]) {
1260 1260
          _flow[_pred[u]] += _forward[u] ? val : -val;
1261 1261
        }
1262 1262
      }
1263 1263
      // Update the state of the entering and leaving arcs
1264 1264
      if (change) {
1265 1265
        _state[in_arc] = STATE_TREE;
1266 1266
        _state[_pred[u_out]] =
1267 1267
          (_flow[_pred[u_out]] == 0) ? STATE_LOWER : STATE_UPPER;
1268 1268
      } else {
1269 1269
        _state[in_arc] = -_state[in_arc];
1270 1270
      }
1271 1271
    }
1272 1272

	
1273 1273
    // Update the tree structure
1274 1274
    void updateTreeStructure() {
1275 1275
      int u, w;
1276 1276
      int old_rev_thread = _rev_thread[u_out];
1277 1277
      int old_succ_num = _succ_num[u_out];
1278 1278
      int old_last_succ = _last_succ[u_out];
1279 1279
      v_out = _parent[u_out];
1280 1280

	
1281 1281
      u = _last_succ[u_in];  // the last successor of u_in
1282 1282
      right = _thread[u];    // the node after it
1283 1283

	
1284 1284
      // Handle the case when old_rev_thread equals to v_in
1285 1285
      // (it also means that join and v_out coincide)
1286 1286
      if (old_rev_thread == v_in) {
1287 1287
        last = _thread[_last_succ[u_out]];
1288 1288
      } else {
1289 1289
        last = _thread[v_in];
1290 1290
      }
1291 1291

	
1292 1292
      // Update _thread and _parent along the stem nodes (i.e. the nodes
1293 1293
      // between u_in and u_out, whose parent have to be changed)
1294 1294
      _thread[v_in] = stem = u_in;
1295 1295
      _dirty_revs.clear();
1296 1296
      _dirty_revs.push_back(v_in);
1297 1297
      par_stem = v_in;
1298 1298
      while (stem != u_out) {
1299 1299
        // Insert the next stem node into the thread list
1300 1300
        new_stem = _parent[stem];
1301 1301
        _thread[u] = new_stem;
1302 1302
        _dirty_revs.push_back(u);
1303 1303

	
1304 1304
        // Remove the subtree of stem from the thread list
1305 1305
        w = _rev_thread[stem];
1306 1306
        _thread[w] = right;
1307 1307
        _rev_thread[right] = w;
1308 1308

	
1309 1309
        // Change the parent node and shift stem nodes
1310 1310
        _parent[stem] = par_stem;
1311 1311
        par_stem = stem;
1312 1312
        stem = new_stem;
1313 1313

	
1314 1314
        // Update u and right
1315 1315
        u = _last_succ[stem] == _last_succ[par_stem] ?
1316 1316
          _rev_thread[par_stem] : _last_succ[stem];
1317 1317
        right = _thread[u];
1318 1318
      }
1319 1319
      _parent[u_out] = par_stem;
1320 1320
      _thread[u] = last;
1321 1321
      _rev_thread[last] = u;
1322 1322
      _last_succ[u_out] = u;
1323 1323

	
1324 1324
      // Remove the subtree of u_out from the thread list except for
1325 1325
      // the case when old_rev_thread equals to v_in
1326 1326
      // (it also means that join and v_out coincide)
1327 1327
      if (old_rev_thread != v_in) {
1328 1328
        _thread[old_rev_thread] = right;
1329 1329
        _rev_thread[right] = old_rev_thread;
1330 1330
      }
1331 1331

	
1332 1332
      // Update _rev_thread using the new _thread values
1333 1333
      for (int i = 0; i < int(_dirty_revs.size()); ++i) {
1334 1334
        u = _dirty_revs[i];
1335 1335
        _rev_thread[_thread[u]] = u;
1336 1336
      }
1337 1337

	
1338 1338
      // Update _pred, _forward, _last_succ and _succ_num for the
1339 1339
      // stem nodes from u_out to u_in
1340 1340
      int tmp_sc = 0, tmp_ls = _last_succ[u_out];
1341 1341
      u = u_out;
1342 1342
      while (u != u_in) {
1343 1343
        w = _parent[u];
1344 1344
        _pred[u] = _pred[w];
1345 1345
        _forward[u] = !_forward[w];
1346 1346
        tmp_sc += _succ_num[u] - _succ_num[w];
1347 1347
        _succ_num[u] = tmp_sc;
1348 1348
        _last_succ[w] = tmp_ls;
1349 1349
        u = w;
1350 1350
      }
1351 1351
      _pred[u_in] = in_arc;
1352 1352
      _forward[u_in] = (u_in == _source[in_arc]);
1353 1353
      _succ_num[u_in] = old_succ_num;
1354 1354

	
1355 1355
      // Set limits for updating _last_succ form v_in and v_out
1356 1356
      // towards the root
1357 1357
      int up_limit_in = -1;
1358 1358
      int up_limit_out = -1;
1359 1359
      if (_last_succ[join] == v_in) {
1360 1360
        up_limit_out = join;
1361 1361
      } else {
1362 1362
        up_limit_in = join;
1363 1363
      }
1364 1364

	
1365 1365
      // Update _last_succ from v_in towards the root
1366 1366
      for (u = v_in; u != up_limit_in && _last_succ[u] == v_in;
1367 1367
           u = _parent[u]) {
1368 1368
        _last_succ[u] = _last_succ[u_out];
1369 1369
      }
1370 1370
      // Update _last_succ from v_out towards the root
1371 1371
      if (join != old_rev_thread && v_in != old_rev_thread) {
1372 1372
        for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ;
1373 1373
             u = _parent[u]) {
1374 1374
          _last_succ[u] = old_rev_thread;
1375 1375
        }
1376 1376
      } else {
1377 1377
        for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ;
1378 1378
             u = _parent[u]) {
1379 1379
          _last_succ[u] = _last_succ[u_out];
1380 1380
        }
1381 1381
      }
1382 1382

	
1383 1383
      // Update _succ_num from v_in to join
1384 1384
      for (u = v_in; u != join; u = _parent[u]) {
1385 1385
        _succ_num[u] += old_succ_num;
1386 1386
      }
1387 1387
      // Update _succ_num from v_out to join
1388 1388
      for (u = v_out; u != join; u = _parent[u]) {
1389 1389
        _succ_num[u] -= old_succ_num;
1390 1390
      }
1391 1391
    }
1392 1392

	
1393 1393
    // Update potentials
1394 1394
    void updatePotential() {
1395 1395
      Cost sigma = _forward[u_in] ?
1396 1396
        _pi[v_in] - _pi[u_in] - _cost[_pred[u_in]] :
1397 1397
        _pi[v_in] - _pi[u_in] + _cost[_pred[u_in]];
1398 1398
      // Update potentials in the subtree, which has been moved
1399 1399
      int end = _thread[_last_succ[u_in]];
1400 1400
      for (int u = u_in; u != end; u = _thread[u]) {
1401 1401
        _pi[u] += sigma;
1402 1402
      }
1403 1403
    }
1404 1404

	
1405 1405
    // Execute the algorithm
1406 1406
    ProblemType start(PivotRule pivot_rule) {
1407 1407
      // Select the pivot rule implementation
1408 1408
      switch (pivot_rule) {
1409 1409
        case FIRST_ELIGIBLE:
1410 1410
          return start<FirstEligiblePivotRule>();
1411 1411
        case BEST_ELIGIBLE:
1412 1412
          return start<BestEligiblePivotRule>();
1413 1413
        case BLOCK_SEARCH:
1414 1414
          return start<BlockSearchPivotRule>();
1415 1415
        case CANDIDATE_LIST:
1416 1416
          return start<CandidateListPivotRule>();
1417 1417
        case ALTERING_LIST:
1418 1418
          return start<AlteringListPivotRule>();
1419 1419
      }
1420 1420
      return INFEASIBLE; // avoid warning
1421 1421
    }
1422 1422

	
1423 1423
    template <typename PivotRuleImpl>
1424 1424
    ProblemType start() {
1425 1425
      PivotRuleImpl pivot(*this);
1426 1426

	
1427 1427
      // Execute the Network Simplex algorithm
1428 1428
      while (pivot.findEnteringArc()) {
1429 1429
        findJoinNode();
1430 1430
        bool change = findLeavingArc();
1431 1431
        if (delta >= INF) return UNBOUNDED;
1432 1432
        changeFlow(change);
1433 1433
        if (change) {
1434 1434
          updateTreeStructure();
1435 1435
          updatePotential();
1436 1436
        }
1437 1437
      }
1438 1438
      
1439 1439
      // Check feasibility
1440 1440
      for (int e = _search_arc_num; e != _all_arc_num; ++e) {
1441 1441
        if (_flow[e] != 0) return INFEASIBLE;
1442 1442
      }
1443 1443

	
1444 1444
      // Transform the solution and the supply map to the original form
1445 1445
      if (_have_lower) {
1446 1446
        for (int i = 0; i != _arc_num; ++i) {
1447 1447
          Value c = _lower[i];
1448 1448
          if (c != 0) {
1449 1449
            _flow[i] += c;
1450 1450
            _supply[_source[i]] += c;
1451 1451
            _supply[_target[i]] -= c;
1452 1452
          }
1453 1453
        }
1454 1454
      }
1455 1455
      
1456 1456
      // Shift potentials to meet the requirements of the GEQ/LEQ type
1457 1457
      // optimality conditions
1458 1458
      if (_sum_supply == 0) {
1459 1459
        if (_stype == GEQ) {
1460
          Cost max_pot = std::numeric_limits<Cost>::min();
1460
          Cost max_pot = -std::numeric_limits<Cost>::max();
1461 1461
          for (int i = 0; i != _node_num; ++i) {
1462 1462
            if (_pi[i] > max_pot) max_pot = _pi[i];
1463 1463
          }
1464 1464
          if (max_pot > 0) {
1465 1465
            for (int i = 0; i != _node_num; ++i)
1466 1466
              _pi[i] -= max_pot;
1467 1467
          }
1468 1468
        } else {
1469 1469
          Cost min_pot = std::numeric_limits<Cost>::max();
1470 1470
          for (int i = 0; i != _node_num; ++i) {
1471 1471
            if (_pi[i] < min_pot) min_pot = _pi[i];
1472 1472
          }
1473 1473
          if (min_pot < 0) {
1474 1474
            for (int i = 0; i != _node_num; ++i)
1475 1475
              _pi[i] -= min_pot;
1476 1476
          }
1477 1477
        }
1478 1478
      }
1479 1479

	
1480 1480
      return OPTIMAL;
1481 1481
    }
1482 1482

	
1483 1483
  }; //class NetworkSimplex
1484 1484

	
1485 1485
  ///@}
1486 1486

	
1487 1487
} //namespace lemon
1488 1488

	
1489 1489
#endif //LEMON_NETWORK_SIMPLEX_H
0 comments (0 inline)