Python Networkx basics and use summary

First, the basics of map

1. Complex network (Complex Network) defined characteristics

Qian gives a complex network of a more stringent definition: self-organization, self-similarity, attractor, small world, scale-free network in part or all of the property called complex networks.

Complex network typically has the characteristics:

(1) Small World. Although a large scale but most network between any two nodes but a relatively short path.

(2) a cluster that is gathering degree (Clustering coefficient). That is the extent of the Group network, which is cohesive tendency of the network. Unicom Group concept reflects the relevant connection status of each gathering a large network of small distribution network. For example, the relationship between the circle of friends in a small group with another small group.

(3) the degree of power-law (Power law) distribution concept. Tightness of the connection relationship between the apex of the correlation reflects the degree.

2. Network structure of the relevant measure

Degree (Degree) - number of edges connected to a node. Description of the connection nodes. Of a network is the average of all the nodes it contains. (Calculation Method: twice the number of edges divided by the number of nodes in the network)

Have equal degrees and the vertex of the figure and the vertex degrees.

Path length (Path length) - the distance between the node and the node, i.e. the minimum number of edges required to pass between the two nodes.

Average path length - the total number of network paths between all pairs of the number of nodes (number of nodes) of all pairs of nodes in the network is divided, is the average path length path.

Unicom degree (Connectivity) - FIG k nodes such, after removal of all such nodes and their associated edges from all figures, FIG obtained is no longer communicating or trivial graph of FIG, k is called in FIG. the connectivity node.

Clustering coefficient (Clustering coefficient) - the number of all the triangles in FIG divided by the maximum possible number of triangles formed by the node (the maximum possible number * is n-(n--. 1) (n--2) /. 3 2 . 1 = n- ( n-1) * (n- 2) / 6).

3.Gephi in statistics

The average degree (degree) - Calculation of the degree of each node, and counts the number of nodes of the same degree. FIG average degree of directional: the sum of all degrees points / nodes * 2; undirected graphs: the sum of all degrees points / nodes. The higher the degree of the node, the more points connected to it, indicating that the key point.

Weighted average of (weighted degree) - weighting means, to obtain a point of a side, if the edge is a source for the node, then the right side of the weight of the weight degree, contrary to the weighted degrees. It is a weighted sum of the degree and adding the weighted degrees. There the weighted average of the graph: a weighted sum of / 2 * number of nodes; FIG undirected weighted average of: the sum of weighted degrees / nodes.

Network diameter (graph distance) - the maximum network distance between any two nodes.

FIG density (graph density) - digraph: number of edges / (number of nodes nodes - nodes); no to FIG: number of edges 2 / (nodes nodes - nodes). Wherein (nodes nodes - nodes) is the n * (n-1), i.e. the maximum number of edges n nodes may be generated (directed graph, if they have free to divide FIG. 2). FIG density is to use the maximum number of edges divided by the actual number of sides may be produced, the greater the results shown in the drawing connected to a node more closely.

Modular (modularity) - a clustering method.

Second, the use of modules in Python networkx

1. Establish a map

import networkx as nx
G=nx.Graph()#创建空的简单图
G=nx.DiGraph()#创建空的简单有向图
G=nx.MultiGraph()#创建空的多图
G=nx.MultiDiGraph()#创建空的有向多图

2. Additional, the pressurized 边

G.add_node(1)#加1这个点
G.add_node(1,1)#用(1,1)这个坐标加点
G.add_nodes_from([2,3])#加列表中的点
 
G.add_edge(1,2)#加边,起点是1终点是2
G.add_weight_edge(1,2,3.0)#第三个是权值
G.add_edges_from(list)#添加列表中的边
G.add_weight_edges_from(list)

3. Delete the point and edges

G.remove_node()
G.remove_nodes_from()
G.remove_edge()
G.remove_edges_from()
G.clear()

4. Loop and edges

G.add_nodes_from([1,2,3])
for n in G.nodes():
    print(n)
G.add_edges_from([(1,2),(1,3)])
for e in G.edges():
    print(e)
print(G.degree())

print

1
2
3
(1, 2)
(1, 3)
[(1, 2), (2, 1), (3, 1)]#1这个点有两条边连着,2、3只有一条边连着

Videos network 5. FIG.

from matplotlib import pyplot as plt
import networkx as nx
G=nx.Graph()
G.add_nodes_from([1,2,3])
G.add_edges_from([(1,2),(1,3)])
nx.draw_networkx(G)
plt.show()

Results
Here Insert Picture Description

Three, networkx module properties and methods used

1. Fig.

degree(G[, nbunch, weight]):返回单个节点或nbunch节点的度数视图。 

degree_histogram(G):返回每个度值的频率列表。

density(G):返回图的密度。

info(G[, n]):打印图G或节点n的简短信息摘要。

create_empty_copy(G[, with_data]):返回图G删除所有的边的拷贝。

is_directed(G):如果图是有向的,返回true。

add_star(G_to_add_to, nodes_for_star, **attr):在图形G_to_add_to上添加一个星形。

add_path(G_to_add_to, nodes_for_path, **attr):在图G_to_add_to中添加一条路径。

add_cycle(G_to_add_to, nodes_for_cycle, **attr):向图形G_to_add_to添加一个循环。

2. Node

nodes(G):在图节点上返回一个迭代器。

number_of_nodes(G):返回图中节点的数量。

all_neighbors(graph, node):返回图中节点的所有邻居。

non_neighbors(graph, node):返回图中没有邻居的节点。

common_neighbors(G, u, v):返回图中两个节点的公共邻居。

3. side

edges(G[, nbunch]):返回与nbunch中的节点相关的边的视图。

number_of_edges(G):返回图中边的数目。

non_edges(graph):返回图中不存在的边。
Published 51 original articles · won praise 184 · views 30000 +

Guess you like

Origin blog.csdn.net/CUFEECR/article/details/103007712