mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
refactor: add/refactor method in include, simplified print code (#471)
This commit is contained in:
@@ -85,8 +85,8 @@ public class GraphAdjList
|
||||
{
|
||||
List<int> tmp = new List<int>();
|
||||
foreach (Vertex vertex in entry.Value)
|
||||
tmp.Add(vertex.Val);
|
||||
Console.WriteLine(entry.Key.Val + ": [" + string.Join(", ", tmp) + "],");
|
||||
tmp.Add(vertex.val);
|
||||
Console.WriteLine(entry.Key.val + ": [" + string.Join(", ", tmp) + "],");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,7 +97,7 @@ public class graph_adjacency_list
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化无向图 */
|
||||
Vertex[] v = Vertex.valsToVets(new int[] { 1, 3, 2, 5, 4 });
|
||||
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] },
|
||||
new Vertex[] { v[1], v[2] }, new Vertex[] { v[2], v[3] },
|
||||
new Vertex[] { v[2], v[4] }, new Vertex[] { v[3], v[4] } };
|
||||
|
||||
@@ -104,7 +104,7 @@ class GraphAdjMat
|
||||
Console.Write("顶点列表 = ");
|
||||
PrintUtil.PrintList(vertices);
|
||||
Console.WriteLine("邻接矩阵 =");
|
||||
PrintUtil.printMatrix(adjMat);
|
||||
PrintUtil.PrintMatrix(adjMat);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ public class graph_bfs
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化无向图 */
|
||||
Vertex[] v = Vertex.valsToVets(new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
||||
Vertex[] v = Vertex.ValsToVets(new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
|
||||
Vertex[][] edges = new Vertex[12][]
|
||||
{
|
||||
new Vertex[2] { v[0], v[1] }, new Vertex[2] { v[0], v[3] }, new Vertex[2] { v[1], v[2] },
|
||||
@@ -62,6 +62,6 @@ public class graph_bfs
|
||||
/* 广度优先遍历 BFS */
|
||||
List<Vertex> res = graphBFS(graph, v[0]);
|
||||
Console.WriteLine("\n广度优先遍历(BFS)顶点序列为");
|
||||
Console.WriteLine(string.Join(" ", Vertex.vetsToVals(res)));
|
||||
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ public class graph_dfs
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化无向图 */
|
||||
Vertex[] v = Vertex.valsToVets(new int[7] { 0, 1, 2, 3, 4, 5, 6 });
|
||||
Vertex[] v = Vertex.ValsToVets(new int[7] { 0, 1, 2, 3, 4, 5, 6 });
|
||||
Vertex[][] edges = new Vertex[6][]
|
||||
{
|
||||
new Vertex[2] { v[0], v[1] }, new Vertex[2] { v[0], v[3] }, new Vertex[2] { v[1], v[2] },
|
||||
@@ -58,6 +58,6 @@ public class graph_dfs
|
||||
/* 深度优先遍历 DFS */
|
||||
List<Vertex> res = graphDFS(graph, v[0]);
|
||||
Console.WriteLine("\n深度优先遍历(DFS)顶点序列为");
|
||||
Console.WriteLine(string.Join(" ", Vertex.vetsToVals(res)));
|
||||
Console.WriteLine(string.Join(" ", Vertex.VetsToVals(res)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user