Python networkx.node_connected_component() Examples

The following are 13 code examples of networkx.node_connected_component(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module networkx , or try the search function .
Example #1
Source File: K3000_gfa_post_treatment.py    From DiscoSnp with GNU Affero General Public License v3.0 6 votes vote down vote up
def remove_cc_with_cycles(DG):
    # remove pairend links and unitig links (unoriented)
    edges_to_remove = []
    for edge in DG.edges.data():
        if edge[2]['type'] == '-1M':
            edges_to_remove.append(edge)
            
    for edge in edges_to_remove:
        DG.remove_edge(edge[0],edge[1])
    cycles = list(nx.simple_cycles(DG))
    # sys.stderr.write(f" removed {len(cycles)} cycles\n")    #DEB
    
    # tmpnb=0                                         #DEB
    G=nx.Graph(DG)
    for nodes in cycles:
        first_node_in_cycle = nodes[0]              # get the first node, the other ones in the cycle are in the same CC
        # remove the whole CC:
        CC_with_cycle = nx.node_connected_component(G, first_node_in_cycle)
        for node_id in CC_with_cycle:
            if node_id in DG.nodes():
                # tmpnb+=1                            #DEB
                DG.remove_node(node_id)
    # sys.stderr.write(f" removed {tmpnb} nodes\n")   #DEB 
Example #2
Source File: proofreading.py    From ffn with Apache License 2.0 5 votes vote down vote up
def add_ccs(self):
    curr = set(self.viewer.state.layers['seg'].segments)
    for sid in self.viewer.state.layers['seg'].segments:
      if sid in self.graph:
        curr |= set(nx.node_connected_component(self.graph, sid))

    self.update_segments(curr) 
Example #3
Source File: utils.py    From graph-generation with MIT License 5 votes vote down vote up
def pick_connected_component(G):
    node_list = nx.node_connected_component(G,0)
    return G.subgraph(node_list) 
Example #4
Source File: utils.py    From GraphRNN with MIT License 5 votes vote down vote up
def pick_connected_component(G):
    node_list = nx.node_connected_component(G,0)
    return G.subgraph(node_list) 
Example #5
Source File: tsp_graph.py    From swarmlib with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_connected_nodes(self, node):
        """Get the connected nodes of the given node"""
        with self.__lock:
            return nx.node_connected_component(self.networkx_graph, node) 
Example #6
Source File: util.py    From multisensory with Apache License 2.0 5 votes vote down vote up
def remove_cc(graph, u):
  graph.remove_nodes_from(nx.node_connected_component(graph, u)) 
Example #7
Source File: test_connected.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_node_connected_components(self):
        ncc = nx.node_connected_component
        G = self.grid
        C = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
        assert_equal(ncc(G, 1), C) 
Example #8
Source File: test_connected.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_connected_raise(self):
        assert_raises(NetworkXNotImplemented, nx.connected_components, self.DG)
        assert_raises(NetworkXNotImplemented, nx.number_connected_components, self.DG)
        assert_raises(NetworkXNotImplemented, nx.connected_component_subgraphs, self.DG)
        assert_raises(NetworkXNotImplemented, nx.node_connected_component, self.DG,1)
        assert_raises(NetworkXNotImplemented, nx.is_connected, self.DG)
        assert_raises(nx.NetworkXPointlessConcept, nx.is_connected, nx.Graph()) 
Example #9
Source File: test_connected.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_node_connected_components(self):
        ncc = nx.node_connected_component
        G = self.grid
        C = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
        assert_equal(ncc(G, 1), C)

    # deprecated 
Example #10
Source File: test_connected.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_connected_raise(self):
        assert_raises(NetworkXNotImplemented, nx.connected_components, self.DG)
        assert_raises(NetworkXNotImplemented, nx.number_connected_components, self.DG)
        assert_raises(NetworkXNotImplemented, nx.node_connected_component, self.DG, 1)
        assert_raises(NetworkXNotImplemented, nx.is_connected, self.DG)
        assert_raises(nx.NetworkXPointlessConcept, nx.is_connected, nx.Graph())
        # deprecated
        assert_raises(NetworkXNotImplemented, nx.connected_component_subgraphs, self.DG) 
Example #11
Source File: test_connected.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_node_connected_components(self):
        ncc = nx.node_connected_component
        G = self.grid
        C = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
        assert_equal(ncc(G, 1), C) 
Example #12
Source File: test_connected.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_connected_raise(self):
        assert_raises(NetworkXNotImplemented, nx.connected_components, self.DG)
        assert_raises(NetworkXNotImplemented, nx.number_connected_components, self.DG)
        assert_raises(NetworkXNotImplemented, nx.connected_component_subgraphs, self.DG)
        assert_raises(NetworkXNotImplemented, nx.node_connected_component, self.DG,1)
        assert_raises(NetworkXNotImplemented, nx.is_connected, self.DG)
        assert_raises(nx.NetworkXPointlessConcept, nx.is_connected, nx.Graph()) 
Example #13
Source File: composers.py    From dwave-hybrid with Apache License 2.0 4 votes vote down vote up
def next(self, states):
        """ICM between two first samples in the first two input states."""

        if len(states) > 2:
            raise ValueError("exactly two input states required")

        inp1, inp2 = states
        bqm = inp1.problem

        ss1 = inp1.samples.change_vartype(dimod.BINARY, inplace=False)
        ss2 = inp2.samples.change_vartype(dimod.BINARY, inplace=False)

        # sanity check: we operate on the same set of variables
        if ss1.variables ^ ss2.variables:
            raise ValueError("input samples not over the same set of variables")

        # reorder variables, if necessary
        # (use sequence comparison, not set)
        variables = list(ss1.variables)
        if ss2.variables != variables:
            reorder = [ss2.variables.index(v) for v in variables]
            record = ss2.record[:, reorder]
            ss2 = dimod.SampleSet(record, variables, ss2.info, ss2.vartype)

        # samples' symmetric difference (XOR)
        # (form clusters of input variables with opposite values)
        sample1 = ss1.record.sample[0]
        sample2 = ss2.record.sample[0]
        symdiff = sample1 ^ sample2

        # for cluster detection we'll use a reduced problem graph
        graph = bqm.to_networkx_graph()
        # note: instead of numpy mask indexing of `notcluster`, we enumerate
        # non-cluster variables manually to avoid conversion of potentially
        # unhashable variable names to numpy types
        notcluster = [v for v, d in zip(variables, symdiff) if d == 0]
        graph.remove_nodes_from(notcluster)

        # pick a random variable that belongs to a cluster, then select the cluster
        node = self.random.choice(list(graph.nodes))
        cluster = nx.node_connected_component(graph, node)

        # flip variables from `cluster` in both input samples
        flipper = np.array([1 if v in cluster else 0 for v in variables])
        ss1.record.sample[0] ^= flipper
        ss2.record.sample[0] ^= flipper

        # change vartype back to input's type
        ss1.change_vartype(inp1.samples.vartype)
        ss2.change_vartype(inp2.samples.vartype)

        # update sampleset's energies
        ss1.record.energy = bqm.energies(ss1)
        ss2.record.energy = bqm.energies(ss2)

        return States(inp1.updated(samples=ss1), inp2.updated(samples=ss2))