In [1]:
%matplotlib inline
# Imports
import networkx as nx
import numpy
import matplotlib.pyplot as plt
import pandas
import seaborn; seaborn.set()
seaborn.set_style("darkgrid")
# Import widget methods
from IPython.html.widgets import *
In [2]:
g = nx.Graph()
print((g.number_of_nodes(), g.number_of_edges()))
In [3]:
g.add_node("alice")
print((g.number_of_nodes(), g.number_of_edges()))
In [4]:
g.add_node("bob")
print((g.number_of_nodes(), g.number_of_edges()))
In [5]:
g.add_edge("alice", "bob")
print((g.number_of_nodes(), g.number_of_edges()))
In [6]:
# Draw the random graph
g_layout = nx.spring_layout(g, iterations=1000)
nx.draw_networkx(g, pos=g_layout, node_color='#dddddd')
In [22]:
# Create a new graph
g = nx.Graph()
num_steps = 10
prob_triad = 0.1
# Iterate through time
for t in range(num_steps):
# Draw a random number of nodes to add
num_nodes_add = numpy.random.binomial(10, 0.25)
num_edges_add = 0
for i in range(g.number_of_nodes(), g.number_of_nodes() + num_nodes_add):
print("adding node {0} at time step {1}".format(i, t))
g.add_node(i)
if i > 0:
print("adding edge from {0} to {1} at time step {2}".format(i, i-1, t))
g.add_edge(i, i-1)
if i > 3:
if numpy.random.random() < prob_triad:
print("adding edge from {0} to {1} at time step {2}".format(i, i-1, t))
g.add_edge(i, i-4)
print((num_nodes_add, num_edges_add))
In [23]:
# Draw the random graph
g_layout = nx.spring_layout(g, iterations=100)
nx.draw_networkx(g, pos=g_layout, node_color='#dddddd')
In [24]:
# Get the giant component
giant_component = g.subgraph(sorted(nx.connected_components(g), key = len, reverse=True)[0])
# Draw the giant component
g_layout = nx.spring_layout(giant_component, iterations=1000)
nx.draw_networkx(giant_component, pos=g_layout, node_color='#dddddd')
In [32]:
# Create a new graph
g = nx.Graph()
num_steps = 10
prob_triad = 1.0
num_node_n = 10
num_node_p = 0.5
num_edge_p = 0.1
# Iterate through time
for t in range(num_steps):
# Draw a random number of nodes to add
num_nodes_add = numpy.random.binomial(n=num_node_n, p=num_node_p)
for i in range(g.number_of_nodes(), g.number_of_nodes() + num_nodes_add):
# Add the new node
g.add_node(i)
# Determine number of edges to sample
num_edges_add = numpy.random.binomial(n=g.number_of_nodes(), p=num_edge_p)
target_nodes = numpy.random.choice(g.nodes(), size=num_edges_add)
for target_node in target_nodes:
g.add_edge(i, target_node)
print(((t, g.number_of_nodes(), g.number_of_edges())))
# Draw the random graph
g_layout = nx.spring_layout(g, iterations=100)
nx.draw_networkx(g, pos=g_layout, node_color='#dddddd')
In [34]:
print(len(list(nx.connected_components(g))))
# Get the giant component
giant_component = g.subgraph(sorted(nx.connected_components(g), key = len, reverse=True)[0])
# Draw the giant component
g_layout = nx.spring_layout(giant_component, iterations=1000)
nx.draw_networkx(giant_component, pos=g_layout, node_color='#dddddd')
In [ ]: