jgrapht

JGraphT exporting a weighted graph And importing it


I'm looking for a method to export my graph with the weighted edges.

I have a simple directed graph with weighted edges.

SimpleDirectedWeightedGraph<Integer, DefaultWeightedEdge> exGraph =
                new SimpleDirectedWeightedGraph<>(vSupplier, SupplierUtil.createDefaultWeightedEdgeSupplier());

This is my graph. I generate some vertices to it and edges.

I tried dot exporter:

    DOTExporter<Integer, DefaultWeightedEdge> dotExporter = new DOTExporter<>();
    Writer writer = new StringWriter();
    dotExporter.exportGraph(exGraph,writer);
    FileWriter fw = new FileWriter("ex2.dot");
    fw.write(writer.toString());
    fw.close();

But with that i cannot export the weights.

Can somebody show me how to do the export first and after that the import?

Thanks!


Solution

  • Different graph formats support edge weights in different ways. Personally, I find the DIMACS graph format the easiest format to export a weighted graph. This graph format simply writes a graph to a file as:

    <edge_source> <edge_target> <edge_weight>
    

    Here's a complete example to export/import a weighted graph using DIMACS:

    Graph<Integer, DefaultWeightedEdge> graph=new SimpleWeightedGraph<>(DefaultWeightedEdge.class);
    Graphs.addAllVertices(graph,Arrays.asList(1,2,3,4));
    Graphs.addEdge(graph,1,2, 10);
    Graphs.addEdge(graph,2,3, 11);
    Graphs.addEdge(graph,3,4, 12);
    Graphs.addEdge(graph,4,1, 13);
    
    //Example of exporting a weighted graph in DIMACS format
    DIMACSExporter<Integer, DefaultWeightedEdge> dimacsExporter=new DIMACSExporter<>();
    //Enable exporting of weights in the DIMACS exporter
    dimacsExporter.setParameter(DIMACSExporter.Parameter.EXPORT_EDGE_WEIGHTS, true);
    //Export the graph
    dimacsExporter.exportGraph(graph, new File("DIMACSgraph.txt"));
    
    //Now import the graph
    Graph<Integer, DefaultWeightedEdge> importGraph = new SimpleWeightedGraph<>(SupplierUtil.createIntegerSupplier(), SupplierUtil.createDefaultWeightedEdgeSupplier());
    DIMACSImporter<Integer, DefaultWeightedEdge> dimacsImporter=new DIMACSImporter<>();
    dimacsImporter.importGraph(importGraph, new File("DIMACSgraph.txt"));
    System.out.println("Imported DIMACS graph: "+importGraph);
    System.out.println("Edge weights: ");
    for(DefaultWeightedEdge edge : importGraph.edgeSet())
        System.out.println("Edge: "+edge+" weight: "+importGraph.getEdgeWeight(edge));
    

    The file created during the export contains the following:

    c
    c SOURCE: Generated using the JGraphT library
    c
    p edge 4 4
    e 1 2 10.0
    e 2 3 11.0
    e 3 4 12.0
    e 4 1 13.0
    

    The output of the above code:

    Imported DIMACS graph: ([0, 1, 2, 3], [{0,1}, {1,2}, {2,3}, {3,0}])
    Edge weights: 
    Edge: (0 : 1) weight: 10.0
    Edge: (1 : 2) weight: 11.0
    Edge: (2 : 3) weight: 12.0
    Edge: (3 : 0) weight: 13.0
    

    Obviously, to import a weighted graph, the graph to which you are importing must be of a weighted type, i.e. graph.getType().isWeighted() must return true.

    You could also use the DOT file format as you were originally doing. Using this format, you would have to export the edge weight as an edge attribute. See their DOT file format documentation for details.