gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:01:36 25 4
gpt4 key购买 nike

我使用 jgrapht 库创建了一个有向图。我使用 successorListOf() 方法来访问顶点的后继者,但我希望能够访问给定顶点的第 n 个后继者(在我的例子中是 Point 对象)。我的有向图有两个分支(此处名为 B 和 C)。我编写了一个简单且较短的代码以使其更容易:

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

我想在第一个点和“B”分支的每个点之间添加一条边,而不是:

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

我想使用:

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

但是在这里我只能到达分支B的第一个后继者。在第n个后继者的情况下,我如何才能到达后继者的后继者等? B 分支中的顶点数量可能会发生变化,这就是为什么我正在寻找一种自动执行此操作而不是逐点执行此操作的方法。

我怎样才能做到这一点?

在绘图上,1是我的起点,2是我的第一个点,然后有两个分支,这是我的B和C分支

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

最佳答案

我编写了以下代码,但尚未经过测试,您可能需要修改它以满足您的要求。

此代码使用您在提供的示例中使用的变量和实例运行 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);
}
}
}

关于java - 到达分支中顶点的第 n 个后继,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31845239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com