From aad75883f498b25ceae13f5bb3888f0c5e4860ad Mon Sep 17 00:00:00 2001 From: RiesBen Date: Fri, 16 Aug 2024 13:40:18 +0200 Subject: [PATCH] fix algorithm --- .../n_nodes_edges_network_algorithm.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/konnektor/network_planners/_networkx_implementations/n_nodes_edges_network_algorithm.py b/src/konnektor/network_planners/_networkx_implementations/n_nodes_edges_network_algorithm.py index 8be24f1..10a890e 100644 --- a/src/konnektor/network_planners/_networkx_implementations/n_nodes_edges_network_algorithm.py +++ b/src/konnektor/network_planners/_networkx_implementations/n_nodes_edges_network_algorithm.py @@ -24,9 +24,18 @@ def generate_network(self, edges: list[tuple[int, int]], w_edges.append((e[0], e[1], w)) nodes.extend(e) - final_edges = [] - node_con = {n: 0 for n in nodes} # early termination crit - for edge in sorted(w_edges, key=lambda x: x[2]): + # Build initial MST: + self.g = nx.Graph() + self.g.add_weighted_edges_from(ebunch_to_add=w_edges) + min_edges = nx.minimum_spanning_edges(self.g, weight='weight') + mse = [(e1, e2, edge_data['weight']) for i, (e1, e2, edge_data) in + enumerate(min_edges)] + + # Select Additional Edges + final_edges = mse + rest_edges = list(filter(lambda e: e not in mse, mse)) + node_con = {n: 1 for n in nodes} # early termination crit + for edge in sorted(rest_edges, key=lambda x: x[2]): e1 = edge[0] e2 = edge[1] if (node_con[e1] < self.target_node_connectivity):