图论-floyd算法-Dijkstra-python实现
这里我写了floyd算法以及求图的中点,中心,直径,需要的自取
1.算法:
import numpy as np a = float("inf") #无穷大 w0 = [[0,1,a,1],\ [a,0,1,a],\ [a,a,0,1],\ [a,a,a,0]] length = len(w0) r0_arr = np.zeros((length,length),dtype = "float32") w0_arr = np.array(w0) #算r0_arr for i in range(length): for j in range(length): if w0_arr[i][j] == a: r0_arr[i][j] = 0 elif w0_arr[i][j] == 0: r0_arr[i][j] = 0 else: r0_arr[i][j] = j + 1 #floyd算法 for k in range(length): print("k = {}:".format(k)) print("w{} = ".format(k)) print(w0_arr) print("r{} = ".format(k)) print(r0_arr) print("\n") for i in range(length): for j in range(length): if w0_arr[i][j] > w0_arr[i][k] + w0_arr[k][j]: w0_arr[i][j] = w0_arr[i][k] + w0_arr[k][j] r0_arr[i][j] = r0_arr[i][k] print("k = {}:".format(length)) print("w{} = ".format(length)) print(w0_arr) print("r{} = ".format(length)) print(r0_arr) print("\n") ''' w0 = [[0,a,a,1.2,9.2,a,0.5],\ [a,0,a,5,a,3.1,2],\ [a,a,0,a,a,4,1.5],\ [1.2,5,a,0,6.7,a,a],\ [9.2,a,a,6.7,0,15.6,a],\ [a,3.1,4,a,15.6,0,a],\ [0.5,2,1.5,a,a,a,0]] w0 = [[0,9,1,3,a,a],\ [1,0,4,a,7,a],\ [2,a,0,a,1,a],\ [a,a,5,0,2,7],\ [a,6,2,8,0,5],\ [7,a,2,a,2,0]] p143 127 '''
2.图的中点,中心,直径
import numpy as np a = float("inf") #无穷大 w0 = [[0,a,a,1.2,9.2,a,0.5],\ [a,0,a,5,a,3.1,2],\ [a,a,0,a,a,4,1.5],\ [1.2,5,a,0,6.7,a,a],\ [9.2,a,a,6.7,0,15.6,a],\ [a,3.1,4,a,15.6,0,a],\ [0.5,2,1.5,a,a,a,0]] length = len(w0) r0_arr = np.zeros((length,length),dtype = "float32") w0_arr = np.array(w0) #算r0_arr for i in range(length): for j in range(length): if w0_arr[i][j] == a: r0_arr[i][j] = 0 elif w0_arr[i][j] == 0: r0_arr[i][j] = 0 else: r0_arr[i][j] = j + 1 #floyd算法 for k in range(length): for i in range(length): for j in range(length): if w0_arr[i][j] > w0_arr[i][k] + w0_arr[k][j]: w0_arr[i][j] = w0_arr[i][k] + w0_arr[k][j] r0_arr[i][j] = r0_arr[i][k] w_length = w0_arr #输出的最终w max_w = [] #存储各行的最大值 sum_w = [] #存储各行的和 for i in range(length): max_w.append(max(w_length[i])) sum_w.append(sum(w_length[i])) print(max_w) print(sum_w) network_center = max_w.index(min(max_w)) + 1 #网的中心 network_point = sum_w.index(min(sum_w)) + 1 #网的中点 network_diameter = max(max_w) #网的直径 print("network_center:",network_center) print("network_point:",network_point) print("network_diameter:",network_diameter)
3. Dijkstra 算法
# path_matrix = { # 'A': {'B': 4, 'C': 6, 'D': 6, 'E': float("inf"), 'F': float("inf"), "G": float("inf")}, # 'B': {'A': float("inf"), "C": 1, "D": float("inf"), "E": 7, "F": float("inf"), "G": float("inf")}, # 'C':{'A':float("inf"),'B':float("inf"),'D':float("inf"),'E':6,'F':4,'G':float("inf")}, # 'D':{'A':float("inf"),'B':float("inf"),'C':2,'E':6,'F':4,'G':float("inf")}, # 'E':{'A':float("inf"),'B':float("inf"),'C':float("inf"),'D' :float("inf"),'F':float("inf"),'G':6}, # 'F':{'A':float("inf"),'B':float("inf"),'C':float("inf"),'D' :float("inf"),'E':1,'G':8}, # 'G':{'A':float("inf"),'B':float("inf"),'C':float("inf"),'D' :float("inf"),'E':1,'F':float("inf")} # } path_matrix = { 'A': {'B': 1, 'C': 1, 'D': float("inf"), 'E': float("inf"), 'F': float("inf"), "G": float("inf"), "H": float("inf")}, 'B': {'A': float("inf"), "C": float("inf"), "D": float("inf"), "E": 1, "F": float("inf"), "G": float("inf"), "H": float("inf")}, 'C': {'A': float("inf"), 'B': float("inf"), 'D': 1, 'E': float("inf"), 'F': 1, 'G': float("inf")}, 'D': {'A': float("inf"), 'B': float("inf"), 'C': float("inf"), 'E': 1, 'F': float("inf"), 'G': float("inf"), "H": float("inf")}, 'E': {'A': float("inf"), 'B': float("inf"), 'C': float("inf"), 'D': float("inf"), 'F': float("inf"), 'G': float("inf"), "H": 1}, 'F': {'A': float("inf"), 'B': float("inf"), 'C': float("inf"), 'D': float("inf"), 'E': float("inf"), 'G': 1, "H": float("inf")}, 'G': {'A': float("inf"), 'B': float("inf"), 'C': float("inf"), 'D': float("inf"), 'E': float("inf"), 'F': float("inf"), "H": 1}, 'H': {'A': float("inf"), 'B': float("inf"), 'C': float("inf"), 'D': float("inf"), 'E': float("inf"), 'F': float("inf"), 'G': float("inf")}, } def get_shortest_path(path_maxtrix,origin_node): ''' 根据dijkstra算法求解从origin_node出发到各点的路径 :param path_maxtrix: 各点到各点的路径情况,dict类型 :param origin_node: 起始位置,str类型 :return: origin_node到各点的最短距离,dict类型 ''' visited_node = [origin_node] result = path_maxtrix[origin_node] del path_maxtrix["A"] unvisited_list = list(path_maxtrix.keys()) # print(visited_node) # print(unvisited_list) # print(result) # print(path_matrix) while len(unvisited_list): temp_node = visited_node[-1] if temp_node != origin_node: # 如果不是起始点 distance_list = [] node_list = [] for item in unvisited_list: distance_list.append(path_matrix[temp_node][item]) node_list.append(item) # print(result[temp_node]+path_matrix[temp_node][item]) if result[item] > result[temp_node] + path_matrix[temp_node][item]: result[item] = result[temp_node] + path_matrix[temp_node][item] print(node_list) min_dist = min(distance_list) the_index = distance_list.index(min_dist) min_node = unvisited_list.pop(the_index) visited_node.append(min_node) else: # 如果是起始点 # 找到距离起点路径最短的地点 min_key = list(result.keys())[0] min_path = result[min_key] for key, value in result.items(): if value < min_path: min_path = value min_key = key visited_node.append(min_key) the_index = unvisited_list.index(min_key) unvisited_list.pop(the_index) print(result) get_shortest_path(path_matrix, "A") # https://zhuanlan.zhihu.com/p/374252139