Пример #1
0
def make_strongly_connected(G):
    components = nx.strongly_connected_components(G)
    num_components = len(components)
    if num_components == 1:
        return G

    # for each connected component, connect one node to a node in
    # the successor component, and delete an edge to make up for it.
    # which edge to delete isn't trivial -- it only needs to be an edge
    # that is somehow redundant in terms of connecting the graph. Our
    # approach is to delete an edge at random, and keep trying until
    # either the graph is connected or we exhaust the number of tries.
    attempts = 0
    max_attempts = num_components * math.log2(num_components)
    while num_components > 1 and attempts < max_attempts:
        for index in range(num_components):
            source_comp = components[index]
            target_comp = components[(index+1) % num_components]

            # pick a random vertex from the source component and connect it
            # to a vertex in the target component, deleting one of the outgoing
            # edges from the source vertex to keep the degree constant
            source_vertex = source_comp[npr.randint(len(source_comp))]
            target_vertex = target_comp[npr.randint(len(target_comp))]
            source_edges = list(G[source_vertex].keys())
            G.remove_edge(source_vertex, source_edges[npr.randint(len(source_edges))])
            G.add_edge(source_vertex, target_vertex)
        components = nx.strongly_connected_components(G)
        num_components = len(components)
        attempts += 1
    return G
Пример #2
0
    def _check_graph(self, out_stream=sys.stdout):
        # Cycles in group w/o solver
        cgraph = self.root._relevance._cgraph
        for grp in self.root.subgroups(recurse=True, include_self=True):
            path = [] if not grp.pathname else grp.pathname.split('.')
            graph = cgraph.subgraph([n for n in cgraph if n.startswith(grp.pathname)])
            renames = {}
            for node in graph.nodes_iter():
                renames[node] = '.'.join(node.split('.')[:len(path)+1])
                if renames[node] == node:
                    del renames[node]

            # get the graph of direct children of current group
            nx.relabel_nodes(graph, renames, copy=False)

            # remove self loops created by renaming
            graph.remove_edges_from([(u,v) for u,v in graph.edges()
                                         if u==v])

            strong = [s for s in nx.strongly_connected_components(graph)
                        if len(s)>1]

            if strong and isinstance(grp.nl_solver, RunOnce): # no solver, cycles BAD
                relstrong = []
                for slist in strong:
                    relstrong.append([])
                    for s in slist:
                        relstrong[-1].append(name_relative_to(grp.pathname, s))
                        relstrong[-1] = sorted(relstrong[-1])
                print("Group '%s' has the following cycles: %s" %
                     (grp.pathname, relstrong), file=out_stream)

            # Components/Systems/Groups are not in the right execution order
            subnames = [s.pathname for s in grp.subsystems()]
            while strong:
                # break cycles to check order
                lsys = [s for s in subnames if s in strong[0]]
                for p in graph.predecessors(lsys[0]):
                    if p in lsys:
                        graph.remove_edge(p, lsys[0])
                strong = [s for s in nx.strongly_connected_components(graph)
                            if len(s)>1]

            visited = set()
            out_of_order = set()
            for sub in grp.subsystems():
                visited.add(sub.pathname)
                for u,v in nx.dfs_edges(graph, sub.pathname):
                    if v in visited:
                        out_of_order.add(v)

            if out_of_order:
                print("In group '%s', the following subsystems are out-of-order: %s" %
                      (grp.pathname, sorted([name_relative_to(grp.pathname, n)
                                                for n in out_of_order])), file=out_stream)
Пример #3
0
    def find_cut_dfg(self, dfg):
        condensed_graph_1 = nx.DiGraph()
        for scc in nx.strongly_connected_components(dfg):
            condensed_graph_1.add_node(tuple(scc))
        for edge in dfg.edges():
            sccu = None
            sccv = None
            u = edge[0]
            for scc in nx.strongly_connected_components(dfg):
                if u in scc:
                    sccu = tuple(scc)
            v = edge[1]
            for scc in nx.strongly_connected_components(dfg):
                if v in scc:
                    sccv = tuple(scc)
            if sccv != sccu:
                condensed_graph_1.add_edge(sccu, sccv)
        xor_graph = nx.DiGraph()
        xor_graph.add_nodes_from(condensed_graph_1)
        scr1 = CutFinderIMSequenceReachability(condensed_graph_1)

        for node in condensed_graph_1.nodes():
            reachable_from_to = scr1.get_reachable_from_to(node)
            not_reachable = set(condensed_graph_1.nodes()).difference(reachable_from_to)
            if node in not_reachable:
                not_reachable.remove(node)
            for node2 in not_reachable:
                xor_graph.add_edge(node, node2)
        condensed_graph_2 = nx.DiGraph()
        for n in nx.strongly_connected_components(xor_graph):
            r = set()
            for s in n:
                r.update(s)
            condensed_graph_2.add_node(tuple(r))
        for edge in condensed_graph_1.edges():
            sccu = None
            sccv = None
            u = edge[0]
            for scc in condensed_graph_2.nodes():
                if u[0] in scc:
                    sccu = tuple(scc)
            v = edge[1]
            for scc in condensed_graph_2.nodes():
                if v[0] in set(scc):
                    sccv = tuple(scc)
            if sccv != sccu:
                condensed_graph_2.add_edge(tuple(sccu), tuple(sccv))
        self.scr2 = CutFinderIMSequenceReachability(condensed_graph_2)
        result = []
        result.extend(set(condensed_graph_2.nodes()))
        result.sort(self.compare)
        return Cut(Operator.sequence, result)
Пример #4
0
 def directed_stats(self):
     #   UG = nx.to_undirected(g) #claims to not have this function     
     if nx.is_strongly_connected(g): 
         sconl = nx.strongly_connected_components(g)
     else: sconl = 'NA - graph is not strongly connected'
     result = {#"""returns boolean"""
         'scon': nx.is_strongly_connected(g), 
         'sconn': nx.number_connected_components(g),
         # """returns boolean"""        
         'dag': nx.is_directed_acyclic_graph(g),
         # """returns lists"""
         'sconl': nx.strongly_connected_components(g),
         #Conl = connected_component_subgraphs(Ug)
         }
     return result
Пример #5
0
def netstats_listsdi(graph):
    G = graph
 #   UG = nx.to_undirected(G) #claims to not have this function     
    if nx.is_strongly_connected(G): 
        sconl = nx.strongly_connected_components(G)
    else: sconl = 'NA - graph is not strongly connected'
    result = {#"""returns boolean"""
              'scon': nx.is_strongly_connected(G), 
              'sconn': nx.number_connected_components(G),
             # """returns boolean"""        
              'dag': nx.is_directed_acyclic_graph(G),
             # """returns lists"""
              'sconl': nx.strongly_connected_components(G),
#              Conl = connected_component_subgraphs(UG)
              }
    return result    
Пример #6
0
    def schwartz_set_heuristic(self):

        # Iterate through using the Schwartz set heuristic
        self.actions = []
        while len(self.graph.edges()) > 0:
            access = nx.connected_components(self.graph)
            mutual_access = nx.strongly_connected_components(self.graph)
            candidates_to_remove = set()
            for candidate in self.graph.nodes():
                candidates_to_remove |= (set(access[candidate]) - set(mutual_access[candidate]))

            # Remove nodes at the end of non-cycle paths
            if len(candidates_to_remove) > 0:
                self.actions.append({'nodes': candidates_to_remove})
                for candidate in candidates_to_remove:
                    self.graph.remove_node(candidate)

            # If none exist, remove the weakest edges
            else:
                edge_weights = self.edge_weights(self.graph)
                self.actions.append({'edges': matching_keys(edge_weights, min(edge_weights.values()))})
                for edge in self.actions[-1]["edges"]:
                    self.graph.del_edge(edge)

        self.graph_winner()
Пример #7
0
def _high_degree_components(G, k):
    """Helper for filtering components that can't be k-edge-connected.

    Removes and generates each node with degree less than k.  Then generates
    remaining components where all nodes have degree at least k.
    """
    # Iteravely remove parts of the graph that are not k-edge-connected
    H = G.copy()
    singletons = set(_low_degree_nodes(H, k))
    while singletons:
        # Only search neighbors of removed nodes
        nbunch = set(it.chain.from_iterable(map(H.neighbors, singletons)))
        nbunch.difference_update(singletons)
        H.remove_nodes_from(singletons)
        for node in singletons:
            yield {node}
        singletons = set(_low_degree_nodes(H, k, nbunch))

    # Note: remaining connected components may not be k-edge-connected
    if G.is_directed():
        for cc in nx.strongly_connected_components(H):
            yield cc
    else:
        for cc in nx.connected_components(H):
            yield cc
