CommunityDetection.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:visa_free 作者: BBischof 项目源码 文件源码
def communitySplits(self, graph, weight=None):
        """
        Compute the splits for the formation of communities. 

        Parameters
        ----------
        graph -  A networkx graph of digraph.
        weight (string) - If None, all edge weights are considered equal. 
            Otherwise holds the name of the edge attribute used as weight


        Returns
        -------
        The graph with weak edges removed. 


        Usage
        -----
        >>> G = nx.path_graph(10)
        >>> out = GirvanNewman(G)
        >>> comm = out.communities(G, weight=None)
        >>> for x in comm:
                print x
        """

        nConnComp = nx.number_connected_components(graph)
        nComm = nConnComp

        while (nComm <= nConnComp):
            betweenness = nx.edge_betweenness_centrality(graph, weight=weight)
            if (len(betweenness.values()) != 0 ):
                max_betweenness = max(betweenness.values())
            else:
                break   
            for u,v in betweenness.iteritems():
                if float(v) == max_betweenness:
                    # print u,v
                    graph.remove_edge(u[0], u[1])
            nComm = nx.number_connected_components(graph)           
        return graph
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号