Python scipy.sparse.csgraph.connected_components() Examples

The following are 30 code examples of scipy.sparse.csgraph.connected_components(). 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 scipy.sparse.csgraph , or try the search function .
Example #1
Source File: CLUB.py    From BanditLib with MIT License 6 votes vote down vote up
def __init__(self, arg_dict):
		BaseAlg.__init__(self, arg_dict)
		self.time = 0
		#N_LinUCBAlgorithm.__init__(dimension = dimension, alpha=alpha,lambda_ = lambda_,n=n)
		self.users = []
		#algorithm have n users, each user has a user structure
		for i in range(self.n):
			self.users.append(CLUBUserStruct(self.dimension,self.lambda_, i)) 
		if (self.cluster_init=="Erdos-Renyi"):
			p = 3*math.log(self.n)/self.n
			self.Graph = np.random.choice([0, 1], size=(self.n,self.n), p=[1-p, p])
			self.clusters = []
			g = csr_matrix(self.Graph)
			N_components, components = connected_components(g)
		else:
			self.Graph = np.ones([self.n,self.n]) 
			self.clusters = []
			g = csr_matrix(self.Graph)
			N_components, components = connected_components(g) 
Example #2
Source File: classes.py    From dislib with Apache License 2.0 6 votes vote down vote up
def _compute_cp_labels(core_points, *in_cp_neighs):
    core_ids = np.cumsum(core_points) - 1
    n_core_pts = np.count_nonzero(core_points)
    adj_matrix = lil_matrix((n_core_pts, n_core_pts))

    # Build adjacency matrix of core points
    in_cp_neighs_iter = chain(*in_cp_neighs)
    core_idx = 0
    for idx, neighbours in zip(core_points.nonzero()[0], in_cp_neighs_iter):
        neighbours = core_ids[neighbours[core_points[neighbours]]]
        adj_matrix.rows[core_idx] = neighbours
        adj_matrix.data[core_idx] = [1] * len(neighbours)
        core_idx += 1

    n_clusters, core_labels = connected_components(adj_matrix, directed=False)
    labels = np.full(core_points.shape, -1)
    labels[core_points] = core_labels
    return labels 
Example #3
Source File: __init__.py    From cmm with GNU General Public License v2.0 6 votes vote down vote up
def check_mesh(verts, tris, filename=None):
    ij = np.r_[np.c_[tris[:,0], tris[:,1]], 
               np.c_[tris[:,0], tris[:,2]], 
               np.c_[tris[:,1], tris[:,2]]]
    G = sparse.csr_matrix((np.ones(len(ij)), ij.T), shape=(verts.shape[0], verts.shape[0]))
    n_components, labels = csgraph.connected_components(G, directed=False)
    if n_components > 1:
        size_components = np.bincount(labels)
        if len(size_components) > 1:
            raise ValueError, "found %d connected components in the mesh (%s)" % (n_components, filename)
        keep_vert = labels == size_components.argmax()
    else:
        keep_vert = np.ones(verts.shape[0], np.bool)
    verts = verts[keep_vert, :]
    tris = filter_reindex(keep_vert, tris[keep_vert[tris].all(axis=1)])
    return verts, tris 
Example #4
Source File: spectral_embedding_.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def _graph_is_connected(graph):
    """ Return whether the graph is connected (True) or Not (False)

    Parameters
    ----------
    graph : array-like or sparse matrix, shape: (n_samples, n_samples)
        adjacency matrix of the graph, non-zero weight means an edge
        between the nodes

    Returns
    -------
    is_connected : bool
        True means the graph is fully connected and False means not
    """
    if sparse.isspmatrix(graph):
        # sparse graph, find all the connected components
        n_connected_components, _ = connected_components(graph)
        return n_connected_components == 1
    else:
        # dense graph, find all connected components start from node 0
        return _graph_connected_component(graph, 0).sum() == graph.shape[0] 
Example #5
Source File: spectral_embedding_.py    From Mastering-Elasticsearch-7.0 with MIT License 6 votes vote down vote up
def _graph_is_connected(graph):
    """ Return whether the graph is connected (True) or Not (False)

    Parameters
    ----------
    graph : array-like or sparse matrix, shape: (n_samples, n_samples)
        adjacency matrix of the graph, non-zero weight means an edge
        between the nodes

    Returns
    -------
    is_connected : bool
        True means the graph is fully connected and False means not
    """
    if sparse.isspmatrix(graph):
        # sparse graph, find all the connected components
        n_connected_components, _ = connected_components(graph)
        return n_connected_components == 1
    else:
        # dense graph, find all connected components start from node 0
        return _graph_connected_component(graph, 0).sum() == graph.shape[0] 