Пример #8
0
    def updateSCCs(self, transitions):
        '''Updates the subgraphs and SCCs associated with each pair in the
        acceptance set.
        
        Note: Assumes that all transitions correspond to a single TS transition.
        '''
        # process transitions and update SCCs based on intersection probabilities
        for k, subg in enumerate(self.pa.subgraphs):
            # check if any of the transitions intersect the k-th bad set
            if all([(d['sat'][k][1] == 0) for _, _, d in transitions]):
                sub_trans = [(u, v, {'prob': d['prob']})
                                                    for u, v, d in transitions]
                subg.add_edges_from(sub_trans) # add all transitions to subgraph
                good_trans = [(u, v) for u, v, d in transitions
                                         if d['sat'][k][0] > 0]
                self.pa.good_transitions[k].extend(good_trans)
#         for u, v, d in transitions: #TODO: check this
#             for k, pathProb in enumerate(d['sat']):
#                 if not pathProb[1]: # does not intersect the bad set
#                     self.pa.subgraphs[k].add_edge(u, v, prob=d['prob'])
#                 if pathProb[0]: # intersects the good set, save it
#                     self.pa.good_transitions[k].append((u, v))
        # update SCCs
        for k, subg in enumerate(self.pa.subgraphs): #TODO: check this
            self.pa.sccs[k] = map(tuple, nx.strongly_connected_components(subg))
        # compute good SCCs
        self.pa.good_sccs = [set() for _ in self.rabin.final]
        for trs, sccs, gsccs in it.izip(self.pa.good_transitions, self.pa.sccs, self.pa.good_sccs):
            for u, v in trs:
                gsccs.update(tuple([scc for scc in sccs if (u in scc) and (v in scc)]))
Пример #9
0
def find_outdag(IGraph):
	"""
	Finds the maximal directed acyclic subgraph that is closed under the successors operation.
	Essentially, these components are the "output cascades" which can be exploited by various algorithms, e.g.
	the computation of basins of attraction.

	**arguments**:
		* *IGraph*: interaction graph

	**returns**:
		* *Names* (list): the outdag

	**example**::

		>>> find_outdag(igraph)
		['v7', 'v8', 'v9']
	"""

	graph = IGraph.copy()

	sccs = networkx.strongly_connected_components(graph)
	sccs = [list(x) for x in sccs]
	candidates = [scc[0] for scc in sccs if len(scc)==1]
	candidates = [x for x in candidates if not graph.has_edge(x,x)]
	sccs = [scc for scc in sccs if len(scc)>1 or graph.has_edge(scc[0],scc[0])]

	graph.add_node("!")
	for scc in sccs:
		graph.add_edge(scc[0],"!")

	outdags = [x for x in candidates if not networkx.has_path(graph,x,"!")]

	return outdags
Пример #10
0
    def check_connect(self):
        """ Check connectivity of rate matrix. 

        Returns
        -------
        keep_states : list
            Indexes of states from the largest strongly connected set.

        keep_keys : list
            Keys for the largest strongly connected set.

        Notes
        -----
        We use the Tarjan algorithm as implemented in NetworkX. [1]_

        ..[1] R. Tarjan, "Depth-first search and linear graph algorithms",
        SIAM Journal of Computing (1972).
        
        """
        D = nx.DiGraph(self.count)
        keep_states = sorted(list(nx.strongly_connected_components(D)), 
                key = len, reverse=True)[0]
        keep_states.sort()
        #keep_states = sorted(nx.strongly_connected_components(D)[0])
        keep_keys = map(lambda x: self.keys[x], keep_states)
        return keep_states, keep_keys
Пример #11
0
def attracting_components(G):
    """Generates a list of attracting components in `G`.

    An attracting component in a directed graph `G` is a strongly connected
    component with the property that a random walker on the graph will never
    leave the component, once it enters the component.

    The nodes in attracting components can also be thought of as recurrent
    nodes.  If a random walker enters the attractor containing the node, then
    the node will be visited infinitely often.

    Parameters
    ----------
    G : DiGraph, MultiDiGraph
        The graph to be analyzed.

    Returns
    -------
    attractors : generator of sets
        A generator of sets of nodes, one for each attracting component of G.

    See Also
    --------
    number_attracting_components
    is_attracting_component 
    attracting_component_subgraphs

    """
    scc = list(nx.strongly_connected_components(G))
    cG = nx.condensation(G, scc)
    for n in cG:
        if cG.out_degree(n) == 0:
            yield scc[n]
Пример #12
0
def demo_rgud():
    R = np.asarray([[ 1.0,  0.4, -0.4],
                    [ 0.4,  1.0,  0.6],
                    [-0.4,  0.6,  1.0]])
    
    nstates = 50
    nactions = 2
    mu = [100.0] * 3
    sigma = [10.0] * 3
    cov = cor2cov(R, sigma)
    rewards = mvnrewards(nstates, nactions, mu, cov)
    G = rgud(nstates, nactions)
    cc = nx.strongly_connected_components(G)
    G2 = make_strongly_connected(G)
    cc2 = nx.strongly_connected_components(G2)
    return (G, G2, cc, cc2)
Пример #13
0
def chaines(nb_ordi,seuil,nb_lignes):
    import csv
    import sqlite3
    import cPickle
    from collections import defaultdict
    from itertools import combinations
    import networkx as nx
    import operator
    conn=sqlite3.connect('proba_apparition_%s.sqlite'%nb_lignes)
    c=conn.cursor()
    prob_cond=cPickle.load(open('dict_prob_cond_%s_%s_%s_new.pkl'%(nb_lignes,seuil,nb_ordi),'rb'))
    H=[(key,value) for key in prob_cond.iterkeys() for value in prob_cond[key]]
    G=nx.DiGraph(H)
    liste=nx.strongly_connected_components(G)
    d=open('dictio_chaine_%s_%s_%s_new.pkl'%(nb_lignes,seuil,nb_ordi),'wb')
    dictio_chaine={item[0]:item for item in liste}
    
    with open('chaines_%s_%s_%s_new.csv'%(nb_lignes,seuil,nb_ordi),'wb') as f:
        f=csv.writer(f,delimiter='|')
        func=lambda k:c.execute('select nom_app from proba_apparition where id_app=={}'.format(k)).next()[0]
        for item in liste:
            if len(item)>=2:
                f.writerow([func(item[0]),map(func,item)])
    conn.close()
    cPickle.dump(dictio_chaine,d,-1)
    d.close()
Пример #14
0
def get_paths_in_scc(f, sc):
    # f: fragment
    # sc: subgraph components in fragment 'f'

    # returns list of those paths that are part of at least one 'large' strongly-connected component
    # i.e. only those paths are returned that are part of an scc of length > 1

    substance_edges_in_paths = set()
    for key in sc.keys():
        for path in sc[key]['n_paths']+sc[key]['p_paths']:
            curr_edge = (path[0],path[2])
#            if curr_edge not in substance_edges_in_paths:
            substance_edges_in_paths.add(curr_edge)
    substance_edges_in_paths = list(substance_edges_in_paths)

    substance_graph = nx.DiGraph()
    substance_graph.add_edges_from(substance_edges_in_paths)

    strong_comp = nx.strongly_connected_components(substance_graph)

    # collect paths for return
    n_paths = set()
    p_paths = set()
    count = 0
    for scc in strong_comp:
        if len(scc) > 1:
            for substance in scc:
                for path in sc[substance]['n_paths']:
                    n_paths.add(path)
                    count = count + 1
                for path in sc[substance]['p_paths']:
                    p_paths.add(path)
                    count = count + 1
    # return
    return {'n_paths': list(n_paths), 'p_paths': list(p_paths), 'count': count}
Пример #15
0
    def find_widening_points(function_addr, function_endpoints, graph):  # pylint: disable=unused-argument
        """
        Given a local transition graph of a function, find all widening points inside.

        Correctly choosing widening points is very important in order to not lose too much information during static
        analysis. We mainly consider merge points that has at least one loop back edges coming in as widening points.

        :param int function_addr: Address of the function.
        :param list function_endpoints: Endpoints of the function, typically coming from Function.endpoints.
        :param networkx.DiGraph graph: A local transition graph of a function, normally Function.graph.
        :return: A list of addresses of widening points.
        :rtype: list
        """

        sccs = networkx.strongly_connected_components(graph)

        widening_addrs = set()

        for scc in sccs:
            if len(scc) == 1:
                node = next(iter(scc))
                if graph.has_edge(node, node):
                    # self loop
                    widening_addrs.add(node.addr)
            else:
                for n in scc:
                    predecessors = graph.predecessors(n)
                    if any([p not in scc for p in predecessors]):
                        widening_addrs.add(n.addr)
                        break

        return list(widening_addrs)
Пример #16
0
 def test_null_graph(self):
     G = nx.DiGraph()
     assert_equal(list(nx.strongly_connected_components(G)), [])
     assert_equal(list(nx.kosaraju_strongly_connected_components(G)), [])
     assert_equal(list(nx.strongly_connected_components_recursive(G)), [])
     assert_equal(len(nx.condensation(G)), 0)
     assert_raises(nx.NetworkXPointlessConcept, nx.is_strongly_connected, nx.DiGraph())
Пример #17
0
def has_large_scc_in_substance_graph(f, sc):
    # f: fragment
    # sc: subgraph components in fragment 'f'

    # tells you if fragment 'f' has strongly-connected component of length greater than one in corresponding directed graph induced by paths in sc
    # this should tell you if fragment f contains subgraphs that have at least one cycle of length greater than one
    
    substance_edges_in_paths = set()
    for key in sc.keys():
        for path in sc[key]['n_paths']+sc[key]['p_paths']:
            # 
            curr_edge = (path[0],path[2])
#            if curr_edge not in substance_edges_in_paths:
            substance_edges_in_paths.add(curr_edge)
    substance_edges_in_paths = list(substance_edges_in_paths)

    substance_graph = nx.DiGraph()
    substance_graph.add_edges_from(substance_edges_in_paths)

    strong_comp = nx.strongly_connected_components(substance_graph)

    if max([len(el) for el in strong_comp]) > 1:
        # strongly-connected component of size > 1 detected in directed substance graph
        return True
    else:
        # all strongly-connected components are individual substance nodes
        return False
Пример #18
0
def process(filename):  
    clauses, literalCount = read(filename)  
  
    G=nx.DiGraph()  
    G.add_nodes_from(range(1, literalCount + 1) + range(-literalCount, 0))  
  
    for clause in clauses:  
        G.add_edge(-clause[0], clause[1])  
        G.add_edge(-clause[1], clause[0])  
  
    print "Graph creation for %s completed" % filename  
  
    components = nx.strongly_connected_components(G)  
  
    print "Calculation of SSC for %s completed" % filename  
  
    isSatisfiable = True  
    for component in filter(lambda component : len(component) > 1, components):  
        isSatisfiableComponent = True  
        vertexes = set()  
        for vertex in component:  
            if -vertex in vertexes:  
                isSatisfiableComponent = False  
                break  
            vertexes.add(vertex)  
        if not isSatisfiableComponent:  
            isSatisfiable = False  
            break  
  
    print "%s satisfiable result: %s" % (filename, isSatisfiable)  
    return isSatisfiable  
Пример #19
0
    def scc_dicts(self) :
        """
        bestimmt SCCs und gibt dictionary Knoten>SCC und SCC>[Knoten] zurueck
        SCCs, die nur aus einem Element bestehen, welches kein Attraktor (= nicht in attr)
        ist, werden in SCC 0 verschoben und tauchen in node2scc nicht auf
        """
        if not self._attrs :
             self._attrs = nx.attracting_components(self.stg())
        sccs=nx.strongly_connected_components(self.stg()) # erzeugt Liste SCC>[Knoten]
        node2scc={}
        scc2nodes={}
        attr_flattened=[item for sublist in [list(x) for x in self._attrs] for item in sublist]
        # Liste durchgehen und a) fuer jeden Knoten SCC und b) fuer jede SCC Knoten speichern
        for (i,nodes) in enumerate(sccs):
            for node in nodes:

                # pruefen, ob Knoten in trivialem SCC liegt und kein Attraktor ist
                if len(nodes)<=1 and (node not in attr_flattened):
                    scc_index=0 # in diesem Fall wird Knoten in SCC 0 verschoben
                else:
                    # ansonsten entspricht die SCC-Nummer dem Index+1
                    # +1, damit Index 0 fuer Sammlung trivialer SCCs zur Verfuegung steht
                    scc_index=i+1

                    node2scc[node]=scc_index # dictionary Knoten>SCC schreiben

                if scc_index not in scc2nodes: # pruefen, ob SCC bereits in dictionary SCC>[Knoten] vorhanden ist
                    scc2nodes[scc_index]=[] # ggf. Eintrag erstellen
                scc2nodes[scc_index].append(node) # und aktuellen Knoten hinzufuegen

        return(node2scc,scc2nodes,sccs)
Пример #20
0
	def splitG(self,minN):
		contcmp=[]
		ct_cliq=[]

		cncp=list(strongly_connected_components(self))
		for item in cncp:
			#print(len(item))
			if len(item)<=minN and len(item)>1:
				#print("topic")
				tmp=set()
				tmp_cliq=[]
				for each in item:
					tmp=tmp.union(set(self.node[each]["keywords"]))
					tmp_cliq.append(each)
					DiGraph.remove_node(self,each)
				contcmp.append(tmp)
				ct_cliq.append(tmp_cliq)
			if len(item)==1:
				DiGraph.remove_node(self,list(item)[0])

		nodes=DiGraph.nodes(self)
		for node in nodes:
			if DiGraph.in_degree(self,node)==0 and DiGraph.out_degree(self,node)==0:
				DiGraph.remove_node(self,node)

		return contcmp,ct_cliq
Пример #21
0
def lesion_met_largest_strong_component(G, orig_order=None):
    """
    Get largest strong component size of a graph.

    Parameters
    ----------
    G : directed networkx graph
        Graph to compute largest component for
    orig_order : int
        Define orig_order if you'd like the largest component proportion

    Returns
    -------
    largest strong component size : int
        Proportion of largest remaning component size if orig_order
        is defined. Otherwise, return number of nodes in largest component.
    """

    components = sorted(nx.strongly_connected_components(G), key=len,
                        reverse=True)
    if len(components) > 0:
        largest_component = len(components[0])
    else:
        largest_component = 0.

    # Check if original component size is defined
    if orig_order is not None:
        return largest_component / float(orig_order)
    else:
        return largest_component
Пример #22
0
def connected_subgraphs(nm_graph, nodes=None):
    """

    >>> anm = autonetkit.topos.house()
    >>> g_phy = anm['phy']
    >>> connected_subgraphs(g_phy)
    [[r4, r5, r1, r2, r3]]
    >>> edges = [("r2", "r4"), ("r3", "r5")]
    >>> g_phy.remove_edges_from(edges)
    >>> connected_subgraphs(g_phy)
    [[r1, r2, r3], [r4, r5]]


    """
    if nodes is None:
        nodes = nm_graph.nodes()
    else:
        nodes = list(unwrap_nodes(nodes))
    graph = unwrap_graph(nm_graph)
    subgraph = graph.subgraph(nodes)
    if not len(subgraph.edges()):

        # print "Nothing to aggregate for %s: no edges in subgraph"

        pass
    if graph.is_directed():
        component_nodes_list = nx.strongly_connected_components(subgraph)
    else:
        component_nodes_list = nx.connected_components(subgraph)

    wrapped = []
    for component in component_nodes_list:
        wrapped.append(list(wrap_nodes(nm_graph, component)))

    return wrapped
Пример #23
0
def aggregate_nodes(nm_graph, nodes, retain=None):
    """Combines connected into a single node"""
    if retain is None:
        retain = []

    try:
        retain.lower()
        retain = [retain]  # was a string, put into list
    except AttributeError:
        pass  # already a list

    nodes = list(unwrap_nodes(nodes))
    graph = unwrap_graph(nm_graph)
    subgraph = graph.subgraph(nodes)
    if not len(subgraph.edges()):

        # print "Nothing to aggregate for %s: no edges in subgraph"

        pass
    total_added_edges = []
    if graph.is_directed():
        component_nodes_list = nx.strongly_connected_components(subgraph)
    else:
        component_nodes_list = nx.connected_components(subgraph)
    for component_nodes in component_nodes_list:
        if len(component_nodes) > 1:
            component_nodes = [nm_graph.node(n) for n in component_nodes]

            # TODO: could choose most connected, or most central?
            # TODO: refactor so use nodes_to_remove

            nodes_to_remove = list(component_nodes)
            base = nodes_to_remove.pop()  # choose a base device to retain
            log.debug("Retaining %s, removing %s", base, nodes_to_remove)

            external_edges = []
            for node in nodes_to_remove:
                external_edges += [e for e in node.edges() if e.dst not in component_nodes]
                # all edges out of component

            log.debug("External edges %s", external_edges)
            edges_to_add = []
            for edge in external_edges:
                dst = edge.dst
                data = dict((key, edge._data.get(key)) for key in retain)
                ports = edge.raw_interfaces
                dst_int_id = ports[dst.node_id]

                # TODO: bind to (and maybe add) port on the new switch?

                data["_ports"] = {dst.node_id: dst_int_id}

                append = (base.node_id, dst.node_id, data)
                edges_to_add.append(append)

            nm_graph.add_edges_from(edges_to_add)
            total_added_edges += edges_to_add
            nm_graph.remove_nodes_from(nodes_to_remove)

    return wrap_edges(nm_graph, total_added_edges)
Пример #24
0
def remove_all_cycles(g):
	n_scc = [x for x in nx.strongly_connected_component_subgraphs(g) if len(x)>1]
	if len(n_scc)>0:
		print "Found",len(n_scc),"cycles, largest one of size",len(n_scc[0])
	else:
		print "No cycles"
		return g
	# Color them 
	for ccc_idx  in range(len(n_scc)): 
		for node in n_scc[ccc_idx].nodes():
			g.node[node]['in_scc']=ccc_idx

	#sys.exit(0)
	g_p=g
	for i in range(1,1000): #Up to a thousand iteration

		g_n= remove_cycle(g_p)
		if g_n.number_of_edges() == g_p.number_of_edges():
			break
	#	assert("10_4" in g_n)
		g_p=g_n
		n_scc_i = [x for x in nx.strongly_connected_component_subgraphs(g_p) if len(x)>1]
		print "Still",len(n_scc_i),"cycles"

	g_trans=g_p
	# Assert graph is acyclic 
	assert(max([len(x) for x in nx.strongly_connected_components(g_trans)])==1)

	return g_trans
Пример #25
0
def test_directed_aux_graph():
    # Graph similar to the one in
    # http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0136264
    a, b, c, d, e, f, g, h, i = 'abcdefghi'
    dipaths = [
        (a, d, b, f, c),
        (a, e, b),
        (a, e, b, c, g, b, a),
        (c, b),
        (f, g, f),
        (h, i)
    ]
    G = nx.DiGraph(it.chain(*[pairwise(path) for path in dipaths]))
    aux_graph = EdgeComponentAuxGraph.construct(G)

    components_1 = fset(aux_graph.k_edge_subgraphs(k=1))
    target_1 = fset([{a, b, c, d, e, f, g}, {h}, {i}])
    assert_equal(target_1, components_1)

    # Check that the directed case for k=1 agrees with SCCs
    alt_1 = fset(nx.strongly_connected_components(G))
    assert_equal(alt_1, components_1)

    components_2 = fset(aux_graph.k_edge_subgraphs(k=2))
    target_2 = fset([{i}, {e}, {d}, {b, c, f, g}, {h}, {a}])
    assert_equal(target_2, components_2)

    components_3 = fset(aux_graph.k_edge_subgraphs(k=3))
    target_3 = fset([{a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}])
    assert_equal(target_3, components_3)
def strongly_connected_components():
    conn = sqlite3.connect("zhihu.db")     
    #following_data = pd.read_sql('select user_url, followee_url from Following where followee_url in (select user_url from User where agree_num > 50000) and user_url in (select user_url from User where agree_num > 50000)', conn)        
    following_data = pd.read_sql('select user_url, followee_url from Following where followee_url in (select user_url from User where agree_num > 10000) and user_url in (select user_url from User where agree_num > 10000)', conn)        
    conn.close()
    
    G = nx.DiGraph()
    cnt = 0
    for d in following_data.iterrows():
        G.add_edge(d[1][0],d[1][1])
        cnt += 1
    print 'links number:', cnt

    scompgraphs = nx.strongly_connected_component_subgraphs(G)
    scomponents = sorted(nx.strongly_connected_components(G), key=len, reverse=True)
    print 'components nodes distribution:', [len(c) for c in scomponents]
    
    #plot graph of component, calculate saverage_shortest_path_length of components who has over 1 nodes
    index = 0
    print 'average_shortest_path_length of components who has over 1 nodes:'
    for tempg in scompgraphs:
        index += 1
        if len(tempg.nodes()) != 1:
            print nx.average_shortest_path_length(tempg)
            print 'diameter', nx.diameter(tempg)
            print 'radius', nx.radius(tempg)
        pylab.figure(index)
        nx.draw_networkx(tempg)
        pylab.show()

    # Components-as-nodes Graph
    cG = nx.condensation(G)
    pylab.figure('Components-as-nodes Graph')
    nx.draw_networkx(cG)
    pylab.show()    
Пример #27
0
def stg2htg(STG):
	"""
	Computes the HTG of the *STG*. For a definition see :ref:`Berenguier2013 <Berenguier2013>`.

	**arguments**:
		* *STG*: state transition graph

	**returns**:
		* *HTG* (networkx.DiGraph): the HTG of *STG*

	**example**::

		>>> htg = stg2htg(stg)
	"""

	graph = networkx.DiGraph()
	graph.graph["node"] = {"color":"none"}

	sccs = []
	cascades = []
	attractors = []
	for x in networkx.strongly_connected_components(STG):
		x=tuple(sorted(x))
		if len(x)>1 or STG.has_edge(x[0],x[0]):
			sccs.append(x)
			suc = PyBoolNet.Utility.DiGraphs.successors(STG,x)
			if set(suc)==set(x):
				attractors.append(x)
		else:
			cascades+= x

	graph.add_nodes_from(sccs, style="filled", fillcolor="lightgray", shape="rect")

	sigma = {}
	for x in cascades:
		pattern = []
		for i, A in enumerate(sccs):
			if PyBoolNet.Utility.DiGraphs.has_path(STG,x,A):
				pattern.append(i)
		pattern = tuple(pattern)

		if not pattern in sigma:
			sigma[pattern] = []
		sigma[pattern].append(x)

	I = [tuple(sorted(x)) for x in sigma.values()]
	graph.add_nodes_from(I)

	for X in graph.nodes():
		for Y in graph.nodes():
			if X==Y: continue

			if PyBoolNet.Utility.DiGraphs.has_edge(STG,X,Y):
				graph.add_edge(X,Y)

	for node in graph.nodes():
		lines = [",".join(x) for x in PyBoolNet.Utility.Misc.divide_list_into_similar_length_lists(node)]
		graph.node[node]["label"]="<%s>"%",<br/>".join(lines)

	return graph
Пример #28
0
def attracting_components(G):
    """Returns a list of attracting components in `G`.

    An attracting component in a directed graph `G` is a strongly connected
    component with the property that a random walker on the graph will never
    leave the component, once it enters the component.

    The nodes in attracting components can also be thought of as recurrent
    nodes.  If a random walker enters the attractor containing the node, then
    the node will be visited infinitely often.

    Parameters
    ----------
    G : DiGraph, MultiDiGraph
        The graph to be analyzed.

    Returns
    -------
    attractors : list
        The list of attracting components, sorted from largest attracting
        component to smallest attracting component.

    See Also
    --------
    number_attracting_components
    is_attracting_component 
    attracting_component_subgraphs

    """
    scc = nx.strongly_connected_components(G)
    cG = nx.condensation(G, scc)
    attractors = [scc[n] for n in cG if cG.out_degree(n) == 0]
    attractors.sort(key=len,reverse=True)
    return attractors
Пример #29
0
def plot_abstraction_scc(ab, ax=None):
    """Plot Regions colored by strongly connected component.
    
    Handy to develop new examples or debug existing ones.
    """
    ppp = ab.ppp
    ts = ab.ts
    ppp2ts = ab.ppp2ts
    
    # each connected component of filtered graph is a symbol
    components = nx.strongly_connected_components(ts)
    
    if ax is None:
        ax = mpl.pyplot.subplot()
    
    l, u = ab.ppp.domain.bounding_box
    ax.set_xlim(l[0,0], u[0,0])
    ax.set_ylim(l[1,0], u[1,0])
    
    for component in components:
        # map to random colors
        red = np.random.rand()
        green = np.random.rand()
        blue = np.random.rand()
        
        color = (red, green, blue)
        
        for state in component:
            i = ppp2ts.index(state)
            ppp[i].plot(ax=ax, color=color)
    return ax
