skip to content

questions

19

Why can a conflict graph be checked for two-colourability in linear time when three-colourability is NP-complete?

level: juniorimportance: must knowfreq 66%

answer

  1. which side of the feasibility line
  2. colour one vertex, then propagate
  3. forced consequence versus free choice
  4. odd cycle is the whole obstruction
  5. the third colour is what bites

basics

~20 s

Two colours leave no freedom: fix one vertex and propagation forces every other, so one traversal either succeeds or exposes an odd cycle. A third colour restores a choice at each vertex, and those choices interact globally.

solid answer

~40 s

With two colours the problem is not a search at all. Pick any vertex, give it colour A, and every neighbour is forced to B, their neighbours back to A, and so on; a breadth-first traversal propagates this through a whole component in `O(V + E)`. Exactly one thing can go wrong — an edge joining two vertices the traversal has already put in the same class — and that edge closes an odd cycle, the only obstruction to two-colourability. With three colours, fixing a vertex leaves each neighbour two options rather than one, nothing propagates, and the consequence of a choice can surface arbitrarily far away. Deciding three-colourability is NP-complete, and it stays NP-complete even on planar graphs whose vertices have at most four neighbours.

code

pseudocode · 12 lines
pseudocode
for each vertex s with colour unset:
    colour[s] = A
    queue = [s]
    while queue not empty:
        v = pop(queue)
        for each neighbour u of v:
            if colour[u] is unset:
                colour[u] = opposite(colour[v])
                push(queue, u)
            else if colour[u] == colour[v]:
                return NOT_TWO_COLOURABLE   // this edge closes an odd cycle
return TWO_COLOURABLE

go deeper

for a junior

Hold the pair the right way round: two colours is a linear traversal, three or more is NP-complete. Being able to say which side a requirement lands on is most of the value here.

for a middle

Explain the propagation argument — one vertex fixed forces all the rest — and name the odd cycle as the only obstruction. Then say precisely what a third colour changes: a free choice at every vertex that no local rule settles.

for a senior

Show that you check the colour count before quoting a complexity. Given a scheduling requirement, say which restriction would move it to the easy side, and test that before anyone reaches for heavy machinery.

for a principal

The judgment is whether to negotiate the requirement rather than solve it. A two-group split with a small exception list is often cheaper to operate than an exact three-group assignment nobody can guarantee in the worst case.

## What the two questions actually ask A **conflict graph** puts one vertex per session and one edge between every pair of sessions that cannot share a room or a slot. A **k-colouring** labels each vertex with one of `k` colours so that no edge joins two vertices carrying the same colour; here a colour is a slot. Two questions then look like the same question with a different number in it: - *Can this programme run in two slots?* - *Can it run in three?* They are not the same question. The first is settled by one traversal of the graph. The second is **NP-complete**, meaning no algorithm is known that settles it in time polynomial in the size of the graph, and finding one would settle every problem in NP at once. The gap has nothing to do with how big the graph is; it is created by the number of colours. ## Two colours: every choice is forced Give any vertex colour A. Every neighbour must then be B, every neighbour of those must be A again, and so on. There is never a moment of choice, so there is nothing to search: - The colour of every vertex in a connected component is determined by the colour of the first vertex, up to swapping the two colour names. - A breadth-first traversal assigns colours by the parity of the level a vertex sits on: even levels A, odd levels B. - Only one thing can go wrong — the traversal meets an edge whose two ends already carry the same colour. Both ends then sit at the same parity from the start vertex, so the edge closes a cycle of **odd** length. - An odd cycle is the *only* obstruction. A graph containing none can always be two-coloured. So a failure is not "my heuristic gave up"; it is a proof of impossibility, and the odd cycle is a short witness you can print. - Total work is `O(V + E)` — one pass, no backtracking, no re-visits. That is the shape of every genuinely easy constraint problem: a local rule that turns a decision into a forced consequence, plus a single clean reason for failure. ## Three colours: the choice comes back Colour a vertex A, and each neighbour now has two admissible colours, B or C. Propagation halts at the very first branch. Worse, the consequences are not local: choosing B here can make a far-away subgraph impossible to finish, and nothing visible at either end says so. The decision is NP-complete, and the hardness survives severe restriction — it remains NP-complete for **planar** graphs in which no vertex has more than four neighbours. So "our conflicts are drawn on a floor plan" and "nothing conflicts with more than four other things" do not rescue the instance. There is also no cheap certificate for a *negative* answer the way an odd cycle certifies the two-colour case. Three-colourability can fail for reasons spread over the whole graph rather than concentrated in one small structure. Two traps worth naming, because both are common: 1. **"No triangles, so three colours are enough."** False. Graphs with no triangle at all can be built that require arbitrarily many colours. 2. **"NP-complete means instances are unsolvable."** No. It is a worst-case statement about a family of inputs; particular instances are often settled instantly. ## Where the line sits | Question about the conflict graph | Cost | What decides it | |---|---|---| | Do two slots suffice? | `O(V + E)` | Parity of cycles, found by one traversal | | Do three slots suffice? | NP-complete | No local characterisation is known | | Does any fixed number of slots above two suffice? | NP-complete | The same branching argument applies | | What is the smallest number of slots? | At least as hard as the decision | Optimising over the same choices | ## Reading it back into the schedule The practical value of this pair is not the algorithm; it is the reflex of asking *how many colours* before quoting a cost. In a requirement that reads "split these into two non-conflicting groups", you can promise an exact answer and a reason when it is impossible. In a requirement that reads "fit these into three rooms", you cannot promise an exact answer in general, and the honest engineering conversation moves to what you do instead — a different subject with its own trade-offs. Getting the two cases the right way round, and saying which one a requirement has landed in, is most of what an interviewer is checking.

  • What single structure certifies that a graph cannot be two-coloured, and how does the traversal find it?
    An odd-length cycle. The traversal gives every vertex a parity relative to its start vertex, and an edge whose two ends share that parity closes a cycle of odd length. Walking the two tree paths back to their common ancestor prints the cycle, so the negative answer arrives with a short checkable witness rather than an exhausted search.
  • Is deciding that a graph needs exactly three colours the same problem as deciding that three colours suffice?
    Not literally, though the two stand or fall together. "Three suffice" is three-colourability. "Exactly three" means three-colourable and not two-colourable, and the second half is the cheap traversal — so the two questions differ by a linear-time test. Interviewers use the distinction to check that you pin down which version of a problem you are classifying.

