hello-algo/codes/swift/chapter_graph/graph_adjacency_list.swift
nuomi1 7359a7cb4b
Review Swift codes (#1150)
* feat(swift): review for chapter_computational_complexity

* feat(swift): review for chapter_data_structure

* feat(swift): review for chapter_array_and_linkedlist

* feat(swift): review for chapter_stack_and_queue

* feat(swift): review for chapter_hashing

* feat(swift): review for chapter_tree

* feat(swift): add codes for heap article

* feat(swift): review for chapter_heap

* feat(swift): review for chapter_graph

* feat(swift): review for chapter_searching

* feat(swift): review for chapter_sorting

* feat(swift): review for chapter_divide_and_conquer

* feat(swift): review for chapter_backtracking

* feat(swift): review for chapter_dynamic_programming

* feat(swift): review for chapter_greedy

* feat(swift): review for utils

* feat(swift): update ci tool

* feat(swift): trailing closure

* feat(swift): array init

* feat(swift): map index
2024-03-20 21:15:39 +08:00

121 lines
3.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* File: graph_adjacency_list.swift
* Created Time: 2023-02-01
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
/* */
public class GraphAdjList {
// keyvalue
public private(set) var adjList: [Vertex: [Vertex]]
/* */
public init(edges: [[Vertex]]) {
adjList = [:]
//
for edge in edges {
addVertex(vet: edge[0])
addVertex(vet: edge[1])
addEdge(vet1: edge[0], vet2: edge[1])
}
}
/* */
public func size() -> Int {
adjList.count
}
/* */
public func addEdge(vet1: Vertex, vet2: Vertex) {
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
fatalError("参数错误")
}
// vet1 - vet2
adjList[vet1]?.append(vet2)
adjList[vet2]?.append(vet1)
}
/* */
public func removeEdge(vet1: Vertex, vet2: Vertex) {
if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {
fatalError("参数错误")
}
// vet1 - vet2
adjList[vet1]?.removeAll { $0 == vet2 }
adjList[vet2]?.removeAll { $0 == vet1 }
}
/* */
public func addVertex(vet: Vertex) {
if adjList[vet] != nil {
return
}
//
adjList[vet] = []
}
/* */
public func removeVertex(vet: Vertex) {
if adjList[vet] == nil {
fatalError("参数错误")
}
// vet
adjList.removeValue(forKey: vet)
// vet
for key in adjList.keys {
adjList[key]?.removeAll { $0 == vet }
}
}
/* */
public func print() {
Swift.print("邻接表 =")
for (vertex, list) in adjList {
let list = list.map { $0.val }
Swift.print("\(vertex.val): \(list),")
}
}
}
#if !TARGET
@main
enum GraphAdjacencyList {
/* Driver Code */
static func main() {
/* */
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初始化后,图为")
graph.print()
/* */
// 1, 2 v[0], v[2]
graph.addEdge(vet1: v[0], vet2: v[2])
print("\n添加边 1-2 后,图为")
graph.print()
/* */
// 1, 3 v[0], v[1]
graph.removeEdge(vet1: v[0], vet2: v[1])
print("\n删除边 1-3 后,图为")
graph.print()
/* */
let v5 = Vertex(val: 6)
graph.addVertex(vet: v5)
print("\n添加顶点 6 后,图为")
graph.print()
/* */
// 3 v[1]
graph.removeVertex(vet: v[1])
print("\n删除顶点 3 后,图为")
graph.print()
}
}
#endif