简体   繁体   English

获取 JGraphT 中的所有子图

[英]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.所以子图的数量是巨大的。

  1. Create a List<Set> containing all possible subsets of vertices.创建一个 List<Set> 包含所有可能的顶点子集。 This is called a powerset (there are many SO posts on how to generate a powerset)这被称为 powerset (有很多关于如何生成 powerset 的帖子)
  2. For each set in your powerset, generate an induced subgraph using AsSubgraph .对于 powerset 中的每个集合,使用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.

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