def test_brandes_erlebach_book(): # Figure 1 chapter 7: Connectivity # http://www.informatik.uni-augsburg.de/thi/personen/kammer/Graph_Connectivity.pdf G = nx.Graph() G.add_edges_from([(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 6), (3, 4), (3, 6), (4, 6), (4, 7), (5, 7), (6, 8), (6, 9), (7, 8), (7, 10), (8, 11), (9, 10), (9, 11), (10, 11)]) for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) # edge cutsets assert_equal(3, len(nx.minimum_edge_cut(G, 1, 11, **kwargs)), msg=msg.format(flow_func.__name__)) edge_cut = nx.minimum_edge_cut(G, **kwargs) # Node 5 has only two edges assert_equal(2, len(edge_cut), msg=msg.format(flow_func.__name__)) H = G.copy() H.remove_edges_from(edge_cut) assert_false(nx.is_connected(H), msg=msg.format(flow_func.__name__)) # node cuts assert_equal(set([6, 7]), minimum_st_node_cut(G, 1, 11, **kwargs), msg=msg.format(flow_func.__name__)) assert_equal(set([6, 7]), nx.minimum_node_cut(G, 1, 11, **kwargs), msg=msg.format(flow_func.__name__)) node_cut = nx.minimum_node_cut(G, **kwargs) assert_equal(2, len(node_cut), msg=msg.format(flow_func.__name__)) H = G.copy() H.remove_nodes_from(node_cut) assert_false(nx.is_connected(H), msg=msg.format(flow_func.__name__))
def test_brandes_erlebach_book(): # Figure 1 chapter 7: Connectivity # http://www.informatik.uni-augsburg.de/thi/personen/kammer/Graph_Connectivity.pdf G = nx.Graph() G.add_edges_from([(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 6), (3, 4), (3, 6), (4, 6), (4, 7), (5, 7), (6, 8), (6, 9), (7, 8), (7, 10), (8, 11), (9, 10), (9, 11), (10, 11)]) for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) # edge cutsets assert_equal(3, len(nx.minimum_edge_cut(G, 1, 11, **kwargs)), msg=msg.format(flow_func.__name__)) edge_cut = nx.minimum_edge_cut(G, **kwargs) # Node 5 has only two edges assert_equal(2, len(edge_cut), msg=msg.format(flow_func.__name__)) H = G.copy() H.remove_edges_from(edge_cut) assert_false(nx.is_connected(H), msg=msg.format(flow_func.__name__)) # node cuts assert_equal(set([6, 7]), minimum_st_node_cut(G, 1, 11, **kwargs), msg=msg.format(flow_func.__name__)) assert_equal(set([6, 7]), nx.minimum_node_cut(G, 1, 11, **kwargs), msg=msg.format(flow_func.__name__)) node_cut = nx.minimum_node_cut(G, **kwargs) assert_equal(2, len(node_cut), msg=msg.format(flow_func.__name__)) H = G.copy() H.remove_nodes_from(node_cut) assert_false(nx.is_connected(H), msg=msg.format(flow_func.__name__))
def min_nodes_to_remove(self, graph, source_node, target_node): cv = check_graph_validity.Graphs() ret = cv.is_valid_min_nodes_graph(graph, source_node, target_node) if (not ret[0]): ret.append({}) print(ret) return ret try: # construct networkx graph from given graph G = nx.Graph() G.add_nodes_from(graph['nodes']) G.add_edges_from(graph['edges']) except Exception as e: return [False, str(e), {}] output = {} # get the minimum set of nodes/edges to disconnect source_node and targert_node nodes = list(minimum_st_node_cut(G, source_node, target_node)) edges = list(minimum_st_edge_cut(G, source_node, target_node)) edges = [list(e) for e in edges] output["nodes"] = nodes output["edges"] = edges return [True, 'success', output]
def test_brandes_erlebach_book(): # Figure 1 chapter 7: Connectivity # http://www.informatik.uni-augsburg.de/thi/personen/kammer/Graph_Connectivity.pdf G = nx.Graph() G.add_edges_from([(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 6), (3, 4), (3, 6), (4, 6), (4, 7), (5, 7), (6, 8), (6, 9), (7, 8), (7, 10), (8, 11), (9, 10), (9, 11), (10, 11)]) for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) errmsg = f"Assertion failed in function: {flow_func.__name__}" # edge cutsets assert 3 == len(nx.minimum_edge_cut(G, 1, 11, **kwargs)), errmsg edge_cut = nx.minimum_edge_cut(G, **kwargs) # Node 5 has only two edges assert 2 == len(edge_cut), errmsg H = G.copy() H.remove_edges_from(edge_cut) assert not nx.is_connected(H), errmsg # node cuts assert {6, 7} == minimum_st_node_cut(G, 1, 11, **kwargs), errmsg assert {6, 7} == nx.minimum_node_cut(G, 1, 11, **kwargs), errmsg node_cut = nx.minimum_node_cut(G, **kwargs) assert 2 == len(node_cut), errmsg H = G.copy() H.remove_nodes_from(node_cut) assert not nx.is_connected(H), errmsg
def get_msis_from_dag(dag, source_nodes, terminal_nodes, fixed_nodes): """ Find a minimum separating information set MSIS from a dag that satisfying I<source_nodes | fixed_nodes union MSIS | terminal_nodes> with the minimum size Parameters ---------- dag : NetworkX DiGraph source_nodes : a set or list of source nodes terminal_nodes : a set or list of terminal nodes fixed_nodes : a set or list of pre-fixed nodes Returns ------- cutset : a set of nodes of MSIS ensuring I<source_nodes | fixed_nodes union MSIS | terminal_nodes> Notes ----- Implementation of this function only intended to ensure the correctness. 1. create an ancestral graph from the input dag with respect to the input nodes 2. moralize the ancestral graph 3. if the cardinality of the source_nodes (terminal_nodes) is greater than 1, introduce a super node 4. connect the super node with all nodes that are adjacent to source_nodes (target_nodes) 5. remove fixed_nodes 6. call nx.minimum_st_node_cut(processed_undirected_graph, source_node, target_node) 7. return the cutset returned by the minimum_st_node_cut """ def add_super_node(undirected_graph, nodes): super_node = max(undirected_graph.nodes_iter()) + 1 neighbor_nodes = set() for nn in nodes: neighbor_nodes.update(undirected_graph.neighbors(nn)) undirected_graph.add_edges_from([u, super_node] for u in neighbor_nodes) return super_node from networkx.algorithms.connectivity import minimum_st_node_cut # local networkx stored under libs all_nodes = set(source_nodes) | set(terminal_nodes) | set(fixed_nodes) anc_graph = ancestor_dag(dag, all_nodes) moral_graph = moralize_dag(anc_graph) if len(source_nodes) > 1: super_source = add_super_node(moral_graph, source_nodes) else: super_source = source_nodes[0] if len(terminal_nodes) > 1: super_terminal = add_super_node(moral_graph, terminal_nodes) else: super_terminal = terminal_nodes[0] moral_graph.remove_nodes_from(fixed_nodes) # remove fixed_nodes msis = minimum_st_node_cut(moral_graph, super_source, super_terminal) msis = msis - (set(source_nodes) | set(terminal_nodes)) return msis
def find_minimum_vertex_cut(new:bool=False,s:tuple=None,t:tuple=None): """ Description: Find minimum vertex cut Args: new (bool): A variable to decide which graph to use. Default value <False> Returns: minimum_node_cut """ global G global new_G if not new: return nx.minimum_node_cut(G) else: if s != None and t != None: return minimum_st_node_cut(new_G,s,t) return nx.minimum_node_cut(new_G) return None
def tests_minimum_st_node_cut(): G = nx.Graph() G.add_nodes_from([0, 1, 2, 3, 7, 8, 11, 12]) G.add_edges_from([(7, 11), (1, 11), (1, 12), (12, 8), (0, 1)]) nodelist = minimum_st_node_cut(G, 7, 11) assert(nodelist == [])
import itertools import sys import networkx as nx from networkx.algorithms.connectivity import local_node_connectivity from collections import defaultdict from networkx.algorithms.connectivity import minimum_st_node_cut from networkx.algorithms.connectivity import minimum_st_edge_cut from networkx.algorithms import approximation as approx from networkx.algorithms.connectivity import (build_auxiliary_node_connectivity ) from networkx.algorithms.flow import build_residual_network if __name__ == "__main__": G = nx.DiGraph() G.add_nodes_from([0, 1, 2, 3, 4, 5]) G.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 2), (2, 1), (2, 4), (3, 2), (3, 5), (4, 3), (4, 5)]) print minimum_st_node_cut(G, 0, 5) print "*************" # print minimum_st_edge_cut(G, 0, 5)
def tests_minimum_st_node_cut(): G = nx.Graph() G.add_nodes_from([0, 1, 2, 3, 7, 8, 11, 12]) G.add_edges_from([(7, 11), (1, 11), (1, 12), (12, 8), (0, 1)]) nodelist = minimum_st_node_cut(G, 7, 11) assert(nodelist == [])
def split_graph(G, s, t, left_vertex_set, right_vertex_set, cut_set, vertex_order_dict, result_edge_list): H = G.copy() #print "left =", left_vertex_set #print "right =", right_vertex_set #print "cut_set =", cut_set #if len(right_vertex_set) >= 10: # exit(1) for v in left_vertex_set: if v != s and v != t: H = nx.contracted_nodes(H, s, v) for v in right_vertex_set: if v != s and v != t: H = nx.contracted_nodes(H, t, v) if len(H.nodes()) <= 10 or H.has_edge(s, t): order_vertex_set = set(G.nodes()) order_vertex_set -= left_vertex_set order_vertex_set -= right_vertex_set order_vertex_set |= cut_set #print "order_by_NDS start" #print "order_vertex_set =", order_vertex_set order_by_NDS(G, vertex_order_dict, order_vertex_set, result_edge_list) #print "vertex_order_dict =", vertex_order_dict #print "result_edge_list =", result_edge_list return cut = minimum_st_node_cut(H, s, t) #print "cut =", cut Hc = H.copy() Hc.remove_nodes_from(cut) cc_list = list(nx.connected_components(Hc)) ccs = None for cc in cc_list: if s in cc: ccs = cc cc_list.remove(cc) break #print "cc_list =", cc_list #print "ccs =", ccs new_right_vertex_set = set() new_right_vertex_set |= right_vertex_set new_right_vertex_set |= cut for cc in cc_list: new_right_vertex_set |= cc #print "new_right_vertex_set =", new_right_vertex_set split_graph(G, s, t, left_vertex_set, new_right_vertex_set, cut, vertex_order_dict, result_edge_list) new_left_vertex_set = set() new_left_vertex_set |= left_vertex_set new_left_vertex_set |= ccs new_left_vertex_set |= cut #print "new_left_vertex_set =", new_left_vertex_set split_graph(G, s, t, new_left_vertex_set, right_vertex_set, cut_set, vertex_order_dict, result_edge_list)
pf_dict[last] += 0 pf_dict[splits[1]] += 0 print # print pf_dict print # Remove nodes with low neighbour count # Donot remove if its destination node. for node in G.nodes(): if len(G.neighbors(node)) < 1 and (not node in dest_as_list): G.remove_node(node) #Save complete graph copy for plotting purposes G_copy = G.copy() print local_node_connectivity(G, "1680", "8867") print minimum_st_node_cut(G, "1680", "8867") # find_cuts(G) fo = open("temp.txt", "w") for mm in all_cut_vertex: fo.write(mm+"\n"); print "number of cuts " + str(len(all_cut_vertex))
nx.diameter(G_karate) nx.diameter(G_electric) nx.diameter(G_internet) nx.density(G_karate) nx.density(G_electric) nx.density(G_internet) import networkx.algorithms.connectivity as nxcon nxcon.minimum_st_node_cut( G_karate, mr_hi, john_a ) # Returns a set of nodes of minimum cardinality that disconnect source from target in G nxcon.minimum_st_edge_cut(G_karate, mr_hi, john_a) nx.node_connectivity( G_karate, mr_hi, john_a ) # Returns an approximation for node connectivity for a graph or digraph G nx.edge_connectivity(G_karate, mr_hi, john_a) nxcon.minimum_node_cut( G_karate ) # Returns a set of nodes of minimum cardinality that disconnects G nxcon.minimum_edge_cut(G_karate)
def node_cut_to_all(self): PATH_FILE = constants.TEST_DATA + self.COUNTRY_CODE + "/" + self.COUNTRY_CODE + "_gao_cbgp_paths" + self.MODE_SUFFIX + ".txt" # PATH_FILE = "/Users/Madhur/Google_Drive/Thesis_Mtech/Test_Data/EG/EG2EG_finalpaths.txt" # PATH_FILE = "/Users/Madhur/Google_Drive/Thesis_Mtech/Test_Data/EG/EG_gao_cbgp_paths_country_56.txt" print "PATH_FILE " + PATH_FILE mapping_dict = self.get_mapping_dict(self.BIT16_TO_AS_MAPPING) (G, all_start_as, all_dest_as) = as_digraph(PATH_FILE, self.IS_CBGP, self.USING_START, mapping_dict, None, None, None, None) set_heuristic_weight(G, self.HEURISTIC) A = auxiliary_graph(G) union = set() print 'len(G.nodes())', len(G.nodes()) ASFILE = constants.TEST_DATA + self.COUNTRY_CODE + "/" + self.COUNTRY_CODE + "_AS.txt" fi = open(ASFILE) asset = set() print 'G.nodes()', G.nodes() for line in fi: line = line.strip() AS = line[2:] asset.add(AS) for node in G.nodes(): if node not in asset: print '$', node # Using Start in All to All case does not make much sense. if self.USING_START: for dest in all_start_as: if dest in all_start_as: print START, dest st_cut = minimum_st_node_cut(G, START, dest, auxiliary=H, residual=R) len_st_cut = len(st_cut) print st_cut print len_st_cut print if len_st_cut <= 200: union.update(st_cut) else: union.add(dest) else: print "warning: graph does not have node : " + dest else: print print "len(all_start_as) " + str(len(all_start_as)) print "len(all_dest_as) " + str(len(all_dest_as)) print count = 0 freq_of_node_in_cut = dict() counter = 0 for i, AS in enumerate(all_start_as): for dest in all_dest_as: if not dest == AS: print i, 'AS', AS, 'dest', dest H = A.copy() defense_cut = defense_st_cut(H, AS, dest) print '* defense_cut', defense_cut print '*' * 50 union.update(defense_cut) for node in defense_cut: if node in freq_of_node_in_cut: freq_of_node_in_cut[ node] = freq_of_node_in_cut[node] + 1 else: freq_of_node_in_cut[node] = 1 new_union = trim_defense_cut(G, freq_of_node_in_cut, all_start_as, all_dest_as) print 'union', union print "len(union) " + str(len(union)) print print 'new_union', new_union print "len(new_union) " + str(len(new_union)) print print "len(G.nodes()) " + str(len(G.nodes())) print raw_input("Press any key to continue...") print