[英]Reach n-th successor of a vertex in a branch
我使用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;
}
}
我想在firstPoint和“ 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將是我的firstPoint,然后有兩個分支將是我的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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.