Java 到达分支中顶点的第n个后续顶点
我使用jgrapht库创建了一个有向图。我使用Java 到达分支中顶点的第n个后续顶点,java,jgrapht,digraphs,Java,Jgrapht,Digraphs,我使用jgrapht库创建了一个有向图。我使用successorListOf()方法访问顶点的后续对象,但我希望能够访问给定顶点的第n个后续对象(在我的例子中,这些是点对象)。我的有向图有两个分支(这里称为B和C)。我编写了一个简单而简短的代码,使之更容易: public static DirectedGraph<Point, DefaultEdge> directedGraph = new DefaultDirectedGraph<Point, DefaultEdge>
successorListOf()
方法访问顶点的后续对象,但我希望能够访问给定顶点的第n个后续对象(在我的例子中,这些是点对象)。我的有向图有两个分支(这里称为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;
}
}
我想使用:
for (Point successor : Graphs.successorListOf (directedGraph, firstPoint)) {
if (successor.type.equals("B") {
directedGraph.addEdge(firstPoint, successor);
}
}
但在这里,我只能找到分支B的第一个继任者。在第n个继任者的情况下,我如何才能找到继任者的继任者等?B分支中的顶点数量可能会改变,这就是为什么我在寻找一种方法来自动实现这一点,而不是逐点进行
我怎么能这样做
在图纸上,1是我的起点,2是我的第一点,然后有两个分支,这两个分支将是我的B&C分支
我编写了以下代码,但它没有经过测试,您可能需要修改它以满足您的需求 这段代码使用您在提供的示例中使用的变量和实例将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);
}
}
}
public void getsuccession(DirectedGraph图形、点、字符串类型、整数深度){
列出访问点=新建ArrayList();
_GetSuccession(图形、点、访问点、类型、深度);
}
私有void _getsuccession(DirectedGraph图形、点、列表访问点、字符串类型、整数深度){
如果(深度==0)
返回;
//将节点标记为已访问
访问点。添加(点);
//在所有的继承者身上循环
for(点后继:Graphs.successorListOf(directedGraph,点)){
//如果节点尚未访问
如果(!visitedPoints.contains(succession)&&succession.type.equals(type)){
directedGraph.addEdge(第一点,后继点);
_GetSuccession(图形、继任者、访问点、类型、深度-1);
}
}
}
A顶点是否连接到B顶点?这就是“后继”的定义吗?你能提供你的图形的图像吗?@TimBiegeleisen当我使用“directedGraph.addEdge(firstPoint,secondPoint);”添加边时secondPoint将是firstPoint的继任者。始终是括号末尾的点,它是括号开头点的后续点。所以唯一的点A是B和C的起始交点branches@CMPS我加了一张照片
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);
}
}
}