简体   繁体   English

在有向图中查找循环,返回语句

[英]Find cycle in directed graph, return statement

I coded up a WORKING solution to find a cycle in a DIRECTED graph, however I would like to know the reason to include a return False statement after removing a key from the recursion stack. 我编写了一个WORKING解决方案以在DIRECTED图中找到一个循环,但是我想知道从递归堆栈中删除键后包含return False语句的原因。


class vertex:

  def __init__(self, key):

    self.neighbours = {}
    self.key = key

  def add(self, key, edge):
    self.neighbours[key] = edge

class graph:

  def __init__(self):

    self.root = {}
    self.nbnodes = 0

  def addnode(self, key1, key2, edge=0):
    if key1 not in self.root:
      self.root[key1] = vertex(key1)
      self.nbnodes += 1
    if key2 not in self.root:
      self.root[key2] = vertex(key2)
      self.nbnodes += 1

    self.root[key1].add(key2, edge)

  def dfs(self):

    visited = set()
    recstack = []

    for key in self.root:
      if key not in visited:
        if self._dfs(key, visited, recstack):
          print('cycle')
        else:
          print('No cycle')


  def _dfs(self, key, visited, recstack):

    visited.add(key)
    recstack.append(key)
    for neighbour in self.root[key].neighbours:
      if neighbour not in visited:
        if self._dfs(neighbour, visited, recstack):
          return True
      elif neighbour in recstack:
        return True
    recstack.remove(key)  
    #return False



if __name__ == '__main__':
  a = graph()
  a.addnode(0,1)
  a.addnode(0,2)
  a.addnode(1,3)
  a.addnode(1,4)
  a.addnode(2,4)
  a.addnode(4,5)
  a.dfs()

The above is my final solution code, I commented out the return False statement, I would like to know why to include it. 上面是我的最终解决方案代码,我注释掉return False语句,我想知道为什么要包含它。

Thank you 谢谢

If you ask for the reason, the obvious answer is readability. 如果您询问原因,那么显而易见的答案就是可读性。

Without explicit return False your are implicitly returning None , which, so it happens for fragment if self._dfs(neighbour, visited, recstack): works the same as the commented return False . 如果没有显式的return False您将隐式返回Noneif self._dfs(neighbour, visited, recstack):工作与注释的return False相同,则它会发生在片段中。 Still I was unclear about the intent and would assume you forgot to return it (or wanted to point the reader there is no status of function in the case). 我仍然不清楚这个意图,并且会以为您忘记了返回它(或者想指出读者在这种情况下没有功能状态)。

Citing documentation : 引用文件

None is frequently used to represent the absence of a value, as when default arguments are not passed to a function. 当没有将默认参数传递给函数时,通常不使用None来表示缺少值。 Assignments to None are illegal and raise a SyntaxError. 分配给None是非法的,并引发SyntaxError。

Your value is not absent, it is perfectly defined for your function. 您的价值不存在,它是为您的功能完美定义的。 Other than following good practices, it won't change anything in your code. 除了遵循良好做法,它不会更改您的代码中的任何内容。

BTW. 顺便说一句。 Names of classes are usually capitalized and CamelCase, while functions use snake_case. 类的名称通常使用大写字母和CamelCase命名,而函数使用snake_case。

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

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