Class AdjacencyMatrixDiGraph<T>

java.lang.Object
it.unimi.di.prog2.h24.digraph.AdjacencyMatrixDiGraph<T>
Type Parameters:
T - the type of the nodes of the graph.
All Implemented Interfaces:
DiGraph<T>

public class AdjacencyMatrixDiGraph<T> extends Object implements DiGraph<T>
A partially mutable directed graph implementation based on an adjacency matrix.

This implementation is partially mutable: the set of nodes is fixed at construction time, while arcs can be added at any time.

  • Field Details

    • nodes

      private final List<T> nodes
      The nodes of the graph, in some order.
    • adjacencyMatrix

      private final boolean[][] adjacencyMatrix
      The adjacency matrix.
  • Constructor Details

    • AdjacencyMatrixDiGraph

      public AdjacencyMatrixDiGraph(Set<? extends T> nodes)
      Creates a graph with the given nodes.
      Parameters:
      nodes - the nodes of the graph.
      Throws:
      NullPointerException - if nodes is, or contains, null.
    • AdjacencyMatrixDiGraph

      public AdjacencyMatrixDiGraph(DiGraph<? extends T> graph)
      Creates a graph with the same nodes and arcs of the given graph.
      Parameters:
      graph - the graph to copy.
      Throws:
      NullPointerException - if graph is null.
  • Method Details

    • nodes

      public Set<T> nodes()
      Description copied from interface: DiGraph
      Returns the graph nodes.
      Specified by:
      nodes in interface DiGraph<T>
      Returns:
      the graph nodes.
    • addNode

      public void addNode(T node)
      Description copied from interface: DiGraph
      Adds a node to this graph.
      Specified by:
      addNode in interface DiGraph<T>
      Parameters:
      node - the node to be added.
    • addArc

      public void addArc(T source, T destination)
      Description copied from interface: DiGraph
      Adds an arc to this graph.

      It the source, or destination, node are not present in the graph, they are also added.

      Specified by:
      addArc in interface DiGraph<T>
      Parameters:
      source - the source of the arc to be added.
      destination - the destination of the arc to be added.
    • outgoing

      public Set<T> outgoing(T source)
      Description copied from interface: DiGraph
      Returns the set of outgoing nodes of a given node.
      Specified by:
      outgoing in interface DiGraph<T>
      Parameters:
      source - the source node.
      Returns:
      the nodes that have an arc with the given source in this graph (the collection is empty if the source does not belong to this graph).