# Undirected Graphs

I have recently started studying graphs and their different traversal algorithms, and can't seem to be able to come up with an answer to this question. I really need your help, I don't even know where to start. P.S. It was my birthday yesterday and I don't want to cry because of this problem.

A company in New York manufactures blue halogen bulbs for cars. Unfortunately, it is very difficult to color bulbs consistently. Naturally, it is also very important to package bulbs that look alike in pairs. To package bulbs in pairs, bulbs coming out of the assembly line are first partitioned into two sets of bulbs sharing similar colors (e.g., one set of darker bulbs, another set of lighter bulbs), and then pairs are formed within each set.

Because of increasing demand, the company wishes to hire more workers to partition bulbs into two sets. To determine whether applicants have appropriate skills to perform this rather delicate task, they are asked to take this simple test: Given a set of n bulbs, compare each pair of bulbs and determine whether two bulbs have “similar” or “different” colors. An applicant also has an option to say “indeterminate” for each pair. The company wishes to hire applicants who are consistent in their judgment. We say m judgments (resulted in either “similar” or “different”) are consistent if it is possible to partition n bulbs into two sets such that (i) for each pair {a,b} determined to be “similar”, a and b indeed belong to the same set, and (ii) for each pair {a,b} determined to be “different”, a and b indeed belong to different sets.

For a given test result for n bulbs with m judgments, design an O(n+m)-time algorithm to determine whether the judgments are consistent. In practice, there should be a minimum number of judgments applicants are required to make, but your algorithm should work for any integer m≥0.

Thank you so much!

• I think there is a typo "Given a set of nn bulbs" DId you mean 2n or mn or just n? Feb 13, 2017 at 3:58
• @AgnishomChattopadhyay Sorry it's a typo, just n bulbs! Feb 13, 2017 at 19:11

Problem: Given a set of rules specifying that a pair of bulbs are in the same set, decide if there is a way for the bulbs to be colored in such a way that they satisfy the rules.

Motivation: Dividing into two sets suspiciously looks like assigning truth values to literals. Hence, try reducing the problem to 2-SAT

(Informal) Reduction: Let's say there are two sets: light and dark. Furthermore, let's say that there are $n$ bulbs. $x_i$ is the claim that the $i$th bulb is light. The negation, $\overline{x_i}$, is therefore, the claim that the $i$th bulb is dark

The judgements could be interpretted this way:

• $i$ and $j$ are similar: $(x_i \implies x_j) \wedge (\overline{x_i} \implies \overline{x_j})$
• $i$ and $j$ are different: $(x_i \implies \overline{x_j}) \wedge (\overline{x_i} \implies x_j)$

This is indeed a 2-SAT problem because each implicative clause consists of just two literals. Now we have a way to put the bulbs into sets respecting the judgements iff there is a way to satisfy this implications.

Solution: 2-SAT is known to be solvable in linear time.

One way to do so is to convert the formula in question to an implication graph and then check whether there are any literals $x_i$ and its negation $\overline{x_i}$ in the same connected component. This could be done in linear time using Kosaraju's Algorithm