title: Recursive function to check if a graph is acyclic
body: <p>I am a bit confused with using recursive functions in python.Below is my code in python that checks if a given set of points form an acylcic graph. The input format is as follows: the first two numbers in input consist of number of vertices and number of edges. The remaining numbers below represent vertices forming edges whose direction is from left number to right number. For example, check the image below
  <a href="https://i.stack.imgur.com/EsmE5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EsmE5.png" alt="enter image description here"></a></p>
  
  <p>Below is my code that solves this problem. The output provides the number of cycles in the graph</p>
  
  <pre><code>#Uses python3
  
  import sys
  
  def acyclic(adj,cc,in_path):
      # check if value of ad
      global clock
      global cycles
      clock += 1
      for i in adj:
          for j in i:
              if visited[j] != 1:
                  num = reach(adj,j,cc,in_path)
  
                  if (0 not in visited):
                      return cc
                  cc += 1
  
      return cc
  
  def reach(adj,x,cc,in_path):
      visited[x] = 1
      # preVisit(x)
      global cycles
      cnum[x] = cc
      print(x)
      in_path.append(x)
      print(in_path)
      for i in adj[x]:
          print(adj[x])
          if visited[i] != 1:
              return reach(adj,i,cc,in_path)
          elif i in in_path:
              # print("in path")
              cycles += 1
              return (cycles)
              break
      # postVisit(x)
      # return visited[x]
  
  def dfs(adj):
      global m
      global cycles
      # print(m)
      for i in range(n):
          in_path = []
          if visited[i] != 1:
              reach(adj,i,0,in_path)
      print (cycles)
  
  
  
  def preVisit(x):
      global clock
      clock += 1
      prev[x] = clock
      # print("prev")
      # print(prev)
  
  
  def postVisit(x):
      global clock
      clock += 1
      postV[x] = clock
      # print("post")
      # print(postV)
  
  
  
  
  if __name__ == '__main__':
      cycles = 0
      clock = 0
      input = sys.stdin.read()
      data = list(map(int, input.split()))
      n, m = data[0:2]
      data = data[2:]
      edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2]))
      prev = [0 for _ in range(n)]
      postV = [0 for _ in range(n)]
      adj = [[] for _ in range(n)]
      cnum = [0 for i in range(n)]
      visited = [0 for i in range(n)]
      for (a, b) in edges:
          adj[a - 1].append(b - 1)
      print(adj)
      dfs(adj)
  </code></pre>
  
  <p>Whenever I use <code>return "reach(adj,i,cc,in_path)"</code> and <code>"return(cycles)"</code> in the function <code>reach()</code>, I get correct answers. However, taking them off gives me wrong answers. Can someone please explain me why this is happening?</p>
  
  <p>Thanks in advance</p>
  
  <p>Regards,</p>
  
  <p>Rishit</p>