Editorial for Baltic OI '08 P2 - Gates
Submitting an official solution before solving the problem yourself is a bannable offence.
Let us denote the state of the switch by . We can think about as a Boolean variable, which is true when the switch is turned on. Using this approach, all we have to do is to find a valuation of these variables fulfilling task conditions. Moreover, we can easily transform the problem into the language of logic. We show it using the example from the task statement:
3 2
1 0 2 1
1 0 2 0
1 1 2 1
In this example, we have two switches. As a result, we will have two variables and . If we look at channel number one, we will see that switch number one has to be off or switch number two has to be on in order for this channel to be closed. For each channel, we can create a logical formula:
- channel 1:
- channel 2:
- channel 3:
Since we are looking for a configuration that closes all the channels, we have to make a conjunction of the constructed logical formulas:
Our task is to find a valuation of variables and that satisfies the above formula. In general, the problem of satisfying a given logical formula is known to be NP-complete. Fortunately, the formula obtained in this task is of a very special form:
where and are literals and each of them stands for some variable or negated variable . The literals and correspond to the gates on the channel. The formula is a conjunction of alternatives of exactly two literals. This form is called the second conjunctive normal form (2-CNF). We show that this problem can be solved in linear time.
For the remaining part of the analysis, we assume that our goal is to find such a valuation of variables , that the following formula is satisfied:
2-CNF Formulas and Graphs
Let us consider an undirected graph with vertices corresponding to all possible literals:
and edges connecting pairs of literals which appear in alternatives of the formula:
Graph is shown on the following figure.
Our goal is to select a subset containing vertices corresponding to literals which have to be true in order for the formula to be true. For that, the following conditions have to be satisfied:
- either or , for
- for every edge , or
Let us construct a directed graph using the definition of graph :
We will call it the inference graph because it can be used to find which vertices have to belong to , provided that some given vertex has already been included in it. The inference graph has vertices and at most edges. The inference for graph is shown below.
Looking at this graph, it is easy to notice that every , which is a correct solution to our problem, must satisfy the condition:
This logical formula is equivalent to condition 2. As a result, we can search for using the inference graph.
Let us denote
where means that there exists a path from vertex to vertex in graph . Using this definition, we can easily rewrite (1) as:
If a set contains two opposite vertices ( and ) we will call the vertex problematic.
Let us introduce another kind of undirected graph: a conflict graph. Using condition 2, we can say that for every edge of graph , it is not possible for and to be both in . We can say that these vertices are in conflict. Therefore, we can build a conflict graph where:
Conflict graph for our example is shown below.
Using these definitions and facts, we can show a solution to the problem.
Polynomial Solution
Using previously introduced definitions, we can solve this problem with the following method:
- while repeat
- let be a vertex such that and
- if both and are problematic then solution does not exist (stop the algorithm)
- let be a non-problematic vertex among and
- is a correct solution for our problem
It is not obvious that this algorithm always returns a correct answer. To see that, let us show the following lemma.
Lemma 1. Let be a set of vertices , such that and . If vertex is non-problematic then there is not an edge in the conflict graph between any pair of vertices from and .
Proof: Let be a vertex from and be a vertex from . If there was a conflict between and , there would have to be an edge in the inference graph . As a result, there would have to be: . But by the definition of . ■
This Lemma shows the correctness of the presented algorithm — setting the value of a certain variable in a way not leading to a contradiction does not affect the vertices not belonging to and thus leads to a proper solution, if one exists. This solution has the overall time complexity , as checking if a vertex is problematic may take time proportional to the size of the graph.
Model Solution
Let us think about strongly connected components of the inference graph. (Two vertices and belong to the same strongly connected component if and only if there is a path from to as well as from to ). Strongly connected components of an inference graph have a very useful property for us: for any component , either , or , as every vertex induces its whole component. As a result, we can consider the graph of components , whose vertices are strongly connected components of graph and edges are inherited from that graph in a natural way. There is a simple algorithm computing strongly connected components of a graph in time.
Obviously, the graph of components is a directed acyclic graph. We sort it topologically and consider its vertices in non-ascending order. This can be done in linear time as well.
We say that we accept a component when that component is chosen and included in while performing the algorithm. Similarly, we say that we reject a component if we decide not to choose the component. Note that if we reject a component, we have to reject all its predecessors as well. Similarly, if we accept a component, we have to accept all the components induced by it.
These observations lead to a more efficient version of the previous algorithm:
- Generate the inference graph .
- Find strongly connected components of and build a graph of components .
- If there are two opposite vertices in a component, reject this component (and all its predecessors).
- Sort the components topologically, and process them in non-ascending order:
- If the current component has not been rejected yet, accept it.
- For each vertex in the accepted component, reject the component containing the opposite vertex (and consequently all its predecessors).
- If exactly vertices have been accepted, they form a correct solution. Otherwise, a solution does not exist.
Because of the topological ordering of components, each time we accept a component , all components induced by have already been accepted. Indeed, if one of those components had been rejected before, then would also have been rejected as its predecessor. It is also clear that set does not contain any conflicts. As a result, if the algorithm finds a solution, it is correct. All we have to do is to show that if a solution exists, this algorithm will find it. It is a consequence of the previous algorithm and Lemma 1, as we reject all problematic vertices in the third step of our algorithm.
As we mentioned before, steps 1–3 can be done in linear time. Moreover, we accept or reject each component exactly once. Therefore, the fourth step also runs in linear time. Hence, the running time of the presented algorithm is .
Comments