mirror of
https://github.com/Estom/notes.git
synced 2026-04-05 03:48:56 +08:00
netowkx和pyg的教程
This commit is contained in:
283
Python/networkx/0 基础使用.md
Normal file
283
Python/networkx/0 基础使用.md
Normal file
@@ -0,0 +1,283 @@
|
||||
# 教程
|
||||
|
||||
> 参考文献
|
||||
> * [官方文档](https://www.osgeo.cn/networkx/tutorial.html)
|
||||
|
||||
## 0 简单介绍
|
||||
|
||||
### 图的类型
|
||||
|
||||
* Graph类是无向图的基类,无向图能有自己的属性或参数,不包含重边,允许有回路,节点可以是任何hash的python对象,节点和边可以保存key/value属性对。该类的构造函数为Graph(data=None,**attr),其中data可以是边列表,或任意一个Networkx的图对象,默认为none;attr是关键字参数,例如key=value对形式的属性。
|
||||
* MultiGraph是可以有重边的无向图,其它和Graph类似。其构造函数MultiGraph(data=None, *attr)。
|
||||
* DiGraph是有向图的基类,有向图可以有数自己的属性或参数,不包含重边,允许有回路;节点可以是任何hash的python对象,边和节点可含key/value属性对。该类的构造函数DiGraph(data=None,**attr),其中data可以是边列表,或任意一个Networkx的图对象,默认为none;attr是关键字参数,例如key=value对形式的属性。
|
||||
* MultiDiGraph是可以有重边的有向图,其它和DiGraph类似。其构造函数MultiDiGraph(data=None, *attr)
|
||||
|
||||
|
||||
### 有向图
|
||||
|
||||
|
||||
这个 DiGraph 提供了附加的属性,DiGraph.out_edges , DiGraph.in_degree , DiGraph.predecessors() , DiGraph.successors() 等。为了使算法能够轻松地与两个类一起工作,有向版本的 neighbors() 等于 successors() 虽然 degree 报告 in_degree 和 out_degree 尽管有时会觉得不一致。
|
||||
|
||||
```py
|
||||
DG = nx.DiGraph()
|
||||
DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)])
|
||||
DG.out_degree(1, weight='weight')
|
||||
0.5
|
||||
DG.degree(1, weight='weight')
|
||||
1.25
|
||||
list(DG.successors(1))
|
||||
[2]
|
||||
list(DG.neighbors(1))
|
||||
[2]
|
||||
```
|
||||
|
||||
有向图和无向图的转换
|
||||
|
||||
```py
|
||||
Graph.to_undirected()
|
||||
H = nx.Graph(G) # create an undirected graph H from a directed graph G
|
||||
```
|
||||
|
||||
|
||||
### 多重图
|
||||
|
||||
多重图
|
||||
NetworkX为允许任意节点对之间存在多个边的图形提供类。这个 MultiGraph 和 MultiDiGraph 类允许您两次添加相同的边缘,可能使用不同的边缘数据。这对某些应用程序来说可能很强大,但许多算法在此类图上没有很好的定义。如果结果定义明确,例如: MultiGraph.degree() 我们提供功能。否则,您应该以一种使测量定义良好的方式转换为标准图。
|
||||
|
||||
```py
|
||||
MG = nx.MultiGraph()
|
||||
MG.add_weighted_edges_from([(1, 2, 0.5), (1, 2, 0.75), (2, 3, 0.5)])
|
||||
dict(MG.degree(weight='weight'))
|
||||
{1: 1.25, 2: 1.75, 3: 0.5}
|
||||
GG = nx.Graph()
|
||||
for n, nbrs in MG.adjacency():
|
||||
for nbr, edict in nbrs.items():
|
||||
minvalue = min([d['weight'] for d in edict.values()])
|
||||
GG.add_edge(n, nbr, weight = minvalue)
|
||||
|
||||
nx.shortest_path(GG, 1, 3)
|
||||
[1, 2, 3]
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 1 增加
|
||||
|
||||
### 创建图形
|
||||
创建一个没有节点和边的空图形。
|
||||
|
||||
```py
|
||||
>>>
|
||||
import networkx as nx
|
||||
G = nx.Graph()
|
||||
```
|
||||
根据定义,a Graph 是一组节点(顶点)和已识别的节点对(称为边、链接等)的集合。在NetworkX中,节点可以是任何 hashable 对象,例如文本字符串、图像、XML对象、另一个图形、自定义节点对象等
|
||||
|
||||
|
||||
### 添加节点
|
||||
|
||||
图 G 可以通过多种方式生长。NetworkX包括许多图形生成器功能和以多种格式读取和写入图形的工具。我们先来看看简单的操作。
|
||||
|
||||
* 一次添加一个节点,
|
||||
|
||||
```py
|
||||
G.add_node(1)
|
||||
```
|
||||
|
||||
* 从任何 iterable 容器,如列表
|
||||
|
||||
```py
|
||||
G.add_nodes_from([2, 3])
|
||||
```
|
||||
|
||||
* 如果容器产生2个元组形式,还可以添加节点和节点属性 (node, node_attribute_dict) ::
|
||||
|
||||
```py
|
||||
G.add_nodes_from([
|
||||
(4, {"color": "red"}),
|
||||
(5, {"color": "green"}),
|
||||
])
|
||||
```
|
||||
|
||||
* 一个图中的节点可以合并到另一个图中
|
||||
|
||||
```py
|
||||
H = nx.path_graph(10)
|
||||
G.add_nodes_from(H)
|
||||
```
|
||||
|
||||
* G 现在包含的节点 H 作为节点 G . 相反,您可以使用图表 H 作为一个节点 G .
|
||||
```
|
||||
G.add_node(H)
|
||||
```
|
||||
|
||||
|
||||
### 添加边缘
|
||||
|
||||
* G 也可以通过一次添加一个边来生长,
|
||||
|
||||
```
|
||||
G.add_edge(1, 2)
|
||||
e = (2, 3)
|
||||
G.add_edge(*e) # unpack edge tuple*
|
||||
```
|
||||
|
||||
* 通过添加边列表,
|
||||
|
||||
```
|
||||
G.add_edges_from([(1, 2), (1, 3)])
|
||||
```
|
||||
|
||||
* 我们添加新的节点/边缘,忽略已经存在的节点和边
|
||||
```
|
||||
>>>
|
||||
G.add_edges_from([(1, 2), (1, 3)])
|
||||
G.add_node(1) # 已经存在1节点,不再重复添加
|
||||
G.add_edge(1, 2) # 已经存在1,2边,不再重复添加
|
||||
G.add_node("spam") # adds node "spam"
|
||||
G.add_nodes_from("spam") # adds 4 nodes: 's', 'p', 'a', 'm'
|
||||
G.add_edge(3, 'm')
|
||||
```
|
||||
|
||||
* 在这个阶段,图表 G 由8个节点和3个边组成,如下所示:
|
||||
|
||||
```py
|
||||
>>>
|
||||
G.number_of_nodes()
|
||||
8
|
||||
G.number_of_edges()
|
||||
3
|
||||
```
|
||||
|
||||
## 2 删除
|
||||
### 删除元素
|
||||
可以用与添加类似的方式从图形中删除节点和边。
|
||||
* Graph.remove_node()
|
||||
* Graph.remove_nodes_from()
|
||||
* Graph.remove_edge()
|
||||
* Graph.remove_edges_from()
|
||||
|
||||
```
|
||||
>>>
|
||||
G.remove_node(2)
|
||||
G.remove_nodes_from("spam")
|
||||
list(G.nodes)
|
||||
[1, 3, 'spam']
|
||||
G.remove_edge(1, 3)
|
||||
```
|
||||
|
||||
|
||||
### 构造函数
|
||||
图形对象不必以增量方式构建——指定图形结构的数据可以直接传递给各种图形类的构造函数。当通过实例化一个图形类来创建一个图结构时,可以用几种格式指定数据。
|
||||
|
||||
```
|
||||
G.add_edge(1, 2)
|
||||
H = nx.DiGraph(G) # create a DiGraph using the connections from G
|
||||
list(H.edges())
|
||||
[(1, 2), (2, 1)]
|
||||
edgelist = [(0, 1), (1, 2), (2, 3)]
|
||||
H = nx.Graph(edgelist)
|
||||
```
|
||||
|
||||
## 3 查找
|
||||
### 访问边缘和邻居
|
||||
* 除了视图 Graph.edges 和 Graph.adj ,可以使用下标表示法访问边和邻居。
|
||||
|
||||
```py
|
||||
G = nx.Graph([(1, 2, {"color": "yellow"})])
|
||||
G[1] # same as G.adj[1]
|
||||
AtlasView({2: {'color': 'yellow'}})
|
||||
G[1][2]
|
||||
{'color': 'yellow'}
|
||||
G.edges[1, 2]
|
||||
{'color': 'yellow'}
|
||||
```
|
||||
|
||||
* 如果边已经存在,可以使用下标表示法获取/设置边的属性。
|
||||
|
||||
```py
|
||||
G.add_edge(1, 3)
|
||||
G[1][3]['color'] = "blue"
|
||||
G.edges[1, 2]['color'] = "red"
|
||||
G.edges[1, 2]
|
||||
{'color': 'red'}
|
||||
```
|
||||
|
||||
* 使用 G.adjacency() 或 G.adj.items() . 注意,对于无向图,邻接迭代可以看到每个边两次。
|
||||
|
||||
```py
|
||||
FG = nx.Graph()
|
||||
FG.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2), (3, 4, 0.375)])
|
||||
for n, nbrs in FG.adj.items():
|
||||
for nbr, eattr in nbrs.items():
|
||||
wt = eattr['weight']
|
||||
if wt < 0.5: print(f"({n}, {nbr}, {wt:.3})")
|
||||
(1, 2, 0.125)
|
||||
(2, 1, 0.125)
|
||||
(3, 4, 0.375)
|
||||
(4, 3, 0.375)
|
||||
```
|
||||
|
||||
* 使用边缘属性可以方便地访问所有边缘。
|
||||
|
||||
```py
|
||||
for (u, v, wt) in FG.edges.data('weight'):
|
||||
if wt < 0.5:
|
||||
print(f"({u}, {v}, {wt:.3})")
|
||||
(1, 2, 0.125)
|
||||
(3, 4, 0.375)
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 4 修改
|
||||
向图形、节点和边添加属性
|
||||
|
||||
诸如权重、标签、颜色或任何您喜欢的python对象等属性都可以附加到图形、节点或边上。
|
||||
|
||||
每个图、节点和边都可以在关联的属性字典中保存键/值属性对(键必须是可哈希的)。默认情况下,这些属性为空,但可以使用 add_edge , add_node 或直接操作命名的属性字典 G.graph , G.nodes 和 G.edges 对于图 G .
|
||||
|
||||
### 图形属性
|
||||
|
||||
创建新图形时分配图形属性
|
||||
|
||||
```
|
||||
G = nx.Graph(day="Friday")
|
||||
G.graph
|
||||
{'day': 'Friday'}
|
||||
```
|
||||
或者您可以稍后修改属性
|
||||
|
||||
```
|
||||
G.graph['day'] = "Monday"
|
||||
G.graph
|
||||
{'day': 'Monday'}
|
||||
```
|
||||
### 节点属性
|
||||
使用添加节点属性 add_node() , add_nodes_from() 或 G.nodes
|
||||
|
||||
```py
|
||||
G.add_node(1, time='5pm')
|
||||
G.add_nodes_from([3], time='2pm')
|
||||
G.nodes[1]
|
||||
{'time': '5pm'}
|
||||
G.nodes[1]['room'] = 714
|
||||
G.nodes.data()
|
||||
NodeDataView({1: {'time': '5pm', 'room': 714}, 3: {'time': '2pm'}})
|
||||
```
|
||||
|
||||
|
||||
请注意,将节点添加到 G.nodes 不将其添加到图表中,使用 G.add_node() 添加新节点。同样适用于边缘。
|
||||
|
||||
### 边缘属性
|
||||
使用添加/更改边缘属性 add_edge() , add_edges_from() 或下标符号。
|
||||
|
||||
```py
|
||||
G.add_edge(1, 2, weight=4.7 )
|
||||
G.add_edges_from([(3, 4), (4, 5)], color='red')
|
||||
G.add_edges_from([(1, 2, {'color': 'blue'}), (2, 3, {'weight': 8})])
|
||||
G[1][2]['weight'] = 4.7
|
||||
G.edges[3, 4]['weight'] = 4.2
|
||||
```
|
||||
|
||||
特殊属性 weight 应该是数字,因为它被需要加权边缘的算法使用。
|
||||
53
Python/networkx/1 图的视图.md
Normal file
53
Python/networkx/1 图的视图.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# 图的视图
|
||||
|
||||
## 视图
|
||||
### 概述
|
||||
* 各种视图提供了图上节点、边、邻接矩阵和度本身的各种基础方法。这些视图提供对属性的迭代,以及成员查询和数据属性查找。
|
||||
* 视图引用图形数据结构,因此对图形的更改反映在视图中。这类似于Python3中的字典视图。是一种图表示的数据结构效率非常高,全是字典。
|
||||
* 如果要在迭代时更改图形,则需要使用例如 for e in list(G.edges): .
|
||||
* 视图提供了类似集合的操作,例如联合和交集,
|
||||
* 视图提供了类似dict的数据属性查找和迭代,使用 G.edges[u, v]['color'] 和 for e, datadict in G.edges.items(): . 方法 G.edges.items() 和 G.edges.values()
|
||||
|
||||
|
||||
## 2 常见的视图
|
||||
|
||||
```py
|
||||
__all__ = [
|
||||
"NodeView", #只包含节点的视图
|
||||
"NodeDataView", # 包含节点和属性的视图
|
||||
|
||||
"EdgeView", # 只保函边的视图
|
||||
"OutEdgeView",
|
||||
"InEdgeView",
|
||||
"EdgeDataView", # 包含边和属性的视图
|
||||
"OutEdgeDataView",
|
||||
"InEdgeDataView",
|
||||
|
||||
|
||||
"DegreeView", # 度的视图
|
||||
"DiDegreeView",
|
||||
"InDegreeView",
|
||||
"OutDegreeView",
|
||||
"MultiDegreeView",
|
||||
"DiMultiDegreeView",
|
||||
"InMultiDegreeView",
|
||||
"OutMultiDegreeView",
|
||||
|
||||
"adjacentView", # 邻接视图
|
||||
"AtlasView", # 邻接属性视图
|
||||
]
|
||||
```
|
||||
|
||||
### 节点视图
|
||||
|
||||
|
||||
### 边视图
|
||||
|
||||
### 邻接视图
|
||||
|
||||
### 度视图
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
225
Python/networkx/1.ipynb
Normal file
225
Python/networkx/1.ipynb
Normal file
@@ -0,0 +1,225 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import networkx as nx\n",
|
||||
"G = nx.Graph()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"G.add_nodes_from([1,2,3])\n",
|
||||
"G.add_edge(3,4)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1, 2, 3, 4]\n",
|
||||
"[(3, 4)]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(G.nodes())\n",
|
||||
"print(G.edges())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"<ipython-input-20-2623cf771fd5>:3: UserWarning: Matplotlib is currently using ps, which is a non-GUI backend, so cannot show the figure.\n",
|
||||
" plt.show()\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"nx.draw(G)\n",
|
||||
"plt.show()\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"(1, 2, 0.125)\n",
|
||||
"(2, 1, 0.125)\n",
|
||||
"(3, 4, 0.375)\n",
|
||||
"(4, 3, 0.375)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"FG = nx.Graph()\n",
|
||||
"FG.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2), (3, 4, 0.375)])\n",
|
||||
"for n, nbrs in FG.adj.items():\n",
|
||||
" for nbr, eattr in nbrs.items():\n",
|
||||
" wt = eattr['weight']\n",
|
||||
" if wt < 0.5: print(f\"({n}, {nbr}, {wt:.3})\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"(1, 2, 0.125)\n",
|
||||
"(3, 4, 0.375)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for (u, v, wt) in FG.edges.data('weight'):\n",
|
||||
" if wt < 0.5:\n",
|
||||
" print(f\"({u}, {v}, {wt:.3})\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"ItemsView(AdjacencyView({1: {2: {'weight': 0.5}}, 2: {}, 3: {1: {'weight': 0.75}}}))\n",
|
||||
"[3]\n",
|
||||
"[2]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"DG = nx.DiGraph()\n",
|
||||
"DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)])\n",
|
||||
"DG.out_degree(1, weight='weight')\n",
|
||||
"print(DG.adj.items())\n",
|
||||
"print(list(DG.predecessors(1)))\n",
|
||||
"print(list(DG.successors(1)))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[0 1 0 1 0 0 1 1 0 0]\n",
|
||||
" [1 0 1 1 1 1 0 1 0 1]\n",
|
||||
" [0 1 0 0 1 1 1 1 0 1]\n",
|
||||
" [0 0 1 0 0 0 0 1 1 1]\n",
|
||||
" [1 0 0 1 1 1 0 1 1 1]\n",
|
||||
" [0 0 0 0 0 0 0 0 1 1]\n",
|
||||
" [0 0 1 0 0 0 1 1 1 1]\n",
|
||||
" [1 0 1 0 0 1 1 0 0 0]\n",
|
||||
" [1 1 1 1 1 0 1 0 0 0]\n",
|
||||
" [1 1 1 0 0 0 0 0 0 1]]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"a = np.random.randint(0, 2, size=(10, 10))\n",
|
||||
"print(a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"D = nx.DiGraph(a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 40,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0., 1., 0., 1., 0., 0., 1., 1., 0., 0.],\n",
|
||||
" [1., 0., 1., 1., 1., 1., 0., 1., 0., 1.],\n",
|
||||
" [0., 1., 0., 0., 1., 1., 1., 1., 0., 1.],\n",
|
||||
" [0., 0., 1., 0., 0., 0., 0., 1., 1., 1.],\n",
|
||||
" [1., 0., 0., 1., 1., 1., 0., 1., 1., 1.],\n",
|
||||
" [0., 0., 0., 0., 0., 0., 0., 0., 1., 1.],\n",
|
||||
" [0., 0., 1., 0., 0., 0., 1., 1., 1., 1.],\n",
|
||||
" [1., 0., 1., 0., 0., 1., 1., 0., 0., 0.],\n",
|
||||
" [1., 1., 1., 1., 1., 0., 1., 0., 0., 0.],\n",
|
||||
" [1., 1., 1., 0., 0., 0., 0., 0., 0., 1.]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 40,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"D.nodes()\n",
|
||||
"D.edges()\n",
|
||||
"nx.to_numpy_array(D)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"interpreter": {
|
||||
"hash": "5ef0042cb263260037aa2928643ae94e240dd3afaec7872ebebe4f07619ddd0c"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.8.8 ('ml')",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.8"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
69
Python/networkx/2 图生成器.md
Normal file
69
Python/networkx/2 图生成器.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# 图形生成器和图形操作
|
||||
|
||||
## 1 应用经典图形操作
|
||||
|
||||
|
||||
| subgraph \(G ,N启动) | 返回在nbunch中的节点上诱导的子图。 |
|
||||
|---|---|
|
||||
| union (g,h) [, rename, name] ) | 返回图g和h的并集。 |
|
||||
| disjoint_union (g,h) | 返回图G和图H的不相交的并集。 |
|
||||
| cartesian_product (g,h) | 返回g和h的笛卡尔积。 |
|
||||
| compose (g,h) | 返回由h组成的g的新图。 |
|
||||
| complement (g) | 返回g的图补。 |
|
||||
| create_empty_copy (g) [, with_data] ) | 返回图形G的副本,并删除所有边。 |
|
||||
| to_undirected [(图)] | 返回图表的无向视图 graph . |
|
||||
| to_directed [(图)] | 返回图形的定向视图 graph . |
|
||||
|
||||
|
||||
## 2 经典生成小图
|
||||
|
||||
|
||||
| petersen_graph \ [create_using] ) | 返回彼得森图。 |
|
||||
|---|---|
|
||||
| tutte_graph \ [create_using] ) | 返回图特图。 |
|
||||
| sedgewick_maze_graph \ [create_using] ) | 返回一个带有循环的小迷宫。 |
|
||||
| tetrahedral_graph \ [create_using] ) | 返回3-正则柏拉图四面体图。 |
|
||||
|
||||
|
||||
## 3 对经典图形使用生成器
|
||||
|
||||
|
||||
| complete_graph n(n) [, create_using] ) | 返回完整图形 K_n 具有n个节点。 |
|
||||
|---|---|
|
||||
| complete_bipartite_graph (N1,N2) [, create_using] ) | 返回完整的二部图 K_{{n_1,n_2}} . |
|
||||
| barbell_graph (M1,M2) [, create_using] ) | 返回杠铃图:由路径连接的两个完整图。 |
|
||||
| lollipop_graph (m,n) [, create_using] ) | 返回棒棒糖图; K_m 连接到 P_n . |
|
||||
|
||||
|
||||
```py
|
||||
K_5 = nx.complete_graph(5)
|
||||
K_3_5 = nx.complete_bipartite_graph(3, 5)
|
||||
barbell = nx.barbell_graph(10, 10)
|
||||
lollipop = nx.lollipop_graph(10, 20)
|
||||
```
|
||||
|
||||
|
||||
## 4 使用随机图形生成器
|
||||
|
||||
|
||||
| erdos_renyi_graph (n,p) [, seed, directed] ) | 返回一个$G{n,p}$随机图,也称为Erdős-Rényi图或二项式图。 |
|
||||
|---|---|
|
||||
| watts_strogatz_graph (n,k,p) [, seed] ) | 返回Watts–Strogaz小世界图。 |
|
||||
| barabasi_albert_graph (n,m) [, seed] ) | 根据barab_si–albert优先连接模型返回随机图。 |
|
||||
| random_lobster \(N、P1、P2)[, seed] ) | 返回随机龙虾图。 |
|
||||
|
||||
|
||||
```py
|
||||
er = nx.erdos_renyi_graph(100, 0.15)
|
||||
ws = nx.watts_strogatz_graph(30, 3, 0.1)
|
||||
ba = nx.barabasi_albert_graph(100, 5)
|
||||
red = nx.random_lobster(100, 0.9, 0.9)
|
||||
```
|
||||
|
||||
使用常用的图形格式读取存储在文件中的图形,如边列表、邻接列表、gml、graphml、pickle、leda等。
|
||||
|
||||
```
|
||||
nx.write_gml(red, "path.to.file")
|
||||
mygraph = nx.read_gml("path.to.file")
|
||||
```
|
||||
有关图形格式的详细信息,请参见 读写图表 关于图形生成器函数,请参见 图形生成器
|
||||
30
Python/networkx/3 分析图形.md
Normal file
30
Python/networkx/3 分析图形.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# 分析图形
|
||||
|
||||
|
||||
|
||||
## 基础处理
|
||||
|
||||
分析图形的结构 G 可以使用各种图论函数进行分析,例如:
|
||||
|
||||
```py
|
||||
G = nx.Graph()
|
||||
G.add_edges_from([(1, 2), (1, 3)])
|
||||
G.add_node("spam") # adds node "spam"
|
||||
list(nx.connected_components(G))
|
||||
[{1, 2, 3}, {'spam'}]
|
||||
sorted(d for n, d in G.degree())
|
||||
[0, 1, 1, 2]
|
||||
nx.clustering(G)
|
||||
{1: 0, 2: 0, 3: 0, 'spam': 0}
|
||||
```
|
||||
|
||||
一些具有大输出的函数迭代(节点、值)2元组。这些很容易存储在 dict 结构,如果你愿意的话。
|
||||
|
||||
```py
|
||||
sp = dict(nx.all_pairs_shortest_path(G))
|
||||
sp[3]
|
||||
{3: [3], 1: [3, 1], 2: [3, 1, 2]}
|
||||
```
|
||||
|
||||
## 图算法
|
||||
|
||||
22
Python/networkx/4 绘制图形.md
Normal file
22
Python/networkx/4 绘制图形.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# 图形绘制
|
||||
|
||||
|
||||
networkx主要不是一个图形绘制包,而是一个带有matplotlib的基本绘图,以及一个使用开源graphviz软件包的接口。这些是 networkx.drawing 模块,如果可能,将导入。
|
||||
|
||||
首先导入Matplotlib的绘图接口(Pylab也可以工作)
|
||||
|
||||
```py
|
||||
import matplotlib.pyplot as plt
|
||||
```
|
||||
|
||||
测试是否导入 networkx.drawing 抽签成功 G 使用其中之一
|
||||
|
||||
```
|
||||
G = nx.petersen_graph()
|
||||
plt.subplot(121)
|
||||
<matplotlib.axes._subplots.AxesSubplot object at ...>
|
||||
nx.draw(G, with_labels=True, font_weight='bold')
|
||||
plt.subplot(122)
|
||||
<matplotlib.axes._subplots.AxesSubplot object at ...>
|
||||
nx.draw_shell(G, nlist=[range(5, 10), range(5)], with_labels=True, font_weight='bold')
|
||||
```
|
||||
77
Python/networkx/5 导入导出.md
Normal file
77
Python/networkx/5 导入导出.md
Normal file
@@ -0,0 +1,77 @@
|
||||
读写图表
|
||||
邻接表
|
||||
邻接表
|
||||
networkx.readwrite.adjlist.read_adjlist
|
||||
networkx.readwrite.adjlist.write_adjlist
|
||||
networkx.readwrite.adjlist.parse_adjlist
|
||||
networkx.readwrite.adjlist.generate_adjlist
|
||||
多行邻接列表
|
||||
多行相邻列表
|
||||
networkx.readwrite.multiline_adjlist.read_multiline_adjlist
|
||||
networkx.readwrite.multiline_adjlist.write_multiline_adjlist
|
||||
networkx.readwrite.multiline_adjlist.parse_multiline_adjlist
|
||||
networkx.readwrite.multiline_adjlist.generate_multiline_adjlist
|
||||
边缘列表
|
||||
边缘列表
|
||||
networkx.readwrite.edgelist.read_edgelist
|
||||
networkx.readwrite.edgelist.write_edgelist
|
||||
networkx.readwrite.edgelist.read_weighted_edgelist
|
||||
networkx.readwrite.edgelist.write_weighted_edgelist
|
||||
networkx.readwrite.edgelist.generate_edgelist
|
||||
networkx.readwrite.edgelist.parse_edgelist
|
||||
GEXF
|
||||
格式
|
||||
networkx.readwrite.gexf.read_gexf
|
||||
networkx.readwrite.gexf.write_gexf
|
||||
networkx.readwrite.gexf.generate_gexf
|
||||
networkx.readwrite.gexf.relabel_gexf_graph
|
||||
GML
|
||||
networkx.readwrite.gml.read_gml
|
||||
networkx.readwrite.gml.write_gml
|
||||
networkx.readwrite.gml.parse_gml
|
||||
networkx.readwrite.gml.generate_gml
|
||||
networkx.readwrite.gml.literal_destringizer
|
||||
networkx.readwrite.gml.literal_stringizer
|
||||
泡菜
|
||||
腌渍图
|
||||
networkx.readwrite.gpickle.read_gpickle
|
||||
networkx.readwrite.gpickle.write_gpickle
|
||||
图形ML
|
||||
图形ML
|
||||
networkx.readwrite.graphml.read_graphml
|
||||
networkx.readwrite.graphml.write_graphml
|
||||
networkx.readwrite.graphml.generate_graphml
|
||||
networkx.readwrite.graphml.parse_graphml
|
||||
JSON
|
||||
JSON数据
|
||||
networkx.readwrite.json_graph.node_link_data
|
||||
networkx.readwrite.json_graph.node_link_graph
|
||||
networkx.readwrite.json_graph.adjacency_data
|
||||
networkx.readwrite.json_graph.adjacency_graph
|
||||
networkx.readwrite.json_graph.cytoscape_data
|
||||
networkx.readwrite.json_graph.cytoscape_graph
|
||||
networkx.readwrite.json_graph.tree_data
|
||||
networkx.readwrite.json_graph.tree_graph
|
||||
networkx.readwrite.json_graph.jit_data
|
||||
networkx.readwrite.json_graph.jit_graph
|
||||
LEDA
|
||||
格式
|
||||
networkx.readwrite.leda.read_leda
|
||||
networkx.readwrite.leda.parse_leda
|
||||
YAML
|
||||
YAML
|
||||
networkx.readwrite.nx_yaml.read_yaml
|
||||
networkx.readwrite.nx_yaml.write_yaml
|
||||
闪光灯6
|
||||
图形6
|
||||
SARSE6
|
||||
帕吉克
|
||||
帕吉克
|
||||
networkx.readwrite.pajek.read_pajek
|
||||
networkx.readwrite.pajek.write_pajek
|
||||
networkx.readwrite.pajek.parse_pajek
|
||||
networkx.readwrite.pajek.generate_pajek
|
||||
地理信息系统
|
||||
整形器
|
||||
networkx.readwrite.nx_shp.read_shp
|
||||
networkx.readwrite.nx_shp.write_shp
|
||||
Reference in New Issue
Block a user