Build parent pointers from adjacency list using BFS from root.
| 47 | |
| 48 | // Build parent pointers from adjacency list using BFS from root. |
| 49 | void BuildParentsFromAdjacencyList( |
| 50 | const std::vector<std::vector<int>>& adjacency_list, |
| 51 | int root, |
| 52 | std::vector<int>& parents) { |
| 53 | const int num_nodes = static_cast<int>(adjacency_list.size()); |
| 54 | parents.assign(num_nodes, -1); |
| 55 | parents[root] = root; |
| 56 | |
| 57 | std::vector<char> visited(num_nodes, false); |
| 58 | visited[root] = true; |
| 59 | |
| 60 | std::queue<int> queue; |
| 61 | queue.push(root); |
| 62 | |
| 63 | while (!queue.empty()) { |
| 64 | const int current = queue.front(); |
| 65 | queue.pop(); |
| 66 | |
| 67 | for (const int neighbor : adjacency_list[current]) { |
| 68 | if (!visited[neighbor]) { |
| 69 | visited[neighbor] = true; |
| 70 | parents[neighbor] = current; |
| 71 | queue.push(neighbor); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | SpanningTree ComputeSpanningTreeInternal( |
| 78 | int num_nodes, |
no test coverage detected