| 1 | [中文版](README_CN.md) | [English Version](README.md) |
| 2 | |
| 3 | # <img src="img/logo.svg" width="300"> |
| 4 | |
| 5 | 这是一款用于创建通用图数据结构、对其进行修改、分析和可视化的库。 |
| 6 | |
| 7 | # 特性 |
| 8 | |
| 9 | * 支持任意类型的通用顶点,例如 `int` 或 `City`。 |
| 10 | * 图的特征和相应的验证,例如在无环图中进行循环检查。 |
| 11 | * 寻找路径或连通图的算法,例如最短路径或强连通图。 |
| 12 | * 转换和表示的算法,例如传递闭包或拓扑排序。 |
| 13 | * 非递归图遍历的算法,例如 DFS 或 BFS。 |
| 14 | * 顶点和边可以包含可选的元数据,例如权重或自定义属性。 |
| 15 | * 使用 DOT 语言和 Graphviz 进行图形可视化。 |
| 16 | * 通过使用自己的 `Store` 实现,可以集成任何存储后端。 |
| 17 | * 包含广泛的测试,覆盖率约为 90%,且没有任何依赖项。 |
| 18 | |
| 19 | > 状态:由于 graph 版本处于 0.x 阶段,公共 API 不应被视为稳定的。 |
| 20 | |
| 21 | > README 可能包含未发布的更改。请查看 [latest documentation](https://pkg.go.dev/github.com/dominikbraun/graph). |
| 22 | |
| 23 | # 入门指南 |
| 24 | |
| 25 | ``` |
| 26 | go get github.com/dominikbraun/graph |
| 27 | ``` |
| 28 | |
| 29 | # 快速示例 |
| 30 | |
| 31 | ## 创建整数类型节点ID图 |
| 32 | |
| 33 |  |
| 34 | |
| 35 | ```go |
| 36 | g := graph.New(graph.IntHash) |
| 37 | |
| 38 | _ = g.AddVertex(1) |
| 39 | _ = g.AddVertex(2) |
| 40 | _ = g.AddVertex(3) |
| 41 | _ = g.AddVertex(4) |
| 42 | _ = g.AddVertex(5) |
| 43 | |
| 44 | _ = g.AddEdge(1, 2) |
| 45 | _ = g.AddEdge(1, 4) |
| 46 | _ = g.AddEdge(2, 3) |
| 47 | _ = g.AddEdge(2, 4) |
| 48 | _ = g.AddEdge(2, 5) |
| 49 | _ = g.AddEdge(3, 5) |
| 50 | ``` |
| 51 | |
| 52 | ## 创建整数类型节点ID有向无环图 |
| 53 | |
| 54 |  |
| 55 | |
| 56 | ```go |
| 57 | g := graph.New(graph.IntHash, graph.Directed(), graph.Acyclic()) |
| 58 | |
| 59 | _ = g.AddVertex(1) |
| 60 | _ = g.AddVertex(2) |
| 61 | _ = g.AddVertex(3) |
| 62 | _ = g.AddVertex(4) |
| 63 | |
| 64 | _ = g.AddEdge(1, 2) |
| 65 | _ = g.AddEdge(1, 3) |
| 66 | _ = g.AddEdge(2, 3) |
| 67 | _ = g.AddEdge(2, 4) |
| 68 | _ = g.AddEdge(3, 4) |
| 69 | ``` |
| 70 | |
| 71 | ## 创建自定义类型节点ID图 |
| 72 | |
| 73 | 要详细了解此示例,请参见 [concept of hashes](https://pkg.go.dev/github.com/dominikbraun/[email protected]#hdr-Hashes). |
| 74 | |
| 75 | ```go |
| 76 | type City struct { |
| 77 | Name string |
| 78 | } |
| 79 | |
| 80 | cityHash := func(c City) string { |
| 81 | return c.Name |
| 82 | } |
| 83 | |
| 84 | g := graph.New(cityHash) |
| 85 | |
| 86 | _ = g.AddVertex(london) |
| 87 | ``` |
| 88 | |
| 89 | ## 创建边带权重的图 |
| 90 | |
| 91 |  |
| 92 | |
| 93 | ```go |
| 94 | g := graph.New(cityHash, graph.Weighted()) |
| 95 | |
| 96 | _ = g.AddVertex(london) |
| 97 | _ = g.AddVertex(munich) |
| 98 | _ = g.AddVertex(paris) |
| 99 | _ = g.AddVertex(madrid) |
| 100 | |
| 101 | _ = g.AddEdge("london", "munich", graph.EdgeWeight(3)) |
| 102 | _ = g.AddEdge("london", "paris", graph.EdgeWeight(2)) |
| 103 | _ = g.AddEdge("london", "madrid", graph.EdgeWeight(5)) |
| 104 | _ = g.AddEdge("munich", "madrid", graph.EdgeWeight(6)) |
| 105 | _ = g.AddEdge("munich", "paris", graph.EdgeWeight(2)) |
| 106 | _ = g.AddEdge("paris", "madrid", graph.EdgeWeight(4)) |
| 107 | ``` |
| 108 | |
| 109 | ## 执行深度优先搜索 |
| 110 | |
| 111 | 这个示例按 DFS 顺序遍历并打印图中的所有顶点。 |
| 112 | |
| 113 |  |
| 114 | |
| 115 | ```go |
| 116 | g := graph.New(graph.IntHash, graph.Directed()) |
| 117 | |
| 118 | _ = g.AddVertex(1) |
| 119 | _ = g.AddVertex(2) |
| 120 | _ = g.AddVertex(3) |
| 121 | _ = g.AddVertex(4) |
| 122 | |
| 123 | _ = g.AddEdge(1, 2) |
| 124 | _ = g.AddEdge(1, 3) |
| 125 | _ = g.AddEdge(3, 4) |
| 126 | |
| 127 | _ = graph.DFS(g, 1, func(value int) bool { |
| 128 | fmt.Println(value) |
| 129 | return false |
| 130 | }) |
| 131 | ``` |
| 132 | |
| 133 | ``` |
| 134 | 1 3 4 2 |
| 135 | ``` |
| 136 | |
| 137 | ## 查找强联通分量 |
| 138 | |
| 139 |  |
| 140 | |
| 141 | ```go |
| 142 | g := graph.New(graph.IntHash) |
| 143 | |
| 144 | // Add vertices and edges ... |
| 145 | |
| 146 | scc, _ := graph.StronglyConnectedComponents(g) |
| 147 | |
| 148 | fmt.Println(scc) |
| 149 | ``` |
| 150 | |
| 151 | ``` |
| 152 | [[1 2 5] [3 4 8] [6 7]] |
| 153 | ``` |
| 154 | |
| 155 | ## 查找最短路径 |
| 156 | |
| 157 |  |
| 158 | |
| 159 | ```go |
| 160 | g := graph.New(graph.StringHash, graph.Weighted()) |
| 161 | |
| 162 | // Add vertices and weighted edges ... |
| 163 | |
| 164 | path, _ := graph.ShortestPath(g, "A", "B") |
| 165 | |
| 166 | fmt.Println(path) |
| 167 | ``` |
| 168 | |
| 169 | ``` |
| 170 | [A C E B] |
| 171 | ``` |
| 172 | |
| 173 | ## 查找生成树 |
| 174 | |
| 175 |  |
| 176 | |
| 177 | ```go |
| 178 | g := graph.New(graph.StringHash, graph.Weighted()) |
| 179 | |
| 180 | // Add vertices and edges ... |
| 181 | |
| 182 | mst, _ := graph.MinimumSpanningTree(g) |
| 183 | ``` |
| 184 | |
| 185 | ## 执行拓扑排序 |
| 186 | |
| 187 |  |
| 188 | |
| 189 | ```go |
| 190 | g := graph.New(graph.IntHash, graph.Directed(), graph.PreventCycles()) |
| 191 | |
| 192 | // Add vertices and edges ... |
| 193 | |
| 194 | // For a deterministic topological ordering, use StableTopologicalSort. |
| 195 | order, _ := graph.TopologicalSort(g) |
| 196 | |
| 197 | fmt.Println(order) |
| 198 | ``` |
| 199 | |
| 200 | ``` |
| 201 | [1 2 3 4 5] |
| 202 | ``` |
| 203 | |
| 204 | ## 执行传递闭包削减 |
| 205 | |
| 206 |  |
| 207 | |
| 208 | ```go |
| 209 | g := graph.New(graph.StringHash, graph.Directed(), graph.PreventCycles()) |
| 210 | |
| 211 | // Add vertices and edges ... |
| 212 | |
| 213 | transitiveReduction, _ := graph.TransitiveReduction(g) |
| 214 | ``` |
| 215 | |
| 216 |  |
| 217 | |
| 218 | ## 禁止创建环路 |
| 219 | |
| 220 |  |
| 221 | |
| 222 | ```go |
| 223 | g := graph.New(graph.IntHash, graph.PreventCycles()) |
| 224 | |
| 225 | _ = g.AddVertex(1) |
| 226 | _ = g.AddVertex(2) |
| 227 | _ = g.AddVertex(3) |
| 228 | |
| 229 | _ = g.AddEdge(1, 2) |
| 230 | _ = g.AddEdge(1, 3) |
| 231 | |
| 232 | if err := g.AddEdge(2, 3); err != nil { |
| 233 | panic(err) |
| 234 | } |
| 235 | ``` |
| 236 | |
| 237 | ``` |
| 238 | panic: 在 2 和 3 之间创建的边将会引入一个环 |
| 239 | ``` |
| 240 | |
| 241 | ## 使用 Graphviz 图可视化 |
| 242 | |
| 243 | 以下示例将为 `g` 生成一个 DOT 描述,并将其写入给定的文件中。 |
| 244 | |
| 245 | ```go |
| 246 | g := graph.New(graph.IntHash, graph.Directed()) |
| 247 | |
| 248 | _ = g.AddVertex(1) |
| 249 | _ = g.AddVertex(2) |
| 250 | _ = g.AddVertex(3) |
| 251 | |
| 252 | _ = g.AddEdge(1, 2) |
| 253 | _ = g.AddEdge(1, 3) |
| 254 | |
| 255 | file, _ := os.Create("./mygraph.gv") |
| 256 | _ = draw.DOT(g, file) |
| 257 | ``` |
| 258 | |
| 259 | 要使用 Graphviz 从创建的文件生成 SVG,请使用如下命令: |
| 260 | |
| 261 | ``` |
| 262 | dot -Tsvg -O mygraph.gv |
| 263 | ``` |
| 264 | |
| 265 | `DOT` 函数还支持渲染图属性: |
| 266 | |
| 267 | ```go |
| 268 | _ = draw.DOT(g, file, draw.GraphAttribute("label", "my-graph")) |
| 269 | ``` |
| 270 | |
| 271 | ### 按照此文档绘制图 |
| 272 | |
| 273 |  |
| 274 | |
| 275 | 图使用以下程序进行渲染: |
| 276 | |
| 277 | ```go |
| 278 | package main |
| 279 | |
| 280 | import ( |
| 281 | "os" |
| 282 | |
| 283 | "github.com/dominikbraun/graph" |
| 284 | "github.com/dominikbraun/graph/draw" |
| 285 | ) |
| 286 | |
| 287 | func main() { |
| 288 | g := graph.New(graph.IntHash) |
| 289 | |
| 290 | _ = g.AddVertex(1, graph.VertexAttribute("colorscheme", "blues3"), graph.VertexAttribute("style", "filled"), graph.VertexAttribute("color", "2"), graph.VertexAttribute("fillcolor", "1")) |
| 291 | _ = g.AddVertex(2, graph.VertexAttribute("colorscheme", "greens3"), graph.VertexAttribute("style", "filled"), graph.VertexAttribute("color", "2"), graph.VertexAttribute("fillcolor", "1")) |
| 292 | _ = g.AddVertex(3, graph.VertexAttribute("colorscheme", "purples3"), graph.VertexAttribute("style", "filled"), graph.VertexAttribute("color", "2"), graph.VertexAttribute("fillcolor", "1")) |
| 293 | _ = g.AddVertex(4, graph.VertexAttribute("colorscheme", "ylorbr3"), graph.VertexAttribute("style", "filled"), graph.VertexAttribute("color", "2"), graph.VertexAttribute("fillcolor", "1")) |
| 294 | _ = g.AddVertex(5, graph.VertexAttribute("colorscheme", "reds3"), graph.VertexAttribute("style", "filled"), graph.VertexAttribute("color", "2"), graph.VertexAttribute("fillcolor", "1")) |
| 295 | |
| 296 | _ = g.AddEdge(1, 2) |
| 297 | _ = g.AddEdge(1, 4) |
| 298 | _ = g.AddEdge(2, 3) |
| 299 | _ = g.AddEdge(2, 4) |
| 300 | _ = g.AddEdge(2, 5) |
| 301 | _ = g.AddEdge(3, 5) |
| 302 | |
| 303 | file, _ := os.Create("./simple.gv") |
| 304 | _ = draw.DOT(g, file) |
| 305 | } |
| 306 | ``` |
| 307 | |
| 308 | 使用 neato 引擎进行可视化: |
| 309 | |
| 310 | ``` |
| 311 | dot -Tsvg -Kneato -O simple.gv |
| 312 | ``` |
| 313 | |
| 314 | 这个例子使用Graphviz支持的 [Brewer color scheme](https://graphviz.org/doc/info/colors.html#brewer)。 |
| 315 | |
| 316 | ## 存储边属性 |
| 317 | |
| 318 | 边可以具有一个或多个属性,用于存储元数据。在[visualizing a graph](#visualize-a-graph-using-graphviz) 时将考虑这些属性。 |
| 319 | 例如,此边将呈现为红色: |
| 320 | |
| 321 | ```go |
| 322 | _ = g.AddEdge(1, 2, graph.EdgeAttribute("color", "red")) |
| 323 | ``` |
| 324 | |
| 325 | 要获取所有支持的属性的概述,请查看 |
| 326 | [DOT documentation](https://graphviz.org/doc/info/attrs.html). |
| 327 | |
| 328 | The stored attributes can be retrieved by getting the edge and accessing the `Properties.Attributes` |
| 329 | field. |
| 330 | 可以通过获取边并访问 `Properties.Attributes` 字段来检索存储的属性。 |
| 331 | |
| 332 | ```go |
| 333 | edge, _ := g.Edge(1, 2) |
| 334 | color := edge.Properties.Attributes["color"] |
| 335 | ``` |
| 336 | |
| 337 | ## 存储边数据 |
| 338 | |
| 339 | 还可以在边上存储任意类型属性数据,而不仅仅是键值字符串对。此数据类型为 `any`。 |
| 340 | |
| 341 | ```go |
| 342 | _ = g.AddEdge(1, 2, graph.EdgeData(myData)) |
| 343 | ``` |
| 344 | |
| 345 | 可以通过获取边并访问 `Properties.Data` 字段来检索存储的数据。 |
| 346 | |
| 347 | ```go |
| 348 | edge, _ := g.Edge(1, 2) |
| 349 | myData := edge.Properties.Data |
| 350 | ``` |
| 351 | |
| 352 | ### 更新边数据 |
| 353 | |
| 354 | 可以使用 `Graph.UpdateEdge` 更新边属性。以下示例向边 (A,B) 添加了一个新的 `color` 属性,并将边权重设置为 10。 |
| 355 | |
| 356 | ```go |
| 357 | _ = g.UpdateEdge("A", "B", graph.EdgeAttribute("color", "red"), graph.EdgeWeight(10)) |
| 358 | ``` |
| 359 | |
| 360 | `Graph.UpdateEdge` 的方法签名和接受的函数选项与 `Graph.AddEdge` 完全相同。 |
| 361 | |
| 362 | ## 存储点属性 |
| 363 | |
| 364 | 顶点可能具有一个或多个属性,可用于存储元数据。在 [visualizing a graph](#visualize-a-graph-using-graphviz) 时将考虑这些属性。 |
| 365 | 例如,此顶点将以红色渲染: |
| 366 | |
| 367 | ```go |
| 368 | _ = g.AddVertex(1, graph.VertexAttribute("style", "filled")) |
| 369 | ``` |
| 370 | |
| 371 | 存储在顶点中的数据可以通过使用 `VertexWithProperties` 获取顶点,并访问 `Attributes` 字段来检索。 |
| 372 | |
| 373 | ```go |
| 374 | vertex, properties, _ := g.VertexWithProperties(1) |
| 375 | style := properties.Attributes["style"] |
| 376 | ``` |
| 377 | |
| 378 | 要获取所有支持的属性的概述,请查看 |
| 379 | [DOT documentation](https://graphviz.org/doc/info/attrs.html). |
| 380 | |
| 381 | ## 将图存储在自定义存储中 |
| 382 | |
| 383 | 可以通过实现 `Store` 接口并使用它初始化一个新的图,来集成任何存储后端: |
| 384 | |
| 385 | ```go |
| 386 | g := graph.NewWithStore(graph.IntHash, myStore) |
| 387 | ``` |
| 388 | |
| 389 | 恰当实现 `Store` 接口,参考 [documentation](https://pkg.go.dev/github.com/dominikbraun/graph#Store)。 |
| 390 | [`graph-sql`](https://github.com/dominikbraun/graph-sql) 是一个可直接使用的 SQL 存储实现。 |
| 391 | # 文档 |
| 392 | |
| 393 | 完整文档可在以下位置找到: [pkg.go.dev](https://pkg.go.dev/github.com/dominikbraun/graph). |