import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_edge(1, 2, object='Hello', color='red') # add undirected edge
G.adj # AdjacencyView({1: {2: {'object': 'Hello', 'color': 'red'}}, 2: {1: {'object': 'Hello', 'color': 'red'}}})
AdjacencyView({1: {2: {'object': 'Hello', 'color': 'red'}}, 2: {1: {'object': 'Hello', 'color': 'red'}}})
# create a grahp
G = nx.Graph()
G.add_node(1) # add one node
# add nodes with attributes
# nodes can be any hashable object e.g., a text string, an image, an XML object,
# another Graph, a customized node object
G.add_nodes_from([
(4, {"color": "red", 'name': 'Lin'}),
(5, {"color": "green"}),
])
# add node attribute
G.add_node(1, time='5pm')
G.nodes[1]
{'time': '5pm'}
# add undirected edges
G.add_edges_from([(1, 4), (1, 5)])
# graph constructors
edgelist = [(0, 1), (1, 2), (2, 3)]
H = nx.Graph(edgelist)
G.number_of_nodes() # 3, number of nodes
G.number_of_edges() # 2, number of edge
2
G.adj # AdjacencyView({1: {4: {}, 5: {}}, 4: {1: {}}, 5: {1: {}}})
H.adj # AdjacencyView({0: {1: {}}, 1: {0: {}, 2: {}}, 2: {1: {}, 3: {}}, 3: {2: {}}})
AdjacencyView({0: {1: {}}, 1: {0: {}, 2: {}}, 2: {1: {}, 3: {}}, 3: {2: {}}})
G.add_nodes_from(H)
G.adj # AdjacencyView({1: {4: {}, 5: {}}, 4: {1: {}}, 5: {1: {}}, 0: {}, 2: {}, 3: {}})
G.add_node(H)
G.adj # AdjacencyView({1: {4: {}, 5: {}}, 4: {1: {}}, 5: {1: {}}, <networkx.classes.graph.Graph object at 0x7ff1b425f1d0>: {}})
AdjacencyView({1: {4: {}, 5: {}}, 4: {1: {}}, 5: {1: {}}, 0: {}, 2: {}, 3: {}, <networkx.classes.graph.Graph object at 0x7f9d87380b10>: {}})
# order
G = nx.DiGraph()
G.add_edge(2, 1) # adds the nodes in order 2, 1, which represents an edge from 2 to 1
G.add_edge(1, 3)
G.add_edge(2, 4)
G.add_edge(1, 2)
list(G.successors(2)) # 2 to 1 and 4
[1, 4]
list(G.nodes) # [2, 1, 3, 4]
list(G.edges) # [(2, 1), (2, 4), (1, 3), (1, 2)]
list(G.adj[1]) # [3, 2]
G.degree # DiDegreeView({2: 3, 1: 3, 3: 1, 4: 1})
G.degree[2] # 3
3
# edges of a subgraph
G.edges([2, 1])
OutEdgeDataView([(2, 1), (2, 4), (1, 3), (1, 2)])
# remove a single node and the edge incident to it
G.remove_node(2)
# remove multiple nodes
G.remove_nodes_from([1, 2])
# remove an edge
G.remove_edge(1, 3)
# remove all nodes and edges
G.clear()
G = nx.Graph([(1, 2, {"color": "yellow"}), (2, 3, {'color': 'blue'})])
G[1][2] # {'color': 'yellow'}
G.edges[1, 2] # {'color': 'yellow'}
G[1][2]['color'] = 'blue' # change the color attribute to be blue
# examination all (node, adjacency) pairs
list(G.adjacency()) # [(1, {2: {'color': 'blue'}}), (2, {1: {'color': 'blue'}}), (3, {2: {'color': 'blue'}})]
list(G.adj.items()) # [(1, AtlasView({2: {'color': 'blue'}})), (2, AtlasView({1: {'color': 'blue'}, 3: {'color': 'blue'}})), (3, AtlasView({2: {'color': 'blue'}}))]
[(1, AtlasView({2: {'color': 'blue'}})), (2, AtlasView({1: {'color': 'blue'}, 3: {'color': 'blue'}})), (3, AtlasView({2: {'color': 'blue'}}))]
for n, nbrs in G.adj.items():
for nbr, attr in nbrs.items():
print(nbr, attr)
2 {'color': 'yellow'} 1 {'color': 'yellow'} 3 {'color': 'blue'} 2 {'color': 'blue'}
# access all edges
for (sn, en, attr) in G.edges.data():
print(sn, en, attr)
1 2 {'color': 'yellow'} 2 3 {'color': 'blue'}
for (sn, en, attr) in G.edges.data('color'):
print(sn, en, attr)
1 2 yellow 2 3 blue
# add attriubtes to graph
G = nx.Graph(name="Lin", age=41)
G.graph['state'] = 'Virginia'
G.graph
{'name': 'Lin', 'age': 41, 'state': 'Virginia'}
# add attributes to node
G.add_node(1, color='red')
G.nodes[1]['color'] = 'blue'
G.nodes[1]
{'color': 'blue'}
G.add_edge(1, 2, weight=4.7 )
G.add_edges_from([(1, 2, {'color': 'blue'})])
G.add_edges_from([(3, 4), (4, 5)], color='red')
DG = nx.DiGraph()
DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)])
DG.add_node(3)
DG.add_node(4)
DG.add_edge(3, 4, color='red')
DG.add_edge(1, 3, name='Lin')
DG.add_edges_from([(1, 4), (1, 5)], name='Lin')
MG = nx.MultiGraph()
MG.add_weighted_edges_from([(1, 2, 0.5), (1, 2, 0.75), (2, 3, 0.5)])
DG = nx.DiGraph()
DG.add_nodes_from(['A', 'B', 'C', 'D', 'E', 'F'])
DG.add_edge('A', 'B', weight=4)
DG.add_edge('A', 'C', weight=2)
DG.add_edge('B', 'C', weight=5)
DG.add_edge('B', 'D', weight=10)
DG.add_edge('C', 'E', weight=3)
DG.add_edge('E', 'D', weight=4)
DG.add_edge('D', 'F', weight=11)
paths = nx.algorithms.shortest_paths.generic.all_shortest_paths(DG, source='A', target='F', weight='weight', method='dijkstra')
for path in paths:
print(path)
['A', 'C', 'E', 'D', 'F']
import matplotlib.pyplot as plt
options = {
'node_color': 'red',
'node_size': 800,
'width': 3,
'with_labels': True,
'font_weight': 'bold',
'edge_cmap' : 'PiYG',
}
nx.draw(DG, pos=nx.circular_layout(DG), **options)
labels = nx.get_edge_attributes(DG,'weight')
nx.draw_networkx_edge_labels(DG,nx.circular_layout(DG),edge_labels=labels)
plt.savefig("path.png", font_weight='bold')
/Users/lchen/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:15: MatplotlibDeprecationWarning: savefig() got unexpected keyword argument "font_weight" which is no longer supported as of 3.3 and will become an error two minor releases later from ipykernel import kernelapp as app