In [1]:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.path_graph(10)
In [2]:
print("nodes = {0} \nlinks = {1}".format(G.nodes(), G.edges()))
nodes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] links = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]
In [3]:
# Draw the graph with two different layouts
ax1 = plt.subplot(121)
nx.draw(G)
ax2 = plt.subplot(122)
nx.draw(G, pos=nx.circular_layout(G), node_color='r', edge_color='b')
In [4]:
# The number of nodes and edges
print("#nodes = {0} \n#links = {1}".format(G.number_of_nodes(), G.number_of_edges()))
#nodes = 10 #links = 9
In [5]:
# The neighbors for each node
for n in G.nodes:
print(n, [k for k in G.neighbors(n)])
0 [1] 1 [0, 2] 2 [1, 3] 3 [2, 4] 4 [3, 5] 5 [4, 6] 6 [5, 7] 7 [6, 8] 8 [7, 9] 9 [8]
In [6]:
# Some examples of bipartite, cycle, and star graph
A = nx.complete_bipartite_graph(3,4)
B = nx.cycle_graph(4)
C = nx.star_graph(6)
plt.subplot(221)
plt.title("Bipartite")
top = nx.bipartite.sets(A)[0]
nx.draw(A, pos=nx.bipartite_layout(A, top))
plt.subplot(222)
plt.title("Cycle")
nx.draw(B, pos=nx.circular_layout(B))
plt.subplot(223)
plt.title("Star")
nx.draw(C)
In [ ]: