# Graph Network#

## References#

Network drawing - https://networkx.org/documentation/stable/reference/drawing.html

[1]:

import networkx as nx
import matplotlib.pyplot as plt


## Undirected Network#

[2]:

G = nx.Graph()

fig, ax = plt.subplots(1, 2, figsize=(15,8))
nx.draw(G, pos=nx.spiral_layout(G),  with_labels=True, font_weight='bold', ax=ax[0])
nx.draw(G, pos=nx.circular_layout(G),  with_labels=True, font_weight='bold', ax=ax[1])
plt.show()


## Directed Network#

### Simple Directed Network#

[3]:

G = nx.DiGraph()

NODES = [1, 2, 3, 4, 5, 6, 7, 8]

fig, ax = plt.subplots(1, 3, figsize=(15,8))
nx.draw(G, pos=nx.spiral_layout(G),  with_labels=True, font_weight='bold', ax=ax[0])
nx.draw(G, pos=nx.circular_layout(G),  with_labels=True, font_weight='bold', ax=ax[1])
nx.draw(G, pos=nx.bipartite_layout(G, nodes=NODES),  with_labels=True, font_weight='bold', ax=ax[2])
plt.show()


### Config based Directed Network#

[4]:

G = nx.DiGraph()

NODES = [1, 2, 3, 4, 5, 6, 7, 8]

EDGES = [
(1, 1),
(1, 2),
(1, 3),
(1, 4),
(1, 5),
(1, 6),
(1, 7),
(3, 4),
(3, 6)
]

fig, ax = plt.subplots(1, 3, figsize=(15,8))
nx.draw(G, pos=nx.spiral_layout(G),  with_labels=True, font_weight='bold', ax=ax[0])
nx.draw(G, pos=nx.circular_layout(G),  with_labels=True, font_weight='bold', ax=ax[1])
nx.draw(G, pos=nx.bipartite_layout(G, nodes=NODES),  with_labels=True, font_weight='bold', ax=ax[2])
plt.show()


### Complex Directed Network#

[5]:

G = nx.DiGraph()

NODES = [ 'Wake Up', 'Brush', 'Breakfast', 'Go to Work', 'Lunch',
'Nap', 'Continue To Work', 'Go to Home', 'Dinner', 'TV', 'Sleep']
EDGES = [
('Wake Up', 'Brush', {"weight" : 10} ),
('Brush', 'Breakfast', {"weight" : 5}),
('Breakfast', 'Go to Work', {"weight" : 8}),
('Go to Work', 'Lunch', {"weight" : 1}),
('Lunch', 'Nap', {"weight" : 12}),
('Nap', 'Continue To Work', {"weight" : 8}),
('Continue To Work', 'Go to Home', {"weight" : 20}),
('Go to Home', 'Dinner', {"weight" : 5}),
('Dinner', 'TV', {"weight" : 4}),
('TV', 'Sleep', {"weight" : 6}),
#     ('Sleep', 'Wake Up'),
('Wake Up', 'Breakfast', {"weight" : 7}),
('Wake Up', 'Go to Work', {"weight" : 0.1}),
('Go to Work',  'Continue To Work', {"weight" : 0.5}),
('Lunch', 'Continue To Work', {"weight" : 0.8}),
('Lunch',  'Go to Home', {"weight" : 0.01}),
('Go to Home', 'Sleep', {"weight" : 1})
]

options = {
'node_size': 1000,
'font_weight' : 'bold',
'with_labels' : True
}

fig, ax = plt.subplots(1, 2, figsize=(17,8))

nx.draw(G, pos=nx.spiral_layout(G, equidistant=False), ax=ax[0], **options)
nx.draw_networkx_edge_labels(G, pos=nx.spiral_layout(G, equidistant=False), ax=ax[0])

nx.draw(G, pos=nx.circular_layout(G), ax=ax[1], **options)
nx.draw_networkx_edge_labels(G, pos=nx.circular_layout(G), ax=ax[1])

plt.tight_layout()
plt.show()