📖 Scenario: You are working on a social networking app. You want to check if the users can be split into two groups so that no two friends are in the same group. This helps in organizing events where friends from different groups meet.
🎯 Goal: Build a program that checks if a given graph of users and their friendships is bipartite. A bipartite graph means you can divide users into two groups with no friendships inside the same group.
📋 What You'll Learn
Create a graph using an adjacency list with exact user connections
Add a color array to track group assignments
Implement a function to check if the graph is bipartite using BFS
Print the result as true or false
💡 Why This Matters
🌍 Real World
Checking bipartite graphs helps in scheduling, grouping people, and solving problems where two distinct sets must be formed without internal conflicts.
💼 Career
Understanding bipartite graphs is useful for software engineers working on social networks, recommendation systems, and network analysis.
Progress0 / 4 steps
1
Create the graph adjacency list
Create a variable called graph as an array of arrays representing the adjacency list for 4 users with these exact connections: user 0 connected to 1 and 3, user 1 connected to 0 and 2, user 2 connected to 1 and 3, user 3 connected to 0 and 2.
DSA Typescript
Hint
Use a constant named graph and assign it an array of arrays with the exact connections.
2
Add a color array to track groups
Create a variable called color as an array of length 4 filled with -1 to represent uncolored users.
DSA Typescript
Hint
Use new Array(4).fill(-1) to create the color array.
3
Implement the bipartite check function
Write a function called isBipartite that takes graph and color as parameters. Use a for loop with variable start from 0 to graph.length. Inside, if color[start] is -1, assign color[start] = 0 and use a queue to perform BFS. For each node, check neighbors: if uncolored, assign opposite color; if same color, return false. Return true if no conflicts.
DSA Typescript
Hint
Use BFS with a queue and assign colors 0 and 1 to neighbors. Return false if a conflict is found.
4
Print the bipartite check result
Write a console.log statement to print the result of calling isBipartite(graph, color).
DSA Typescript
Hint
Call console.log(isBipartite(graph, color)) to print the result.
Practice
(1/5)
1. What does it mean if a graph is bipartite?
easy
A. Its vertices can be divided into two groups with no edges inside the same group
B. It has an equal number of vertices and edges
C. It contains at least one cycle
D. All vertices have the same degree
Solution
Step 1: Understand bipartite graph definition
A bipartite graph can be split into two sets where no edges connect vertices within the same set.
Step 2: Compare options with definition
Only Its vertices can be divided into two groups with no edges inside the same group matches this definition exactly.
Final Answer:
Its vertices can be divided into two groups with no edges inside the same group -> Option A
Quick Check:
Bipartite = Two groups, no internal edges [OK]
Hint: Bipartite means two groups with no edges inside groups [OK]
Common Mistakes:
Confusing bipartite with equal vertices and edges
Thinking bipartite means no cycles
Assuming all vertices have same degree
2. Which TypeScript data structure is best to represent a graph for bipartite check?
easy
A. Adjacency list using Map
B. Array of numbers
C. String array
D. Single number variable
Solution
Step 1: Identify graph representation needs
We need to store edges for each vertex efficiently to check bipartite property.
Step 2: Choose suitable data structure
An adjacency list using Map stores neighbors for each vertex clearly and is efficient.
Final Answer:
Adjacency list using Map<number, number[]> -> Option A
Quick Check:
Graph as adjacency list = Map [OK]
Hint: Use adjacency list (Map) for graph edges [OK]
Common Mistakes:
Using simple arrays without edge info
Using strings which don't represent graph
Using single variable instead of structure
3. What is the output of this TypeScript code that checks if a graph is bipartite?
const graph = new Map([
[0, [1, 3]],
[1, [0, 2]],
[2, [1, 3]],
[3, [0, 2]]
]);
function isBipartite(graph: Map): boolean {
const color = new Map();
for (const node of graph.keys()) {
if (!color.has(node)) {
const queue = [node];
color.set(node, 0);
while (queue.length) {
const u = queue.shift()!;
for (const v of graph.get(u)!) {
if (!color.has(v)) {
color.set(v, 1 - color.get(u)!);
queue.push(v);
} else if (color.get(v) === color.get(u)) {
return false;
}
}
}
}
}
return true;
}
console.log(isBipartite(graph));
medium
A. false
B. true
C. undefined
D. SyntaxError
Solution
Step 1: Analyze graph structure
The graph is a square cycle: 0-1-2-3-0, which is an even cycle and bipartite.
Step 2: Trace BFS coloring
Starting from node 0 with color 0, neighbors get color 1, their neighbors color 0, no conflicts found.
Final Answer:
true -> Option B
Quick Check:
Even cycle graph is bipartite = true [OK]
Hint: Even cycles are bipartite, odd cycles are not [OK]
Common Mistakes:
Assuming all cycles are not bipartite
Confusing color assignment logic
Missing BFS queue processing
4. Find the bug in this TypeScript function that checks if a graph is bipartite:
function isBipartite(graph: Map): boolean {
const color = new Map();
for (const node of graph.keys()) {
if (!color.has(node)) {
const queue = [node];
color.set(node, 0);
while (queue.length) {
const u = queue.shift()!;
for (const v of graph.get(u)!) {
if (!color.has(v)) {
color.set(v, color.get(u)!);
queue.push(v);
} else if (color.get(v) === color.get(u)) {
return false;
}
}
}
}
}
return true;
}
medium
A. Missing return type boolean
B. queue.shift()! should be queue.pop()
C. color.set(v, color.get(u)!) should be color.set(v, 1 - color.get(u)!)
D. graph.get(u)! should be graph.get(v)!
Solution
Step 1: Identify color assignment error
The code assigns the same color to neighbors, which breaks bipartite logic.
Step 2: Correct color assignment
Neighbors must get opposite color: 1 - color of current node.