Python |在相邻矩阵中查找多个生成树

时间:2019-03-18 00:28:25

标签: python matrix brute-force spanning-tree

我试图从可靠性相邻矩阵中找到多个/所有生成树。然后选择一棵高于特定目标值的生成树。

我已经找到了一棵生成树,但是我不确定如何修改代码以找到多个生成树。

def FindingSystemReliabilty(n, Rg, R, C):
    R_sys = 0
    C_sys = 0
    stepsArray = {}
    # spanningTree = {} , used to store each spanning tree
    visited = [False] * n # set n amount of cities false

    # finds first spanning tree of system that meets the specifications
    while (R_sys < Rg):   

        # Not creating multiple spanning trees??
        # loop through upper half of matrix 
        for i in range(n):

            for j in range(n):
                if (i>j): 

                    # check if city has been been visited
                    if( (visited[i] or visited[j]) == False):
                        # generating overall system output parameters 
                        R_sys *= R[i][j]
                        C_sys += C[i][j]
                        stepsArray.update({i:j})
                        #stepsArray[i] = j  
                        print(stepsArray)
                        if (visited[i] == False):
                            visited[i] = True
                        if (visited[j] == False):
                            visited[j] = True

                    elif(visited == [True] *n):
                        print(visited)
                        return R_sys, C_sys, stepsArray

        #storing Spanning Tree
        print("R_sys", R_sys, "C_sys", C_sys, "stepsArray", stepsArray)

    return "Error says hello"




# Driver code/ Unit test
R =     [[0, 0.94, 0.91, 0.96, 0.93, 0.92],
        [0.94, 0, 0.94, 0.97, 0.91, 0.92],
        [0.91, 0.94, 0, 0.94, 0.90, 0.94],
        [0.96, 0.97, 0.94, 0, 0.93, 0.96],
        [0.93, 0.91, 0.90, 0.93, 0, 0.91],
        [0.92, 0.92, 0.94, 0.96, 0.91, 0]]

C = [[0,  10, 25, 10, 20, 30],
    [10, 0,  10, 10, 25, 20],
    [25, 10, 0,  20, 40, 10],
    [10, 10, 20, 0,  20, 10],
    [20, 25, 40, 20, 0,  30],
    [30, 20, 10, 10, 30, 0]]


Rg = 0.9

print(FindingSystemReliabilty(6, Rg, R, C ))

当前输出:

{1: 0}
{1: 0, 3: 2}
{1: 0, 3: 2, 5: 4}
R_sys 0.0 C_sys 60 stepsArray {1: 0, 3: 2, 5: 4}
[True, True, True, True, True, True]
(0.0, 60, {1: 0, 3: 2, 5: 4})

0 个答案:

没有答案