| 1 | [中文版](README_CN.md) | [English Version](README.md) |
| 2 | |
| 3 | # <img src="img/banner.png"> |
| 4 | |
| 5 | A library for creating generic graph data structures and modifying, analyzing, |
| 6 | and visualizing them. |
| 7 | |
| 8 | **Are you using graph? [Check out the graph user survey.](https://forms.gle/MLKUZKMeCRxTfj4v9)** |
| 9 | |
| 10 | # Features |
| 11 | |
| 12 | * Generic vertices of any type, such as `int` or `City`. |
| 13 | * Graph traits with corresponding validations, such as cycle checks in acyclic graphs. |
| 14 | * Algorithms for finding paths or components, such as shortest paths or strongly connected components. |
| 15 | * Algorithms for transformations and representations, such as transitive reduction or topological order. |
| 16 | * Algorithms for non-recursive graph traversal, such as DFS or BFS. |
| 17 | * Vertices and edges with optional metadata, such as weights or custom attributes. |
| 18 | * Visualization of graphs using the DOT language and Graphviz. |
| 19 | * Integrate any storage backend by using your own `Store` implementation. |
| 20 | * Extensive tests with ~90% coverage, and zero dependencies. |
| 21 | |
| 22 | > Status: Because `graph` is in version 0, the public API shouldn't be considered stable. |
| 23 | |
| 24 | > This README may contain unreleased changes. Check out the [latest documentation](https://pkg.go.dev/github.com/dominikbraun/graph). |
| 25 | |
| 26 | # Getting started |
| 27 | |
| 28 | ``` |
| 29 | go get github.com/dominikbraun/graph |
| 30 | ``` |
| 31 | |
| 32 | # Quick examples |
| 33 | |
| 34 | ## Create a graph of integers |
| 35 | |
| 36 |  |
| 37 | |
| 38 | ```go |
| 39 | g := graph.New(graph.IntHash) |
| 40 | |
| 41 | _ = g.AddVertex(1) |
| 42 | _ = g.AddVertex(2) |
| 43 | _ = g.AddVertex(3) |
| 44 | _ = g.AddVertex(4) |
| 45 | _ = g.AddVertex(5) |
| 46 | |
| 47 | _ = g.AddEdge(1, 2) |
| 48 | _ = g.AddEdge(1, 4) |
| 49 | _ = g.AddEdge(2, 3) |
| 50 | _ = g.AddEdge(2, 4) |
| 51 | _ = g.AddEdge(2, 5) |
| 52 | _ = g.AddEdge(3, 5) |
| 53 | ``` |
| 54 | |
| 55 | ## Create a directed acyclic graph of integers |
| 56 | |
| 57 |  |
| 58 | |
| 59 | ```go |
| 60 | g := graph.New(graph.IntHash, graph.Directed(), graph.Acyclic()) |
| 61 | |
| 62 | _ = g.AddVertex(1) |
| 63 | _ = g.AddVertex(2) |
| 64 | _ = g.AddVertex(3) |
| 65 | _ = g.AddVertex(4) |
| 66 | |
| 67 | _ = g.AddEdge(1, 2) |
| 68 | _ = g.AddEdge(1, 3) |
| 69 | _ = g.AddEdge(2, 3) |
| 70 | _ = g.AddEdge(2, 4) |
| 71 | _ = g.AddEdge(3, 4) |
| 72 | ``` |
| 73 | |
| 74 | ## Create a graph of a custom type |
| 75 | |
| 76 | To understand this example in detail, see the [concept of hashes](https://pkg.go.dev/github.com/dominikbraun/graph#hdr-Hashes). |
| 77 | |
| 78 | ```go |
| 79 | type City struct { |
| 80 | Name string |
| 81 | } |
| 82 | |
| 83 | cityHash := func(c City) string { |
| 84 | return c.Name |
| 85 | } |
| 86 | |
| 87 | g := graph.New(cityHash) |
| 88 | |
| 89 | _ = g.AddVertex(london) |
| 90 | ``` |
| 91 | |
| 92 | ## Create a weighted graph |
| 93 | |
| 94 |  |
| 95 | |
| 96 | ```go |
| 97 | g := graph.New(cityHash, graph.Weighted()) |
| 98 | |
| 99 | _ = g.AddVertex(london) |
| 100 | _ = g.AddVertex(munich) |
| 101 | _ = g.AddVertex(paris) |
| 102 | _ = g.AddVertex(madrid) |
| 103 | |
| 104 | _ = g.AddEdge("london", "munich", graph.EdgeWeight(3)) |
| 105 | _ = g.AddEdge("london", "paris", graph.EdgeWeight(2)) |
| 106 | _ = g.AddEdge("london", "madrid", graph.EdgeWeight(5)) |
| 107 | _ = g.AddEdge("munich", "madrid", graph.EdgeWeight(6)) |
| 108 | _ = g.AddEdge("munich", "paris", graph.EdgeWeight(2)) |
| 109 | _ = g.AddEdge("paris", "madrid", graph.EdgeWeight(4)) |
| 110 | ``` |
| 111 | |
| 112 | ## Perform a Depth-First Search |
| 113 | |
| 114 | This example traverses and prints all vertices in the graph in DFS order. |
| 115 | |
| 116 |  |
| 117 | |
| 118 | ```go |
| 119 | g := graph.New(graph.IntHash, graph.Directed()) |
| 120 | |
| 121 | _ = g.AddVertex(1) |
| 122 | _ = g.AddVertex(2) |
| 123 | _ = g.AddVertex(3) |
| 124 | _ = g.AddVertex(4) |
| 125 | |
| 126 | _ = g.AddEdge(1, 2) |
| 127 | _ = g.AddEdge(1, 3) |
| 128 | _ = g.AddEdge(3, 4) |
| 129 | |
| 130 | _ = graph.DFS(g, 1, func(value int) bool { |
| 131 | fmt.Println(value) |
| 132 | return false |
| 133 | }) |
| 134 | ``` |
| 135 | |
| 136 | ``` |
| 137 | 1 3 4 2 |
| 138 | ``` |
| 139 | |
| 140 | ## Find strongly connected components |
| 141 | |
| 142 |  |
| 143 | |
| 144 | ```go |
| 145 | g := graph.New(graph.IntHash) |
| 146 | |
| 147 | // Add vertices and edges ... |
| 148 | |
| 149 | scc, _ := graph.StronglyConnectedComponents(g) |
| 150 | |
| 151 | fmt.Println(scc) |
| 152 | ``` |
| 153 | |
| 154 | ``` |
| 155 | [[1 2 5] [3 4 8] [6 7]] |
| 156 | ``` |
| 157 | |
| 158 | ## Find the shortest path |
| 159 | |
| 160 |  |
| 161 | |
| 162 | ```go |
| 163 | g := graph.New(graph.StringHash, graph.Weighted()) |
| 164 | |
| 165 | // Add vertices and weighted edges ... |
| 166 | |
| 167 | path, _ := graph.ShortestPath(g, "A", "B") |
| 168 | |
| 169 | fmt.Println(path) |
| 170 | ``` |
| 171 | |
| 172 | ``` |
| 173 | [A C E B] |
| 174 | ``` |
| 175 | |
| 176 | ## Find spanning trees |
| 177 | |
| 178 |  |
| 179 | |
| 180 | ```go |
| 181 | g := graph.New(graph.StringHash, graph.Weighted()) |
| 182 | |
| 183 | // Add vertices and edges ... |
| 184 | |
| 185 | mst, _ := graph.MinimumSpanningTree(g) |
| 186 | ``` |
| 187 | |
| 188 | ## Perform a topological sort |
| 189 | |
| 190 |  |
| 191 | |
| 192 | ```go |
| 193 | g := graph.New(graph.IntHash, graph.Directed(), graph.PreventCycles()) |
| 194 | |
| 195 | // Add vertices and edges ... |
| 196 | |
| 197 | // For a deterministic topological ordering, use StableTopologicalSort. |
| 198 | order, _ := graph.TopologicalSort(g) |
| 199 | |
| 200 | fmt.Println(order) |
| 201 | ``` |
| 202 | |
| 203 | ``` |
| 204 | [1 2 3 4 5] |
| 205 | ``` |
| 206 | |
| 207 | ## Perform a transitive reduction |
| 208 | |
| 209 |  |
| 210 | |
| 211 | ```go |
| 212 | g := graph.New(graph.StringHash, graph.Directed(), graph.PreventCycles()) |
| 213 | |
| 214 | // Add vertices and edges ... |
| 215 | |
| 216 | transitiveReduction, _ := graph.TransitiveReduction(g) |
| 217 | ``` |
| 218 | |
| 219 |  |
| 220 | |
| 221 | ## Prevent the creation of cycles |
| 222 | |
| 223 |  |
| 224 | |
| 225 | ```go |
| 226 | g := graph.New(graph.IntHash, graph.PreventCycles()) |
| 227 | |
| 228 | _ = g.AddVertex(1) |
| 229 | _ = g.AddVertex(2) |
| 230 | _ = g.AddVertex(3) |
| 231 | |
| 232 | _ = g.AddEdge(1, 2) |
| 233 | _ = g.AddEdge(1, 3) |
| 234 | |
| 235 | if err := g.AddEdge(2, 3); err != nil { |
| 236 | panic(err) |
| 237 | } |
| 238 | ``` |
| 239 | |
| 240 | ``` |
| 241 | panic: an edge between 2 and 3 would introduce a cycle |
| 242 | ``` |
| 243 | |
| 244 | ## Visualize a graph using Graphviz |
| 245 | |
| 246 | The following example will generate a DOT description for `g` and write it into the given file. |
| 247 | |
| 248 | ```go |
| 249 | g := graph.New(graph.IntHash, graph.Directed()) |
| 250 | |
| 251 | _ = g.AddVertex(1) |
| 252 | _ = g.AddVertex(2) |
| 253 | _ = g.AddVertex(3) |
| 254 | |
| 255 | _ = g.AddEdge(1, 2) |
| 256 | _ = g.AddEdge(1, 3) |
| 257 | |
| 258 | file, _ := os.Create("./mygraph.gv") |
| 259 | _ = draw.DOT(g, file) |
| 260 | ``` |
| 261 | |
| 262 | To generate an SVG from the created file using Graphviz, use a command such as the following: |
| 263 | |
| 264 | ``` |
| 265 | dot -Tsvg -O mygraph.gv |
| 266 | ``` |
| 267 | |
| 268 | The `DOT` function also supports rendering graph attributes: |
| 269 | |
| 270 | ```go |
| 271 | _ = draw.DOT(g, file, draw.GraphAttribute("label", "my-graph")) |
| 272 | ``` |
| 273 | |
| 274 | ### Draw a graph as in this documentation |
| 275 | |
| 276 |  |
| 277 | |
| 278 | This graph has been rendered using the following program: |
| 279 | |
| 280 | ```go |
| 281 | package main |
| 282 | |
| 283 | import ( |
| 284 | "os" |
| 285 | |
| 286 | "github.com/dominikbraun/graph" |
| 287 | "github.com/dominikbraun/graph/draw" |
| 288 | ) |
| 289 | |
| 290 | func main() { |
| 291 | g := graph.New(graph.IntHash) |
| 292 | |
| 293 | _ = g.AddVertex(1, graph.VertexAttribute("colorscheme", "blues3"), graph.VertexAttribute("style", "filled"), graph.VertexAttribute("color", "2"), graph.VertexAttribute("fillcolor", "1")) |
| 294 | _ = g.AddVertex(2, graph.VertexAttribute("colorscheme", "greens3"), graph.VertexAttribute("style", "filled"), graph.VertexAttribute("color", "2"), graph.VertexAttribute("fillcolor", "1")) |
| 295 | _ = g.AddVertex(3, graph.VertexAttribute("colorscheme", "purples3"), graph.VertexAttribute("style", "filled"), graph.VertexAttribute("color", "2"), graph.VertexAttribute("fillcolor", "1")) |
| 296 | _ = g.AddVertex(4, graph.VertexAttribute("colorscheme", "ylorbr3"), graph.VertexAttribute("style", "filled"), graph.VertexAttribute("color", "2"), graph.VertexAttribute("fillcolor", "1")) |
| 297 | _ = g.AddVertex(5, graph.VertexAttribute("colorscheme", "reds3"), graph.VertexAttribute("style", "filled"), graph.VertexAttribute("color", "2"), graph.VertexAttribute("fillcolor", "1")) |
| 298 | |
| 299 | _ = g.AddEdge(1, 2) |
| 300 | _ = g.AddEdge(1, 4) |
| 301 | _ = g.AddEdge(2, 3) |
| 302 | _ = g.AddEdge(2, 4) |
| 303 | _ = g.AddEdge(2, 5) |
| 304 | _ = g.AddEdge(3, 5) |
| 305 | |
| 306 | file, _ := os.Create("./simple.gv") |
| 307 | _ = draw.DOT(g, file) |
| 308 | } |
| 309 | ``` |
| 310 | |
| 311 | It has been rendered using the `neato` engine: |
| 312 | |
| 313 | ``` |
| 314 | dot -Tsvg -Kneato -O simple.gv |
| 315 | ``` |
| 316 | |
| 317 | The example uses the [Brewer color scheme](https://graphviz.org/doc/info/colors.html#brewer) supported by Graphviz. |
| 318 | |
| 319 | ## Storing edge attributes |
| 320 | |
| 321 | Edges may have one or more attributes which can be used to store metadata. Attributes will be taken |
| 322 | into account when [visualizing a graph](#visualize-a-graph-using-graphviz). For example, this edge |
| 323 | will be rendered in red color: |
| 324 | |
| 325 | ```go |
| 326 | _ = g.AddEdge(1, 2, graph.EdgeAttribute("color", "red")) |
| 327 | ``` |
| 328 | |
| 329 | To get an overview of all supported attributes, take a look at the |
| 330 | [DOT documentation](https://graphviz.org/doc/info/attrs.html). |
| 331 | |
| 332 | The stored attributes can be retrieved by getting the edge and accessing the `Properties.Attributes` |
| 333 | field. |
| 334 | |
| 335 | ```go |
| 336 | edge, _ := g.Edge(1, 2) |
| 337 | color := edge.Properties.Attributes["color"] |
| 338 | ``` |
| 339 | |
| 340 | ## Storing edge data |
| 341 | |
| 342 | It is also possible to store arbitrary data inside edges, not just key-value string pairs. This data |
| 343 | is of type `any`. |
| 344 | |
| 345 | ```go |
| 346 | _ = g.AddEdge(1, 2, graph.EdgeData(myData)) |
| 347 | ``` |
| 348 | |
| 349 | The stored data can be retrieved by getting the edge and accessing the `Properties.Data` field. |
| 350 | |
| 351 | ```go |
| 352 | edge, _ := g.Edge(1, 2) |
| 353 | myData := edge.Properties.Data |
| 354 | ``` |
| 355 | |
| 356 | ### Updating edge data |
| 357 | |
| 358 | Edge properties can be updated using `Graph.UpdateEdge`. The following example adds a new `color` |
| 359 | attribute to the edge (A,B) and sets the edge weight to 10. |
| 360 | |
| 361 | ```go |
| 362 | _ = g.UpdateEdge("A", "B", graph.EdgeAttribute("color", "red"), graph.EdgeWeight(10)) |
| 363 | ``` |
| 364 | |
| 365 | The method signature and the accepted functional options are exactly the same as for `Graph.AddEdge`. |
| 366 | |
| 367 | ## Storing vertex attributes |
| 368 | |
| 369 | Vertices may have one or more attributes which can be used to store metadata. Attributes will be |
| 370 | taken into account when [visualizing a graph](#visualize-a-graph-using-graphviz). For example, this |
| 371 | vertex will be rendered in red color: |
| 372 | |
| 373 | ```go |
| 374 | _ = g.AddVertex(1, graph.VertexAttribute("style", "filled")) |
| 375 | ``` |
| 376 | |
| 377 | The stored data can be retrieved by getting the vertex using `VertexWithProperties` and accessing |
| 378 | the `Attributes` field. |
| 379 | |
| 380 | ```go |
| 381 | vertex, properties, _ := g.VertexWithProperties(1) |
| 382 | style := properties.Attributes["style"] |
| 383 | ``` |
| 384 | |
| 385 | To get an overview of all supported attributes, take a look at the |
| 386 | [DOT documentation](https://graphviz.org/doc/info/attrs.html). |
| 387 | |
| 388 | ## Store the graph in a custom storage |
| 389 | |
| 390 | You can integrate any storage backend by implementing the `Store` interface and initializing a new |
| 391 | graph with it: |
| 392 | |
| 393 | ```go |
| 394 | g := graph.NewWithStore(graph.IntHash, myStore) |
| 395 | ``` |
| 396 | |
| 397 | To implement the `Store` interface appropriately, take a look at the [documentation](https://pkg.go.dev/github.com/dominikbraun/graph#Store). |
| 398 | [`graph-sql`](https://github.com/dominikbraun/graph-sql) is a ready-to-use SQL store implementation. |
| 399 | |
| 400 | # Documentation |
| 401 | |
| 402 | The full documentation is available at [pkg.go.dev](https://pkg.go.dev/github.com/dominikbraun/graph). |
| 403 | |
| 404 | **Are you using graph? [Check out the graph user survey.](https://forms.gle/MLKUZKMeCRxTfj4v9)** |