I have found a simple algorithm to find all cycles in a graph here. I need to print out the cycles too, is it possible with this algorithm. Please find the code below.
I'm getting the number of cycles correctly!
node1, node2 are integers. visited is a dictionary
def dfs(self,node1, node2):
if self.visited[node2]:
if(node1 == node2):
self.count += 1
print node2
return
self.visited[node2] = True
for x in self.adj_lst[node2-1]:
self.dfs(node1, x)
self.visited[node2] = False
def allCycles(self):
self.count = 0
for x in self.VList:
self.dfs(x.num, x.num)
self.visited[x.num] = True
print "Number of cycles: "+str(self.count)