本文整理汇总了Python中networkx.DiGraph.number_of_nodes方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.number_of_nodes方法的具体用法?Python DiGraph.number_of_nodes怎么用?Python DiGraph.number_of_nodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.DiGraph
的用法示例。
在下文中一共展示了DiGraph.number_of_nodes方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: analyze
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import number_of_nodes [as 别名]
def analyze(graph: nx.DiGraph, root_link: str):
print("Graph of {0}".format(root_link))
num_v = graph.number_of_nodes()
num_e = graph.number_of_edges()
#print("Shotest path:")
#print(nx.shortest_path(graph))
print("Number of vertices: {0}".format(num_v))
print("Number of edges: {0}".format(num_e))
print("Connected components:")
#print(nx.connected_components(graph.to_undirected()))
print("Degree:")
print(list(nx.degree(graph))[:10])
示例2: page_rank
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import number_of_nodes [as 别名]
def page_rank(graph: nx.DiGraph, root_link: str):
num_pages = graph.number_of_nodes()
d = 0.85
Q = deque([root_link])
rank = {}
while Q:
cur = Q.pop()
if cur not in rank:
indices = graph[cur]
r = (1 - d)/num_pages
if indices:
r += d * (rank.get(cur, 1.0) / len(indices))
# else:
# print("Weird case: {0}".format(cur))
rank[cur] = r
for link in indices.keys():
Q.appendleft(link)
print(list(rank.items())[:10])
示例3: load
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import number_of_nodes [as 别名]
def load(self,fname, verbose=True, **kwargs):
"""
Load a data file. The expected data format is three columns
(comma seperated by default) with source, target, flux.
No header should be included and the node IDs have to run contuously
from 0 to Number_of_nodes-1.
Parameters
----------
fname : str
Path to the file
verbose : bool
Print information about the data. True by Default
kwargs : dict
Default parameters can be changed here. Supported key words are
dtype : float (default)
delimiter : "," (default)
return_graph : bool
If True, the graph is returned (False by default).
Returns:
--------
The graph is saved internally in self.graph.
"""
delimiter = kwargs["delimiter"] if "delimiter" in kwargs.keys() else " "
data = np.genfromtxt(fname, delimiter=delimiter, dtype=int, unpack=False)
source, target = data[:,0], data[:,1]
if data.shape[1] > 2:
flux = data[:,2]
else:
flux = np.ones_like(source)
nodes = set(source) | set(target)
self.nodes = len(nodes)
lines = len(flux)
if set(range(self.nodes)) != nodes:
new_node_ID = {old:new for new,old in enumerate(nodes)}
map_new_node_ID = np.vectorize(new_node_ID.__getitem__)
source = map_new_node_ID(source)
target = map_new_node_ID(target)
if verbose:
print "\nThe node IDs have to run continuously from 0 to Number_of_nodes-1."
print "Node IDs have been changed according to the requirement.\n-----------------------------------\n"
print 'Lines: ',lines , ', Nodes: ', self.nodes
print '-----------------------------------\nData Structure:\n\nsource, target, weight \n'
for ii in range(7):
print "%i, %i, %1.2e" %(source[ii], target[ii], flux[ii])
print '-----------------------------------\n'
G = DiGraph() # Empty, directed Graph
G.add_nodes_from(range(self.nodes))
for ii in xrange(lines):
u, v, w = int(source[ii]), int(target[ii]), float(flux[ii])
if u != v: # ignore self loops
assert not G.has_edge(u,v), "Edge appeared twice - not supported"
G.add_edge(u,v,weight=w)
else:
if verbose:
print "ignore self loop at node", u
symmetric = True
for s,t,w in G.edges(data=True):
w1 = G[s][t]["weight"]
try:
w2 = G[t][s]["weight"]
except KeyError:
symmetric = False
G.add_edge(t,s,weight=w1)
w2 = w1
if w1 != w2:
symmetric = False
G[s][t]["weight"] += G[t][s]["weight"]
G[s][t]["weight"] /= 2
G[t][s]["weight"] = G[s][t]["weight"]
if verbose:
if not symmetric:
print "The network has been symmetricised."
ccs = strongly_connected_component_subgraphs(G)
ccs = sorted(ccs, key=len, reverse=True)
G_GSCC = ccs[0]
if G_GSCC.number_of_nodes() != G.number_of_nodes():
G = G_GSCC
if verbose:
print "\n--------------------------------------------------------------------------"
print "The network has been restricted to the giant strongly connected component."
self.nodes = G.number_of_nodes()
#.........这里部分代码省略.........