使用 JGraphT 更改顶点的内容

发布于 2024-12-25 21:52:19 字数 49 浏览 1 评论 0原文

有没有一种方法可以更改顶点的内容,同时保留其所有边。我找不到,但似乎会提供一些东西。

Is there a method to change the contents of a vertex while keeping all its edges. I couldn't find one but it seems like something that would be provided.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2025-01-01 21:52:19

JGraphT 没有替换顶点的方法。您可以用它来做您想做的事:

public static <V, E> void replaceVertex(Graph<V, E> graph, V vertex, V replace) {
    graph.addVertex(replace);
    for (E edge : graph.outgoingEdgesOf(vertex)) graph.addEdge(replace, graph.getEdgeTarget(edge), edge);
    for (E edge : graph.incomingEdgesOf(vertex)) graph.addEdge(graph.getEdgeSource(edge), replace, edge);
    graph.removeVertex(vertex);
}

JGraphT has no method for replacing a vertex. You can use this to do what you want:

public static <V, E> void replaceVertex(Graph<V, E> graph, V vertex, V replace) {
    graph.addVertex(replace);
    for (E edge : graph.outgoingEdgesOf(vertex)) graph.addEdge(replace, graph.getEdgeTarget(edge), edge);
    for (E edge : graph.incomingEdgesOf(vertex)) graph.addEdge(graph.getEdgeSource(edge), replace, edge);
    graph.removeVertex(vertex);
}
黯然#的苍凉 2025-01-01 21:52:19

改变顶点的内容是什么意思?顶点是用泛型创建的,这意味着您可以在其中放置任何对象,这意味着您可以提供一种方法来替换该对象的内容(如果该对象不是不可变的)。或者您想在那里放置一个全新的对象(全新的顶点)并保存旧对象的所有关系?然后您可能必须找到具有此顶点的边(java.util.SetedgesOf(V vertex)),删除它们(boolean removeEdge(E e) ),并替换为包含新顶点的新顶点 (E addEdge(V sourceVertex, V targetVertex))。

What do you mean by changing contents of a vertex? Vertices are made with generics, which means you can put just any object there, which means it's up to you to provide a method to replace the contents of this object, if it is not immutable. Or do you want to put completely a new object there (completely new vertex) and save all relations of the old one? Then you would probably have to find edges with this vertex (java.util.Set<E> edgesOf(V vertex)), remove them (boolean removeEdge(E e)), and replace with new ones, containing the new vertex (E addEdge(V sourceVertex, V targetVertex)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文