Guest User

Untitled

a guest
Jun 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.46 KB | None | 0 0
  1. The inverse problem of MST (inverse-MST - minimum spanning tree inverse problem) in O (N M2)
  2.  
  3. Given a weighted undirected graph G with N vertices and M edges (without loops and multiple edges). It is known that the graph is connected. Also, given a skeleton T of the graph (ie, selected the N-1 rib, which form a tree with N vertices). Want to change the weights of the edges so that the skeleton of T is specified by the minimum spanning tree of this graph (more precisely, one of the minimum spanning tree), and to do this so that the total change of all was the smallest scales.
  4. Solution
  5.  
  6. We reduce the problem of inverse-MST problem to min-cost-flow, more precisely, the problem of the dual min-cost-flow (in the sense of duality of linear programming problems), then solve the latter problem.
  7.  
  8. Thus, suppose that a graph G with N vertices, M edges. The weight of each edge is denoted by Ci. We assume without loss of generality that the edges with numbers from 1 to N-1 are edges T.
  9. A. A necessary and sufficient condition for the MST
  10.  
  11. Suppose we are given a skeleton of S (not necessarily minimal).
  12.  
  13. We first introduce notation. Consider some edge j, not belonging to S. Obviously, the graph S has only one path connecting the ends of this edge, ie the only path that connects the ends of j and consisting only of edges belonging to S. We denote by P [j] the set of edges forming the path for the j-th rib.
  14.  
  15. In order to be a skeleton of S is minimal if and only if:
  16.  
  17. Ci <= Cj for all j ∉ S and every i ∈ P [j]
  18.  
  19. It may be noted that, since in our problem belongs to the edge skeleton of T 1 .. N-1, then we can write this condition as follows:
  20.  
  21. Ci <= Cj for all j = N.. M and every i ∈ P [j]
  22. (And all the i lie in the range 1 .. N-1)
  23.  
  24. Two. Count the ways
  25.  
  26. The notion of graph paths is directly related to the previous theorem.
  27.  
  28. Suppose we are given a skeleton of S (not necessarily minimal).
  29.  
  30. Then count the ways H for a graph G is the following graph:
  31.  
  32. * It contains M vertices, each vertex in H bijectively corresponds to some edge in G.
  33. * The graph H is bipartite. In the first of its share are node i, which correspond to the edges of G, belonging to the skeleton S. Accordingly, in the second part are the top j, which correspond to the edges not belonging to S.
  34. * An edge is drawn from vertex i to vertex j if and only if i belongs to P [j].
  35. In other words, for each vertex j of the second part it consists of edges from all vertices of the first part of the set of edges of P [j].
  36.  
  37. In the case of our problem, we can simplify the description of some ways to graph:
  38.  
  39. edge (i, j) exists in H, if i ∈ P [j], j = N.. M, i = 1 .. N-1
  40.  
  41. Three. The mathematical formulation of the problem
  42.  
  43. Purely formal inverse-MST problem can be written as follows:
  44.  
  45. find an array A [1 .. M] such that
  46. Ci + Ai <= Cj + Aj for all j = N.. M and every i ∈ P [j] (i in 1 .. N-1)
  47. and minimize the amount of | A1 | + | A2 | + ... + | Am |
  48.  
  49. Here at the desired array A, we mean those values ​​that you want to add to the weights of the edges (ie, solving the problem inverse-MST, we replace each edge weight of Ci i on the value of Ci + Ai).
  50.  
  51. Obviously, it makes no sense to increase the weight of edges belonging to T, ie
  52.  
  53. Ai <= 0, i = 1 .. N-1
  54.  
  55. and does not make sense to shorten the edges not belonging to T:
  56.  
  57. Ai> = 0, i = N.. M
  58.  
  59. (Because otherwise we will only worsen the response)
  60.  
  61. Then we can slightly simplify the problem by removing the sum of the following modules:
  62.  
  63. find an array A [1 .. M] such that
  64. Ci + Ai <= Cj + Aj for all j = N.. M and every i ∈ P [j] (i in 1 .. N-1)
  65. Ai <= 0, i = 1 .. N-1,
  66. Ai> = 0, i = N.. M,
  67. and minimize the amount of An + ... + Am - (A1 + ... + An-1)
  68.  
  69. Finally, just change "minimize" to "maximize", and in the amount of change all the signs to the contrary:
  70.  
  71. find an array A [1 .. M] such that
  72. Ci + Ai <= Cj + Aj for all j = N.. M and every i ∈ P [j] (i in 1 .. N-1)
  73. Ai <= 0, i = 1 .. N-1,
  74. Ai> = 0, i = N.. M,
  75. and maximize the amount of A1 + ... + An-1 - (An + ... + Am)
  76. 4. Reducing the problem to the inverse-MST problem, the dual problem of the assignment
  77.  
  78. Formulation of the inverse-MST, we just gave is the formulation of a linear programming problem with unknown A1 .. Am.
  79.  
  80. We apply the classical technique - consider its dual problem.
  81.  
  82. By definition, to obtain the dual problem, you need to compare each inequality, the dual variable Xij, interchange the roles of the objective function (which had to be minimized) and the coefficients on the right side, change the characters "<=" on "> =", and vice versa, change to the maximization minimization.
  83.  
  84. Thus, the dual to the inverse-MST problem:
  85.  
  86. find all Xij for each (i, j) ∈ H, such that:
  87. all Xij> = 0,
  88. for each i = 1 .. N-1 Σ Xij for all j: (i, j) ∈ H <= 1,
  89. for each j = N.. M Σ Xij for all i: (i, j) ∈ H <= 1,
  90. and to minimize Σ Xij (Cj - Ci) for all (i, j) ∈ H
  91.  
  92. The latter task is a task assignment: we need the graph to select multiple paths H edges so that none of the edges does not interfere with others in the top, and the sum of weights of edges (edge ​​weights (i, j) is defined as Cj - Ci) be the smallest .
  93.  
  94. Thus, the dual problem of inverse-MST is equivalent to the assignment. If we can solve the dual problem of the assignment, we will automatically solve the problem of inverse-MST.
  95. Five. The solution of the dual assignment problem
  96.  
  97. First, will pay little attention to the particular case of the assignment problem, which we have received. First, it is unbalanced assignment problem, because one portion is N-1 vertices, and another - M vertices, ie in general, the number of vertices in the second one more than an order of magnitude. To solve such a dual assignment problem is a special algorithm that decides it for O (N3), but here, this algorithm will not be considered. Second, such a task assignment can be called a task assignment with weighted vertices: the edge weights are set equal to 0, the weight of each vertex of the first part is set equal to-Ci, from the second part - equal to Cj, and the solution of the resulting problem will be the same.
  98.  
  99. We will solve the problem of the dual problem of the assignment using a modified algorithm for min-cost-flow, which will be to find the minimum cost flow and simultaneously a solution of the dual problem.
  100.  
  101. Reduce the problem of appointments to the task of min-cost-flow very easily, but for the sake of completeness we describe this process.
  102.  
  103. Add to the graph source s and drain t. From s to each vertex of the first part will hold an edge with capacity 1 and cost = 0. From each vertex of the second part will hold an edge to t with capacity 1 and cost = 0. Capacities of all edges between the first and second installments are also set equal to 1.
  104.  
  105. Finally, the modified algorithm to min-cost-flow (described below) to work, you need to add an edge from s to t with capacity = N +1 and the value 0.
  106. 6. A modified algorithm for min-cost-flow solutions for the assignment problem
  107.  
  108. Here we consider the successive shortest path algorithm with potentials, which resembles the usual algorithm min-cost-flow, but also uses the concept of potential, which by the end of the algorithm will contain the solution to the dual problem.
  109.  
  110. We introduce some notation. For each edge (i, j) is denoted by Uij its capacity through Cij - the cost, through Fij - flow along this edge.
  111.  
  112. Also, we introduce the notion of potential. Each vertex has its own potential PIi. The residual value of an edge CPIij is defined as:
  113.  
  114. CPIij = Cij - PIi + PIj
  115.  
  116. At any time of the algorithm capabilities are such that the following conditions:
  117.  
  118. if Fij = 0, then CPIij> = 0
  119. if Fij = Uij, then CPIij <= 0
  120. CPIij = 0 otherwise
  121.  
  122. The algorithm starts with a zero flow, and we need to find some of the initial values ​​of the potentials that satisfy the above conditions. It is easy to verify that this method is one of the possible solutions:
  123.  
  124. PIj = 0 for j = N.. M
  125. PIi = min Cij, where (i, j) ∈ H
  126. PIs = min PIi, where i = 1 .. N-1
  127. PIt = 0
  128.  
  129. Actually the algorithm min-cost-flow consists of several iterations. At each iteration, we find the shortest path from s to t in residual network, and the weights of the edges using the residual value of CPI. Then we increase the flow along the path found by one, and update capabilities as follows:
  130.  
  131. PIi - = Di
  132.  
  133. where Di - found the shortest distance from s to i (again, in the residual network with edge weights CPI).
  134.  
  135. Sooner or later we will find the path from s to t, which consists of a single edge (s, t). Then, after this iteration, we need to complete the work of the algorithm: indeed, unless we stop the algorithm, it will already be on the way to a non-negative value, and add them to the answer is not necessary.
  136.  
  137. By the end of the algorithm, we obtain the solution of the assignment problem (in the form of flow Fij) and the solution of the dual assignment problem (in the array PIi).
  138.  
  139. (C PIi will need to spend a little modification, from all the values ​​PIi take PIs, because its values ​​have meaning only when PIs = 0)
  140. 6. Total
  141.  
  142. So, we decided to dual task assignment, and, consequently, the problem of inverse-MST.
  143.  
  144. We estimate the asymptotic behavior of the resulting algorithm.
  145.  
  146. First, we must construct a graph paths. To do this simply for each edge j ∉ T preorder traversal of the skeleton of T we find the path P [j]. Then we construct a graph of paths in O (M) * O (N) = O (NM).
  147.  
  148. Then we find the initial values ​​of the potentials for the O (N) * O (M) = O (NM).
  149.  
  150. Then we iterate min-cost-flow, all iterations will be no more than N (as N goes from the source of the ribs, each with a bandwidth = 1) at each iteration, we are looking at ways to graph the shortest path from source to all other vertices. Since the vertices in the graph paths is M +2, and the number of edges - O (NM), then, if you implement search for the simplest version of the shortest path Dijkstra algorithm, each iteration of the min-cost-flow will perform for the O (M2), and the whole algorithm min -cost-flow will be executed in O (N M2).
  151.  
  152. The resulting asymptotic behavior of the algorithm is O (N M2).
  153. Implementation
  154.  
  155. We sell all of the above algorithm. The only change - instead of algorithm Dijkstra algorithm is used Leviticus, which in many tests should run slightly faster.
  156.  
  157. const int INF = 1000 * 1000 * 1000;
  158.  
  159. struct rib {
  160. int v, c, id;
  161. };
  162.  
  163. struct rib2 {
  164. int a, b, c;
  165. };
  166.  
  167. int main () {
  168.  
  169. int n, m;
  170. cin >> n >> m;
  171. vector <vector <rib>> g (n); / / graph in adjacency list format
  172. vector <rib2> ribs (m); / / all edges in one list
  173. ... Reading a graph ...
  174.  
  175. int nn = m +2, s = nn-2, t = nn-1;
  176. vector <vector <int>> f (nn, vector <int> (nn));
  177. vector <vector <int>> u (nn, vector <int> (nn));
  178. vector <vector <int>> c (nn, vector <int> (nn));
  179. for (int i = n-1; i <m; + + i) {
  180. vector <int> q (n);
  181. int h = 0, t = 0;
  182. rib2 & cur = ribs [i];
  183. q [t + +] = cur.a;
  184. vector <int> rib_id (n, -1);
  185. rib_id [cur.a] = -2;
  186. while (h <t) {
  187. int v = q [h + +];
  188. for (size_t j = 0; j <g [v]. size (); + + j)
  189. if (g [v] [j]. id> = n-1)
  190. break;
  191. else if (rib_id [g [v] [j]. v] == -1) {
  192. rib_id [g [v] [j]. v] = g [v] [j]. id;
  193. q [t + +] = g [v] [j]. v;
  194. }
  195. }
  196. for (int v = cur.b, pv; v! = cur.a; v = pv) {
  197. int r = rib_id [v];
  198. pv = v! = ribs [r]. a? ribs [r]. a: ribs [r]. b;
  199. u [r] [i] = n;
  200. c [r] [i] = ribs [i]. c - ribs [r]. c;
  201. c [i] [r] =-c [r] [i];
  202. }
  203. }
  204. u [s] [t] = n +1;
  205. for (int i = 0; i <n-1; + + i)
  206. u [s] [i] = 1;
  207. for (int i = n-1; i <m; + + i)
  208. u [i] [t] = 1;
  209.  
  210. vector <int> pi (nn);
  211. pi [s] = INF;
  212. for (int i = 0; i <n-1; + + i) {
  213. pi [i] = INF;
  214. for (int j = n-1; j <m; + + j)
  215. if (u [i] [j])
  216. pi [i] = min (pi [i], ribs [j]. c-ribs [i]. c);
  217. pi [s] = min (pi [s], pi [i]);
  218. }
  219.  
  220. for (; ;) {
  221. vector <int> id (nn);
  222. deque <int> q;
  223. q.push_back (s);
  224. vector <int> d (nn, INF);
  225. d [s] = 0;
  226. vector <int> p (nn, -1);
  227. while (! q.empty ()) {
  228. int v = q.front (); q.pop_front ();
  229. id [v] = 2;
  230. for (int i = 0; i <nn; + + i)
  231. if (f [v] [i] <u [v] [i]) {
  232. int new_d = d [v] + c [v] [i] - pi [v] + pi [i];
  233. if (new_d <d [i]) {
  234. d [i] = new_d;
  235. if (id [i] == 0)
  236. q.push_back (i);
  237. else if (id [i] == 2)
  238. q.push_front (i);
  239. id [i] = 1;
  240. p [i] = v;
  241. }
  242. }
  243. }
  244. for (int i = 0; i <nn; + + i)
  245. pi [i] - = d [i];
  246. for (int v = t; v! = s; v = p [v]) {
  247. int pv = p [v];
  248. + + F [pv] [v], - f [v] [pv];
  249. }
  250. if (p [t] == ​​s) break;
  251. }
  252.  
  253. for (int i = 0; i <m; + + i)
  254. pi [i] - = pi [s];
  255. for (int i = 0; i <n-1; + + i)
  256. if (f [s] [i])
  257. ribs [i]. c + = pi [i];
  258. for (int i = n-1; i <m; + + i)
  259. if (f [i] [t])
  260. ribs [i]. c + = pi [i];
  261.  
  262. ... the output graph ...
  263.  
  264. }
Add Comment
Please, Sign In to add comment