Inilah solusi saya untuk masalah ini. Saya menghitung semua kombinasi yang mungkin dari k item n dan menghitung saling ketergantungan mereka dengan mengubah masalah dalam grafik-teoretis: Manakah grafik lengkap yang berisi semua node k dengan jumlah tepi terkecil (dependensi)? Berikut skrip python menggunakan perpustakaan networkx dan satu kemungkinan keluaran. Mohon maaf atas ambiguitas dalam pertanyaan saya!
Kode:
import networkx as nx
import itertools
import os
#Create new graph
G=nx.Graph()
#Each node represents a dimension
G.add_nodes_from([1,2,3,4,5,6,7,8,9,10,11])
#For each dimension add edges and correlations as weights
G.add_weighted_edges_from([(3,1,0.563),(3,2,0.25)])
G.add_weighted_edges_from([(4,1,0.688),(4,3,0.438)])
G.add_weighted_edges_from([(5,1,0.25),(5,2,0.063),(5,3,0.063),(5,4,0.063)])
G.add_weighted_edges_from([(6,1,0.063),(6,2,0.25),(6,3,0.063),(6,4,0.063),(6,5,0.063)])
G.add_weighted_edges_from([(7,2,0.25),(7,3,0.063),(7,5,0.125),(7,6,0.063)])
G.add_weighted_edges_from([(8,1,0.125),(8,2,0.125),(8,3,0.5625),(8,5,0.25),(8,6,0.188),(8,7,0.125)])
G.add_weighted_edges_from([(9,1,0.063),(9,2,0.063),(9,3,0.25),(9,6,0.438),(9,7,0.063),(9,8,0.063)])
G.add_weighted_edges_from([(10,1,0.25),(10,2,0.25),(10,3,0.563),(10,4,0.125),(10,5,0.125),(10,6,0.125),(10,7,0.125),(10,8,0.375),(10,9,0.125)])
G.add_weighted_edges_from([(11,1,0.125),(11,2,0.063),(11,3,0.438),(11,5,0.063),(11,6,0.1875),(11,7,0.125),(11,8,0.563),(11,9,0.125),(11,9,0.188)])
nodes = set(G.nodes())
combs = set(itertools.combinations(nodes,6))
sumList = []
for comb in combs:
S=G.subgraph(list(comb))
sum=0
for edge in S.edges(data=True):
sum+=edge[2]['weight']
sumList.append((sum,comb))
sorted = sorted(sumList, key=lambda tup: tup[0])
fo = open("dependency_ranking.txt","wb")
for i in range(0,len(sorted)):
totalWeight = sorted[i][0]
nodes = list(sorted[i][1])
nodes.sort()
out = str(i)+": "+str(totalWeight)+","+str(nodes)
fo.write(out.encode())
fo.write("\n".encode())
fo.close()
S=G.subgraph([1,2,3,4,6,7])
sum = 0
for edge in S.edges(data=True):
sum+=edge[2]['weight']
print(sum)
Output sampel:
0: 1.0659999999999998,[2, 4, 5, 7, 9, 11]
1: 1.127,[4, 5, 7, 9, 10, 11]
2: 1.128,[2, 4, 5, 9, 10, 11]
3: 1.19,[2, 4, 5, 7, 8, 9]
4: 1.2525,[4, 5, 6, 7, 10, 11]
5: 1.377,[2, 4, 5, 7, 9, 10]
6: 1.377,[2, 4, 7, 9, 10, 11]
7: 1.377,[2, 4, 5, 7, 10, 11]
Input grafik:
Grafik solusi:
Untuk contoh mainan, k = 4, n = 6: Input grafik:
Grafik solusi:
Terbaik,
Kristen