• 图论-floyd算法-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("
    ")
        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("
    ")   
           
    
    '''
    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)
        
  • 相关阅读:
    mysql uodate 报错 You can't specify target table '**' for update in FROM clause
    设置mysql InnoDB存储引擎下取消自动提交事务
    SQL插入数据--数据中的某一列来自本表中的数据
    服务器部署静态页面
    json 和 jsonp
    Git 回滚
    java 自定义注解
    java BlockingQueque的多种实现
    java 多线程之ReentrantLock与condition
    storm 架构原理
  • 原文地址:https://www.cnblogs.com/xiao-yu-/p/13897293.html
Copyright © 2020-2023  润新知