不可变图
示例
有向图
let u = "☝️", v = "✌️"
let vertices = Set(arrayLiteral: u, v)
let e = Edge(source: u, destination: v)
let edges = Set(arrayLiteral: e)
let graph = Graph(vertices: vertices, edges: edges)
加权有向图
let w_e = WeightedEdge(source: u, destination: v, weight: 3.9)
let weightedEdges = Set(arrayLiteral: w_e)
let weightedGraph = Graph(vertices: vertices, edges: weightedEdges)
从源点执行DFS
let simpleGraphAnnotation = dfs(graph, source: v)
在图上执行DFS
let simpleGraphAnnotation = dfs(graph)
从源点执行BFS
let simpleGraphAnnotation = bfs(graph, source: v)
从图注释中查找路径
let parentByVertex = simpleGraphAnnotation.parentByVertex
let result = findPath(parentByVertex: parentByVertex, source: s, destination: d)
CocoaPods 安装
ImmutableGraph
可通过CocoaPods获取。要安装它,请简单地将以下行添加到您的Podfile
pod "ImmutableGraph"
Swift Package 安装
如果您的包依赖于ImmutableGraph
,请将以下依赖项添加到您的Package.swift
dependencies: [
.package(url: "https://github.com/horothesun/ImmutableGraph", .upToNextMinor(from: "0.1.0"))
]
生成 Xcode 项目
swift package generate-xcodeproj
测试
macOS
swift test
Docker Linux
重要:执行以下操作以重新生成 Linux 测试列表
swift test --generate-linuxmain
在基本 swift:5.2
镜像上执行
docker run --rm \
--volume "$(pwd):/package" \
--workdir "/package" \
swift:5.2 \
/bin/bash -c "swift test --build-path ./.build/linux"
或基于 Dockerfile
创建新的镜像并运行
docker build --tag immutable-graph .
docker run --rm immutable-graph
注释
这个库使用 TDD 构建。
用于单元测试 BFS 和 DFS 算法的某些图表来自 YouTube 视频频道 Algorithms with Attitude 的相关视频。
作者
Nicola Di Pol, [email protected]
许可证
ImmutableGraph
适用于 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。