networkx.algorithms.minors.quotient_graph
的用法。用法:
quotient_graph(G, partition, edge_relation=None, node_data=None, edge_data=None, relabel=False, create_using=None)
返回节点上指定等价关系下
G
的商图。- G:NetworkX 图
要为其返回具有指定节点关系的商图的图。
- partition:函数、字典或列表、元组或集合的列表
如果是函数,则该函数必须表示
G
的节点上的等价关系。它必须接受两个参数u
和v
并在u
和v
处于同一个等价类时返回 True。等价类形成返回图中的节点。如果是列表/元组/集的字典,键可以是任何有意义的块标签,但值必须是块列表/元组/集(每个块一个列表/元组/集),并且块必须形成有效的分区图的节点。也就是说,每个节点必须恰好位于分区的一个块中。
如果是集合列表,则该列表必须形成图的节点的有效分区。也就是说,每个节点必须恰好位于分区的一个块中。
- edge_relation:带两个参数的布尔函数
此函数必须表示
G
的partition
的blocks
上的边关系。它必须有两个参数B
和C
,每个参数都是一组节点,并且在返回的图形中应该有一条边连接块B
到块C
时返回 True。如果未指定
edge_relation
,则假定为以下关系。根据G
的边集,当且仅当B
中的某个节点与C
中的某个节点相邻时,块B
与块C
相关。- edge_data:函数
此函数接受两个参数,
B
和C
,每个参数是一组节点,并且必须返回一个字典,表示要在连接B
和C
的边上设置的边数据属性,如果有边在商图中加入B
和C
(如果由edge_relation
确定的商图中没有出现这样的边,则忽略此函数的输出)。如果商图是多重图,则不应用此函数,因为图
G
中每条边的边数据出现在商图的边中。- node_data:函数
此函数采用一个参数
B
,即G
中的一组节点,并且必须返回一个表示节点数据属性的字典,以在商图中表示B
的节点上设置。如果没有,将设置以下节点属性:- ‘graph’,这个块代表的图
G
的子图, - ‘nnodes’,该区块的节点数,
- ‘nedges’,这个块内的边数,
- ‘density’,该块表示的
G
的子图的密度。
- ‘graph’,这个块代表的图
- relabel:bool
如果为 True,则将商图的节点重新标记为非负整数。否则,节点用
frozenset
实例标识,表示在partition
中给出的块。- create_using:NetworkX 图形构造函数,可选(默认=nx.Graph)
要创建的图表类型。如果是图形实例,则在填充之前清除。
- NetworkXException
如果给定的分区不是
G
节点的有效分区。
参数:
返回:
抛出:
参考:
- 1
Patrick Doreian, Vladimir Batagelj, and Anuska Ferligoj.
Generalized Blockmodeling
. Cambridge University Press, 2004.
例子:
“same neighbors”等价关系下的完全二部图的商图为
K_2
。在这种关系下,如果两个节点不相邻但具有相同的邻居集,则它们是等价的。>>> G = nx.complete_bipartite_graph(2, 3) >>> same_neighbors = lambda u, v: ( ... u not in G[v] and v not in G[u] and G[u] == G[v] ... ) >>> Q = nx.quotient_graph(G, same_neighbors) >>> K2 = nx.complete_graph(2) >>> nx.is_isomorphic(Q, K2) True
“同强连通分量”等价关系下的有向图的商图是图的凝聚(见
condensation()
)。此示例来自 Wikipedia 文章`Strongly connected component`_
。>>> G = nx.DiGraph() >>> edges = [ ... "ab", ... "be", ... "bf", ... "bc", ... "cg", ... "cd", ... "dc", ... "dh", ... "ea", ... "ef", ... "fg", ... "gf", ... "hd", ... "hf", ... ] >>> G.add_edges_from(tuple(x) for x in edges) >>> components = list(nx.strongly_connected_components(G)) >>> sorted(sorted(component) for component in components) [['a', 'b', 'e'], ['c', 'd', 'h'], ['f', 'g']] >>> >>> C = nx.condensation(G, components) >>> component_of = C.graph["mapping"] >>> same_component = lambda u, v: component_of[u] == component_of[v] >>> Q = nx.quotient_graph(G, same_component) >>> nx.is_isomorphic(C, Q) True
节点标识可以表示为等价关系下的图的商,该等价关系将两个节点放在一个块中,并且每个其他节点都放在自己的单例块中。
>>> K24 = nx.complete_bipartite_graph(2, 4) >>> K34 = nx.complete_bipartite_graph(3, 4) >>> C = nx.contracted_nodes(K34, 1, 2) >>> nodes = {1, 2} >>> is_contracted = lambda u, v: u in nodes and v in nodes >>> Q = nx.quotient_graph(K34, is_contracted) >>> nx.is_isomorphic(Q, C) True >>> nx.is_isomorphic(Q, K24) True
[1] 中说明的块建模技术可以作为商图来实现。
>>> G = nx.path_graph(6) >>> partition = [{0, 1}, {2, 3}, {4, 5}] >>> M = nx.quotient_graph(G, partition, relabel=True) >>> list(M.edges()) [(0, 1), (1, 2)]
这是示例示例,但使用分区作为块集的字典。
>>> G = nx.path_graph(6) >>> partition = {0: {0, 1}, 2: {2, 3}, 4: {4, 5}} >>> M = nx.quotient_graph(G, partition, relabel=True) >>> list(M.edges()) [(0, 1), (1, 2)]
分区可以用多种方式表示:
(0) a list/tuple/set of block lists/tuples/sets (1) a dict with block labels as keys and blocks lists/tuples/sets as values (2) a dict with block lists/tuples/sets as keys and block labels as values (3) a function from nodes in the original iterable to block labels (4) an equivalence relation function on the target iterable
由于
quotient_graph
被设计为仅接受表示为 (0)、(1) 或 (4) 的分区,因此equivalence_classes
函数可用于获取正确形式的分区,以便调用quotient_graph
。
相关用法
- Python NetworkX negative_edge_cycle用法及代码示例
- Python NetworkX voronoi_cells用法及代码示例
- Python NetworkX numerical_edge_match用法及代码示例
- Python NetworkX inverse_line_graph用法及代码示例
- Python NetworkX LFR_benchmark_graph用法及代码示例
- Python NetworkX write_graph6用法及代码示例
- Python NetworkX DiGraph.__contains__用法及代码示例
- Python NetworkX average_degree_connectivity用法及代码示例
- Python NetworkX eulerian_circuit用法及代码示例
- Python NetworkX single_source_dijkstra_path_length用法及代码示例
- Python NetworkX from_dict_of_dicts用法及代码示例
- Python NetworkX weisfeiler_lehman_subgraph_hashes用法及代码示例
- Python NetworkX transitive_closure_dag用法及代码示例
- Python NetworkX intersection用法及代码示例
- Python NetworkX MultiGraph.size用法及代码示例
- Python NetworkX Graph.size用法及代码示例
- Python NetworkX from_scipy_sparse_array用法及代码示例
- Python NetworkX local_and_global_consistency用法及代码示例
- Python NetworkX number_of_selfloops用法及代码示例
- Python NetworkX single_source_bellman_ford用法及代码示例
- Python NetworkX all_simple_paths用法及代码示例
- Python NetworkX Graph.to_undirected用法及代码示例
- Python NetworkX numeric_assortativity_coefficient用法及代码示例
- Python NetworkX binomial_graph用法及代码示例
- Python NetworkX dedensify用法及代码示例
注:本文由纯净天空筛选整理自networkx.org大神的英文原创作品 networkx.algorithms.minors.quotient_graph。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。