How to copy a graph in JUNG 2.0 framework?

你离开我真会死。 提交于 2019-12-10 17:44:00

问题


in JUNG 1.7.6 there was the function copy() for this purpose (myGraph.copy()), but in JUNG 2.0 this function does not exist anymore. I could not find any other possibility to create a copy of a graph object. I would be very glad if someone could help me. A workaround would be nice, too.

Thanks a lot!


回答1:


Code below with generics, so you should replace V and E with String for your Graph<String, String>.

    Graph<V, E> src;
    Graph<V, E> dest;

    for (V v : src.getVertices())
        dest.addVertex(v);

    for (E e : src.getEdges())
        dest.addEdge(e, src.getIncidentVertices(e));



回答2:


You can copy the graph manually by iterating over all vertices and all edges and adding them to a new graph. See getVertices() in the API




回答3:


You could do a simple copy of vertices & edges, that would create a new Graph, but the objects inside will be passed by reference so you could use this cloning library https://code.google.com/p/cloning/

and do a deep copy:

Cloner cloner = new Cloner();
Graph<V, E> clonedGraph = cloner.deepClone(graph);


来源:https://stackoverflow.com/questions/10470213/how-to-copy-a-graph-in-jung-2-0-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!