Interface ValueGraph<N,V>

Type Parameters:
N - node type | 节点类型
V - edge value type | 边值类型
All Known Subinterfaces:
MutableValueGraph<N,V>

public interface ValueGraph<N,V>
ValueGraph - Value Graph Interface ValueGraph - 值图接口

An interface for graph data structures where edges have associated values (weights).

边带有关联值(权重)的图数据结构接口。

Features | 主要功能:

  • Node and edge operations - 节点和边操作
  • Edge value queries - 边值查询
  • Directed/undirected support - 有向/无向支持
  • Adjacency queries - 邻接查询

Usage Examples | 使用示例:

MutableValueGraph<String, Double> graph = MutableValueGraph.directed();
graph.addNode("A");
graph.addNode("B");
graph.putEdgeValue("A", "B", 3.5);

Optional<Double> weight = graph.edgeValue("A", "B");  // Optional[3.5]
boolean connected = graph.hasEdgeConnecting("A", "B"); // true

Security | 安全性:

  • Thread-safe: No (interface, implementation-dependent) - 否(接口,取决于实现)
  • Null-safe: No - 否
Since:
JDK 25, opencode-base-collections V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final record 
    Endpoint pair representing an edge between two nodes.
  • Method Summary

    Modifier and Type
    Method
    Description
    Return all adjacent nodes (both successors and predecessors).
    int
    degree(N node)
    Return the degree of a node (number of edges incident to it).
    default int
    Return the number of edges.
    Return the set of all edges in this graph.
    edgeValue(N nodeU, N nodeV)
    Return the value of the edge connecting nodeU to nodeV, if present.
    edgeValueOrDefault(N nodeU, N nodeV, V defaultValue)
    Return the value of the edge connecting nodeU to nodeV, or defaultValue if no edge exists.
    boolean
    hasEdgeConnecting(N nodeU, N nodeV)
    Check if there is an edge connecting nodeU to nodeV.
    boolean
    Check if this graph is directed.
    default int
    Return the number of nodes.
    Return the set of all nodes in this graph.
    Return the predecessors (incoming neighbors) of a node.
    successors(N node)
    Return the successors (outgoing neighbors) of a node.
  • Method Details

    • nodes

      Set<N> nodes()
      Return the set of all nodes in this graph. 返回此图中所有节点的集合。
      Returns:
      the set of nodes | 节点集合
    • edges

      Return the set of all edges in this graph. 返回此图中所有边的集合。
      Returns:
      the set of edges | 边集合
    • edgeValue

      Optional<V> edgeValue(N nodeU, N nodeV)
      Return the value of the edge connecting nodeU to nodeV, if present. 返回连接 nodeUnodeV 的边的值(如果存在)。
      Parameters:
      nodeU - the source node | 源节点
      nodeV - the target node | 目标节点
      Returns:
      the edge value, or empty if no edge exists | 边值,如果不存在则返回空
      Throws:
      IllegalArgumentException - if either node is not in the graph | 如果节点不在图中则抛出异常
    • edgeValueOrDefault

      V edgeValueOrDefault(N nodeU, N nodeV, V defaultValue)
      Return the value of the edge connecting nodeU to nodeV, or defaultValue if no edge exists. 返回连接 nodeUnodeV 的边的值,如果不存在则返回默认值。
      Parameters:
      nodeU - the source node | 源节点
      nodeV - the target node | 目标节点
      defaultValue - the default value | 默认值
      Returns:
      the edge value, or default if no edge exists | 边值或默认值
      Throws:
      IllegalArgumentException - if either node is not in the graph | 如果节点不在图中则抛出异常
    • hasEdgeConnecting

      boolean hasEdgeConnecting(N nodeU, N nodeV)
      Check if there is an edge connecting nodeU to nodeV. 检查是否存在从 nodeUnodeV 的边。
      Parameters:
      nodeU - the source node | 源节点
      nodeV - the target node | 目标节点
      Returns:
      true if edge exists | 如果边存在则返回 true
    • adjacentNodes

      Set<N> adjacentNodes(N node)
      Return all adjacent nodes (both successors and predecessors). 返回所有邻接节点(后继和前驱)。
      Parameters:
      node - the node | 节点
      Returns:
      the set of adjacent nodes | 邻接节点集合
      Throws:
      IllegalArgumentException - if node is not in the graph | 如果节点不在图中则抛出异常
    • predecessors

      Set<N> predecessors(N node)
      Return the predecessors (incoming neighbors) of a node. 返回节点的前驱(入边邻居)。
      Parameters:
      node - the node | 节点
      Returns:
      the set of predecessors | 前驱集合
      Throws:
      IllegalArgumentException - if node is not in the graph | 如果节点不在图中则抛出异常
    • successors

      Set<N> successors(N node)
      Return the successors (outgoing neighbors) of a node. 返回节点的后继(出边邻居)。
      Parameters:
      node - the node | 节点
      Returns:
      the set of successors | 后继集合
      Throws:
      IllegalArgumentException - if node is not in the graph | 如果节点不在图中则抛出异常
    • degree

      int degree(N node)
      Return the degree of a node (number of edges incident to it). 返回节点的度数(入射边的数量)。
      Parameters:
      node - the node | 节点
      Returns:
      the degree | 度数
      Throws:
      IllegalArgumentException - if node is not in the graph | 如果节点不在图中则抛出异常
    • isDirected

      boolean isDirected()
      Check if this graph is directed. 检查此图是否有向。
      Returns:
      true if directed | 如果有向则返回 true
    • nodeCount

      default int nodeCount()
      Return the number of nodes. 返回节点数量。
      Returns:
      the number of nodes | 节点数量
    • edgeCount

      default int edgeCount()
      Return the number of edges. 返回边数量。
      Returns:
      the number of edges | 边数量