Read and write graphs.ΒΆ

Read and write graphs.

../../_images/sphx_glr_plot_read_write_001.png

Out:

#/home/travis/build/networkx/networkx/examples/basic/plot_read_write.py
# GMT Wed Sep 20 17:27:14 2017
#
(1, 3) (1, 2) (0, 3) (2, 3) (1, 4)
(3, 0) (2, 0) (3, 1) (4, 0)
(2, 1) (2, 0) (3, 1) (1, 1) (2, 2)
(0, 3) (0, 2) (0, 4)
(4, 0) (4, 1)
(1, 2) (1, 1) (0, 2) (2, 2)
(3, 3) (3, 4) (3, 2) (2, 3) (4, 3)
(4, 4) (3, 4) (4, 3)
(2, 2) (3, 2) (2, 3)
(4, 1) (4, 2) (3, 1)
(1, 1) (0, 1) (1, 0)
(3, 2) (4, 2) (3, 1)
(0, 0) (0, 1) (1, 0)
(0, 4) (1, 4)
(1, 4) (2, 4)
(2, 3) (2, 4)
(4, 2) (4, 3)
(1, 0) (2, 0)
(0, 1) (0, 2)
(3, 1)
(2, 4) (3, 4)
(2, 0)
(4, 3)
(3, 4)
(0, 2)

# Author: Aric Hagberg (hagberg@lanl.gov)

#    Copyright (C) 2004-2017 by
#    Aric Hagberg <hagberg@lanl.gov>
#    Dan Schult <dschult@colgate.edu>
#    Pieter Swart <swart@lanl.gov>
#    All rights reserved.
#    BSD license.

import sys

import matplotlib.pyplot as plt
import networkx as nx

G = nx.grid_2d_graph(5, 5)  # 5x5 grid
try:  # Python 2.6+
    nx.write_adjlist(G, sys.stdout)  # write adjacency list to screen
except TypeError:  # Python 3.x
    nx.write_adjlist(G, sys.stdout.buffer)  # write adjacency list to screen
# 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=":")

nx.draw(H)
plt.show()

Total running time of the script: ( 0 minutes 0.130 seconds)

Generated by Sphinx-Gallery