Class AdjacencyMatrix<G extends BaseGraph<G,​N,​E>,​N extends Node<G,​N,​E>,​E extends Edge<G,​N,​E>>

  • Type Parameters:
    G - the type of the BaseGraphs this matrix can be used in
    N - the type of the Nodes in this matrix
    E - the type of the Edges in this matrix
    All Implemented Interfaces:
    java.lang.Iterable<java.util.Map.Entry<N,​AdjacencyMatrix.NodeEdges<G,​N,​E>>>

    public class AdjacencyMatrix<G extends BaseGraph<G,​N,​E>,​N extends Node<G,​N,​E>,​E extends Edge<G,​N,​E>>
    extends java.lang.Object
    implements java.lang.Iterable<java.util.Map.Entry<N,​AdjacencyMatrix.NodeEdges<G,​N,​E>>>
    An adjacency matrix for a graph that has Nodes as nodes and Edges as edges. It is represented as a map between a node and a AdjacencyMatrix.NodeEdges.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  AdjacencyMatrix.NodeEdges<G extends BaseGraph<G,​N,​E>,​N extends Node<G,​N,​E>,​E extends Edge<G,​N,​E>>
      Utility class for representing the edges tied to a node, split into two sets: ingoing and outgoing.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addEdge​(E e)
      Adds an edge to this matrix.
      void addNode​(N node)
      Adds the given node to the set of nodes.
      void clear()
      Empties this matrix, removing all the nodes and the edges in it.
      boolean containsEdge​(E edge)
      Yields true if the given edge is contained in this matrix.
      boolean containsNode​(N node)
      Yields true if the given node is contained in this matrix.
      int distance​(N from, N to)
      Yields the minimum distance, in terms of number of edges to traverse, between the given nodes.
      boolean equals​(java.lang.Object obj)  
      java.util.Collection<N> followersOf​(N node)
      Yields the collection of the nodes that are followers of the given one, that is, all nodes such that there exist an edge in this matrix going from the given node to such node.
      E getEdgeConnecting​(N source, N destination)
      Yields the edge connecting the two given nodes, if any.
      java.util.Collection<E> getEdges()
      Yields the set of edges of this matrix.
      java.util.Collection<E> getEdgesConnecting​(N source, N destination)
      Yields all edges connecting the two given nodes, if any.
      java.util.Collection<N> getEntries()
      Yields the entry nodes of this matrix, that is, the nodes that have no predecessors.
      java.util.Collection<N> getExits()
      Yields the exit nodes of this matrix, that is, the nodes that have no followers.
      java.util.Collection<E> getIngoingEdges​(N node)
      Yields the ingoing edges to the given node.
      java.util.Collection<N> getNodes()
      Yields the collection of nodes of this matrix.
      java.util.Collection<E> getOutgoingEdges​(N node)
      Yields the outgoing edges from the given node.
      int hashCode()  
      java.util.Iterator<java.util.Map.Entry<N,​AdjacencyMatrix.NodeEdges<G,​N,​E>>> iterator()  
      void mergeWith​(AdjacencyMatrix<G,​N,​E> other)
      Merges this matrix with the given one, by adding all nodes and all edges contained in other.
      java.util.Collection<N> predecessorsOf​(N node)
      Yields the collection of the nodes that are predecessors of the given vertex, that is, all nodes such that there exist an edge in this matrix going from such node to the given one.
      void removeEdge​(E e)
      Removes the given edge from the matrix.
      void removeNode​(N node)
      Removes the given node from the matrix, together with all its connected edges.
      java.lang.String toString()  
      void validate​(java.util.Collection<N> entrypoints)
      Validates this matrix, ensuring that the it is well formed.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • AdjacencyMatrix

        public AdjacencyMatrix()
        Builds a new matrix.
      • AdjacencyMatrix

        public AdjacencyMatrix​(AdjacencyMatrix<G,​N,​E> other)
        Copies the given matrix by shallow-copying the Nodes and deep-copying the values.
        Parameters:
        other - the matrix to copy
    • Method Detail

      • addNode

        public void addNode​(N node)
        Adds the given node to the set of nodes. Note that, if the given node is already present in the matrix, all existing edges are kept.
        Parameters:
        node - the node to add
      • removeNode

        public void removeNode​(N node)
        Removes the given node from the matrix, together with all its connected edges.
        Parameters:
        node - the node to remove
      • getNodes

        public java.util.Collection<N> getNodes()
        Yields the collection of nodes of this matrix.
        Returns:
        the collection of nodes
      • addEdge

        public void addEdge​(E e)
        Adds an edge to this matrix.
        Parameters:
        e - the edge to add
        Throws:
        java.lang.UnsupportedOperationException - if the source or the destination of the given edge are not part of this matrix
      • removeEdge

        public void removeEdge​(E e)
        Removes the given edge from the matrix.
        Parameters:
        e - the edge to remove
      • getEdgeConnecting

        public E getEdgeConnecting​(N source,
                                   N destination)
        Yields the edge connecting the two given nodes, if any. Yields null if such edge does not exist, or if one of the two node is not inside this matrix.
        Parameters:
        source - the source node
        destination - the destination node
        Returns:
        the edge connecting source to destination, or null
      • getEdgesConnecting

        public java.util.Collection<E> getEdgesConnecting​(N source,
                                                          N destination)
        Yields all edges connecting the two given nodes, if any. Yields an empty collection if no edge exists, or if one of the two nodes is not inside this matrix.
        Parameters:
        source - the source node
        destination - the destination node
        Returns:
        the edges connecting source to destination
      • getIngoingEdges

        public java.util.Collection<E> getIngoingEdges​(N node)
        Yields the ingoing edges to the given node.
        Parameters:
        node - the node
        Returns:
        the collection of ingoing edges
      • getOutgoingEdges

        public java.util.Collection<E> getOutgoingEdges​(N node)
        Yields the outgoing edges from the given node.
        Parameters:
        node - the node
        Returns:
        the collection of outgoing edges
      • getEdges

        public java.util.Collection<E> getEdges()
        Yields the set of edges of this matrix.
        Returns:
        the collection of edges
      • followersOf

        public java.util.Collection<N> followersOf​(N node)
        Yields the collection of the nodes that are followers of the given one, that is, all nodes such that there exist an edge in this matrix going from the given node to such node.
        Parameters:
        node - the node
        Returns:
        the collection of followers
        Throws:
        java.lang.IllegalArgumentException - if the node is not in the graph
      • predecessorsOf

        public java.util.Collection<N> predecessorsOf​(N node)
        Yields the collection of the nodes that are predecessors of the given vertex, that is, all nodes such that there exist an edge in this matrix going from such node to the given one.
        Parameters:
        node - the node
        Returns:
        the collection of predecessors
        Throws:
        java.lang.IllegalArgumentException - if the node is not in the graph
      • containsNode

        public boolean containsNode​(N node)
        Yields true if the given node is contained in this matrix.
        Parameters:
        node - the node to check
        Returns:
        true if the node is in this matrix
      • containsEdge

        public boolean containsEdge​(E edge)
        Yields true if the given edge is contained in this matrix.
        Parameters:
        edge - the edge to check
        Returns:
        true if the edge is in this matrix
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getEntries

        public java.util.Collection<N> getEntries()
        Yields the entry nodes of this matrix, that is, the nodes that have no predecessors.
        Returns:
        the entries nodes
      • getExits

        public java.util.Collection<N> getExits()
        Yields the exit nodes of this matrix, that is, the nodes that have no followers.
        Returns:
        the exit nodes
      • distance

        public int distance​(N from,
                            N to)
        Yields the minimum distance, in terms of number of edges to traverse, between the given nodes. If one of the nodes is not inside this matrix, or if no path can be found, this method returns -1. If the distance is greater than Integer.MAX_VALUE, Integer.MAX_VALUE is returned.
        Parameters:
        from - the starting node
        to - the destination node
        Returns:
        the minimum distance, in terms of number of edges to traverse, between the given nodes
      • mergeWith

        public void mergeWith​(AdjacencyMatrix<G,​N,​E> other)
        Merges this matrix with the given one, by adding all nodes and all edges contained in other.
        Parameters:
        other - the matrix to merge into this one
      • validate

        public void validate​(java.util.Collection<N> entrypoints)
                      throws ProgramValidationException
        Validates this matrix, ensuring that the it is well formed. This method checks that:
        • each Edge is connected to Nodes contained in the matrix
        • all Nodes with no ingoing edges are marked as entrypoints of the cfg (i.e. no deadcode)
        Parameters:
        entrypoints - the collection of Nodes that are considered as entrypoints of the graph built over this adjacency matrix
        Throws:
        ProgramValidationException - if one of the aforementioned checks fail
      • clear

        public void clear()
        Empties this matrix, removing all the nodes and the edges in it.