Three interview problems about splitting items into two sides. The move: colour one node, force the rest, then answer per component.
You work on a bank app, a social app and a printing tool. The three problems on this street split items into two sides, so that two items that conflict never share a side. The Academy calls this a bipartite graph.
The three problems add something to the split.
| Problem | Two items must be apart when | The split is also asked to |
|---|---|---|
| Four-eyes review teams | they worked on the same case | respect analysts already pinned to a team |
| Possible bipartition of users | they muted each other | be counted over all valid ways |
| Two-ink district map | their districts touch along a side | cost as little as possible |
The question changes, but the walk does not. Colour one item, force the others, and look for a clash. What you read from the colouring is the new part.
Exercise · Predict the output: What colour does each node get?. The code gives each node colour 0 or 1 so that linked nodes differ. It starts each new component with colour 0. What does it print?
edges = [[0, 1], [0, 2], [1, 3], [2, 3], [4, 5]]
graph = [[] for _ in range(6)]
for a, b in edges:
graph[a].append(b)
graph[b].append(a)
colour = [-1] * 6
for start in range(6):
if colour[start] != -1:
continue
colour[start] = 0
queue = [start]
for a in queue:
for b in graph[a]:
if colour[b] == -1:
colour[b] = 1 - colour[a]
queue.append(b)
print(*colour)Walk a component from any start. Give the start colour 0 and every neighbour the opposite colour. If a neighbour already has the same colour as the node you stand on, you have a clash, and the split does not exist.
A clash happens exactly when the component has a cycle with an odd number of links. An even cycle, such as four nodes in a ring, colours without trouble.
When a component does colour, the start's colour decides every other colour. So it has exactly two colourings, and one is the swap of the other.
In a component that colours, one node's colour fixes every other. So there are exactly two colourings, one the swap of the other, and the components can be decided independently.
That one fact answers four kinds of question.
| The question | What you do for each component |
|---|---|
| Is a split possible? | colour it, and fail on a clash |
| Do the pins fit? | check the colouring, then its swap, against the pins |
| How many splits? | multiply by 2 |
| What is the cheapest cost? | add the colouring's cost and the swap's cost, keep the smaller |
Colour one node and force the rest: a component that colours has exactly two colourings, one the swap of the other.
Exercise · Find the bug: A seen neighbour is not always a clash. This tests whether the analysts can be split into two teams, so that no conflicting pair shares a team. It rejects some inputs that can be split. Which line is wrong?
def can_split(n, conflicts):
graph = [[] for _ in range(n)]
for a, b in conflicts:
graph[a].append(b)
graph[b].append(a)
colour = [-1] * n
for start in range(n):
if colour[start] != -1:
continue
colour[start] = 0
stack = [start]
while stack:
a = stack.pop()
for b in graph[a]:
if colour[b] == -1:
colour[b] = 1 - colour[a]
stack.append(b)
elif colour[b] != -1:
return False
return TrueThe Academy lesson Cycles, colouring and components tests whether a graph can be split in two. BFS and DFS on any graph gives the walk and the components. This street changes three things.
Pins pick a colouring. Some analysts are fixed to the north team or the south team. A component's two colourings are the colouring and its swap.
Check the pins against the first, then against the second. If neither fits, the answer is false.
Take the links 0 to 1 and 1 to 2. Nodes 0 and 2 must share a side, so pins of north on 0 and south on 2 fail. Pins of north on both pass.
Count instead of test. Every component that colours can swap its two sides, and the components do not affect each other. With c components the number of splits is 2 to the power c.
Trying every split checks 1,048,576 assignments for 20 users. The component count gives the same 1,024 valid splits from one walk.
The count grows fast. With 40 users and no mutes, there are 1,099,511,627,776 splits, and the problem asks for the remainder modulo 1,000,000,007, which is 511,620,083.
A cost, and a hidden graph. The district map gives a grid, not links. Two districts are linked when two of their cells touch along a side. Then each component pays the cheaper of its two colourings.
Take districts 0, 1, 2 in a strip with costs 1 or 9, 1 or 9, and 5 or 3. The cheaper colouring costs 13, and the other costs 15. A greedy pick of the cheapest ink for each district costs 5, but it puts districts 0 and 1 on the same ink, which is not allowed.
Exercise · Break it: The count that grows past the modulus. This counts the ways to put users in two rooms so that no muted pair shares a room. The answer must be given modulo 1,000,000,007. Find an input where it gives the wrong count.
def ways_to_split(n, mutes):
graph = [[] for _ in range(n)]
for a, b in mutes:
graph[a].append(b)
graph[b].append(a)
colour = [-1] * n
components = 0
for start in range(n):
if colour[start] != -1:
continue
components += 1
colour[start] = 0
stack = [start]
while stack:
a = stack.pop()
for b in graph[a]:
if colour[b] == -1:
colour[b] = 1 - colour[a]
stack.append(b)
elif colour[b] == colour[a]:
return 0
return 2 ** componentsCheck the pins against a component's colouring and against its swap. A path of three with opposite pins on its ends fails both.
Solve them in this order. The first adds pins, the second counts the splits and the third adds a cost on a hidden graph.
Before you code each problem, write what the component's two colourings are, and what you read from them: a yes or no, a count, or a cost. If the graph is hidden, write how two nodes become linked.
Give each problem about 25 minutes. Draw a graph with one even cycle, one odd cycle and one isolated node.
Then write three small tests: a single link, a ring of four and a triangle. A solution that rejects the single link or the ring has confused a seen neighbour with a clash.
When a count is wrong, check the modulus first. When a cost is wrong, check that you take the smaller of the two colourings for each component, not for each node.
Checkpoint · Bipartite and colouring
Q1/5 During a colouring walk you meet a neighbour that already has a colour. When is it a clash?
Answer: B
A seen neighbour with the opposite colour agrees with the colouring. The node you came from is such a neighbour, so a ring of four passes.
Q2/5 A component colours without a clash. How many valid colourings does it have?
Answer: B
The start's colour decides every other colour, so there are only two colourings, and swapping all colours gives the second.
Q3/5 The links are 0 to 1 and 1 to 2. Node 0 is pinned north and node 2 is pinned south. Can the analysts be split?
Answer: B
Both 0 and 2 are the opposite of node 1, so they share a side. The pins ask for different sides, which no colouring gives.
Q4/5 A graph of 20 users has 10 components and each colours. How many valid splits are there?
Answer: C
Each component can swap its sides on its own, so the count is 2 to the 10. The value 1,048,576 is the number of assignments to check one by one.
Q5/5 A district map has three districts in a strip with costs 1 or 9, 1 or 9, and 5 or 3. Why is picking the cheapest ink for each district wrong?
Answer: B
The cheapest ink for all three costs 5, but districts 0 and 1 would share an ink. The cheaper valid colouring costs 13.
Review cards from this lesson.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.