如何在 SQL 中对贝叶斯网络或更普遍的有向加权图进行建模?
我在网上找到了一些文章,提供了如何在 SQL 中对各种图形(特别是 DAG)进行建模的示例,但鉴于它们所建模的内容相对简单,它们看起来都非常复杂。
有没有最好/标准的方法来做到这一点? 我现在的想法是这样的:
create table node (
id int not null auto_increment,
name TEXT
)
create table edge (
from_node int not null,
to_node int not null,
weight float
)
这有什么问题吗? 有人知道更好的(也许更强大的)方法吗?
I found a few articles online providing examples of how to model graphs of various kinds (DAGs, in particular) in SQL, but they all seemed enormously complex, given the relative simplicity of what they're modeling.
Is there a best / standard way of doing this? My current thinking is something like this:
create table node (
id int not null auto_increment,
name TEXT
)
create table edge (
from_node int not null,
to_node int not null,
weight float
)
Is there anything wrong with that? Anyone know of a better (more robust, perhaps) way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将是一个相当合理的方法。 尽管某些系统(例如Oracle或SQL Server)具有递归查询功能,但SQL实际上并不能很好地处理递归结构。
尽管您可能会发现一种更适合特定搜索类型的结构,但我认为您在一般情况下不会找到明显更好的结构。 如果您的应用程序的要求受到这种方式的限制,这样的优化可能会给您带来好处。
由于贝叶斯网络是一个有向无环图(DAG),一种纯粹递归的父子关系不足以对网络进行建模(即一个节点可以有多个父节点),因此您所描述的类型的 M:M 关系将是必要的。
Joe Celko 很好地概述了在 SQL 中实现和查询层次结构和图形结构的技术。 这些是迄今为止我所知道的关于该主题的最佳资源。 强烈推荐。
This would be quite a reasonable approach. SQL does not really do recursive structures well, although some systems such as Oracle or SQL Server have a recursive query function.
Although you may find a structure that works better for specific search types I don't think you will find an appreciably better structure in the general case. If your application's requirements are limited in this way, such an optimisation may bring you benefit.
As a bayesian network is a Directed Acyclic Graph (DAG), a purely recursive parent-child relationship is not sufficient to model the network (i.e. a node can have more than one parent), so a M:M relationship of the type you've described is going to be necessary.
Various of the 'SQL for Smarties' books by Joe Celko give a good overview of techniques for implementing and querying hierarchical and graph structures in SQL. These are by far the best resource on the subject that I know of. Highly recommended.