NetworkX是一个创建,操作,研究复杂网络的结构,动态,功能的python包。
#创建一个network import networkx as nx G = nx.Graph()
#nodes import networkx as nx G = nx.Graph() ''' 在networkx中,nodes可以是任何能够hash的对象, 例如a text string,an image,an XML object,another Graph,a customized node object等等''' G.add_node(11) G.add_nodes_from([12, 13]) print(G.nodes()) H = nx.path_graph(10) G.add_nodes_from(H) G.add_node(H) print(G.nodes()) ''' 输出: [11, 12, 13] [11, 12, 13, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, <networkx.classes.graph.Graph object at 0x00000000021C8828>] G可以将H中的node作为自己的node,也可以将H单独作为一个node'''
添加edges
import networkx as nx G = nx.Graph() '''G.add_edge(1, 2) e = (2, 3) G.add_edge(*e) G.add_edges_from([(4, 5), (6, 7)])''' ''' adding any ebunch of edges. An ebunch is any iterable container of edge-tuples. An edge-tuple can be a 2-tuple of nodes or a 3-tuple with 2 nodes followed by an edge attribute dictionary, e.g., (2, 3, {'weight': 3.1415})''' H = nx.path_graph(10) G.add_edges_from(H.edges()) print(G.nodes()) print(G.edges()) print(G.number_of_edges()) print(G.number_of_nodes()) ''' 输出: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)] 9 10 ''' print('..............') print(list(G.adj[1])) print(G.neighbors(1)) print(G.degree(1)) ''' 输出: [0, 2] [0, 2] 2 ''' print(G.edges([2, 5])) print(G.degree([2, 3])) ''' 输出: [(2, 1), (2, 3), (5, 4), (5, 6)] {2: 2, 3: 2} ''' G.remove_node(2) G.remove_edge(6, 7) print(G.nodes()) print(G.edges()) ''' 输出: [0, 1, 3, 4, 5, 6, 7, 8, 9] [(0, 1), (3, 4), (4, 5), (5, 6), (7, 8), (8, 9)] ''' G.add_edge(6, 7) H = nx.DiGraph(G) print(H.edges()) ''' [(0, 1), (1, 0), (3, 4), (4, 3), (4, 5), (5, 4), (5, 6), (6, 5), (6, 7), (7, 8), (7, 6), (8, 7), (8, 9), (9, 8)] ''' edgelist = [(0, 1), (2, 3), (4, 5)] H = nx.Graph(edgelist) print(H.edges()) ''' [(0, 1), (2, 3), (4, 5)] '''
访问edges或neighbors:
#访问edges或neighbors import networkx as nx G = nx.Graph() H = nx.path_graph(7) G.add_edges_from(H.edges()) print('G.nodes()为:', G.nodes()) print('G.edges()为:', G.edges()) ''' G.nodes()为: [0, 1, 2, 3, 4, 5, 6] G.edges()为: [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] ''' print('...............') print('G[1]为:', G[1]) print('G[1][2]为:', G[1][2]) ''' G[1]为: {0: {}, 2: {}} G[1][2]为: {} ''' G.add_edge(1, 3) G[1][3]['color'] = 'blue' G[1][3]['size'] = 22 print('...............') print('G[1]为:', G[1]) print('G[1][3]为:', G[1][3]) ''' G[1]为: {0: {}, 2: {}, 3: {'color': 'blue', 'size': 22}} G[1][3]为: {'color': 'blue', 'size': 22}''' print('...................') print('G.adj.items()为: ', G.adj.items()) print('G.adjacency_list()为: ',G.adjacency_list()) print('G.adjlist_dict_factory为: ', G.adjlist_dict_factory) ''' G.adj.items()为: dict_items([(0, {1: {}}), (1, {0: {}, 2: {}, 3: {'color': 'blue', 'size': 22}}), (2, {1: {}, 3: {}}), (3, {2: {}, 4: {}, 1: {'color': 'blue', 'size': 22}}), (4, {3: {}, 5: {}}), (5, {4: {}, 6: {}}), (6, {5: {}})]) G.adjacency_list()为: [[1], [0, 2, 3], [1, 3], [2, 4, 1], [3, 5], [4, 6], [5]] G.adjlist_dict_factory为: <class 'dict'> ''' print('..........................') 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, edgeAttr in nbrs.items(): wt = edgeAttr['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) '''
为graphs,nodes,edges添加属性
#Adding attributes to graphs, nodes, and edges #任何python object比如weights,labels,colors都可以作为graphs,nodes,edges的属性 '''Graph attributes''' import networkx as nx G = nx.Graph(day='Friday') print(G.graph) #{'day': 'Friday'} #modify attributes G.graph['day'] = "monday" print(G.graph) #{'day': 'monday'} '''Node attributes''' #用add_node(),add_nodes_from()或G.nodes为node添加属性 G.add_node(1, time='11am') G.add_nodes_from([3],time='2pm') print(G.node[1]) #{'time': '11am'} G.node[1]['room'] = 714 print(G.node) #{1: {'time': '11am', 'room': 714}, 3: {'time': '2pm'}} '''Edge Attributes''' #用add_edge(), add_edges_from(),或下标来为edge添加或修改属性 G.add_edge(1, 2, weight=4.5) 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'] = 7878 G.edge[1][2]['color'] = 'wetuweywiu' print(G.edge) ''' {1: {2: {'weight': 7878, 'color': 'wetuweywiu'}}, 3: {4: {'color': 'red'}, 2: {'weight': 8}}, 2: {1: {'weight': 7878, 'color': 'wetuweywiu'}, 3: {'weight': 8}}, 4: {3: {'color': 'red'}, 5: {'color': 'red'}}, 5: {4: {'color': 'red'}}} ''' print(G.edges()) #[(1, 2), (3, 4), (3, 2), (4, 5)] print(G[1])#1的邻接node以及edge的属性 # {2: {'weight': 7878, 'color': 'wetuweywiu'}} print(G[1][2]) #{'weight': 7878, 'color': 'wetuweywiu'} print(G.edge[1][2]) #{'weight': 7878, 'color': 'wetuweywiu'} ''' 总结: 访问node的具体属性,必须是G.node[u][attr], 而访问edge的具体属性可以是G.edge[u][v][attr]或G[u][v][attr] G.node[u]:node u的所有属性, G.edge[u][v]或G[u][v]:边(u, v)的所有属性 G.node:所有点以及属性, G.edge:所有edge以及属性 '''
有向图:
import networkx as nx DG = nx.DiGraph() DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)]) print(DG.out_degree(1, weight='weight')) #0.5 print(DG.in_degree(1, weight='weight')) #0.75 print(DG.degree(1, weight='weight')) #1.25 print(DG.successors(1)) #[2] print(DG.neighbors(1)) #[2] print(DG.out_edges(3)) #[(3, 1)] print(DG.in_edges(2)) #[(1, 2)] print(DG.predecessors(1)) [3] ''' 总结: DiGraph.out_edges(), DiGraph.in_edges() DiGraph.in_degree(), DiGraph.out_degree(),DiGraph.degree() DiGraph.predecessors(), DiGraph.successors()相当于DiGraph.neighbours() ''' H = nx.Graph(DG)#将有向图转化为无向图 print(H.edge) # {1: {2: {'weight': 0.5}, 3: {'weight': 0.75}}, 2: {1: {'weight': 0.5}}, 3: {1: {'weight': 0.75}}} H1 = DG.to_undirected() print(H1.edge) #{1: {2: {'weight': 0.5}, 3: {'weight': 0.75}}, 2: {1: {'weight': 0.5}}, 3: {1: {'weight': 0.75}}}
MultiGraph:
任意一对nodes之间可以有多条边。边的属性不同
#任意一对nodes之间可以有多条边。边的属性不同 import networkx as nx MG = nx.MultiGraph() MG.add_weighted_edges_from([(1, 2, 0.5), (1, 2, 0.75), (2, 3, 0.5)]) print(MG.degree(weight='weight')) #{1: 1.25, 2: 1.75, 3: 0.5} GG = nx.Graph() for n, nbrs in MG.adj.items(): for nbr, edgeDict in nbrs.items(): minvalue = min([d['weight'] for d in edgeDict.values()]) GG.add_edge(n, nbr, weight=minvalue) print(nx.shortest_path(GG, 1, 3)) #[1, 2, 3] print(MG.adj.items()) #dict_items([(1, {2: {0: {'weight': 0.5}, 1: {'weight': 0.75}}}), # (2, {1: {0: {'weight': 0.5}, 1: {'weight': 0.75}}, 3: {0: {'weight': 0.5}}}), # (3, {2: {0: {'weight': 0.5}}})])