Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the colors vector with -1 for all nodes.
DSA Rust
let mut colors = vec![[1]; graph.len()];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 which represents a colored node.
Using 1 which represents the opposite color.
Using 2 which is unnecessary for bipartite graphs.
✗ Incorrect
The correct value to initialize uncolored nodes is -1. vec![-1; graph.len()] creates a vector of length graph.len() filled with -1.
2fill in blank
mediumComplete the code to assign the starting node color as 0.
DSA Rust
colors[start] = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning -1 which means uncolored.
Assigning 1 which is the other color but not the start.
✗ Incorrect
We assign color 0 to the starting node to begin the bipartite coloring process.
3fill in blank
hardFix the error in the BFS loop condition to process nodes until the queue is empty.
DSA Rust
while let Some(node) = queue.[1]() {
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which is not defined for VecDeque.
Using pop_back() which leads to DFS behavior.
✗ Incorrect
Using pop_front() removes and returns the first element from the queue, which is correct for BFS.
4fill in blank
hardFill both blanks to assign the opposite color to the neighbor and push it to the queue.
DSA Rust
colors[neighbor] = 1 [1] colors[node]; queue.[2](neighbor);
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or - instead of XOR for color flipping.
Using push instead of push_back for VecDeque.
✗ Incorrect
XOR with 1 flips the color between 0 and 1. push_back adds the neighbor to the end of the queue.
5fill in blank
hardFill all three blanks to check if a neighbor has the same color as the current node and return false if so.
DSA Rust
if colors[neighbor] != -1 && colors[neighbor] [1] colors[node] { return [2]; } // After BFS loop return [3];
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == to check color conflict.
Returning true inside the loop instead of after.
✗ Incorrect
If neighbor color equals current node color, graph is not bipartite, so return false. After BFS, return true.
