From 4ea6d73006204bed52cc42cd9674d3de8733b326 Mon Sep 17 00:00:00 2001 From: Erel Segal-Halevi Date: Wed, 31 Jul 2024 10:46:19 +0300 Subject: [PATCH] minbudget using mincut --- .../firefighter_problem/Firefighter_Problem.py | 11 ++++++----- .../approximation/firefighter_problem/Utils.py | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/networkz/algorithms/approximation/firefighter_problem/Firefighter_Problem.py b/networkz/algorithms/approximation/firefighter_problem/Firefighter_Problem.py index be0d810..81ac065 100644 --- a/networkz/algorithms/approximation/firefighter_problem/Firefighter_Problem.py +++ b/networkz/algorithms/approximation/firefighter_problem/Firefighter_Problem.py @@ -259,7 +259,7 @@ def non_spreading_minbudget(Graph:nx.DiGraph, source:int, targets:list) -> int: ------- min_budget : int Minimum budget required to save all target nodes. - + Examples: -------- Example 1: @@ -293,12 +293,13 @@ def non_spreading_minbudget(Graph:nx.DiGraph, source:int, targets:list) -> int: validate_parameters(Graph, source, targets) logger.info(f"Starting the non_spreading_minbudget function with source node {source} and targets: {targets}") - G = create_st_graph(Graph, targets) - logger.info(f"Calculating the minimum budget using edmonds karp algorithm") - min_budget = len(algo.minimum_st_node_cut(G, source, 't')) + G = create_st_graph(Graph, targets, 't') + min_cut = algo.minimum_st_node_cut(G, source, 't') + logger.info(f"Minimum s-t node cut: {min_cut}") + min_budget = len(min_cut) logger.info(f"Returning minimum budget: {min_budget}") - return min_budget, [] + return min_budget, [] # TODO: vaccinate all nodes in min_cut in the first step. def non_spreading_dirlaynet_minbudget(Graph:nx.DiGraph, source:int, targets:list) -> tuple[int, list]: """ diff --git a/networkz/algorithms/approximation/firefighter_problem/Utils.py b/networkz/algorithms/approximation/firefighter_problem/Utils.py index 4f05e1b..e4f4565 100644 --- a/networkz/algorithms/approximation/firefighter_problem/Utils.py +++ b/networkz/algorithms/approximation/firefighter_problem/Utils.py @@ -500,7 +500,7 @@ def adjust_nodes_capacity(graph:nx.DiGraph, source:int) -> list: return layers -def create_st_graph(graph:nx.DiGraph, targets:list) -> nx.DiGraph: +def create_st_graph(graph:nx.DiGraph, targets:list, new_target:str) -> nx.DiGraph: """ Create an s-t graph from the original graph for use in connectivity algorithms. @@ -532,9 +532,9 @@ def create_st_graph(graph:nx.DiGraph, targets:list) -> nx.DiGraph: logger.info(f"Creating a s-t graph to connect nodes to save") G = copy.deepcopy(graph) - G.add_node('t', status = Status.VULNERABLE.value) + G.add_node(new_target, status = Status.VULNERABLE.value) for node in targets: - G.add_edge(node,'t') + G.add_edge(node, new_target) #display_graph(G) logger.info(f"Done creating a s-t graph")