Я пытаюсь найти несколько / все охватывающие деревья из надежной матрицы. Затем выберите одно связующее дерево, которое выше определенного целевого значения.
Я нашел одно связующее дерево, но я не уверен, как изменить мой код, чтобы найти несколько связующих деревьев.
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})