Пример #30
0
    def HD_naive(E,increasing):

        import networkx as nx

        vertices = edges_to_vertices(E)
        edges = sorted(E, key = lambda e:e[2], reverse = not increasing)
        weights = sorted(list(set(e[2] for e in edges)), reverse = not increasing)

        # Add all of the vertices to the graph.

        G = nx.DiGraph()
        G.add_nodes_from(vertices)

        T = {}
        if increasing:
            roots = {v:tuple([0.0,v]) for v in vertices}
        elif not increasing:
            roots = {v:tuple([max(weights),v]) for v in vertices}

        i = 0
        m = len(edges)
        n = len(vertices)

        # Consider each weight.

        for weight in weights:

            # Add all of the edges with the same weight to the graph.

            while edges[i][2]==weight:
                G.add_edge(edges[i][0], edges[i][1], weight=edges[i][2])
                i += 1
                if i==m:
                    break

            # Find the SCCs of the resulting graph.

            components = [component for component in nx.strongly_connected_components(G)]

            # Check if any the components contracted by comparing the number of
            # components previously to the number of components currently.

            if len(components)<n:
                n = len(components)
                for component in components:

                    # Check each component for contractions by checking if
                    # every vertex in the component has the same root.  If not,
                    # a contraction occurred while adding the latest weight, so
                    # update the tree and roots.

                    first_root = roots[component[0]]
                    if not all(roots[v]==first_root for v in component):
                        root = tuple([weight]+sorted(component))
                        for v in component:
                            T[roots[v]] = root
                            roots[v] = root

        return T
def add_net_stats(compiled, data_dict, start_idx):
    """ Get data from adjacency matrix and add to dict """
    for k in data_dict.keys():
        A = data_dict[k]['adj']
        # Get stats
        g = nx.DiGraph(A.T.tolil())
        n = A.shape[0]
        scc = [list(c) for c in nx.strongly_connected_components(g)]
        scc_sz = [len(c) for c in scc]
        wcc = [list(c) for c in nx.weakly_connected_components(g)]
        wcc_sz = [len(c) for c in wcc]
        # Diameter of the largest scc
        diam = nx.diameter(nx.subgraph(g, scc[np.argmax(scc_sz)]))
        # Add to dictionary
        compiled["max_wcc"][start_idx + k] = np.max(wcc_sz)/n
        compiled["max_scc"][start_idx + k] = np.max(scc_sz)/n
        compiled["singletons"][start_idx + k] = np.sum(np.array(scc_sz) == 1)
        compiled["nscc"][start_idx + k] = len(scc)
        compiled["nwcc"][start_idx + k] = len(wcc)
        compiled["assort"][start_idx + k] = assort(g)
        compiled["cluster"][start_idx + k] = nx.average_clustering(g)
        compiled["diam"][start_idx + k] = diam
Пример #32
0
    def test_correctness(self):
        """
        This test check the correctness of shortest path and connected_component
        by compar it to the results from networkx on the same graph.
        :return:None
        """
        ga = GraphAlgo.GraphAlgo()
        filename = '../data/G_20000_160000_1.json'
        ga.load_from_json(filename)
        ganx = self.graph_nx(ga.get_graph())

        l = ga.shortest_path(0, 2)

        l2 = nx.single_source_dijkstra(ganx, 0, 2)
        self.assertEqual(l, l2)
        l = ga.connected_components()

        l2 = list(nx.strongly_connected_components(ganx))

        for i in range(len(l)):
            l[i].sort()
            self.assertTrue(set(l[i]) in l2)
Пример #33
0
def strong_weak_package_connections(s, g=None):
    if not g:
        g = create_graph(get_pkg_nodelist(s), get_pkg_edgelist(s))
    strong = [
        t for t in list(nx.strongly_connected_components(g)) if len(t) > 1
    ]
    strong_names = []
    for c in strong:
        names = []
        for p in c:
            names.append(
                s.query(db.Package.name).filter(db.Package.id == p).first())
        strong_names.append(names)
    weak = [t for t in list(nx.weakly_connected_components(g)) if len(t) > 1]
    weak_names = []
    for c in weak:
        names = []
        for p in c:
            names.append(
                s.query(db.Package.name).filter(db.Package.id == p).first())
        weak_names.append(names)
    return {'strong': strong_names, 'weak': weak_names}
Пример #34
0
def sanitize_graph(g):
    """Make sure that g can be used for benchmarking."""
    LOG.debug('Sanitizing %s (%d nodes, %d edges)', g.name,
              g.number_of_nodes(), g.number_of_edges())
    g.remove_edges_from(g.selfloop_edges())
    LOG.debug('Removed selfloops: %d nodes, %d edges', g.number_of_nodes(),
              g.number_of_edges())
    connected_component = max(nx.strongly_connected_components(g), key=len)
    g.remove_nodes_from(set(g.nodes_iter()).difference(connected_component))
    LOG.debug(
        'Kept only the largest strongly connected component: '
        '%d nodes, %d edges', g.number_of_nodes(), g.number_of_edges())
    g.build_spt()
    g.net_diameter = max(
        len(p[0]) for s, d in g.spt.iteritems() for p in d.itervalues())
    egresses = random.sample(g.nodes(), int(len(g) * .3))
    LOG.debug('Registering egresses: %s', egresses)
    for e in egresses:
        g.register_egress(e)
    LOG.info('Built graph for %s (%d nodes, %d edges, diameter: %d)', g.name,
             g.number_of_nodes(), g.number_of_edges(), g.net_diameter)
    return g
Пример #35
0
    def decluster(comp, parent, tree, sim_mat, thresh, rate):
        # print("comp\n",comp,"parent=",parent,"thresh=",thresh)
        node_index = parent

        alt_mat = sim_mat[list(comp),:][:,list(comp)] # filtering mat for rows of words in comp only
        mapping = dict()
        i=0
        for word in comp:
            mapping[i]=word
            i+=1
        # print(mapping)
        adj_mat = adjMatrix.build(alt_mat,seed=thresh,mode='absolute')
        g = nx.convert_matrix.from_numpy_array(adj_mat, create_using=nx.DiGraph) 
        g = nx.relabel_nodes(g,mapping)
        scc = nx.strongly_connected_components(g)

        scc = list(scc)
        # print(scc)

        comp_len = 0
        for comp in scc:
            comp_len+=1
        if comp_len is 1:
            # print('Redundant Iteration (No new node created)')
            return graphInfo.decluster(comp, parent, tree, sim_mat, thresh+rate, rate)
                # try another iter with a higher threshold
        else:
            for compC in scc:	# child component of parent node
                if len(compC) is 1:
                    for word in compC:
                        # print("new edge W:",parent,"->",word)
                        tree.add_edge(parent,word)
                else: 
                    # print("new node added F:", node_index+1)
                    new_node = node_index = node_index+1	# new null node created
                    tree.add_edge(parent,new_node)
                    # print("new edge N:",parent,"->",new_node)
                    node_index, tree = graphInfo.decluster(compC, new_node, tree, sim_mat, thresh+rate, rate)
            return node_index, tree
Пример #36
0
def strongly_connected_components():
    conn = sqlite3.connect("zhihu.db")
    #following_data = pd.read_sql('select user_url, followee_url from Following where followee_url in (select user_url from User where agree_num > 50000) and user_url in (select user_url from User where agree_num > 50000)', conn)
    following_data = pd.read_sql(
        'select user_url, followee_url from Following where followee_url in (select user_url from User where agree_num > 10000) and user_url in (select user_url from User where agree_num > 10000)',
        conn)
    conn.close()

    G = nx.DiGraph()
    cnt = 0
    for d in following_data.iterrows():
        G.add_edge(d[1][0], d[1][1])
        cnt += 1
    print 'links number:', cnt

    scompgraphs = nx.strongly_connected_component_subgraphs(G)
    scomponents = sorted(nx.strongly_connected_components(G),
                         key=len,
                         reverse=True)
    print 'components nodes distribution:', [len(c) for c in scomponents]

    #plot graph of component, calculate saverage_shortest_path_length of components who has over 1 nodes
    index = 0
    print 'average_shortest_path_length of components who has over 1 nodes:'
    for tempg in scompgraphs:
        index += 1
        if len(tempg.nodes()) != 1:
            print nx.average_shortest_path_length(tempg)
            print 'diameter', nx.diameter(tempg)
            print 'radius', nx.radius(tempg)
        pylab.figure(index)
        nx.draw_networkx(tempg)
        pylab.show()

    # Components-as-nodes Graph
    cG = nx.condensation(G)
    pylab.figure('Components-as-nodes Graph')
    nx.draw_networkx(cG)
    pylab.show()
def maincheck_networkX(s, keys):
    ga0 = GraphAlgo()
    ga0.load_from_json(s)
    ga = graph_to_nx(ga0.get_graph())

    t = time.time()
    list(nx.strongly_connected_components(ga))
    t = time.time() - t
    print('sccs:', t)
    print()

    s = keys.pop(0)
    e = keys.pop(0)
    t = time.time()

    nx.dijkstra_path(ga,
                     source=s,
                     target=e,
                     weight=lambda x, y, z: ga[x][y]['weight'])
    t = time.time() - t
    print('sp:', t)
    print()
def getFeatures(graph):
	d1 = graph.number_of_nodes()
	d2 = graph.number_of_edges()

	sg = max(nx.strongly_connected_components(graph), key=len)
	sg = graph.subgraph(sg)

	d3 = nx.diameter(sg)
	d4 = nx.average_shortest_path_length(sg)
	d5 = nx.density(graph)
	d6 = nx.average_clustering(graph)
	d7 = nx.betweenness_centrality(sg)

	edges = list(graph.edges())
	ug = nx.Graph()
	ug.add_edges_from(edges)

	d8 = community.modularity(community.best_partition(ug), ug)

	#print(d7.values())

	return [d1, d2, d3, d4, d5, d6, np.mean(list(d7.values())), d8]
Пример #39
0
 def test_contract_scc1(self):
     G = nx.DiGraph()
     G.add_edges_from([(1,2),(2,3),(2,11),(2,12),(3,4),(4,3),(4,5),
                       (5,6),(6,5),(6,7),(7,8),(7,9),(7,10),(8,9),
                       (9,7),(10,6),(11,2),(11,4),(11,6),(12,6),(12,11)])
     scc = list(nx.strongly_connected_components(G))
     cG = nx.condensation(G, scc)
     # DAG
     assert_true(nx.is_directed_acyclic_graph(cG))
     # # nodes
     assert_equal(sorted(cG.nodes()),[0,1,2,3])
     # # edges
     mapping={}
     for i,component in enumerate(scc):
         for n in component:
             mapping[n] = i
     edge=(mapping[2],mapping[3])
     assert_true(cG.has_edge(*edge))
     edge=(mapping[2],mapping[5])
     assert_true(cG.has_edge(*edge))
     edge=(mapping[3],mapping[5])
     assert_true(cG.has_edge(*edge))
Пример #40
0
    def test_condensation_as_quotient(self):
        """This tests that the condensation of a graph can be viewed as the
        quotient graph under the "in the same connected component" equivalence
        relation.

        """
        # This example graph comes from the file `test_strongly_connected.py`.
        G = nx.DiGraph()
        G.add_edges_from([(1, 2), (2, 3), (2, 11), (2, 12), (3, 4), (4, 3),
                          (4, 5), (5, 6), (6, 5), (6, 7), (7, 8), (7, 9),
                          (7, 10), (8, 9), (9, 7), (10, 6), (11, 2), (11, 4),
                          (11, 6), (12, 6), (12, 11)])
        scc = list(nx.strongly_connected_components(G))
        C = nx.condensation(G, scc)
        component_of = C.graph['mapping']
        # Two nodes are equivalent if they are in the same connected component.

        def same_component(u, v):
            return component_of[u] == component_of[v]

        Q = nx.quotient_graph(G, same_component)
        assert nx.is_isomorphic(C, Q)
Пример #41
0
    def _parse_loops_from_graph(self, graph: networkx.DiGraph):
        """
        Return all Loop instances that can be extracted from a graph.

        :param graph:   The graph to analyze.

        :return:        A list of all the Loop instances that were found in the graph.
        """
        outtop = []
        outall = []
        subg: networkx.DiGraph
        for subg in (
                networkx.induced_subgraph(graph, nodes).copy()
                for nodes in networkx.strongly_connected_components(graph)):
            if len(subg.nodes()) == 1:
                if len(list(subg.successors(list(subg.nodes())[0]))) == 0:
                    continue
            thisloop, allloops = self._parse_loop_graph(subg, graph)
            if thisloop is not None:
                outall += allloops
                outtop.append(thisloop)
        return outtop, outall
Пример #42
0
    def test_moderate_grns(self):
        k, N = 0, 0

        dex = DataDex()
        data = dex.select(['name', 'filename'],
                          ['is_biological', 'node_count <= 22'])
        for name, filename in data:
            exp_filename = os.path.join(filename, 'expressions.txt')
            ext_filename = os.path.join(filename, 'external.txt')
            net = nb.LogicNetwork.read_logic(exp_filename, ext_filename)
            g = net.to_networkx_graph()
            print()
            print("{}: {}".format(name, net.size))
            print("  Modules: {}".format(
                list(map(len, nx.strongly_connected_components(g)))))
            print("  DAG: {}".format(list(nx.condensation(g).edges())))

            start = time.time()
            got = main.attractors(net)
            stop = time.time()
            modular = stop - start
            print("  Modular Time:     {}s".format(modular))

            start = time.time()
            expected = ns.attractors(net)
            stop = time.time()
            brute_force = stop - start
            print("  Brute Force Time: {}s".format(brute_force))
            print("  Factor: {}".format(modular / brute_force))

            k += modular / brute_force
            N += 1

            self.assertUnorderedEqual(expected, got, msg=name)
            self.assertUnorderedEqual(got, expected, msg=name)

        print(k / N)
        self.assertTrue(k / N < 1.0,
                        msg='algorithm is slower than brute force on average')
def getFeatures(graph):
	d1 = graph.number_of_nodes()
	d2 = graph.number_of_edges()

	sg = nx.strongly_connected_components(graph)
	sg = [graph.subgraph(c) for c in sg]

	d3 = np.mean([nx.diameter(s) for s in sg])
	d4 = np.mean([nx.average_shortest_path_length(s) for s in sg])
	d5 = nx.density(graph)
	d6 = nx.average_clustering(graph)
	#d7 = nx.betweenness_centrality(sg)

	#edges = list(graph.edges())
	#ug = nx.Graph()
	#ug.add_edges_from(edges)

	#d8 = community.modularity(community.best_partition(ug), ug)

	#print(d7.values())

	return [d1, d2, d3, d4, d5, d6]
def flow_hierarchy(G, weight=None):
    """Returns the flow hierarchy of a directed network.

    Flow hierarchy is defined as the fraction of edges not participating
    in cycles in a directed graph [1]_.

    Parameters
    ----------
    G : DiGraph or MultiDiGraph
       A directed graph

    weight : key,optional (default=None)
       Attribute to use for node weights. If None the weight defaults to 1.

    Returns
    -------
    h : float
       Flow hierarchy value

    Notes
    -----
    The algorithm described in [1]_ computes the flow hierarchy through
    exponentiation of the adjacency matrix.  This function implements an
    alternative approach that finds strongly connected components.
    An edge is in a cycle if and only if it is in a strongly connected
    component, which can be found in $O(m)$ time using Tarjan's algorithm.

    References
    ----------
    .. [1] Luo, J.; Magee, C.L. (2011),
       Detecting evolving patterns of self-organizing networks by flow
       hierarchy measurement, Complexity, Volume 16 Issue 6 53-61.
       DOI: 10.1002/cplx.20368
       http://web.mit.edu/~cmagee/www/documents/28-DetectingEvolvingPatterns_FlowHierarchy.pdf
    """
    if not G.is_directed():
        raise nx.NetworkXError("G must be a digraph in flow_hierarchy")
    scc = nx.strongly_connected_components(G)
    return 1. - sum(G.subgraph(c).size(weight) for c in scc) / float(G.size(weight))
Пример #45
0
def compute_potentials(pa):
    '''Computes the potential function for each state of the product automaton.
    The potential function represents the minimum distance to a self-reachable
    final state in the product automaton.
    '''
    assert 'v' not in pa.g
    # add virtual node which connects to all initial states in the product
    pa.g.add_node('v')
    pa.g.add_edges_from([('v', p) for p in pa.init])
    # create strongly connected components of the product automaton w/ 'v'
    scc = list(nx.strongly_connected_components(pa.g))
    dag = nx.condensation(pa.g, scc)
    # get strongly connected component which contains 'v'
    for k, sc in enumerate(scc[::-1]):
        if 'v' in sc:
            start = len(scc) - k - 1
            break
    assert 'v' in scc[start]
    assert map(lambda sc: 'v' in sc, scc).count(True) == 1
    # get self-reachable final states
    pa.srfs = self_reachable_final_states_dag(pa, dag, scc, start)
    # remove virtual node from product automaton
    pa.g.remove_node('v')
    assert 'v' not in pa.g
    if not pa.srfs:
        return False
    # add artificial node 'v' and edges from the set of self reachable
    # states (pa.srfs) to 'v'
    pa.g.add_node('v')
    for p in pa.srfs:
        pa.g.add_edge(p, 'v', **{'weight': 0})
    # compute the potentials for each state of the product automaton
    lengths = nx.shortest_path_length(pa.g, target='v', weight='weight')
    for p in pa.g:
        pa.g.node[p]['potential'] = lengths[p]
    # remove virtual state 'v'
    pa.g.remove_node('v')
    return True
Пример #46
0
def filtering(GG, u):
    # filter GG with constraint u and return if GG is still satisfiable
    v = set()
    for i in unitlist[u]:
        if not set(GG[i]):
            return False
        v |= set(GG[i])
    G = GG.subgraph(unitlist[u] + list(v))
    max_matching = hopcroft_karp_matching(G, unitlist[u])
    max_matching = [(k, v) for k, v in max_matching.items()]
    v2x = [
        p if len(p[1]) == 2 else p[::-1] for p in list(
            map(
                tuple,
                set(map(frozenset, G.edges)) -
                set(map(frozenset, max_matching))))
    ]
    x2v = [p if len(p[0]) == 2 else p[::-1] for p in max_matching]
    assert {i for p in G.edges for i in p} == set(G.nodes)
    GM = nx.DiGraph(v2x + x2v)
    used = set(map(frozenset, max_matching))
    for i in nx.strongly_connected_components(GM):
        used |= set(map(frozenset, GM.subgraph(i).edges))
    m_free = list(set(G.nodes) - {i for p in max_matching for i in p})
    if m_free:
        for i in nx.bfs_successors(GM, m_free[0]):
            for j in i[1]:
                used.add(frozenset({i[0], j}))
    unused = list(map(tuple, set(map(frozenset, G.edges)) - used))
    GG.remove_edges_from(unused)

    affected_units = set()
    for e in unused:
        affected_units |= units[e[0] if len(e[0]) == 2 else e[1]]
    for unit in list(affected_units - {u}):
        if not filtering(GG, unit):
            return False
    return True
Пример #47
0
def digraph2sccgraph(Digraph):
    """
    Creates the strongly connected component graph from the interaction graph.
    Each node consists of a tuple of names the make up that nodes SCC.
    Note that the SCC graph is cycle-free and can therefore not distinguish
    between constants and inputs.
    The graph has no additional data.

    **arguments**:
        * *Digraph* (networkx.DiGraph): directed graph

    **returns**:
        * *(networkx.DiGraph)*: the SCC graph

    **example**::

            >>> sccgraph = digraph2sccgraph(igraph)
            >>> sccgraph.nodes()
            [('Ash1', 'Cbf1'), ('gal',), ('Gal80',), ('Gal4','Swi5)]
            >>> sccgraph.edges()
            [(('gal',), ('Ash1', 'Cbf1')), (('gal',), ('Gal80',)),
             (('Gal80',),('Gal4','Swi5))]
    """

    sccs = sorted([tuple(sorted(c)) for c in networkx.strongly_connected_components(Digraph)])

    sccgraph = networkx.DiGraph()
    sccgraph.add_nodes_from(sccs)

    for U,W in itertools.product(sccs, sccs):
        if U==W: continue # no self-loops in SCC graph

        for u,w in itertools.product(U,W):
            if Digraph.has_edge(u,w):
                sccgraph.add_edge(U,W)
                break

    return sccgraph
Пример #48
0
def get_explosive_diffuse(F: np.ndarray) -> np.ndarray:
    """
    If contains explosive roots, find strongly
    connected components. Check eigenvalues for
    each component submatrix, set large number to
    the ones with large component and run through 
    Lyapunov solver again to identify all the explosive
    roots.
    
    Parameters:
    ----------
    F : input state transition matrix
    
    Returns:
    ----------
    is_diffuse : indicator of diffuse states
    """
    # Find edges
    nonzeros = np.nonzero(F)
    index_ = list(zip(nonzeros[1], nonzeros[0]))
    
    # Calculate strongly connected components (scc)
    DG = nx.DiGraph()
    DG.add_edges_from(index_)
    scc = list(nx.strongly_connected_components(DG))
    F_ = F.copy()
    
    # Loop through each component for explosive roots
    for comp_ in scc:
        l_comp = list(comp_)
        sub_F = F_[np.ix_(l_comp, l_comp)]
        eig_ = linalg.eigvals(sub_F)
        if np.any(np.abs(eig_) > 1 + min_val):
            F_[l_comp, l_comp] = inf_val
    
    # Get diffuse states 
    is_diffuse = np.diag(F_) > max_val
    return is_diffuse
Пример #49
0
def attracting_components(G):
    """Generates a list of attracting components in `G`.

    An attracting component in a directed graph `G` is a strongly connected
    component with the property that a random walker on the graph will never
    leave the component, once it enters the component.

    The nodes in attracting components can also be thought of as recurrent
    nodes.  If a random walker enters the attractor containing the node, then
    the node will be visited infinitely often.

    Parameters
    ----------
    G : DiGraph, MultiDiGraph
        The graph to be analyzed.

    Returns
    -------
    attractors : generator of sets
        A generator of sets of nodes, one for each attracting component of G.

    Raises
    ------
    NetworkXNotImplemented :
        If the input graph is undirected.

    See Also
    --------
    number_attracting_components
    is_attracting_component
    attracting_component_subgraphs

    """
    scc = list(nx.strongly_connected_components(G))
    cG = nx.condensation(G, scc)
    for n in cG:
        if cG.out_degree(n) == 0:
            yield scc[n]
Пример #50
0
def print_info(fname: str,
               render_in: str = None,
               render_in_without_reactions: str = None):
    graph = nxgraph_from_file(fname)
    size = defaultdict(int)  # nb node: nb of SCC having this number of node
    for nodes in nx.strongly_connected_components(graph):
        nodes = frozenset(nodes)
        size[len(nodes)] += 1
    nb_node_computed = sum(nbnode * count for nbnode, count in size.items())
    assert nb_node_computed == graph.number_of_nodes(), (
        nb_node_computed, graph.number_of_nodes())
    print(f"- {graph.number_of_nodes()} nodes")
    print(f"- {graph.number_of_edges()} edges")
    print(f"- {sum(size.values())} Strongly Connected Components")
    nb_nontrivial_scc = sum(count for nbnode, count in size.items()
                            if nbnode > 1)
    print(
        f"- {nb_nontrivial_scc} Strongly Connected Components having more than 1 node"
    )
    headers = '#node', '#scc'
    colwidth = len(headers[0])
    print(' | '.join(header.center(len(header)) for header in headers))
    for nbnode in sorted(tuple(size.keys())):
        print(
            str(nbnode).rjust(len(headers[0])), '|',
            str(size[nbnode]).rjust(len(headers[1])))

    if render_in or render_in_without_reactions:
        asp_graph = graph_from_file(fname)
    if render_in:
        print('Input graph rendered in',
              utils.render_network(asp_graph, render_in, with_reactions=True))
    if render_in_without_reactions:
        print(
            'Input graph rendered in',
            utils.render_network(asp_graph,
                                 render_in_without_reactions,
                                 with_reactions=False))
Пример #51
0
def decompose_java_class(class_ast: AST, strength: str) -> List[AST]:
    """
    Splits java_class fields and methods by their usage and
    construct for each case an AST with only those fields and methods kept.
    Use "strength" parameter to control splitting criteria. Use "strong" or "weak"
    for splitting fields and methods by strong and weak connectivity.
    """

    usage_graph = _create_usage_graph(class_ast)

    components: Iterator[Set[int]]
    if strength == "strong":
        components = strongly_connected_components(usage_graph)
    elif strength == "weak":
        components = weakly_connected_components(usage_graph)
    else:
        raise ValueError(
            f"'strength' argument must be either 'strong' or 'weak', but '{strength}' was provided."
        )

    class_parts: List[AST] = []
    for component in components:
        field_names = {
            usage_graph.nodes[node]["name"]
            for node in component if usage_graph.nodes[node]["type"] == "field"
        }

        method_names = {
            usage_graph.nodes[node]["name"]
            for node in component
            if usage_graph.nodes[node]["type"] == "method"
        }

        class_parts.append(
            _filter_class_methods_and_fields(class_ast, field_names,
                                             method_names))

    return class_parts
