<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 网络科学简介
王成军 
wangchengjun@nju.edu.cn
计算传播网 http
Step1:  Directed
Links
Step2:  <img src = './img/networks.png' width = 1000>
Degree, Average Degree and Degree Distribution
Step3:  Undirected network
Step4:  Directed network
In directed networks we can define an in-degree and out-degree. The (total) degree is the sum of in-and out-degree.
$k_3^{in} = 2, k_3^{out} = 1, k_3 = 3$
Source
Step5:  For a sample of N values
Step6:  Average Degree
Undirected
$<k> = \frac{1}{N} \sum_{i = 1}^{N} k_i  = \frac{2L}{N}$
Directed
$<k^{in}> = \frac{1}{N} \sum_{i=1}^N k_i^{in}= <k^{out}> = \frac{1}{N} \sum_{i=1}^N k_i^{out} = \frac{L}{N}$
Degree distribution
P(k)
Step7:  Undirected
$A_{ij} =1$ if there is a link between node i and j
$A_{ij} =0$ if there is no link between node i and j
$A_{ij}=\begin{bmatrix} 0&1  &0  &1 \ 1&0  &0  &1 \ 0 &0  &0  &1 \ 1&1  &1  & 0 \end{bmatrix}$
Undirected
$A_{ij} = A_{ji}, \
Step8:  THREE CENTRAL QUANTITIES IN NETWORK SCIENCE
- A. Degree distribution
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import matplotlib.pyplot as plt
import networkx as nx
Gu = nx.Graph()
for i, j in [(1, 2), (1, 4), (4, 2), (4, 3)]:
    Gu.add_edge(i,j)
nx.draw(Gu, with_labels = True)
Explanation: 网络科学简介
王成军 
wangchengjun@nju.edu.cn
计算传播网 http://computational-communication.com
FROM SADDAM HUSSEIN TO NETWORK THEORY
A SIMPLE STORY (1)      The fate of Saddam and network science
Invasion that started in March 19,  2003. Many of the regime's high ranking officials, including Saddam Hussein, avoided capture. 
Hussein was last spotted kissing a baby in Baghdad in April 2003, and then his trace went cold. 
Designed a deck of cards, each card engraved with the images of the 55 most wanted. 
It worked: by May 1, 2003, 15 men on the cards were captured, and by the end of the month another 12 were under custody. 
Yet, the ace of spades, i.e. Hussein himself, remained at large.
<img src = './img/saddam.png' width = 500>
The capture of Saddam Hussein:
shows the strong predictive power of networks.  
underlies the need to obtain accurate maps of the networks we aim to study; and the often heroic difficulties we encounter during the mapping process.
demonstrates the remarkable stability of these networks:  The capture of Hussein was not based on fresh intelligence, but rather on his pre-invasion social links, unearthed from old photos stacked in his family album.
shows that the choice of network we focus on makes a huge difference: the hierarchical tree, that captured the official organization of the Iraqi government, was of no use when it came to Saddam Hussein's whereabouts.  
A SIMPLE STORY (2): August 15, 2003 blackout.
<img src='./img/blackout.png' width = 800>
VULNERABILITY DUE TO INTERCONNECTIVITY
The 2003 blackout is a typical example of a cascading failure. 
1997, when the International Monetary Fund pressured the central banks of several Pacific nations to limit their credit. 
2009-2011 financial melt-down
An important theme of this class:  
we must understand how network structure affects the robustness of a complex system. 
develop quantitative tools to assess the interplay between network structure and the dynamical processes on the networks, and their impact on failures.  
We will learn that failures reality failures follow reproducible laws, that can be quantified and even predicted using the tools of network science.
NETWORKS AT THE HEART OF COMPLEX SYSTEMS
Complex
[adj., v. kuh m-pleks, kom-pleks; n. kom-pleks] 
–adjective 
- 1. composed of many interconnected parts; compound; composite: a complex highway system. 
- 2. characterized by a very complicated or involved arrangement of parts, units, etc.: complex machinery. 
- 3. so complicated or intricate as to be hard to understand or deal with: a complex problem. 
                Source: Dictionary.com
Complexity
a scientific theory which asserts that some systems display behavioral phenomena that are completely inexplicable by any conventional analysis of the systems’ constituent parts. These phenomena, commonly referred to as emergent behaviour, seem to occur in many complex systems involving living organisms, such as a stock market or the human brain.
 
Source: John L. Casti, Encyclopædia Britannica
 
