how do i map a jgrapht graphfooe to another graphbare?

Its actually rather straight forward after having a look at clone() in AbstractBaseGraph. I have put the complete code with a small demo in a gist.

public static <V, Vv, E> Graph<Vv, E> convert(Graph<V, E> source, Function<V, Vv> vertexMapper) {
    Graph<Vv, E> result = GraphTypeBuilder.<Vv, E>forGraphType(source.getType()).buildGraph();

    source.vertexSet().forEach(v -> result.addVertex(vertexMapper.apply(v)));
    for (E e : source.edgeSet()) {
        Vv s = vertexMapper.apply(source.getEdgeSource(e));
        Vv t = vertexMapper.apply(source.getEdgeTarget(e));
        result.addVertex(s);
        result.addVertex(t);
        result.addEdge(s, t, e);
    }
    return result;
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top