You are required to concurrently process multiple breadth-first search (BFS) queries on a single graph. Each query specifies a starting node, and you must compute the BFS order from that node. Unlike a simple single-threaded BFS, your solution should utilize goroutines and channels (or concurrency-safe data structures) to handle multiple queries efficiently and in parallel.
func ConcurrentBFSQueries(graph map[int][]int, queries []int, numWorkers int) map[int][]intParameters:
- graph: A representation of the graph as an adjacency list.
- The key is a node (an integer).
- The value is a slice of adjacent nodes.
- queries: A slice of starting nodes for which BFS must be performed.
- numWorkers: The number of goroutines (workers) that concurrently handle these BFS queries.
Returns:
- A map from the query node to the BFS order starting from that node.
- You must use concurrency (goroutines + channels, or concurrency-safe data structures) to process BFS queries in parallel.
- A naive or purely sequential approach may be too slow, especially for large graphs and many queries.
- The BFS algorithm itself can be standard (using a queue), but each BFS query should run concurrently if workers are available.
func main() {
graph := map[int][]int{
0: {1, 2},
1: {2, 3},
2: {3},
3: {4},
4: {},
}
queries := []int{0, 1, 2}
numWorkers := 2
results := ConcurrentBFSQueries(graph, queries, numWorkers)
/*
Possible output:
results[0] = [0 1 2 3 4]
results[1] = [1 2 3 4]
results[2] = [2 3 4]
*/
}- Fork this repository and clone your fork.
- Create a directory for your submission:
challenge-4/submissions/<yourgithubusername>/. - Copy
solution-template.gointo your submission directory. - Implement the function
ConcurrentBFSQueries(graph map[int][]int, queries []int, numWorkers int) map[int][]int. - Use goroutines to handle BFS queries in parallel, respecting the number of workers.
- Open a pull request with your solution.
From inside challenge-4/, run:
go test -v