cconnected.
Understand the pattern. Find your own way.Find your starting point
Your learning feed/Idea 68 of 218
Ideas this builds on
Graphs68 / 218

Explore every component, then enforce local constraints

Picture

A graph may contain several disconnected groups. One traversal reaches only the starting group. An outer loop starts a new traversal from each still-unseen vertex. While exploring, you can also propagate constraints—for example, requiring the two endpoints of every edge to receive opposite colors.

Predict

A graph contains one isolated vertex and a separate triangle. If you start BFS at the isolated vertex and find no conflict, have you proved the whole graph can be split into two non-conflicting groups?

Reveal the reasoning

No. You checked only one component. A triangle cannot be two-colored: the third edge connects two vertices that would need the same color. Every component must be examined.

Go deeper: a traversal can carry a constraint

For an undirected adjacency list on vertices 0..n-1, assign each undiscovered neighbor the opposite color. A conflicting already assigned neighbor disproves bipartiteness.

from collections import deque

def two_color(graph):
    color = {}
    for root in range(len(graph)):
        if root in color:
            continue
        color[root] = 0
        queue = deque([root])
        while queue:
            u = queue.popleft()
            for v in graph[u]:
                if v not in color:
                    color[v] = 1 - color[u]
                    queue.append(v)
                elif color[v] == color[u]:
                    return None
    return color

assert two_color([[1], [0, 2], [1], []]) == {0: 0, 1: 1, 2: 0, 3: 0}
assert two_color([[], [2, 3], [1, 3], [1, 2]]) is None
assert two_color([[0]]) is None

The outer loop also gives a connected-components algorithm: label every reached vertex with the current group number and increment the number at each new root. Across all searches, each vertex and adjacency entry is processed a constant number of times: O(V+E) time and O(V) extra space.

An island problem is this same idea on an implicit graph: land cells are vertices and allowed land-to-land steps are edges. The definition of adjacency—four directions versus eight—changes the graph and can change the component count.

Why does an odd cycle cause a color conflict? Colors alternate along edges; walking an odd number of edges around a cycle demands the start have its own opposite color. Conversely, a successful full coloring explicitly witnesses that every edge crosses the two groups.

Analogy limit: “two groups” is not sufficient by itself to imply bipartite coloring. The constraint here is specifically that every edge’s endpoints must differ.

Change one rule

Edges are one-way, and a group should mean every member can reach every other member. Is undirected component labeling sufficient?

Check the variation

No. That asks for strongly connected components. Directional mutual reachability is a different equivalence relation, handled by algorithms such as Kosaraju or Tarjan in an extension lesson.

Connect

Disjoint sets maintain undirected components during insertions. Spanning trees connect components cheaply. Board word search revisits a board under path-specific constraints rather than permanent island labels.