CONN-P2

Every group, in order

Practice 2 of 4Connectivity11 tests

The problem

Split all n junctions into their strongly connected groups. Return a list group with one number per junction. Two junctions get the same number exactly when each can reach the other. Number the groups 0, 1, 2, … with no number missing, and in an order that every street respects: for every street (u, v), group[u] <= group[v]. That order is a topological order of the graph of groups (lesson 1).

streets = [(0, 1), (1, 4), (4, 0), (5, 4), (5, 6),
           (1, 2), (2, 3), (3, 7), (7, 6), (6, 2)]
scc(8, streets)   # [1, 1, 2, 2, 1, 0, 2, 2]
scc(4, [(2, 3), (0, 1), (1, 2)])   # [0, 1, 2, 3]

When several numberings are right (groups with no street between them can come in either order), any of them is accepted. The tests check the numbers you return: the groups, the order, and no gaps.

Limits

  • Up to 100,000 junctions and 160,000 streets.
  • The search can go 100,000 levels deep. The big tests have 4 seconds each.

Builds on CONN-2 Kosaraju and Tarjan.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

11 tests, not run yet
Test results
#TestResultReads · time
1The exampleNot run
2A one-way chainNot run
3No streetsNot run
4One junctionNot run
5Loops and repeated streetsNot run
6Two loops joined both waysNot run
7A cross street to a finished groupNot run
8A chain of 100,000Not run
9A ring of 100,000Not run
1080,000 random streetsNot run
1120,000 small groupsNot run