Пример #52
0
def create_connected_components(graph):
    """
    Breaks a graph apart into non overlapping components (subgraphs). Nodes with 3 or more edges are removed which
    effectively splits the graph into sub-components.  This each component will resembles a single road constructed
    of multiple edges strung out in a line, but no intersections.
    Args:
        graph (networkx graph):
    Returns:
        list of connected components where each component is a networkx graph
    """

    # remove nodes with degree > 2
    graph_d2 = graph.copy()
    remove_nodes = []
    for node in graph.nodes():
        if graph_d2.degree(node) > 2:
            remove_nodes.append(node)
        graph_d2.remove_nodes_from(remove_nodes)

    # get connected components from graph with
    comps = [graph_d2.subgraph(c).copy() for c in nx.strongly_connected_components(graph_d2)]
    comps = [non_empty_comp for non_empty_comp in comps if len(non_empty_comp.edges()) > 0]
    return comps
Пример #53
0
    def components(self, how='STRONG', verbose=False):
        """

        :return:
        """
        if self.mode == 'ig':
            comps = self.g.components(mode=how)
            comps = [x for x in comps if x]
            graphs = [
                self.g.subgraph(x, implementation="create_from_scratch")
                for x in comps
            ]
            self.graphs = [g for g in graphs if len(g.es()) > 0]
        elif self.mode == 'nx':
            if how == 'STRONG':
                comps = nx.strongly_connected_components(self.g)
            else:
                comps = nx.connected_components(self.g)
            comps = [x for x in comps if x]
            graphs = [self.g.subgraph(x) for x in comps]
            self.graphs = graphs
        if verbose:
            print(f'found {len(self.graphs)} {how} components')
Пример #54
0
def transformToDAG(G):
    g2 = nx.DiGraph()
    components = list(nx.strongly_connected_components(G))
    for component in components:
        new_node = ','.join(str(e) for e in component)
        if not (new_node in g2.nodes):
            g2.add_node(new_node, size=len(component))

        suc_nodes = set()
        for e in component:
            for nei in G.neighbors(e):
                if not (nei in component):
                    suc_nodes.add(nei)
        for node in suc_nodes:
            for c in components:
                if node in c:
                    to_n = ','.join(str(e) for e in c)
                    break
            if not (to_n in g2.nodes):
                g2.add_node(to_n, size=len(component))
            if not ((new_node, to_n) in g2.edges):
                g2.add_edge(new_node, to_n, weight=g2.node[to_n]['size'])
    return g2
Пример #55
0
def _high_degree_components(G, k):
    """Helper for filtering components that can't be k-edge-connected.

    Removes and generates each node with degree less than k.  Then generates
    remaining components where all nodes have degree at least k.
    """
    # Iteravely remove parts of the graph that are not k-edge-connected
    H = G.copy()
    singletons = set(_low_degree_nodes(H, k))
    while singletons:
        # Only search neighbors of removed nodes
        nbunch = set(it.chain.from_iterable(map(H.neighbors, singletons)))
        nbunch.difference_update(singletons)
        H.remove_nodes_from(singletons)
        for node in singletons:
            yield {node}
        singletons = set(_low_degree_nodes(H, k, nbunch))

    # Note: remaining connected components may not be k-edge-connected
    if G.is_directed():
        yield from nx.strongly_connected_components(H)
    else:
        yield from nx.connected_components(H)
Пример #56
0
def deter_reach(v_min, m):
    
    if type(G) != nx.DiGraph and type(G) != nx.MultiDiGraph:
        raise GraphError("Given graph is not directed")
        
    # get all the strongly connected components
    sccs = list(sorted(nx.strongly_connected_components(G), key = len, reverse = False))
    n_sccs = len(sccs)
    sccs_sub = [0] * n_sccs
    
    # create subgraphs for each scc
    for i in range(n_sccs):
        sccs_sub[i] = G.subgraph(sccs[i])
        
    # for each subgraph try to connect it to the biggest scc
    for i in sccs_sub:
        # if the scc only has one node, it always needs a reverse arc
        if i.number_of_nodes() == 1:
            node_i = list(i.nodes())[0]
            pred, succ = list(G.predecessors(node_i)), list(G.neighbors(node_i))
    C_det = v_min / m
    
    return C_det    
Пример #57
0
def analyze(G):
    largest = max(nx.strongly_connected_component_subgraphs(G), key=len)
    #TODO some kind of check to ensure the lscc is much larger than others
    print("Node count of largest strongly connected component"), len(largest)
    print("Node count of first 10 strongly connected components"),
    print[
        len(c) for c in sorted(
            nx.strongly_connected_components(G), key=len, reverse=True)[0:10]
    ]
    H = largest
    uSPL = nx.average_shortest_path_length(H)
    logNodeCount = math.log(len(H))  # natural logarithm
    print("H = largest strongly connected sub component")
    print "H average clustering coefficient", nx.average_clustering(
        nx.Graph(H))
    print "H nodes", len(H), "H.edges", len(H.edges), "ln(nodes)",
    print("%.2f" % logNodeCount)
    print "H average shortest path length", uSPL
    print "Small-world found:"
    if uSPL < logNodeCount:
        print "YES"
    else:
        print "NO"
Пример #58
0
    def test_builtins(self):
        nets = [
            s_pombe, s_cerevisiae, c_elegans, p53_dmg, p53_no_dmg,
            mouse_cortical_7B, mouse_cortical_7C, myeloid
        ]

        k, N = 0, 0
        for net in nets:
            name = net.metadata['name']
            g = net.to_networkx_graph()
            print()
            print("{}: {}".format(name, net.size))
            print("  Modules: {}".format(
                list(map(len, nx.strongly_connected_components(g)))))
            print("  DAG: {}".format(list(nx.condensation(g).edges())))

            start = time.time()
            got = main.attractors(net)
            stop = time.time()
            modular = stop - start
            print("  Modular Time:     {}s".format(modular))

            start = time.time()
            expected = ns.attractors(net)
            stop = time.time()
            brute_force = stop - start
            print("  Brute Force Time: {}s".format(brute_force))
            print("  Factor: {}".format(modular / brute_force))

            k += modular / brute_force
            N += 1

            self.assertUnorderedEqual(expected, got, msg=name)
            self.assertUnorderedEqual(got, expected, msg=name)

        self.assertTrue(k / N < 1.0,
                        msg='algorithm is slower than brute force on average')
Пример #59
0
def plot_abstraction_scc(ab, ax=None):
    """Plot Regions colored by strongly connected component.
    
    Handy to develop new examples or debug existing ones.
    """
    try:
        import matplotlib as mpl
    except:
        logger.error('failed to load matplotlib')
        return

    ppp = ab.ppp
    ts = ab.ts
    ppp2ts = ab.ppp2ts

    # each connected component of filtered graph is a symbol
    components = nx.strongly_connected_components(ts)

    if ax is None:
        ax = mpl.pyplot.subplot()

    l, u = ab.ppp.domain.bounding_box
    ax.set_xlim(l[0, 0], u[0, 0])
    ax.set_ylim(l[1, 0], u[1, 0])

    for component in components:
        # map to random colors
        red = np.random.rand()
        green = np.random.rand()
        blue = np.random.rand()

        color = (red, green, blue)

        for state in component:
            i = ppp2ts.index(state)
            ppp[i].plot(ax=ax, color=color)
    return ax
Пример #60
0
def create_transition_graph( size ):
    # print( '[INFO] Creating transition network of size %d' % size )
    # print( '\t States of each element = %d' % num_states )
    allStates = itertools.product( [0, 1], repeat = size )
    network = nx.DiGraph( )
    network.graph['graph'] = { 
            'overlap' : 'false'
            , 'rankdir' : 'LR' 
            , 'splines' : 'true'
            }

    for i, ss in enumerate(allStates):
        network.add_node( ss, label = label(ss), order=i  )

    assert network.number_of_nodes( ) == 2 ** size
    # print( '[INFO] Total states %d' % network.number_of_nodes( ) )

    # Only connect nodes which are one distance away.
    for n in network.nodes():
        for nn in network.nodes():
            up, down = up_and_down_transitions( n, nn )
            # Probability of transition.
            if up + down != 1:
                pass
            elif up == 1:
                network.add_edge( n, nn, transition = str( up_ ) )
            elif down == 1:
                network.add_edge( n, nn, transition = str( down_ ) )

    components = 0
    for c in nx.strongly_connected_components( network ):
        components += 1
    if components != 1:
        print( "[WARN] We must have a strongly connected network" )
        print( "\tFound %d" % components )

    return network