This page was generated from docs/source/notebooks/graph.ipynb.

Graphs and Graph Routines#

[1]:
from sigmaepsilon.math.topology import Graph
import matplotlib.pyplot as plt
import networkx as nx

grid = nx.grid_2d_graph(5, 5)  # 5x5 grid
G = Graph(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)
(0, 0) (1, 0) (0, 1)
(0, 1) (1, 1) (0, 2)
(0, 2) (1, 2) (0, 3)
(0, 3) (1, 3) (0, 4)
(0, 4) (1, 4)
(1, 0) (2, 0) (1, 1)
(1, 1) (2, 1) (1, 2)
(1, 2) (2, 2) (1, 3)
(1, 3) (2, 3) (1, 4)
(1, 4) (2, 4)
(2, 0) (3, 0) (2, 1)
(2, 1) (3, 1) (2, 2)
(2, 2) (3, 2) (2, 3)
(2, 3) (3, 3) (2, 4)
(2, 4) (3, 4)
(3, 0) (4, 0) (3, 1)
(3, 1) (4, 1) (3, 2)
(3, 2) (4, 2) (3, 3)
(3, 3) (4, 3) (3, 4)
(3, 4) (4, 4)
(4, 0) (4, 1)
(4, 1) (4, 2)
(4, 2) (4, 3)
(4, 3) (4, 4)
(4, 4)
[2]:
nx.draw(H, pos)
plt.show()
../_images/notebooks_graph_2_0.png
[3]:
G.adjacency_matrix()
[3]:
<25x25 sparse array of type '<class 'numpy.intc'>'
        with 80 stored elements in Compressed Sparse Row format>
[4]:
G.pseudo_peripheral_nodes()
[4]:
array([24,  0], dtype=int64)
[5]:
G.rooted_level_structure()
[5]:
DictType[int64,array(int64, 1d, C)]<iv=None>({0: [0], 1: [1 5], 2: [ 2  6 10], 3: [ 3  7 11 15], 4: [ 4  8 12 16 20], 5: [ 9 13 17 21], 6: [14 18 22], 7: [19 23], 8: [24]})

Calculate the properties of a random graph with directed edges

[6]:
seed = 13648  # Seed random number generators for reproducibility
G = Graph(nx.random_k_out_graph(10, 3, 0.5, seed=seed))
G.pseudo_peripheral_nodes()
[6]:
array([2, 1], dtype=int64)
[7]:
pos = nx.spring_layout(G, seed=200)
nx.draw(G, pos)
plt.show()
../_images/notebooks_graph_8_0.png