简体   繁体   中英

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. 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.

Here is the image for better understanding : 在此处输入图片说明

Then here 2 cycles should be counted that are ABEDA and BCFEB but not 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). 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). 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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