邻接矩阵转为邻接表(字典的形式输出) # def matrix2table(martrix): # """输入图的邻接矩阵,输出邻接表""" # result = defaultdict(set) # N = len(matrix) # for i in range(N): # tmp1 = {} # for j in range(N): # if (matrix[i][j] and (i != j)): # result[i].add(j) # return result # # adj_lists = defaultdict(set) # matrix = data.adj # 邻接矩阵 # matrix = [[0, 1, 0, 1, 0], [1, 0, 1, 0, 1], [0, 1, 0, 1, 0], [1, 0, 1, 0, 0], [0, 1, 1, 0, 0]] # adj_lists = matrix2table(matrix.tolist()) # print("matrix: ", matrix) # print("adj_lists: ", adj_lists)
python实现邻接矩阵转邻接表
最新推荐文章于 2023-08-27 23:38:05 发布