COMPLEX SYSTEMS
society
brain
market
cell
Stephen Hawking: I think the next century will be the century of complexity.
Behind each complex system there is a network, that defines the interactions between the component.
<img src = './img/facebook.png' width = 800>
Social graph
Organization
Brain
finantial network
business 
Internet
Genes
Behind each system studied in complexity there is an intricate wiring diagram, or a network, that defines the interactions between the component.   
We will never understand complex system unless we map out and understand the networks behind them.
TWO FORCES HELPED THE EMERGENCE OF NETWORK SCIENCE
THE HISTORY OF NETWORK ANALYSIS
Graph theory: 1735, Euler
Social Network Research:  1930s, Moreno
Communication networks/internet: 1960s
Ecological Networks: May, 1979.
<img src = './img/citation.png' width = 500>
While the study of networks has a long history from graph theory to sociology, the modern chapter of network science emerged only during the first decade of the 21st century, following the publication of two seminal papers in 1998 [2] and 1999 [3]. The explosive interest in network science is well documented by the citation pattern of two classic network papers, the 1959 paper by Paul Erdos and Alfréd Rényi that marks the beginning of the study of random networks in graph theory [4] and the 1973 paper by Mark Granovetter, the most cited social network paper [5]. Both papers were hardly or only moderately cited before 2000. The explosive growth of citations to these papers in the 21st century documents the emergence of network science, drawing a new, interdisciplinary audience to these classic publications. 
THE EMERGENCE OF NETWORK SCIENCE
Movie Actor Network,  1998;
World Wide Web,  1999.
C elegans neural wiring diagram 1990
Citation Network,  1998
Metabolic Network, 2000; 
PPI network, 2001
The universality of network characteristics:
The architecture of networks emerging in various domains of science, nature, and technology are more similar to each other than one would have expected. 
THE CHARACTERISTICS OF NETWORK SCIENCE
Interdisciplinary 
Empirical
Quantitative and Mathematical 
Computational 
THE IMPACT OF NETWORK SCIENCE
Google
Market Cap(2010 Jan 1): 
$189 billion
Cisco Systems
networking gear Market cap (Jan 1, 2919): 
$112 billion
Facebook
market cap: 
$50 billion
Health: From drug design to metabolic engineering.
The human genome project, completed in 2001, offered the first comprehensive list of all human genes. Yet, to fully understand how our cells function, and the origin of disease, we need accurate maps that tell us how these genes and other cellular components interact with each other. 
Security: Fighting Terrorism.
Terrorism is one of the maladies of the 21st century, absorbing significant resources to combat it worldwide. Network thinking is increasingly present in the arsenal of various law enforcement agencies in charge of limiting terrorist activities. It is used to disrupt the financial network of terrorist organizations, to map terrorist networks, and to uncover the role of their members and their capabilities. While much of the work in this area is classified, several success stories have surfaced. Examples include the use of social networks to capture Saddam Hussein or the capture of the individuals behind the March 11, 2004 Madrid train bombings through the examination of the mobile call net- work. 
Epidemics: From forecasting to halting deadly viruses.
While the H1N1 pandemic was not as devastating as it was feared at the beginning of the outbreak in 2009, it gained a special role in the history of epidemics: it was the first pandemic whose course and time evolution was accurately predicted months before the pandemic reached its peak (Image 1.9) [14]. This was possible thanks to fundamental advances in understanding the role of networks in the spread of viruses. Indeed, before 2000 epidemic modeling was dominated by compartment models, assuming that everyone can infect everyone else one word the same socio-physical compartment. The emergence of a network-based framework has fundamentally changed this, offering a new level of predictability in epidemic phenomena. 
Today epidemic prediction is one of the most active applications of network science. It is the source several fundamental results, covered in this book, that are used to predict the spread of both biological and electronic viruses. The impact of these advances are felt beyond biological viruses. In January 2010 network science tools have predicted the conditions necessary for the emergence of viruses spreading through mobile phones. The first major mobile epidemic outbreak that started in the fall of 2010 in China, infecting over 300,000 phones each day, closely followed the predicted scenario. 
Brain Research: Mapping neural network.
The human brain, consisting of hundreds of billions of interlinked neurons, is one of the least understood net- works from the perspective of network science. The reason is simple: we lack maps telling us which neurons link to each other. The only fully mapped neural map available for research is that of the C.Elegans worm, with only 300 neurons. Should detailed maps of mammalian brains become available, brain research could become the most prolific application area of network science. Driven by the potential impact of such maps, in 2010 the National Institutes of Health has initiated the Connectome project, aimed at developing the technologies that could provide an accurate neuron-level map of mammalian brains. 
The Bridges of Konigsberg
<img src = './img/konigsberg.png' width = 500>
Can one walk across the seven bridges and never cross the same bridge twice and get back to the starting place?
Can one walk across the seven bridges and never cross the same bridge twice and get back to the starting place?     
<img src ='./img/euler.png' width = 300>
1735: Euler’s theorem:
If a graph has more than two nodes of odd degree, there is no path. 
If a graph is connected and has no odd degree nodes, it has at least one path.
COMPONENTS OF A COMPLEX SYSTEM
Networks and graphs
components: nodes, vertices        N
interactions:  links, edges             L
system:        network, graph        (N,L)
network often refers to real systems
- www, 
- social network
- metabolic network. 
Language: (Network, node, link)
graph: mathematical representation of a network
- web graph, 
- social graph (a Facebook term)
Language: (Graph, vertex, edge)
<img src = './img/net.png' width = 800>
CHOOSING A PROPER REPRESENTATION
The choice of the proper network representation determines our ability to use network theory successfully.
In some cases there is a unique, unambiguous representation. 
In other cases, the representation is by no means unique.
 
