[英]Algorithm to simplify/reduce graph
Are you looking for something out-of-the box or for algorithmic ideas on how to implement this yourself? 您是否正在寻找开箱即用的东西或关于如何自己实现此算法的想法? I can help you with the latter.
我可以为您提供帮助。
What you want to do is called contraction of vertices which have exactly 2
neighbours, ie that have degree 2
. 您要执行的操作称为顶点收缩 ,该顶点具有
2
邻居,即2
度。
For implementing this, do the following: 为此,请执行以下操作:
while exists vertex v with degree 2:
- remove v and the 2 outgoing edges
- add a new edge between the neighbours of v
- the weight of the new edge is the sum of the weights of the deleted edge
That is, if you have the following part of the graph: u ---2--- v ---5--- w
and apply the contraction, you end up with u ---7--- w
. 也就是说,如果您具有图形的以下部分:
u ---2--- v ---5--- w
并执行收缩,则最终得到u ---7--- w
。
Just doing this iteratively until no vertex wit degree 2
remains will transform the graph in the first picture into the graph in your second picture. 只是迭代地执行此操作直到没有顶点和度
2
剩余,才会将第一张图片中的图形转换为第二张图片中的图形。
The exact implementation details will, of course, depend on which data structure you use to represent your graph in Python (or any other language that is being used). 当然,确切的实现细节将取决于您使用哪种数据结构以Python(或使用的任何其他语言)表示图形。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.