2023-02-23 20:00:40 +08:00
|
|
|
|
"""
|
|
|
|
|
File: graph_adjacency_matrix.py
|
|
|
|
|
Created Time: 2023-02-23
|
|
|
|
|
Author: Krahets (krahets@163.com)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import sys, os.path as osp
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
2023-02-23 20:00:40 +08:00
|
|
|
|
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
2023-03-03 03:07:22 +08:00
|
|
|
|
from modules import *
|
2023-02-23 20:00:40 +08:00
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
2023-02-23 20:00:40 +08:00
|
|
|
|
class GraphAdjMat:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""基于邻接矩阵实现的无向图类"""
|
|
|
|
|
|
2023-02-23 20:00:40 +08:00
|
|
|
|
# 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
|
2023-03-23 18:51:56 +08:00
|
|
|
|
vertices: list[int] = []
|
2023-02-23 20:00:40 +08:00
|
|
|
|
# 邻接矩阵,行列索引对应“顶点索引”
|
2023-03-23 18:51:56 +08:00
|
|
|
|
adj_mat: list[list[int]] = []
|
2023-02-23 20:00:40 +08:00
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def __init__(self, vertices: list[int], edges: list[list[int]]) -> None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""构造方法"""
|
2023-03-23 18:51:56 +08:00
|
|
|
|
self.vertices: list[int] = []
|
|
|
|
|
self.adj_mat: list[list[int]] = []
|
2023-02-23 20:00:40 +08:00
|
|
|
|
# 添加顶点
|
|
|
|
|
for val in vertices:
|
|
|
|
|
self.add_vertex(val)
|
|
|
|
|
# 添加边
|
|
|
|
|
# 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引
|
|
|
|
|
for e in edges:
|
|
|
|
|
self.add_edge(e[0], e[1])
|
|
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
|
def size(self) -> int:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""获取顶点数量"""
|
2023-02-23 20:00:40 +08:00
|
|
|
|
return len(self.vertices)
|
|
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
|
def add_vertex(self, val: int) -> None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""添加顶点"""
|
2023-02-23 20:00:40 +08:00
|
|
|
|
n = self.size()
|
|
|
|
|
# 向顶点列表中添加新顶点的值
|
|
|
|
|
self.vertices.append(val)
|
|
|
|
|
# 在邻接矩阵中添加一行
|
2023-03-12 18:49:52 +08:00
|
|
|
|
new_row = [0] * n
|
2023-02-23 20:00:40 +08:00
|
|
|
|
self.adj_mat.append(new_row)
|
|
|
|
|
# 在邻接矩阵中添加一列
|
|
|
|
|
for row in self.adj_mat:
|
|
|
|
|
row.append(0)
|
|
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
|
def remove_vertex(self, index: int) -> None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""删除顶点"""
|
2023-02-23 20:00:40 +08:00
|
|
|
|
if index >= self.size():
|
|
|
|
|
raise IndexError()
|
|
|
|
|
# 在顶点列表中移除索引 index 的顶点
|
|
|
|
|
self.vertices.pop(index)
|
|
|
|
|
# 在邻接矩阵中删除索引 index 的行
|
|
|
|
|
self.adj_mat.pop(index)
|
|
|
|
|
# 在邻接矩阵中删除索引 index 的列
|
|
|
|
|
for row in self.adj_mat:
|
|
|
|
|
row.pop(index)
|
|
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
|
def add_edge(self, i: int, j: int) -> None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""添加边"""
|
2023-03-03 03:07:22 +08:00
|
|
|
|
# 参数 i, j 对应 vertices 元素索引
|
2023-02-23 20:00:40 +08:00
|
|
|
|
# 索引越界与相等处理
|
|
|
|
|
if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:
|
|
|
|
|
raise IndexError()
|
|
|
|
|
# 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i)
|
|
|
|
|
self.adj_mat[i][j] = 1
|
|
|
|
|
self.adj_mat[j][i] = 1
|
|
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
|
def remove_edge(self, i: int, j: int) -> None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""删除边"""
|
2023-03-03 03:07:22 +08:00
|
|
|
|
# 参数 i, j 对应 vertices 元素索引
|
2023-02-23 20:00:40 +08:00
|
|
|
|
# 索引越界与相等处理
|
|
|
|
|
if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:
|
|
|
|
|
raise IndexError()
|
|
|
|
|
self.adj_mat[i][j] = 0
|
|
|
|
|
self.adj_mat[j][i] = 0
|
|
|
|
|
|
2023-03-12 18:49:52 +08:00
|
|
|
|
def print(self) -> None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""打印邻接矩阵"""
|
2023-02-23 20:00:40 +08:00
|
|
|
|
print("顶点列表 =", self.vertices)
|
|
|
|
|
print("邻接矩阵 =")
|
|
|
|
|
print_matrix(self.adj_mat)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" Driver Code """
|
|
|
|
|
if __name__ == "__main__":
|
2023-04-09 05:05:35 +08:00
|
|
|
|
# 初始化无向图
|
2023-02-23 20:00:40 +08:00
|
|
|
|
# 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引
|
2023-03-23 18:51:56 +08:00
|
|
|
|
vertices: list[int] = [1, 3, 2, 5, 4]
|
|
|
|
|
edges: list[list[int]] = [[0, 1], [0, 3], [1, 2], [2, 3], [2, 4], [3, 4]]
|
2023-02-23 20:00:40 +08:00
|
|
|
|
graph = GraphAdjMat(vertices, edges)
|
|
|
|
|
print("\n初始化后,图为")
|
|
|
|
|
graph.print()
|
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
# 添加边
|
2023-02-23 20:00:40 +08:00
|
|
|
|
# 顶点 1, 2 的索引分别为 0, 2
|
|
|
|
|
graph.add_edge(0, 2)
|
|
|
|
|
print("\n添加边 1-2 后,图为")
|
|
|
|
|
graph.print()
|
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
# 删除边
|
2023-02-23 20:00:40 +08:00
|
|
|
|
# 顶点 1, 3 的索引分别为 0, 1
|
|
|
|
|
graph.remove_edge(0, 1)
|
|
|
|
|
print("\n删除边 1-3 后,图为")
|
|
|
|
|
graph.print()
|
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
# 添加顶点
|
2023-02-23 20:00:40 +08:00
|
|
|
|
graph.add_vertex(6)
|
|
|
|
|
print("\n添加顶点 6 后,图为")
|
|
|
|
|
graph.print()
|
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
# 删除顶点
|
2023-02-23 20:00:40 +08:00
|
|
|
|
# 顶点 3 的索引为 1
|
|
|
|
|
graph.remove_vertex(1)
|
|
|
|
|
print("\n删除顶点 3 后,图为")
|
|
|
|
|
graph.print()
|