mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-15 08:26:06 +00:00
build
This commit is contained in:
@@ -580,58 +580,49 @@ comments: true
|
||||
|
||||
```csharp title="graph_adjacency_matrix.cs"
|
||||
/* 基于邻接矩阵实现的无向图类 */
|
||||
class GraphAdjMat
|
||||
{
|
||||
class GraphAdjMat {
|
||||
List<int> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
||||
List<List<int>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”
|
||||
|
||||
/* 构造函数 */
|
||||
public GraphAdjMat(int[] vertices, int[][] edges)
|
||||
{
|
||||
public GraphAdjMat(int[] vertices, int[][] edges) {
|
||||
this.vertices = new List<int>();
|
||||
this.adjMat = new List<List<int>>();
|
||||
// 添加顶点
|
||||
foreach (int val in vertices)
|
||||
{
|
||||
foreach (int val in vertices) {
|
||||
addVertex(val);
|
||||
}
|
||||
// 添加边
|
||||
// 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引
|
||||
foreach (int[] e in edges)
|
||||
{
|
||||
foreach (int[] e in edges) {
|
||||
addEdge(e[0], e[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取顶点数量 */
|
||||
public int size()
|
||||
{
|
||||
public int size() {
|
||||
return vertices.Count;
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
public void addVertex(int val)
|
||||
{
|
||||
public void addVertex(int val) {
|
||||
int n = size();
|
||||
// 向顶点列表中添加新顶点的值
|
||||
vertices.Add(val);
|
||||
// 在邻接矩阵中添加一行
|
||||
List<int> newRow = new List<int>(n);
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
for (int j = 0; j < n; j++) {
|
||||
newRow.Add(0);
|
||||
}
|
||||
adjMat.Add(newRow);
|
||||
// 在邻接矩阵中添加一列
|
||||
foreach (List<int> row in adjMat)
|
||||
{
|
||||
foreach (List<int> row in adjMat) {
|
||||
row.Add(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* 删除顶点 */
|
||||
public void removeVertex(int index)
|
||||
{
|
||||
public void removeVertex(int index) {
|
||||
if (index >= size())
|
||||
throw new IndexOutOfRangeException();
|
||||
// 在顶点列表中移除索引 index 的顶点
|
||||
@@ -639,16 +630,14 @@ comments: true
|
||||
// 在邻接矩阵中删除索引 index 的行
|
||||
adjMat.RemoveAt(index);
|
||||
// 在邻接矩阵中删除索引 index 的列
|
||||
foreach (List<int> row in adjMat)
|
||||
{
|
||||
foreach (List<int> row in adjMat) {
|
||||
row.RemoveAt(index);
|
||||
}
|
||||
}
|
||||
|
||||
/* 添加边 */
|
||||
// 参数 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)
|
||||
throw new IndexOutOfRangeException();
|
||||
@@ -659,18 +648,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)
|
||||
throw new IndexOutOfRangeException();
|
||||
adjMat[i][j] = 0;
|
||||
adjMat[j][i] = 0;
|
||||
}
|
||||
|
||||
|
||||
/* 打印邻接矩阵 */
|
||||
public void print()
|
||||
{
|
||||
public void print() {
|
||||
Console.Write("顶点列表 = ");
|
||||
PrintUtil.PrintList(vertices);
|
||||
Console.WriteLine("邻接矩阵 =");
|
||||
@@ -989,7 +976,7 @@ comments: true
|
||||
def add_edge(self, vet1: Vertex, vet2: Vertex) -> None:
|
||||
"""添加边"""
|
||||
if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:
|
||||
raise ValueError
|
||||
raise ValueError()
|
||||
# 添加边 vet1 - vet2
|
||||
self.adj_list[vet1].append(vet2)
|
||||
self.adj_list[vet2].append(vet1)
|
||||
@@ -997,7 +984,7 @@ comments: true
|
||||
def remove_edge(self, vet1: Vertex, vet2: Vertex) -> None:
|
||||
"""删除边"""
|
||||
if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:
|
||||
raise ValueError
|
||||
raise ValueError()
|
||||
# 删除边 vet1 - vet2
|
||||
self.adj_list[vet1].remove(vet2)
|
||||
self.adj_list[vet2].remove(vet1)
|
||||
@@ -1012,7 +999,7 @@ comments: true
|
||||
def remove_vertex(self, vet: Vertex) -> None:
|
||||
"""删除顶点"""
|
||||
if vet not in self.adj_list:
|
||||
raise ValueError
|
||||
raise ValueError()
|
||||
# 在邻接表中删除顶点 vet 对应的链表
|
||||
self.adj_list.pop(vet)
|
||||
# 遍历其他顶点的链表,删除所有包含 vet 的边
|
||||
@@ -1307,18 +1294,15 @@ comments: true
|
||||
|
||||
```csharp title="graph_adjacency_list.cs"
|
||||
/* 基于邻接表实现的无向图类 */
|
||||
class GraphAdjList
|
||||
{
|
||||
class GraphAdjList {
|
||||
// 邻接表,key: 顶点,value:该顶点的所有邻接顶点
|
||||
public Dictionary<Vertex, List<Vertex>> adjList;
|
||||
|
||||
/* 构造函数 */
|
||||
public GraphAdjList(Vertex[][] edges)
|
||||
{
|
||||
public GraphAdjList(Vertex[][] edges) {
|
||||
this.adjList = new Dictionary<Vertex, List<Vertex>>();
|
||||
// 添加所有顶点和边
|
||||
foreach (Vertex[] edge in edges)
|
||||
{
|
||||
foreach (Vertex[] edge in edges) {
|
||||
addVertex(edge[0]);
|
||||
addVertex(edge[1]);
|
||||
addEdge(edge[0], edge[1]);
|
||||
@@ -1326,14 +1310,12 @@ comments: true
|
||||
}
|
||||
|
||||
/* 获取顶点数量 */
|
||||
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
|
||||
@@ -1342,8 +1324,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
|
||||
@@ -1352,8 +1333,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
public void addVertex(Vertex vet)
|
||||
{
|
||||
public void addVertex(Vertex vet) {
|
||||
if (adjList.ContainsKey(vet))
|
||||
return;
|
||||
// 在邻接表中添加一个新链表
|
||||
@@ -1361,25 +1341,21 @@ comments: true
|
||||
}
|
||||
|
||||
/* 删除顶点 */
|
||||
public void removeVertex(Vertex vet)
|
||||
{
|
||||
public void removeVertex(Vertex vet) {
|
||||
if (!adjList.ContainsKey(vet))
|
||||
throw new InvalidOperationException();
|
||||
// 在邻接表中删除顶点 vet 对应的链表
|
||||
adjList.Remove(vet);
|
||||
// 遍历其他顶点的链表,删除所有包含 vet 的边
|
||||
foreach (List<Vertex> list in adjList.Values)
|
||||
{
|
||||
foreach (List<Vertex> list in adjList.Values) {
|
||||
list.Remove(vet);
|
||||
}
|
||||
}
|
||||
|
||||
/* 打印邻接表 */
|
||||
public void print()
|
||||
{
|
||||
public void print() {
|
||||
Console.WriteLine("邻接表 =");
|
||||
foreach (KeyValuePair<Vertex, List<Vertex>> entry in adjList)
|
||||
{
|
||||
foreach (KeyValuePair<Vertex, List<Vertex>> entry in adjList) {
|
||||
List<int> tmp = new List<int>();
|
||||
foreach (Vertex vertex in entry.Value)
|
||||
tmp.Add(vertex.val);
|
||||
|
||||
@@ -225,8 +225,7 @@ 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>();
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
@@ -235,14 +234,11 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
|
||||
Queue<Vertex> que = new Queue<Vertex>();
|
||||
que.Enqueue(startVet);
|
||||
// 以顶点 vet 为起点,循环直至访问完所有顶点
|
||||
while (que.Count > 0)
|
||||
{
|
||||
while (que.Count > 0) {
|
||||
Vertex vet = que.Dequeue(); // 队首顶点出队
|
||||
res.Add(vet); // 记录访问顶点
|
||||
foreach (Vertex adjVet in graph.adjList[vet])
|
||||
{
|
||||
if (visited.Contains(adjVet))
|
||||
{
|
||||
foreach (Vertex adjVet in graph.adjList[vet]) {
|
||||
if (visited.Contains(adjVet)) {
|
||||
continue; // 跳过已被访问过的顶点
|
||||
}
|
||||
que.Enqueue(adjVet); // 只入队未访问的顶点
|
||||
@@ -535,15 +531,12 @@ 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); // 标记该顶点已被访问
|
||||
// 遍历该顶点的所有邻接顶点
|
||||
foreach (Vertex adjVet in graph.adjList[vet])
|
||||
{
|
||||
if (visited.Contains(adjVet))
|
||||
{
|
||||
foreach (Vertex adjVet in graph.adjList[vet]) {
|
||||
if (visited.Contains(adjVet)) {
|
||||
continue; // 跳过已被访问过的顶点
|
||||
}
|
||||
// 递归访问邻接顶点
|
||||
@@ -553,8 +546,7 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质
|
||||
|
||||
/* 深度优先遍历 DFS */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet)
|
||||
{
|
||||
List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
|
||||
// 顶点遍历序列
|
||||
List<Vertex> res = new List<Vertex>();
|
||||
// 哈希表,用于记录已被访问过的顶点
|
||||
|
||||
Reference in New Issue
Block a user