Reformat the C# codes.

Disable creating new line before open brace.
This commit is contained in:
krahets
2023-04-23 03:03:12 +08:00
parent ac6eece4f3
commit 73dcb4cea9
49 changed files with 561 additions and 1135 deletions

View File

@@ -10,18 +10,15 @@ using NUnit.Framework;
namespace hello_algo.chapter_graph;
/* 基于邻接表实现的无向图类 */
public class GraphAdjList
{
public 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]);
@@ -29,14 +26,12 @@ public class GraphAdjList
}
/* 获取顶点数量 */
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
@@ -45,8 +40,7 @@ public class GraphAdjList
}
/* 删除边 */
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
@@ -55,8 +49,7 @@ public class GraphAdjList
}
/* 添加顶点 */
public void addVertex(Vertex vet)
{
public void addVertex(Vertex vet) {
if (adjList.ContainsKey(vet))
return;
// 在邻接表中添加一个新链表
@@ -64,25 +57,21 @@ public class GraphAdjList
}
/* 删除顶点 */
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);
@@ -91,11 +80,9 @@ public class GraphAdjList
}
}
public class graph_adjacency_list
{
public class graph_adjacency_list {
[Test]
public void Test()
{
public void Test() {
/* 初始化无向图 */
Vertex[] v = Vertex.ValsToVets(new int[] { 1, 3, 2, 5, 4 });
Vertex[][] edges = new Vertex[][] { new Vertex[] { v[0], v[1] }, new Vertex[] { v[0], v[3] },

View File

@@ -10,58 +10,49 @@ using NUnit.Framework;
namespace hello_algo.chapter_graph;
/* 基于邻接矩阵实现的无向图类 */
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 的顶点
@@ -69,16 +60,14 @@ class GraphAdjMat
// 在邻接矩阵中删除索引 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();
@@ -89,18 +78,16 @@ class GraphAdjMat
/* 删除边 */
// 参数 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("邻接矩阵 =");
@@ -108,11 +95,9 @@ class GraphAdjMat
}
}
public class graph_adjacency_matrix
{
public class graph_adjacency_matrix {
[Test]
public void Test()
{
public void Test() {
/* 初始化无向图 */
// 请注意edges 元素代表顶点索引,即对应 vertices 元素索引
int[] vertices = { 1, 3, 2, 5, 4 };

View File

@@ -9,12 +9,10 @@ using NUnit.Framework;
namespace hello_algo.chapter_graph;
public class graph_bfs
{
public class graph_bfs {
/* 广度优先遍历 BFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
public static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet)
{
public static List<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列
List<Vertex> res = new List<Vertex>();
// 哈希表,用于记录已被访问过的顶点
@@ -23,14 +21,11 @@ public class graph_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); // 只入队未访问的顶点
@@ -43,8 +38,7 @@ public class graph_bfs
}
[Test]
public void Test()
{
public void Test() {
/* 初始化无向图 */
Vertex[] v = Vertex.ValsToVets(new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
Vertex[][] edges = new Vertex[12][]
@@ -64,4 +58,4 @@ public class graph_bfs
Console.WriteLine("\n广度优先遍历BFS顶点序列为");
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
}
}
}

View File

@@ -9,18 +9,14 @@ using NUnit.Framework;
namespace hello_algo.chapter_graph;
public class graph_dfs
{
public class graph_dfs {
/* 深度优先遍历 DFS 辅助函数 */
public void dfs(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet)
{
public 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; // 跳过已被访问过的顶点
}
// 递归访问邻接顶点
@@ -30,8 +26,7 @@ public class graph_dfs
/* 深度优先遍历 DFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
public List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet)
{
public List<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {
// 顶点遍历序列
List<Vertex> res = new List<Vertex>();
// 哈希表,用于记录已被访问过的顶点
@@ -41,8 +36,7 @@ public class graph_dfs
}
[Test]
public void Test()
{
public void Test() {
/* 初始化无向图 */
Vertex[] v = Vertex.ValsToVets(new int[7] { 0, 1, 2, 3, 4, 5, 6 });
Vertex[][] edges = new Vertex[6][]
@@ -60,4 +54,4 @@ public class graph_dfs
Console.WriteLine("\n深度优先遍历DFS顶点序列为");
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
}
}
}