Class SubgraphUtil
Utility class for creating subgraphs from existing graphs through various selection criteria.
通过各种选择条件从现有图创建子图的工具类。
Features | 主要功能:
- Vertex-induced subgraph - 顶点诱导子图
- Edge-induced subgraph - 边诱导子图
- Filter by vertex/edge predicates - 按顶点/边谓词过滤
- K-hop neighborhood extraction - K跳邻域提取
- Graph union/intersection/difference - 图的并集/交集/差集
- Reverse graph - 反转图
- Ego network extraction - 自我网络提取
Usage Examples | 使用示例:
// Vertex-induced subgraph
Graph<String> sub = SubgraphUtil.induced(graph, Set.of("A", "B", "C"));
// Filter vertices by predicate
Graph<String> filtered = SubgraphUtil.filterVertices(graph, v -> v.startsWith("node"));
// K-hop neighborhood
Graph<String> neighborhood = SubgraphUtil.neighborhood(graph, "A", 2);
// Graph union
Graph<String> union = SubgraphUtil.union(graph1, graph2);
// Ego network (1-hop neighborhood)
Graph<String> ego = SubgraphUtil.egoNetwork(graph, "A");
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: Yes (returns empty graphs for null inputs) - 空值安全: 是(null输入返回空图)
Performance | 性能特性:
- Time complexity: O(V + E) - 时间复杂度: O(V + E)
- Space complexity: O(V + E) - 空间复杂度: O(V + E)
- Since:
- JDK 25, opencode-base-graph V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <V> Graph<V> complement(Graph<V> graph) Create the complement of a graph.static <V> Graph<V> Create a copy of the graph.static <V> Graph<V> difference(Graph<V> g1, Graph<V> g2) Compute difference of two graphs (g1 - g2).static <V> Graph<V> edgeInduced(Graph<V> graph, Set<Edge<V>> edges) Create edge-induced subgraph.static <V> Graph<V> egoNetwork(Graph<V> graph, V ego) Extract ego network (1-hop neighborhood).static <V> Graph<V> egoNetwork(Graph<V> graph, V ego, int radius) Extract ego network with specified radius.static <V> Graph<V> filterByWeight(Graph<V> graph, double minWeight, double maxWeight) Filter edges by weight range.static <V> Graph<V> filterEdges(Graph<V> graph, Predicate<Edge<V>> predicate) Filter graph by edge predicate.static <V> Graph<V> filterVertices(Graph<V> graph, Predicate<V> predicate) Filter graph by vertex predicate.static <V> Graph<V> Create vertex-induced subgraph.static <V> Graph<V> intersection(Graph<V> g1, Graph<V> g2) Compute intersection of two graphs.static <V> Graph<V> neighborhood(Graph<V> graph, V center, int k) Extract k-hop neighborhood of a vertex.static <V> Graph<V> removeIsolated(Graph<V> graph) Remove isolated vertices (vertices with no edges).static <V> Graph<V> Create a reversed (transposed) graph.static <V> Graph<V> sampleEdges(Graph<V> graph, int numEdges, Random random) Sample a random subgraph with specified number of edges.static <V> Graph<V> sampleVertices(Graph<V> graph, int numVertices, Random random) Sample a random subgraph with specified number of vertices.static <V> Graph<V> symmetricDifference(Graph<V> g1, Graph<V> g2) Compute symmetric difference of two graphs.static <V> Graph<V> Compute union of two graphs.
-
Method Details
-
induced
Create vertex-induced subgraph. 创建顶点诱导子图。The induced subgraph contains all specified vertices and all edges between those vertices that exist in the original graph.
诱导子图包含所有指定的顶点以及原图中这些顶点之间存在的所有边。
- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图vertices- the vertices to include | 要包含的顶点- Returns:
- the induced subgraph | 诱导子图
-
edgeInduced
Create edge-induced subgraph. 创建边诱导子图。The induced subgraph contains all specified edges and their endpoint vertices.
诱导子图包含所有指定的边及其端点顶点。
- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图edges- the edges to include | 要包含的边- Returns:
- the induced subgraph | 诱导子图
-
filterVertices
-
filterEdges
-
filterByWeight
Filter edges by weight range. 按权重范围过滤边。- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图minWeight- minimum weight (inclusive) | 最小权重(含)maxWeight- maximum weight (inclusive) | 最大权重(含)- Returns:
- filtered subgraph | 过滤后的子图
-
neighborhood
Extract k-hop neighborhood of a vertex. 提取顶点的k跳邻域。Returns the subgraph containing all vertices within k hops of the center vertex and all edges between them.
返回包含中心顶点k跳范围内所有顶点及其之间所有边的子图。
- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图center- the center vertex | 中心顶点k- maximum distance (hops) | 最大距离(跳数)- Returns:
- neighborhood subgraph | 邻域子图
-
egoNetwork
-
egoNetwork
Extract ego network with specified radius. 提取指定半径的自我网络。- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图ego- the ego vertex | 自我顶点radius- the radius (hops) | 半径(跳数)- Returns:
- ego network subgraph | 自我网络子图
-
union
Compute union of two graphs. 计算两个图的并集。The union contains all vertices and edges from both graphs.
并集包含两个图的所有顶点和边。
- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
g1- the first graph | 第一个图g2- the second graph | 第二个图- Returns:
- union graph | 并集图
-
intersection
Compute intersection of two graphs. 计算两个图的交集。The intersection contains vertices and edges present in both graphs.
交集包含两个图中都存在的顶点和边。
- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
g1- the first graph | 第一个图g2- the second graph | 第二个图- Returns:
- intersection graph | 交集图
-
difference
Compute difference of two graphs (g1 - g2). 计算两个图的差集(g1 - g2)。The difference contains vertices and edges from g1 that are not in g2.
差集包含g1中但不在g2中的顶点和边。
- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
g1- the first graph | 第一个图g2- the second graph | 第二个图- Returns:
- difference graph | 差集图
-
symmetricDifference
Compute symmetric difference of two graphs. 计算两个图的对称差集。The symmetric difference contains elements in either graph but not in both.
对称差集包含在任一图中但不同时在两个图中的元素。
- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
g1- the first graph | 第一个图g2- the second graph | 第二个图- Returns:
- symmetric difference graph | 对称差集图
-
reverse
Create a reversed (transposed) graph. 创建反转(转置)图。For directed graphs, all edge directions are reversed. For undirected graphs, returns a copy.
对于有向图,所有边方向反转。对于无向图,返回副本。
- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图- Returns:
- reversed graph | 反转图
-
copy
-
complement
-
removeIsolated
-
sampleVertices
Sample a random subgraph with specified number of vertices. 采样具有指定顶点数的随机子图。- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图numVertices- number of vertices to sample | 要采样的顶点数random- random number generator | 随机数生成器- Returns:
- sampled subgraph | 采样子图
-
sampleEdges
Sample a random subgraph with specified number of edges. 采样具有指定边数的随机子图。- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图numEdges- number of edges to sample | 要采样的边数random- random number generator | 随机数生成器- Returns:
- sampled subgraph | 采样子图
-