Example #6
Source File: utils.py    From gnn-meta-attack with MIT License 6 votes vote down vote up
def largest_connected_components(adj, n_components=1):
    """Select the largest connected components in the graph.
    Parameters
    ----------
    adj : gust.SparseGraph
        Input graph.
    n_components : int, default 1
        Number of largest connected components to keep.
    Returns
    -------
    sparse_graph : gust.SparseGraph
        Subgraph of the input graph where only the nodes in largest n_components are kept.
    """
    _, component_indices = connected_components(adj)
    component_sizes = np.bincount(component_indices)
    components_to_keep = np.argsort(component_sizes)[::-1][:n_components]  # reverse order to sort descending
    nodes_to_keep = [
        idx for (idx, component) in enumerate(component_indices) if component in components_to_keep


    ]
    print("Selecting {0} largest connected components".format(n_components))
    return nodes_to_keep 
Example #7
Source File: skater.py    From region with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def make_cut(self, in_node, out_node, score, MSF=None):
        """
        make a cut on the MSF inplace, provided the in_node, out_node, MSF, and score. 
        in_node: int, ID of the source node for the edge to be cut
        out_node: int, ID of the destination node for the edge to be cut
        score: float, the value of the score being cut. if the score is infinite, the cut is not made. 
        MSF: the spanning forest to use when making the cut. If not provided,
             uses the defualt tree in self.minimum_spanning_forest_
        """
        if MSF is None:
            MSF = self.minimum_spanning_forest_
        if np.isfinite(score):
            MSF[in_node, out_node] = 0
            MSF.eliminate_zeros()
            return (MSF, *cg.connected_components(MSF, directed=False))
        raise OptimizeWarning('Score of the ({},{}) cut is inf, the quorum is likely not met!') 
Example #8
Source File: CLUB.py    From BanditLib with MIT License 6 votes vote down vote up
def updateGraphClusters(self,userID, binaryRatio):
		n = len(self.users)
		for j in range(n):
			ratio = float(np.linalg.norm(self.users[userID].UserTheta - self.users[j].UserTheta,2))/float(self.users[userID].CBPrime + self.users[j].CBPrime)
			#print float(np.linalg.norm(self.users[userID].UserTheta - self.users[j].UserTheta,2)),'R', ratio
			if ratio > 1:
				ratio = 0
			elif binaryRatio == 'True':
				ratio = 1
			elif binaryRatio == 'False':
				ratio = 1.0/math.exp(ratio)
			#print 'ratio',ratio
			self.Graph[userID][j] = ratio
			self.Graph[j][userID] = self.Graph[userID][j]
		N_components, component_list = connected_components(csr_matrix(self.Graph))
		#print 'N_components:',N_components
		self.clusters = component_list
		return N_components 
Example #9
Source File: matching.py    From peakonly with MIT License 6 votes vote down vote up
def rt_grouping(mzregions):
    """
    A function that groups roi inside mzregions.
    :param mzregions: a list of mzRegion objects
    :return: a list of defaultdicts, where the key is the name of file and value is a list of ROIs
    """
    components = []
    for region in mzregions:
        region = np.array([(name, roi) for name, s in region.rois.items() for roi in s])
        n = len(region)
        graph = np.zeros((n, n), dtype=np.uint8)
        for i in range(n - 1):
            for j in range(i + 1, n):
                graph[i, j] = roi_intersected(region[i][1], region[j][1])
        n_components, labels = connected_components(graph, directed=False)

        for k in range(n_components):
            rois = region[labels == k]
            component = defaultdict(list)
            for roi in rois:
                component[roi[0]].append(roi[1])
            components.append(component)
    return components 
Example #10
Source File: rcc.py    From pyrcc with MIT License 6 votes vote down vote up
def compute_assignment(self, epsilon):
        """
        Assigns points to clusters based on their representative. Two points are part of the same cluster if their
        representative are close enough (their squared euclidean distance is < delta)
        """
        diff = np.sum((self.U[self.i, :] - self.U[self.j, :])**2, axis=1)

        # computing connected components.
        is_conn = np.sqrt(diff) <= self.clustering_threshold*epsilon

        G = scipy.sparse.coo_matrix((np.ones((2*np.sum(is_conn),)),
                                     (np.concatenate([self.i[is_conn], self.j[is_conn]], axis=0),
                                      np.concatenate([self.j[is_conn], self.i[is_conn]], axis=0))),
                                    shape=[self.n_samples, self.n_samples])

        num_components, labels = connected_components(G, directed=False)

        return labels, num_components 
