def test_k_truss(self): # k=-1 k_truss_subgraph = nx.k_truss(self.G, -1) assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21)) # k=0 k_truss_subgraph = nx.k_truss(self.G, 0) assert sorted(k_truss_subgraph.nodes()) == list(range(1, 21)) # k=1 k_truss_subgraph = nx.k_truss(self.G, 1) assert sorted(k_truss_subgraph.nodes()) == list(range(1, 13)) # k=2 k_truss_subgraph = nx.k_truss(self.G, 2) assert sorted(k_truss_subgraph.nodes()) == list(range(1, 9)) # k=3 k_truss_subgraph = nx.k_truss(self.G, 3) assert sorted(k_truss_subgraph.nodes()) == []
def test_ktruss_subgraph_Graph_nx(graph_file, nx_ground_truth): gc.collect() k = 5 M = utils.read_csv_for_nx(graph_file, read_weights_in_sp=True) G = nx.from_pandas_edgelist(M, source="0", target="1", edge_attr="weight", create_using=nx.Graph()) k_subgraph = cugraph.k_truss(G, k) k_truss_nx = nx.k_truss(G, k) assert nx.is_isomorphic(k_subgraph, k_truss_nx)
def max_ktruss(G, min_truss, max_truss): """Computes, plots and returns max k-truss value Searches for a maximum k-truss between min_truss and max_truss max_truss can be set arbitrarily large """ verbose = 0 G_save = G itruss = min_truss # look for max k-truss for truss in range(min_truss, max_truss + 1): G_truss = nx.k_truss(G, truss) if G_truss.number_of_nodes() == 0: # Max k-truss reached print(f'Max-truss = {itruss}') break else: G_save = G_truss itruss += 1 if verbose: print(f"Nodes in k-truss : {G_save.nodes()}") return itruss, G_save
def decomposition_democrat(GU, core=False, truss=False): # Democrat Graph nodes = ( node for node, data in GU.nodes(data=True) if data.get("Party") == "Democrat" ) largest_cc = max(nx.connected_components(nx.Graph(GU.subgraph(nodes))), key=len) GU_Democrat = GU.subgraph(largest_cc) if core: nodes = nx.k_core(GU_Democrat, k=12).nodes() elif truss: nodes = nx.k_truss(GU_Democrat, k=4).nodes() node_color = [] for node in GU_Democrat.nodes(): if node in nodes: node_color.append("yellow") else: node_color.append("blue") d = nx.degree(GU_Democrat) sizes = [(d[node] + 1) * 5 for node in GU_Democrat.nodes()] # Specify the settings for the Force Atlas 2 algorithm forceatlas2 = ForceAtlas2( # Behavior alternatives outboundAttractionDistribution=True, # Dissuade hubs linLogMode=False, # NOT IMPLEMENTED adjustSizes=False, # Prevent overlap (NOT IMPLEMENTED) edgeWeightInfluence=1.0, # Performance jitterTolerance=1.0, # Tolerance barnesHutOptimize=True, barnesHutTheta=1.2, multiThreaded=False, # NOT IMPLEMENTED # Tuning scalingRatio=2.0, strongGravityMode=False, gravity=5.0, # Log verbose=True) positions = forceatlas2.forceatlas2_networkx_layout(GU, pos=None, iterations=2000) plt.figure(num=None, figsize=(15, 9), dpi=80, facecolor='w', edgecolor='k') sns.set_style('whitegrid') sns.set_context('talk') # create legend plt.scatter([], [], c='yellow', alpha=0.7, s=100, label='Most Influential Democrats') plt.legend(scatterpoints=1, frameon=True, labelspacing=1) nx.draw_networkx_nodes(GU_Democrat, positions, node_size=sizes, node_color=node_color, alpha=0.7) nx.draw_networkx_edges(GU_Democrat, positions, edge_color="grey", alpha=0.08) ax = plt.gca() ax.collections[0].set_linewidth(0.1) ax.set_title('US House Democrat Representatives of 2020 network', fontsize=16); plt.axis('off') if core: plt.savefig("./images/core_democrat.png", format="PNG") elif truss: plt.savefig("./images/truss_democrat.png", format="PNG") return nodes, GU_Democrat
import networkx as nx import configuration as conf import utils as ut from itertools import combinations if __name__ == "__main__": dataset = ut.load_obj(conf.data_dir, conf.dataset) G = nx.Graph() for v in dataset: if len(v) > 1: G.add_edges_from([t for t in combinations(v, 2)]) ut.save_obj(G, conf.data_dir, f'{conf.dataset}_graph') trussness_g = {} k = 1 while k < 100: ktruss = nx.k_truss(G, k) if len(ktruss) > 0: trussness_g[k] = ktruss k += 1 else: break ut.save_obj(trussness_g, conf.data_dir, f'{conf.dataset}_graph_trussness')