def has_cycles(self):
        '''
        Check if the graph has cycles or not.
        We will do this by traversing starting
        from any leaf node and recording
        both the edges traversed and the nodes
        discovered. From stackoverflow, if
        an unexplored edge leads to a
        previously found node then it has
        cycles.
        '''
        discovered_nodes = set()
        traversed_edges = set()
        q = Queue()
        for node in self.nodes:
            if node.is_leaf():
                start_node = node
                break
        q.put(start_node)
        while not q.empty():
            current_node = q.get()
            if DEBUG:
                print "Current Node: ", current_node
                print "Discovered Nodes before adding Current Node: ", \
                    discovered_nodes
            if current_node.name in discovered_nodes:
                # We have a cycle!
                if DEBUG:
                    print 'Dequeued node already processed: %s', current_node
                return True
            discovered_nodes.add(current_node.name)
            if DEBUG:
                print "Discovered Nodes after adding Current Node: ", \
                    discovered_nodes
            for neighbour in current_node.neighbours:
                edge = [current_node.name, neighbour.name]
                # Since this is undirected and we want
                # to record the edges we have traversed
                # we will sort the edge alphabetically
                edge.sort()
                edge = tuple(edge)
                if edge not in traversed_edges:
                    # This is a new edge...
                    if neighbour.name in discovered_nodes:
                        return True
                # Now place all neighbour nodes on the q
                # and record this edge as traversed
                if discovered_nodes not in neighbour.name:
                    if DEBUG:
                        print 'Enqueuing: %s' % neighbour
                    q.put(neighbour)
                traversed_edges.add(edge)
        return False
dataset/ETHPy150Open eBay/bayesian-belief-networks/bayesian/factor_graph.py/FactorGraph.has_cycles/swapped operands of `not in`