| 123456789101112131415161718192021222324 |
- """
- ======================
- Read and write graphs.
- ======================
- Read and write graphs.
- """
- import matplotlib.pyplot as plt
- import networkx as nx
- G = nx.grid_2d_graph(5, 5) # 5x5 grid
- # print the adjacency list
- for line in nx.generate_adjlist(G):
- print(line)
- # write edgelist to grid.edgelist
- nx.write_edgelist(G, path="grid.edgelist", delimiter=":")
- # read edgelist from grid.edgelist
- H = nx.read_edgelist(path="grid.edgelist", delimiter=":")
- pos = nx.spring_layout(H, seed=200)
- nx.draw(H, pos)
- plt.show()
|