Example #11
Source File: DCCComputation.py    From DCC with MIT License 6 votes vote down vote up
def computeObj(U, pairs, _delta, gtlabels, numeval):
    """ This is similar to computeObj function in Matlab """
    numsamples = len(U)
    diff = np.linalg.norm(U[pairs[:, 0].astype(int)] - U[pairs[:, 1].astype(int)], axis=1)**2

    # computing clustering measures
    index1 = np.sqrt(diff) < _delta
    index = np.where(index1)
    adjacency = csr_matrix((np.ones(len(index[0])), (pairs[index[0], 0].astype(int), pairs[index[0], 1].astype(int))),
                           shape=(numsamples, numsamples))
    adjacency = adjacency + adjacency.transpose()
    n_components, labels = connected_components(adjacency, directed=False)

    index2 = labels[pairs[:, 0].astype(int)] == labels[pairs[:, 1].astype(int)]

    ari, ami, nmi, acc = benchmarking(gtlabels[:numeval], labels[:numeval])

    return index2, ari, ami, nmi, acc, n_components, labels 
Example #12
Source File: spectral_embedding_.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def _graph_is_connected(graph):
    """ Return whether the graph is connected (True) or Not (False)

    Parameters
    ----------
    graph : array-like or sparse matrix, shape: (n_samples, n_samples)
        adjacency matrix of the graph, non-zero weight means an edge
        between the nodes

    Returns
    -------
    is_connected : bool
        True means the graph is fully connected and False means not
    """
    if sparse.isspmatrix(graph):
        # sparse graph, find all the connected components
        n_connected_components, _ = connected_components(graph)
        return n_connected_components == 1
    else:
        # dense graph, find all connected components start from node 0
        return _graph_connected_component(graph, 0).sum() == graph.shape[0] 
Example #13
Source File: components.py    From PyPSA with GNU General Public License v3.0 5 votes vote down vote up
def determine_network_topology(self):
        """
        Build sub_networks from topology.
        """

        adjacency_matrix = self.adjacency_matrix(self.passive_branch_components)
        n_components, labels = csgraph.connected_components(adjacency_matrix, directed=False)

        # remove all old sub_networks
        for sub_network in self.sub_networks.index:
            obj = self.sub_networks.at[sub_network,"obj"]
            self.remove("SubNetwork", sub_network)
            del obj

        for i in np.arange(n_components):
            # index of first bus
            buses_i = (labels == i).nonzero()[0]
            carrier = self.buses.carrier.iat[buses_i[0]]

            if carrier not in ["AC","DC"] and len(buses_i) > 1:
                logger.warning("Warning, sub network {} is not electric but contains multiple buses\n"
                                "and branches. Passive flows are not allowed for non-electric networks!".format(i))

            if (self.buses.carrier.iloc[buses_i] != carrier).any():
                logger.warning("Warning, sub network {} contains buses with mixed carriers! Value counts:\n{}".format(i,
                                self.buses.carrier.iloc[buses_i].value_counts()))

            self.add("SubNetwork", i, carrier=carrier)

        #add objects
        self.sub_networks["obj"] = [SubNetwork(self, name) for name in self.sub_networks.index]

        self.buses.loc[:, "sub_network"] = labels.astype(str)

        for c in self.iterate_components(self.passive_branch_components):
            c.df["sub_network"] = c.df.bus0.map(self.buses["sub_network"])

        for sub in self.sub_networks.obj:
            find_cycles(sub)
            sub.find_bus_controls() 
Example #14
Source File: coupling.py    From qiskit-terra with Apache License 2.0 5 votes vote down vote up
def reduce(self, mapping):
        """Returns a reduced coupling map that
        corresponds to the subgraph of qubits
        selected in the mapping.

        Args:
            mapping (list): A mapping of reduced qubits to device
                            qubits.

        Returns:
            CouplingMap: A reduced coupling_map for the selected qubits.

        Raises:
            CouplingError: Reduced coupling map must be connected.
        """
        reduced_qubits = len(mapping)
        inv_map = [None] * (max(mapping) + 1)
        for idx, val in enumerate(mapping):
            inv_map[val] = idx

        reduced_cmap = []

        for edge in self.get_edges():
            if edge[0] in mapping and edge[1] in mapping:
                reduced_cmap.append([inv_map[edge[0]], inv_map[edge[1]]])

        # Verify coupling_map is connected
        rows = np.array([edge[0] for edge in reduced_cmap], dtype=int)
        cols = np.array([edge[1] for edge in reduced_cmap], dtype=int)
        data = np.ones_like(rows)

        mat = sp.coo_matrix((data, (rows, cols)),
                            shape=(reduced_qubits, reduced_qubits)).tocsr()

        if cs.connected_components(mat)[0] != 1:
            raise CouplingError('coupling_map must be connected.')

        return CouplingMap(reduced_cmap) 
