def compute_cliques(G): s_in = open(pickle_path + "id2n.p", "rb") id2n = pickle.load(s_in) clique = nx.make_max_clique_graph(G) print len(clique), clique.size()
def toNetXGraph(connection, nodes): gx = nx.Graph() passg = makeEdgeDictFromCol(connection, 4) flight = makeEdgeDictFromCol(connection, 6) gx.add_nodes_from(nodes) print gx.nodes() gx.add_edges_from(ebunch=connection[:, 2:4]) #how to do attributes #gx.add_weighted_edges_from(ebunch = connection[:,2:4], weight = "flights", attr = connection[:,6]) #how to do attributes #bb=nx.edge_betweenness_centrality(gx, normalized=False) #print bb #nx.set_edge_attributes(gx, name = 'betweenness', values = bb) nx.set_edge_attributes(gx, name='passengerCount', values=passg) nx.set_edge_attributes(gx, name='flights', values=flight) #nx.set_node_attributes(gx, name = 'lat', values = ) nx.draw_spring(gx, node_size=20, font_size=100) plt.show() print nx.degree_centrality(gx) clique = nx.make_max_clique_graph(gx) #plot(obj = clique) #nx.set_edge_attributes(gx, name='passengers', values = connection[:,3]) #nx.set_node_attributes(gx, name='lat', values = ) #print gx.edges(data=True) print "NetX loaded" return gx
def test_make_clique_bipartite(self): G=self.G B=nx.make_clique_bipartite(G) assert_equal(sorted(B.nodes()), [-5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) H=nx.project_down(B) assert_equal(H.adj,G.adj) H1=nx.project_up(B) assert_equal(H1.nodes(),[1, 2, 3, 4, 5]) H2=nx.make_max_clique_graph(G) assert_equal(H1.adj,H2.adj)
def make_max_clique_graph(grafo): tiempo=[] for i in range(30): tiempo_inicial = dt.datetime.now() for j in range(15): nx.make_max_clique_graph(grafo) tiempo_final = dt.datetime.now() tiempo_ejecucion = (tiempo_final - tiempo_inicial).total_seconds() tiempo.append(tiempo_ejecucion) media=nup.mean(tiempo) desv=nup.std(tiempo) mediana=nup.median(tiempo) datos["algoritmo"].append("make_max_clique_graph") datos["grafo"].append(grafo.name) datos["cant_vertice"].append(grafo.number_of_nodes()) datos["cant_arista"].append(grafo.number_of_edges()) datos["media"].append(media) datos["desv"].append(desv) datos["mediana"].append(mediana) return datos
def test_make_max_clique_graph(self): """Tests that the maximal clique graph is the same as the bipartite clique graph after being projected onto the nodes representing the cliques. """ G = self.G B = nx.make_clique_bipartite(G) # Project onto the nodes representing the cliques. H1 = nx.project(B, range(-5, 0)) # Relabel the negative numbers as nonnegative ones, starting at # 0. H1 = nx.relabel_nodes(H1, {-v: v - 1 for v in range(1, 6)}) H2 = nx.make_max_clique_graph(G) assert_equal(H1.adj, H2.adj)
def max_clique(nombre):# Para Grafo Simple No dirigido df = pd.read_csv(nombre, header=None) b = nx.from_pandas_adjacency(df, create_using=nx.Graph()) for i in range(30): # Ejecutar las 50 corridas inicio = datetime.datetime.now() for key in range(50): nx.make_max_clique_graph(b, create_using=None) final = datetime.datetime.now() tiempos.append((final - inicio).total_seconds()) # Guardo los tiempos de las corridas media=np.mean(tiempos) desv=np.std(tiempos) mediana=np.median(tiempos) nodos=nx.number_of_nodes(b) arcos=nx.number_of_edges(b) salvar=[] salvar.append(media) salvar.append(desv) salvar.append(mediana) salvar.append(nodos) salvar.append(arcos) return salvar
def test_make_max_clique_graph(self): """Tests that the maximal clique graph is the same as the bipartite clique graph after being projected onto the nodes representing the cliques. """ G = self.G B = nx.make_clique_bipartite(G) # Project onto the nodes representing the cliques. H1 = nx.project(B, range(-5, 0)) # Relabel the negative numbers as nonnegative ones, starting at # 0. H1 = nx.relabel_nodes(H1, {-v: v - 1 for v in range(1, 6)}) H2 = nx.make_max_clique_graph(G) assert H1.adj == H2.adj
def clique(self): import config graphs = config.graph file = open('clique.txt', 'w') clique_string = [ "This txt file will show you the finding of cliques in your network.\n\n" + "Description : In complex network, a clique is a maximal subset of the vertices or nodes in an undirected network such that every member\n" + "of the set is connected by an edge or link to every other node." + "The meaning of 'maximal' here means there is no other vertex or node in the network that can be added to the subset while keeping or preserving\n" + "the property that every vertex or node is connected to every other.\n" ] file.write(clique_string[0]) max_clique = list(nx.make_max_clique_graph(graphs)) max_clique_str = str(max_clique) max_clique_string = [ "Maximal Cliques:\n-The maximal cliques and treats these cliques as nodes.\n -These nodes in a [] are connected if they have common members in the original graph.\n" + "-" + max_clique_str + '\n' ] file.write(max_clique_string[0]) all_maximal_cliques = str(list(nx.find_cliques(graphs))) all_maximal_cliques_string = [ "Cliques:\n-The possible cliques in the network.\n" + "-" + all_maximal_cliques + '\n' ] file.write(all_maximal_cliques_string[0]) number_of_maximum_clique = str(nx.graph_number_of_cliques(graphs)) number_of_node_in_largest_clique = str(nx.graph_clique_number(graphs)) clique_number_string = [ "Basic statistic of cliques in network:\n-The (largest) number of cliques in the network:" + number_of_maximum_clique + "\n" + "-The number of nodes in the largest clique in the network:" + number_of_node_in_largest_clique ] file.write(clique_number_string[0]) file.close() # this must add or only display a empty txt import os os.system("notepad.exe clique.txt")
def to_clique_graph(g): cliques = [ c for c in nx.find_cliques(g) ] clique_graph = nx.make_max_clique_graph(g) ### networkx.clique_graph does not tell which nodes it contains frozenset_clique_graph = nx.Graph() for a in clique_graph: a_clique = frozenset(cliques[a-1]) frozenset_clique_graph.add_node(a_clique) for b in clique_graph[a]: b_clique = frozenset(cliques[b-1]) ### weight it with the negative cardinality of the ### intersection so that you can use the built in ### minimum_spanning_tree function to get the maximum ### spanning tree overlap_size = len(a_clique.intersection(b_clique)) frozenset_clique_graph.add_edge( a_clique, b_clique, weight = -overlap_size) return frozenset_clique_graph
# RA, 2017-11-04 import topology import networkx as nx G0 = nx.fast_gnp_random_graph(20, 0.5, seed=255) G1 = nx.make_max_clique_graph(G0) print(sorted([d for (n, d) in G0.degree()], reverse=True)) print(sorted([d for (n, d) in G1.degree()], reverse=True))
def _create_clique_graph(self, th: nx.Graph) -> nx.Graph: return nx.make_max_clique_graph(th)
node_size=500, edgelist=edge_list, edge_color=edge_color, width=edge_width, alpha=node_alpha, edgealpha=edge_alpha, ) nx.draw_networkx_nodes(G, pos, nodelist=nx.isolates(G), with_labels=True, node_size=500, node_color="w") plt.figure() plt.title("The maximal cliques of graph G") nx.draw(H, with_labels=True) ClG = nx.make_max_clique_graph(G) # print 'ClG nodes' # print ClG.nodes() # print str(" ") # # print 'ClG edges' # print ClG.edges() # print str(" ") # # print graphs_labels # print type(graphs_labels) # graphs=sorted(nx.find_cliques(G), key = len, reverse=True) # graphs_labels_s=sorted(graphs_labels, key = len, reverse=True)
to_plot = [a2_t1_times, a2_t2_times, a2_t3_times, a2_t4_times, a2_t5_times] fig = plt.figure(1, figsize=(9, 6)) ax = fig.add_subplot(111) bp = ax.boxplot(to_plot, showfliers=False) plt.xlabel('Grafo') plt.ylabel('Tiempo (segundos)') plt.title('Árbol Búsqueda Profunda') plt.savefig('BP2.eps', format='eps', dpi=1000) plt.show() # *********************max_clique********************* for j in range(1, 31): algorithm2_start_time = time.time() g1_start_time = time.time() for i in range(6): nx.make_max_clique_graph(G11) a3_t1_times.append(time.time() - g1_start_time) g2_start_time = time.time() for i in range(6): nx.make_max_clique_graph(G12) a3_t2_times.append(time.time() - g2_start_time) g3_start_time = time.time() for i in range(6): nx.make_max_clique_graph(G13) a3_t3_times.append(time.time() - g3_start_time) g4_start_time = time.time() for i in range(6): nx.make_max_clique_graph(G14)
def MaxClique(graph): start_time=time() R = nx.make_max_clique_graph(graph, create_using=None) time_elapsed = time() - start_time return time_elapsed
N= nx.MultiDiGraph() N.add_nodes_from(['A1','A2','A3','A4','A5','A6']) N.add_edges_from([('A1','A2'),('A1','A3'),('A2','A4'),('A2','A5'),('A3','A5'),('A3','A6')]) ex = nx.MultiDiGraph() ex.add_nodes_from(['a','b','c','d','e','f','g','h','i','j','k','l','d1','d2','d3','d4','v1','v2','v3','v4']) ex.add_edges_from([('a','c'),('b','c'),('b','d'),('b','d1'),('d','f'),('c','e'),('d','e'),('e','g')]) ex.add_edges_from([('e', 'd2'), ('f', 'd2'), ('f', 'h'), ('h', 'j'), ('h', 'k'), ('k', 'v3'), ('j', 'v3')]) ex.add_edges_from([('d4','l'), ('l', 'v4'), ('i', 'l')]) ex.add_edges_from([('d1','d'), ('d1', 'v1'), ('d2', 'i'), ('d2', 'd3'), ('d3', 'd4'), ('d4', 'l')]) #nx.draw_graphviz(ex) #nx.draw_networkx_edge_labels(ex,) nx.draw_networkx(ex,with_labels=True) ex2 = nx.MultiGraph(ex) plt.figure() nx.draw_networkx(ex2) #print nx.is_chordal(ex2) #print nx.chordal_graph_cliques(ex2) print nx.find_cliques(ex2) cli = nx.make_max_clique_graph(ex2) plt.figure() nx.draw_networkx(cli,with_labels=True)
def calculate(graph): return nx.make_max_clique_graph(graph)
edge_color=edge_color, width=edge_width, alpha=node_alpha, edgealpha=edge_alpha) nx.draw_networkx_nodes(G, pos, nodelist=nx.isolates(G), with_labels=True, node_size=500, node_color='w') plt.figure() plt.title('The maximal cliques of graph G') nx.draw(H, with_labels=True) ClG = nx.make_max_clique_graph(G) # print 'ClG nodes' # print ClG.nodes() # print str(" ") # # print 'ClG edges' # print ClG.edges() # print str(" ") # # print graphs_labels # print type(graphs_labels) # graphs=sorted(nx.find_cliques(G), key = len, reverse=True) # graphs_labels_s=sorted(graphs_labels, key = len, reverse=True)
graph = graphbuilder.GraphBuilder() top_tags = [t for t, c in ip.tags.items() if c >= 10] graph.edges_from_text(ip.d_insta, top_tags) #ccs = nx.connected_component_subgraphs(graph.G) #for c in ccs: cliq = list(nx.find_cliques(graph.G)) skips = ['firestone'] next_scrapings = set() for c in cliq: for word in c: if b_any(x in word for x in skips): print word else: next_scrapings.add(word) f.write_clique(next_scrapings) nx.make_max_clique_graph(graph.G) pos = nx.spring_layout(graph.G) elarge = [(u, v) for (u, v, d) in graph.G.edges(data=True) if d['weight'] > 15] esmall = [(u, v) for (u, v, d) in graph.G.edges(data=True) if d['weight'] <= 15] nx.draw_networkx_nodes(graph.G, pos, node_size=10) nx.draw_networkx_edges(graph.G, pos, edgelist=elarge, width=3, edge_color='g') nx.draw_networkx_edges(graph.G, pos, edgelist=esmall, width=1,