Class GraphTransform
java.lang.Object
cloud.opencode.base.graph.GraphTransform
Graph Transform - Utility for transforming graphs through vertex/edge mapping and filtering
图变换 - 通过顶点/边映射和过滤来变换图的工具类
Provides static methods for common graph transformations including vertex mapping, vertex filtering (induced subgraph), edge filtering, and edge reversal.
提供常见图变换的静态方法,包括顶点映射、顶点过滤(诱导子图)、边过滤和边反转。
Features | 主要功能:
- Map vertices to new types - 将顶点映射为新类型
- Filter vertices (induced subgraph) - 过滤顶点(诱导子图)
- Filter edges - 过滤边
- Reverse directed graphs - 反转有向图
Usage Examples | 使用示例:
Graph<Integer> intGraph = OpenGraph.directed();
intGraph.addEdge(1, 2, 3.0);
// Map vertices: Integer -> String
Graph<String> strGraph = GraphTransform.mapVertices(intGraph, String::valueOf);
// Filter vertices
Graph<Integer> filtered = GraphTransform.filterVertices(intGraph, v -> v > 0);
// Reverse edges
Graph<Integer> reversed = GraphTransform.reverse(intGraph);
- Since:
- JDK 25, opencode-base-graph V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <V> Graph<V> filterEdges(Graph<V> graph, Predicate<Edge<V>> predicate) Filter edges, keeping all vertices but only edges matching the predicate 过滤边,保留所有顶点但仅保留匹配谓词的边static <V> Graph<V> filterVertices(Graph<V> graph, Predicate<V> predicate) Filter vertices, keeping only those matching the predicate (induced subgraph) 过滤顶点,仅保留匹配谓词的顶点(诱导子图)static <V,R> Graph <R> mapVertices(Graph<V> graph, Function<V, R> mapper) Map all vertices to a new type, preserving edges and weights 将所有顶点映射为新类型,保留边和权重static <V> Graph<V> Reverse all edges in a directed graph; for undirected graphs, return a copy 反转有向图的所有边;对于无向图,返回副本
-
Method Details
-
mapVertices
Map all vertices to a new type, preserving edges and weights 将所有顶点映射为新类型,保留边和权重Note: If the mapper is not injective (i.e. two distinct vertices are mapped to the same value), the resulting vertices are silently merged. Edges from both original vertices will appear to originate from the single merged vertex. Callers should ensure the mapper produces unique values when vertex identity must be preserved.
注意: 如果映射函数不是单射(即两个不同的顶点映射为相同的值), 则结果中这些顶点会被静默合并。如需保留顶点唯一性,调用方应确保映射函数产生唯一值。
- Type Parameters:
V- the original vertex type | 原始顶点类型R- the new vertex type | 新顶点类型- Parameters:
graph- the source graph | 源图mapper- the vertex mapping function (should be injective to preserve structure) | 顶点映射函数(应为单射以保留结构)- Returns:
- a new graph with mapped vertices | 映射顶点后的新图
- Throws:
NullPointerException- if graph or mapper is null | 当图或映射函数为null时抛出
-
filterVertices
Filter vertices, keeping only those matching the predicate (induced subgraph) 过滤顶点,仅保留匹配谓词的顶点(诱导子图)Edges are kept only if both endpoints match the predicate.
仅当边的两个端点都匹配谓词时才保留边。
- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图predicate- the vertex filter predicate | 顶点过滤谓词- Returns:
- a new graph with filtered vertices and their connecting edges | 过滤后的新图
- Throws:
NullPointerException- if graph or predicate is null | 当图或谓词为null时抛出
-
filterEdges
Filter edges, keeping all vertices but only edges matching the predicate 过滤边,保留所有顶点但仅保留匹配谓词的边- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图predicate- the edge filter predicate | 边过滤谓词- Returns:
- a new graph with all vertices and filtered edges | 包含所有顶点和过滤后边的新图
- Throws:
NullPointerException- if graph or predicate is null | 当图或谓词为null时抛出
-
reverse
Reverse all edges in a directed graph; for undirected graphs, return a copy 反转有向图的所有边;对于无向图,返回副本- Type Parameters:
V- the vertex type | 顶点类型- Parameters:
graph- the source graph | 源图- Returns:
- a new graph with reversed edges (directed) or a copy (undirected) | 反转边的新图(有向)或副本(无向)
- Throws:
NullPointerException- if graph is null | 当图为null时抛出
-