擅长:python、mysql、java
<p>你想要的是<a href="https://en.wikipedia.org/wiki/Minimum_spanning_tree" rel="nofollow noreferrer">minimum spanning three</a>。使用<code>networkx</code>可以这样做:</p>
<pre><code>import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from([(1,2), (1,3), (2,3), (4,5), (4,6), (5,6)])
nx.draw(nx.minimum_spanning_tree(G), with_labels=True)
plt.show()
</code></pre>
<p>然而,我有点怀疑<code>networkx</code>是否能够根据<a href="https://www.timlrx.com/2019/05/05/benchmark-of-popular-graph-network-packages/" rel="nofollow noreferrer">to this benchmark</a>在这么多边上执行。我已经在<code>igraph</code>上测试了<code>connected components</code>算法,它对我也很有效(当然,速度要快得多),所以您可能还想寻找基于<code>igraph</code>的解决方案</p>
<h3>结果</h3>
<p><a href="https://i.stack.imgur.com/doRHD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/doRHD.png" alt="enter image description here"/></a></p>