python - 高效增量实现poset

标签 python sql sqlalchemy poset

我正在使用 SQLAlchemy 实现一个具有 Partially Ordered Set 数学特征的结构,其中我需要能够一次添加和删除一条边。

在我目前最好的设计中,我使用了两个邻接列表,一个是分配列表(大约是哈斯图中的边),因为我需要保留哪些节点对被明确设置为有序,另一个是邻接列表是第一个的传递闭包,这样我就可以有效地查询一个节点是否相对于另一个节点有序。现在,每次在分配邻接列表中添加或删除边时,我都会重新计算传递闭包。

看起来像这样:

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() 的实用性,并且只有分配被刷新到 session 中并有适当的连接。如果我想看到 ORM 中反射(reflect)的更改,我必须再次刷新 session 。我想,如果我能用 orm 来表达重新计算祖先操作,那就容易多了。

最佳答案

好吧,我去解决了我自己的问题。它的粗略部分是在父节点的祖先的后代与子节点的后代的祖先的交集上应用Floyd-Warshall算法,但仅将输出应用于 parent 的祖先和 child 的后代的联盟。我花了很多时间在上面我最终发布了过程 on my blog ,但这是代码。

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

association_table = Table('edges', Base.metadata,
    Column('predecessor', Integer, 
           ForeignKey('nodes.id'), primary_key=True),
    Column('successor', Integer, 
           ForeignKey('nodes.id'), primary_key=True))

path_table = Table('paths', Base.metadata,
    Column('predecessor', Integer, 
           ForeignKey('nodes.id'), primary_key=True),
    Column('successor', Integer, 
           ForeignKey('nodes.id'), primary_key=True))

class Node(Base):
    __tablename__ = 'nodes'
    id = Column(Integer, primary_key=True)
    # extra columns

    def __repr__(self):
        return '<Node #%r>' % (self.id,)

    successors = relationship('Node', backref='predecessors',
        secondary=association_table,
        primaryjoin=id == association_table.c.predecessor,
        secondaryjoin=id == association_table.c.successor)

    before = relationship('Node', backref='after',
        secondary=path_table,
        primaryjoin=id == path_table.c.predecessor,
        secondaryjoin=id == path_table.c.successor)

    def __lt__(self, other):
        return other in self.before

    def add_successor(self, other):
        if other in self.successors:
            return
        self.successors.append(other)
        self.before.append(other)
        for descendent in other.before:
            if descendent not in self.before:
                self.before.append(descendent)
        for ancestor in self.after:
            if ancestor not in other.after:
                other.after.append(ancestor)

    def del_successor(self, other):
        if not self < other:
            # nodes are not connected, do nothing!
            return
        if not other in self.successors:
            # nodes aren't adjacent, but this *could*
            # be a warning...
            return

        self.successors.remove(other)

        # we buld up a set of nodes that will be affected by the removal
        # we just did.  
        ancestors = set(other.after)
        descendents = set(self.before)

        # we also need to build up a list of nodes that will determine
        # where the paths may be.  basically, we're looking for every 
        # node that is both before some node in the descendents and
        # ALSO after the ancestors.  Such nodes might not be comparable
        # to self or other, but may still be part of a path between
        # the nodes in ancestors and the nodes in descendents.
        ancestors_descendents = set()
        for ancestor in ancestors:
            ancestors_descendents.add(ancestor)
            for descendent in ancestor.before:
                ancestors_descendents.add(descendent)

        descendents_ancestors = set()
        for descendent in descendents:
            descendents_ancestors.add(descendent)
            for ancestor in descendent.after:
                descendents_ancestors.add(ancestor)
        search_set = ancestors_descendents & descendents_ancestors

        known_good = set() # This is the 'paths' from the 
                           # original algorithm.  

        # as before, we need to initialize it with the paths we 
        # know are good.  this is just the successor edges in
        # the search set.
        for predecessor in search_set:
            for successor in search_set:
                if successor in predecessor.successors:
                    known_good.add((predecessor, successor))

        # We now can work our way through floyd_warshall to resolve
        # all adjacencies:
        for ancestor in ancestors:
            for descendent in descendents:
                if (ancestor, descendent) in known_good:
                    # already got this one, so we don't need to look for an
                    # intermediate.  
                    continue
                for intermediate in search_set:
                    if (ancestor, intermediate) in known_good \
                            and (intermediate, descendent) in known_good:
                        known_good.add((ancestor, descendent))
                        break # don't need to look any further for an
                              # intermediate, we can move on to the next
                              # descendent.  


        # sift through the bad nodes and update the links
        for ancestor in ancestors:
            for descendent in descendents:
                if descendent in ancestor.before \
                        and (ancestor, descendent) not in known_good:
                    ancestor.before.remove(descendent)

关于python - 高效增量实现poset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6131098/

相关文章:

sql - 使用 SQL/SQLAlchemy 过滤选定的列

python - 当 pytest 测试断言查询结果 rowcount 失败时数据库操作挂起

python - 只读对象模型的 SqlAlchemy 优化

Python 无效语法

python - 仅基于 2 列获取唯一行

mysql - 替换 MySQL 数据库所有表上的文本

MySQL/SQL 多表查询

mysql - 使用 MySql 外键连接 2 个表

python - 如何在保留调试的同时从字符串加载 Python 模块?

python - 在 python asyncio 上获取第一个可用的锁/信号量