For example, the way we assign the links between a group of individuals will determine the nature of the question we can study.
If you connect individuals that work with each other, you will explore the professional network.
http://www.theyrule.net
If you connect those that have a romantic and sexual relationship, you will be exploring the sexual networks.
If you connect individuals based on their first name (all Peters connected to each other), you will be exploring what? 
It is a network, nevertheless.
UNDIRECTED VS. DIRECTED NETWORKS
Undirected
Links: undirected
- coauthorship 
- actor network
- protein interactions
End of explanation
import networkx as nx
Gd = nx.DiGraph()
for i, j in [(1, 2), (1, 4), (4, 2), (4, 3)]:
    Gd.add_edge(i,j)
nx.draw(Gd, with_labels = True)
Explanation: Directed
Links: directed
- urls on the www
- phone callss
- metabolic reactions
End of explanation
nx.draw(Gu, with_labels = True)
Explanation: <img src = './img/networks.png' width = 1000>
Degree, Average Degree and Degree Distribution
End of explanation
nx.draw(Gd, with_labels = True)
Explanation: Undirected network:
Node degree: the number of links connected to the node.
$k_1 = k_2 = 2, k_3 = 3, k_4 = 1$
End of explanation
import numpy as np
x = [1, 1, 1, 2, 2, 3]
np.mean(x), np.sum(x), np.std(x)
Explanation: Directed network
In directed networks we can define an in-degree and out-degree. The (total) degree is the sum of in-and out-degree.
$k_3^{in} = 2, k_3^{out} = 1, k_3 = 3$
Source: a node with $k^{in}= 0$; Sink: a node with $k^{out}= 0$.
For a sample of N values: $x_1, x_2, ..., x_N$:
Average(mean):
$<x> = \frac{x_1 +x_2 + ...+x_N}{N} = \frac{1}{N}\sum_{i = 1}^{N} x_i$
For a sample of N values: $x_1, x_2, ..., x_N$:
The nth moment:
$<x^n> = \frac{x_1^n +x_2^n + ...+x_N^n}{N} = \frac{1}{N}\sum_{i = 1}^{N} x_i^n$
For a sample of N values: $x_1, x_2, ..., x_N$:
Standard deviation:
$\sigma_x = \sqrt{\frac{1}{N}\sum_{i = 1}^{N} (x_i - <x>)^2}$
End of explanation
plt.hist(x)
plt.show()
from collections import defaultdict, Counter
freq = defaultdict(int)
for i in x:
    freq[i] +=1
