2023-02-05 15:41:23 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: graph_adjacency_list.swift
|
|
|
|
|
* Created Time: 2023-02-01
|
|
|
|
|
* Author: nuomi1 (nuomi1@qq.com)
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-21 21:35:28 +08:00
|
|
|
|
import utils
|
2023-02-05 15:41:23 +08:00
|
|
|
|
|
|
|
|
|
/* 基于邻接表实现的无向图类 */
|
2023-02-22 19:41:31 +08:00
|
|
|
|
public class GraphAdjList {
|
2023-02-23 20:32:26 +08:00
|
|
|
|
// 邻接表,key: 顶点,value:该顶点的所有邻接顶点
|
2023-02-22 19:41:31 +08:00
|
|
|
|
public private(set) var adjList: [Vertex: [Vertex]]
|
2023-02-05 15:41:23 +08:00
|
|
|
|
|
2023-02-15 03:35:54 +08:00
|
|
|
|
/* 构造方法 */
|
2023-02-22 19:41:31 +08:00
|
|
|
|
public init(edges: [[Vertex]]) {
|
2023-02-05 15:41:23 +08:00
|
|
|
|
adjList = [:]
|
|
|
|
|
// 添加所有顶点和边
|
|
|
|
|
for edge in edges {
|
|
|
|
|
addVertex(vet: edge[0])
|
|
|
|
|
addVertex(vet: edge[1])
|
|
|
|
|
addEdge(vet1: edge[0], vet2: edge[1])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取顶点数量 */
|
2023-02-22 19:41:31 +08:00
|
|
|
|
public func size() -> Int {
|
2023-02-05 15:41:23 +08:00
|
|
|
|
adjList.count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 添加边 */
|
2023-02-22 19:41:31 +08:00
|
|
|
|
public func addEdge(vet1: Vertex, vet2: Vertex) {
|
2023-02-05 15:41:23 +08:00
|
|
|
|
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
|
|
|
|
|
fatalError("参数错误")
|
|
|
|
|
}
|
|
|
|
|
// 添加边 vet1 - vet2
|
2023-02-21 21:35:28 +08:00
|
|
|
|
adjList[vet1]?.append(vet2)
|
|
|
|
|
adjList[vet2]?.append(vet1)
|
2023-02-05 15:41:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除边 */
|
2023-02-22 19:41:31 +08:00
|
|
|
|
public func removeEdge(vet1: Vertex, vet2: Vertex) {
|
2023-02-05 15:41:23 +08:00
|
|
|
|
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
|
|
|
|
|
fatalError("参数错误")
|
|
|
|
|
}
|
|
|
|
|
// 删除边 vet1 - vet2
|
2023-02-21 21:35:28 +08:00
|
|
|
|
adjList[vet1]?.removeAll(where: { $0 == vet2 })
|
|
|
|
|
adjList[vet2]?.removeAll(where: { $0 == vet1 })
|
2023-02-05 15:41:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 添加顶点 */
|
2023-02-22 19:41:31 +08:00
|
|
|
|
public func addVertex(vet: Vertex) {
|
2023-02-05 15:41:23 +08:00
|
|
|
|
if adjList[vet] != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-02-15 03:35:54 +08:00
|
|
|
|
// 在邻接表中添加一个新链表
|
2023-02-05 15:41:23 +08:00
|
|
|
|
adjList[vet] = []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除顶点 */
|
2023-02-22 19:41:31 +08:00
|
|
|
|
public func removeVertex(vet: Vertex) {
|
2023-02-05 15:41:23 +08:00
|
|
|
|
if adjList[vet] == nil {
|
|
|
|
|
fatalError("参数错误")
|
|
|
|
|
}
|
2023-02-15 03:35:54 +08:00
|
|
|
|
// 在邻接表中删除顶点 vet 对应的链表
|
2023-02-05 15:41:23 +08:00
|
|
|
|
adjList.removeValue(forKey: vet)
|
2023-02-15 03:35:54 +08:00
|
|
|
|
// 遍历其它顶点的链表,删除所有包含 vet 的边
|
2023-02-05 15:41:23 +08:00
|
|
|
|
for key in adjList.keys {
|
2023-02-21 21:35:28 +08:00
|
|
|
|
adjList[key]?.removeAll(where: { $0 == vet })
|
2023-02-05 15:41:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 打印邻接表 */
|
2023-02-22 19:41:31 +08:00
|
|
|
|
public func print() {
|
2023-02-05 15:41:23 +08:00
|
|
|
|
Swift.print("邻接表 =")
|
|
|
|
|
for entry in adjList {
|
|
|
|
|
var tmp: [Int] = []
|
|
|
|
|
for vertex in entry.value {
|
|
|
|
|
tmp.append(vertex.val)
|
|
|
|
|
}
|
|
|
|
|
Swift.print("\(entry.key.val): \(tmp),")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 19:41:31 +08:00
|
|
|
|
#if !TARGET
|
|
|
|
|
|
2023-02-05 15:41:23 +08:00
|
|
|
|
@main
|
|
|
|
|
enum GraphAdjacencyList {
|
|
|
|
|
/* Driver Code */
|
|
|
|
|
static func main() {
|
|
|
|
|
/* 初始化无向图 */
|
2023-02-22 19:41:31 +08:00
|
|
|
|
let v = Vertex.valsToVets(vals: [1, 3, 2, 5, 4])
|
2023-02-21 21:35:28 +08:00
|
|
|
|
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]]]
|
2023-02-05 15:41:23 +08:00
|
|
|
|
let graph = GraphAdjList(edges: edges)
|
|
|
|
|
print("\n初始化后,图为")
|
|
|
|
|
graph.print()
|
|
|
|
|
|
|
|
|
|
/* 添加边 */
|
2023-02-21 21:35:28 +08:00
|
|
|
|
// 顶点 1, 2 即 v[0], v[2]
|
|
|
|
|
graph.addEdge(vet1: v[0], vet2: v[2])
|
2023-02-05 15:41:23 +08:00
|
|
|
|
print("\n添加边 1-2 后,图为")
|
|
|
|
|
graph.print()
|
|
|
|
|
|
|
|
|
|
/* 删除边 */
|
2023-02-21 21:35:28 +08:00
|
|
|
|
// 顶点 1, 3 即 v[0], v[1]
|
|
|
|
|
graph.removeEdge(vet1: v[0], vet2: v[1])
|
2023-02-05 15:41:23 +08:00
|
|
|
|
print("\n删除边 1-3 后,图为")
|
|
|
|
|
graph.print()
|
|
|
|
|
|
|
|
|
|
/* 添加顶点 */
|
|
|
|
|
let v5 = Vertex(val: 6)
|
|
|
|
|
graph.addVertex(vet: v5)
|
|
|
|
|
print("\n添加顶点 6 后,图为")
|
|
|
|
|
graph.print()
|
|
|
|
|
|
|
|
|
|
/* 删除顶点 */
|
2023-02-21 21:35:28 +08:00
|
|
|
|
// 顶点 3 即 v[1]
|
|
|
|
|
graph.removeVertex(vet: v[1])
|
2023-02-05 15:41:23 +08:00
|
|
|
|
print("\n删除顶点 3 后,图为")
|
|
|
|
|
graph.print()
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-22 19:41:31 +08:00
|
|
|
|
|
|
|
|
|
#endif
|