feat: add Swift codes for graph_traversal article (#378)

* feat: add Swift codes for graph_traversal article

* refactor: rename parameters

* Update graph_dfs.swift

---------

Co-authored-by: Yudong Jin <krahets@163.com>
pull/380/head
nuomi1 2 years ago committed by GitHub
parent f2d2cca5f1
commit c6c4c9d997
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -37,6 +37,8 @@ let package = Package(
// chapter_graph
.executable(name: "graph_adjacency_matrix", targets: ["graph_adjacency_matrix"]),
.executable(name: "graph_adjacency_list", targets: ["graph_adjacency_list"]),
.executable(name: "graph_bfs", targets: ["graph_bfs"]),
.executable(name: "graph_dfs", targets: ["graph_dfs"]),
// chapter_searching
.executable(name: "linear_search", targets: ["linear_search"]),
.executable(name: "binary_search", targets: ["binary_search"]),
@ -49,7 +51,9 @@ let package = Package(
.executable(name: "radix_sort", targets: ["radix_sort"]),
],
targets: [
// helper
.target(name: "utils", path: "utils"),
.target(name: "graph_adjacency_list_target", dependencies: ["utils"], path: "chapter_graph", sources: ["graph_adjacency_list_target.swift"], swiftSettings: [.define("TARGET")]),
// chapter_computational_complexity
.executableTarget(name: "time_complexity", path: "chapter_computational_complexity", sources: ["time_complexity.swift"]),
.executableTarget(name: "worst_best_time_complexity", path: "chapter_computational_complexity", sources: ["worst_best_time_complexity.swift"]),
@ -82,6 +86,8 @@ let package = Package(
// chapter_graph
.executableTarget(name: "graph_adjacency_matrix", dependencies: ["utils"], path: "chapter_graph", sources: ["graph_adjacency_matrix.swift"]),
.executableTarget(name: "graph_adjacency_list", dependencies: ["utils"], path: "chapter_graph", sources: ["graph_adjacency_list.swift"]),
.executableTarget(name: "graph_bfs", dependencies: ["utils", "graph_adjacency_list_target"], path: "chapter_graph", sources: ["graph_bfs.swift"]),
.executableTarget(name: "graph_dfs", dependencies: ["utils", "graph_adjacency_list_target"], path: "chapter_graph", sources: ["graph_dfs.swift"]),
// chapter_searching
.executableTarget(name: "linear_search", dependencies: ["utils"], path: "chapter_searching", sources: ["linear_search.swift"]),
.executableTarget(name: "binary_search", path: "chapter_searching", sources: ["binary_search.swift"]),

@ -7,13 +7,13 @@
import utils
/* */
class GraphAdjList {
public class GraphAdjList {
// 使
// adjList Vertex
private var adjList: [Vertex: [Vertex]]
public private(set) var adjList: [Vertex: [Vertex]]
/* */
init(edges: [[Vertex]]) {
public init(edges: [[Vertex]]) {
adjList = [:]
//
for edge in edges {
@ -24,12 +24,12 @@ class GraphAdjList {
}
/* */
func size() -> Int {
public func size() -> Int {
adjList.count
}
/* */
func addEdge(vet1: Vertex, vet2: Vertex) {
public func addEdge(vet1: Vertex, vet2: Vertex) {
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
fatalError("参数错误")
}
@ -39,7 +39,7 @@ class GraphAdjList {
}
/* */
func removeEdge(vet1: Vertex, vet2: Vertex) {
public func removeEdge(vet1: Vertex, vet2: Vertex) {
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
fatalError("参数错误")
}
@ -49,7 +49,7 @@ class GraphAdjList {
}
/* */
func addVertex(vet: Vertex) {
public func addVertex(vet: Vertex) {
if adjList[vet] != nil {
return
}
@ -58,7 +58,7 @@ class GraphAdjList {
}
/* */
func removeVertex(vet: Vertex) {
public func removeVertex(vet: Vertex) {
if adjList[vet] == nil {
fatalError("参数错误")
}
@ -71,7 +71,7 @@ class GraphAdjList {
}
/* */
func print() {
public func print() {
Swift.print("邻接表 =")
for entry in adjList {
var tmp: [Int] = []
@ -83,12 +83,14 @@ class GraphAdjList {
}
}
#if !TARGET
@main
enum GraphAdjacencyList {
/* Driver Code */
static func main() {
/* */
let v = Vertex.valsToVets([1, 3, 2, 5, 4])
let v = Vertex.valsToVets(vals: [1, 3, 2, 5, 4])
let edges = [[v[0], v[1]], [v[0], v[3]], [v[1], v[2]], [v[2], v[3]], [v[2], v[4]], [v[3], v[4]]]
let graph = GraphAdjList(edges: edges)
print("\n初始化后,图为")
@ -119,3 +121,5 @@ enum GraphAdjacencyList {
graph.print()
}
}
#endif

@ -0,0 +1,56 @@
/**
* File: graph_bfs.swift
* Created Time: 2023-02-21
* Author: nuomi1 (nuomi1@qq.com)
*/
import graph_adjacency_list_target
import utils
/* 广 BFS */
// 使便
func graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {
//
var res: [Vertex] = []
// 访
var visited: Set<Vertex> = [startVet]
// BFS
var que: [Vertex] = [startVet]
// vet 访
while !que.isEmpty {
let vet = que.removeFirst() //
res.append(vet) // 访
//
for adjVet in graph.adjList[vet] ?? [] {
if visited.contains(adjVet) {
continue // 访
}
que.append(adjVet) // 访
visited.insert(adjVet) // 访
}
}
//
return res
}
@main
enum GraphBFS {
/* Driver Code */
static func main() {
/* */
let v = Vertex.valsToVets(vals: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
let edges = [
[v[0], v[1]], [v[0], v[3]], [v[1], v[2]], [v[1], v[4]],
[v[2], v[5]], [v[3], v[4]], [v[3], v[6]], [v[4], v[5]],
[v[4], v[7]], [v[5], v[8]], [v[6], v[7]], [v[7], v[8]],
]
let graph = GraphAdjList(edges: edges)
print("\n初始化后,图为")
graph.print()
/* 广 BFS */
let res = graphBFS(graph: graph, startVet: v[0])
print("\n广度优先遍历BFS顶点序列为")
print(Vertex.vetsToVals(vets: res))
}
}

@ -0,0 +1,54 @@
/**
* File: graph_dfs.swift
* Created Time: 2023-02-21
* Author: nuomi1 (nuomi1@qq.com)
*/
import graph_adjacency_list_target
import utils
/* DFS */
func dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {
res.append(vet) // 访
visited.insert(vet) // 访
//
for adjVet in graph.adjList[vet] ?? [] {
if visited.contains(adjVet) {
continue // 访
}
// 访
dfs(graph: graph, visited: &visited, res: &res, vet: adjVet)
}
}
/* DFS */
// 使便
func graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {
//
var res: [Vertex] = []
// 访
var visited: Set<Vertex> = []
dfs(graph: graph, visited: &visited, res: &res, vet: startVet)
return res
}
@main
enum GraphDFS {
/* Driver Code */
static func main() {
/* */
let v = Vertex.valsToVets(vals: [0, 1, 2, 3, 4, 5, 6])
let edges = [
[v[0], v[1]], [v[0], v[3]], [v[1], v[2]],
[v[2], v[5]], [v[4], v[5]], [v[5], v[6]],
]
let graph = GraphAdjList(edges: edges)
print("\n初始化后,图为")
graph.print()
/* DFS */
let res = graphDFS(graph: graph, startVet: v[0])
print("\n深度优先遍历DFS顶点序列为")
print(Vertex.vetsToVals(vets: res))
}
}

@ -21,7 +21,7 @@ public class Vertex: Hashable {
}
/* vals vets */
public static func valsToVets(_ vals: [Int]) -> [Vertex] {
public static func valsToVets(vals: [Int]) -> [Vertex] {
var vets: [Vertex] = []
for val in vals {
vets.append(Vertex(val: val))
@ -30,7 +30,7 @@ public class Vertex: Hashable {
}
/* vets vals */
public static func vetsToVals(_ vets: [Vertex]) -> [Int] {
public static func vetsToVals(vets: [Vertex]) -> [Int] {
var vals: [Int] = []
for vet in vets {
vals.append(vet.val)

@ -79,7 +79,7 @@ BFS 常借助「队列」来实现。队列具有“先入先出”的性质,
=== "Swift"
```swift title="graph_bfs.swift"
[class]{}-[func]{graphBFS}
```
=== "Zig"
@ -196,7 +196,9 @@ BFS 常借助「队列」来实现。队列具有“先入先出”的性质,
=== "Swift"
```swift title="graph_dfs.swift"
[class]{}-[func]{dfs}
[class]{}-[func]{graphDFS}
```
=== "Zig"

Loading…
Cancel
Save