Package dev.hypera.chameleon.util.graph
Interface Graph<T>
-
- Type Parameters:
T- Node type.
public interface Graph<T>Graph.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceGraph.Builder<T>Graph builder.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description booleanaddNode(T node)Addsnodeto this graph, if it is not already present.@NotNull Collection<T>adjacentNodes(T node)Returns the nodes which have an edge withnodein this graph.static <T> @NotNull Graph.Builder<T>directed()Create a new directed graph builder.@NotNull Collection<Edge<T>>edges()Returns all edges in this graph.@NotNull Collection<T>nodes()Returns all nodes in this graph.@NotNull Collection<T>predecessors(T node)Returns the nodes in this graph that have an edge withnodeas a target.booleanputEdge(@NotNull Edge<T> edge)Adds an edge connectingEdge.source()andEdge.target(), if not already present.default booleanputEdge(T source, T target)Adds an edge connectingsourceandtarget, if not already present.booleanremoveEdge(@NotNull Edge<T> edge)Removes the edge connectingEdge.source()andEdge.target(), if present.default booleanremoveEdge(T source, T target)Removes the edge connectingsourceandtarget, if present.booleanremoveNode(T node)Removesnodefrom this graph, if it is present.@NotNull Collection<T>successors(T node)Returns the nodes in this graph that have an edge withnodeas a source.
-
-
-
Method Detail
-
directed
@Contract(value="-> new", pure=true) @NotNull static <T> @NotNull Graph.Builder<T> directed()Create a new directed graph builder.- Type Parameters:
T- Graph node type.- Returns:
- new builder.
-
nodes
@Contract(value="-> _", pure=true) @NotNull @NotNull Collection<T> nodes()Returns all nodes in this graph.- Returns:
- graph nodes.
-
edges
@Contract(value="-> _", pure=true) @NotNull @NotNull Collection<Edge<T>> edges()Returns all edges in this graph.- Returns:
- graph edges.
-
adjacentNodes
@Contract(value="_ -> _", pure=true) @NotNull @NotNull Collection<T> adjacentNodes(@NotNull T node)Returns the nodes which have an edge withnodein this graph.This is equivalent to the union of
predecessors(Object)andsuccessors(Object)- Parameters:
node- Node.- Returns:
- adjacent nodes to
node.
-
predecessors
@Contract(value="_ -> _", pure=true) @NotNull @NotNull Collection<T> predecessors(@NotNull T node)Returns the nodes in this graph that have an edge withnodeas a target.In an undirected graph this is equivalent to
adjacentNodes(Object).- Parameters:
node- Node to get predecessors.- Returns:
- predecessors to
node.
-
successors
@Contract(value="_ -> _", pure=true) @NotNull @NotNull Collection<T> successors(@NotNull T node)Returns the nodes in this graph that have an edge withnodeas a source.In an undirected graph this is equivalent to
adjacentNodes(Object).- Parameters:
node- Node to get successors of.- Returns:
- successors to
node.
-
addNode
@Contract("_ -> _") boolean addNode(@NotNull T node)Addsnodeto this graph, if it is not already present.- Parameters:
node- Node to be added.- Returns:
trueif the graph changed as a result of this call.
-
removeNode
@Contract("_ -> _") boolean removeNode(@NotNull T node)Removesnodefrom this graph, if it is present.All edges connecting to
nodewill also be removed.- Parameters:
node- Node to be removed.- Returns:
trueif the graph changed as a result of this call.
-
putEdge
@Contract("_, _ -> _") default boolean putEdge(@NotNull T source, @NotNull T target)Adds an edge connectingsourceandtarget, if not already present.If this graph is directed, the edge will also be directed; otherwise it will be undirected.
- Parameters:
source- Source node.target- Target node.- Returns:
trueif the graph changed as a result of this call.- Throws:
IllegalArgumentException- if this graph does not allow self-loops and this edge is a self-loop.
-
putEdge
@Contract("_ -> _") boolean putEdge(@NotNull @NotNull Edge<T> edge)Adds an edge connectingEdge.source()andEdge.target(), if not already present.If this graph is directed, the edge will also be directed; otherwise it will be undirected.
- Parameters:
edge- Edge to be added.- Returns:
trueif the graph changed as a result of this call.- Throws:
IllegalArgumentException- if this graph does not allow self-loops and this edge is a self-loop.
-
removeEdge
@Contract("_, _ -> _") default boolean removeEdge(@NotNull T source, @NotNull T target)Removes the edge connectingsourceandtarget, if present.- Parameters:
source- Source node.target- Target node.- Returns:
trueif the graph changed as a result of this call.
-
removeEdge
@Contract("_ -> _") boolean removeEdge(@NotNull @NotNull Edge<T> edge)Removes the edge connectingEdge.source()andEdge.target(), if present.- Parameters:
edge- Edge to be removed.- Returns:
trueif the graph changed as a result of this call.
-
-