Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从邻接列表图形中删除边_Python_Python 3.x_Graph - Fatal编程技术网

Python 从邻接列表图形中删除边

Python 从邻接列表图形中删除边,python,python-3.x,graph,Python,Python 3.x,Graph,我正在玩一个不包含remove\u edge方法的 我的尝试如下所示: def remove_edge(self, src, dest): new_graph = [] for i in range(self.V): node = self.graph[i] while node is not None: if i == src and node.vertex == dest: new_gra

我正在玩一个不包含
remove\u edge
方法的

我的尝试如下所示:

def remove_edge(self, src, dest):
    new_graph = []
    for i in range(self.V):
        node = self.graph[i]
        while node is not None:
            if i == src and node.vertex == dest:
                new_graph.append(AdjNode(src))
            elif i == dest and node.vertex == src:
                new_graph.append(AdjNode(dest))
            else:
                new_graph.append(node)
            node = node.next
    self.graph = new_graph
。。。但当我稍后调用has_edge时,它会导致索引错误。我假设添加一个没有
next
值的新节点可以在这里工作,但我需要做一些不同的事情

以下是完整的代码:

class AdjNode:
    def __init__(self, data):
        self.vertex = data  # int
        self.next = None  # behave like a linked list node


class AdjGraph:
    def __init__(self, num_vertices):
        self.V = num_vertices
        self.graph = [None] * self.V

    # Function to print the graph
    def print_graph(self):
        for i in range(self.V):
            print("Adjacency list of vertex {}\n head".format(i), end="")
            temp = self.graph[i]
            while temp:
                print(" -> {}".format(temp.vertex), end="")
                temp = temp.next
            print(" \n")

    def add_edge(self, src, dest):
        # undirected...
        node = AdjNode(dest)
        node.next = self.graph[src]
        self.graph[src] = node

        node = AdjNode(src)
        node.next = self.graph[dest]
        self.graph[dest] = node

    def has_edge(self, src, dest):
        for i in range(self.V):
            node = self.graph[i]
            while node is not None:
                if i == src and node.vertex == dest:
                    return True
                if i == dest and node.vertex == src:
                    return True
                node = node.next
        return False


# tests:
graph = AdjGraph(5)
graph.add_edge(0, 1)
graph.add_edge(3, 1)

print(graph.has_edge(1, 3))  # True, as expected
graph.remove_edge(1, 3)
print(graph.has_edge(1, 3)) # expecting False here but I get an error:


  File "~\Desktop\pytester.py", line 96, in <module>
    print(graph.has_edge(1, 3))
  File "~\Desktop\pytester.py", line 64, in has_edge
    node = self.graph[i]
IndexError: list index out of range
类节点:
定义初始化(自身,数据):
self.vertex=data#int
self.next=None#行为类似于链表节点
类别调整图:
def uuu init uuuu(self,num_顶点):
self.V=num_顶点
self.graph=[None]*self.V
#函数打印图形
def打印图形(自):
对于范围内的i(self.V):
打印(“顶点{}\n头的邻接列表”。格式(i),end=“”)
温度=自曲线图[i]
而温度:
打印(“->{}”.format(temp.vertex),end=”“)
温度=下一个温度
打印(“\n”)
def添加_边缘(自身、src、目的地):
#无方向的。。。
节点=调整节点(目的地)
node.next=self.graph[src]
self.graph[src]=节点
节点=调整节点(src)
node.next=self.graph[dest]
self.graph[dest]=节点
def具有_边缘(自身、src、目的地):
对于范围内的i(self.V):
node=self.graph[i]
当节点不是“无”时:
如果i==src和node.vertex==dest:
返回真值
如果i==dest和node.vertex==src:
返回真值
node=node.next
返回错误
#测试:
图=调整图(5)
图.添加_边(0,1)
图.添加_边(3,1)
打印(如预期,图形具有_边(1,3))35;真
图.删除_边(1,3)
print(graph.has_edge(1,3))35;此处应为False,但我得到一个错误:
文件“~\Desktop\pytester.py”,第96行,在
打印(图有_边(1,3))
文件“~\Desktop\pytester.py”,第64行,在
node=self.graph[i]
索引器:列表索引超出范围

奇怪:基于代码的
具有_edge
实现毫无理由是非常低效的。它与合理的
remove_edge
也几乎没有关系,因此我不确定这里要回答什么,这不是对后一个函数的完全重写。我知道,在邻接列表图中,边查询比矩阵表示要花费更长的时间,但我不知道这是不必要的低效。如果我存储一个顶点(int键)到边映射(int键列表)的dict并遍历它,那么这个图还会被认为是一个邻接列表图吗?它仍然是一个邻接列表,不管它的容器是什么,但效率低下与容器类型无关。当你知道你要找的端点时,为什么你会看所有的邻接表?我的假设是GeksFFEGEGEICS家伙用java或C++的另一种语言编写代码,使用一个带O(n)的链表。为了搜索,他们没有对其进行优化,因为他们试图证明在效率上的权衡。但该代码将该方法的效率夸大了一半,这是疯狂的。我(更悲伤)的假设是没有人检查他们在那里的工作。