Consider how you can apply network flow to this problem. It should be something like a stream going from the head of the crab to its legs. And the flow coming to the head should have a value equivalent to the number of legs, and each edge from the head to the legs of the crab should have a capacity of one.
How can we achieve this? It is definitely difficult to come to this on our own, but I hope that by seeing the example several times, you can hang it.
We need to create a new graph where the original vertices are duplicated. One set can give each vertex a chance to be a head, and another set can act like legs. With this in mind, the exact algorithm can be written like this: -
1. We create a new graph where original vertices are duplicated (vertex i becomes 2*i (head set) and 2*i+1 (feet set) ). 2.For i and j vertices connected in original graph make sure that 2*i and 2*j+1 plus 2*j and 2*i+1 gets connected in new graph with capacity 1. 3.source vertex (S) is connected to each 2*i vertex(head set) with capacity min(T, degree). 4. Each 2*i+1(feet set) vertex is connected to target vertex (T) with capacity 1 5. Maxflow is the answer.
The image below can give a decent idea of ββhow the graph is formed. Creating a new schedule
Explanation of paragraph 3: - each vertex in the headset must be connected to a source with a power of min (t, degree), because if we wanted the maximum flow on this edge to be as large as T, no more than that, that, since a crab cannot have more than t feet, and the cost of power here is also limited by the degree of the vertex to which it is connected 0. The head cannot have more legs than its degree.
Explanation of paragraph 4: - So that the pairs do not intersect, so that each vertex comes only on one crab, each leg is connected with a thickness of 1 to target 10 in the figure.
I can post the code if necessary.
source share