mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-05 13:15:30 +08:00
Squash the language code blocks and fix list.md (#865)
This commit is contained in:
@ -28,77 +28,9 @@
|
||||
|
||||
以下是基于邻接矩阵表示图的实现代码。
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="graph_adjacency_matrix.py"
|
||||
[class]{GraphAdjMat}-[func]{}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="graph_adjacency_matrix.cpp"
|
||||
[class]{GraphAdjMat}-[func]{}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="graph_adjacency_matrix.java"
|
||||
[class]{GraphAdjMat}-[func]{}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="graph_adjacency_matrix.cs"
|
||||
[class]{GraphAdjMat}-[func]{}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="graph_adjacency_matrix.go"
|
||||
[class]{graphAdjMat}-[func]{}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="graph_adjacency_matrix.swift"
|
||||
[class]{GraphAdjMat}-[func]{}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="graph_adjacency_matrix.js"
|
||||
[class]{GraphAdjMat}-[func]{}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="graph_adjacency_matrix.ts"
|
||||
[class]{GraphAdjMat}-[func]{}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="graph_adjacency_matrix.dart"
|
||||
[class]{GraphAdjMat}-[func]{}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="graph_adjacency_matrix.rs"
|
||||
[class]{GraphAdjMat}-[func]{}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="graph_adjacency_matrix.c"
|
||||
[class]{graphAdjMat}-[func]{}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="graph_adjacency_matrix.zig"
|
||||
|
||||
```
|
||||
```src
|
||||
[file]{graph_adjacency_matrix}-[class]{graph_adj_mat}-[func]{}
|
||||
```
|
||||
|
||||
## 基于邻接表的实现
|
||||
|
||||
@ -131,77 +63,9 @@
|
||||
2. 如果类似邻接矩阵那样,使用顶点列表索引来区分不同顶点。那么,假设我们想要删除索引为 $i$ 的顶点,则需要遍历整个邻接表,将其中 $> i$ 的索引全部减 $1$ ,这样操作效率较低。
|
||||
3. 因此我们考虑引入顶点类 `Vertex` ,使得每个顶点都是唯一的对象,此时删除顶点时就无须改动其余顶点了。
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="graph_adjacency_list.py"
|
||||
[class]{GraphAdjList}-[func]{}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="graph_adjacency_list.cpp"
|
||||
[class]{GraphAdjList}-[func]{}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="graph_adjacency_list.java"
|
||||
[class]{GraphAdjList}-[func]{}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="graph_adjacency_list.cs"
|
||||
[class]{GraphAdjList}-[func]{}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="graph_adjacency_list.go"
|
||||
[class]{graphAdjList}-[func]{}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="graph_adjacency_list.swift"
|
||||
[class]{GraphAdjList}-[func]{}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="graph_adjacency_list.js"
|
||||
[class]{GraphAdjList}-[func]{}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="graph_adjacency_list.ts"
|
||||
[class]{GraphAdjList}-[func]{}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="graph_adjacency_list.dart"
|
||||
[class]{GraphAdjList}-[func]{}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="graph_adjacency_list.rs"
|
||||
[class]{GraphAdjList}-[func]{}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="graph_adjacency_list.c"
|
||||
[class]{graphAdjList}-[func]{}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="graph_adjacency_list.zig"
|
||||
[class]{GraphAdjList}-[func]{}
|
||||
```
|
||||
```src
|
||||
[file]{graph_adjacency_list}-[class]{graph_adj_list}-[func]{}
|
||||
```
|
||||
|
||||
## 效率对比
|
||||
|
||||
|
@ -20,77 +20,9 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这
|
||||
|
||||
为了防止重复遍历顶点,我们需要借助一个哈希表 `visited` 来记录哪些节点已被访问。
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="graph_bfs.py"
|
||||
[class]{}-[func]{graph_bfs}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="graph_bfs.cpp"
|
||||
[class]{}-[func]{graphBFS}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="graph_bfs.java"
|
||||
[class]{graph_bfs}-[func]{graphBFS}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="graph_bfs.cs"
|
||||
[class]{graph_bfs}-[func]{GraphBFS}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="graph_bfs.go"
|
||||
[class]{}-[func]{graphBFS}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="graph_bfs.swift"
|
||||
[class]{}-[func]{graphBFS}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="graph_bfs.js"
|
||||
[class]{}-[func]{graphBFS}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="graph_bfs.ts"
|
||||
[class]{}-[func]{graphBFS}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="graph_bfs.dart"
|
||||
[class]{}-[func]{graphBFS}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="graph_bfs.rs"
|
||||
[class]{}-[func]{graph_bfs}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="graph_bfs.c"
|
||||
[class]{}-[func]{graphBFS}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="graph_bfs.zig"
|
||||
[class]{}-[func]{graphBFS}
|
||||
```
|
||||
```src
|
||||
[file]{graph_bfs}-[class]{}-[func]{graph_bfs}
|
||||
```
|
||||
|
||||
代码相对抽象,建议对照下图来加深理解。
|
||||
|
||||
@ -147,101 +79,9 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这
|
||||
|
||||
这种“走到尽头再返回”的算法范式通常基于递归来实现。与广度优先遍历类似,在深度优先遍历中我们也需要借助一个哈希表 `visited` 来记录已被访问的顶点,以避免重复访问顶点。
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="graph_dfs.py"
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{graph_dfs}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="graph_dfs.cpp"
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{graphDFS}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="graph_dfs.java"
|
||||
[class]{graph_dfs}-[func]{dfs}
|
||||
|
||||
[class]{graph_dfs}-[func]{graphDFS}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="graph_dfs.cs"
|
||||
[class]{graph_dfs}-[func]{DFS}
|
||||
|
||||
[class]{graph_dfs}-[func]{GraphDFS}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="graph_dfs.go"
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{graphDFS}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="graph_dfs.swift"
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{graphDFS}
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="graph_dfs.js"
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{graphDFS}
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="graph_dfs.ts"
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{graphDFS}
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="graph_dfs.dart"
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{graphDFS}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="graph_dfs.rs"
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{graph_dfs}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="graph_dfs.c"
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{graphDFS}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="graph_dfs.zig"
|
||||
[class]{}-[func]{dfs}
|
||||
|
||||
[class]{}-[func]{graphDFS}
|
||||
```
|
||||
```src
|
||||
[file]{graph_dfs}-[class]{}-[func]{graph_dfs}
|
||||
```
|
||||
|
||||
深度优先遍历的算法流程如下图所示。
|
||||
|
||||
|
Reference in New Issue
Block a user