Class AdjacencyMatrix<G extends BaseGraph<G,N,E>,N extends Node<G,N,E>,E extends Edge<G,N,E>>
- java.lang.Object
-
- it.unive.lisa.util.datastructures.graph.AdjacencyMatrix<G,N,E>
-
- Type Parameters:
G- the type of theBaseGraphs this matrix can be used inN- the type of theNodes in this matrixE- the type of theEdges 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 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<G,N,E> other)Copies the given matrix by 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.voidclear()Empties this matrix, removing all the nodes and the edges in it.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<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.inthashCode()java.util.Iterator<java.util.Map.Entry<N,AdjacencyMatrix.NodeEdges<G,N,E>>>iterator()voidmergeWith(AdjacencyMatrix<G,N,E> 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.voidremoveNode(N node)Removes the given node from the matrix, together with all its connected edges.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<G,N,E> other)
Copies the given matrix by 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 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. 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
-
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 nodedestination- the destination node- Returns:
- the edges connecting
sourcetodestination
-
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)
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<G,N,E>>> 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
-
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<G,N,E> 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
-
clear
public void clear()
Empties this matrix, removing all the nodes and the edges in it.
-
-