mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-28 12:52:57 +08:00
build
This commit is contained in:
@ -1044,10 +1044,10 @@ comments: true
|
||||
```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 @@ comments: true
|
||||
// 向顶点列表中添加新顶点的值
|
||||
vertices.add(value)
|
||||
// 在邻接矩阵中添加一行
|
||||
val newRow: MutableList<Int> = mutableListOf()
|
||||
val newRow = mutableListOf<Int>()
|
||||
for (j in 0..<n) {
|
||||
newRow.add(0)
|
||||
}
|
||||
@ -1084,7 +1084,8 @@ comments: true
|
||||
|
||||
/* 删除顶点 */
|
||||
fun removeVertex(index: Int) {
|
||||
if (index >= size()) throw IndexOutOfBoundsException()
|
||||
if (index >= size())
|
||||
throw IndexOutOfBoundsException()
|
||||
// 在顶点列表中移除索引 index 的顶点
|
||||
vertices.removeAt(index)
|
||||
// 在邻接矩阵中删除索引 index 的行
|
||||
@ -1099,7 +1100,8 @@ comments: true
|
||||
// 参数 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 @@ comments: true
|
||||
// 参数 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 @@ comments: true
|
||||
/* 基于邻接表实现的无向图类 */
|
||||
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 @@ comments: true
|
||||
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)
|
||||
}
|
||||
|
@ -418,24 +418,24 @@ BFS 通常借助队列来实现,代码如下所示。队列具有“先入先
|
||||
```kotlin title="graph_bfs.kt"
|
||||
/* 广度优先遍历 */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
fun graphBFS(graph: GraphAdjList, startVet: Vertex): List<Vertex> {
|
||||
fun graphBFS(graph: GraphAdjList, startVet: Vertex): MutableList<Vertex?> {
|
||||
// 顶点遍历序列
|
||||
val res: MutableList<Vertex> = ArrayList()
|
||||
val res = mutableListOf<Vertex?>()
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
val visited: MutableSet<Vertex> = HashSet()
|
||||
val visited = HashSet<Vertex>()
|
||||
visited.add(startVet)
|
||||
// 队列用于实现 BFS
|
||||
val que: Queue<Vertex> = LinkedList()
|
||||
val que = LinkedList<Vertex>()
|
||||
que.offer(startVet)
|
||||
// 以顶点 vet 为起点,循环直至访问完所有顶点
|
||||
while (!que.isEmpty()) {
|
||||
val vet = que.poll() // 队首顶点出队
|
||||
res.add(vet) // 记录访问顶点
|
||||
res.add(vet) // 记录访问顶点
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
for (adjVet in graph.adjList[vet]!!) {
|
||||
if (visited.contains(adjVet)) continue // 跳过已被访问的顶点
|
||||
|
||||
que.offer(adjVet) // 只入队未访问的顶点
|
||||
if (visited.contains(adjVet))
|
||||
continue // 跳过已被访问的顶点
|
||||
que.offer(adjVet) // 只入队未访问的顶点
|
||||
visited.add(adjVet) // 标记该顶点已被访问
|
||||
}
|
||||
}
|
||||
@ -866,11 +866,12 @@ BFS 通常借助队列来实现,代码如下所示。队列具有“先入先
|
||||
res: MutableList<Vertex?>,
|
||||
vet: Vertex?
|
||||
) {
|
||||
res.add(vet) // 记录访问顶点
|
||||
res.add(vet) // 记录访问顶点
|
||||
visited.add(vet) // 标记该顶点已被访问
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
for (adjVet in graph.adjList[vet]!!) {
|
||||
if (visited.contains(adjVet)) continue // 跳过已被访问的顶点
|
||||
if (visited.contains(adjVet))
|
||||
continue // 跳过已被访问的顶点
|
||||
// 递归访问邻接顶点
|
||||
dfs(graph, visited, res, adjVet)
|
||||
}
|
||||
@ -878,14 +879,11 @@ BFS 通常借助队列来实现,代码如下所示。队列具有“先入先
|
||||
|
||||
/* 深度优先遍历 */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
fun graphDFS(
|
||||
graph: GraphAdjList,
|
||||
startVet: Vertex?
|
||||
): List<Vertex?> {
|
||||
fun graphDFS(graph: GraphAdjList, startVet: Vertex?): MutableList<Vertex?> {
|
||||
// 顶点遍历序列
|
||||
val res: MutableList<Vertex?> = ArrayList()
|
||||
val res = mutableListOf<Vertex?>()
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
val visited: MutableSet<Vertex?> = HashSet()
|
||||
val visited = HashSet<Vertex?>()
|
||||
dfs(graph, visited, res, startVet)
|
||||
return res
|
||||
}
|
||||
|
Reference in New Issue
Block a user