Directed graph:
letu="☝️",v="✌️"letvertices=Set(arrayLiteral: u, v)lete=Edge(source: u, destination: v)letedges=Set(arrayLiteral: e)letgraph=Graph(vertices: vertices, edges: edges)Weighted directed graph:
letw_e=WeightedEdge(source: u, destination: v, weight:3.9)letweightedEdges=Set(arrayLiteral: w_e)letweightedGraph=Graph(vertices: vertices, edges: weightedEdges)DFS from source:
letsimpleGraphAnnotation=dfs(graph, source: v)DFS on graph:
letsimpleGraphAnnotation=dfs(graph)BFS from source:
letsimpleGraphAnnotation=bfs(graph, source: v)Finding paths from graph annotations:
letparentByVertex= simpleGraphAnnotation.parentByVertex
letresult=findPath(parentByVertex: parentByVertex, source: s, destination: d)ImmutableGraph is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod"ImmutableGraph"If your package depends on ImmutableGraph, add the following dependency to your Package.swift:
dependencies:[.package(url:"https://github.com/horothesun/ImmutableGraph",.upToNextMinor(from:"0.1.0"))]swift package generate-xcodeprojswift testIMPORTANT: regenerate Linux test list executing
swift test --generate-linuxmainExecute on base swift:5.2 image
docker run --rm \
--volume "$(pwd):/package" \
--workdir '/package' \
swift:5.2 \
/bin/bash -c 'swift test'or create a new image based on Dockerfile and run it
docker build --tag immutable-graph .
docker run --rm immutable-graphThis library was built using TDD.
Some of the graphs used to unit test BFS and DFS algorithms come from Algorithms with Attitude YouTube channel's related videos:
Nicola Di Pol, horothesun@gmail.com
ImmutableGraph is available under the MIT license. See the LICENSE file for more info.