如何添加链接或将现有链接属性更新为JUNG图(how to add links or update existing links property to a JUNG graph)

编程入门 行业动态 更新时间:2024-10-26 00:26:39
如何添加链接或将现有链接属性更新为JUNG图(how to add links or update existing links property to a JUNG graph)

我有一个来自mysql数据库的结果集,我正在尝试使用这些值构建一个JUNG图。 我已经将我的空图实例化为:

Graph<Node, Edge> g = new SparseMultigraph<>();

然后我添加了节点(586)。 我现在正在添加链接,现在事情变得更复杂了。 我的自定义Edge的结构非常简单,它只有一个“时间”属性,如:

Multiset<Timestamp> time;

链接的结果集包含以下形式的3273684个条目:

id time sender receiver 12 2014-03-20 09:26:04.000 2 99

现在,我想要做的是从id为2的节点创建一个链接,如果链接不存在则创建id为99的节点,或者只是将时间戳添加到现有链接。 我所做的是:

while (resultSet.next()) { // retrieve sender Node sender = findNode(resultSet.getInt("sender"), g); // retrieve receiver Node receiver = findNode(resultSet.getInt("receiver"), g); // if they are already linked if(g.isPredecessor(sender, receiver)){ // just add the new timestamp to the existing link Collection<Edge> outEdges = g.getOutEdges(sender); // find the right edge for(Edge e:outEdges){ // if this edge is connected to receiver if(g.getDest(e).equals(receiver)){ // add the new timestamp to this edge e.setTime(resultSet.getTimestamp("time")); } } } else { // else a new link is added Information e = new Information(); e.setId(resultSet.getInt("id")); e.setTime(resultSet.getTimestamp("time")); g.addEdge(e, sender, receiver, EdgeType.DIRECTED); } }

我的问题是这真的很慢,我不明白它是否正常,因为结果集非常大,或者我错过了更清晰/更快的方式来实现我需要的东西。

为了清楚起见,我的findNode()方法是这样的:

private static Node findNode(int aInt, Graph<Node, Edge>g) { for(Node n:g.getVertices()){ if(n.getId()== aInt){ return n; } } return null; }

I have a result set coming from a mysql database and I am trying to build a JUNG graph using these values. I already have instantiated my empty graph as:

Graph<Node, Edge> g = new SparseMultigraph<>();

then I have added nodes (586). I am now in the process of adding links and now things get more complicated. The structure of my custom Edge is very simple, it just has a "time" property like:

Multiset<Timestamp> time;

The result set for links contains 3273684 entries in the form:

id time sender receiver 12 2014-03-20 09:26:04.000 2 99

Now, what I want to do is to create a link from node with id 2 and node with id 99 if the link does not exist, or to just add the timestamp to the already existing link. What I do is:

while (resultSet.next()) { // retrieve sender Node sender = findNode(resultSet.getInt("sender"), g); // retrieve receiver Node receiver = findNode(resultSet.getInt("receiver"), g); // if they are already linked if(g.isPredecessor(sender, receiver)){ // just add the new timestamp to the existing link Collection<Edge> outEdges = g.getOutEdges(sender); // find the right edge for(Edge e:outEdges){ // if this edge is connected to receiver if(g.getDest(e).equals(receiver)){ // add the new timestamp to this edge e.setTime(resultSet.getTimestamp("time")); } } } else { // else a new link is added Information e = new Information(); e.setId(resultSet.getInt("id")); e.setTime(resultSet.getTimestamp("time")); g.addEdge(e, sender, receiver, EdgeType.DIRECTED); } }

My problem is that this is really slow, and I do not understand if it is normal, since the result set is quite big, or if I am missing a clearer/faster way to implement what I need.

For the sake of clarity, my findNode() method is like this:

private static Node findNode(int aInt, Graph<Node, Edge>g) { for(Node n:g.getVertices()){ if(n.getId()== aInt){ return n; } } return null; }

最满意答案

这有点慢,原因有两个:

(1)您没有有效的方法来查找给定ID的节点。 对于此大小的图形,我建议在填充图形时构建Map,并使用该映射来实现findNode()。

(2)一旦你拥有了两个节点,并且想要获得连接它们的边缘(如果有的话),只需使用Graph.findEdge()。

(1)是目前代码速度较慢的最大原因。 (2)不会帮助那么多,但它也会使你的代码更容易阅读和更优雅。

This is slow for two reasons:

(1) You don't have an efficient way to look up a node given an ID. For a graph of this size I'd recommend building a Map as you populate the graph, and using that map to implement findNode().

(2) Once you have your two nodes and you want to get the edge that connects them (if any), just use Graph.findEdge().

(1) is by far the biggest reason that your code is slow. (2) will not help as much, but it will also make your code easier to read and more elegant.

更多推荐

本文发布于:2023-08-02 01:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1367966.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   或将   属性   JUNG   add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!