EDITORIAL: Urban computing studies often involve 图论
time-related knowledge, and often the face of certain terms, do not know what to say recently contacted NetworkX this graph processing tool, this tool has been found to solve most of the. 图论
Problems (I think that just maybe, there is no evidence), so the next use this tool to learn, to learn the way 图论
of knowledge.
Create a chart
import networkx as nx
G = nx.Graph()
node
Add one node
G.add_node(1)
Add a list of nodes
G.add_nodes_from([2, 3])
side
One can add an edge to grow
G.add_edge(1, 2)
e = (2, 3)
G.add_edge(*e) # unpack edge tuple*
It can also be side list by adding
G.add_edges_from([(1, 2), (1, 3)])
Figure delete all nodes and edges
G.clear()
We add a new node / edge, and NetworkX will automatically ignore any node that already exists.
G.add_edges_from([(1, 2), (1, 3)])
G.add_node(1)
G.add_edge(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')
In this stage, the pattern G
consists of eight nodes, and three sides, as shown below:
>>> G.number_of_nodes()
8
>>> G.number_of_edges()
3
We can check nodes and edges. Four basic graphic G.nodes
attributes: G.edges
, , G.adj
and G.degree
. These views are nodes, edges, Neighbor (adjacent point) and extent of the sets of nodes in the graph.
>>> list(G.nodes)
['a', 1, 2, 3, 'spam', 'm', 'p', 's']
>>> list(G.edges)
[(1, 2), (1, 3), (3, 'm')]
>>> list(G.adj[1]) # or list(G.neighbors(1))
[2, 3]
>>> G.degree[1] # the number of edges incident to 1
2
May be removed from the nodes and edges in a manner similar to FIG added. Use Graph.remove_node()
, Graph.remove_nodes_from()
, Graph.remove_edge()
and Graph.remove_edges_from()
, as
>>> 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)
访问边和节点邻居
除了视图之外Graph.edges()
,Graph.adj()
还可以使用下标符号来访问边和邻居。
>>> G[1] # same as G.adj[1]
AtlasView({2: {}})
>>> G[1][2]
{}
>>> G.edges[1, 2]
{}
如果边已经存在,可以使用下标符号来获取/设置边的属性。
>>> G.add_edge(1, 3)
>>> G[1][3]['color'] = "blue"
>>> G.edges[1, 2]['color'] = "red"
所有(节点,邻接节点)的快速查询都是使用 G.adjacency()
或G.adj.items()完成的
。请注意,对于无向图,邻接迭代会将每个边看两次。
>>> 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('(%d, %d, %.3f)' % (n, nbr, wt))
(1, 2, 0.125)
(2, 1, 0.125)
(3, 4, 0.375)
(4, 3, 0.375)
通过边属性可以方便地访问所有的边。
>>> for (u, v, wt) in FG.edges.data('weight'):
... if wt < 0.5: print('(%d, %d, %.3f)' % (u, v, wt))
(1, 2, 0.125)
(3, 4, 0.375)
将属性添加到图形,节点和边
属性(如权重,标签,颜色或任何您喜欢的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
>>> 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: {'room': 714, 'time': '5pm'}, 3: {'time': '2pm'}})
边属性
添加/更改边使用的属性add_edge()
,add_edges_from()
或标符号。
>>> 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
有向图:DiGraph()
DiGraph类提供特定于有向边的附加属性,例如DiGraph.out_edges()
,DiGraph.in_degree()
,DiGraph.predecessors()
,DiGraph.successors()
等。为了使算法能够轻松地处理这两个类, neighbor()
的功能等同于successors()
,而degree
会报告in_degree
和out_degree
的总和,即使有时可能会感觉不一致
dg = nx.DiGraph()
nodes1 = [
('Variable', {'name': 'avariable', 'table': 'tablename'}),
('Select', {'conditions': {'pro_code': 44}}),
('GroupBy', {'varname': 'gender'}),
('Mean', {}),
('Which1', {'level': 1}),
('Decimal1', {'place': 1}),
]
nodes2 = [
('Which1', {'level': 2}),
('Decimal2', {'place': 1}),
]
nodes3 = [
('Add', {})
]
dg.add_nodes_from(nodes1)
dg.add_nodes_from(nodes2)
dg.add_nodes_from(nodes3)
dg.add_edges_from([
('Variable', 'Select'),
('Select', 'GroupBy'),
('GroupBy', 'Mean'),
('Mean', 'Which1'),
('Mean', 'Which2'),
('Which1', 'Decimal1'),
('Which2', 'Decimal2'),
('Decimal1', 'Add'),
('Decimal2', 'Add'),
])
nx.draw(dg, with_labels=True)