Complete the code to initialize the priority queue for Prim's algorithm.
let mut pq = std::collections::BinaryHeap::new();
pq.push([1]);We start Prim's algorithm by pushing the starting vertex with cost 0 into the priority queue.
Complete the code to mark a vertex as visited in Prim's algorithm.
let mut visited = vec![false; n];
visited[[1]] = true;The starting vertex is usually vertex 0, so we mark visited[0] as true after adding it to the MST.
Fix the error in the edge relaxation step of Prim's algorithm.
for &(cost, to) in &graph[[1]] { if !visited[to] && cost < dist[to] { dist[to] = cost; pq.push(([2], to)); } }
We iterate over edges from the current vertex. We push (-cost, to) because Rust's BinaryHeap is a max-heap, so we negate cost to simulate a min-heap.
Fill both blanks to correctly pop the next vertex and check if it is visited.
while let Some(([1], [2])) = pq.pop() { let cost = -cost; if visited[to] { continue; }
We pop (cost, to) from the priority queue. We negate cost to get the original positive cost. Then we check if 'to' vertex is visited.
Fill all three blanks to update the MST cost and mark the vertex visited.
mst_cost += [1]; visited[[2]] = true; current_vertex = [3];
Add the edge cost to mst_cost, mark the 'to' vertex visited, and update current_vertex to 'to' for the next iteration.
