0
0
DSA Typescriptprogramming~10 mins

Bipartite Graph Check in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Bipartite Graph Check
Start with uncolored graph
Pick an unvisited node
Assign color 0
Use BFS/DFS to visit neighbors
If neighbor uncolored, assign opposite color
If neighbor colored same as current, stop: Not Bipartite
Repeat for all nodes
If no conflicts, graph is Bipartite
We color nodes using two colors while traversing. If a conflict arises, graph is not bipartite.
Execution Sample
DSA Typescript
function isBipartite(graph: number[][]): boolean {
  const color = new Array(graph.length).fill(-1);
  for (let i = 0; i < graph.length; i++) {
    if (color[i] === -1) {
      if (!bfsCheck(i, graph, color)) return false;
    }
  }
  return true;
}

function bfsCheck(start: number, graph: number[][], color: number[]): boolean {
  const queue: number[] = [start];
  color[start] = 0;
  while (queue.length > 0) {
    const node = queue.shift()!;
    for (const neighbor of graph[node]) {
      if (color[neighbor] === -1) {
        color[neighbor] = 1 - color[node];
        queue.push(neighbor);
      } else if (color[neighbor] === color[node]) {
        return false;
      }
    }
  }
  return true;
}
Checks if graph is bipartite by coloring nodes using BFS.
Execution Table
StepOperationCurrent NodeNeighborColor ArrayConflict DetectedVisual State
1Start BFS from node 00-[-1, -1, -1, -1]No[0(-), 1(-), 2(-), 3(-)]
2Color node 0 with 00-[0, -1, -1, -1]No[0(0), 1(-), 2(-), 3(-)]
3Visit neighbor 1 of node 001[0, -1, -1, -1]No[0(0), 1(-), 2(-), 3(-)]
4Color neighbor 1 with 11-[0, 1, -1, -1]No[0(0), 1(1), 2(-), 3(-)]
5Visit neighbor 3 of node 003[0, 1, -1, -1]No[0(0), 1(1), 2(-), 3(-)]
6Color neighbor 3 with 13-[0, 1, -1, 1]No[0(0), 1(1), 2(-), 3(1)]
7Visit neighbor 0 of node 110[0, 1, -1, 1]No[0(0), 1(1), 2(-), 3(1)]
8Neighbor 0 already colored 0 (opposite), continue10[0, 1, -1, 1]No[0(0), 1(1), 2(-), 3(1)]
9Visit neighbor 2 of node 112[0, 1, -1, 1]No[0(0), 1(1), 2(-), 3(1)]
10Color neighbor 2 with 02-[0, 1, 0, 1]No[0(0), 1(1), 2(0), 3(1)]
11Visit neighbor 1 of node 221[0, 1, 0, 1]No[0(0), 1(1), 2(0), 3(1)]
12Neighbor 1 already colored 1 (opposite), continue21[0, 1, 0, 1]No[0(0), 1(1), 2(0), 3(1)]
13Visit neighbor 3 of node 223[0, 1, 0, 1]No[0(0), 1(1), 2(0), 3(1)]
14Neighbor 3 already colored 1 (opposite), continue23[0, 1, 0, 1]No[0(0), 1(1), 2(0), 3(1)]
15Visit neighbor 0 of node 330[0, 1, 0, 1]No[0(0), 1(1), 2(0), 3(1)]
16Neighbor 0 already colored 0 (opposite), continue30[0, 1, 0, 1]No[0(0), 1(1), 2(0), 3(1)]
17Visit neighbor 2 of node 332[0, 1, 0, 1]No[0(0), 1(1), 2(0), 3(1)]
18Neighbor 2 already colored 0 (opposite), continue32[0, 1, 0, 1]No[0(0), 1(1), 2(0), 3(1)]
19All nodes visited, no conflict--[0, 1, 0, 1]No[0(0), 1(1), 2(0), 3(1)]
💡 All nodes colored without conflict, graph is bipartite
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 10Final
color[-1, -1, -1, -1][0, -1, -1, -1][0, 1, -1, -1][0, 1, -1, 1][0, 1, 0, 1][0, 1, 0, 1]
currentNode-0132-
conflictDetectedNoNoNoNoNoNo
Key Moments - 3 Insights
Why do we assign colors only when a node is uncolored?
Because if a node is already colored, it means it was visited before and assigned a color. Reassigning could cause conflicts. See steps 7 and 11 where neighbors already have colors and no reassignment happens.
What happens if a neighbor has the same color as the current node?
This means the graph is not bipartite because two connected nodes share the same color. The algorithm stops immediately. In this example, no such conflict occurs, but if it did, the 'Conflict Detected' column would show 'Yes'.
Why do we start BFS from every unvisited node?
Because the graph can be disconnected. We must check each component separately. In this example, the graph is connected, so BFS starts only once at node 0.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at Step 4, what is the color assigned to node 1?
A0
B1
C-1 (uncolored)
D2
💡 Hint
Check the 'Color Array' column at Step 4 in the execution table.
At which step does node 2 get its color assigned?
AStep 10
BStep 6
CStep 14
DStep 2
💡 Hint
Look for the step where 'Color neighbor 2 with 0' happens in the 'Operation' column.
If node 3 was colored 0 instead of 1 at Step 6, what would happen?
ANo conflict, graph still bipartite
BConflict detected later, graph not bipartite
CAlgorithm would skip node 3
DColor array would remain unchanged
💡 Hint
Refer to the 'Conflict Detected' column and the rule that neighbors must have opposite colors.
Concept Snapshot
Bipartite Graph Check:
- Use two colors (e.g., 0 and 1) to color nodes.
- Traverse graph using BFS or DFS.
- Assign opposite color to neighbors.
- If a neighbor has same color, graph is not bipartite.
- Check all components for disconnected graphs.
Full Transcript
This visualization shows how to check if a graph is bipartite by coloring nodes with two colors during a BFS traversal. We start with all nodes uncolored (-1). We pick an unvisited node, color it 0, then color its neighbors 1, their neighbors 0, and so on. If at any point a neighbor has the same color as the current node, the graph is not bipartite. The execution table tracks each step, the current node, neighbors, color assignments, and conflicts. The variable tracker shows how the color array changes over time. Key moments clarify why colors are assigned only once, what conflicts mean, and why BFS starts at every unvisited node. The quiz tests understanding of color assignments and conflict detection. The snapshot summarizes the process in simple steps.