freq
freq_sum = np.sum(freq.values())
freq_sum
px = [float(i)/freq_sum for i in freq.values()]
px
plt.plot(freq.keys(), px, 'r-o')
plt.show()
Explanation: For a sample of N values: $x_1, x_2, ..., x_N$:
Distribution of x:
$p_x = \frac{The \: frequency \: of \: x}{The\: Number \:of\: Observations}$
其中，$p_x 满足 \sum_i p_x = 1$
End of explanation
plt.figure(1)
plt.subplot(121)
pos = nx.spring_layout(Gu)  #定义一个布局，此处采用了spring布局方式
nx.draw(Gu, pos, with_labels = True)
plt.subplot(122)
nx.draw(Gd, pos, with_labels = True)
Explanation: Average Degree
Undirected
$<k> = \frac{1}{N} \sum_{i = 1}^{N} k_i  = \frac{2L}{N}$
Directed
$<k^{in}> = \frac{1}{N} \sum_{i=1}^N k_i^{in}= <k^{out}> = \frac{1}{N} \sum_{i=1}^N k_i^{out} = \frac{L}{N}$
Degree distribution
P(k): probability that a randomly selected node  has degree k
$N_k = The \:number\: of \:nodes\:with \:degree\: k$
$P(k) = \frac{N_k}{N}$
Adjacency matrix
$A_{ij} =1$ if there is a link between node i and j
$A_{ij} =0$ if there is no link between node i and j
End of explanation
G1 = nx.complete_graph(4)
pos = nx.spring_layout(G1)  #定义一个布局，此处采用了spring布局方式
nx.draw(G1, pos = pos, with_labels = True)
print(nx.transitivity(G1))
G2 = nx.Graph()
for i, j in [(1, 2), (1, 3), (1, 0), (3, 0)]:
    G2.add_edge(i,j)
nx.draw(G2,pos = pos, with_labels = True)
print(nx.transitivity(G2))
G3 = nx.Graph()
for i, j in [(1, 2), (1, 3), (1, 0)]:
    G3.add_edge(i,j)
nx.draw(G3, pos =pos, with_labels = True)
print(nx.transitivity(G3))
Explanation: Undirected
$A_{ij} =1$ if there is a link between node i and j
$A_{ij} =0$ if there is no link between node i and j
$A_{ij}=\begin{bmatrix} 0&1  &0  &1 \ 1&0  &0  &1 \ 0 &0  &0  &1 \ 1&1  &1  & 0 \end{bmatrix}$
Undirected
$A_{ij} = A_{ji}, \: A_{ii} = 0$
$k_i = \sum_{j=1}^N A_{ij}, \: k_j = \sum_{i=1}^N A_{ij} $
$ L = \frac{1}{2}\sum_{i=1}^N k_i = \frac{1}{2}\sum_{ij}^N A_{ij} $
Directed
$A_{ij} =1$ if there is a link between node i and j
$A_{ij} =0$ if there is no link between node i and j
$A_{ij}=\begin{bmatrix} 0&0  &0  &0 \ 1&0  &0  &1 \ 0 &0  &0  &1 \ 1&0  &0  & 0 \end{bmatrix}$
Note that for a directed graph the matrix is not symmetric.
Directed
$A_{ij} \neq A_{ji}, \: A_{ii} = 0$
$k_i^{in} = \sum_{j=1}^N A_{ij}, \: k_j^{out} = \sum_{i=1}^N A_{ij} $
$ L = \sum_{i=1}^N k_i^{in} = \sum_{j=1}^N k_j^{out}= \frac{1}{2}\sum_{i,j}^N A_{ij} $
WEIGHTED AND UNWEIGHTED NETWORKS
$A_{ij} = W_{ij}$
BIPARTITE NETWORKS
bipartite graph (or bigraph) is a graph whose nodes can be divided into two disjoint sets U and V such that every link connects a node in U to one in V; that is, U and V are independent sets. 
Hits algorithm
recommendation system
Ingredient-Flavor Bipartite Network
<img src = './img/bipartite.png' width = 800>
Path
A path is a sequence of nodes in which  each node is adjacent to the next one
    - In a directed network, the path can follow only the direction of an arrow. 
