Voici ma solution au problème. Je calcule toutes les combinaisons possibles de k éléments n et calcule leurs dépendances mutuelles en transformant le problème en un graphique-théorique: Quel est le graphique complet contenant tous les k nœuds avec la plus petite somme de bord (dépendances)? Voici un script python utilisant la bibliothèque networkx et une sortie possible. Veuillez vous excuser pour toute ambiguïté dans ma question!
Code:
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)
Exemple de sortie:
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]
Graphique d'entrée:
Graphique de la solution:
Pour un exemple de jouet, k = 4, n = 6: Graphique d'entrée:
Graphique de la solution:
Meilleur,
Christian