在 JgraphT 中显示权重
我已经实现了这个图表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设类 MyWeightedEdge 已经包含一个方法,例如
如果情况确实如此,那么您需要做的是:
从 ListenableDirectedWeightedGraph 派生您自己的子类(例如 ListenableDirectedWeightedGraph >)。 我将添加两个构造函数版本,委托给“super”以确保与原始类的兼容性。
按照您的问题创建图表,但使用新类
重写方法 setEdgeWeight,如下所示:
最后但并非最不重要的一点是,重写类 toString 方法 >MyWeightedEdge 返回您希望边缘具有的标签(大概包括现在可用的权重)。
我希望这有帮助。
I assume that the class MyWeightedEdge already contains a method such as
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
Override the method setEdgeWeight as follows:
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.