在 Ruby 中使用 RGL 处理有向图
我使用 RGL 在 Ruby 中实现了一个有向图,只是很难弄清楚如何对于给定节点仅查找具有传入连接的节点和具有传出连接的节点。也许我错过了一些简单的事情。
I've implemented a directed graph in Ruby using RGL, just having difficulty figuring out how to, for a given node, find only the nodes with incoming connections and the nodes with outgoing connections. Perhaps I'm missing something simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚遇到这个问题。使用反向方法可能会起作用,尽管它可能不是最优雅的方法:
更长的答案是它似乎尚未实现。
它应该工作的方式是通过将 RGL::Bi Direction 模块包含在有向图类中,这将为您提供所有重要的each_in_neighbor 方法。所以像这样的东西应该有效(但没有):
我还没有深入研究代码来看看这会需要多少工作,但这可能是一个更好的选择,具体取决于您的需求。
编辑:我忘了提及入度节点无法访问源和目标属性。但另一种方法可能是从图中收集所有感兴趣的边,并对它们进行比较,看看其中是否有任何一个将您感兴趣的节点作为目标。
I just ran into this problem. Using the reverse method might work, though it might not be the most elegant approach:
The longer answer is that it doesn't appear to be implemented yet.
The way it's supposed to work is by including the RGL::Bidirectional module with with your directed graph class, this will give you the all important each_in_neighbor method. So something like this should work (but doesn't):
I haven't dug into the code to see how much work this would be, but that might be a better option depending upon your needs.
Edit: I forgot to mention that the source and target attributes are not accessible to an in-degree node. But an alternate approach could be to collect all the edges of interest from the graph and compare them to see if any of them have your node of interest as a target.
RGL::DirectedAdjacencyGraph 仅有效地实现传出连接。如果您还希望有效地访问传入边缘,您应该高效地实现 RGL::Bi DirectionGraph 定义的概念。这应该通过在特定的有向图实现中实现方法 RGL::Bi DirectionGraph#each_in_neighbor 来完成。
假设您将每个顶点的内邻居也存储在一个列表中,就像 DirectedAdjacencyGraph 为外邻居所做的那样。那么该方法可以是这样的:
使用这种方法,您必须在插入或删除边和顶点时管理两个顶点字典。
RGL::DirectedAdjacencyGraph only implements outgoing connections efficiently. If you also want to have efficient access to the incoming edges you should implement the concept defined by RGL::BidirectionalGraph as efficiently. That should be done implementing the method RGL::BidirectionalGraph#each_in_neighbor in your specific directed graph implementation.
Suppose you store the in-neighbors for each vertex also in a list like DirectedAdjacencyGraph does for the out-neighbors. Then the method could like this:
Using this approach you have to manage two vertex dictionaries when inserting or removing edges and vertice.
看起来有向边有:
这有帮助吗?我不太确定我理解你的问题。
It looks like Directed Edges have:
Does that help? I'm not quite sure I understand your question.