Difference constraint Algorithm

Source: Internet
Author: User

(This article assumes that the reader has the following knowledge: the basic nature of the shortest path and the Bellman-Ford algorithm .)
For example, there is a set of inequalities:

X1-X2 <= 0
X1-X5 <=-1
X2-X5 <= 1
X3-X1 <= 5
X4-X1 <= 4
X4-X3 <=-1
X5-X3 <=-3
X5-X4 <=-3

Inequality group (1)


All are TwoUnknown Less than or equalA constant (or greater than or equal to, because it can be converted to less than or equal by multiplying the left and right by-1 ). Such an inequality group is called a difference constraint system.
This inequality group either has no solution or has no array solution. Because if there is a group of solutions {x1, x2 ,..., xn}, then for any constant K, {x1 + k, X2 + k ,..., xn + k} must also be a group of solutions. Because the difference between any two numbers plus one number at the same time remains unchanged, all inequalities in the difference constraint system will not be damaged.

The solution of the differential constraint system utilizes the triangle inequality in the single-source shortest path problem. That is, for any side u-> V, there are:
D (v) <= d (u) + W (u, v)

Where d (u) and D (v) are the weights of the shortest paths from the source point to the Point U and the point V respectively, w (u, v) is the weight of edge U-> v.
Obviously, the above inequality is d (v)-d (u) <= W (u, v ). This form is exactly the same as the inequality form in the difference constraint system. So we can convert a difference constraint system into a graph. Each unknown Xi corresponds to a vertex VI in the graph, and all the inequalities are converted into an edge in the graph. For the inequality Xi-XJ <= C, convert it into a triangle inequality: xi <= XJ + C, you can convert it into an edge VJ-> VI, and the weight is C. Finally, we can find a single-source shortest path in this figure. These triangle inequalities will all meet the requirements, because it is the basic nature of the shortest path problem.
In other words, the single-source shortest path requires a source point, and then calculates the shortest path from this source point to all other points. So where is the source point? We may wish to create one by ourselves. The above inequality group is used as an example. We will add an unknown x0. Then add an inequality to x0 for each unknown. (This inequality must be in the same form as other inequalities, that is, the difference between the two unknowns is less than or equal to a constant ). We simply write them all into Xn-x0 <= 0, so the difference constraint system has the following inequality:

X1-x0 <= 0
X2-x0 <= 0
X3-x0 <= 0
X4-x0 <= 0
X5-x0 <= 0

Inequality group (2)


For these five inequalities, the corresponding edge is also created in the graph. The final figure is as follows:


Figure 1

Each edge in the figure represents an inequality in the difference constraint system. Now, we use V0 as the source point to find the single-source shortest path. The shortest path length from V0 to VN is a solution of XN. As shown in figure 1, the solutions for this group are {-5,-3, 0,-1,-4 }. Adding 10 to each number is also a group of solutions: {5, 7, 10, 9, 6 }. However, this group of solutions only satisfy the inequality group (1), that is, the original differential constraint system, but not the inequality group (2), that is, the inequalities we added later. Of course, this is irrelevant, because x0 was originally an outsider and we added it later. We don't care if it does not satisfy the inequality related to x0.
There may also be no solution, that is, there is no shortest path from the source point to a vertex. In other words, there is a negative weight circle in the figure. I will not expand this point. Please refer to the basic theorem of the shortest path problem.

In fact, for Figure 1, it represents a group of solutions {0,-5,-3, 0,-1,-4 }, that is to say, the value of x0 is also in this group of solutions. However, the value of x0 is undisputed. Since it serves as the shortest path for the source point, the shortest path length from the source point to it is of course 0. Therefore, the difference constraint system we actually solve has another condition:

X0 = 0

That is to say, in the case of a differential constraint system composed of (1) and (2), the value of an unknown number is fixed to death. This is a common problem. For example, there are some inequalities on the surface of a problem, but some inequalities are hidden. For example, all unknown numbers must be greater than or equal to 0 or cannot exceed a certain upper limit. For example, the inequality group (2) above specifies that all unknown numbers are less than or equal to 0.

There is also an interesting property for this type of differential constraint system with an unknown number, that is, all the unknown numbers reach the maximum value in a group of solutions obtained through the shortest path algorithm. Next I will give a rough proof that this process should be explained in conjunction with the Bellman-Ford algorithm.
Assume that x0 is fixed. When X1 to XN meet all constraints, the maximum values that can be obtained are M1, M2 ,...... , Mn (of course we don't know what their values are); the shortest path length from the source point to each point is D1, D2 ,...... , DN.
The basic Bellman-Ford algorithm initializes D1 to DN at the beginning and is infinite. Then, check the Triangle Inequality corresponding to all edges. Once a triangle inequality is discovered, the corresponding D value is updated. The obtained D1 to DN is the shortest path length from the source point to each point.
If we initialize D1, D2 ,...... And dn values are M1, M2 ,...... AND Mn, then because they all satisfy the Triangle Inequality (we have assumed that M1 to Mn is a combination of solutions), then the Bellman-Ford algorithm will no longer update any combination of D values, then the final solution is M1, M2 ,...... AND Mn.
Now, when the initial value is infinite, the calculated values are D1, D2 ,...... , DN; when the initial value is relatively small, the calculated values are M1, M2 ,...... AND Mn. In most cases, the same algorithm is used. In the same computing process, it is impossible to calculate the results with a large initial value, rather than small values. So D1, D2 ,...... DN is M1, M2 ,...... AND Mn.

So what if the minimum values of all other unknown values are required when an unknown number is fixed? You only need to find the longest path in turn. The triangle inequality in the longest path is opposite to that in the shortest path:
D (v)> = d (u) + W (u, v)
That is, d (v)-d (u)> = W (u, v)

Therefore, when creating a graph, you must first convert all the inequalities into those greater than or equal to numbers. Other processes, including the proof of why the minimum value is solved, are completely similar.

I wish you good luck with zju 2770, which is used in the differential constraint system.

 

Difference constraint Algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.