nx.path_graph:
"""Returns the Path graph `P_n` of linearly connected nodes.
Parameters
----------
n : int or iterable
If an integer, node labels are 0 to n with center 0.
If an iterable of nodes, the center is the first.
create_using : NetworkX graph constructor, optional (default=nx.Graph)
Graph type to create. If graph instance, then cleared before populated.
"""
d = G.degree('a') # 获取'a'的度
d_some = G.degree([0, 1, 2]) # 可以传入列表,得到的是 [(0, 1), (1, 2), (2, 2)],注意这个不是list,需要用list函数转换一下
d_all = G.degree() # 如果不传入参数,默认得到的是全部顶点的度,格式和上一步相同
node_nums =G.number_of_nodes() # 获取节点的数目
nodes = G.nodes() # 获取所有的节点
edge_nums = G.size() # 获取无向图边的数目
edges = G.edges() # 获取所有的边
neighbors_a = G.neighbors('a') # 获取'a'的所有相邻顶点
# 遍历边
for u,v,d in G.edges(data = 'weight'):
print((u,v,d))
# 通过邻接矩阵遍历边
for n, nbrs in G.adjacency():
for nbr, eattr in nbrs.items():
print(n, nbr, eattr)
adjacency:
"""Returns an iterator over (node, adjacency dict) tuples for all nodes.
For directed graphs, only outgoing neighbors/adjacencies are included.
Returns
-------
adj_iter : iterator
An iterator over (node, adjacency dictionary) for all nodes in
the graph.
Examples
--------
>>> G = nx.path_graph(4) # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> [(n, nbrdict) for n, nbrdict in G.adjacency()]
[(0, {1: {}}), (1, {0: {}, 2: {}}), (2, {1: {}, 3: {}}), (3, {2: {}})]
"""