NetworkX
import networkx as nx
一、创建空的图
1.G = nx.Graph() #创建无向图
2.G = nx.Digraph() #创建有向图
二、添加节点
Node、edge、weighted_edge
-
Graph.__init__(**attr[, data]) Initialize a graph with edges, name, graph attributes. -
Graph.add_node(n, **attr[, attr_dict]) Add a single node n and update node attributes. -
Graph.add_nodes_from(nodes, **attr) Add multiple nodes. -
Graph.remove_node(n) Remove node n. -
Graph.remove_nodes_from(nodes) Remove multiple nodes. -
Graph.add_edge(u, v, **attr[, attr_dict]) Add an edge between u and v. -
Graph.add_edges_from(ebunch,**attr[,attr_dict])Add all the edges in ebunch. -
Graph.add_weighted_edges_from(ebunch,**attr)Add all the edges in ebunch as weighted edges with specified weights. -
Graph.remove_edge(u, v) Remove the edge between u and v. -
Graph.remove_edges_from(ebunch) Remove all edges specified in ebunch. -
Graph.add_star(nodes, **attr) Add a star. -
Graph.add_path(nodes, **attr) Add a path. -
Graph.add_cycle(nodes, **attr) Add a cycle. -
Graph.clear() Remove all nodes and edges from the graph.
三、迭代节点
Node、edge


本文详细介绍NetworkX库中图的基本操作,包括创建空图、添加和删除节点与边,以及如何添加星形、路径和环形结构。适用于需要进行复杂图数据结构处理的开发者。
1587

被折叠的 条评论
为什么被折叠?



