TraverseConcurrently performs a concurrent depth-first traversal of the graph starting from the node
(visited map[string]bool, wg *sync.WaitGroup)
| 48 | |
| 49 | // TraverseConcurrently performs a concurrent depth-first traversal of the graph starting from the node |
| 50 | func (n *Node) TraverseConcurrently(visited map[string]bool, wg *sync.WaitGroup) { |
| 51 | defer wg.Done() |
| 52 | |
| 53 | if visited[n.id] { |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | visited[n.id] = true |
| 58 | log.Printf("Visiting node %s", n.id) |
| 59 | |
| 60 | var neighborWgs sync.WaitGroup |
| 61 | for _, neighbor := range n.neighbors { |
| 62 | neighborWgs.Add(1) |
| 63 | go func(neighbor *Node) { |
| 64 | neighbor.TraverseConcurrently(visited, &neighborWgs) |
| 65 | }(neighbor) |
| 66 | } |
| 67 | |
| 68 | neighborWgs.Wait() |
| 69 | } |