def minimum_st_node_cut(G, s, t, aux_digraph=None, mapping=None): r"""Returns a set of nodes of minimum cardinality that disconnect source from target in G. This function returns the set of nodes of minimum cardinality that, if removed, would destroy all paths among source and target in G. Parameters ---------- G : NetworkX graph s : node Source node. t : node Target node. Returns ------- cutset : set Set of nodes that, if removed, would destroy all paths between source and target in G. Examples -------- >>> # Platonic icosahedral graph has node connectivity 5 >>> G = nx.icosahedral_graph() >>> len(nx.minimum_node_cut(G, 0, 6)) 5 Notes ----- This is a flow based implementation of minimum node cut. The algorithm is based in solving a number of max-flow problems (ie local st-node connectivity, see local_node_connectivity) to determine the capacity of the minimum cut on an auxiliary directed network that corresponds to the minimum node cut of G. It handles both directed and undirected graphs. This implementation is based on algorithm 11 in [1]_. We use the Ford and Fulkerson algorithm to compute max flow (see ford_fulkerson). See also -------- node_connectivity edge_connectivity minimum_edge_cut max_flow ford_fulkerson References ---------- .. [1] Abdol-Hossein Esfahanian. Connectivity Algorithms. http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf """ if aux_digraph is None or mapping is None: H, mapping = _aux_digraph_node_connectivity(G) else: H = aux_digraph edge_cut = minimum_st_edge_cut(H, '%sB' % mapping[s], '%sA' % mapping[t]) # Each node in the original graph maps to two nodes of the auxiliary graph node_cut = set(H.node[node]['id'] for edge in edge_cut for node in edge) return node_cut - set([s,t])
def minimum_node_cut(G, s=None, t=None): r"""Returns a set of nodes of minimum cardinality that disconnects G. If source and target nodes are provided, this function returns the set of nodes of minimum cardinality that, if removed, would destroy all paths among source and target in G. If not, it returns a set of nodes of minimum cardinality that disconnects G. Parameters ---------- G : NetworkX graph s : node Source node. Optional (default=None) t : node Target node. Optional (default=None) Returns ------- cutset : set Set of nodes that, if removed, would disconnect G. If source and target nodes are provided, the set contians the nodes that if removed, would destroy all paths between source and target. Examples -------- >>> # Platonic icosahedral graph has node connectivity 5 >>> G = nx.icosahedral_graph() >>> len(nx.minimum_node_cut(G)) 5 >>> # this is the minimum over any pair of non adjacent nodes >>> from itertools import combinations >>> for u,v in combinations(G, 2): ... if v not in G[u]: ... assert(len(nx.minimum_node_cut(G,u,v)) == 5) ... Notes ----- This is a flow based implementation of minimum node cut. The algorithm is based in solving a number of max-flow problems (ie local st-node connectivity, see local_node_connectivity) to determine the capacity of the minimum cut on an auxiliary directed network that corresponds to the minimum node cut of G. It handles both directed and undirected graphs. This implementation is based on algorithm 11 in [1]_. We use the Ford and Fulkerson algorithm to compute max flow (see ford_fulkerson). See also -------- node_connectivity edge_connectivity minimum_edge_cut max_flow ford_fulkerson References ---------- .. [1] Abdol-Hossein Esfahanian. Connectivity Algorithms. http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf """ # Local minimum node cut if s is not None and t is not None: if s not in G: raise nx.NetworkXError('node %s not in graph' % s) if t not in G: raise nx.NetworkXError('node %s not in graph' % t) return minimum_st_node_cut(G, s, t) # Global minimum node cut # Analog to the algoritm 11 for global node connectivity in [1] if G.is_directed(): if not nx.is_weakly_connected(G): raise nx.NetworkXError('Input graph is not connected') iter_func = itertools.permutations def neighbors(v): return itertools.chain.from_iterable([G.predecessors_iter(v), G.successors_iter(v)]) else: if not nx.is_connected(G): raise nx.NetworkXError('Input graph is not connected') iter_func = itertools.combinations neighbors = G.neighbors_iter # Choose a node with minimum degree deg = G.degree() min_deg = min(deg.values()) v = next(n for n,d in deg.items() if d == min_deg) # Initial node cutset is all neighbors of the node with minimum degree min_cut = set(G[v]) # Reuse the auxiliary digraph H, mapping = _aux_digraph_node_connectivity(G) # compute st node cuts between v and all its non-neighbors nodes in G # and store the minimum for w in set(G) - set(neighbors(v)) - set([v]): this_cut = minimum_st_node_cut(G, v, w, aux_digraph=H, mapping=mapping) if len(min_cut) >= len(this_cut): min_cut = this_cut # Same for non adjacent pairs of neighbors of v for x,y in iter_func(neighbors(v),2): if y in G[x]: continue this_cut = minimum_st_node_cut(G, x, y, aux_digraph=H, mapping=mapping) if len(min_cut) >= len(this_cut): min_cut = this_cut return min_cut