[英]Get all subgraphs in JGraphT
How can I get all possible subgraphs of a graph in JGraphT in a List<MyGraph>
or Set<MyGraph>
collection?如何在
List<MyGraph>
或Set<MyGraph>
集合中获取 JGraphT 中图形的所有可能子图? I've read the documentation of JGraphT, but I couldn't find anything to help me with that particular issue.我已经阅读了 JGraphT 的文档,但是我找不到任何可以帮助我解决这个特定问题的东西。
The answer to this question depends on whether the OP wants a vertex-induced subgraph or an edge-induced subgraph.这个问题的答案取决于 OP 是想要顶点诱导子图还是边诱导子图。 To create a vertex or edge induced subgraph in JGraphT, use the AsSubgraph class.
要在 JGraphT 中创建顶点或边诱导子图,请使用AsSubgraph class。 There is no method that will simply generate all possible subgraphs as this is a very uncommon procedure, but it is easy to implement yourself.
没有一种方法可以简单地生成所有可能的子图,因为这是一个非常不常见的过程,但您自己很容易实现。 Do notice that there are
2^n
possible vertex induced subgraphs, where n
is the number of vertices.请注意,有
2^n
可能的顶点诱导子图,其中n
是顶点数。 So the number of subgraphs is huge.所以子图的数量是巨大的。
AsSubgraph
.AsSubgraph
生成一个诱导子图。 Coarsely, the code looks like this:粗略地说,代码如下所示:
//Initiate some graph. The vertex/edge type is irrelevant for this question
Graph<Integer,DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class);
...
//Generate powerset
List<Set<Integer>> powerSet = powerSet(graph.vertexSet());
//Create subgraphs:
for(Set<Integer> subset : powerSet)
Graph<Integer,DefaultEdge> subGraph = new AsSubgraph(graph, subset);
To implement the powerSet function, many examples can be found on SO.要实现 powerSet function,可以在 SO 上找到许多示例。 To compute edge induced subgraphs, repeat the above but instead of graph.vertexSet() use graph.edgeSet();
要计算边诱导子图,请重复上述步骤,但使用 graph.edgeSet() 代替 graph.vertexSet();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.