我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用networkx.read_edgelist()。
def remove_cycle_edges_by_mfas(graph_file): g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = int) from remove_self_loops import remove_self_loops_from_graph self_loops = remove_self_loops_from_graph(g) scc_nodes,_,_,_ = scc_nodes_edges(g) degree_dict = get_nodes_degree_dict(g,scc_nodes) sccs = get_big_sccs(g) if len(sccs) == 0: print("After removal of self loop edgs: %s" % nx.is_directed_acyclic_graph(g)) return self_loops edges_to_be_removed = [] import timeit t1 = timeit.default_timer() greedy_local_heuristic(sccs,degree_dict,edges_to_be_removed) t2 = timeit.default_timer() print("mfas time usage: %0.4f s" % (t2 - t1)) edges_to_be_removed = list(set(edges_to_be_removed)) g.remove_edges_from(edges_to_be_removed) edges_to_be_removed += self_loops edges_to_be_removed_file = graph_file[:len(graph_file)-6] + "_removed_by_mfas.edges" write_pairs_to_file(edges_to_be_removed,edges_to_be_removed_file) return edges_to_be_removed
def introduce_cycles_2_DAG(graph_file,num_extra_edges,path_length): if path_length <= 0: print("no constraints on path length") else: print("path length: %d" % path_length) g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = int) extra_edges = introduce_cycles(g,num_extra_edges,path_length = path_length) extra_edges_file = graph_file[:len(graph_file)-6] + "_extra_" + str(num_extra_edges) + "_path_len_" + str(path_length) + ".edges" graph_with_extra_edges_file = graph_file[:len(graph_file)-6] + "_graph_w_extra_" + str(num_extra_edges) + "_path_len_" + str(path_length) + ".edges" print("extra edges saved in: %s" % extra_edges_file) print("graph with extra edges saved in: %s" % graph_with_extra_edges_file) from file_io import write_pairs_to_file write_pairs_to_file(extra_edges,extra_edges_file) write_pairs_to_file(extra_edges + g.edges(),graph_with_extra_edges_file) return (extra_edges_file,graph_with_extra_edges_file)
def read_net(fname, weighted, directed, log): if weighted: G = nx.read_edgelist(inodetype=int, data=(('weight', float),), create_using=nx.DiGraph()) else: G = nx.read_edgelist(fname, nodetype=int, create_using=nx.DiGraph()) for edge in G.edges(): G[edge[0]][edge[1]]['weight'] = 1 if not directed: G = G.to_undirected() log.info('N: %d E: %d' % (G.number_of_nodes(), G.number_of_edges())) log.info('CC: %d' % nx.number_connected_components(G)) giant = max(nx.connected_component_subgraphs(G), key=len) log.info('N: %d E: %d' % (giant.number_of_nodes(), giant.number_of_edges())) return giant
def read_graph(): """ Read 'edges.txt.gz' into a networkx **undirected** graph. Done for you. Returns: A networkx undirected graph. """ return nx.read_edgelist('edges.txt.gz', delimiter='\t')
def __load_edgelist(self): """ Load the graph """ self.G = nx.read_edgelist(self.params["edgelist_path"],create_using=nx.DiGraph()) # self.G = nx.convert_node_labels_to_integers(self.G, first_label=0,ordering="sorted") return
def read_graph(self, nx_g): if self.is_weighted: self.G = nx.read_edgelist(nx_g, data=(('weight', float),), create_using=nx.DiGraph(), edgetype=str) else: self.G = nx.read_edgelist(nx_g, create_using=nx.DiGraph(), edgetype=str) for edge in self.G.edges(): self.G[edge[0]][edge[1]]['weight'] = 1 if not self.is_directed: self.G = self.G.to_undirected()
def test_edges_to_graph_edgelist(self): graph = nx.read_edgelist(fixtures_path('graph.edgelist')) with open(fixtures_path('graph.json'), 'r') as json_graph: edges = json.load(json_graph) out = nx.parse_edgelist( utils.edges_to_graph(edges, fmt='edgelist').split('\n')) assert nodeset(out) == nodeset(graph) assert edgeset(out) == edgeset(graph)
def evaluation(graph_file,gt_edges_file,method): g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = int) if method == "dfs": from remove_cycle_edges_by_dfs import dfs_performance edges_to_be_removed = dfs_performance(graph_file,gt_edges_file) elif method == "mfas": from remove_cycle_edges_by_minimum_feedback_arc_set_greedy import mfas_performance mfas_performance(graph_file,gt_edges_file) elif method == "pagerank" or method == "ensembling" or method == "trueskill" or method == "socialagony": from remove_cycle_edges_by_hierarchy import breaking_cycles_by_hierarchy_performance breaking_cycles_by_hierarchy_performance(graph_file,gt_edges_file,method)
def c_c(graph_file): g = nx.read_edgelist(graph_file,create_using = nx.Graph(),nodetype = int) graphs = nx.connected_component_subgraphs(g) for graph in graphs: print graph.number_of_nodes(),graph.number_of_edges() print len(graphs)
def main(edges_file_name = "/home/sunjiank/Dropbox/Data/cit-Patents/cit-Patents.txt"): g = nx.read_edgelist(edges_file_name,create_using = nx.DiGraph(),nodetype = int) graphbased_trueskill(g)
def remove_cycle_edges_strategies(graph_file,nodes_score_dict,score_name = "socialagony"): g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = int) # greedy e1 = scc_based_to_remove_cycle_edges_iterately(g,nodes_score_dict) g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = int) # forward e2 = remove_cycle_edges_BF_iterately(g,nodes_score_dict,is_Forward = True,score_name = score_name) # backward g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = int) e3 = remove_cycle_edges_BF_iterately(g,nodes_score_dict,is_Forward = False,score_name = score_name) return e1,e2,e3
def remove_self_loops_from_edges_file(graph_file): g = nx.read_edgelist(args.original_graph, nodetype = int, create_using = nx.DiGraph()) return remove_self_loops_from_graph(g)
def dfs_remove_back_edges(graph_file): ''' 0: white, not visited 1: grey, being visited 2: black, already visited ''' g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = int) nodes_color = {} edges_to_be_removed = [] for node in g.nodes_iter(): nodes_color[node] = 0 nodes_order = list(g.nodes_iter()) nodes_order = np.random.permutation(nodes_order) num_dfs = 0 for node in nodes_order: if nodes_color[node] == 0: num_dfs += 1 dfs_visit_recursively(g,node,nodes_color,edges_to_be_removed) #print("number of nodes to start dfs: %d" % num_dfs) #print("number of back edges: %d" % len(edges_to_be_removed)) edges_to_be_removed_file = graph_file[:len(graph_file)-6] + "_removed_by_dfs.edges" print("edges to be removed, saved in file: %s" % edges_to_be_removed_file) from file_io import write_pairs_to_file write_pairs_to_file(edges_to_be_removed,edges_to_be_removed_file) return edges_to_be_removed
def remove_cycle_edges_heuristic(graph_file,edges_score): g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = int) from remove_self_loops import remove_self_loops_from_graph self_loops = remove_self_loops_from_graph(g) edges_to_be_removed = scc_based_to_remove_cycle_edges_iterately(g,edges_score) edges_to_be_removed = list(set(edges_to_be_removed)) return edges_to_be_removed+self_loops
def read_graph(): if args.directed: g = nx.read_edgelist(args.input, nodetype=str, data=[('Release_Detained', int)], create_using=nx.DiGraph()) else: g = nx.read_edgelist(args.input, nodetype=str, data=[('Release_Detained', int)], create_using=nx.Graph()) # instantiate MyGraph using G myg = MyGraph(g, args.dimensions, args.window_size, is_directed=False, node_attributes=False, walks_per_node=args.num_walks) print 'Graph created...' return myg
def read_graph(): colnames = [] edge_attributes = args.edge_attribute_names.split(',') colnames += edge_attributes node_attributes = dict() for node_input in args.node_names_attributes.split(':'): names = node_input.split(',') node_attributes[names[0]] = names[1:] colnames += names edge_attribute_types = [] for edge_attr in edge_attributes: edge_attribute_types.append((edge_attr, str)) if args.directed: g = nx.read_edgelist(args.input, nodetype=str, data=edge_attribute_types, create_using=nx.DiGraph()) else: g = nx.read_edgelist(args.input, nodetype=str, data=edge_attribute_types, create_using=nx.Graph()) x = np.genfromtxt(args.attributes_file, delimiter='\t', dtype=None, names=True, usecols=colnames) for i in range(len(x)): for node_type in node_attributes: for node_attr in node_attributes[node_type]: g.add_node(str(x[i][node_type]), {str(node_attr): x[i][node_attr]}) myg = MyGraph(g, args.dimensions, args.window_size, is_directed=False, node_attributes=False, walks_per_node=args.num_walks) myg.create_pattern_nodes() print 'Graph created...' return myg
def _read_graph(self): self.graph = nx.read_edgelist(self.edgelist, data=(('weight',float),), create_using=nx.DiGraph(), edgetype = str) self.N = len(self.graph.nodes()) #overriding of the parent function
def main(): """ Main method """ filename = sys.argv[1] if filename.split('.')[1] == 'gml': G = read_gml('networks/' + filename) else: G = nx.read_edgelist('networks/' + filename, delimiter='\t', nodetype=int) G = G.to_undirected() for node in G.nodes_with_selfloops(): G.remove_edge(node, node) G1 = nx.Graph() for edge in G.edges(): u = edge[0] v = edge[1] if u == v: continue if not G1.has_edge(u, v): G1.add_edge(u, v, weight=1.0) G = G1 print "Number of nodes:", G.number_of_nodes() print "Number of edges:", G.number_of_edges() print subg = greedy_degree_density(G) print "----Greedy Degree Density----" print "Degree Density: " + str(degree_density(subg)) print "Density: " + str(density(subg)) print "Triangle Density: " + str(triangle_density(subg)) print "# Nodes: " + str(subg.number_of_nodes()) print subg = greedy_quasi_cliques(G, 0.333) print "----Greedy Edge Surplus with alpha=1/3----" print "Degree Density: " + str(degree_density(subg)) print "Density: " + str(density(subg)) print "Triangle Density: " + str(triangle_density(subg)) print "# Nodes: " + str(subg.number_of_nodes()) print subg = greedy_triangle_density(G) print "----Greedy Triangle Density----" print "Degree Density: " + str(degree_density(subg)) print "Density: " + str(density(subg)) print "Triangle Density: " + str(triangle_density(subg)) print "# Nodes: " + str(subg.number_of_nodes()) print subg = greedy_triangle_graph_density(G) print "----Greedy Triangle-Graph Density----" print "Degree Density: " + str(degree_density(subg)) print "Density: " + str(density(subg)) print "Triangle Density: " + str(triangle_density(subg)) print "# Nodes: " + str(subg.number_of_nodes())