Add the section of Graph Traversal.

This commit is contained in:
krahets
2023-02-15 03:22:57 +08:00
parent c74f8293b9
commit dc441928d9
31 changed files with 383 additions and 54 deletions

View File

@ -89,7 +89,7 @@ comments: true
=== "Zig"
```zig title="graph_adjacency_matrix.zig"
```
## 9.2.2. 基于邻接表的实现
@ -119,11 +119,17 @@ comments: true
基于邻接表实现图的代码如下所示。
!!! question "为什么需要使用顶点类 `Vertex` "
如果我们直接通过顶点值来区分不同顶点,那么值重复的顶点将无法被区分。
如果建立一个顶点列表,用索引来区分不同顶点,那么假设我们想要删除索引为 `i` 的顶点,则需要遍历整个邻接表,将其中 $> i$ 的索引全部执行 $-1$ ,这样的操作是比较耗时的。
因此,通过引入顶点类 `Vertex` ,每个顶点都是唯一的对象,这样在删除操作时就无需改动其余顶点了。
=== "Java"
```java title="graph_adjacency_list.java"
[class]{Vertex}-[func]{}
[class]{GraphAdjList}-[func]{}
```
@ -131,7 +137,7 @@ comments: true
```cpp title="graph_adjacency_list.cpp"
[class]{Vertex}-[func]{}
[class]{GraphAdjList}-[func]{}
```
@ -139,7 +145,7 @@ comments: true
```python title="graph_adjacency_list.py"
[class]{Vertex}-[func]{}
[class]{GraphAdjList}-[func]{}
```
@ -147,7 +153,7 @@ comments: true
```go title="graph_adjacency_list.go"
[class]{vertex}-[func]{}
[class]{graphAdjList}-[func]{}
```
@ -155,7 +161,7 @@ comments: true
```javascript title="graph_adjacency_list.js"
[class]{Vertex}-[func]{}
[class]{GraphAdjList}-[func]{}
```
@ -163,7 +169,7 @@ comments: true
```typescript title="graph_adjacency_list.ts"
[class]{Vertex}-[func]{}
[class]{GraphAdjList}-[func]{}
```
@ -171,7 +177,7 @@ comments: true
```c title="graph_adjacency_list.c"
[class]{vertex}-[func]{}
[class]{graphAdjList}-[func]{}
```
@ -179,7 +185,7 @@ comments: true
```csharp title="graph_adjacency_list.cs"
[class]{Vertex}-[func]{}
[class]{GraphAdjList}-[func]{}
```
@ -187,7 +193,7 @@ comments: true
```swift title="graph_adjacency_list.swift"
[class]{Vertex}-[func]{}
[class]{GraphAdjList}-[func]{}
```
@ -195,7 +201,7 @@ comments: true
```zig title="graph_adjacency_list.zig"
[class]{Vertex}-[func]{}
[class]{GraphAdjList}-[func]{}
```