- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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分支
最佳答案
我编写了以下代码,但尚未经过测试,您可能需要修改它以满足您的要求。
此代码使用您在提供的示例中使用的变量和实例运行 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/
我想使用图中所示的迷宫,使用迭代深度优先搜索找到从起始节点到目标的路径。它是一个仅包含一对数字的文本文件,例如成对连接,又称边/弧。像这样: 11 3 2 3 0 3 1 4 5 4 5 7 6 7
问题:您有一个无向图 G = (V, E)(V = 顶点,E = 边),您必须访问每个顶点并在两个方向上通过每个边。 我所知道的图算法只有 DFS、BFS 和一些 MST(Kruskal 等)不幸的是
枚举任意图中两个顶点之间的所有简单路径通常需要指数时间,因为顶点之间可能存在指数数量的简单路径。但是,如果我们只对位于两个末端顶点之间的至少一条简单路径上的顶点怎么办? 即:给定一个无向图和两个不同的
我正在开发一个简单的 opengl 游戏以了解更多相关信息。但是由于某种原因,当我尝试随时间旋转我的立方体时,它会被拉伸(stretch)。你可以在照片中看到它: 我认为这与我的模型矩阵有关,但我不确
我已经在谷歌上搜索了很长一段时间,但我找不到任何东西。如何使用 Graphviz 绘制没有连接顶点的图形? 最佳答案 像这样: digraph g { SingleNode; } 简单地不定义
我目前正在使用 R 中的“igraph”包进行一些社交网络分析,我想知道是否有一种方法可以个性化社交网络中节点的放置。 例如,使用以下玩具代码: library(igraph) edg
我在 Box2D 中有一个多边形形状。形状是一个三角形,我希望有 3 个顶点。事实上,我创建的所有形状都会输出 8 个顶点。为什么是这样?如果我输出顶点数,那总是正确的数量。我不想渲染不必要的线条,但
来自user manual CGAL Surface_mesh 类: the data structure uses integer indices as descriptors for vertic
我正在尝试找到引用 ARFaceGeometry 网格索引的方法为了使用 ARKit 将图形放置在面部的特定部位。 我见过很多例子,其中功能与一些索引号,但我找不到对此列表的任何引用。它似乎有超过12
Apache TomCat(版本未知) 业务对象 4.1 顶点 4.4.3 在一台服务器上,我们拥有 TomCat 和 Business Objects。 APEX 也使用 TomCat。 在对我们的
我正在使用 MX Graph 进行一些工作,以帮助识别网站中的关键内容路径。我将其设置为每个顶点代表网站上的一个页面,每条边代表一组从页面 A 访问页面 B 的访问者。 一切都运行良好,除了边太多,我
我正在尝试使用三角形 strip 绘制一个平面。我了解如何手动执行此操作,但我真的很难使用 for 循环来执行此操作。到目前为止,下面的代码绘制了两个三角形。 //vertices for trian
如果我想通过 id 顶点获取名称,我可以使用这个函数:VAS(g, "name",id)但是如果我想要相反的方式,通过名称获取 id,我该怎么做呢? 最佳答案 igraph 本身不提供按名称查找顶点的
我有一个三角形,其任意顶点位于 3D 空间中。 我知道通过以下操作很容易找到这种三角形的质心: float centroid[3] = { 0, 0, 0 }; for (int i = 0; i =
我有一个点数组。每个点都有位置(x, y, z) 和法 vector (xn, yn, zn) ,一共6个 double 。考虑到浮点容差,我需要在此数组中找到唯一元素并删除重复条目。 实现它的简单有
我有一个相互连接的边列表 (E),如何找到从一个顶点连接到另一个顶点的最短路径? 我正在考虑使用 lowest common ancestors ,但边缘没有明确定义的根,所以我认为该解决方案不起作用
我现在正在使用计算着色器开发粒子系统。我将所有粒子都放在着色器存储缓冲区中。一个粒子包含两个顶点,当前位置和先前位置。 struct Particle{ glm::vec4 _currPo
我将我的顶点剪裁在边缘上,如这张专辑所示: http://imgur.com/a/VkCrJ 当我的地形大小为 400 x 400 时,我得到裁剪,但在 40x40 或更小时,我没有得到任何裁剪。这是
总是在顶点着色器中而不是在片段着色器中更好地进行硬计算吗?即使是具有超过 100.000 个多边形的高网格模型(假设有一堆独特的顶点)? 最佳答案 不,它并不总是更好。 选择合适的计算位置的最佳方法是
我想编辑一个立方体上的 1 个顶点,但我不知道该怎么做。我试过到处寻找此功能,但找不到解决方案。 这是我想要实现的目标的图像: 最佳答案 http://answers.unity3d.com/ques
我是一名优秀的程序员,十分优秀!