Distance
The distance (shortest path, geodesic path) between two nodes is defined as the number of edges along the shortest path connecting them.
- *If the two nodes are disconnected, the distance is infinity.
Diameter:
Diameter dmax is the maximum distance between any pair of nodes in the graph. 
Shortest Path
The path with the shortest length between two nodes (distance). 
Average path length/distance, $<d>$
The average of the shortest paths for all pairs of nodes.
for a connected graph: where $d_{ij}$ is the distance from node i to node j
$<d> = \frac{1}{2 L_{max} }\sum_{i, j \neq i} d_{ij}$
In an undirected graph $d_{ij} =d_{ji}$ , so we only need to count them once:
$<d> = \frac{1}{L_{max} }\sum_{i, j > i} d_{ij}$
Cycle
A path with the same start and end node. 
CONNECTEDNESS
Connected (undirected) graph:
any two vertices can be joined by a path. A disconnected graph is made up by two or more connected components.   
Largest Component:  Giant Component
The rest: Isolates
Bridge:
if  we erase it, the graph becomes disconnected. 
The adjacency matrix of a network with several components can be written in a block-diagonal form, so that nonzero elements are confined to squares, with all other elements being zero:
<img src = './img/block.png' width = 600>
Strongly connected directed graph:
has a path from each node to every other node and vice versa (e.g. AB path and BA path).
Weakly connected directed graph:
it is connected if we disregard the edge directions.
Strongly connected components can be identified, but not every node is partof a nontrivial strongly connected component.   
In-component: nodes that can reach the scc, 
Out-component: nodes that can be reached from the scc. 
Clustering coefficient
Clustering coefficient:
what fraction of your neighbors are connected? Watts & Strogatz, Nature 1998.
Node i with degree $k_i$
$C_i$ in [0,1]
$C_i = \frac{2e_i}{k_i(k_i -1)}$
<img src = './img/cc.png' width = 500>
Global Clustering Coefficient:
$C = \frac{3 \times \mbox{number of triangles}}{\mbox{number of connected triplets of vertices}} = \frac{\mbox{number of closed triplets}}{\mbox{number of connected triplets of vertices}}$
End of explanation
import networkx as nx
import matplotlib.pyplot as plt
BA= nx.random_graphs.barabasi_albert_graph(500,1)  #生成n=20、m=1的BA无标度网络
pos = nx.spring_layout(BA)          #定义一个布局，此处采用了spring布局方式
nx.draw(BA,pos,with_labels=False,node_size = 30)  #绘制图形
plt.show()
nx.degree_histogram(BA)[:3]
BA.degree().items()[:3]
plt.hist(BA.degree().values())
plt.show()
def plotDegreeDistributionLongTail(G):
    degs = defaultdict(int)
    for i in G.degree().values(): degs[i]+=1
    items = sorted ( degs.items () )
    x, y = np.array(items).T
    plt.plot(x, y, 'b-o')
    plt.legend(['Degree'])
    plt.xlabel('$K$', fontsize = 20)
    plt.ylabel('$P_K$', fontsize = 20)
    plt.title('$Degree\,Distribution$', fontsize = 20)
    plt.show()  
    
plotDegreeDistributionLongTail(BA)
def plotDegreeDistribution(G):
    degs = defaultdict(int)
    for i in G.degree().values(): degs[i]+=1
    items = sorted ( degs.items () )
    x, y = np.array(items).T
    plt.plot(x, y, 'b-o')
    plt.xscale('log')
    plt.yscale('log')
    plt.legend(['Degree'])
    plt.xlabel('$K$', fontsize = 20)
    plt.ylabel('$P_K$', fontsize = 20)
    plt.title('$Degree\,Distribution$', fontsize = 20)
    plt.show()   
    
plotDegreeDistribution(BA)
BA= nx.random_graphs.barabasi_albert_graph(50000,10)  #生成n=50000、m=1的BA无标度网络
plotDegreeDistribution(BA)
Explanation: THREE CENTRAL QUANTITIES IN NETWORK SCIENCE
- A. Degree distribution:             $p_k$
- B. Path length:                       $<d>$         
- C. Clustering coefficient: $C_i$
Typical Network Science Research
Discovering, Modeling, Verification
WATTSDJ,STROGATZSH.Collective dynamics of‘small-world’ networks. Nature, 1998, 393(6684): 440–442.
BARABÁSI A-L, ALBERT R. Emergence of scaling in random networks. Science, 1999, 286(5439): 509-512.
Typical Math Style
Fan Chung & Linyuan Lu, The average distance in random graphs with given expected degree,. PNAS, 19, 15879-15882 (2002).
Typical Physical Style
A.-L.Barabási,R.Albert,H.Jeong Mean-field theory for scale-free random networks. Physica A 272, 173–187 (1999).
Typical Computer Science Style
Community detection
Link prediction
Recommendation algorithms
Typical control sytle
Controllability of Complex Networks
Liu Y Y, Slotine J J, Barabási A L. Nature, 2011, 473(7346): 167-173.
<img src ='./img/cocktail.png' width = 500>
The random network model
Erdös-Rényi model (1960)
Definition: A random graph is a graph of N nodes where each pair of nodes is connected by probability p.
G(N, L) Model
N lableled nodes are connected with L randomly placed links. Erdös-Rényi(1959)
G(N, p) Model
Each pair of N labeled nodes is connected with probability p. Gilbert (1959)
End of explanation
<END_TASK>