首页 > 解决方案 > 类方法从其先前的执行继承列表(Python)

问题描述

在此处查看完整的工作目录:https ://github.com/sharpvik/python-libs/tree/alpha/Testing

所以我正在研究一些图论和 Python 编码的东西。我创建了一个名为 Graph 的类,如下所示(不用担心缩进,在文件本身就可以了):

class Graph:                                            # undirected graph; no values for the edges;
def __init__(self):
    self.nodes_counter = 0
    self.nodes_dict = dict()

def node_add(self, name):                           # name(int / str) -- name of the node;
    self.nodes_dict.update( { name : list() } )
    self.nodes_counter += 1
    return name

def node_del(self, name):                           # name(int / str) -- name of the node;
    self.nodes_dict.pop(name, None)
    for each in self.nodes_dict:
        self.nodes_dict[each].remove(name)
    self.nodes_counter -= 1
    return name

def connection_add(self, one, two):                 # one(int / str) and two(int / str) -- two nodes you want to connect;
    self.nodes_dict[one].append(two)
    self.nodes_dict[two].append(one)
    return [one, two]

def connection_del(self, one, two):                 # one(int / str) and two(int / str) -- two nodes you want to disconnect;
    self.nodes_dict[one].remove(two)
    self.nodes_dict[two].remove(one)
    return [one, two]

def nodes_count(self):                              # --> function returns the number of nodes in the graph;
    return self.nodes_counter

def nodes_return(self):                             # --> function returns the whole dict containing nodes and their connections;
    return self.nodes_dict

def node_return(self, name):                        # name(int / str) -- name of the node you're checking;
                                                    # --> function returns connections of the given node;
    return self.nodes_dict[name]

# search

## --> breadth first search using class Queue
def bfs( self, final, queue=Queue(None), checked=list() ):      # final(int / str) -- name of the node you're trying to establish connection with;
                                                                # queue(class Queue) -- Queue containing the element you are beginning with (format: element);
                                                                # checked(list) -- leave empty *** internal use ***;
                                                                # --> function returns True if the two nodes are connected, otherwise it returns False;
    if queue.length() == 0:
        return False
    temp = queue.pop()
    if temp == final:
        return True
    else: 
        checked.append(temp)
        for child in self.node_return(temp):
            if child not in checked:
                queue.ins(child)
        return self.bfs(final, queue, checked)
## breadth first search using class Queue <--

## --> depth first serach using class Stack
def dfs( self, final, stack=Stack(None), checked=list() ):      # final(int / str) -- name of the node you're trying to establish connection with;
                                                                # stack(class Stack) -- Stack containing the element you are beginning with (format: element);
                                                                # checked(list) -- leave empty *** internal use ***;
                                                                # --> function returns True if the two nodes are connected, otherwise it returns False;
    if stack.length() == 0:
        return False
    temp = stack.pop()
    if temp == final:
        return True
    else:
        checked.append(temp)
        for child in self.node_return(temp):
            if child not in checked:
                stack.add(child)
        return self.dfs(final, stack, checked)
## depth first serach using class Stack <--

最终的bfsanddfs函数依赖于我的另外两个类:

class Stack:
def __init__(self, element):
    if isinstance(element, list):
        self.storage = element
    else:
        self.storage = [element]

def add(self, element):
    self.storage.append(element)
    return element

def pop(self):
    temp = self.storage[-1]
    self.storage.pop(-1)
    return temp

def length(self):
    return len(self.storage)

class Queue:
def __init__(self, element):
    if isinstance(element, list):
        self.storage = element
    else:
        self.storage = [element]

def ins(self, element):
    self.storage.insert(0, element)
    return element

def pop(self):
    temp = self.storage[0]
    self.storage.pop(0)
    return temp

def length(self):
    return len(self.storage)

现在,在另一个文件中,我初始化了一个Graph我要求g简单的新文件并对其进行了测试。

print("BFS for 1 and 2 # --> should return True")
print( g.bfs(2, Queue(1) ), end="\n\n" )

print("BFS for 1 and 4 # --> should return True")    # but returns False!!!
print( g.bfs(4, Queue(1) ), end="\n\n" )

现在,当我开始调试它时,我注意到由于checked某种原因跟踪检查节点的列表中仍然充满了第一次调用该函数时检查的节点,即使我已将其隐式设置为checked=list()def bfs(...)声明中这样每次有人调用该函数时它都是空的。这种不寻常的行为导致它不检查节点 4,因为它认为它已经被检查过 ( checked = [1, 3, 4]),因此第二次函数确定两个节点未连接并返回 False。

[]如果我在类似之后传递空列表,则可以解决此问题Queue(1)

print( g.bfs(4, Queue(1), [] ) )

然而,这会使事情复杂化,我也只是想知道它为什么会这样做。因此,如果有人知道这里到底发生了什么,我期待着您的回复。

标签: pythonclassgraph-theory

解决方案


推荐阅读