Imagine you need to organize a networking event where people can only talk to others from a different group. You're given a social network graph where each person is connected to their acquaintances.

Your task is to determine if you can divide all attendees into exactly two groups such that:

  • Every person only knows people from the opposite group
  • No one knows anyone from their same group

You're given an undirected graph with n nodes (people), where graph[u] contains all the people that person u knows. The graph has no self-connections and may have multiple disconnected components (separate friend circles).

Goal: Return true if the social network can be perfectly divided into two groups with the networking constraint, false otherwise.

Input & Output

example_1.py โ€” Simple Bipartite Graph
$ Input: graph = [[1,2,3],[0,2],[0,1,3],[0,2]]
โ€บ Output: false
๐Ÿ’ก Note: The graph forms a 4-cycle (square), but nodes 0,1,2 form a triangle. Since triangles are odd cycles, this graph cannot be colored with 2 colors and is not bipartite.
example_2.py โ€” Valid Bipartite Graph
$ Input: graph = [[1,3],[0,2],[1,3],[0,2]]
โ€บ Output: true
๐Ÿ’ก Note: We can divide the nodes into two groups: Group A = {0,2} and Group B = {1,3}. Every edge connects a node from Group A to Group B, satisfying the bipartite condition.
example_3.py โ€” Disconnected Components
$ Input: graph = [[1],[0],[3],[2]]
โ€บ Output: true
๐Ÿ’ก Note: The graph has two disconnected components: {0,1} and {2,3}. Each component is bipartite (just an edge), so the entire graph is bipartite.

Visualization

Tap to expand
The Two-Team Networking EventTeam RedAliceCharlieEveTeam BlueBobDavidFrankโœ“ Valid Networking EventEvery connection is between different teams!Algorithm Process:1. Put Alice in Team Red โ†’ All her friends go to Team Blue2. Put Bob in Team Blue โ†’ All his friends go to Team Red3. Continue until everyone is assigned or conflict found4. If no conflicts arise, the event can be organized! (Bipartite)
Understanding the Visualization
1
Assign Teams
Start by putting the first person in Team Red. All their connections must go to Team Blue.
2
Propagate Rules
Everyone Team Red knows goes to Team Blue, and everyone Team Blue knows goes to Team Red.
3
Check Conflicts
If we ever need to put someone in both teams, it's impossible to organize the event.
4
Handle Groups
Repeat for separate friend circles that aren't connected to the main group.
Key Takeaway
๐ŸŽฏ Key Insight: A graph is bipartite if we can divide all nodes into two groups where every edge connects different groups. This is equivalent to checking if the graph can be colored with only 2 colors!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(V + E)

Visit each vertex once and each edge once during DFS traversal

n
2n
โœ“ Linear Growth
Space Complexity
O(V)

Color array storage plus recursion stack depth

n
2n
โœ“ Linear Space

Constraints

  • graph.length == n
  • 1 โ‰ค n โ‰ค 100
  • 0 โ‰ค graph[u].length < n
  • 0 โ‰ค graph[u][i] โ‰ค n - 1
  • graph[u] does not contain u
  • All values of graph[u] are unique
  • If graph[u] contains v, then graph[v] contains u
Asked in
Google 15 Facebook 12 Microsoft 8 Amazon 6
78.5K Views
Medium Frequency
~18 min Avg. Time
3.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen