Class Graph<N,E extends Number>
java.lang.Object
util.collection.Graph<N,E>
- Type Parameters:
N
- the type of nodes in the graphE
- the type of edge weights, which must extendNumber
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 ClassesModifier and TypeClassDescriptionstatic 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
ConstructorsConstructorDescriptionGraph()
Constructs an empty undirected graph.Graph
(Collection<N> nodes, Collection<Graph.Edge<N, E>> edges) 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.Graph
(Collection<Graph.Edge<N, E>> edges) 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 TypeMethodDescriptionboolean
allEdgesMatch
(Predicate<Graph.Edge<N, E>> condition) Checks if all edges in the graph match the given condition.boolean
allNodesMatch
(Predicate<N> condition) Checks if all nodes in the graph match the given condition.boolean
anyEdgesMatch
(Predicate<Graph.Edge<N, E>> condition) Checks if any edge in the graph matches the given condition.boolean
anyNodesMatch
(Predicate<N> condition) 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
containsEdge
(Graph.Edge<N, E> edge) Checks if the graph contains the specified edge.boolean
containsNode
(N node) 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.Set
<Graph.Edge<N, E>> getEdges()
Returns the set of all edges in the graph.getNeighbors
(N node) Returns the set of neighbors for a given node.getNodes()
Returns the set of all nodes in the graph.boolean
hasCycle()
Checks if the graph contains any cycles.boolean
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
isEmpty()
Checks if the graph is empty (contains no nodes).boolean
isTree()
Checks if the graph is a tree (undirected, connected, and acyclic).
-
Constructor Details
-
Graph
Constructs a graph with the specified nodes, edges, and directionality.- Parameters:
nodes
- the collection of nodesedges
- the collection of edgesdirected
- true if the graph is directed, false if undirected
-
Graph
Constructs an undirected graph with the specified nodes and edges.- Parameters:
nodes
- the collection of nodesedges
- the collection of edges
-
Graph
Constructs a graph with the specified edges and directionality.- Parameters:
edges
- the collection of edgesdirected
- true if the graph is directed, false if undirected
-
Graph
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
-
getNodes
-
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
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
-
containsEdge
Checks if the graph contains the specified edge.- Parameters:
edge
- the edge to check- Returns:
- true if the edge exists, false otherwise
-
anyNodesMatch
-
allNodesMatch
-
anyEdgesMatch
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
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
-
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
-