Example #15
Source File: clean_network.py    From panaroo with MIT License 5 votes vote down vote up
def single_linkage(G, distances_bwtn_centroids, centroid_to_index, neighbours):
    index = []
    neigh_array = []
    for neigh in neighbours:
        for sid in G.nodes[neigh]['centroid']:
            index.append(centroid_to_index[sid])
            neigh_array.append(neigh)
    index = np.array(index, dtype=int)
    neigh_array = np.array(neigh_array)

    n_components, labels = connected_components(
        csgraph=distances_bwtn_centroids[index][:, index],
        directed=False,
        return_labels=True)
    # labels = labels[index]
    for neigh in neighbours:
        l = list(set(labels[neigh_array == neigh]))
        if len(l) > 1:
            for i in l[1:]:
                labels[labels == i] = l[0]

    clusters = [
        del_dups(list(neigh_array[labels == i])) for i in np.unique(labels)
    ]

    return (clusters)


# @profile 
Example #16
Source File: detection.py    From ramp-workflow with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_connected_components(nodes, matrix):
    from scipy.sparse.csgraph import connected_components
    ncon, labels = connected_components(matrix, directed=False)
    a_nodes = np.empty(len(nodes), dtype=object)
    a_nodes[:] = nodes
    return [a_nodes[labels == i] for i in range(ncon)] 
Example #17
Source File: wordGraph.py    From TextDetector with GNU General Public License v3.0 5 votes vote down vote up
def relationMat2wordbb(bblist1, relationMat):
        ''' relationMat2wordbb(bblist1, relationMat)
    
        Convert character bounding boxes bblist1 into word bounding boxes wordbb,
        using sparse graph
        Input:
                bblist1, character bounding boxes, each row represents a char, 
                        with x, y, w, h format
                relationMat, sparse graph get from bblist2relationMat,
                        each element indicate True or False
                        that corresponding char belong to the same word
        Output:
                wordbb, word bounding boxes, each row represents a word, with x, y, w, h format.
        '''
        ncc, labelcc = connected_components(relationMat)
        cclabel = numpy.unique(labelcc)
        wordbb = []
        for i in cclabel:
                wlist = bblist1[labelcc == i, ...]
                wlist[:, 2] = wlist[:, 0] + wlist[:, 2]
                wlist[:, 3] = wlist[:, 1] + wlist[:, 3]
                x = min(wlist[:, 0])
                y = min(wlist[:, 1])
                w = max(wlist[:, 2]) - x
                h = max(wlist[:, 3]) - y
                wordbb.append([x, y, w, h])
        return wordbb 
Example #18
Source File: helpers.py    From cgpm with Apache License 2.0 5 votes vote down vote up
def retrieve_weakly_connected_components(cgpms):
    v_to_c = retrieve_variable_to_cgpm(cgpms)
    adjacency = retrieve_adjacency_matrix(cgpms, v_to_c)
    n_components, labels = connected_components(
        adjacency, directed=True, connection='weak', return_labels=True)
    return labels 
Example #19
Source File: markovChain.py    From discreteMarkovChain with MIT License 5 votes vote down vote up
def assertSingleClass(self,P):
        """ 
        Check whether the rate/probability matrix consists of a single connected class.
        If this is not the case, the steady state distribution is not well defined.
        """
        components, _ = csgraph.connected_components(P, directed=True, connection='weak')   
        assert components==1, "The Markov chain has %r communicating classes. Make sure there is a single communicating class." %components 
