Arraylists arraylist as a representation of relationships

I have several meanings, for example: (Elements in a row are in a relationship.)

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 

I have to find all the values ​​that are in the relationship and put them in a list, for example: [26,287 154,303 375,338 260,393] I tried using this code:

  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); } } } } } } 

It creates only one ArrayList, it gives all the elements in a string, and not separately. I tried debugging, but I don't see what causes this.

An example of what I want: enter image description here

+5
source share
1 answer

I will do this in three steps: identify the data structures, identify the problem, provide a solution.

Define data structures

  • Vertex: in your example, the vertex seems to be a unique pair of integers. Point should be very well suited
  • Relations: This seems to be an edge defined by two vertices. You should write a simple pojo for this, but for brevity we will use the Pair from apache commons. Let's announce that the relationship goes from right to left. Thus, Pair<Point, Point> relationship = new ImmutablePair<Point, Point>(new Point(26, 287), new Point(154, 303)); equivalent to the first line in your example data.

Identify the problem

You need a method that takes a list of relationships and displays a list of lists in which you can find access to any given vertex. I will take it one more and return a map with points in the form of keys and sets of possible points as values. i.e. Map<Point,Set<Point>>

Decision

At this point, the background is clearly defined and finding a solution is easy

 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; } 

Notice i have not tested this in any way

+1
source

Source: https://habr.com/ru/post/1216382/


All Articles