Class Graph<N,E extends Number>

java.lang.Object
util.collection.Graph<N,E>
Type Parameters:
N - the type of nodes in the graph
E - the type of edge weights, which must extend Number

public class Graph<N,E extends Number> extends Object
Graph is a generic class representing a graph data structure with nodes and weighted edges.

It supports both directed and undirected graphs, and provides methods for common graph operations such as adding nodes and edges, checking connectivity, cycles, paths, and more.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Graph.Edge<N,E extends Number>
    Edge represents a connection between two nodes in the graph, possibly with a weight and additional properties.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty undirected graph.
    Constructs an undirected graph with the specified nodes and edges.
    Graph(Collection<N> nodes, Collection<Graph.Edge<N,E>> edges, boolean directed)
    Constructs a graph with the specified nodes, edges, and directionality.
    Constructs an undirected graph with the specified edges.
    Graph(Collection<Graph.Edge<N,E>> edges, boolean directed)
    Constructs a graph with the specified edges and directionality.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Checks if all edges in the graph match the given condition.
    boolean
    Checks if all nodes in the graph match the given condition.
    boolean
    Checks if any edge in the graph matches the given condition.
    boolean
    Checks if any node in the graph matches the given condition.
    int
    Returns the number of connected components in the graph.
    boolean
    containsEdge(N from, N to)
    Checks if the graph contains an edge from one node to another.
    boolean
    Checks if the graph contains the specified edge.
    boolean
    Checks if the graph contains the specified node.
    int
    Returns the number of edges in the graph.
    int
    Returns the number of nodes in the graph.
    Returns the set of all edges in the graph.
    Returns the set of neighbors for a given node.
    Returns the set of all nodes in the graph.
    boolean
    Checks if the graph contains any cycles.
    boolean
    hasPath(N start, N end)
    Checks if there is a path between two nodes in the graph.
    boolean
    Checks if the graph is a binary tree (each node has at most two children).
    boolean
    Checks if the graph is connected (there is a path between every pair of nodes).
    boolean
    Checks if the graph is directed.
    boolean
    Checks if the graph is empty (contains no nodes).
    boolean
    Checks if the graph is a tree (undirected, connected, and acyclic).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Graph

      public Graph(Collection<N> nodes, Collection<Graph.Edge<N,E>> edges, boolean directed)
      Constructs a graph with the specified nodes, edges, and directionality.
      Parameters:
      nodes - the collection of nodes
      edges - the collection of edges
      directed - true if the graph is directed, false if undirected
    • Graph

      public Graph(Collection<N> nodes, Collection<Graph.Edge<N,E>> edges)
      Constructs an undirected graph with the specified nodes and edges.
      Parameters:
      nodes - the collection of nodes
      edges - the collection of edges
    • Graph

      public Graph(Collection<Graph.Edge<N,E>> edges, boolean directed)
      Constructs a graph with the specified edges and directionality.
      Parameters:
      edges - the collection of edges
      directed - true if the graph is directed, false if undirected
    • Graph

      public Graph(Collection<Graph.Edge<N,E>> edges)
      Constructs an undirected graph with the specified edges.
      Parameters:
      edges - the collection of edges
    • Graph

      public Graph()
      Constructs an empty undirected graph.
  • Method Details

    • getNeighbors

      public Set<N> getNeighbors(N node)
      Returns the set of neighbors for a given node.
      Parameters:
      node - the node whose neighbors are to be returned
      Returns:
      a set of neighboring nodes
    • getNodes

      public Set<N> getNodes()
      Returns the set of all nodes in the graph.
      Returns:
      a set of all nodes
    • getEdges

      public Set<Graph.Edge<N,E>> getEdges()
      Returns the set of all edges in the graph.
      Returns:
      a set of all edges
    • isEmpty

      public boolean isEmpty()
      Checks if the graph is empty (contains no nodes).
      Returns:
      true if the graph is empty, false otherwise
    • isTree

      public boolean isTree()
      Checks if the graph is a tree (undirected, connected, and acyclic).
      Returns:
      true if the graph is a tree, false otherwise
    • isBinaryTree

      public boolean isBinaryTree()
      Checks if the graph is a binary tree (each node has at most two children).
      Returns:
      true if the graph is a binary tree, false otherwise
    • isDirected

      public boolean isDirected()
      Checks if the graph is directed.
      Returns:
      true if the graph is directed, false otherwise
    • isConnected

      public boolean isConnected()
      Checks if the graph is connected (there is a path between every pair of nodes).
      Returns:
      true if the graph is connected, false otherwise
    • hasCycle

      public boolean hasCycle()
      Checks if the graph contains any cycles.
      Returns:
      true if the graph has a cycle, false otherwise
    • containsNode

      public boolean containsNode(N node)
      Checks if the graph contains the specified node.
      Parameters:
      node - the node to check
      Returns:
      true if the node exists in the graph, false otherwise
    • containsEdge

      public boolean containsEdge(N from, N to)
      Checks if the graph contains an edge from one node to another.
      Parameters:
      from - the source node
      to - the destination node
      Returns:
      true if the edge exists, false otherwise
    • containsEdge

      public boolean containsEdge(Graph.Edge<N,E> edge)
      Checks if the graph contains the specified edge.
      Parameters:
      edge - the edge to check
      Returns:
      true if the edge exists, false otherwise
    • anyNodesMatch

      public boolean anyNodesMatch(Predicate<N> condition)
      Checks if any node in the graph matches the given condition.
      Parameters:
      condition - the predicate to test nodes
      Returns:
      true if any node matches, false otherwise
    • allNodesMatch

      public boolean allNodesMatch(Predicate<N> condition)
      Checks if all nodes in the graph match the given condition.
      Parameters:
      condition - the predicate to test nodes
      Returns:
      true if all nodes match, false otherwise
    • anyEdgesMatch

      public boolean anyEdgesMatch(Predicate<Graph.Edge<N,E>> condition)
      Checks if any edge in the graph matches the given condition.
      Parameters:
      condition - the predicate to test edges
      Returns:
      true if any edge matches, false otherwise
    • allEdgesMatch

      public boolean allEdgesMatch(Predicate<Graph.Edge<N,E>> condition)
      Checks if all edges in the graph match the given condition.
      Parameters:
      condition - the predicate to test edges
      Returns:
      true if all edges match, false otherwise
    • hasPath

      public boolean hasPath(N start, N end)
      Checks if there is a path between two nodes in the graph.
      Parameters:
      start - the starting node
      end - the target node
      Returns:
      true if a path exists, false otherwise
    • countNodes

      public int countNodes()
      Returns the number of nodes in the graph.
      Returns:
      the number of nodes
    • countEdges

      public int countEdges()
      Returns the number of edges in the graph.
      Returns:
      the number of edges
    • connectedComponents

      public int connectedComponents()
      Returns the number of connected components in the graph.
      Returns:
      the number of connected components