PIXELBANKv9.1.0
Menu

Given an undirected graph as an adjacency list, determine if it is bipartite (can be colored with 2 colors such that no adjacent nodes share a color).

Input: each line has comma-separated neighbor indices (0-indexed).

Example:

Input:
1,3
0,2
1,3
0,2
Output:
True
Reasoning:
  • The input represents an undirected graph as an adjacency list, where each line corresponds to a node and its neighbors.
  • We can represent the graph as follows:
    • Node 0 is connected to nodes 1 and 3
    • Node 1 is connected to nodes 0 and 2
    • Node 2 is connected to nodes 1 and 3
    • Node 3 is connected to nodes 0 and 2
  • To determine if the graph is bipartite, we can attempt to color the nodes using two colors, ensuring that no adjacent nodes share the same color.
  • A possible coloring is:
    • Nodes 0 and 1 are colored with color A
    • Nodes 2 and 3 are colored with color B
  • The graph can be successfully colored with two colors, so the output is True.

Constraints:

  • 1 <= n <= 100
  • 0 <= edges
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Is Graph Bipartite - Medium | PixelBank