All problems
Component Label Array
Problem
Label each node of an undirected graph with its component identifier.
The label of a component is the smallest node index it contains, so scanning start nodes in increasing order automatically produces the right labels. edges is a list of [u, v] pairs and isolated nodes form their own components. Return labels in node-index order.
Hint
The first node visited in a component is its minimum index because starts are ascending.
Test cases
case 1
in: [4,[[0,1],[2,3]]]
expected: [0,0,2,2]
case 2
in: [5,[]]
expected: [0,1,2,3,4]
case 3
in: [3,[[1,2]]]
expected: [0,1,1]
case 4
in: [4,[[1,3],[0,2]]]
expected: [0,1,0,1]
case 5
in: [2,[[0,1]]]
expected: [0,0]
Starter codePython
def component_labels(n, edges):
# Your code here
pass5 test cases · run your code against them in the editor