【问题标题】:Delete key and its values in a double nested dictionary in O(1) without loop?在没有循环的O(1)中删除双嵌套字典中的键及其值?
【发布时间】:2016-08-24 03:31:08
【问题描述】:

我正在学习 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:

def remove_vertex(self, v):
"""Remove the vertex v and all its incident edges,
and return the vertex been removed.
Parameter v is an instance of Vertex
Algorithm should run in O(deg(v)) time
"""

 for i in self.incident_edges(v):
   self.remove_edge(i)

 del self._outgoing[v]

 return v

这是我使用 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) 中的嵌套字典中导航。不知道该怎么办!请有更多经验的人帮忙!

电流输出:

    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

谢谢!

PS:如果您可能需要更多参考,请告诉我!再次感谢!

【问题讨论】:

  • 您能否发布您当前正在生成的输出的快照和预期输出的快照?
  • 嗨@AshSharma,我已将其添加到我的帖子中!谢谢!

标签: python dictionary data-structures graph nested


【解决方案1】:

将您的图形表示 Adjacency List (https://en.wikipedia.org/wiki/Adjacency_list) 转换为 Adjacency matrix (https://en.wikipedia.org/wiki/Adjacency_matrix) ?

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

但是,Adjacency ListAdjacency Matrix 的转换是在 O(|E|) 中完成的(其中 E 是您的一组边),但我认为您的练习中没有考虑到它。

【讨论】:

猜你喜欢
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
  • 2019-11-18
  • 2018-11-03
  • 1970-01-01
相关资源
最近更新 更多