Example #20
Source File: markovChain.py    From discreteMarkovChain with MIT License 5 votes vote down vote up
def absorbTime(self):
        P = self.getTransitionMatrix(probabilities=True)
        components,labels = csgraph.connected_components(P, directed=True, connection='strong',return_labels=True)
        if components == 1:
            print("no absorbing states")
            return
            
        transientStates = np.ones(P.shape[0],dtype=bool)

        for component in range(components):
            indices = np.where(labels==component)[0]
            n = len(indices)
            if n==1:
                probSum = P[indices,indices].sum()
            else:            
                probSum = P[np.ix_(indices,indices)].sum()
            if np.isclose(probSum,n):
                transientStates[indices] = False

        indices = np.where(transientStates)[0]
        n = len(indices)
        if n==1:
            Q = P[indices,indices]
        else:
            Q = P[np.ix_(indices,indices)]
        #N will be dense  
        N = inv(eye(n)-Q).A
        N2 = N*(2*N[np.arange(n),np.arange(n)]-np.eye(n))-np.power(N,2)
        t = np.zeros(P.shape[0])
        t[indices] = np.sum(N,axis=1)
        for index in indices:
            print( self.mapping[index],t[index] ) 
Example #21
Source File: dcs.py    From broca with MIT License 5 votes vote down vote up
def _lexical_chains(self, doc, term_concept_map):
        """
        Builds lexical chains, as an adjacency matrix,
        using a disambiguated term-concept map.
        """
        concepts = list({c for c in term_concept_map.values()})

        # Build an adjacency matrix for the graph
        # Using the encoding:
        # 1 = identity/synonymy, 2 = hypernymy/hyponymy, 3 = meronymy, 0 = no edge
        n_cons = len(concepts)
        adj_mat = np.zeros((n_cons, n_cons))

        for i, c in enumerate(concepts):
            # TO DO can only do i >= j since the graph is undirected
            for j, c_ in enumerate(concepts):
                edge = 0
                if c == c_:
                    edge = 1
                # TO DO when should simulate root be True?
                elif c_ in c._shortest_hypernym_paths(simulate_root=False).keys():
                    edge = 2
                elif c in c_._shortest_hypernym_paths(simulate_root=False).keys():
                    edge = 2
                elif c_ in c.member_meronyms() + c.part_meronyms() + c.substance_meronyms():
                    edge = 3
                elif c in c_.member_meronyms() + c_.part_meronyms() + c_.substance_meronyms():
                    edge = 3

                adj_mat[i,j] = edge

        # Group connected concepts by labels
        concept_labels = connected_components(adj_mat, directed=False)[1]
        lexical_chains = [([], []) for i in range(max(concept_labels) + 1)]
        for i, concept in enumerate(concepts):
            label = concept_labels[i]
            lexical_chains[label][0].append(concept)
            lexical_chains[label][1].append(i)

        # Return the lexical chains as (concept list, adjacency sub-matrix) tuples
        return [(chain, adj_mat[indices][:,indices]) for chain, indices in lexical_chains] 
Example #22
Source File: csgraph_utils.py    From region with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def is_connected(adj):
    """
    Parameters
    ----------
    adj : :class:`scipy.sparse.csr_matrix`
        Adjacency matrix.

    Returns
    -------
    connected : `bool`
        `True` if graph defined by adjecency matrix `adj` is connected.
        `False` otherwise.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csr_matrix
    >>> connected = csr_matrix(np.array([[0, 1],
    ...                                  [1, 0]]))
    >>> is_connected(connected)
    True
    >>> disconnected = csr_matrix(np.array([[0, 0],
    ...                                     [0, 0]]))
    >>> is_connected(disconnected)
    False
    """
    n_connected_components = csg.connected_components(adj, directed=False,
                                                      return_labels=False)
    return True if n_connected_components == 1 else False 
Example #23
Source File: transform_manager.py    From pytransform3d with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def connected_components(self):
        """Get number of connected components.

        If the number is larger than 1 there will be frames without
        connections.

        Returns
        -------
        n_connected_components : int
            Number of connected components.
        """
        return csgraph.connected_components(
            self.connections, directed=False, return_labels=False) 
Example #24
Source File: test_image.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_grid_to_graph():
    # Checking that the function works with graphs containing no edges
    size = 2
    roi_size = 1
    # Generating two convex parts with one vertex
    # Thus, edges will be empty in _to_graph
    mask = np.zeros((size, size), dtype=np.bool)
    mask[0:roi_size, 0:roi_size] = True
    mask[-roi_size:, -roi_size:] = True
    mask = mask.reshape(size ** 2)
    A = grid_to_graph(n_x=size, n_y=size, mask=mask, return_as=np.ndarray)
    assert_true(connected_components(A)[0] == 2)

    # Checking that the function works whatever the type of mask is
    mask = np.ones((size, size), dtype=np.int16)
    A = grid_to_graph(n_x=size, n_y=size, n_z=size, mask=mask)
    assert_true(connected_components(A)[0] == 1)

    # Checking dtype of the graph
    mask = np.ones((size, size))
    A = grid_to_graph(n_x=size, n_y=size, n_z=size, mask=mask, dtype=np.bool)
    assert_true(A.dtype == np.bool)
    A = grid_to_graph(n_x=size, n_y=size, n_z=size, mask=mask, dtype=np.int)
    assert_true(A.dtype == np.int)
    A = grid_to_graph(n_x=size, n_y=size, n_z=size, mask=mask,
                      dtype=np.float64)
    assert_true(A.dtype == np.float64) 
Example #25
Source File: test_image.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_connect_regions():
    try:
        face = sp.face(gray=True)
    except AttributeError:
        # Newer versions of scipy have face in misc
        from scipy import misc
        face = misc.face(gray=True)
    for thr in (50, 150):
        mask = face > thr
        graph = img_to_graph(face, mask)
        assert_equal(ndimage.label(mask)[1], connected_components(graph)[0]) 
Example #26
Source File: test_image.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_connect_regions_with_grid():
    try:
        face = sp.face(gray=True)
    except AttributeError:
        # Newer versions of scipy have face in misc
        from scipy import misc
        face = misc.face(gray=True)
    mask = face > 50
    graph = grid_to_graph(*face.shape, mask=mask)
    assert_equal(ndimage.label(mask)[1], connected_components(graph)[0])

    mask = face > 150
    graph = grid_to_graph(*face.shape, mask=mask, dtype=None)
    assert_equal(ndimage.label(mask)[1], connected_components(graph)[0]) 
Example #27
Source File: test_connected_components.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_ticket1876():
    # Regression test: this failed in the original implementation
    # There should be two strongly-connected components; previously gave one
    g = np.array([[0, 1, 1, 0],
                  [1, 0, 0, 1],
                  [0, 0, 0, 1],
                  [0, 0, 1, 0]])
    n_components, labels = csgraph.connected_components(g, connection='strong')

    assert_equal(n_components, 2)
    assert_equal(labels[0], labels[1])
    assert_equal(labels[2], labels[3]) 
Example #28
Source File: utils.py    From nettack with MIT License 5 votes vote down vote up
def largest_connected_components(adj, n_components=1):
    """Select the largest connected components in the graph.

    Parameters
    ----------
    sparse_graph : gust.SparseGraph
        Input graph.
    n_components : int, default 1
        Number of largest connected components to keep.

    Returns
    -------
    sparse_graph : gust.SparseGraph
        Subgraph of the input graph where only the nodes in largest n_components are kept.

    """
    _, component_indices = connected_components(adj)
    component_sizes = np.bincount(component_indices)
    components_to_keep = np.argsort(component_sizes)[::-1][:n_components]  # reverse order to sort descending
    nodes_to_keep = [
        idx for (idx, component) in enumerate(component_indices) if component in components_to_keep


    ]
    print("Selecting {0} largest connected components".format(n_components))
    return nodes_to_keep 
Example #29
Source File: test_connected_components.py    From Computable with MIT License 5 votes vote down vote up
def test_weak_connections():
    Xde = np.array([[0, 1, 0],
                    [0, 0, 0],
                    [0, 0, 0]])

    Xsp = csgraph.csgraph_from_dense(Xde, null_value=0)

    for X in Xsp, Xde:
        n_components, labels =\
            csgraph.connected_components(X, directed=True,
                                         connection='weak')

        assert_equal(n_components, 2)
        assert_array_almost_equal(labels, [0, 0, 1]) 
Example #30
Source File: test_connected_components.py    From Computable with MIT License 5 votes vote down vote up
def test_strong_connections():
    X1de = np.array([[0, 1, 0],
                     [0, 0, 0],
                     [0, 0, 0]])
    X2de = X1de + X1de.T

    X1sp = csgraph.csgraph_from_dense(X1de, null_value=0)
    X2sp = csgraph.csgraph_from_dense(X2de, null_value=0)

    for X in X1sp, X1de:
        n_components, labels =\
            csgraph.connected_components(X, directed=True,
                                         connection='strong')

        assert_equal(n_components, 3)
        labels.sort()
        assert_array_almost_equal(labels, [0, 1, 2])

    for X in X2sp, X2de:
        n_components, labels =\
            csgraph.connected_components(X, directed=True,
                                         connection='strong')

        assert_equal(n_components, 2)
        labels.sort()
        assert_array_almost_equal(labels, [0, 0, 1])