在 JgraphT 中显示权重

发布于 2024-07-06 07:25:38 字数 460 浏览 10 评论 0原文

我已经实现了这个图表:

ListenableDirectedWeightedGraph<String, MyWeightedEdge> g = 
    new ListenableDirectedWeightedGraph<String, MyWeightedEdge>(MyWeightedEdge.class); 

为了显示类名所说的内容; 一个简单的可听的有向加权图。 我想更改边缘的标签,而不是格式,

return "(" + source + " : " + target + ")"; 

我希望它显示边缘的权重。 我意识到节点上的所有操作(例如 getEdgesWeight() 方法)都是从图而不是边委托的。 如何显示边缘的重量? 我是否必须以某种方式将图表传递到边缘?

任何帮助表示赞赏。

I have implemented this Graph:

ListenableDirectedWeightedGraph<String, MyWeightedEdge> g = 
    new ListenableDirectedWeightedGraph<String, MyWeightedEdge>(MyWeightedEdge.class); 

In order to show what the class name says; a simple listenable directed weighted graph. I want to change the label of the edges and instead of the format

return "(" + source + " : " + target + ")"; 

I want it to show the weight of the edge. I realise that all actions on the nodes, e.g. the getEdgesWeight() method, are delegated from the graph and not the edge. How can I show the weight of the edge? Do I have to pass in the Graph to the edge somehow?

Any help is appreciated.

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

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

发布评论

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

评论(1

樱娆 2024-07-13 07:25:38

我假设类 MyWeightedEdge 已经包含一个方法,例如

public void setWeight(double weight)

如果情况确实如此,那么您需要做的是:

ListenableDirectedWeightedGraph 派生您自己的子类(例如 ListenableDirectedWeightedGraph >)。 我将添加两个构造函数版本,委托给“super”以确保与原始类的兼容性。

按照您的问题创建图表,但使用新类

ListenableDirectedWeightedGraph g = 
    new CustomListenableDirectedWeightedGraph(
        MyWeightedEdge.class);

重写方法 setEdgeWeight,如下所示:

public void setEdgeWeight(E e, double weight) {
    super.setEdgeWeight(e, weight);
    ((MyWeightedEdge)e).setWeight(weight);
}

最后但并非最不重要的一点是,重写类 toString 方法 >MyWeightedEdge 返回您希望边缘具有的标签(大概包括现在可用的权重)。

我希望这有帮助。

I assume that the class MyWeightedEdge already contains a method such as

public void setWeight(double weight)

If this is indeed the case, then what you need to do is:

Derive your own subclass from ListenableDirectedWeightedGraph (e.g., ListenableDirectedWeightedGraph). I would add both constructor versions, delegating to "super" to ensure compatibility with the original class.

Create the graph as in your question, but using the new class

ListenableDirectedWeightedGraph g = 
    new CustomListenableDirectedWeightedGraph(
        MyWeightedEdge.class);

Override the method setEdgeWeight as follows:

public void setEdgeWeight(E e, double weight) {
    super.setEdgeWeight(e, weight);
    ((MyWeightedEdge)e).setWeight(weight);
}

And, last but not least, override the toString method of the class MyWeightedEdge to return the label you want the edge to have (presumably including the weight, which is now available to it).

I hope this helps.

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