高效增量实现poset
我正在根据 SQLAlchemy 实现一个具有 偏序集 数学特征的结构,我需要能够一次添加和删除一个边缘。
在我当前的最佳设计中,我使用两个邻接列表,一个是分配列表(大约是哈斯图中的边),因为我需要保留哪些节点对被显式设置为有序,另一个邻接列表是传递的第一个的关闭,以便我可以有效地查询一个节点是否相对于另一个节点排序。现在,每次在分配邻接列表中添加或删除一条边时,我都会重新计算传递闭包。
它看起来像这样:
assignment = Table('assignment', metadata,
Column('parent', Integer, ForeignKey('node.id')),
Column('child', Integer, ForeignKey('node.id')))
closure = Table('closure', metadata,
Column('ancestor', Integer, ForeignKey('node.id')),
Column('descendent', Integer, ForeignKey('node.id')))
class Node(Base):
__tablename__ = 'node'
id = Column(Integer, primary_key=True)
parents = relationship(Node, secondary=assignment,
backref='children',
primaryjoin=id == assignment.c.parent,
secondaryjoin=id == assignment.c.child)
ancestors = relationship(Node, secondary=closure,
backref='descendents',
primaryjoin=id == closure.c.ancestor,
secondaryjoin=id == closure.c.descendent,
viewonly=True)
@classmethod
def recompute_ancestry(cls.conn):
conn.execute(closure.delete())
adjacent_values = conn.execute(assignment.select()).fetchall()
conn.execute(closure.insert(), floyd_warshall(adjacent_values))
其中 floyd_warshall()
是同名算法的实现。
这导致我遇到两个问题。首先,它似乎效率不高,但我不确定可以使用哪种算法。
第二个更多的是关于每次分配发生时都必须显式调用 Node.recompute_ancestry() 的实用性,并且仅在分配被刷新到会话中之后才使用正确的连接。如果我想查看 ORM 中反映的更改,我必须再次刷新会话。我想,如果我能用 orm 来表达重新计算祖先操作,那就容易多了。
I'm implementing in terms of SQLAlchemy a structure that has the mathematical characteristic of Partially Ordered Set, in which I need to be able to add and remove edges one at a time.
In my current, best design, I use two adjacency lists, one being the assignment list (approximately edges in the Hass Diagram), since I need to preserve which pairs of nodes are explicitly set as ordered, and the other adjacency list is the transitive closure of the first, so that I can efficiently query if one node is ordered with respect to another. Right now, I recompute the transitive closure each time an edge is added to or removed from the assignment adjacency list.
It looks something like this:
assignment = Table('assignment', metadata,
Column('parent', Integer, ForeignKey('node.id')),
Column('child', Integer, ForeignKey('node.id')))
closure = Table('closure', metadata,
Column('ancestor', Integer, ForeignKey('node.id')),
Column('descendent', Integer, ForeignKey('node.id')))
class Node(Base):
__tablename__ = 'node'
id = Column(Integer, primary_key=True)
parents = relationship(Node, secondary=assignment,
backref='children',
primaryjoin=id == assignment.c.parent,
secondaryjoin=id == assignment.c.child)
ancestors = relationship(Node, secondary=closure,
backref='descendents',
primaryjoin=id == closure.c.ancestor,
secondaryjoin=id == closure.c.descendent,
viewonly=True)
@classmethod
def recompute_ancestry(cls.conn):
conn.execute(closure.delete())
adjacent_values = conn.execute(assignment.select()).fetchall()
conn.execute(closure.insert(), floyd_warshall(adjacent_values))
where floyd_warshall()
is an implementation of the algorithm by the same name.
This is leading me to two problems. The first is that It doesn't seem to be very efficient, but I'm not sure of what sort of algorithm I could use instead.
The second is more about the practicality of having to explicitly call Node.recompute_ancestry()
each time an assignment occurs, and only after the assignments are flushed into the session and with the proper connections. If I want to see the changes reflected in the ORM, I'd have to flush the session again. It would be much easier, I think, If I could express the recompute ancestry operation in terms of the orm.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我去想办法解决我自己的问题。它的粗略部分是将Floyd-Warshall算法应用于父节点的祖先的后代与子节点的后代的祖先的交集,但仅将输出应用于父母的祖先和孩子的后代的联合。我花了很多时间在上面,最终发布了该过程
Well, I went and worked out the solution to my own problem. The crude part of it is to apply the Floyd-Warshall algorithm on the intersection of the descendents of the ancestors of the parent node with the ancestors of the descendents of the child node, but only apply the output to the union of the parent's ancestors and child's descendents. I spent so much time on it I ended up posting the process on my blog, but here is teh codes.
插入时更新闭包,并根据 orm 执行此操作:
如果需要删除分配,这在纯 sql 中更快:
Update closure as you insert, and do so in terms of orm:
If you need to delete assignments, this is faster in pure sql: