本文整理汇总了Python中networkx.Graph.number_of_nodes方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.number_of_nodes方法的具体用法?Python Graph.number_of_nodes怎么用?Python Graph.number_of_nodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.Graph
的用法示例。
在下文中一共展示了Graph.number_of_nodes方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_fbvs
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import number_of_nodes [as 别名]
def get_fbvs(self, graph: Graph):
if is_acyclic(graph):
return set()
if type(graph) is not MultiGraph:
graph = MultiGraph(graph)
for i in range(1, graph.number_of_nodes()):
result = self.get_fbvs_max_size(graph, i)
if result is not None:
return result # in the worst case, result is n-2 nodes
示例2: test_algorithms
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import number_of_nodes [as 别名]
def test_algorithms(algorithms, graph: nx.Graph):
print()
print("Testing graph with {0} nodes and {1} edges".format(graph.number_of_nodes(), graph.number_of_edges()))
results = []
for algorithm, name in algorithms:
# make a copy of the graph in case the algorithm mutates it
graph_copy = graph.copy()
start_time = time.time()
result = len(algorithm.get_fbvs(graph_copy))
print("{0}: {1}, time: {2}".format(name, result, time.time() - start_time))
results.append(result)
assert results.count(results[0]) == len(results), "The algorithms's results are not the same!"
示例3: test_algorithms
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import number_of_nodes [as 别名]
def test_algorithms(algorithms, graph: Graph, k):
print()
print("Testing graph with {0} nodes and {1} edges, expected result: {2}"
.format(graph.number_of_nodes(), graph.number_of_edges(), k))
for algorithm, name in algorithms:
start_time = time.time()
args = inspect.getfullargspec(algorithm)[0]
if len(args) == 2:
result = len(algorithm(graph))
else:
result = len(algorithm(graph, k))
print("{0}: {1}, time: {2}".format(name, result, time.time() - start_time))
assert k == result, "Wrong result!"
示例4: open
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import number_of_nodes [as 别名]
__author__ = 'zplin'
import sys
import json
import csv
from os import path
import numpy as np
from networkx import Graph, transitivity, clustering, average_shortest_path_length, connected_component_subgraphs
from networkx.readwrite import json_graph
if __name__ == '__main__':
with open(sys.argv[1]) as g_file:
data = json.load(g_file)
g = Graph(json_graph.node_link_graph(data))
print('Number of nodes:', g.number_of_nodes())
print('Average degree:', 2 * g.number_of_edges()/g.number_of_nodes())
print('Transitivity:', transitivity(g))
cc = clustering(g)
print('Average clustering coefficient:', np.mean(list(cc.values())))
for subgraph in connected_component_subgraphs(g):
if subgraph.number_of_nodes() > 1:
print('Average shortest path length for subgraph of', subgraph.number_of_nodes(), ':',
average_shortest_path_length(subgraph))
# Calculating average clustering coefficient for different degrees
degree_cc = {}
for node, degree in g.degree_iter():
if degree not in degree_cc:
degree_cc[degree] = []
degree_cc[degree].append(cc[node])
with open(path.join(path.dirname(sys.argv[1]), 'clustering.csv'), 'w', newline='') as cc_file:
示例5: isinstance
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import number_of_nodes [as 别名]
graph.add_edge(last, node, type=tags['highway'])
#edges += 1
last = node
elif isinstance(item, Node):
pos = utm.from_latlon(item.lat, item.lon, force_zone_number=utm_zone_number)
if not utm_zone_number:
utm_zone_number = pos[2]
graph.add_node(item.id, lat=item.lat, lon=item.lon, pos=pos[:2])
#nodes += 1
items += 1
print('{0} items processed'.format(items), end='\r')
print('{0} items processed'.format(items))
if shape_file:
n = graph.number_of_nodes()
i = 0
print('Apply shapefile')
for node in graph.nodes():
p = Point(graph.node[node]['lon'], graph.node[node]['lat'])
if not shape_file.contains(p):
graph.remove_node(node)
i += 1
print('{0}/{1} nodes processed'.format(i, n), end='\r')
print('{0}/{1} nodes processed'.format(i, n))
print('Search for orphaned nodes')
orphaned = set()
n = graph.number_of_nodes()
i = 0
for node in graph.nodes_iter():
示例6: open
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import number_of_nodes [as 别名]
ips = {}
# filter all relays in this consensus to those that
# have a descriptor, are running, and are fast
for relay in consensus.relays:
if (relay in descriptors):
sd = descriptors[relay] # server descriptor
rse = consensus.relays[relay] # router status entry
if "Running" in rse.flags and "Fast" in rse.flags:
if relay not in ips: ips[relay] = []
ips[relay].append(sd.address)
# build edges between every relay that could have been
# selected in a path together
for r1 in ips:
for r2 in ips:
if r1 is r2: continue
g.add_edges_from(product(ips[r1], ips[r2]))
nsf_i += 1
# check if we should do a checkpoint and save our progress
if nsf_i == nsf_len or "01-00-00-00" in fname:
chkpntstart = fname[0:10]
with open("relaypairs.{0}--{1}.json".format(chkpntstart, chkpntend), 'wb') as f: json.dump(g.edges(), f)
print ""
print('Num addresses: {0}'.format(g.number_of_nodes()))
print('Num unique pairs: {0}'.format(g.number_of_edges()))
# write final graph to disk
with open(out_file, 'wb') as f: json.dump(g.edges(), f)
##########