简体   繁体   English

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!谢谢!

Different graph formats support edge weights in different ways.不同的图形格式以不同的方式支持边权重。 Personally, I find the DIMACS graph format the easiest format to export a weighted graph.就个人而言,我发现DIMACS图形格式是导出加权图形最简单的格式。 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:这是使用 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:上述代码的output:

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, ie graph.getType().isWeighted() must return true.显然,要导入weighted graph ,您要导入的图必须是加权类型,即graph.getType().isWeighted()必须返回 true。

You could also use the DOT file format as you were originally doing.您也可以像原来一样使用 DOT 文件格式。 Using this format, you would have to export the edge weight as an edge attribute .使用这种格式,您必须将边缘权重导出为边缘属性 See their DOT file format documentation for details.有关详细信息,请参阅他们的DOT 文件格式文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM