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>
This commit is contained in:
nuomi1
2023-02-22 19:41:31 +08:00
committed by GitHub
parent f2d2cca5f1
commit c6c4c9d997
7 changed files with 136 additions and 13 deletions

View File

@@ -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

View File

@@ -0,0 +1 @@
graph_adjacency_list.swift

View File

@@ -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))
}
}

View File

@@ -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))
}
}