简体   繁体   English

无向图中的总循环

[英]Total cycles in a Undirected graph

Given an undirected graph with N vertices and M edges I need to find the number of cycles in the graph. 给定一个具有N个顶点和M个边的无向图,我需要找到图中的循环数。 But there is a constraint. 但是有一个约束。

Here is an example of it: Consider this graph with 6 vertices and 7 edge pairs :- AB , BC , CF , AD , DE , EF , BE. 这是一个示例:考虑具有6个顶点和7个边对的图形:-AB,BC,CF,AD,DE,EF,BE。

Here is the image for better understanding : 这是更好理解的图像: 在此处输入图片说明

Then here 2 cycles should be counted that are ABEDA and BCFEB but not ABCFEDA 然后在这里应计算两个周期,即ABEDA和BCFEB,而不是ABCFEDA

So I need to find the count of the total cycles in the graph. 因此,我需要在图中找到总周期数。

I think you are looking for a cycle basis of your graph. 我认为您正在寻找图形的周期基础 You do that by finding any spanning tree of the graph (for example a DFS or BFS tree). 通过查找图的任何生成树(例如DFS或BFS树)来实现。 The non-tree edges of the graph represent a cycle basis: If you connect the endpoints by the unique path through the tree, you get an element of the basis. 图的非树边缘代表循环基础:如果通过树上的唯一路径连接端点,则将获得基础元素。

So if the graph is connected, the number of basis elements is m - n + 1 (m = number of edges, n = number of nodes). 因此,如果连接了图,则基本元素的数量为m - n + 1 (m =边数,n =节点数)。 If it's not connected, just decompose it into connected components and sum up the number of basis elements of the components. 如果未连接,只需将其分解为已连接的组件,然后汇总这些组件的基本元素的数量。 You get something like m - n + c where c is the number of connected components. 您会得到类似m - n + c ,其中c是连接的组件数。 Thus, if you're not interested in the actual cycles and only in their count, you just need to find the number of connected components. 因此,如果您对实际周期不感兴趣,而仅对周期数感兴趣,则只需查找已连接组件的数量即可。 You can use DFS or BFS for that as well. 您也可以使用DFS或BFS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM