Java中的邻接列表

Java中的邻接列表,java,adjacency-list,Java,Adjacency List,我有一张邻接表。见上文: e1.p1.x e1.p1.y e1.p2.x e1.p2.y x1 46 y1 280 x2 346 y2 339 x1 331 y1 229 x2 46 y2 280 x1 1 y1 74 x2 207 y2 325 x1 388 y1 29 x2 1 y2 74 x1 237 y1 72

我有一张邻接表。见上文:

e1.p1.x     e1.p1.y     e1.p2.x     e1.p2.y
x1 46       y1 280      x2 346      y2 339
x1 331      y1 229      x2 46       y2 280
x1 1        y1 74       x2 207      y2 325
x1 388      y1 29       x2 1        y2 74
x1 237      y1 72       x2 46       y2 280
x1 346      y1 339      x2 331      y2 229
x1 46       y1 280      x2 331      y2 229
x1 207      y1 325      x2 101      y2 152
x1 132      y1 55       x2 46       y2 280
x1 101      y1 152      x2 1        y2 74
x1 331      y1 229      x2 346      y2 339
x1 346      y1 339      x2 101      y2 152
x1 101      y1 152      x2 132      y2 55
x1 346      y1 339      x2 1        y2 74
x1 237      y1 72       x2 132      y2 55
x1 331      y1 229      x2 207      y2 325
每行有2个点,它们是相邻点。我想列出这样的列表,每个点的所有邻居。但我也得到了假邻接,我在上面的列表中得到了比表中表示的更多的邻接

    Output:
    [207, 325, 0, 1, 74, 0, 101, 152, 0, 331, 229, 0]
    [331, 229, 0, 46, 280, 0, 346, 339, 0, 207, 325, 0]
    [46, 280, 0, 346, 339, 0, 331, 229, 0, 237, 72, 0, 132, 55, 0]
    [346, 339, 0, 46, 280, 0, 331, 229, 0, 101, 152, 0, 1, 74, 0]
    [101, 152, 0, 207, 325, 0, 1, 74, 0, 346, 339, 0, 132, 55, 0]
    [132, 55, 0, 46, 280, 0, 101, 152, 0, 237, 72, 0]
    [237, 72, 0, 46, 280, 0, 132, 55, 0]
    [1, 74, 0, 207, 325, 0, 388, 29, 0, 101, 152, 0, 346, 339, 0]
    [388, 29, 0, 1, 74, 0]
这是Java代码:

