简体   繁体   English

快速确定JUNG图中的两个节点是否通过一条以上路径连接的快速方法?

[英]Fast way of determining whether two nodes in a JUNG graph are connected by more than one path?

Given two nodes A and B from a directed JUNG graph, I want to determine whether there is more than one path from A to B (not necessarely a shortest path). 给定有向JUNG图中的两个节点AB ,我想确定从AB是否有一条以上的路径(不一定是最短的路径)。

I can think of two approaches only, both very time-consuming. 我只能想到两种方法,两者都很费时。

  1. Retrieve all paths connecting the two nodes (question Finding all paths in JUNG? ) and check if there is more than one. 检索连接两个节点的所有路径 (问题是否在JUNG中找到所有路径? ),然后检查是否有多个路径

  2. Retrieve the shortest path by using the class DijkstraShortestPath , then break this path and search for the shortest path again. 通过使用类DijkstraShortestPath检索最短路径,然后断开该路径并再次搜索最短路径。 If there is still one, it means there were multiple paths. 如果还有一条,则意味着存在多条路径。 Note that this also requires to clone the graph, since I do not want to alter the original graph. 请注意,这也需要克隆图,因为我不想更改原始图。

How can I do this smarter (ie faster )? 我如何才能更智能(即更快 )?

I found a solution myself. 我自己找到了解决方案。

My problem has the additional constraint that I only want to check whether there is more than one path only for two nodes that are directly connected with and edge . 我的问题还有一个额外的约束,即我只想检查是否只有两个直接与and edge连接的节点有一条以上的路径。 This means that by simply computing the shortest path you will always get this single edge as path. 这意味着通过简单地计算最短路径,您将始终获得这条单边作为路径。

So, my question can be reformulated as: 因此,我的问题可以改写为:

Is there another path connecting the two nodes of an edge, aside from the edge itself? 除了边缘本身之外,是否还有另一条路径连接边缘的两个节点?

The solution is to use a weighted shortest path. 解决方案是使用加权的最短路径。 If we assign a very high weight to our edge of interest, and weight 1 to all the others, then if the minimal distance is lower than our high weight, the answer is YES , otherwise NO . 如果我们给感兴趣的边缘分配了非常高的权重,而给所有其他权重分配了权重1 ,那么如果最小距离小于我们的高权重,则答案为 ,否则为

Here is the code: 这是代码:

public static boolean areThereMultiplePaths(final Edge edge, DirectedGraph<Entity, Edge> graph) {
        Transformer<Edge, Integer> transformer = new Transformer<Edge, Integer>() {
            public Integer transform(Edge otherEdge) {
                if (otherEdge.equals(edge))
                    return Integer.MAX_VALUE;
                else
                    return 1;
            }
        };

        DijkstraShortestPath<Entity, Edge> algorithm = new DijkstraShortestPath<Entity, Edge>(graph, transformer);
        Double distance = (Double) algorithm.getDistance(edge.getStartNode(), edge.getEndNode());

        return distance < Integer.MAX_VALUE; 
    }

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

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