使用Python,networkx构造有向图及无向图以及图合并等api
方法名 | 方法作用 |
---|
subgraph(G, nbunch) | 返回包含nbunch节点的子图 |
union(G, H[, rename]) | 合并G和H图 |
disjoint_union(G, H) | 合并G和H图 |
cartesian_product(G, H) | 返回G和H的笛卡尔积 |
compose(G, H) | 组合G和H图,通过把节点和边都合并到一个图中 |
complement(G) | 返回G图的全集 |
create_empty_copy(G[, with_data]) | 返回G的复制图,包括所有节点,删除全部边 |
to_undirected(graph) | 转换G图为无向图 |
to_directed(graph) | 转换G图为有向图 |
源码
图的构造、节点及边的添加等
'''
networkx绘制图相关api
'''import networkx as nx
from networkx.classes.coreviews import AtlasView
from networkx.classes.reportviews import NodeDataView
G = nx.Graph()
G.add_node(1)
G.add_nodes_from([2, 3])
G.add_nodes_from([(4, {"color": "red"}), (5, {"color": "green"})])
H = nx.path_graph(10)
G.add_nodes_from(H)
G.add_edge(1, 2)
e = (2, 3)
G.add_edge(*e)
G.add_edges_from([(1, 2), (1, 3)])
G.add_edges_from(H.edges)
G.clear()
G.add_edges_from([(1, 2), (1, 3)])
G.add_node(1)
G.add_edge(1, 2)
G.add_node("spam")
G.add_nodes_from("spam")
G.add_edge(3, 'm')print('num of nodes: ', str(G.number_of_nodes()))
print('num of edges: ', str(G.number_of_edges()))
DG = nx.DiGraph()
DG.add_edge(2, 1)
DG.add_edge(1, 3)
DG.add_edge(2, 4)
DG.add_edge(1, 2)
assert list(DG.successors(2)) == [1, 4]
assert list(DG.edges) == [(2, 1), (2, 4), (1, 3), (1, 2)]
print(list(G.nodes))
print(list(G.edges))
print(list(G.adj[1]))
print(G.degree[1])
print(G.edges([2, 'm']))
print(G.degree([2, 3]))
G.remove_node(2)
G.remove_nodes_from("spam")
print('after del: ', list(G.nodes))
G.remove_edge(1, 3)
print('after del: ', list(G.edges))
G.add_edge(1, 2)
H = nx.DiGraph(G)
print(list(H.edges()))
edgelist = [(0, 1), (1, 2), (2, 3)]
H = nx.Graph(edgelist)
print(list(H.edges()))
adjacency_dict = {0: (1, 2), 1: (0, 2), 2: (0, 1)}
H = nx.Graph(adjacency_dict)
print(list(H.edges()))
G = nx.Graph([(1, 2, {"color": "yellow"})])
print(G[1])
AtlasView({2: {'color': 'yellow'}})
print(G[1][2])
print(G.edges[1, 2])
G.add_edge(1, 3)
G[1][3]['color'] = "blue"
G.edges[1, 2]['color'] = "red"
print(G.edges[1, 2])
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})")
for (u, v, wt) in FG.edges.data('weight'):if wt < 0.5:print(f"\t({u}, {v}, {wt:.3})")
G = nx.Graph(day="Friday")
print(G.graph)
G.graph['day'] = "Monday"
print(G.graph)
G.add_node(1, time='5pm')
G.add_nodes_from([3], time='2pm')
print(G.nodes[1])
G.nodes[1]['room'] = 714
print(G.nodes.data())
NodeDataView({1: {'time': '5pm', 'room': 714}, 3: {'time': '2pm'}})
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
print(G.edges)
print(G.edges.data())
有向图及无向图及多重图
'''
有向图及无向图,以及一些公共api
'''
import networkx as nx
from networkx import to_directed, to_undirected, create_empty_copy, subgraph, union, disjoint_union, cartesian_product, \compose, complement
DG = nx.DiGraph()
DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)])
print(DG.out_degree(1, weight='weight'))
print(DG.degree(1, weight='weight'))
print(list(DG.successors(1)))
print(list(DG.neighbors(1)))
H = nx.Graph(DG)
MG = nx.MultiGraph()
MG.add_weighted_edges_from([(1, 2, 0.5), (1, 2, 0.75), (2, 3, 0.5)])
print(dict(MG.degree(weight='weight')))
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)print(nx.shortest_path(GG, 1, 3)) print(subgraph(GG, nbunch=2))
print('subgraph', list(subgraph(GG, nbunch=2)))graph1 = nx.Graph([(8, 9, {"color": "yellow"}), (9, 10, {"color": "red"})])
print(union(graph1, GG))
print('union', union(graph1, GG).edges.data(), '\n')graph1 = nx.Graph([(111, 222, {"color": "yellow"}), (222, 333, {"color": "red"}), (1, 2, {"color": "green"})])
print(disjoint_union(graph1, GG))
print('disjoint_union', disjoint_union(graph1, GG).edges.data(), '\n')graph1 = nx.Graph([(111, 222, {"color": "yellow"})])
print(cartesian_product(graph1, GG))
print('cartesian_product', cartesian_product(graph1, GG).edges.data(), '\n')print(compose(graph1, GG))
print('compose', compose(graph1, GG).edges.data(), '\n')print(complement(GG))
print('complement', complement(GG).edges.data(), '\n')print(to_directed(GG))
print('to_directed', to_directed(GG).edges.data(), '\n')print(to_undirected(GG))
print('to_undirected', to_undirected(GG).edges.data(), '\n')print(create_empty_copy(GG))
print('create_empty_copy', create_empty_copy(GG).nodes)
参考
- https://networkx.org/documentation/latest/tutorial.html#accessing-edges-and-neighbors