Package com.amalgamasimulation.graph
Class Graph<N,A>
java.lang.Object
com.amalgamasimulation.graph.Graph<N,A>
- Type Parameters:
N- type of values contained in graph nodesA- type of values contained in graph nodes
Represents an oriented graph with instances of NodeValueType in nodes and
instances of ArcValueType in arcs. The graph does not necessarily has a
spatial representation, it can be a graph of dependencies between objects or
anything else.
This graph has two inner classes - Graph<N, and Graph<N,. These
classes are wrappers for values contained in graph nodes and arcs,
respectively.
The simple example of creating a graph with 3 nodes and 3 arcs containing String values can look like the following:
Graph<String, String> graph = new Graph<>();
graph.addNode("A");
graph.addNode("B");
graph.addNode("C");
graph.addArc("AB", "A", "B");
graph.addArc("BA", "B", "A");
graph.addArc("BC", "B", "C");
- Author:
- Andrey Malykhanov
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassRepresents an arc of the graph.classRepresents a node of the graph. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds an arc containing the specified value to the graph.Adds a node containing the specified value to the graph.voidclear()Clears this graph, removes all nodes and arcs from it.booleancontainsArc(Graph<N, A>.Arc arc) Checks if the graph contains the specified arc.booleancontainsNode(Graph<N, A>.Node node) Checks if the graph contains the specified node.booleancontainsNode(N value) Checks if the graph contains the specified value in its nodes.Returns a graph arc containing the specified value.getArcs()Returns unmodifiable list of all arcs in the graph.Returns a graph node containing the specified value.getNodes()Returns unmodifiable list of all nodes in the graph.booleanisEmpty()Checks if the graph is empty (i.e.intremoveAllArcs(Collection<Graph<N, A>.Arc> arcsToRemove) Attempts to remove all specified arcs from the graph and returns number of arcs actually removed.intremoveAllNodes(Collection<Graph<N, A>.Node> nodesToRemove) Attempts to remove all specified nodes from the graph and returns number of nodes actually removed.booleanAttempts to remove arc with the specified value from the graph.booleanAttempts to remove the specified arc from the graph.booleanremoveNode(Graph<N, A>.Node node) Attempts to remove the specified node from the graph.toString()
-
Field Details
-
valuesToNodes
-
valuesToArcs
-
nodesList
-
arcsList
-
nextArcId
protected int nextArcId -
nextNodeId
protected int nextNodeId
-
-
Constructor Details
-
Graph
public Graph()Creates a new empty graph (the one with no nodes and no arcs).
-
-
Method Details
-
clear
public void clear()Clears this graph, removes all nodes and arcs from it. -
isEmpty
public boolean isEmpty()Checks if the graph is empty (i.e. contains no nodes and no arcs).- Returns:
- true if graph is empty, false otherwise
-
toString
-
addNode
Adds a node containing the specified value to the graph. If a node with specified value already exists in the graph, a new node containing the same value will be created anyway. If necessary, usecontainsNode(Object)method to check if graph already contains node containing the specified value.- Parameters:
value- The value of the node being added- Returns:
- Created node of the graph
-
addArc
Adds an arc containing the specified value to the graph. If an arc with specified value already exists in the graph, a new arc containing the same value will be created anyway. If there are no nodes with specified values of sourceNodeValue and destNodeValue, no arc will be created andnullwill be returned.- Parameters:
arcValue- The value of the arc being addedsourceNodeValue- The value of the source node of the arc being addeddestNodeValue- The value of the destination node of the arc being added- Returns:
- Created arc of the graph, or
nullif no nodes with sourceNodeValue and destNodeValue were found
-
removeArc
Attempts to remove arc with the specified value from the graph. If there is an arc with the specified value in the graph, it is removed from the graph. All references from nodes to this arc are also removed from the graph. If there is no arc with the specified value, does nothing and returnsfalse- Parameters:
arcValue- The specified value- Returns:
trueif an arc was removed from the graph,falseotherwise
-
removeArc
Attempts to remove the specified arc from the graph. If the specified arc is in the graph, it is removed from the graph. All references from nodes to this arc are also removed from the graph. If there is no specified arc in the graph, does nothing and returnsfalse- Parameters:
arc- The specified arc- Returns:
trueif the arc was removed from the graph,falseotherwise
-
removeAllArcs
Attempts to remove all specified arcs from the graph and returns number of arcs actually removed.- Parameters:
arcsToRemove- The specified arcs- Returns:
- Number of arcs actually removed
-
removeNode
Attempts to remove the specified node from the graph. If the specified node is in the graph, it is removed from the graph. All arcs adjacent to this node are also removed from the graph. If there is no specified node in the graph, does nothing and returnsfalse- Parameters:
node- The specified node- Returns:
trueif the node was removed from the graph,falseotherwise
-
removeAllNodes
Attempts to remove all specified nodes from the graph and returns number of nodes actually removed.- Parameters:
nodesToRemove- The specified nodes- Returns:
- Number of nodes actually removed
-
containsNode
Checks if the graph contains the specified value in its nodes.- Parameters:
value- The specified value- Returns:
- True if the graph contains the specified value, false otherwise
-
containsNode
Checks if the graph contains the specified node.- Parameters:
node- The specified node- Returns:
- True if the graph contains the specified node, false otherwise
-
containsArc
Checks if the graph contains the specified arc.- Parameters:
arc- The specified arc- Returns:
- True if the graph contains the specified arc, false otherwise
-
getNode
Returns a graph node containing the specified value.- Parameters:
value- The specified value- Returns:
- Node containing the specified value if such node exists,
nullotherwise
-
getArc
Returns a graph arc containing the specified value.- Parameters:
value- The specified value- Returns:
- Arc containing the specified value if such arc exists,
nullotherwise
-
getArcs
Returns unmodifiable list of all arcs in the graph. List is sorted in the order the arcs were added to the graph.- Returns:
- List of all arcs in the graph
-
getNodes
Returns unmodifiable list of all nodes in the graph. List is sorted in the order the nodes were added to the graph.- Returns:
- List of all nodes in the graph
-