简体   繁体   English

c# - 定向图的最佳持久性方法/工具/库是什么

[英]c# - what is best persistence approach/tool/library for a Directed Graph

What is best persistence approach/tool/library for a Directed Graph in C#. 什么是C#中的定向图的最佳持久性方法/工具/库。 That is assuming I have a class model for a directed graph (eg Nodes & Relationships, or Vertex's and Edge's if you like), what would you recommend regarding persisting to a SQL Database? 这假设我有一个有向图的类模型(例如节点和关系,或者如果你喜欢的是Vertex和Edge),你会建议你坚持使用SQL数据库吗? (or if you wish a 2nd question would be where I don't specify a SQL database as a requirement) (或者,如果您希望第二个问题是我没有指定SQL数据库作为要求)

For example I was thinking I would simply go with a Relationships table and a Nodes table. 例如,我想我会简单地使用Relationships表和Nodes表。

I'm not familiar with the tools available in C#, but your question seems to be mostly one about storage rather than C#. 我不熟悉C#中可用的工具,但你的问题似乎主要是关于存储而不是C#。

How you store a DAG will no doubt depend on the use cases you have in mind. 如何存储DAG无疑将取决于您的用例。 You can represent a DAG in two tables in a relational database, for example, where one table holds information about the nodes (A, B, C, etc.) and another holds information about the edges between the nodes (A -> B, A -> C, etc.). 您可以在关系数据库中的两个表中表示DAG,例如,其中一个表包含有关节点(A,B,C等)的信息,另一个表包含有关节点之间边缘的信息(A - > B, A - > C等)。

A graph database such as Neo4j might also be a good way to go. 像Neo4j这样的图形数据库也可能是一个很好的方法。

Your mileage will vary when it comes to things like horizontal scaling and concurrent access, depending on the approach you adopt. 根据您采用的方法,在横向扩展和并发访问等方面,您的里程会有所不同。 You may want to keep a denormalized representation around for speeding up some kinds of queries, but such a strategy involves tradeoffs you'll want to understand before going with it. 您可能希望保留一个非规范化表示来加速某些类型的查询,但是这样的策略需要在使用它之前进行权衡。

A Nodes table and a Relationships (link) table seems the most natural to me since that's generally (always?) how you implement many-to-many relationships in a relational database, and a directed graph is basically a many-to-many relationship mapping for nodes (right?). Nodes表和Relationships(链接)表似乎对我来说最自然,因为通常(总是?)如何在关系数据库中实现多对多关系,有向图基本上是多对多关系节点映射(对吗?)。 I suppose you could store it in an XML field, or some binary value, but that really ignores the fact that you have a database. 我想你可以将它存储在XML字段或一些二进制值中,但这实际上忽略了你有一个数据库的事实。

If you didn't have a database, and were writing directly to some type of file, I would probably still use a similar mechanism of uniquely identifying each node with a key and separately defining how the key values are related. 如果你没有数据库,并且直接写入某种类型的文件,我可能仍然会使用类似的机制,用一个键唯一地标识每个节点,并分别定义关键值的关联方式。

This all assumes that your directed graph is many-to-many and not strictly one-to-many. 这一切都假定您的有向图是多对多的,而不是严格的一对多。 If it were strictly one-to-many, without loops, I might use XML if writing to a file (utilizing the fact that child elements in the XML are related to their parent), or foreign keys if writing to a database (which would work well even if there were loops). 如果它是严格一对多的,没有循环,我可能会使用XML,如果写入文件(利用XML中的子元素与其父元素相关的事实),或者如果写入数据库则使用外键(这将是即使有循环也能很好地工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM