Label the components
Warm-up · Trace the algorithm · about 5 min
Watch the scan label the graph. A component is a group of nodes joined by edges. Count the groups first, then click the node that shares a component with node 3.
Example 1
Input:
{"n":6,"edges":[[0,1],[1,2],[3,4]],"positions":[[0,0],[1,0],[2,0],[0,1.6],[1,1.6],[2.5,1.6]]}
Output:
["G/node:4"]
Node 4 is the only other node in node 3's group.
Constraints
- The trace uses the graph shown
Learn the idea first: Connected components and flood fill, a visual lesson.
More Connected components and flood fill problems
- Number of islands Standard
- Max area of island Standard
- Count components Standard
- Surrounded regions Challenge