| 1 | package graph |
| 2 | |
| 3 | import "fmt" |
| 4 | |
| 5 | // DFS performs a depth-first search on the graph, starting from the given vertex. The visit |
| 6 | // function will be invoked with the hash of the vertex currently visited. If it returns false, DFS |
| 7 | // will continue traversing the graph, and if it returns true, the traversal will be stopped. In |
| 8 | // case the graph is disconnected, only the vertices joined with the starting vertex are visited. |
| 9 | // |
| 10 | // This example prints all vertices of the graph in DFS-order: |
| 11 | // |
| 12 | // g := graph.New(graph.IntHash) |
| 13 | // |
| 14 | // _ = g.AddVertex(1) |
| 15 | // _ = g.AddVertex(2) |
| 16 | // _ = g.AddVertex(3) |
| 17 | // |
| 18 | // _ = g.AddEdge(1, 2) |
| 19 | // _ = g.AddEdge(2, 3) |
| 20 | // _ = g.AddEdge(3, 1) |
| 21 | // |
| 22 | // _ = graph.DFS(g, 1, func(value int) bool { |
| 23 | // fmt.Println(value) |
| 24 | // return false |
| 25 | // }) |
| 26 | // |
| 27 | // Similarly, if you have a graph of City vertices and the traversal should stop at London, the |
| 28 | // visit function would look as follows: |
| 29 | // |
| 30 | // func(c City) bool { |
| 31 | // return c.Name == "London" |
| 32 | // } |
| 33 | // |
| 34 | // DFS is non-recursive and maintains a stack instead. |
| 35 | func DFS[K comparable, T any](g Graph[K, T], start K, visit func(K) bool) error { |
| 36 | adjacencyMap, err := g.AdjacencyMap() |
| 37 | if err != nil { |
| 38 | return fmt.Errorf("could not get adjacency map: %w", err) |
| 39 | } |
| 40 | |
| 41 | if _, ok := adjacencyMap[start]; !ok { |
| 42 | return fmt.Errorf("could not find start vertex with hash %v", start) |
| 43 | } |
| 44 | |
| 45 | stack := make([]K, 0) |
| 46 | visited := make(map[K]bool) |
| 47 | |
| 48 | stack = append(stack, start) |
| 49 | |
| 50 | for len(stack) > 0 { |
| 51 | currentHash := stack[len(stack)-1] |
| 52 | |
| 53 | stack = stack[:len(stack)-1] |
| 54 | |
| 55 | if _, ok := visited[currentHash]; !ok { |
| 56 | // Stop traversing the graph if the visit function returns true. |
| 57 | if stop := visit(currentHash); stop { |
| 58 | break |
| 59 | } |
| 60 | visited[currentHash] = true |
| 61 | |
| 62 | for adjacency := range adjacencyMap[currentHash] { |
| 63 | stack = append(stack, adjacency) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return nil |
| 69 | } |
| 70 | |
| 71 | // BFS performs a breadth-first search on the graph, starting from the given vertex. The visit |
| 72 | // function will be invoked with the hash of the vertex currently visited. If it returns false, BFS |
| 73 | // will continue traversing the graph, and if it returns true, the traversal will be stopped. In |
| 74 | // case the graph is disconnected, only the vertices joined with the starting vertex are visited. |
| 75 | // |
| 76 | // This example prints all vertices of the graph in BFS-order: |
| 77 | // |
| 78 | // g := graph.New(graph.IntHash) |
| 79 | // |
| 80 | // _ = g.AddVertex(1) |
| 81 | // _ = g.AddVertex(2) |
| 82 | // _ = g.AddVertex(3) |
| 83 | // |
| 84 | // _ = g.AddEdge(1, 2) |
| 85 | // _ = g.AddEdge(2, 3) |
| 86 | // _ = g.AddEdge(3, 1) |
| 87 | // |
| 88 | // _ = graph.BFS(g, 1, func(value int) bool { |
| 89 | // fmt.Println(value) |
| 90 | // return false |
| 91 | // }) |
| 92 | // |
| 93 | // Similarly, if you have a graph of City vertices and the traversal should stop at London, the |
| 94 | // visit function would look as follows: |
| 95 | // |
| 96 | // func(c City) bool { |
| 97 | // return c.Name == "London" |
| 98 | // } |
| 99 | // |
| 100 | // BFS is non-recursive and maintains a stack instead. |
| 101 | func BFS[K comparable, T any](g Graph[K, T], start K, visit func(K) bool) error { |
| 102 | ignoreDepth := func(vertex K, _ int) bool { |
| 103 | return visit(vertex) |
| 104 | } |
| 105 | return BFSWithDepth(g, start, ignoreDepth) |
| 106 | } |
| 107 | |
| 108 | // BFSWithDepth works just as BFS and performs a breadth-first search on the graph, but its |
| 109 | // visit function is passed the current depth level as a second argument. Consequently, the |
| 110 | // current depth can be used for deciding whether or not to proceed past a certain depth. |
| 111 | // |
| 112 | // _ = graph.BFSWithDepth(g, 1, func(value int, depth int) bool { |
| 113 | // fmt.Println(value) |
| 114 | // return depth > 3 |
| 115 | // }) |
| 116 | // |
| 117 | // With the visit function from the example, the BFS traversal will stop once a depth greater |
| 118 | // than 3 is reached. |
| 119 | func BFSWithDepth[K comparable, T any](g Graph[K, T], start K, visit func(K, int) bool) error { |
| 120 | adjacencyMap, err := g.AdjacencyMap() |
| 121 | if err != nil { |
| 122 | return fmt.Errorf("could not get adjacency map: %w", err) |
| 123 | } |
| 124 | |
| 125 | if _, ok := adjacencyMap[start]; !ok { |
| 126 | return fmt.Errorf("could not find start vertex with hash %v", start) |
| 127 | } |
| 128 | |
| 129 | queue := make([]K, 0) |
| 130 | visited := make(map[K]bool) |
| 131 | |
| 132 | visited[start] = true |
| 133 | queue = append(queue, start) |
| 134 | depth := 0 |
| 135 | |
| 136 | for len(queue) > 0 { |
| 137 | currentHash := queue[0] |
| 138 | |
| 139 | queue = queue[1:] |
| 140 | depth++ |
| 141 | |
| 142 | // Stop traversing the graph if the visit function returns true. |
| 143 | if stop := visit(currentHash, depth); stop { |
| 144 | break |
| 145 | } |
| 146 | |
| 147 | for adjacency := range adjacencyMap[currentHash] { |
| 148 | if _, ok := visited[adjacency]; !ok { |
| 149 | visited[adjacency] = true |
| 150 | queue = append(queue, adjacency) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | } |
| 155 | |
| 156 | return nil |
| 157 | } |