arraylist的Arraylists作为关系的代表

5 java arraylist graph-algorithm

我有几个值,如下所示:(连续的元素是关系.)

       Vertex relationships(edges)
    Source vertex   Destination vertex

    x1 26   y1 287   x2 154   y2 303
    x1 22   y1 114   x2 115   y2 185
    x1 26   y1 287   x2 375   y2 338
    x1 26   y1 287   x2 260   y2 393
    x1 115  y1 185   x2 121   y2 7
    x1 200  y1 101   x2 392   y2 238
    x1 99   y1 394   x2 375   y2 338
    x1 99   y1 394   x2 121   y2 7
    x1 274  y1 28    x2 22    y2 114
    x1 296  y1 185   x2 200   y2 101
    x1 115  y1 185   x2 154   y2 303
Run Code Online (Sandbox Code Playgroud)

我应该找到关系中的所有值并将它们放入列表中,如下所示:[26,287 154,303 375,338 260,393] 我尝试使用此代码:

    for (int i=0; i<vertexnum; i++) {
        adjLists.add(new ArrayList<Integer>());
    }

    for (int j=0; j<vertexnum; j++) {
        for (Point p : nodes) {
            for (Edge e : edges) {
                adjLists.get(j).add(e.p1.x);
                adjLists.get(j).add(e.p1.y);
                adjLists.get(j).add(0);

                adjLists.get(j).add(e.p2.x);
                adjLists.get(j).add(e.p2.y);
                adjLists.get(j).add(0);
                for (Point p1 : nodes) {
                    for (Edge e1 : edges) {
                        if (e1.p1.x == e.p1.x && e1.p1.y == e.p1.y && !adjLists.get(j).contains(e1.p2.x) && !adjLists.get(j).contains(e1.p2.y)) {
                            adjLists.get(j).add(e1.p2.x);
                            adjLists.get(j).add(e1.p2.y);
                            adjLists.get(j).add(0);
                        } else if(e1.p2.x == e.p1.x && e1.p2.y == e.p1.y && !adjLists.contains(e1.p1.x) && !adjLists.contains(e1.p1.y)){
                            adjLists.get(j).add(e1.p1.x);
                            adjLists.get(j).add(e1.p1.y);
                            adjLists.get(j).add(0);
                        }
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

它只创建一个ArrayList,它提供一行中的所有元素而不是单独的.我试过调试,但我看不出是什么导致这种情况.

我想要的例子: 在此输入图像描述

And*_*eas 1

我将分三个步骤进行:定义数据结构、定义问题、提供解决方案。

定义数据结构

  • 顶点:在您的示例中,顶点似乎是一对唯一的整数。应该很合适
  • 关系:这似乎是由两个顶点定义的边。您应该为此编写一个简单的 pojo,但为了简洁起见,我们将使用apache commons 中的Pair。让我们声明关系是从右到左的。因此Pair<Point, Point> relationship = new ImmutablePair<Point, Point>(new Point(26, 287), new Point(154, 303));相当于示例数据中的第一行。

定义问题

您需要一种方法,该方法接收关系列表并输出一系列列表,显示可以从任何给定顶点到达的位置。我将更进一步并返回地图,其中从点作为键,并以可能的点集作为值。IE。Map<Point,Set<Point>>

解决方案

至此背景已经明确,找到解决方案就很容易了

public static Map<Point, Set<Point>> createTraversalMap(List<Pair<Point, Point>> relationshipList) {
    Map<Point, Set<Point>> traversalMap = new HashMap<Point, Set<Point>>();
    for (Pair<Point, Point> relationship : relationshipList) {
        Point fromVertex = relationship.getLeft(), toVertex = relationship.getRight();
        Set<Point> toSet = traversalMap.get(fromVertex);// set of Vertexes we've found so far for the current "from" Vertex
        if (toSet == null) {// bootstrap the set
            toSet = new HashSet<Point>();
            traversalMap.put(fromVertex, toSet);
        }
        toSet.add(toVertex);
        // traversalMap.put(fromVertex, toSet); //not needed, but good to keep in mind
    }
    return traversalMap;
}
Run Code Online (Sandbox Code Playgroud)

注意,我没有以任何方式测试过这个