Exemple #1
0
def newSLPAwauto(G, T=100):
    egoLabelsNum = {}
    egoLabelsNum = egoLabelsNum.fromkeys(G.nodes(), 1)
    for ego in G.nodes():
        ego_minus_ego = nx.ego_graph(G, ego, 1, False)
        '利用非重叠的BGLL'
        partition = BGLL.best_partition(ego_minus_ego)
        '将partion换成community'
        community_to_nodes = defaultdict(list)
        for node, comID in partition.items():
            community_to_nodes[comID].append(node)
        '将ego加入每个社团'
        for com in community_to_nodes.values():
            com.append(ego)
        '记录ego的标签个数'
        egoLabelsNum[ego] = len(community_to_nodes)
    '再用SLPAwAuto'
    communities = SLPAwAuto(G, T, egoLabelsNum)
    return communities
Exemple #2
0
def runOrderedNodePreception(G,simThreshold,slpar=0.3):
    '''
    有访问顺序
    :param G: 输入的图
    :param simThreshold: 构建新的图的相似度
    :return: 二维数组的社团列表结构
    '''
    '1.初始化社团标号为自己,注意一个点可以属于多个社团'
    for n in G.nodes():
        G.node[n]['communities']=[n]

    '2.给节点排序'
    #按节点的度降序排序
    # visitedSequece=sorted(G.nodes(),key=lambda n:G.degree(n),reverse=True)
    #按节点核值排序
    # coreDict = nx.core_number(G)
    # print coreDict
    # visitedSequece = [t[0] for t in sorted(coreDict.items(),key=lambda d:d[1],reverse=True)]
    # print visitedSequece


    visitedSequece=computeCentrilityandSort4(G)
    #存储节点是否该访问的标志
    visiteFlag={}
    for n in G.nodes():
        visiteFlag[n]=False #所有节点初始化
    '初始化'
    allSubCommunities=[] ##所有的子社团
    H=nx.Graph() ##重新构建的子图
    subcomid_to_subcom={} ##H中节点的映射到包含的suncommunity
    # subcom_to_suncomid={}
    '3.找邻居社团,构建超图构成的子图H'
    for ego in visitedSequece:
        if(visiteFlag[ego]==False):
            ego_minus_ego=nx.ego_graph(G,ego,1,center=False) ##不包含ego节点,1跳邻居
            '访问标志'
            for n in ego_minus_ego:
                visiteFlag[n]=True
            visiteFlag[ego]=True

            '重叠LPA'
            # community_to_nodes=overlappingLPA(ego_minus_ego,ego)

            '利用非重叠的BGLL'
            partition=BGLL.best_partition(ego_minus_ego)
            '将partion换成community'
            community_to_nodes=defaultdict(list)
            for node,comID in partition.items():
                community_to_nodes[comID].append(node)
            '将ego加入每个社团'
            for com in community_to_nodes.values():
                com.append(ego)

            '相同的子社团只允许出现一次'
            for subcom in  community_to_nodes.values():
                sortedCom=tuple(sorted(subcom)) ##先给节点排个序
                if not sortedCom in allSubCommunities:
                    '还没有该社团'
                    allSubCommunities.append(sortedCom) ##加入一个社团
                    id=len(allSubCommunities)-1 ##社团编号
                    # subcom_to_suncomid[sortedCom]=id ##subcom社团到id的映射
                    subcomid_to_subcom[id]=sortedCom  ##H的id到suncom的映射
                    '创造H的节点'
                    Hnodes=H.nodes()
                    H.add_node(id)
                    '创造id与其他节点的边'
                    for n in Hnodes:
                        com=allSubCommunities[n]
                        sim=jaccardSim(sortedCom,com)
                        if sim>simThreshold:
                            '边不需要考虑方向'
                            H.add_edge(id,n,weight=sim)
                else:
                    '该社团已经有过了'
                    continue
        else:
            continue

    '3:对超图H进行社团搜索'
    hcommunities=SLPAw(H,100,slpar,True)

    '4:后处理,将超图节点还原成原来节点'
    allCommunities=[]
    for hcom in hcommunities.values():
        com=set()
        for hnode in hcom:
           subcom=subcomid_to_subcom[hnode]
           for i in subcom:
               com.add(i)
        sortedtmp=sorted(list(com))
        allCommunities.append(sortedtmp)
    # return  allCommunities

    '删掉嵌套的社团'
    # # Remove nested communities
    # nestedCommunities = set()
    # N=len(allCommunities)
    # for i in range(0,N):
    #     comm0=set(allCommunities[i])
    #     for j in range(i+1,N):
    #         comm1=set(allCommunities[j])
    #         print comm1
    #         if comm0.issubset(comm1):
    #             nestedCommunities.add(comm0)
    #         elif comm1.issubset(comm0):
    #             nestedCommunities.add(comm1)
    #
    # for comm in nestedCommunities:
    #     allCommunities.remove(list(comm))

    return allCommunities
Exemple #3
0
@file:test3.py
@time:2017/3/2313:15
"""
import communityLouvain as cd
import networkx as nx
import matplotlib.pyplot as plt
import random
from collections import defaultdict
from overlapping_modularity import compute_overlap_modularity_cxm

#better with karate_graph() as defined in networkx example.
#erdos renyi don't have true community structure
G = nx.karate_club_graph()

#first compute the best partitio
partition = cd.best_partition(G)

#drawing
size = float(len(set(partition.values())))
pos = nx.spring_layout(G)
count = 0.
for com in set(partition.values()):
    count = count + 1.
    list_nodes = [
        nodes for nodes in partition.keys() if partition[nodes] == com
    ]
    c = [random.random()] * len(list_nodes)  # random color...
    nx.draw_networkx_nodes(G,
                           pos,
                           list_nodes,
                           alpha=0.5,
Exemple #4
0
def runNodePreception(G, simThreshold, slpar=0.3):
    '''
    :param G: 输入的图
    :param simThreshold: 构建新的图的相似度
    :return: 二维数组的社团列表结构
    '''
    '1.初始化社团标号为自己,注意一个点可以属于多个社团'
    for n in G.nodes():
        G.node[n]['communities'] = [n]

    '2.找邻居社团,构建超图构成的子图H'
    allSubCommunities = []  ##所有的子社团
    H = nx.Graph()  ##重新构建的子图
    subcomid_to_subcom = {}  ##H中节点的映射到包含的suncommunity
    # subcom_to_suncomid={}
    for ego in G.nodes():
        ego_minus_ego = nx.ego_graph(G, ego, 1, center=False)  ##不包含ego节点,1跳邻居
        '利用非重叠的BGLL'
        # community_to_nodes=overlappingLPA(ego_minus_ego,ego)
        partition = BGLL.best_partition(ego_minus_ego)
        '将partion换成community'
        community_to_nodes = defaultdict(list)
        for node, comID in partition.items():
            community_to_nodes[comID].append(node)
        '将ego加入每个社团'
        for com in community_to_nodes.values():
            com.append(ego)

        '相同的子社团只允许出现一次'
        for subcom in community_to_nodes.values():
            sortedCom = tuple(sorted(subcom))  ##先给节点排个序
            if not sortedCom in allSubCommunities:
                '还没有该社团'
                allSubCommunities.append(sortedCom)  ##加入一个社团
                id = len(allSubCommunities) - 1  ##社团编号
                # subcom_to_suncomid[sortedCom]=id ##subcom社团到id的映射
                subcomid_to_subcom[id] = sortedCom  ##H的id到suncom的映射
                '创造H的节点'
                Hnodes = H.nodes()
                H.add_node(id)
                '创造id与其他节点的边'
                for n in Hnodes:
                    com = allSubCommunities[n]
                    sim = jaccardSim(sortedCom, com)
                    if sim > simThreshold:
                        '边不需要考虑方向'
                        H.add_edge(id, n, weight=sim)
            else:
                '该社团已经有过了'
                continue

    '3:对超图H进行社团搜索'
    hcommunities = SLPAw(H, 100, slpar, False)

    '4:后处理,将超图节点还原成原来节点'
    allCommunities = []
    for hcom in hcommunities.values():
        com = set()
        for hnode in hcom:
            subcom = subcomid_to_subcom[hnode]
            for i in subcom:
                com.add(i)
        sortedtmp = sorted(list(com))
        allCommunities.append(sortedtmp)
    return allCommunities