saying these in an interview costs you the question

  • Says graph colouring is NP-complete regardless of how many colours are allowed.
  • Claims deciding two colours needs backtracking like the general case does.
  • Thinks a graph without triangles can always be coloured with three colours.
  • Believes NP-complete means no three-colouring instance is ever settled quickly.
  • Blames graph size for the hardness instead of the number of colours allowed.
open as a page

Cook-Levin proved satisfiability NP-complete without reducing from an earlier complete problem - why was there no alternative?

level: middleimportance: must knowfreq 62%

basics

~20 s

A hardness proof normally transfers hardness from a problem already known complete, and at the time none existed. Cook-Levin therefore argued about every problem in NP at once, encoding an arbitrary verifier's computation directly as a Boolean formula.

open as a page

A reviewer calls a shift-assignment problem NP-hard; which problems show that is weaker than NP-complete?

level: middleimportance: must knowfreq 58%

basics

~20 s

NP-hard only says everything in NP reduces into the problem; it does not place the problem in NP. Optimization phrasings, which are not decision problems, and the halting problem, which is undecidable, are NP-hard and outside NP.

open as a page

A design document calls a shift-assignment feature NP-complete. What two obligations must that claim discharge?

level: middleimportance: must knowfreq 66%

basics

~20 s

An NP-completeness claim owes two proofs: membership, that a short certificate for a yes answer is checkable in polynomial time, and hardness, that an already-complete problem reduces into it. Proving only the second gives NP-hardness, not completeness.

open as a page

Partition asks whether parcel weights split evenly between two trucks; why doesn't an O(n*T) table over totals put it in P?

level: middleimportance: must knowfreq 62%

basics

~10 s

An O(n*T) table is pseudo-polynomial: T is a numeric value carried by only about log T digits, so the table is exponential in the instance's written length, not polynomial in it.

open as a page

Why is 2-SAT decidable in linear time when 3-SAT, one literal wider per clause, is NP-complete?

level: middleimportance: must knowfreq 55%

basics

~20 s

A two-literal clause is a pair of implications: falsify one literal and the other is forced, so 2-SAT becomes a reachability question answered by strongly connected components in linear time. A three-literal clause forces nothing; it leaves a choice.

open as a page

How does the Cook-Levin proof turn an arbitrary polynomial-time verifier's whole run into one Boolean formula?

level: seniorimportance: must knowfreq 50%

basics

~20 s

It writes the run out as a grid with one row per step and one column per tape square, gives each cell a Boolean variable for every symbol it could hold, and adds clauses forcing a legal, accepting history.

