#NEWLINE# def node_connectivity( G , s = None , t = None , flow_func = None ) : #NEWLINE# #INDENT# 'Returns node connectivity for a graph or digraph G.\n\n    Node connectivity is equal to the minimum number of nodes that\n    must be removed to disconnect G or render it trivial. If source\n    and target nodes are provided, this function returns the local node\n    connectivity: the minimum number of nodes that must be removed to break\n    all paths from source to target in G.\n\n    Parameters\n    ----------\n    G : NetworkX graph\n        Undirected graph\n\n    s : node\n        Source node. Optional. Default value: None.\n\n    t : node\n        Target node. Optional. Default value: None.\n\n    flow_func : function\n        A function for computing the maximum flow among a pair of nodes.\n        The function has to accept at least three parameters: a Digraph,\n        a source node, and a target node. And return a residual network\n        that follows NetworkX conventions (see :meth:`maximum_flow` for\n        details). If flow_func is None, the default maximum flow function\n        (:meth:`edmonds_karp`) is used. See below for details. The\n        choice of the default function may change from version\n        to version and should not be relied on. Default value: None.\n\n    Returns\n    -------\n    K : integer\n        Node connectivity of G, or local node connectivity if source\n        and target are provided.\n\n    Examples\n    --------\n    >>> # Platonic icosahedral graph is 5-node-connected\n    >>> G = nx.icosahedral_graph()\n    >>> nx.node_connectivity(G)\n    5\n\n    You can use alternative flow algorithms for the underlying maximum\n    flow computation. In dense networks the algorithm\n    :meth:`shortest_augmenting_path` will usually perform better\n    than the default :meth:`edmonds_karp`, which is faster for\n    sparse networks with highly skewed degree distributions. Alternative\n    flow functions have to be explicitly imported from the flow package.\n\n    >>> from networkx.algorithms.flow import shortest_augmenting_path\n    >>> nx.node_connectivity(G, flow_func=shortest_augmenting_path)\n    5\n\n    If you specify a pair of nodes (source and target) as parameters,\n    this function returns the value of local node connectivity.\n\n    >>> nx.node_connectivity(G, 3, 7)\n    5\n\n    If you need to perform several local computations among different\n    pairs of nodes on the same graph, it is recommended that you reuse\n    the data structures used in the maximum flow computations. See\n    :meth:`local_node_connectivity` for details.\n\n    Notes\n    -----\n    This is a flow based implementation of node connectivity. The\n    algorithm works by solving `O((n-\\delta-1+\\delta(\\delta-1)/2))`\n    maximum flow problems on an auxiliary digraph. Where `\\delta`\n    is the minimum degree of G. For details about the auxiliary\n    digraph and the computation of local node connectivity see\n    :meth:`local_node_connectivity`. This implementation is based\n    on algorithm 11 in [1]_.\n\n    See also\n    --------\n    :meth:`local_node_connectivity`\n    :meth:`edge_connectivity`\n    :meth:`maximum_flow`\n    :meth:`edmonds_karp`\n    :meth:`preflow_push`\n    :meth:`shortest_augmenting_path`\n\n    References\n    ----------\n    .. [1] Abdol-Hossein Esfahanian. Connectivity Algorithms.\n        http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf\n\n    ' #NEWLINE# if ( ( ( s is not None ) and ( t is None ) ) or ( ( s is None ) and ( t is not None ) ) ) : #NEWLINE# #INDENT# raise nx . NetworkXError ( 'Both source and target must be specified.' ) #NEWLINE# #UNINDENT# if ( ( s is not None ) and ( t is not None ) ) : #NEWLINE# #INDENT# if ( s not in G ) : #NEWLINE# #INDENT# raise nx . NetworkXError ( ( 'node %s not in graph' % s ) ) #NEWLINE# #UNINDENT# if ( t not in G ) : #NEWLINE# #INDENT# raise nx . NetworkXError ( ( 'node %s not in graph' % t ) ) #NEWLINE# #UNINDENT# return local_node_connectivity ( G , s , t , flow_func = flow_func ) #NEWLINE# #UNINDENT# if G . is_directed ( ) : #NEWLINE# #INDENT# if ( not nx . is_weakly_connected ( G ) ) : #NEWLINE# #INDENT# return 0 #NEWLINE# #UNINDENT# iter_func = itertools . permutations #NEWLINE# def neighbors( v ) : #NEWLINE# #INDENT# return itertools . chain . from_iterable ( [ G . predecessors ( v ) , G . successors ( v ) ] ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( not nx . is_connected ( G ) ) : #NEWLINE# #INDENT# return 0 #NEWLINE# #UNINDENT# iter_func = itertools . combinations #NEWLINE# neighbors = G . neighbors #NEWLINE# #UNINDENT# H = build_auxiliary_node_connectivity ( G ) #NEWLINE# R = build_residual_network ( H , 'capacity' ) #NEWLINE# kwargs = dict ( flow_func = flow_func , auxiliary = H , residual = R ) #NEWLINE# ( v , K ) = min ( G . degree ( ) , key = itemgetter ( 1 ) ) #NEWLINE# for w in ( ( set ( G ) - set ( neighbors ( v ) ) ) - set ( [ v ] ) ) : #NEWLINE# #INDENT# kwargs [ 'cutoff' ] = K #NEWLINE# K = min ( K , local_node_connectivity ( G , v , w , ** kwargs ) ) #NEWLINE# #UNINDENT# for ( x , y ) in iter_func ( neighbors ( v ) , 2 ) : #NEWLINE# #INDENT# if ( y in G [ x ] ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# kwargs [ 'cutoff' ] = K #NEWLINE# K = min ( K , local_node_connectivity ( G , x , y , ** kwargs ) ) #NEWLINE# #UNINDENT# return K