Class Graph<N,A>

java.lang.Object
com.amalgamasimulation.graph.Graph<N,A>
Type Parameters:
N - type of values contained in graph nodes
A - type of values contained in graph nodes

public class Graph<N,A> extends Object
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,A>.Node and Graph<N,A>.Arc. 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
  • Field Details

    • valuesToNodes

      protected Map<N,Graph<N,A>.Node> valuesToNodes
    • valuesToArcs

      protected Map<A,Graph<N,A>.Arc> valuesToArcs
    • nodesList

      protected List<Graph<N,A>.Node> nodesList
    • arcsList

      protected List<Graph<N,A>.Arc> 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

      public String toString()
      Overrides:
      toString in class Object
    • addNode

      public Graph<N,A>.Node addNode(N value)
      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, use containsNode(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

      public Graph<N,A>.Arc addArc(A arcValue, N sourceNodeValue, N destNodeValue)
      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 and null will be returned.
      Parameters:
      arcValue - The value of the arc being added
      sourceNodeValue - The value of the source node of the arc being added
      destNodeValue - The value of the destination node of the arc being added
      Returns:
      Created arc of the graph, or null if no nodes with sourceNodeValue and destNodeValue were found
    • removeArc

      public boolean removeArc(A arcValue)
      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 returns false
      Parameters:
      arcValue - The specified value
      Returns:
      true if an arc was removed from the graph, false otherwise
    • removeArc

      public boolean removeArc(Graph<N,A>.Arc arc)
      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 returns false
      Parameters:
      arc - The specified arc
      Returns:
      true if the arc was removed from the graph, false otherwise
    • removeAllArcs

      public int removeAllArcs(Collection<Graph<N,A>.Arc> arcsToRemove)
      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

      public boolean removeNode(Graph<N,A>.Node node)
      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 returns false
      Parameters:
      node - The specified node
      Returns:
      true if the node was removed from the graph, false otherwise
    • removeAllNodes

      public int removeAllNodes(Collection<Graph<N,A>.Node> nodesToRemove)
      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

      public boolean containsNode(N value)
      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

      public boolean containsNode(Graph<N,A>.Node node)
      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

      public boolean containsArc(Graph<N,A>.Arc arc)
      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

      public Graph<N,A>.Node getNode(N value)
      Returns a graph node containing the specified value.
      Parameters:
      value - The specified value
      Returns:
      Node containing the specified value if such node exists, null otherwise
    • getArc

      public Graph<N,A>.Arc getArc(A value)
      Returns a graph arc containing the specified value.
      Parameters:
      value - The specified value
      Returns:
      Arc containing the specified value if such arc exists, null otherwise
    • getArcs

      public List<Graph<N,A>.Arc> 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

      public List<Graph<N,A>.Node> 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