Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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 删除O(1)中双嵌套字典中的键及其值而不使用循环?_Python_Dictionary_Data Structures_Graph_Nested - Fatal编程技术网

Python 删除O(1)中双嵌套字典中的键及其值而不使用循环?

Python 删除O(1)中双嵌套字典中的键及其值而不使用循环?,python,dictionary,data-structures,graph,nested,Python,Dictionary,Data Structures,Graph,Nested,我正在学习python中的图形数据结构,其中一个问题是基于无向图和有向图。它要求您在O(deg(v))时间内删除顶点,并在O(1)时间内删除边。我已经设法删除了顶点,但在删除顶点后,需要删除从该顶点到该顶点的边。delete_edge fxn是我遇到的问题,因为这是一个嵌套字典,我发现很难删除边 这是无向原始图: C {A: (A,C,2), B: (B,C,5), E: (C,E,7), D: (C,D,6)} A {B: (A,B,1), C: (A,C,2), E: (A,E,4),

我正在学习python中的图形数据结构,其中一个问题是基于无向图和有向图。它要求您在O(deg(v))时间内删除顶点,并在O(1)时间内删除边。我已经设法删除了顶点,但在删除顶点后,需要删除从该顶点到该顶点的边。delete_edge fxn是我遇到的问题,因为这是一个嵌套字典,我发现很难删除边

这是无向原始图:

 C {A: (A,C,2), B: (B,C,5), E: (C,E,7), D: (C,D,6)}
 A {B: (A,B,1), C: (A,C,2), E: (A,E,4), D: (A,D,3)}
 B {A: (A,B,1), C: (B,C,5)}
 E {A: (A,E,4), C: (C,E,7)}
 D {A: (A,D,3), C: (C,D,6)}
这是用于查找给定顶点的所有关联边的fxn:

def incident_edges(self, v, outgoing=True):   
#Return all (outgoing) edges incident to vertex v in the graph.
#If graph is directed, optional parameter used to request incoming edges.

self._validate_vertex(v)
adj = self._outgoing if outgoing else self._incoming
for edge in adj[v].values():
  yield edge
这是我写的在O(deg(v))时间内删除顶点的fxn:

这是我使用remove_edge fxn所取得的成绩:

def remove_edge(self, e):
"""remove the edge e from the adjacency map for each
incident vertex, and return the edge removed.
Parameter e is an instance of Edge
Algorithm should run in O(1) time.
""" 
   list(list(self._outgoing.values())[list(self._outgoing.values())[list(self._outgoing.values()).index(e)]])
但它不起作用!我似乎无法在O(1)中的嵌套dict中导航。不知道该怎么办!有经验的人请帮忙

电流输出:

    Undirected Original Graph:
    D {A: (A,D,3), C: (C,D,6)}
    C {A: (A,C,2), B: (B,C,5), D: (C,D,6), E: (C,E,7)}
    B {A: (A,B,1), C: (B,C,5)}
    A {B: (A,B,1), C: (A,C,2), D: (A,D,3), E: (A,E,4)}
    E {A: (A,E,4), C: (C,E,7)}

    Number of vertices is 5
    Number of edges is 7

    Undirected Graph After deleting Vertex 'D':
    (which consequently deletes its incident edges)
    C {A: (A,C,2), B: (B,C,5), D: (C,D,6), E: (C,E,7)}
    B {A: (A,B,1), C: (B,C,5)}
    A {B: (A,B,1), C: (A,C,2), D: (A,D,3), E: (A,E,4)}
    E {A: (A,E,4), C: (C,E,7)}

    Number of vertices is 4
    Number of edges is 6
预期产出:

    Undirected Original Graph:
    D {A: (A,D,3), C: (C,D,6)}
    C {A: (A,C,2), B: (B,C,5), D: (C,D,6), E: (C,E,7)}
    B {A: (A,B,1), C: (B,C,5)}
    A {B: (A,B,1), C: (A,C,2), D: (A,D,3), E: (A,E,4)}
    E {A: (A,E,4), C: (C,E,7)}

    Number of vertices is 5
    Number of edges is 7

    Undirected Graph After deleting Vertex 'D':
    (which consequently deletes its incident edges)
    C {A: (A,C,2), B: (B,C,5), E: (C,E,7)}
    B {A: (A,B,1), C: (B,C,5)}
    A {B: (A,B,1), C: (A,C,2), E: (A,E,4)}
    E {A: (A,E,4), C: (C,E,7)}

    Number of vertices is 4
    Number of edges is 6
谢谢


附言:如果有什么遗漏,你可能需要更多的参考资料,请让我知道!再次感谢

将图形表示法
邻接列表
()转换为
邻接矩阵
()

在我看来,它更适合于您的用例,因为如果您这样做,您可以在两个操作中删除一个节点及其边缘,即“删除对应于您节点的行”和“删除对应于您节点的列”。这可以在
O(1)
中完成


然而,将
邻接列表
转换为
邻接矩阵
是在
O(| E |)
中完成的(E是你的边集),但我认为在你的练习中没有考虑到它。

你能发布你当前生成的输出的快照和预期输出的快照吗,我已将其添加到我的帖子中!谢谢考虑查看<代码>关联矩阵< /代码>文章()。
    Undirected Original Graph:
    D {A: (A,D,3), C: (C,D,6)}
    C {A: (A,C,2), B: (B,C,5), D: (C,D,6), E: (C,E,7)}
    B {A: (A,B,1), C: (B,C,5)}
    A {B: (A,B,1), C: (A,C,2), D: (A,D,3), E: (A,E,4)}
    E {A: (A,E,4), C: (C,E,7)}

    Number of vertices is 5
    Number of edges is 7

    Undirected Graph After deleting Vertex 'D':
    (which consequently deletes its incident edges)
    C {A: (A,C,2), B: (B,C,5), E: (C,E,7)}
    B {A: (A,B,1), C: (B,C,5)}
    A {B: (A,B,1), C: (A,C,2), E: (A,E,4)}
    E {A: (A,E,4), C: (C,E,7)}

    Number of vertices is 4
    Number of edges is 6