Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the color array with -1 for all vertices.
DSA C++
std::vector<int> color(V, [1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 1 as initial color which conflicts with actual colors.
Initializing with the number of vertices instead of a color marker.
✗ Incorrect
We use -1 to mark all vertices as uncolored initially.
2fill in blank
mediumComplete the code to assign the first color (0) to the starting vertex in BFS.
DSA C++
color[start] = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning -1 which means uncolored.
Assigning the vertex index as color.
✗ Incorrect
We assign color 0 to the starting vertex to begin the bipartite coloring.
3fill in blank
hardFix the error in the condition to check if the adjacent vertex has the same color.
DSA C++
if (color[adj] == [1]) return false;
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with -1 which means uncolored.
Comparing with 0 or adj vertex index instead of current vertex color.
✗ Incorrect
If the adjacent vertex has the same color as the current vertex, the graph is not bipartite.
4fill in blank
hardFill both blanks to assign the opposite color to the adjacent vertex and push it into the queue.
DSA C++
color[adj] = [1]; q.push([2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning same color as current vertex.
Pushing the current vertex instead of adjacent vertex.
✗ Incorrect
Assign opposite color to adjacent vertex and push it into the queue for BFS.
5fill in blank
hardFill all three blanks to complete the BFS loop for bipartite check.
DSA C++
while (!q.empty()) { int u = q.front(); q.pop(); for (int adj : graph[[1]]) { if (color[adj] == -1) { color[adj] = [2]; q.push([3]); } else if (color[adj] == color[u]) { return false; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable for adjacency list.
Assigning wrong color or pushing wrong vertex.
✗ Incorrect
Iterate over neighbors of u, assign opposite color to uncolored adj, and push adj to queue.
