Bug fixes and improvements (#1813)

* Sync zh and zh-hant version.

* Add the Warp sponsor banner.

* Update README with acknowledgments and Warp recommendation

Added acknowledgments and a recommendation for the Warp terminal application.

* Update README.md

* Update links in README.md to use HTTPS

* Sync zh and zh-hant versions.

* Add special thanks for Warp spnsorship.

* Use official warp image link.
This commit is contained in:
Yudong Jin
2025-09-23 20:44:38 +08:00
committed by GitHub
parent 790a6d17e1
commit 44effb07e6
37 changed files with 1009 additions and 636 deletions
@@ -11,7 +11,7 @@ use std::collections::HashMap;
/* 基於鄰接表實現的無向圖型別 */
pub struct GraphAdjList {
// 鄰接表,key:頂點,value:該頂點的所有鄰接頂點
pub adj_list: HashMap<Vertex, Vec<Vertex>>,
pub adj_list: HashMap<Vertex, Vec<Vertex>>, // maybe HashSet<Vertex> for value part is better?
}
impl GraphAdjList {
@@ -38,31 +38,27 @@ impl GraphAdjList {
/* 新增邊 */
pub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {
if !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2
{
if vet1 == vet2 {
panic!("value error");
}
// 新增邊 vet1 - vet2
self.adj_list.get_mut(&vet1).unwrap().push(vet2);
self.adj_list.get_mut(&vet2).unwrap().push(vet1);
self.adj_list.entry(vet1).or_default().push(vet2);
self.adj_list.entry(vet2).or_default().push(vet1);
}
/* 刪除邊 */
#[allow(unused)]
pub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {
if !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2
{
if vet1 == vet2 {
panic!("value error");
}
// 刪除邊 vet1 - vet2
self.adj_list
.get_mut(&vet1)
.unwrap()
.retain(|&vet| vet != vet2);
.entry(vet1)
.and_modify(|v| v.retain(|&e| e != vet2));
self.adj_list
.get_mut(&vet2)
.unwrap()
.retain(|&vet| vet != vet1);
.entry(vet2)
.and_modify(|v| v.retain(|&e| e != vet1));
}
/* 新增頂點 */
@@ -77,9 +73,6 @@ impl GraphAdjList {
/* 刪除頂點 */
#[allow(unused)]
pub fn remove_vertex(&mut self, vet: Vertex) {
if !self.adj_list.contains_key(&vet) {
panic!("value error");
}
// 在鄰接表中刪除頂點 vet 對應的鏈結串列
self.adj_list.remove(&vet);
// 走訪其他頂點的鏈結串列,刪除所有包含 vet 的邊
@@ -45,7 +45,7 @@ impl GraphAdjMat {
// 在鄰接矩陣中新增一行
self.adj_mat.push(vec![0; n]);
// 在鄰接矩陣中新增一列
for row in &mut self.adj_mat {
for row in self.adj_mat.iter_mut() {
row.push(0);
}
}
@@ -60,7 +60,7 @@ impl GraphAdjMat {
// 在鄰接矩陣中刪除索引 index 的行
self.adj_mat.remove(index);
// 在鄰接矩陣中刪除索引 index 的列
for row in &mut self.adj_mat {
for row in self.adj_mat.iter_mut() {
row.remove(index);
}
}
@@ -22,8 +22,7 @@ fn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {
let mut que = VecDeque::new();
que.push_back(start_vet);
// 以頂點 vet 為起點,迴圈直至訪問完所有頂點
while !que.is_empty() {
let vet = que.pop_front().unwrap(); // 佇列首頂點出隊
while let Some(vet) = que.pop_front() {
res.push(vet); // 記錄訪問頂點
// 走訪該頂點的所有鄰接頂點