<jupyter_start><jupyter_text>CSCS530 Winter 2016 Complex Systems 530 - Computer Modeling of Complex Systems (Winter 2016)  * Course ID: CMPLXSYS 530  * Course Title: Computer Modeling of Complex Systems  * Term: Winter 2016  * Schedule: Wednesdays and Friday, 1:00-2:30PM ET  * Location: 120 West Hall (http://www.lsa.umich.edu/cscs/research/computerlab)  * Teachers: [<NAME>](https://www.linkedin.com/in/bommarito) and [<NAME>](https://www.linkedin.com/pub/sarah-cherng/35/1b7/316)<jupyter_code>%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 *<jupyter_output>:0: FutureWarning: IPython widgets are experimental and may change in the future.<jupyter_text>Graph constructionLet's start by demonstrating how to manually create a deterministic graph.  We'll learn how to add individual nodes and edges.<jupyter_code>g = nx.Graph()
print((g.number_of_nodes(), g.number_of_edges()))
g.add_node("alice")
print((g.number_of_nodes(), g.number_of_edges()))
g.add_node("bob")
print((g.number_of_nodes(), g.number_of_edges()))
g.add_edge("alice", "bob")
print((g.number_of_nodes(), g.number_of_edges()))
# Draw the random graph
g_layout = nx.spring_layout(g, iterations=1000)
nx.draw_networkx(g, pos=g_layout, node_color='#dddddd')<jupyter_output>/data/workspace/env/local/lib/python2.7/site-packages/matplotlib/collections.py:650: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if self._edgecolors_original != str('face'):
/data/workspace/env/local/lib/python2.7/site-packages/matplotlib/collections.py:590: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if self._edgecolors == str('face'):<jupyter_text>Random graph constructionNext, let's simulate our own growing graph.  We'll use the following procedure:  * each time step, sample a number of nodes to add  * for each node to add, sample a random set of edges to add<jupyter_code># 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))    
# Draw the random graph
g_layout = nx.spring_layout(g, iterations=100)
nx.draw_networkx(g, pos=g_layout, node_color='#dddddd')
# 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')
# 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')
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')<jupyter_output>1
{'fraction_non_alphanumeric': 0.11667026094457623, 'fraction_numerical': 0.022212637481130042, 'mean_word_length': 4.276450511945392, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '288452', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1575, 'n_words': 403, 'path': 'notebooks/basic-space/004-my-first-network.ipynb', 'repo_name': 'mjbommar/cscs-530-w2016', 'size': '4651'}