[英]How to find all unique permutations of a set of elements over a graph
I have a material science problem which I am reasonably sure can be solved using.networkx, but I'm not sure how.我有一个材料科学问题,我有理由相信可以使用 .networkx 来解决,但我不确定如何解决。
Firstly I would like to find all unique combinations of 3 elements, with replacement.首先,我想找到 3 个元素的所有唯一组合,并进行替换。 This I have already done with itertools as follows:
我已经用 itertools 完成了,如下所示:
elements = ["Mg","Cu","Zn"]
combinations = list(itertools.combinations_with_replacement(elements, 3))
For each of these combinations, I would like to find all unique permutations over a simple graph.对于这些组合中的每一个,我想在一个简单的图形上找到所有唯一的排列。 The graph has three nodes and three edges, where each node is connected to two other nodes.
该图具有三个节点和三个边,其中每个节点都连接到另外两个节点。 Importantly, the edges have a distance of 1, but one of the edges has a distance of 2. Basically, like a right-angle triangle.
重要的是,边的距离为 1,但其中一条边的距离为 2。基本上,就像一个直角三角形。
eg something like Node1 <-Distance=1-> Node2 <-Distance=2-> Node3 <-Distance=1-> Node1例如像 Node1 <-Distance=1-> Node2 <-Distance=2-> Node3 <-Distance=1-> Node1
So for the combination ["Mg", "Cu", "Cu"] there should be two unique permutations:所以对于组合 ["Mg", "Cu", "Cu"] 应该有两个独特的排列:
a ) Mg(site1) -1- Cu(site2) -1- Mg(site3) -2- Mg(site1) a ) Mg(site1) -1- Cu(site2) -1- Mg(site3) -2- Mg(site1)
b ) Mg(site1) -1- Mg(site2) -1- Cu(site3) -2- Mg(site1) b ) Mg(site1) -1- Mg(site2) -1- Cu(site3) -2- Mg(site1)
c ) Cu(site1) -1- Mg(site2) -1- Mg(site3) -2- Cu(site1) (This is the same as b ) c ) Cu(site1) -1- Mg(site2) -1- Mg(site3) -2- Cu(site1) (这与b相同)
NOTE: I'm not sure of the best way to define the graph, it could be something like:注意:我不确定定义图表的最佳方式,它可能类似于:
import networkx as nx
FG = nx.Graph()
FG.add_weighted_edges_from([(1, 2, 1), (2, 3, 1), (3, 1, 2)])
The uniqueness criteria you want to use is called graph isomorphism .您要使用的唯一性标准称为图同构。 NetworkX has a submodule for it: networkx.algorithms.isomorphism .
NetworkX 有一个子模块: networkx.algorithms.isomorphism 。 You can specify how exactly your nodes/edges of graphs should be treated as "equal" with
node_match/edge_match
parameters.您可以使用
node_match/edge_match
参数指定图形的节点/边应该如何被视为“相等”。 Here is the example:这是示例:
import networkx as nx
FG1 = nx.Graph()
FG1.add_node(1, element='Cu')
FG1.add_node(2, element='Cu')
FG1.add_node(3, element='Mg')
FG1.add_weighted_edges_from([(1, 2, 1), (2, 3, 1), (3, 1, 2)])
FG2 = nx.Graph()
FG2.add_node(1, element='Cu')
FG2.add_node(2, element='Mg')
FG2.add_node(3, element='Cu')
FG2.add_weighted_edges_from([(1, 3, 1), (2, 3, 1), (1, 2, 2)])
nx.is_isomorphic(
FG1,
FG2,
node_match=lambda n1, n2: n1['element'] == n2['element'],
edge_match=lambda e1, e2: e1['weight'] == e2['weight']
)
True
If you will rename any element or change any edge weight, graphs will become non-isomorphic (with those parameters).如果您要重命名任何元素或更改任何边权重,图形将变为非同构(使用这些参数)。 It is how you can find unique graphs - the set of non-isomorphic graphs.
这就是您如何找到独特的图 - 一组非同构图。 Note, that graph isomorphism problem is very computational-heavy so you should not use it even for medium-sized graphs.
请注意,图同构问题的计算量非常大,因此即使是中型图也不应该使用它。
But your task has so many restrictions that graphs usage is not necessary.但是您的任务有很多限制,因此没有必要使用图表。 If you have only 3 element in a "molecule", you will have only 3 types of element combinations:
如果“分子”中只有 3 个元素,则只有 3 种元素组合:
1-1-1
1-1-2
1-2-3
For each of them you can calculate and state the number of unique combinations:对于它们中的每一个,您可以计算 state 独特组合的数量:
1-1-1
: One - 1=1-1
1-1-1
: 一 - 1=1-1
1-1-2
: Two - 1=1-2
and 1-1=2
1-1-2
: 两个 - 1=1-2
和1-1=2
1-2-3
: Three - 1=2-3
, 1-2=3
and 1-2-3(=1)
1-2-3
: 三 - 1=2-3
, 1-2=3
和1-2-3(=1)
So you can just multiply each itertools-combination to the number of possible combinations:所以你可以将每个 itertools-combination 乘以可能的组合数:
number_of_molecular_combinations = 0
for c in combinations:
number_of_molecular_combinations += len(set(c))
print(number_of_molecular_combinations)
18
This method will work FAR faster than graph processing but is usable only in the case of very strong restrictions, like yours.这种方法的工作速度比图形处理快得多,但只能在非常严格的限制情况下使用,比如你的限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.