[英]Is there an efficient algorithm to detect dependency cycles in complex data structures?
I have a smallish, complex database (a few millions of records split over very low thousands of tables). 我有一个小而复杂的数据库(几百万条记录分散在非常低的数千个表中)。 The records can be thought of as business rules.
可以将记录视为业务规则。 There is provision for users to define their own rules, in terms of existing rules (including other user defined rules).
用户可以根据现有规则(包括其他用户定义的规则)定义自己的规则。 These rules are dependent on other rules, sometimes via complex paths.
这些规则有时会通过复杂的路径依赖于其他规则。 The dependencies form an extended network, rather than a hierarchy.
依赖关系形成一个扩展的网络,而不是一个层次结构。
I am looking for an algorithm to determine, in a newly defined rule (or set of rules) whether the new rule is itself cyclic, or whether it creates cycles when taken together with existing rules. 我正在寻找一种算法,用于在新定义的规则(或规则集)中确定新规则本身是否是循环的,或者与现有规则一起使用时是否会创建循环。
I need an algorithm that is efficient in the following circumstances: 我需要一种在以下情况下有效的算法:
EDIT 编辑
I am accepting Nick's answer, with one modification. 我接受尼克的回答,但有一个修改。 Storing the dependencies is a very easy modification to the database.
存储依赖项是对数据库的非常简单的修改。 I am only going to store the direct dependencies rather than all dependencies whether direct or indirect.
我将只存储直接依赖关系,而不是存储所有直接或间接依赖关系。 I can view the two sets of dependency C,D,F,G and X,Y,Z (in Nick's answer) as tree structures, and use one of the various techniques for deriving hierarchical structures from a single level dependency table.
我可以将两组相关性C,D,F,G和X,Y,Z(由Nick回答)视为树结构,并使用各种技术之一从一个级别的相关性表中导出层次结构。 I think the cost of this will be acceptable in this context.
我认为在这种情况下,这样做的代价是可以接受的。
EDIT 编辑
I hope I understood your problem correctly: 希望我能正确理解您的问题:
Lets assume you add rule A to the database, then you also add dependency information like A depends on C,D,F,G
and X,Y,Z depend on A
. 假设您将规则A添加到数据库,然后还添加依赖项信息,例如
A depends on C,D,F,G
以及X,Y,Z depend on A
I would assume there is no way of detecting a cycle at insertion time without really looking at the whole structure, which you say is disallowed. 我认为如果没有真正查看整个结构,就无法在插入时检测到周期,这是不允许的。
So my idea would be to have everything precomputed and stored, ie for each rule R store all other rules it depends on (not only directly, but also indirectly). 因此,我的想法是预先计算并存储所有内容,即对于每个规则R,存储它依赖的所有其他规则(不仅直接,而且间接)。 Now when you insert rule A simply get all dependencies from
C, D, F, G
and see if they include any of X,Y,Z or A
if they don't there is no cycle and you can safely add A to your ruleset and store all the dependencies from C, D, F, G
plus C, D, F, G
themselves as A's dependecies. 现在,当您插入规则A时,只需从
C, D, F, G
获取所有依赖项C, D, F, G
然后查看它们是否包含X,Y,Z or A
如果它们没有循环,则可以安全地将A添加到规则集中并将C, D, F, G
以及C, D, F, G
本身的所有依赖项存储为A的依赖项。
This of course requires some restructuring (and rebuilding) of the database. 当然,这需要对数据库进行一些重组(和重建)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.