This commit is contained in:
krahets
2023-04-23 14:58:03 +08:00
parent 881ece517f
commit fe8027e64a
26 changed files with 344 additions and 626 deletions
+28 -52
View File
@@ -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);