Init
- init with the first interface and tests - added some basic class for the tests
This commit is contained in:
463
src/berack96/sim/util/graph/Graph.java
Normal file
463
src/berack96/sim/util/graph/Graph.java
Normal file
@@ -0,0 +1,463 @@
|
||||
package berack96.sim.util.graph;
|
||||
|
||||
import berack96.sim.util.graph.visit.VisitStrategy;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* An interface for the graphs.<br>
|
||||
* This interface is used for the graphs with Directed edges.<br>
|
||||
* A directed edge between V1 and V2 is an edge that has V1 as source and V2 as destination.<br>
|
||||
*
|
||||
* @param <V> The Object that represent a vertex
|
||||
* @param <W> The Object that represent the edge (more specifically the weight of the edge)
|
||||
* @author Berack96
|
||||
*/
|
||||
public interface Graph<V, W extends Number> extends Iterable<V> {
|
||||
|
||||
String NOT_DAG = "The graph is not a DAG";
|
||||
String NOT_CONNECTED = "The source vertex doesn't have a path that reach the destination";
|
||||
String PARAM_NULL = "The parameter must not be null";
|
||||
String VERTEX_NOT_CONTAINED = "The vertex must be contained in the graph";
|
||||
|
||||
/**
|
||||
* Tells if the graph has some cycle.<br>
|
||||
* A cycle is detected if visiting the graph G starting from V1 (that is any of the vertex of G),
|
||||
* the visit can return to V1 in any point.
|
||||
*
|
||||
* @return true if has cycle, false otherwise
|
||||
*/
|
||||
boolean isCyclic();
|
||||
|
||||
/**
|
||||
* Tells if the graph has the property of DAG (Directed Acyclic Graph).<br>
|
||||
* A graph is a DAG only if absent of any cycle. ( see {@link #isCyclic()} )
|
||||
*
|
||||
* @return true if is a DAG, false otherwise
|
||||
*/
|
||||
boolean isDAG();
|
||||
|
||||
/**
|
||||
* Add the vertex to the graph. If it's already in the graph it will be replaced.<br>
|
||||
* Of course the vertex added will have no edge to any other vertex nor form any other vertex.
|
||||
*
|
||||
* @param vertex the vertex to add
|
||||
* @throws NullPointerException if the vertex is null
|
||||
*/
|
||||
void addVertex(V vertex) throws NullPointerException;
|
||||
|
||||
/**
|
||||
* Add the specified vertex to the graph only if the graph doesn't contains it.<br>
|
||||
* The graph contains a vertex only if the method {@link #contains(V)} returns true.
|
||||
*
|
||||
* @param vertex the vertex to add
|
||||
* @return true if the vertex is added, false if the graph contains the vertex and therefore the new one is not added
|
||||
* @throws NullPointerException if the vertex is null
|
||||
*/
|
||||
boolean addVertexIfAbsent(V vertex) throws NullPointerException;
|
||||
|
||||
/**
|
||||
* Add all the vertices contained in the set to the graph.<br>
|
||||
* If a vertex is contained in the set and in the graph is ignored and it will not be replaced.<br>
|
||||
* Null vertices will be ignored and they will not be added to the graph.
|
||||
*
|
||||
* @param vertices a set containing the vertices
|
||||
* @throws NullPointerException if the set is null
|
||||
*/
|
||||
void addAllVertices(Set<V> vertices) throws NullPointerException;
|
||||
|
||||
/**
|
||||
* Remove the selected vertex from the graph.<br>
|
||||
* After this method's call the vertex will be no longer present in the graph, and nether all his edges.
|
||||
*
|
||||
* @param vertex the vertex to remove
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is not contained in the graph
|
||||
*/
|
||||
void removeVertex(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Remove all the vertex contained in the graph.<br>
|
||||
* After this method's call the graph will be empty; no vertices nor edges.
|
||||
*/
|
||||
void removeAllVertex();
|
||||
|
||||
/**
|
||||
* Check if the vertex passed is contained in the graph or not.<br>
|
||||
* The vertex V1 is contained in the graph G, if and only if:<br>
|
||||
* exist V2 in G such that V2.equals(V1)
|
||||
*
|
||||
* @param vertex the vertex to check
|
||||
* @return true if the vertex is contained, false otherwise
|
||||
* @throws NullPointerException if the vertex is null
|
||||
*/
|
||||
boolean contains(V vertex) throws NullPointerException;
|
||||
|
||||
/**
|
||||
* Add an edge between the two vertex.<br>
|
||||
* The edge will be created from the vertex V1 and the vertex V2<br>
|
||||
* This method will overwrite any existing edge between the two vertex.<br>
|
||||
* If there was a previous edge then it is returned
|
||||
*
|
||||
* @param vertex1 a vertex of the graph
|
||||
* @param vertex2 a vertex of the graph
|
||||
* @param weight the weight of the edge
|
||||
* @return null or the previous value of the edge if there was already one
|
||||
* @throws NullPointerException if one of the parameter is null
|
||||
* @throws IllegalArgumentException if one of the vertex is not contained in the graph
|
||||
*/
|
||||
W addEdge(V vertex1, V vertex2, W weight) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* This particular function add an edge to the graph.<br>
|
||||
* If one of the two, or both vertices aren't contained in the graph, then the vertices will be added.<br>
|
||||
* The edge will be created from the vertex V1 and the vertex V2<br>
|
||||
* This method will overwrite any existing edge between the two vertex.<br>
|
||||
* If there was a previous edge then it is returned
|
||||
*
|
||||
* @param vertex1 a vertex of the graph
|
||||
* @param vertex2 a vertex of the graph
|
||||
* @param weight the weight of the edge
|
||||
* @return null or the previous value of the edge if there was already one
|
||||
* @throws NullPointerException if one of the parameter is null
|
||||
*/
|
||||
W addEdgeAndVertices(V vertex1, V vertex2, W weight) throws NullPointerException;
|
||||
|
||||
/**
|
||||
* Add all the edges of the set in the graph.<br>
|
||||
* If one of the two, or both vertices aren't contained in the graph, then the vertices will be added.<br>
|
||||
* Any null edges will be ignored.<br>
|
||||
* This method will overwrite any existing edge between the two vertex.
|
||||
*
|
||||
* @param edges the edges to add
|
||||
* @throws NullPointerException if the set is null
|
||||
*/
|
||||
void addAllEdges(Set<Edge<V, W>> edges) throws NullPointerException;
|
||||
|
||||
/**
|
||||
* Get the weight of the selected edge.<br>
|
||||
* If the edge doesn't exist, then null is returned
|
||||
*
|
||||
* @param vertex1 a vertex of the graph
|
||||
* @param vertex2 a vertex of the graph
|
||||
* @return the weight previously set, or null if the edge doesn't exist
|
||||
* @throws NullPointerException if one of the parameters is null
|
||||
* @throws IllegalArgumentException if one of the vertex is not contained in the graph
|
||||
*/
|
||||
W getWeight(V vertex1, V vertex2) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Remove the edge between the two vertex.<br>
|
||||
* If the edge doesn't exist, then this call does nothing.<br>
|
||||
* After this method's call it will be no longer possible to travel from V1 to V2, nether from V2 to V1.
|
||||
*
|
||||
* @param vertex1 a vertex of the graph
|
||||
* @param vertex2 a vertex of the graph
|
||||
* @throws NullPointerException if one of the parameters is null
|
||||
* @throws IllegalArgumentException if one of the vertex is not contained in the graph
|
||||
*/
|
||||
void removeEdge(V vertex1, V vertex2) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Remove all the edges that goes in the vertex.<br>
|
||||
* After this method's call it will be no longer possible travel to this vertex.
|
||||
*
|
||||
* @param vertex a vertex of the graph
|
||||
* @throws NullPointerException if one of the parameters is null
|
||||
* @throws IllegalArgumentException if one of the vertex is not contained in the graph
|
||||
*/
|
||||
void removeAllInEdge(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Remove all the edges that start from this vertex.<br>
|
||||
* After this method's call it will be no longer possible travel to any vertex from this one.
|
||||
*
|
||||
* @param vertex a vertex of the graph
|
||||
* @throws NullPointerException if one of the parameters is null
|
||||
* @throws IllegalArgumentException if one of the vertex is not contained in the graph
|
||||
*/
|
||||
void removeAllOutEdge(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Remove all edges form a particular vertex of the graph.<br>
|
||||
* After this method's call the selected vertex will have 0 edges.<br>
|
||||
* It will be no longer possible to reach this vertex from any other vertex, and vice versa.
|
||||
*
|
||||
* @param vertex a vertex of the graph
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is not contained in the graph
|
||||
*/
|
||||
void removeAllEdge(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Remove all the edges of the graph.<br>
|
||||
* After this method's call the graph will have only vertices, and no edge.
|
||||
*/
|
||||
void removeAllEdge();
|
||||
|
||||
/**
|
||||
* Check if the edge between the two vertex passed is contained in the graph or not.<br>
|
||||
* An edge between V1 and V2 is contained in the graph if and only if i can travel from V1 to V2.
|
||||
*
|
||||
* @param vertex1 a vertex of the graph
|
||||
* @param vertex2 a vertex of the graph
|
||||
* @return true if the edge is contained, false otherwise
|
||||
* @throws NullPointerException if one of the parameters is null
|
||||
* @throws IllegalArgumentException if one of the vertex is not contained in the graph
|
||||
*/
|
||||
boolean containsEdge(V vertex1, V vertex2) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Get all the vertices in the graph.<br>
|
||||
* If the graph doesn't contains vertices, it'll return an empty set.<br>
|
||||
* Note that this set is completely different than the set used for the vertices, so any modification of this set will not change the graph.
|
||||
*
|
||||
* @return a set that include all the vertices
|
||||
*/
|
||||
Set<V> vertices();
|
||||
|
||||
/**
|
||||
* Get all the edges in the graph.<br>
|
||||
* If the graph doesn't contains edges, it'll return an empty set.<br>
|
||||
* Note that this set is completely different than the set used for the edges, so any modification of this set will not change the graph.
|
||||
*
|
||||
* @return a set that include all the edges
|
||||
*/
|
||||
Set<Edge<V, W>> edges();
|
||||
|
||||
/**
|
||||
* Get all the vertices that are children of the vertex passed as parameter.<br>
|
||||
* The vertices V(0-N) that are 'children' of a vertex V1, are all the vertices that have an edge
|
||||
* where V1 is the source of that edge.
|
||||
*
|
||||
* @param vertex the source vertex
|
||||
* @return a set of vertices
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is not contained in the graph
|
||||
*/
|
||||
Set<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* This method will get all the child of the vertex selected.<br>
|
||||
* The map created will be a {@link java.util.LinkedHashMap LinkedHashMap}<br>
|
||||
* The order in which the vertex are iterated in the map will be from the vertex with the lowest weight to the one with the highest.
|
||||
*
|
||||
* @param vertex a vertex of the graph
|
||||
* @return a map of all the child and their respective weight
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is not contained in the graph
|
||||
*/
|
||||
Map<V, W> getChildrenAndWeight(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Get all the vertices that have the vertex passed as their child.<br>
|
||||
* Basically is the opposite of {@link #getChildren(Object)}
|
||||
*
|
||||
* @param vertex a vertex of the graph
|
||||
* @return a set of ancestors of the vertex
|
||||
* @throws NullPointerException if one of the parameters is null
|
||||
* @throws IllegalArgumentException if one of the vertex is not contained in the graph
|
||||
*/
|
||||
Set<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tells the degree of all the edges that goes to this vertex.<br>
|
||||
* Basically, it'll count how many edge towards himself it have.
|
||||
*
|
||||
* @param vertex a vertex of the graph
|
||||
* @return the in degree of the vertex
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is not contained in the graph
|
||||
*/
|
||||
int degreeIn(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tells the degree of all the edges that goes form this vertex to others.<br>
|
||||
* Basically, it'll count how many edge towards any other vertex it have.
|
||||
*
|
||||
* @param vertex a vertex of the graph
|
||||
* @return the out degree of the vertex
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is not contained in the graph
|
||||
*/
|
||||
int degreeOut(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tells the degree of a vertex.<br>
|
||||
* The degree of a vertex is the quantity of edges that have.<br>
|
||||
* Basically, it'll count how many edge it have.
|
||||
*
|
||||
* @param vertex a vertex of the graph
|
||||
* @return the degree of the vertex
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is not contained in the graph
|
||||
*/
|
||||
int degree(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tells how many vertices are in the graph.
|
||||
*
|
||||
* @return the number of vertices
|
||||
*/
|
||||
int numberOfVertices();
|
||||
|
||||
/**
|
||||
* Tells how many edges are in the graph.
|
||||
*
|
||||
* @return the number of edges
|
||||
*/
|
||||
int numberOfEdges();
|
||||
|
||||
/**
|
||||
* Visit the graph accordingly to the strategy that is passed.<br>
|
||||
* This method visit the graph from the source to all the vertex that are reachable form the source.<br>
|
||||
* Some strategy can accept a source vertex null, because they visit all the graph anyway.
|
||||
*
|
||||
* @param source the source vertex of the visit
|
||||
* @param strategy the algorithm for visiting the graph
|
||||
* @param visit the function to apply at each vertex
|
||||
* @throws NullPointerException if one of the parameter is null (except the consumer)
|
||||
* @throws IllegalArgumentException if the vertex is not in the graph
|
||||
*/
|
||||
void visit(V source, VisitStrategy<V, W> strategy, Consumer<V> visit) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* This method will create a new Graph that is the transposed version of the original.<br>
|
||||
* At the end of this method the new graph will have all the edges inverted in orientation.<br>
|
||||
* Example: if the graph G contains (V1, V2, V3) as vertex, and (V1->V2, V3->V2) as edges, the transpose graph G' will contain (V1, V2, V3) as vertex, and (V2->V1, V2->V3) as edges.
|
||||
*
|
||||
* @return a transposed graph of this instance
|
||||
*/
|
||||
Graph<V, W> transpose();
|
||||
|
||||
/**
|
||||
* If the current graph is a DAG, it returns a topological sort of this graph.<br>
|
||||
* A topological ordering of a graph is a linear ordering of its vertices such that for every directed edge (V1, V2) from vertex V1 to vertex V2, V2 comes before V1 in the ordering.
|
||||
*
|
||||
* @return an array containing the topological order of the vertices
|
||||
* @throws UnsupportedOperationException if the graph is not a DAG (see {@link #isDAG()})
|
||||
*/
|
||||
List<V> topologicalSort() throws UnsupportedOperationException;
|
||||
|
||||
/**
|
||||
* The strongly connected components or diconnected components of an arbitrary directed graph form a partition into subgraphs that are themselves strongly connected.
|
||||
*
|
||||
* @return a set containing the strongly connected components
|
||||
*/
|
||||
Set<Set<V>> stronglyConnectedComponents();
|
||||
|
||||
/**
|
||||
* Get a sub-graph of the current one based on the maximum depth that is given.<br>
|
||||
* If the depth is 1 then only the source and it's children will be in the sub-graph.<br>
|
||||
* If the depth is 2 then only the source, it's children and it's children of it's children will be in the sub-graph.<br>
|
||||
* And so on.<br>
|
||||
* Of course the sub-graph will contain the edges that link the vertices, but only the one selected.
|
||||
*
|
||||
* @param source the source vertex
|
||||
* @param depth the maximum depth (must be a positive number, if >=0 a graph containing only the source is returned)
|
||||
* @return a sub-graph of the original
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is null
|
||||
*/
|
||||
Graph<V, W> subGraph(V source, int depth) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Get the minimum path from the source vertex to the destination vertex.<br>
|
||||
* If the source vertex can't reach the destination, then an exception is thrown.
|
||||
*
|
||||
* @param source the vertex where to start
|
||||
* @param destination the destination chosen
|
||||
* @return an ordered list of edges from source to destination that represent the minimum path between the two vertices
|
||||
* @throws NullPointerException if one of the parameter is null (except the consumer)
|
||||
* @throws IllegalArgumentException if the vertex is not in the graph
|
||||
* @throws UnsupportedOperationException if from the source it's not possible to reach the destination
|
||||
*/
|
||||
List<Edge<V, W>> distance(V source, V destination) throws NullPointerException, IllegalArgumentException, UnsupportedOperationException;
|
||||
|
||||
/**
|
||||
* Get the minimum path from the source vertex to all the possible reachable vertices.
|
||||
*
|
||||
* @param source the vertex where to start
|
||||
* @return a map containing all the possible reachable vertices from the source and the minimum path to reach them
|
||||
* @throws NullPointerException if one of the parameter is null (except the consumer)
|
||||
* @throws IllegalArgumentException if the vertex is not in the graph
|
||||
*/
|
||||
Map<V, List<Edge<V, W>>> distance(V source) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
// TODO maybe -> STATIC saveOnFile(orString) INSTANCE loadFromFile(orString), but need JSON parser
|
||||
// TODO maybe, but i don't think so... STATIC DISTANCE V* -> V*
|
||||
|
||||
/**
|
||||
* Class used for retrieving the edges of the graph.
|
||||
*
|
||||
* @param <V> the vertices
|
||||
* @param <W> the weight of the edge
|
||||
*/
|
||||
class Edge<V, W extends Number> {
|
||||
private final V source;
|
||||
private final V destination;
|
||||
private final W weight;
|
||||
|
||||
/**
|
||||
* Create an final version of this object
|
||||
*
|
||||
* @param source the source of the edge
|
||||
* @param destination the destination of the edge
|
||||
* @param weight the weight od the edge
|
||||
*/
|
||||
public Edge(V source, V destination, W weight) {
|
||||
this.source = source;
|
||||
this.destination = destination;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* The vertex where the edge goes
|
||||
*
|
||||
* @return the vertex
|
||||
*/
|
||||
public V getDestination() {
|
||||
return destination;
|
||||
}
|
||||
|
||||
/**
|
||||
* The vertex where the edge starts from
|
||||
*
|
||||
* @return the vertex
|
||||
*/
|
||||
public V getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
/**
|
||||
* The weight of the edge
|
||||
*
|
||||
* @return the weight
|
||||
*/
|
||||
public W getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[" + source + " -> " + destination + ", " + weight + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return toString().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
try {
|
||||
return obj.getClass().equals(getClass()) && obj.toString().equals(toString());
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
52
src/berack96/sim/util/graph/visit/BFS.java
Normal file
52
src/berack96/sim/util/graph/visit/BFS.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package berack96.sim.util.graph.visit;
|
||||
|
||||
import berack96.sim.util.graph.Graph;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Breadth-first search<br>
|
||||
* The algorithm starts at the root node and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.
|
||||
*
|
||||
* @param <V> the vertex of the graph
|
||||
* @param <W> the weight of the graph
|
||||
*/
|
||||
public class BFS<V, W extends Number> implements VisitStrategy<V, W> {
|
||||
|
||||
private VisitInfo<V> lastVisit = null;
|
||||
|
||||
/**
|
||||
* Retrieve the info of the last visit
|
||||
*
|
||||
* @return an info of the visit
|
||||
*/
|
||||
public VisitInfo<V> getLastVisit() {
|
||||
return lastVisit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Graph<V, W> graph, V source, Consumer<V> visit) throws NullPointerException, IllegalArgumentException {
|
||||
lastVisit = new VisitInfo<>(source);
|
||||
final LinkedList<V> toVisitChildren = new LinkedList<>();
|
||||
|
||||
toVisitChildren.push(source);
|
||||
if (visit != null)
|
||||
visit.accept(source);
|
||||
lastVisit.setVisited(source);
|
||||
|
||||
while (!toVisitChildren.isEmpty()) {
|
||||
V current = toVisitChildren.removeFirst();
|
||||
|
||||
for (V child : graph.getChildren(current))
|
||||
if (!lastVisit.isDiscovered(child)) {
|
||||
toVisitChildren.addLast(child);
|
||||
|
||||
lastVisit.setVisited(child);
|
||||
lastVisit.setParent(current, child);
|
||||
if (visit != null)
|
||||
visit.accept(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
58
src/berack96/sim/util/graph/visit/DFS.java
Normal file
58
src/berack96/sim/util/graph/visit/DFS.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package berack96.sim.util.graph.visit;
|
||||
|
||||
import berack96.sim.util.graph.Graph;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Stack;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Depth-first search<br>
|
||||
* The algorithm starts at the root node and explores as far as possible along each branch before backtracking.
|
||||
*
|
||||
* @param <V> the vertex of the graph
|
||||
* @param <W> the weight of the graph
|
||||
*/
|
||||
public class DFS<V, W extends Number> implements VisitStrategy<V, W> {
|
||||
|
||||
private VisitInfo<V> lastVisit = null;
|
||||
|
||||
/**
|
||||
* Retrieve the info of the last visit
|
||||
*
|
||||
* @return an info of the visit
|
||||
*/
|
||||
public VisitInfo<V> getLastVisit() {
|
||||
return lastVisit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Graph<V, W> graph, V source, Consumer<V> visit) throws NullPointerException, IllegalArgumentException {
|
||||
lastVisit = new VisitInfo<>(source);
|
||||
final Stack<V> toVisit = new Stack<>();
|
||||
|
||||
toVisit.push(source);
|
||||
|
||||
while (!toVisit.isEmpty()) {
|
||||
V current = toVisit.peek();
|
||||
boolean hasChildToVisit = false;
|
||||
Iterator<V> iter = graph.getChildren(current).iterator();
|
||||
|
||||
while (iter.hasNext() && !hasChildToVisit) {
|
||||
V child = iter.next();
|
||||
if (!lastVisit.isDiscovered(child)) {
|
||||
hasChildToVisit = true;
|
||||
toVisit.push(child);
|
||||
lastVisit.setParent(current, child);
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasChildToVisit) {
|
||||
toVisit.pop();
|
||||
lastVisit.setVisited(current);
|
||||
if (visit != null)
|
||||
visit.accept(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
193
src/berack96/sim/util/graph/visit/VisitStrategy.java
Normal file
193
src/berack96/sim/util/graph/visit/VisitStrategy.java
Normal file
@@ -0,0 +1,193 @@
|
||||
package berack96.sim.util.graph.visit;
|
||||
|
||||
import berack96.sim.util.graph.Graph;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* This class is used for define some strategy for the visit of a graph.
|
||||
*
|
||||
* @param <V> The Object that represent a vertex
|
||||
* @param <W> The Object that represent the edge (more specifically the weight of the edge)
|
||||
* @author Berack96
|
||||
*/
|
||||
public interface VisitStrategy<V, W extends Number> {
|
||||
|
||||
/**
|
||||
* With this the graph will be visited accordingly to the strategy of the visit.<br>
|
||||
* Some strategy can accept a source vertex null, because they visit all the graph anyway.<br>
|
||||
* If you want to stop the visit of the graph, you just have to throw any exception in the visit function, but be sure to catch it
|
||||
*
|
||||
* @param graph the graph to visit
|
||||
* @param source the source of the visit
|
||||
* @param visit the function to apply at each vertex when they are visited
|
||||
* @throws NullPointerException if one of the arguments is null (only the consumers can be null)
|
||||
* @throws IllegalArgumentException if the source vertex is not in the graph
|
||||
* @throws UnsupportedOperationException in the case that the visit algorithm cannot be applied to the graph
|
||||
*/
|
||||
void visit(Graph<V, W> graph, V source, Consumer<V> visit) throws NullPointerException, IllegalArgumentException, UnsupportedOperationException;
|
||||
|
||||
/**
|
||||
* The class used for getting the info of the visit.<br>
|
||||
* It could be used with the algorithm of the visit for set some useful data.
|
||||
*
|
||||
* @param <V> the vertex of the visit
|
||||
* @author Berack96
|
||||
*/
|
||||
class VisitInfo<V> {
|
||||
private final Map<V, Long> discovered;
|
||||
private final Map<V, Long> visited;
|
||||
private final Map<V, V> parent;
|
||||
private final V source;
|
||||
private long time;
|
||||
|
||||
/**
|
||||
* Need a source for initialize the basic values
|
||||
*
|
||||
* @param source the source of the visit
|
||||
* @throws NullPointerException if the source is null
|
||||
*/
|
||||
public VisitInfo(V source) {
|
||||
if (source == null)
|
||||
throw new NullPointerException();
|
||||
|
||||
discovered = new Hashtable<>();
|
||||
visited = new Hashtable<>();
|
||||
parent = new Hashtable<>();
|
||||
|
||||
this.time = 0;
|
||||
this.source = source;
|
||||
setDiscovered(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* The time of the vertex when it is discovered in the visit.<br>
|
||||
* For "discovered" i mean when the node is first found by the visit algorithm. It may depends form {@link VisitStrategy}<br>
|
||||
* The time starts at 0 and for each vertex discovered it is increased by one. If a vertex is visited it also increase the time<br>
|
||||
*
|
||||
* @param vertex the vertex needed
|
||||
* @return the time of it's discovery
|
||||
* @throws IllegalArgumentException if the vertex is not discovered
|
||||
* @throws NullPointerException if the vertex is null
|
||||
*/
|
||||
public long getTimeDiscover(V vertex) throws IllegalArgumentException, NullPointerException {
|
||||
Long time = discovered.get(vertex);
|
||||
if (time == null)
|
||||
throw new IllegalArgumentException();
|
||||
return time;
|
||||
}
|
||||
|
||||
/**
|
||||
* The time when the vertex is visited by the algorithm<br>
|
||||
* For "visited" i mean when the node is finally visited by the visit algorithm. It may depends form {@link VisitStrategy}<br>
|
||||
* The time starts at 0 and for each vertex discovered or visited is increased by one<br>
|
||||
*
|
||||
* @param vertex the vertex needed
|
||||
* @return the time of it's visit
|
||||
* @throws IllegalArgumentException if the vertex is not visited
|
||||
* @throws NullPointerException if the vertex is null
|
||||
*/
|
||||
public long getTimeVisit(V vertex) throws IllegalArgumentException, NullPointerException {
|
||||
Long time = visited.get(vertex);
|
||||
if (time == null)
|
||||
throw new IllegalArgumentException();
|
||||
return time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells if a vertex is discovered or not
|
||||
*
|
||||
* @param vertex the vertex chosen
|
||||
* @return true if is discovered
|
||||
*/
|
||||
public boolean isDiscovered(V vertex) throws NullPointerException {
|
||||
try {
|
||||
return discovered.containsKey(vertex);
|
||||
} catch (NullPointerException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells if the vertex is visited or not
|
||||
*
|
||||
* @param vertex the vertex chosen
|
||||
* @return true if is visited
|
||||
*/
|
||||
public boolean isVisited(V vertex) throws NullPointerException {
|
||||
try {
|
||||
return visited.containsKey(vertex);
|
||||
} catch (NullPointerException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a vertex as "visited". After this call the vertex is set as discovered (if not already) and visited.<br>
|
||||
* Next this call it will be possible to get the time of visit of that vertex<br>
|
||||
* Does nothing if the vertex is already been visited.
|
||||
*
|
||||
* @param vertex the vertex that has been visited
|
||||
*/
|
||||
public synchronized void setVisited(V vertex) {
|
||||
setDiscovered(vertex);
|
||||
if (!visited.containsKey(vertex))
|
||||
visited.put(vertex, time++);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a vertex as "discovered". After this call the vertex is set as discovered and it will be possible to get the time of it's discovery<br>
|
||||
* Does nothing if the vertex is already been discovered.
|
||||
*
|
||||
* @param vertex the vertex that has been discovered
|
||||
*/
|
||||
public synchronized void setDiscovered(V vertex) {
|
||||
if (!discovered.containsKey(vertex))
|
||||
discovered.put(vertex, time++);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the parent of a particular vertex<br>
|
||||
* The parent of a vertex is the one that has discovered it<br>
|
||||
* If the target vertex is not already discovered, then {@link #setDiscovered(Object)} is called<br>
|
||||
*
|
||||
* @param parent the vertex that is the parent
|
||||
* @param child the vertex discovered
|
||||
* @throws IllegalArgumentException if the parent is not already discovered
|
||||
*/
|
||||
public synchronized void setParent(V parent, V child) throws IllegalArgumentException {
|
||||
if (!isDiscovered(parent))
|
||||
throw new IllegalArgumentException(parent.toString());
|
||||
|
||||
setDiscovered(child);
|
||||
this.parent.putIfAbsent(child, parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the source of the visit.
|
||||
*
|
||||
* @return the source vertex where it's started the visit
|
||||
*/
|
||||
public V getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the parent of a particular vertex.<br>
|
||||
* The parent of a vertex is the one that has discovered it<br>
|
||||
* If the vertex has no parent (it has not been set by the visit algorithm or it's the source) then null is returned.
|
||||
*
|
||||
* @param vertex the child vertex
|
||||
* @return the parent of the child
|
||||
* @throws IllegalArgumentException if the vertex has not been discovered yet
|
||||
*/
|
||||
public V getParentOf(V vertex) throws IllegalArgumentException {
|
||||
if (isDiscovered(vertex))
|
||||
return parent.get(vertex);
|
||||
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user