mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-12 20:00:58 +00:00
Many bug fixes and improvements (#1270)
* Add Ruby and Kotlin icons Add the avatar of @curtishd * Update README * Synchronize zh-hant and zh versions. * Translate the pythontutor blocks to traditional Chinese * Fix en/mkdocs.yml * Update the landing page of the en version. * Fix the Dockerfile * Refine the en landingpage * Fix en landing page * Reset the README.md
This commit is contained in:
@@ -11,7 +11,7 @@ import utils.Vertex
|
||||
/* 基於鄰接表實現的無向圖類別 */
|
||||
class GraphAdjList(edges: Array<Array<Vertex?>>) {
|
||||
// 鄰接表,key:頂點,value:該頂點的所有鄰接頂點
|
||||
val adjList: MutableMap<Vertex, MutableList<Vertex>> = HashMap()
|
||||
val adjList = HashMap<Vertex, MutableList<Vertex>>()
|
||||
|
||||
/* 建構子 */
|
||||
init {
|
||||
@@ -70,11 +70,11 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
|
||||
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)
|
||||
tmp.add(vertex._val)
|
||||
}
|
||||
println("${pair.key.value}: $tmp,")
|
||||
println("${pair.key._val}: $tmp,")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,7 +82,7 @@ class GraphAdjList(edges: Array<Array<Vertex?>>) {
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* 初始化無向圖 */
|
||||
val v: Array<Vertex?> = Vertex.valsToVets(intArrayOf(1, 3, 2, 5, 4))
|
||||
val v = Vertex.valsToVets(intArrayOf(1, 3, 2, 5, 4))
|
||||
val edges = arrayOf(
|
||||
arrayOf(v[0], v[1]),
|
||||
arrayOf(v[0], v[3]),
|
||||
|
||||
@@ -10,8 +10,8 @@ import utils.printMatrix
|
||||
|
||||
/* 基於鄰接矩陣實現的無向圖類別 */
|
||||
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 {
|
||||
@@ -32,12 +32,12 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
|
||||
}
|
||||
|
||||
/* 新增頂點 */
|
||||
fun addVertex(value: Int) {
|
||||
fun addVertex(_val: Int) {
|
||||
val n = size()
|
||||
// 向頂點串列中新增新頂點的值
|
||||
vertices.add(value)
|
||||
vertices.add(_val)
|
||||
// 在鄰接矩陣中新增一行
|
||||
val newRow: MutableList<Int> = mutableListOf()
|
||||
val newRow = mutableListOf<Int>()
|
||||
for (j in 0..<n) {
|
||||
newRow.add(0)
|
||||
}
|
||||
@@ -50,7 +50,8 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
|
||||
|
||||
/* 刪除頂點 */
|
||||
fun removeVertex(index: Int) {
|
||||
if (index >= size()) throw IndexOutOfBoundsException()
|
||||
if (index >= size())
|
||||
throw IndexOutOfBoundsException()
|
||||
// 在頂點串列中移除索引 index 的頂點
|
||||
vertices.removeAt(index)
|
||||
// 在鄰接矩陣中刪除索引 index 的行
|
||||
@@ -65,7 +66,8 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
|
||||
// 參數 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;
|
||||
@@ -75,7 +77,8 @@ class GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {
|
||||
// 參數 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;
|
||||
}
|
||||
|
||||
@@ -11,24 +11,24 @@ import java.util.*
|
||||
|
||||
/* 廣度優先走訪 */
|
||||
// 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點
|
||||
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) // 標記該頂點已被訪問
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,11 +15,12 @@ fun dfs(
|
||||
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)
|
||||
}
|
||||
@@ -27,14 +28,11 @@ fun dfs(
|
||||
|
||||
/* 深度優先走訪 */
|
||||
// 使用鄰接表來表示圖,以便獲取指定頂點的所有鄰接頂點
|
||||
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