Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在三角形顶点列表中查找连接的组件_Python_Arrays_Graph_Networkx_Connected Components - Fatal编程技术网

Python 在三角形顶点列表中查找连接的组件

Python 在三角形顶点列表中查找连接的组件,python,arrays,graph,networkx,connected-components,Python,Arrays,Graph,Networkx,Connected Components,考虑两个图,G1=(V1,E1),G2=(V2,E2) 在空间中,这些顶点由三角形面连接(每个面有三个顶点) 以上就是我想要找到的。如果给我们整个面阵列: faces = [[ 2, 1, 0], [ 0, 3, 2], [ 1, 4, 0], [ 0, 4, 3], [ 5, 1, 2], [ 3, 5, 2], [ 5, 4, 1], [ 4, 5, 3], [ 8, 7, 6], [ 6, 9, 8], [ 7, 10, 6],

考虑两个图,G1=(V1,E1),G2=(V2,E2)

在空间中,这些顶点由三角形面连接(每个面有三个顶点)

以上就是我想要找到的。如果给我们整个面阵列:

faces = [[ 2,  1,  0],  [ 0,  3,  2],  [ 1,  4,  0],  [ 0,  4,  3],  [ 5,  1,  2],  [ 3,  5,  2],
 [ 5,  4,  1],  [ 4,  5,  3],  [ 8,  7,  6],  [ 6,  9,  8],  [ 7, 10,  6],  [ 6, 10,  9],
 [11,  7,  8],  [ 9, 11,  8],  [11, 10,  7],  [10, 11,  9]]
我们能否找到连接的组件,并将其分为
F1
F2

这个问题的一个版本已经出现在Mathematica中,但我无法翻译


我的工作就在这里。

从你的脸上绘制一个图形非常简单:每个三元组产生3条边,对应于三元组成员的所有组合。然后只需实例化networkx
Graph
对象并调用
networkx.algorithms.components.connected\u components

#!/usr/bin/env python
"""
Given a list of triangles, find the connected components.

https://stackoverflow.com/q/61584283/2912349
"""
import itertools
import networkx as nx

faces = [[ 2,  1,  0],  [ 0,  3,  2],  [ 1,  4,  0],  [ 0,  4,  3],  [ 5,  1,  2],  [ 3,  5,  2],
         [ 5,  4,  1],  [ 4,  5,  3],  [ 8,  7,  6],  [ 6,  9,  8],  [ 7, 10,  6],  [ 6, 10,  9],
         [11,  7,  8],  [ 9, 11,  8],  [11, 10,  7],  [10, 11,  9]]

#create graph
edges = []
for face in faces:
    edges.extend(list(itertools.combinations(face, 2)))
g = nx.from_edgelist(edges)

# compute connected components and print results
components = list(nx.algorithms.components.connected_components(g))

for component in components:
    print(component)

# {0, 1, 2, 3, 4, 5}
# {6, 7, 8, 9, 10, 11}

# separate faces by component
component_to_faces = dict()
for component in components:
    component_to_faces[tuple(component)] = [face for face in faces if set(face) <= component] # <= operator tests for subset relation

for component, component_faces in component_to_faces.items():
    print(component, component_faces)
# (0, 1, 2, 3, 4, 5) [[2, 1, 0], [0, 3, 2], [1, 4, 0], [0, 4, 3], [5, 1, 2], [3, 5, 2], [5, 4, 1], [4, 5, 3]]
# (6, 7, 8, 9, 10, 11) [[8, 7, 6], [6, 9, 8], [7, 10, 6], [6, 10, 9], [11, 7, 8], [9, 11, 8], [11, 10, 7], [10, 11, 9]]
#/usr/bin/env python
"""
给定三角形列表,查找连接的组件。
https://stackoverflow.com/q/61584283/2912349
"""
进口itertools
将networkx导入为nx
面=[[2,1,0],[0,3,2],[1,4,0],[0,4,3],[5,1,2],[3,5,2],
[ 5,  4,  1],  [ 4,  5,  3],  [ 8,  7,  6],  [ 6,  9,  8],  [ 7, 10,  6],  [ 6, 10,  9],
[11,  7,  8],  [ 9, 11,  8],  [11, 10,  7],  [10, 11,  9]]
#创建图形
边=[]
对于面中的面:
扩展(列表(itertools.组合(面,2)))
g=nx.从边缘列表(边缘)
#计算连接的组件并打印结果
组件=列表(nx.algorithms.components.connected_components(g))
对于组件中的组件:
打印(组件)
# {0, 1, 2, 3, 4, 5}
# {6, 7, 8, 9, 10, 11}
#按组件分离面
组件到面=dict()
对于组件中的组件:
组件_到_面[元组(组件)]=[面中面对面,如果设置(面)
faces = [[ 2,  1,  0],  [ 0,  3,  2],  [ 1,  4,  0],  [ 0,  4,  3],  [ 5,  1,  2],  [ 3,  5,  2],
 [ 5,  4,  1],  [ 4,  5,  3],  [ 8,  7,  6],  [ 6,  9,  8],  [ 7, 10,  6],  [ 6, 10,  9],
 [11,  7,  8],  [ 9, 11,  8],  [11, 10,  7],  [10, 11,  9]]
#!/usr/bin/env python
"""
Given a list of triangles, find the connected components.

https://stackoverflow.com/q/61584283/2912349
"""
import itertools
import networkx as nx

faces = [[ 2,  1,  0],  [ 0,  3,  2],  [ 1,  4,  0],  [ 0,  4,  3],  [ 5,  1,  2],  [ 3,  5,  2],
         [ 5,  4,  1],  [ 4,  5,  3],  [ 8,  7,  6],  [ 6,  9,  8],  [ 7, 10,  6],  [ 6, 10,  9],
         [11,  7,  8],  [ 9, 11,  8],  [11, 10,  7],  [10, 11,  9]]

#create graph
edges = []
for face in faces:
    edges.extend(list(itertools.combinations(face, 2)))
g = nx.from_edgelist(edges)

# compute connected components and print results
components = list(nx.algorithms.components.connected_components(g))

for component in components:
    print(component)

# {0, 1, 2, 3, 4, 5}
# {6, 7, 8, 9, 10, 11}

# separate faces by component
component_to_faces = dict()
for component in components:
    component_to_faces[tuple(component)] = [face for face in faces if set(face) <= component] # <= operator tests for subset relation

for component, component_faces in component_to_faces.items():
    print(component, component_faces)
# (0, 1, 2, 3, 4, 5) [[2, 1, 0], [0, 3, 2], [1, 4, 0], [0, 4, 3], [5, 1, 2], [3, 5, 2], [5, 4, 1], [4, 5, 3]]
# (6, 7, 8, 9, 10, 11) [[8, 7, 6], [6, 9, 8], [7, 10, 6], [6, 10, 9], [11, 7, 8], [9, 11, 8], [11, 10, 7], [10, 11, 9]]