mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-30 13:53:36 +08:00
build
This commit is contained in:
@ -1044,10 +1044,10 @@ Below is the implementation code for graphs represented using an adjacency matri
|
||||
```kotlin title="graph_adjacency_matrix.kt"
|
||||
/* 基于邻接矩阵实现的无向图类 */
|
||||
class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
|
||||
val vertices: MutableList<Int> = ArrayList() // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
val adjMat: MutableList<MutableList<Int>> = ArrayList() // 邻接矩阵,行列索引对应“顶点索引”
|
||||
val vertices = mutableListOf<Int>() // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
val adjMat = mutableListOf<MutableList<Int>>() // 邻接矩阵,行列索引对应“顶点索引”
|
||||
|
||||
/* 构造函数 */
|
||||
/* 构造方法 */
|
||||
init {
|
||||
// 添加顶点
|
||||
for (vertex in vertices) {
|
||||
@ -1071,7 +1071,7 @@ Below is the implementation code for graphs represented using an adjacency matri
|
||||
// 向顶点列表中添加新顶点的值
|
||||
vertices.add(value)
|
||||
// 在邻接矩阵中添加一行
|
||||
val newRow: MutableList<Int> = mutableListOf()
|
||||
val newRow = mutableListOf<Int>()
|
||||
for (j in 0..<n) {
|
||||
newRow.add(0)
|
||||
}
|
||||
@ -1084,7 +1084,8 @@ Below is the implementation code for graphs represented using an adjacency matri
|
||||
|
||||
/* 删除顶点 */
|
||||
fun removeVertex(index: Int) {
|
||||
if (index >= size()) throw IndexOutOfBoundsException()
|
||||
if (index >= size())
|
||||
throw IndexOutOfBoundsException()
|
||||
// 在顶点列表中移除索引 index 的顶点
|
||||
vertices.removeAt(index)
|
||||
// 在邻接矩阵中删除索引 index 的行
|
||||
@ -1099,7 +1100,8 @@ Below is the implementation code for graphs represented using an adjacency matri
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
fun addEdge(i: Int, j: Int) {
|
||||
// 索引越界与相等处理
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw java.lang.IndexOutOfBoundsException()
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
|
||||
throw IndexOutOfBoundsException()
|
||||
// 在无向图中,邻接矩阵关于主对角线对称,即满足 (i, j) == (j, i)
|
||||
adjMat[i][j] = 1;
|
||||
adjMat[j][i] = 1;
|
||||
@ -1109,7 +1111,8 @@ Below is the implementation code for graphs represented using an adjacency matri
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
fun removeEdge(i: Int, j: Int) {
|
||||
// 索引越界与相等处理
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw java.lang.IndexOutOfBoundsException()
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
|
||||
throw IndexOutOfBoundsException()
|
||||
adjMat[i][j] = 0;
|
||||
adjMat[j][i] = 0;
|
||||
}
|
||||
@ -2158,9 +2161,9 @@ Additionally, we use the `Vertex` class to represent vertices in the adjacency l
|
||||
/* 基于邻接表实现的无向图类 */
|
||||
class GraphAdjList(edges: Array<Array<Vertex?>>) {
|
||||
// 邻接表,key:顶点,value:该顶点的所有邻接顶点
|
||||
val adjList: MutableMap<Vertex, MutableList<Vertex>> = HashMap()
|
||||
val adjList = HashMap<Vertex, MutableList<Vertex>>()
|
||||
|
||||
/* 构造函数 */
|
||||
/* 构造方法 */
|
||||
init {
|
||||
// 添加所有顶点和边
|
||||
for (edge in edges) {
|
||||
@ -2217,7 +2220,7 @@ Additionally, we use the `Vertex` class to represent vertices in the adjacency l
|
||||
fun print() {
|
||||
println("邻接表 =")
|
||||
for (pair in adjList.entries) {
|
||||
val tmp = ArrayList<Int>()
|
||||
val tmp = mutableListOf<Int>()
|
||||
for (vertex in pair.value) {
|
||||
tmp.add(vertex.value)
|
||||
}
|
||||
|
Reference in New Issue
Block a user