欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

无向图

程序员文章站 2022-06-03 18:37:57
...
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from(
    [('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'),
     ('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')])

val_map = {'A': 1.0,
           'D': 0.5714285714285714,
           'H': 0}

values = [val_map.get(node, 0.25) for node in G.nodes()]

nx.draw(G, cmap = plt.get_cmap('jet'), node_color = values,with_labels=True)
plt.show()

无向图

import networkx as nx 导包

G = nx.Graph() Create an empty graph with no nodes and no edges

Node

add_node() add one node

G.clear()
import networkx as nx
G = nx.Graph()
G.add_node(1)
nx.draw(G,with_labels=True)
plt.show()

无向图

G.clear()
import networkx as nx
G = nx.Graph()
H = nx.path_graph(10)
G.add_node(H)
nx.draw(G,with_labels=True)
plt.show()

无向图

print(type(H))
<class 'networkx.classes.graph.Graph'>
G.clear()
import networkx as nx
G = nx.Graph()
G.add_node("spam")        # adds node "spam"
nx.draw(G,with_labels=True)
plt.show()

无向图

add_nodes_from() add a list of nodes

G.clear()
import networkx as nx
G = nx.Graph()
G.add_nodes_from([2, 3, 4, 5])
nx.draw(G,with_labels=True)
plt.show()

无向图

G.clear()
import networkx as nx
G = nx.Graph()
G.add_nodes_from("spam")  # adds 4 nodes: 's', 'p', 'a', 'm'
nx.draw(G,with_labels=True)
plt.show()

无向图

G.clear()
import networkx as nx
G = nx.Graph()
H = nx.path_graph(10)
G.add_nodes_from(H)
nx.draw(G,with_labels=True)
plt.show()

在这里插入图片描述

Edges

add_edge() adding one edge

G.clear()
import networkx as nx
G = nx.Graph()
G.add_edge(3, 'm')
nx.draw(G,with_labels=True)
plt.show()

无向图

G.clear()
import networkx as nx
G = nx.Graph()
G.add_edge(1, 2)
e = (2, 3)
G.add_edge(*e)  # unpack edge tuple*
nx.draw(G,with_labels=True)
plt.show()

无向图

adding a list of edges

G.clear()
import networkx as nx
G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3)])
nx.draw(G,with_labels=True)
plt.show()

无向图

G.clear()
import networkx as nx
G = nx.Graph()
H = nx.path_graph(10)
G.add_edges_from(H.edges)
nx.draw(G,with_labels=True)
plt.show()

无向图

Graph attributes

G.clear()
import networkx as nx
G = nx.Graph()
G.add_edges_from([(1, 2), (1, 3)])
G.add_node(1)
G.add_edge(1, 2)
G.add_node("spam")        # adds node "spam"
G.add_nodes_from("spam")  # adds 4 nodes: 's', 'p', 'a', 'm'
G.add_edge(3, 'm')
nx.draw(G,with_labels=True)
plt.show()

无向图

图的节点属性

G.number_of_nodes()
8
list(G.nodes)
[1, 2, 3, 'spam', 's', 'p', 'a', 'm']

图的边属性

G.number_of_edges()
3
list(G.edges)
[(1, 2), (1, 3), (3, 'm')]
G.edges([2, 'm',1])
EdgeDataView([(2, 1), ('m', 3), (1, 3)])
G.edges[1, 2]
{}
G[1][2]
{}

节点的邻居

list(G.adj[1])
[2, 3]
list(G.neighbors(1))
[2, 3]
list(G.adj[3])
[1, 'm']
list(G.neighbors(3))
[1, 'm']
list(G.adj['spam'])
[]
list(G.neighbors('spam'))
[]
G[1]  # same as G.adj[1]
AtlasView({2: {}, 3: {}})

节点的度(degrees of nodes)

G.degree[1]  # the number of edges incident to 1
2
G.degree['spam']
0
G.degree([2, 3,'spam'])
DegreeView({2: 1, 3: 2, 'spam': 0})

移除节点

移除前的原始图
无向图

G.remove_node(2)
nx.draw(G,with_labels=True)
plt.show()

无向图

G.remove_node('spam')
nx.draw(G,with_labels=True)
plt.show()

无向图

G.remove_nodes_from("spam")
nx.draw(G,with_labels=True)
plt.show()

无向图

移除边

G.remove_edge(1, 3)
nx.draw(G,with_labels=True)
plt.show()

无向图

颜色属性

G.clear()
import networkx as nx
G = nx.Graph()
G.add_edge(1, 2, weight=4.7 )
G.add_edges_from([(3, 4), (4, 5)], color='red')
G.add_edges_from([(1, 2, {'color': 'blue'}), (2, 3, {'weight': 8})])
G[1][2]['weight'] = 4.7
G.edges[3, 4]['weight'] = 4.2
nx.draw(G,with_labels=True,node_color='green')
plt.show()

无向图

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from(
    [('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'),
     ('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')])

val_map = {'A': 1.0,
           'D': 0.5714285714285714,
           'H': 0}

values = [val_map.get(node, 0.25) for node in G.nodes()]

nx.draw(G, cmap = plt.get_cmap('jet'), node_color = values,with_labels=True)
plt.show()

无向图
对代码进行解读

dict = {'Name': 'Zara', 'Age': 27}
print(dict.get('Name'))
print(dict.get('mikowoo',0.25))
Zara
0.25
values = [val_map.get(node, 0.25) for node in G.nodes()]
print(values)
[0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25]
val_map = {'A': 1.0,
           'D': 0.5714285714285714,
           'H': 0}

values = [val_map.get(node, 0.25) for node in G.nodes()]
print(values)
print(G.nodes())
[1.0, 0.25, 0.25, 0.5714285714285714, 0.25, 0.25, 0, 0.25]
['A', 'B', 'C', 'D', 'E', 'F', 'H', 'G']
相关标签: 图论_python_networkx