open as a page

Knapsack is stated as maximise the value loaded, yet NP-completeness is defined for yes-or-no problems, so how is Knapsack made one?

level: middleimportance: should knowfreq 46%

basics

~20 s

Add a threshold. The decision form asks whether some selection fits the capacity and reaches at least value K. That yes-or-no form is NP-complete, and a threshold oracle recovers the optimum by binary search over K.

open as a page

In a conflict graph, how do maximum independent set, maximum clique and minimum vertex cover relate?

level: middleimportance: should knowfreq 44%

basics

~20 s

They are one problem in three costumes. A conflict-free set of sessions is an independent set; the vertices left out of it form a vertex cover; and the same set is a clique in the complemented graph. Solve one exactly, solve all three.

open as a page

The formula Cook-Levin builds has long clauses; how do you shrink them to three literals without changing satisfiability?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Chain the clause through fresh variables: a clause of k literals becomes k-2 three-literal clauses linked by k-3 new variables. Every solution of the original extends to one of the rewrite, and every solution of the rewrite restricts back.

open as a page

A team's only evidence is an exponential-time algorithm for shift assignment; why does that not prove NP-hardness?

level: seniorimportance: should knowfreq 45%

basics

~20 s

An algorithm bounds one solution's cost from above; hardness is a claim about every possible algorithm. Only a polynomial-time reduction from an already-complete problem establishes it, and plenty of problems with exponential brute force turn out to be polynomial.

open as a page

Why is minimising when the later of two loading crews finishes the same hard problem as splitting parcel weights evenly?

level: seniorimportance: should knowfreq 38%

basics

~20 s

The two crew loads always sum to the fixed total S, so the later finish is at least S/2 and equals it exactly when the weights split into two halves of S/2. Solving the schedule therefore answers Partition.

open as a page

A cleaning route must cover every corridor once; the revised route must visit every room once — why does the second become NP-complete?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Covering every edge once has a local test: each pass through a vertex uses two edge-ends, so every vertex needs even degree, and one component plus even degrees is enough. Visiting every vertex once has no such local characterisation, and deciding it is NP-complete.

open as a page

As reviewer of a design that calls shift assignment NP-hard, what evidence do you require before accepting the claim?

level: principalimportance: should knowfreq 36%

basics

~20 s

Require a stated decision version, a named already-complete source problem, a polynomial-time transformation that preserves the answer, and the right word for what was proved. Then check the claim is about the problem the team actually ships.

open as a page

Finance wants parcel weights stored in grams instead of kilograms, and your exact packing check is a weight-indexed table; what do you tell them?

level: principalimportance: should knowfreq 33%

basics

~20 s

That the unit change multiplies the table's width a thousandfold while the depot, the parcel count and the problem's hardness are unchanged. The cost of this method tracks numeric precision, so precision and granularity must be decided deliberately.

open as a page

Why would one polynomial-time algorithm for a single NP-complete problem put every NP problem into P?

level: seniorimportance: nice to knowfreq 32%

basics

~20 s

Completeness means every NP problem already reduces into that one in polynomial time. Composing the polynomial transformation with the polynomial solver answers any NP problem in polynomial time, because polynomials compose and the transformed instance stays polynomially sized.

open as a page

Capping every parcel weight at a small constant makes Subset Sum easy but leaves 3-Partition hard; what distinction is that?

level: seniorimportance: nice to knowfreq 27%

basics

~20 s

Weak versus strong NP-hardness. A strongly NP-hard problem stays hard even when every number is bounded by a polynomial in the instance length; a weakly hard one collapses to a value-indexed table once its numbers are small.

open as a page

Why is 2-SAT decidable in linear time when maximising the number of satisfied two-literal clauses is NP-hard?

level: seniorimportance: nice to knowfreq 24%

basics

~20 s

Deciding 2-SAT works because every clause is a hard constraint, so falsifying one literal forces its partner and consequences propagate. Once clauses may be broken, nothing is forced — you are choosing which to sacrifice — and that version is NP-hard at width two.

open as a page

Cook-Levin says any polynomial-time check compiles into one Boolean formula. As a lead, why is that construction still the wrong way to actually build such a formula?

level: principalimportance: nice to knowfreq 24%

basics

~20 s

Polynomial is not small: the grid is quadratic in a time bound that is already a polynomial of the input length, and it encodes a tape machine rather than the problem. The theorem classifies; it does not compile.

open as a page