mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-26 04:56:06 +00:00
build
This commit is contained in:
@@ -294,8 +294,8 @@ comments: true
|
||||
```csharp title="graph_adjacency_matrix.cs"
|
||||
/* 基于邻接矩阵实现的无向图类 */
|
||||
class GraphAdjMat {
|
||||
List<int> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
List<List<int>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”
|
||||
readonly List<int> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
readonly List<List<int>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”
|
||||
|
||||
/* 构造函数 */
|
||||
public GraphAdjMat(int[] vertices, int[][] edges) {
|
||||
@@ -303,27 +303,27 @@ comments: true
|
||||
this.adjMat = new List<List<int>>();
|
||||
// 添加顶点
|
||||
foreach (int val in vertices) {
|
||||
addVertex(val);
|
||||
AddVertex(val);
|
||||
}
|
||||
// 添加边
|
||||
// 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引
|
||||
foreach (int[] e in edges) {
|
||||
addEdge(e[0], e[1]);
|
||||
AddEdge(e[0], e[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取顶点数量 */
|
||||
public int size() {
|
||||
public int Size() {
|
||||
return vertices.Count;
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
public void addVertex(int val) {
|
||||
int n = size();
|
||||
public void AddVertex(int val) {
|
||||
int n = Size();
|
||||
// 向顶点列表中添加新顶点的值
|
||||
vertices.Add(val);
|
||||
// 在邻接矩阵中添加一行
|
||||
List<int> newRow = new List<int>(n);
|
||||
List<int> newRow = new(n);
|
||||
for (int j = 0; j < n; j++) {
|
||||
newRow.Add(0);
|
||||
}
|
||||
@@ -335,8 +335,8 @@ comments: true
|
||||
}
|
||||
|
||||
/* 删除顶点 */
|
||||
public void removeVertex(int index) {
|
||||
if (index >= size())
|
||||
public void RemoveVertex(int index) {
|
||||
if (index >= Size())
|
||||
throw new IndexOutOfRangeException();
|
||||
// 在顶点列表中移除索引 index 的顶点
|
||||
vertices.RemoveAt(index);
|
||||
@@ -350,9 +350,9 @@ comments: true
|
||||
|
||||
/* 添加边 */
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
public void addEdge(int i, int j) {
|
||||
public void AddEdge(int i, int j) {
|
||||
// 索引越界与相等处理
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
|
||||
if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)
|
||||
throw new IndexOutOfRangeException();
|
||||
// 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i)
|
||||
adjMat[i][j] = 1;
|
||||
@@ -361,16 +361,16 @@ comments: true
|
||||
|
||||
/* 删除边 */
|
||||
// 参数 i, j 对应 vertices 元素索引
|
||||
public void removeEdge(int i, int j) {
|
||||
public void RemoveEdge(int i, int j) {
|
||||
// 索引越界与相等处理
|
||||
if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)
|
||||
if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)
|
||||
throw new IndexOutOfRangeException();
|
||||
adjMat[i][j] = 0;
|
||||
adjMat[j][i] = 0;
|
||||
}
|
||||
|
||||
/* 打印邻接矩阵 */
|
||||
public void print() {
|
||||
public void Print() {
|
||||
Console.Write("顶点列表 = ");
|
||||
PrintUtil.PrintList(vertices);
|
||||
Console.WriteLine("邻接矩阵 =");
|
||||
@@ -1390,19 +1390,19 @@ comments: true
|
||||
this.adjList = new Dictionary<Vertex, List<Vertex>>();
|
||||
// 添加所有顶点和边
|
||||
foreach (Vertex[] edge in edges) {
|
||||
addVertex(edge[0]);
|
||||
addVertex(edge[1]);
|
||||
addEdge(edge[0], edge[1]);
|
||||
AddVertex(edge[0]);
|
||||
AddVertex(edge[1]);
|
||||
AddEdge(edge[0], edge[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取顶点数量 */
|
||||
public int size() {
|
||||
public int Size() {
|
||||
return adjList.Count;
|
||||
}
|
||||
|
||||
/* 添加边 */
|
||||
public void addEdge(Vertex vet1, Vertex vet2) {
|
||||
public void AddEdge(Vertex vet1, Vertex vet2) {
|
||||
if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)
|
||||
throw new InvalidOperationException();
|
||||
// 添加边 vet1 - vet2
|
||||
@@ -1411,7 +1411,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 删除边 */
|
||||
public void removeEdge(Vertex vet1, Vertex vet2) {
|
||||
public void RemoveEdge(Vertex vet1, Vertex vet2) {
|
||||
if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)
|
||||
throw new InvalidOperationException();
|
||||
// 删除边 vet1 - vet2
|
||||
@@ -1420,7 +1420,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
public void addVertex(Vertex vet) {
|
||||
public void AddVertex(Vertex vet) {
|
||||
if (adjList.ContainsKey(vet))
|
||||
return;
|
||||
// 在邻接表中添加一个新链表
|
||||
@@ -1428,7 +1428,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 删除顶点 */
|
||||
public void removeVertex(Vertex vet) {
|
||||
public void RemoveVertex(Vertex vet) {
|
||||
if (!adjList.ContainsKey(vet))
|
||||
throw new InvalidOperationException();
|
||||
// 在邻接表中删除顶点 vet 对应的链表
|
||||
@@ -1440,10 +1440,10 @@ comments: true
|
||||
}
|
||||
|
||||
/* 打印邻接表 */
|
||||
public void print() {
|
||||
public void Print() {
|
||||
Console.WriteLine("邻接表 =");
|
||||
foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {
|
||||
List<int> tmp = new List<int>();
|
||||
List<int> tmp = new();
|
||||
foreach (Vertex vertex in pair.Value)
|
||||
tmp.Add(vertex.val);
|
||||
Console.WriteLine(pair.Key.val + ": [" + string.Join(", ", tmp) + "],");
|
||||
|
||||
@@ -119,13 +119,13 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这
|
||||
```csharp title="graph_bfs.cs"
|
||||
/* 广度优先遍历 BFS */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
|
||||
List<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {
|
||||
// 顶点遍历序列
|
||||
List<Vertex> res = new List<Vertex>();
|
||||
List<Vertex> res = new();
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
HashSet<Vertex> visited = new HashSet<Vertex>() { startVet };
|
||||
HashSet<Vertex> visited = new() { startVet };
|
||||
// 队列用于实现 BFS
|
||||
Queue<Vertex> que = new Queue<Vertex>();
|
||||
Queue<Vertex> que = new();
|
||||
que.Enqueue(startVet);
|
||||
// 以顶点 vet 为起点,循环直至访问完所有顶点
|
||||
while (que.Count > 0) {
|
||||
@@ -532,7 +532,7 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这
|
||||
|
||||
```csharp title="graph_dfs.cs"
|
||||
/* 深度优先遍历 DFS 辅助函数 */
|
||||
void dfs(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {
|
||||
void Dfs(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {
|
||||
res.Add(vet); // 记录访问顶点
|
||||
visited.Add(vet); // 标记该顶点已被访问
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
@@ -541,18 +541,18 @@ BFS 通常借助队列来实现。队列具有“先入先出”的性质,这
|
||||
continue; // 跳过已被访问过的顶点
|
||||
}
|
||||
// 递归访问邻接顶点
|
||||
dfs(graph, visited, res, adjVet);
|
||||
Dfs(graph, visited, res, adjVet);
|
||||
}
|
||||
}
|
||||
|
||||
/* 深度优先遍历 DFS */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
|
||||
List<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {
|
||||
// 顶点遍历序列
|
||||
List<Vertex> res = new List<Vertex>();
|
||||
List<Vertex> res = new();
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
HashSet<Vertex> visited = new HashSet<Vertex>();
|
||||
dfs(graph, visited, res, startVet);
|
||||
HashSet<Vertex> visited = new();
|
||||
Dfs(graph, visited, res, startVet);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user