for (j = 0; j < size; j++) {
    ArrayList < Integer > aList = adjLists.get(j);
    for (Edge e: edges) {
        if ((points[j][0] == e.p1.x && points[j][1] == e.p1.y)) {
            aList.add(e.p1.x);
            aList.add(e.p1.y);
            aList.add(0);

            for (Edge e1: edges) {
                if (e1.p1.x == e.p1.x && e1.p1.y == e.p1.y && !aList.contains(e1.p2.x) && !aList.contains(e1.p2.y)) {
                    aList.add(e1.p2.x);
                    aList.add(e1.p2.y);
                    aList.add(0);

                }
            }
            break;
        }

        if ((points[j][0] == e.p2.x && points[j][1] == e.p2.y)) {

            aList.add(e.p2.x);
            aList.add(e.p2.y);
            aList.add(0);
            for (Edge e1: edges) {
                if (e1.p2.x == e.p1.x && e1.p2.y == e.p1.y && !aList.contains(e1.p1.x) && !aList.contains(e1.p1.y)) {
                    aList.add(e1.p1.x);
                    aList.add(e1.p1.y);
                    aList.add(0);
                }
            }
            break;
        }
    }
}
(j=0;j{ ArrayListaList=adjLists.get(j); 用于(边e:边){ 如果((点[j][0]==e.p1.x和点[j][1]==e.p1.y)){ 列表.添加(e.p1.x); 列表添加(e.p1.y); 添加(0); 用于(边e1:边){ 如果(e1.p1.x==e.p1.x&&e1.p1.y==e.p1.y&&aList.contains(e1.p2.x)&&aList.contains(e1.p2.y)){ 列表添加(e1.p2.x); 列表添加(e1.p2.y); 添加(0); } } 打破 } if((点[j][0]==e.p2.x和点[j][1]==e.p2.y)){ 列表.增补(e.p2.x); 列表添加(e.p2.y); 添加(0); 用于(边e1:边){ 如果(e1.p2.x==e.p1.x&&e1.p2.y==e.p1.y&&aList.contains(e1.p1.x)&&aList.contains(e1.p1.y)){ 列表添加(e1.p1.x); 列表添加(e1.p1.y); 添加(0); } } 打破 } } }
Size是顶点的数量,应该存储邻接。我认为正确的方法是定义类点,而不是使用原始整数:

 class Point {
   private final int x;
   private final int y;
   Point(int x, int y) {this.x = x; this.y = y;}
   int getX() {return x;}
   int getY() {return y;}
 }
则邻接列表为:

Map<Point, Set<Point>> adjacencies;
映射邻接;
然后使用它,你只要写

for (/* rows */) {
  Point pt1 = new Point(x1, y1); // Extract from the table.
  Point pt2 = new Point(x2, y2); // Extract from the table.
  Set<Point> adjs = adjacencies.find(pt1);
  if (adjs == null) {
    adjs = new HashSet<Point>();
    adjacencies.put(pt1, adjs);
  }
  adjs.put(pt2);
  // Same for pt2
}
(/*行*/)的
{
点pt1=新点(x1,y1);//从表中提取。
点pt2=新点(x2,y2);//从表中提取。
Set adjs=adjances.find(pt1);
if(adjs==null){
adjs=新的HashSet();
邻接。放置(pt1,邻接);
}
形容词put(pt2);
//pt2也一样
}
通过这种方式,可以获得点的邻接,如下所示:

Set<Point> adjs = asjacencies.get(point);
List<List<Integer>> toLists(Map<Set<Point>> adjacencies) {
  List<List<Integer>> result = new ArrayList<List<Integer>>();
  for (Point pt : adjacencies.getKeys()) {
    List<Integer> adjs = new ArrayList<Integer>();
    adjs.add(pt.getX());
    adjs.add(pt.getY());
    adjs.add(0);
    for (Point adjPt : adjacencies.get(pt)) {
      adjs.add(adjPt.getX());
      adjs.add(adjPt.getY());
      adjs.add(0);
    }
    result.add(adjs);
  }
  return result; 
}
Set adjs=asjacenties.get(点);
比搜索一个数组并将每两个数组解析为一个点容易得多

如果其他模块需要数组形式的数据,可以执行以下操作:

Set<Point> adjs = asjacencies.get(point);
List<List<Integer>> toLists(Map<Set<Point>> adjacencies) {
  List<List<Integer>> result = new ArrayList<List<Integer>>();
  for (Point pt : adjacencies.getKeys()) {
    List<Integer> adjs = new ArrayList<Integer>();
    adjs.add(pt.getX());
    adjs.add(pt.getY());
    adjs.add(0);
    for (Point adjPt : adjacencies.get(pt)) {
      adjs.add(adjPt.getX());
      adjs.add(adjPt.getY());
      adjs.add(0);
    }
    result.add(adjs);
  }
  return result; 
}
列表列表列表(地图邻接){
列表结果=新建ArrayList();
对于(点pt:adjacencies.getKeys()){
List adjs=new ArrayList();
adj.add(pt.getX());
添加(pt.getY());
添加(0);
for(点邻接:邻接。获取(点)){
add(adjPt.getX());
add(adjPt.getY());
添加(0);
}
结果。添加(调整);
}
返回结果;
}

你有问题吗?是的,当然有!使用上面的代码是什么导致错误的邻接?我理解你的问题,你能提供一个完整的代码工作示例,以便于调试吗?(即“边”、“点”和“调整列表”的代码。)如果没有getter,如何获得x和y值?可能吧,但我代码的另一部分使用它作为输入。所以修复这段代码会很好,并减少开发的时间消耗。这就是你的意思吗?好的设计比好的调试节省更多的时间。我不需要getter,值在数组中,但不仅仅是正确的值。