简体   繁体   English

在分支中达到顶点的第n个后继

[英]Reach n-th successor of a vertex in a branch

I created a digraph using the jgrapht library. 我使用jgrapht库创建了一个有向图。 I use the successorListOf() method to access a vertex's successor, but I'd like to be able to reach the n-th successor of a given vertex (which are Point objects in my case). 我使用successorListOf()方法访问顶点的后继者,但我希望能够到达给定顶点的第n个后继者(在我的例子中是Point对象)。 My digraph has two branches (named B and C here). 我的有向图有两个分支(这里分别命名为B和C)。 I made a simple and shorter code to make it easier: 我编写了一个简单而简短的代码以使其变得更容易:

public static DirectedGraph<Point, DefaultEdge> directedGraph = new DefaultDirectedGraph<Point, DefaultEdge>(DefaultEdge.class);
public static Point startPoint = new Point(2, 6, "S");
public static Point firstPoint = new Point(2, 7, "A");
public static Point secondPoint = new Point(2, 8, "B");
public static Point thirdPoint = new Point(2, 9, "B");
public static Point fourthPoint = new Point(2, 10, "B");
public static Point fifthPoint = new Point(3, 7, "C");
public static Point sixthPoint = new Point(4, 7, "C");
public static Point seventhPoint = new Point(5, 7, "C");


void setup ()  {
  directedGraph.addVertex(startPoint);
  directedGraph.addVertex(firstPoint);
  directedGraph.addVertex(secondPoint);
  directedGraph.addVertex(thirdPoint);
  directedGraph.addVertex(fourthPoint);
  directedGraph.addVertex(fifthPoint);
  directedGraph.addVertex(sixthPoint);
  directedGraph.addVertex(seventhPoint);
  directedGraph.addEdge(startPoint, firstPoint);
  directedGraph.addEdge(firstPoint, secondPoint);
  directedGraph.addEdge(firstPoint, thirdPoint);
  directedGraph.addEdge(firstPoint, fourthPoint);
}

// --------------------------------------------------------------
public static ArrayList<Point> pointList = new ArrayList<Point>();
public static class Point {

  public int x;
  public int y;
  public String iD;
  public  Point(int x, int y, String iD) 
  {

    this.x = x;
    this.y = y;
    this.iD= iD;
  }
  @Override
    public String toString() {
    return ("[x="+x+" y="+y+" iD="+iD+ "]");
  }

  @Override
    public int hashCode() {
    int hash = 7;
    hash = 71 * hash + this.x;
    hash = 71 * hash + this.y;

    return hash;
  }



  @Override
    public boolean equals(Object other) 
  {
    if (this == other)
      return true;

    if (!(other instanceof Point))
      return false;

    Point otherPoint = (Point) other;
    return otherPoint.x == x && otherPoint.y == y;
  }
}

I'd like to add an edge between the firstPoint and each point of the "B" branch, and instead of having: 我想在firstPoint和“ B”分支的每个点之间添加一条边,而不是:

directedGraph.addEdge(firstPoint, secondPoint);
  directedGraph.addEdge(firstPoint, thirdPoint);
  directedGraph.addEdge(firstPoint, fourthPoint);

I'd like to use: 我想使用:

for (Point successor : Graphs.successorListOf (directedGraph, firstPoint)) {
        if (successor.type.equals("B") {
               directedGraph.addEdge(firstPoint, successor);
        }
}

But here I can only reach the first successor of the branch B. How can I reach the successor of the successor etc, in the case of n-th successor ? 但是在这里,我只能到达分支B的第一个后继者。对于第n个后继者,我如何到达后继者等的后继者? The number of vertices in the B branch may change, that's why I'm looking for a way to do this automatically instead of Point by Point. B分支中的顶点数量可能会发生变化,这就是为什么我正在寻找一种自动执行此操作的方法,而不是逐点进行操作的原因。

How could I do this ? 我该怎么办?

On the drawing,1 would be my startPoint, 2 would be my firstPoint, and then there are two branches which would be my B & C branches 在图形上,1将是我的起点,2将是我的firstPoint,然后有两个分支将是我的B&C分支

在图形上,1将是我的起点,2将是我的firstPoint,然后有两个分支将是我的B&C分支

I wrote the following code, but it's not tested and you might need to modify it to fit your requirements. 我编写了以下代码,但未经测试,您可能需要对其进行修改以满足您的要求。

This code is running a DFS (depth first search) to a predefined depth using the variables and instances you used in your provided examples. 此代码使用您在提供的示例中使用的变量和实例将DFS(深度优先搜索)运行到预定义的深度。

public void getSuccessor(DirectedGraph<Point, DefaultEdge> graph, Point point, String type, int depth) {
    List<Point> visitedPoints = new ArrayList<>();
    _getSuccessor(graph, point, visitedPoints, type, depth);
}

private void _getSuccessor(DirectedGraph<Point, DefaultEdge> graph, Point point, List<Point> visitedPoints, String type, int depth){

    if(depth == 0)
        return;

    // Mark node as visited
    visitedPoints.add(point);

    // Loop on all its successors
    for(Point successor : Graphs.successorListOf (directedGraph, point)){

        // If node not already visited
        if(!visitedPoints.contains(successor) && successor.type.equals(type)) {
            directedGraph.addEdge(firstPoint, successor);
            _getSuccessor(graph, successor, visitedPoints, type, depth-1);
        }
    }
}

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

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