[docs]@not_implemented_for("multigraph")defNOBE_SH(G,K,topk):"""detect SH spanners via NOBE[1]. Parameters ---------- G : easygraph.Graph An unweighted and undirected graph. K : int Embedding dimension k topk : int top - k structural hole spanners Returns ------- SHS : list The top-k structural hole spanners. Examples -------- >>> NOBE_SH(G,K=8,topk=5) References ---------- .. [1] https://www.researchgate.net/publication/325004496_On_Spectral_Graph_Embedding_A_Non-Backtracking_Perspective_and_Graph_Approximation """fromsklearn.clusterimportKMeansY=eg.graph_embedding.NOBE(G,K)dict={}a=0foriinG.nodes:dict[i]=aa+=1ifisinstance(Y[0,0],complex):Y=abs(Y)kmeans=KMeans(n_clusters=K,random_state=0).fit(Y)com={}cluster={}foriindict:com[i]=kmeans.labels_[dict[i]]foriincom:ifcom[i]incluster:cluster[com[i]].append(i)else:cluster[com[i]]=[]cluster[com[i]].append(i)vector={}foriindict:vector[i]=Y[dict[i]]rds=RDS(com,cluster,vector,K)rds_sort=sorted(rds.items(),key=lambdad:d[1],reverse=True)SHS=list()a=0foriinrds_sort:SHS.append(i[0])a+=1ifa==topk:breakreturnSHS
[docs]@not_implemented_for("multigraph")defNOBE_GA_SH(G,K,topk):"""detect SH spanners via NOBE-GA[1]. Parameters ---------- G : easygraph.Graph An unweighted and undirected graph. K : int Embedding dimension k topk : int top - k structural hole spanners Returns ------- SHS : list The top-k structural hole spanners. Examples -------- >>> NOBE_GA_SH(G,K=8,topk=5) References ---------- .. [1] https://www.researchgate.net/publication/325004496_On_Spectral_Graph_Embedding_A_Non-Backtracking_Perspective_and_Graph_Approximation """fromsklearn.clusterimportKMeansY=eg.NOBE_GA(G,K)ifisinstance(Y[0,0],complex):Y=abs(Y)kmeans=KMeans(n_clusters=K,random_state=0).fit(Y)com={}cluster={}a=0foriinG.nodes:com[i]=kmeans.labels_[a]a+=1foriincom:ifcom[i]incluster:cluster[com[i]].append(i)else:cluster[com[i]]=[]cluster[com[i]].append(i)vector={}a=0foriinG.nodes:vector[i]=Y[a]a+=1rds=RDS(com,cluster,vector,K)rds_sort=sorted(rds.items(),key=lambdad:d[1],reverse=True)SHS=list()a=0foriinrds_sort:SHS.append(i[0])a+=1ifa==topk:breakreturnSHS