- java.lang.Object
-
- it.unive.lisa.util.datastructures.graph.AdjacencyMatrix<N,E,G>
-
- Type Parameters:
N- the type of theNodes in this matrixE- the type of theEdges in this matrixG- the type of theGraphs this matrix can be used in
- All Implemented Interfaces:
java.lang.Iterable<java.util.Map.Entry<N,AdjacencyMatrix.NodeEdges<N,E,G>>>
public class AdjacencyMatrix<N extends Node<N,E,G>,E extends Edge<N,E,G>,G extends Graph<G,N,E>> extends java.lang.Object implements java.lang.Iterable<java.util.Map.Entry<N,AdjacencyMatrix.NodeEdges<N,E,G>>>
An adjacency matrix for a graph that hasNodes as nodes andEdges as edges. It is represented as a map between a node and aAdjacencyMatrix.NodeEdges.
-
-
Constructor Summary
Constructors Constructor Description AdjacencyMatrix()Builds a new matrix.AdjacencyMatrix(AdjacencyMatrix<N,E,G> other)Copies the given matrix by keeping the same edgeExternalSetCache, shallow-copying theNodes and deep-copying the values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddEdge(E e)Adds an edge to this matrix.voidaddNode(N node)Adds the given node to the set of nodes.booleancontainsEdge(E edge)Yieldstrueif the given edge is contained in this matrix.booleancontainsNode(N node)Yieldstrueif the given node is contained in this matrix.intdistance(N from, N to)Yields the minimum distance, in terms of number of edges to traverse, between the given nodes.booleanequals(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.EgetEdgeConnecting(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<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.inthashCode()java.util.Iterator<java.util.Map.Entry<N,AdjacencyMatrix.NodeEdges<N,E,G>>>iterator()voidmergeWith(AdjacencyMatrix<N,E,G> other)Merges this matrix with the given one, by adding all nodes and all edges contained inother.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.voidremoveEdge(E e)Removes the given edge from the matrix.voidremoveFrom(N root)Removes every node and edge that is reachable from the givenroot.voidremoveNode(N node)Removes the given node from the matrix, together with all its connected edges.voidsimplify(java.lang.Iterable<N> targets, java.util.Collection<N> entrypoints, java.util.Collection<E> removedEdges, java.util.Map<org.apache.commons.lang3.tuple.Pair<E,E>,E> replacedEdges)Simplifies this matrix, removing all the given nodes and rewriting the edge set accordingly.java.lang.StringtoString()voidvalidate(java.util.Collection<N> entrypoints)Validates this matrix, ensuring that the it is well formed.
-
-
-
Constructor Detail
-
AdjacencyMatrix
public AdjacencyMatrix()
Builds a new matrix.
-
AdjacencyMatrix
public AdjacencyMatrix(AdjacencyMatrix<N,E,G> other)
Copies the given matrix by keeping the same edgeExternalSetCache, shallow-copying theNodes 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 final 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 final E getEdgeConnecting(N source, N destination)
Yields the edge connecting the two given nodes, if any. Yieldsnullif such edge does not exist, or if one of the two node is not inside this matrix.- Parameters:
source- the source nodedestination- the destination node- Returns:
- the edge connecting
sourcetodestination, ornull
-
getIngoingEdges
public final 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 final 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 final java.util.Collection<E> getEdges()
Yields the set of edges of this matrix.- Returns:
- the collection of edges
-
followersOf
public final 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 final 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
-
simplify
public void simplify(java.lang.Iterable<N> targets, java.util.Collection<N> entrypoints, java.util.Collection<E> removedEdges, java.util.Map<org.apache.commons.lang3.tuple.Pair<E,E>,E> replacedEdges)
Simplifies this matrix, removing all the given nodes and rewriting the edge set accordingly. This method will throw anUnsupportedOperationExceptionif one of the nodes being simplified has an outgoing edge that is not simplifiable, according toEdge.canBeSimplified().- Parameters:
targets- the set of theNodes that needs to be simplifiedentrypoints- the collection ofNodes that are considered as entrypoints of the graph built over this adjacency matrixremovedEdges- the collections of edges that got removed during the simplification, filled by this method (the collection will be cleared before simplifying)replacedEdges- the map of edges that got replaced during the simplification, filled by this method (the map will be cleared before simplifying); each entry refers to a single simplified edge, and is in the form<<ingoing removed, outgoing removed>, added>- Throws:
java.lang.UnsupportedOperationException- if there exists at least one node being simplified with an outgoing non-simplifiable edge
-
containsNode
public boolean containsNode(N node)
Yieldstrueif the given node is contained in this matrix.- Parameters:
node- the node to check- Returns:
trueif the node is in this matrix
-
containsEdge
public boolean containsEdge(E edge)
Yieldstrueif the given edge is contained in this matrix.- Parameters:
edge- the edge to check- Returns:
trueif the edge is in this matrix
-
iterator
public java.util.Iterator<java.util.Map.Entry<N,AdjacencyMatrix.NodeEdges<N,E,G>>> iterator()
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
removeFrom
public void removeFrom(N root)
Removes every node and edge that is reachable from the givenroot.- Parameters:
root- the node to use as root for the removal
-
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 thanInteger.MAX_VALUE,Integer.MAX_VALUEis returned.- Parameters:
from- the starting nodeto- the destination node- Returns:
- the minimum distance, in terms of number of edges to traverse, between the given nodes
-
mergeWith
public void mergeWith(AdjacencyMatrix<N,E,G> other)
Merges this matrix with the given one, by adding all nodes and all edges contained inother.- 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:- Parameters:
entrypoints- the collection ofNodes that are considered as entrypoints of the graph built over this adjacency matrix- Throws:
ProgramValidationException- if one of the aforementioned checks fail
-
-