This commit is contained in:
krahets
2024-03-25 22:43:12 +08:00
parent 22017aa8e5
commit 87af663929
70 changed files with 7428 additions and 32 deletions

View File

@ -1039,6 +1039,91 @@ comments: true
}
```
=== "Kotlin"
```kotlin title="graph_adjacency_matrix.kt"
/* 基于邻接矩阵实现的无向图类 */
class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
val vertices: MutableList<Int> = ArrayList() // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
val adjMat: MutableList<MutableList<Int>> = ArrayList() // 邻接矩阵,行列索引对应“顶点索引”
/* 构造函数 */
init {
// 添加顶点
for (vertex in vertices) {
addVertex(vertex)
}
// 添加边
// 请注意edges 元素代表顶点索引,即对应 vertices 元素索引
for (edge in edges) {
addEdge(edge[0], edge[1])
}
}
/* 获取顶点数量 */
fun size(): Int {
return vertices.size
}
/* 添加顶点 */
fun addVertex(value: Int) {
val n = size()
// 向顶点列表中添加新顶点的值
vertices.add(value)
// 在邻接矩阵中添加一行
val newRow: MutableList<Int> = mutableListOf()
for (j in 0..<n) {
newRow.add(0)
}
adjMat.add(newRow)
// 在邻接矩阵中添加一列
for (row in adjMat) {
row.add(0)
}
}
/* 删除顶点 */
fun removeVertex(index: Int) {
if (index >= size()) throw IndexOutOfBoundsException()
// 在顶点列表中移除索引 index 的顶点
vertices.removeAt(index)
// 在邻接矩阵中删除索引 index 的行
adjMat.removeAt(index)
// 在邻接矩阵中删除索引 index 的列
for (row in adjMat) {
row.removeAt(index)
}
}
/* 添加边 */
// 参数 i, j 对应 vertices 元素索引
fun addEdge(i: Int, j: Int) {
// 索引越界与相等处理
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw java.lang.IndexOutOfBoundsException()
// 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) == (j, i)
adjMat[i][j] = 1;
adjMat[j][i] = 1;
}
/* 删除边 */
// 参数 i, j 对应 vertices 元素索引
fun removeEdge(i: Int, j: Int) {
// 索引越界与相等处理
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw java.lang.IndexOutOfBoundsException()
adjMat[i][j] = 0;
adjMat[j][i] = 0;
}
/* 打印邻接矩阵 */
fun print() {
print("顶点列表 = ")
println(vertices);
println("邻接矩阵 =");
printMatrix(adjMat)
}
}
```
=== "Zig"
```zig title="graph_adjacency_matrix.zig"
@ -2061,6 +2146,81 @@ comments: true
}
```
=== "Kotlin"
```kotlin title="graph_adjacency_list.kt"
/* 基于邻接表实现的无向图类 */
class GraphAdjList(edges: Array<Array<Vertex?>>) {
// 邻接表key顶点value该顶点的所有邻接顶点
val adjList: MutableMap<Vertex, MutableList<Vertex>> = HashMap()
/* 构造函数 */
init {
// 添加所有顶点和边
for (edge in edges) {
addVertex(edge[0]!!);
addVertex(edge[1]!!);
addEdge(edge[0]!!, edge[1]!!);
}
}
/* 获取顶点数量 */
fun size(): Int {
return adjList.size
}
/* 添加边 */
fun addEdge(vet1: Vertex, vet2: Vertex) {
if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
throw IllegalArgumentException()
// 添加边 vet1 - vet2
adjList[vet1]?.add(vet2)
adjList[vet2]?.add(vet1);
}
/* 删除边 */
fun removeEdge(vet1: Vertex, vet2: Vertex) {
if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)
throw IllegalArgumentException()
// 删除边 vet1 - vet2
adjList[vet1]?.remove(vet2);
adjList[vet2]?.remove(vet1);
}
/* 添加顶点 */
fun addVertex(vet: Vertex) {
if (adjList.containsKey(vet))
return
// 在邻接表中添加一个新链表
adjList[vet] = mutableListOf()
}
/* 删除顶点 */
fun removeVertex(vet: Vertex) {
if (!adjList.containsKey(vet))
throw IllegalArgumentException()
// 在邻接表中删除顶点 vet 对应的链表
adjList.remove(vet);
// 遍历其他顶点的链表,删除所有包含 vet 的边
for (list in adjList.values) {
list.remove(vet)
}
}
/* 打印邻接表 */
fun print() {
println("邻接表 =")
for (pair in adjList.entries) {
val tmp = ArrayList<Int>()
for (vertex in pair.value) {
tmp.add(vertex.value)
}
println("${pair.key.value}: $tmp,")
}
}
}
```
=== "Zig"
```zig title="graph_adjacency_list.zig"

View File

@ -413,6 +413,37 @@ BFS 通常借助队列来实现,代码如下所示。队列具有“先入先
}
```
=== "Kotlin"
```kotlin title="graph_bfs.kt"
/* 广度优先遍历 */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
fun graphBFS(graph: GraphAdjList, startVet: Vertex): List<Vertex> {
// 顶点遍历序列
val res: MutableList<Vertex> = ArrayList()
// 哈希表,用于记录已被访问过的顶点
val visited: MutableSet<Vertex> = HashSet()
visited.add(startVet)
// 队列用于实现 BFS
val que: Queue<Vertex> = LinkedList()
que.offer(startVet)
// 以顶点 vet 为起点,循环直至访问完所有顶点
while (!que.isEmpty()) {
val vet = que.poll() // 队首顶点出队
res.add(vet) // 记录访问顶点
// 遍历该顶点的所有邻接顶点
for (adjVet in graph.adjList[vet]!!) {
if (visited.contains(adjVet)) continue // 跳过已被访问的顶点
que.offer(adjVet) // 只入队未访问的顶点
visited.add(adjVet) // 标记该顶点已被访问
}
}
// 返回顶点遍历序列
return res
}
```
=== "Zig"
```zig title="graph_bfs.zig"
@ -819,6 +850,41 @@ BFS 通常借助队列来实现,代码如下所示。队列具有“先入先
}
```
=== "Kotlin"
```kotlin title="graph_dfs.kt"
/* 深度优先遍历辅助函数 */
fun dfs(
graph: GraphAdjList,
visited: MutableSet<Vertex?>,
res: MutableList<Vertex?>,
vet: Vertex?
) {
res.add(vet) // 记录访问顶点
visited.add(vet) // 标记该顶点已被访问
// 遍历该顶点的所有邻接顶点
for (adjVet in graph.adjList[vet]!!) {
if (visited.contains(adjVet)) continue // 跳过已被访问的顶点
// 递归访问邻接顶点
dfs(graph, visited, res, adjVet)
}
}
/* 深度优先遍历 */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
fun graphDFS(
graph: GraphAdjList,
startVet: Vertex?
): List<Vertex?> {
// 顶点遍历序列
val res: MutableList<Vertex?> = ArrayList()
// 哈希表,用于记录已被访问过的顶点
val visited: MutableSet<Vertex?> = HashSet()
dfs(graph, visited, res, startVet)
return res
}
```
=== "Zig"
```zig title="graph_dfs.zig"