In [1]:
import networkx as nx
import matplotlib.pyplot as plt
from scipy import sparse
from IPython.display import Image, display
%matplotlib inline
G = nx.house_graph()
pos = nx.spring_layout(G)
nx.draw(G, pos=pos)
Mathematical definition:
A graph is is an ordered pair $G = (V,E)$ of a set $V$ of vertices or nodes and a set $E \subseteq [V]^2$ of edges or lines. The elements of $E$ are therefore 2-element subsets of $V$, representing a relation between the two elemnts of $V$.
Examples:
graph | nodes | edges |
---|---|---|
internet | computers | network connections |
power grid | power plants, consumers | power cables |
transportation network | locations | routes |
social network | individuals | relationship |
citation network | scientific paper | citation |
neural network | neuron | synapses |
food web | functional groups | feeding relationships |
In [2]:
display(Image(filename='pictures/air_net_light.jpg'))
display(Image(filename='pictures/europ_grid.jpg'))
display(Image(filename='pictures/ice_net.jpg'))
display(Image(filename='pictures/leaf_net.jpg'))
display(Image(filename='pictures/mold_net.png'))
display(Image(filename='pictures/mouse_brain.jpg'))
In [4]:
nx.draw_networkx(G, pos=pos, node_color='black', font_color='white')
dump = nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=dict(zip(G.edges(), G.edges())))
In [5]:
A = nx.adjacency_matrix(G)
print(A)
$$B_{ei} := \left\{ \begin{array}{ll} 1 & : \text{edge } e \text{ connects to node } j\\\ 0 & : \text{else} \end{array} \right. \quad e \in E, i \in V$$
In [7]:
B = nx.incidence_matrix(G).astype(int)
print(B)
In [8]:
edgelist = G.edges()
print(edgelist)
$$N := |V|$$
In [11]:
N = G.order() #G.number_of_nodes()
print(N)
In [12]:
M = G.size() #G.number_of_edges()
print(M)
$\rightarrow$ number of edges connected to a node
In [ ]:
degrees = G.degree()
print degrees
In [38]:
nx.clustering(G)
Out[38]:
In [39]:
nx.draw(G)
In [13]:
N_d = np.array(nx.degree_histogram(G), dtype=float)
print(N_d / N)
A path from node $v_1$ to node $v_n$ is a sequence of pairwise connected nodes $v_i \in V$, so that $(v_i, v_{i+1}) \in E$: $$p(v_1,v_n) = \{v_1, v_2, \dots, v_n\}$$
The length of a path in an unweighted graph is the number of edges it contains: $$l_{p(v_i, v_j)} = |p(v_i, v_j)| - 1$$
distance between two nodes $u$ and $v$ is: $$d(u,v)=\min_{\text{paths p between u and v}} l_p$$
In [26]:
p = nx.shortest_path(G, source=0, target=3)
print(p)
In [27]:
print(nx.shortest_path_length(G, source=0, target=3))
$\rightarrow$ information on how close a node is to all other nodes
In [19]:
CC = nx.closeness_centrality(G)
print(CC)
In [20]:
nx.draw(G)
$\rightarrow$ information on how often a node is "in-between" all other nodes
In [21]:
BC = nx.betweenness_centrality(G)
print(BC)
In [34]:
nx.diameter(G)
Out[34]:
In [35]:
nx.radius(G)
Out[35]:
There exists many models for producing random graphs, each achieving certain criteria. Here we list two.
properties:
properties: