当前位置:网站首页>NetworkX的基本用法
NetworkX的基本用法
2022-07-21 06:46:00 【aelum】
一、前言
NetworkX 是一个用于图计算的开源第三方库,常用如下缩写
import networkx as nx
二、图、结点与边的创建/删除
2.1 图的创建
NetworkX 提供了四种类型的图,如下:
以无向图为例,创建它只需要
G = nx.Graph()
print(G)
# Graph with 0 nodes and 0 edges
可以看出,使用这种方法创建的图是空图。
2.2 添加结点
NetworkX 的结点可以是除了 None
以外的任何可哈希对象,例如字符串,数字,图像等(当然可以是另外一个 Graph 对象)。
若要每次只添加一个结点,则可以使用 add_node
方法:
G = nx.Graph()
G.add_node(1)
G.add_node('ABC')
G.add_node(nx.Graph())
print(G.number_of_nodes()) # 查看G中有多少个结点
# 3
print(G.nodes)
# [1, 'ABC', <networkx.classes.graph.Graph object at 0x1173ec880>]
若要一次添加多个结点,则可以使用 add_nodes_from
方法(传入参数必须是可迭代容器):
G = nx.Graph()
G.add_nodes_from([1, 'ABC', nx.Graph()])
print(G.nodes)
# [1, 'ABC', <networkx.classes.graph.Graph object at 0x1174f4f40>]
G = nx.Graph()
G.add_nodes_from("hello")
print(G.nodes) # 因为结点不可重复,所以最终只有4个结点
# ['h', 'e', 'l', 'o']
2.3 添加边
同样,我们可以每次只添加一条边,这需要用到 add_edge
方法
G = nx.Graph()
G.add_edge(1, 2) # 在结点1,2之间添加一条边
print(G.edges)
# [(1, 2)]
print(G.nodes)
# [1, 2]
可以看出,在两个结点之间添加边时,若这两个结点在图中不存在,则会自动创建。
若要一次性添加多个边,则需使用 add_edges_from
方法:
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3)])
print(G.nodes)
# [0, 1, 2, 3]
print(G.edges)
# [(0, 1), (1, 2), (2, 3)]
可视化:
nx.draw(G, with_labels=True)
2.4 删除结点
删除单个结点:
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2)])
G.remove_node(1)
print(G.edges)
# []
删除多个结点:
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3)])
G.remove_nodes_from([1, 2])
print(G.edges)
# []
print(G.nodes)
# [0, 3]
2.5 删除边
删除单个边:
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3)])
G.remove_edge(1, 2)
print(G.edges)
# [(0, 1), (2, 3)]
删除多条边:
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3)])
G.remove_edges_from([(0, 1), (2, 3)])
print(G.edges)
# [(1, 2)]
注意,删除边并不会删除结点。
三、图的相关计算
图的邻接表:
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3)])
print(G.adj)
# {0: {1: {}}, 1: {0: {}, 2: {}}, 2: {1: {}, 3: {}}, 3: {2: {}}}
某个结点的所有邻居:
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3)])
nbr = G.neighbors(1) # 返回一个迭代器
print([n for n in nbr])
# [0, 2]
注意,G.neighbors(n)
、G.adj[n]
和 G[n]
均可以访问结点 n n n 的邻居。
判断结点和边是否存在:
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3)])
print(G.has_node(4)) # 等价于 4 in G
# False
print(G.has_edge(1, 2))
# True
求每个结点的度:
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3)])
print(G.degree)
# [(0, 1), (1, 2), (2, 2), (3, 1)]
计算有多少个结点和多少条边:
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3)])
print(G.number_of_nodes())
# 4
print(G.number_of_edges())
# 3
边栏推荐
- [semidrive source code analysis] [module bringup] 37 - LCM driven bringup process
- LeetCode 剑指 Offer II 061. 和最小的 k 个数对***
- uniapp,微信小程序input正则校验只能输入为数字和小数点位数限制
- 接口测试中,要验证和使用数据库吗
- Es installation & IK Chinese parser
- Difference between two lists
- MQTT5.0新特性(比对3.1.1)
- 【JVM】垃圾收集器的选择
- Steve Aoki 的人物化身将来到 The Sandbox 元宇宙!
- Simple understanding of gateway
猜你喜欢
Interpreting the maker education model in line with the mainstream of the new era
List parsing < STL elementary > (runner's notes)
查看IAR工程所用版本号
Introduction to common interfaces of vector
Lecture 1 Overview
lua环境配置
11 ways to obtain SSL certificates for free
Steve Aoki's Avatar will come to the sandbox metauniverse!
Chrome实现自动化测试:录制回放网页动作
Centos7 online MySQL installation
随机推荐
连接远程服务器的vscode无法格式化代码/文档(已解决)
Es installation & IK Chinese parser
MQTT5.0新特性(比对3.1.1)
(pytorch advanced road VI) implementation of swing transformer
[终端_1]-Xshell 5 最火的终端软件!
解析惠及中小学校的Steam教育
Design and implementation of tcp/ip protocol stack LwIP: Part VI
KY novel collection rules (5 collection rules)
Leetcode sword finger offer II 061 And the minimum K number pairs***
软件资源大全
JS get the server IP, port and protocol
46:第四章:开发文件服务:7:在【files】文件服务中,整合阿里OSS;
Unity learning notes - Hot update related
【微信小程序】解决代码上传超过大小限制,小程序分包
Role of subnet mask
mysql docker安装 主从部署
Diffusion Model
TCP/IP协议栈Lwip的设计与实现:之五
LeetCode 1928. 规定时间内到达终点的最小花费
Selenium被检测为爬虫,怎么屏蔽和绕过