简体   繁体   中英

Number Of Cycles In a Undirected Graph

The problem is as the title suggests, the graph is given in Adjacency List Form. My approach is to call DFS in any one vertex and whenever I hit a visited vertex during my DFS recursive step I increment the counter of a global variable starting from 0 and do not call DFS for that visited vertex (as we do in general). Is this going to work, am I thinking right ? I haven't found any relevant article in the internet as well to learn about #Number of cycles in undirected graph.

Elaboration: I mean to use a simple DFS method. While we do nothing in the recursive step in encountering a visited vertex, I increase the counter global variable value for that situation. I do this, since whenever I am encountering a visited vertex, implies I am completing a cycle. I claim that : every cycle in the graph is encountered only once (atleast and atmost once) in any DFS run. AM I CORRECT IN THIS CLAIM ?

Edit: No back edges

I am sorry to post answer to my answer itself, just for those who have similar doubt in future : I found that I will be counting many cycles redundantly, for eg :-

    A 
  /   \
B ----- C
  \   /
    D

Starting DFS from A->B->C->DI get the cycle CBD counted twice. I also learned the problem for finding all cycles in an undirected graph is np-hard. Bad Luck :'(

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