def test_k_crust(self): # k = 0 k_crust_subgraph = nx.k_crust(self.H, k=2) assert sorted(k_crust_subgraph.nodes()) == sorted(self.H.nodes()) # k=1 k_crust_subgraph = nx.k_crust(self.H, k=1) assert sorted(k_crust_subgraph.nodes()) == [0, 1, 3] # k=2 k_crust_subgraph = nx.k_crust(self.H, k=0) assert sorted(k_crust_subgraph.nodes()) == [0]
def test_k_crust(self): # k = 0 k_crust_subgraph = nx.k_crust(self.H, k=2) assert_equal(sorted(k_crust_subgraph.nodes()), sorted(self.H.nodes())) # k=1 k_crust_subgraph = nx.k_crust(self.H, k=1) assert_equal(sorted(k_crust_subgraph.nodes()), [0, 1, 3]) # k=2 k_crust_subgraph = nx.k_crust(self.H, k=0) assert_equal(sorted(k_crust_subgraph.nodes()), [0])
def find_k_cores(self, max_k): current_graph = self.G if self.verbose: print 'K-CORES' for i in range(max_k,0,-1): core_k = nx.k_core(current_graph, i) if len(core_k) > 0: self.k_cores.append(core_k.nodes()) current_graph = nx.k_crust(current_graph, i) if self.verbose: print 'Found %s k-cores' %(len(self.k_cores)) return len(self.k_cores)
def test_main_crust(self): main_crust_subgraph = nx.k_crust(self.H) assert_equal(sorted(main_crust_subgraph.nodes()), [0, 1, 3])
pos = graphviz_layout(F) nx.draw_networkx(F, pos, **dzcnapy.attrs) dzcnapy.set_extent(pos, plt) dzcnapy.plot("abcNwtwork") G = nx.Graph( (("Alpha", "Bravo"), ("Bravo", "Charlie"), ("Charlie", "Delta"), ("Charlie", "Echo"), ("Charlie", "Foxtrot"), ("Delta", "Echo"), ("Delta", "Foxtrot"), ("Echo", "Foxtrot"), ("Echo", "Golf"), ("Echo", "Hotel"), ("Foxtrot", "Golf"), ("Foxtrot", "Hotel"), ("Delta", "Hotel"), ("Golf", "Hotel"), ("Delta", "India"), ("Charlie", "India"), ("India", "Juliet"), ("Golf", "Kilo"), ("Alpha", "Kilo"), ("Bravo", "Lima"))) pos = graphviz_layout(G) core = nx.k_core(G) crust = nx.k_crust(G) corona3 = nx.k_corona(G, k=3).nodes() nx.draw_networkx(G, pos, nodelist=core, **dzcnapy.attrs) nx.draw_networkx_edges(G, pos, core.edges(), **dzcnapy.tick_attrs) nx.draw_networkx_edges(G, pos, crust.edges(), **dzcnapy.tick_attrs) nx.draw_networkx_nodes(G, pos, crust, node_shape='v', **dzcnapy.attrs) nx.draw_networkx_nodes(G, pos, corona3, node_shape='s', **dzcnapy.attrs) dzcnapy.set_extent(pos, plt) dzcnapy.plot("CoresAndCoronas") # Generate a 5-clique G = nx.complete_graph(5, nx.Graph()) nx.relabel_nodes(G, dict(enumerate(("Alpha", "Bravo", "Charlie", "Delta", "Echo"))), copy=False) # Attach a pigtail to it
def main(): #Reading in the graph G = nx.read_edgelist("GameOfThrones.txt", delimiter=",", data=(('strength', int), ('season', int))) #Displaying the graph pos = nx.spring_layout(G) plt.figure(figsize=(10, 10)) nx.draw_networkx(G, pos=pos, with_labels=True) plt.axis('off') plt.show() #Outputting info print(nx.info(G)) #Outputting highest degree node name & degree nodes = list(G.degree()) print("\nHighest degree node is:", nodes[0][0], "with degree", nodes[0][1]) #Outputting num of connected components print("\nNumber of connected components:", len(list(nx.connected_components(G)))) #Outputting the num of maximal cliques print("\nNumber of maximal cliques:", len(list(nx.find_cliques(G)))) #Outputting num of nodes in main core and k val core_num = list(nx.k_core(G).nodes()) for x in range(len(core_num)): if core_num == list(nx.k_core(G, k=x)): k_val = x print("\nNumber of nodes in main core:", len(core_num), "with k val:", k_val) #Outputting num of nodes in main crust print("\nNumber of nodes in main crust:", len(list(nx.k_crust(G).nodes()))) #Output num of nodes in the k corona print("\nNumber of nodes in k corona:", len(list(nx.k_corona(G, k=k_val).nodes()))) #Output num of nodes in main shell print("\nNumber of nodes in main shell:", len(list(nx.k_shell(G).nodes()))) #Display graph, main core - red, main crust - blue color_map = [] for node in G: if node in list(nx.k_core(G).nodes()): color_map.append('red') elif node in list(nx.k_crust(G).nodes()): color_map.append('blue') nx.draw_networkx(G, pos=pos, node_color=color_map, with_labels=False) plt.axis('off') plt.show() #Louvain Method, output num of communities, size or largest commnunity, size of smallest community, modularity of partition partition = community.best_partition(G) com_num = partition[max(partition, key=partition.get)] print("\nLouvain Method:") print("Number of communities:", com_num + 1) count = [] comm_list = list(partition.values()) for x in range(com_num): count.append(comm_list.count(x)) print("The largest community has count:", max(count)) print("The smallest community has count:", min(count)) print("The modularity of this partitioning:", community.modularity(partition, G)) #Display graph using Louvain, with nodes in diff colors per partition cmap = cm.get_cmap('viridis', max(partition.values()) + 1) nx.draw_networkx_nodes(G, pos, partition.keys(), node_size=40, cmap=cmap, node_color=list(partition.values())) nx.draw_networkx_edges(G, pos, alpha=0.5) plt.axis('off') plt.show() #Girvan-Newman Method, output num of communities, size or largest commnunity, size of smallest community, modularity of partition print("\nGirvan-Newman Method:") components = c.girvan_newman(G) i = 0 for row in components: if (i == 0): finalResult = row i = i + 1 partitions = dict() L = list(finalResult) p = 0 for comp in L: for entry in comp: partitions[entry] = p p = p + 1 com_num = partitions[max(partitions, key=partitions.get)] print("Number of communities:", com_num + 1) count = [] comm_list = list(partition.values()) for x in range(com_num): count.append(comm_list.count(x)) print("The largest community has count:", max(count)) print("The smallest community has count:", min(count)) print("The modularity of this partitioning:", community.modularity(partitions, G)) #Display graph using Girvan-Newman, with nodes in diff colors per partition cmap = cm.get_cmap('viridis', max(partitions.values()) + 1) nx.draw_networkx_nodes(G, pos, partitions.keys(), node_size=60, cmap=cmap, node_color=list(partitions.values())) plt.axis('off') plt.show()
def test_main_crust(self): main_crust_subgraph = nx.k_crust(self.H) assert sorted(main_crust_subgraph.nodes()) == [0, 1, 3]
target=fig_name, layout=layout, vertex_size=7, vertex_color='gray', vertex_label_size=10, vertex_label_dist=2, mark_groups=group_markers) #vertex_label=None # print 'ΟΡΙΣΜΟΣ: Η k-κρούστα είναι ο υπογράφος του G που μένει όταν αφαιρεθεί ο k-πυρήνας.' # # print 'DEFINITION: The k-crust is the graph G with the k-core removed.' # print str(" ") kcrusts = [] for i in set(nx.core_number(G).values()): # for i in set(degree_sequence): if i > 0 and len(nx.k_crust(G, k=i - 1).nodes()) > 0: # print "i =", i kcrGi = nx.k_crust(G, k=i - 1) print 'Οι κόμβοι της', str(i) + '-κρούστας:' # print 'The nodes of the', str(i)+'-crust:' print kcrGi.nodes() # print 'Οι ακμές της', str(i)+'-κρούστας:' # # print 'The edges of the k-crust of G are:' # print kcrGi.edges() # print 'Η ακολουθία βαθμών των κόμβων της', str(i)+'-κρούστας:' # print 'The degree sequence of the nodes of the', str(i)+'-crust:' # print list(nx.degree(kcrGi).values()) # # # print 'The order of the main k-crust of G is:' # # # print 'k =', min(list(nx.degree(kcrGi).values())) print str(" ") kcrusts.append(kcrGi.nodes())
import networkx as nx import plot_multigraph import matplotlib.pylab as plt from matplotlib import pylab as plt n = 80 k = int(.2 * n) p = 10. / n G = nx.fast_gnp_random_graph(n, p, seed=42) def set_to_list(set_, G): return [1. * (k in set_) for k in G.nodes()] graph_colors = [ ("center", set_to_list(nx.center(G), G)), ("periphery", set_to_list(nx.periphery(G), G)), ("k_core", set_to_list(nx.k_core(G), G)), ("k_shell", set_to_list(nx.k_shell(G), G)), ("k_crust", set_to_list(nx.k_crust(G), G)), ("k_corona", set_to_list(nx.k_corona(G, k), G)), ] fig = plot_multigraph.plot_color_multigraph(G, graph_colors, 2, 3, node_size=50) plt.savefig('graphs/sets.png', facecolor=fig.get_facecolor())
colors_lists.append(color_to_set) group_markers = [(kshells[i], colors_lists[i]) for i in range(len(kshells))] # ig.plot(g, mark_groups=group_markers) fig_name=os.path.join(file_dir_figs,str(G.name)+'_[k='+str(max(nx.core_number(G).values()))+']_k-shells.png') ig.plot(g, target=fig_name, layout=layout, vertex_size=7, vertex_color='gray', vertex_label_size=10, vertex_label_dist=2, mark_groups=group_markers) #vertex_label=None # print 'ΟΡΙΣΜΟΣ: Η k-κρούστα είναι ο υπογράφος του G που μένει όταν αφαιρεθεί ο k-πυρήνας.' # # print 'DEFINITION: The k-crust is the graph G with the k-core removed.' # print str(" ") kcrusts=[] for i in set(nx.core_number(G).values()): # for i in set(degree_sequence): if i > 0 and len(nx.k_crust(G,k=i-1).nodes()) > 0: # print "i =", i kcrGi=nx.k_crust(G,k=i-1) print 'Οι κόμβοι της', str(i)+'-κρούστας:' # print 'The nodes of the', str(i)+'-crust:' print kcrGi.nodes() # print 'Οι ακμές της', str(i)+'-κρούστας:' # # print 'The edges of the k-crust of G are:' # print kcrGi.edges() # print 'Η ακολουθία βαθμών των κόμβων της', str(i)+'-κρούστας:' # print 'The degree sequence of the nodes of the', str(i)+'-crust:' # print list(nx.degree(kcrGi).values()) # # # print 'The order of the main k-crust of G is:' # # # print 'k =', min(list(nx.degree(kcrGi).values())) print str(" ") kcrusts.append(kcrGi.nodes())
maxKValue = nodeCoreDict[node] print("K value that gives the main core: " + str(maxKValue)) #Create subgraph that contains just the main core coreGraph = nx.Graph() for edge in mainCoreEdges: coreGraph.add_edge(edge[0], edge[1]) drawGraph(coreGraph) ''' ################################################ Step 4 output: a) Number of nodes in the main crust ##################################################''' print("\nNumber of nodes in the main crust: " + str(len(nx.k_crust(G).nodes()))) ''' ################################################ Step 5 output: a) Number of nodes in the k-corona where k is the max k-val (main core kval) ##################################################''' print("\nNumber of nodes in the k-corona: " + str(len(nx.k_corona(G, k=maxKValue).nodes()))) coronaGraph = nx.Graph() coronaEdges = nx.k_corona(G, k=maxKValue).edges() for edge in coronaEdges(): coronaGraph.add_edge(edge[0], edge[1]) drawGraph(coronaGraph) ''' ################################################