2023-02-15 21:24:24 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: graph_adjacency_list.cs
|
|
|
|
|
* Created Time: 2023-02-06
|
|
|
|
|
* Author: zjkung1123 (zjkung1123@gmail.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace hello_algo.chapter_graph;
|
|
|
|
|
|
|
|
|
|
/* 基于邻接表实现的无向图类 */
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public class GraphAdjList {
|
2023-02-23 20:32:26 +08:00
|
|
|
|
// 邻接表,key: 顶点,value:该顶点的所有邻接顶点
|
2023-03-08 19:16:46 +08:00
|
|
|
|
public Dictionary<Vertex, List<Vertex>> adjList;
|
2023-02-15 21:24:24 +08:00
|
|
|
|
|
|
|
|
|
/* 构造函数 */
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public GraphAdjList(Vertex[][] edges) {
|
2023-02-15 21:24:24 +08:00
|
|
|
|
this.adjList = new Dictionary<Vertex, List<Vertex>>();
|
|
|
|
|
// 添加所有顶点和边
|
2023-04-23 03:03:12 +08:00
|
|
|
|
foreach (Vertex[] edge in edges) {
|
2023-10-08 01:33:46 +08:00
|
|
|
|
AddVertex(edge[0]);
|
|
|
|
|
AddVertex(edge[1]);
|
|
|
|
|
AddEdge(edge[0], edge[1]);
|
2023-02-15 21:24:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取顶点数量 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public int Size() {
|
2023-02-15 21:24:24 +08:00
|
|
|
|
return adjList.Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 添加边 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public void AddEdge(Vertex vet1, Vertex vet2) {
|
2023-02-15 21:24:24 +08:00
|
|
|
|
if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
// 添加边 vet1 - vet2
|
|
|
|
|
adjList[vet1].Add(vet2);
|
|
|
|
|
adjList[vet2].Add(vet1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除边 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public void RemoveEdge(Vertex vet1, Vertex vet2) {
|
2023-02-15 21:24:24 +08:00
|
|
|
|
if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
// 删除边 vet1 - vet2
|
|
|
|
|
adjList[vet1].Remove(vet2);
|
|
|
|
|
adjList[vet2].Remove(vet1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 添加顶点 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public void AddVertex(Vertex vet) {
|
2023-02-15 21:24:24 +08:00
|
|
|
|
if (adjList.ContainsKey(vet))
|
|
|
|
|
return;
|
|
|
|
|
// 在邻接表中添加一个新链表
|
|
|
|
|
adjList.Add(vet, new List<Vertex>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除顶点 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public void RemoveVertex(Vertex vet) {
|
2023-02-15 21:24:24 +08:00
|
|
|
|
if (!adjList.ContainsKey(vet))
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
// 在邻接表中删除顶点 vet 对应的链表
|
|
|
|
|
adjList.Remove(vet);
|
2023-04-09 19:12:37 +08:00
|
|
|
|
// 遍历其他顶点的链表,删除所有包含 vet 的边
|
2023-04-23 03:03:12 +08:00
|
|
|
|
foreach (List<Vertex> list in adjList.Values) {
|
2023-02-15 21:24:24 +08:00
|
|
|
|
list.Remove(vet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 打印邻接表 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public void Print() {
|
2023-02-15 21:24:24 +08:00
|
|
|
|
Console.WriteLine("邻接表 =");
|
2023-06-14 02:01:06 +08:00
|
|
|
|
foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {
|
2023-10-08 01:33:46 +08:00
|
|
|
|
List<int> tmp = new();
|
2023-06-14 02:01:06 +08:00
|
|
|
|
foreach (Vertex vertex in pair.Value)
|
2023-04-21 14:59:22 +08:00
|
|
|
|
tmp.Add(vertex.val);
|
2023-06-14 02:01:06 +08:00
|
|
|
|
Console.WriteLine(pair.Key.val + ": [" + string.Join(", ", tmp) + "],");
|
2023-02-15 21:24:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public class graph_adjacency_list {
|
2023-02-15 21:24:24 +08:00
|
|
|
|
[Test]
|
2023-04-23 03:03:12 +08:00
|
|
|
|
public void Test() {
|
2023-02-15 21:24:24 +08:00
|
|
|
|
/* 初始化无向图 */
|
2023-04-21 14:59:22 +08:00
|
|
|
|
Vertex[] v = Vertex.ValsToVets(new int[] { 1, 3, 2, 5, 4 });
|
2023-02-15 21:24:24 +08:00
|
|
|
|
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] } };
|
2023-10-08 01:33:46 +08:00
|
|
|
|
GraphAdjList graph = new(edges);
|
2023-02-15 21:24:24 +08:00
|
|
|
|
Console.WriteLine("\n初始化后,图为");
|
2023-10-08 01:33:46 +08:00
|
|
|
|
graph.Print();
|
2023-02-15 21:24:24 +08:00
|
|
|
|
|
|
|
|
|
/* 添加边 */
|
|
|
|
|
// 顶点 1, 2 即 v[0], v[2]
|
2023-10-08 01:33:46 +08:00
|
|
|
|
graph.AddEdge(v[0], v[2]);
|
2023-02-15 21:24:24 +08:00
|
|
|
|
Console.WriteLine("\n添加边 1-2 后,图为");
|
2023-10-08 01:33:46 +08:00
|
|
|
|
graph.Print();
|
2023-02-15 21:24:24 +08:00
|
|
|
|
|
|
|
|
|
/* 删除边 */
|
|
|
|
|
// 顶点 1, 3 即 v[0], v[1]
|
2023-10-08 01:33:46 +08:00
|
|
|
|
graph.RemoveEdge(v[0], v[1]);
|
2023-02-15 21:24:24 +08:00
|
|
|
|
Console.WriteLine("\n删除边 1-3 后,图为");
|
2023-10-08 01:33:46 +08:00
|
|
|
|
graph.Print();
|
2023-02-15 21:24:24 +08:00
|
|
|
|
|
|
|
|
|
/* 添加顶点 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
Vertex v5 = new(6);
|
|
|
|
|
graph.AddVertex(v5);
|
2023-02-15 21:24:24 +08:00
|
|
|
|
Console.WriteLine("\n添加顶点 6 后,图为");
|
2023-10-08 01:33:46 +08:00
|
|
|
|
graph.Print();
|
2023-02-15 21:24:24 +08:00
|
|
|
|
|
|
|
|
|
/* 删除顶点 */
|
|
|
|
|
// 顶点 3 即 v[1]
|
2023-10-08 01:33:46 +08:00
|
|
|
|
graph.RemoveVertex(v[1]);
|
2023-02-15 21:24:24 +08:00
|
|
|
|
Console.WriteLine("\n删除顶点 3 后,图为");
|
2023-10-08 01:33:46 +08:00
|
|
|
|
graph.Print();
|
2023-02-15 21:24:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|