Refactoring
- changed to maven - modified hashMap and HashSet to Tree - modified files path
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -125,6 +125,7 @@ local.properties
|
||||
|
||||
|
||||
# mine
|
||||
target/
|
||||
.idea/*
|
||||
.project
|
||||
*.iml
|
||||
|
||||
32
build.gradle
32
build.gradle
@@ -1,32 +0,0 @@
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'eclipse'
|
||||
|
||||
version='1.0-SNAPSHOT'
|
||||
|
||||
jar {
|
||||
manifest {
|
||||
attributes 'Main-Class': 'berack96.lib.graph.view.Main'
|
||||
}
|
||||
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
sourceSets.main.java {
|
||||
srcDirs "src"
|
||||
srcDirs "test"
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.junit.jupiter:junit-jupiter:5.5.2'
|
||||
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
|
||||
|
||||
/*compile group: 'commons-collections', name: 'commons-collections', version: '3.2'*/
|
||||
testCompile 'junit:junit:5.5.2'
|
||||
}
|
||||
80
pom.xml
Normal file
80
pom.xml
Normal file
@@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>berack96</groupId>
|
||||
<artifactId>Graph</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.9.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>5.9.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.9.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<configuration>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>berack96.lib.graph.view.Main</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -18,18 +18,46 @@ import java.util.function.Consumer;
|
||||
*/
|
||||
public abstract class Graph<V> implements Iterable<V> {
|
||||
|
||||
//------------------- STAIC -----------------
|
||||
|
||||
public static final int NO_EDGE = 0;
|
||||
public final static String NOT_CONNECTED = "The source vertex doesn't have a path that reach the destination";
|
||||
public final static String PARAM_NULL = "The parameter must not be null";
|
||||
public final static String VERTEX_NOT_CONTAINED = "The vertex must be contained in the graph";
|
||||
|
||||
/**
|
||||
* Map that contains the vertex as key and a set of all the marker associated with it.
|
||||
* Create the default map. All operations are O(log(n))<br>
|
||||
* It returns a TreeMap with a ObjectComparator as comparator.<br>
|
||||
* This way all the graphs will use the same maps.<br>
|
||||
* It is not required to use this method, but it is highly recommended.<br>
|
||||
*
|
||||
* @return A newly created TreeMap instance with ObjectsComparator as comparator
|
||||
*/
|
||||
private final Map<V, Set<Object>> markers = new HashMap<>();
|
||||
public final static <X, Y> Map<X, Y> getDefaultMap() {
|
||||
return new TreeMap<X, Y>(ObjectsComparator.instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new instance of this Graph.
|
||||
* Create the default set. All operations are O(log(n))<br>
|
||||
* It returns a TreeSet with a ObjectComparator as comparator.<br>
|
||||
* This way all the graphs will use the same sets.<br>
|
||||
* It is not required to use this method, but it is highly recommended.<br>
|
||||
*
|
||||
* @return A newly created TreeSet instance with ObjectsComparator as comparator
|
||||
*/
|
||||
public final static <X> Set<X> getDefaultSet() {
|
||||
return new TreeSet<X>(ObjectsComparator.instance);
|
||||
}
|
||||
|
||||
//------------------- INSTANCE -----------------
|
||||
|
||||
/**
|
||||
* Map that contains the vertex as key and a set of all the marker associated with it.
|
||||
*/
|
||||
private final Map<V, Set<Object>> markers = getDefaultMap();
|
||||
|
||||
/**
|
||||
* Get a new instance of this graph.
|
||||
*
|
||||
* @return A new instance of the graph
|
||||
*/
|
||||
@@ -90,7 +118,7 @@ public abstract class Graph<V> implements Iterable<V> {
|
||||
* @param vertices a collection of the vertices to add
|
||||
* @throws NullPointerException if the set is null
|
||||
*/
|
||||
public void addAll(@SuppressWarnings("ConstantConditions") Collection<V> vertices) throws NullPointerException {
|
||||
public void addAll(Collection<V> vertices) throws NullPointerException {
|
||||
check(vertices);
|
||||
for (V vertex : vertices)
|
||||
addIfAbsent(vertex);
|
||||
@@ -283,7 +311,7 @@ public abstract class Graph<V> implements Iterable<V> {
|
||||
* After this method's call the graph will have only vertices, and no edge.
|
||||
*/
|
||||
public void removeAllEdge() {
|
||||
Collection<V> vertices = vertices();
|
||||
Set<V> vertices = vertices();
|
||||
removeAll();
|
||||
addAll(vertices);
|
||||
}
|
||||
@@ -292,11 +320,11 @@ public abstract class Graph<V> implements Iterable<V> {
|
||||
* Retrieve all the edges of a particular vertex.<br>
|
||||
*
|
||||
* @param vertex a vertex of the graph
|
||||
* @return a collection of edges
|
||||
* @return a set of edges
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is not contained in the graph
|
||||
*/
|
||||
public abstract Collection<Edge<V>> edgesOf(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
public abstract Set<Edge<V>> edgesOf(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Get all the vertices that are children of the vertex passed as parameter.<br>
|
||||
@@ -304,33 +332,33 @@ public abstract class Graph<V> implements Iterable<V> {
|
||||
* where V1 is the source of that edge.
|
||||
*
|
||||
* @param vertex the source vertex
|
||||
* @return an array of vertices
|
||||
* @return a set of vertices
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is not contained in the graph
|
||||
*/
|
||||
public abstract Collection<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
public abstract Set<V> getChildren(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 an array of ancestors of the vertex
|
||||
* @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
|
||||
*/
|
||||
public abstract Collection<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
public abstract Set<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Get all the marks of this graph.<br>
|
||||
* Specifically it will return a collection of marks where every mark<br>
|
||||
* Specifically it will return a Set of marks where every mark<br>
|
||||
* as associated at least one vertex of the graph.<br>
|
||||
* If the graph doesn't have vertex marked then it is returned an empty collection.
|
||||
* If the graph doesn't have vertex marked then it is returned an empty Set.
|
||||
*
|
||||
* @return a collection of marks
|
||||
* @return a set of marks
|
||||
*/
|
||||
public final Collection<Object> marks() {
|
||||
Collection<Object> ret = new HashSet<>();
|
||||
public final Set<Object> marks() {
|
||||
Set<Object> ret = getDefaultSet();
|
||||
markers.forEach((v, set) -> ret.addAll(set));
|
||||
return ret;
|
||||
}
|
||||
@@ -348,7 +376,7 @@ public abstract class Graph<V> implements Iterable<V> {
|
||||
public final void mark(V vertex, Object mark) throws NullPointerException, IllegalArgumentException {
|
||||
check(mark);
|
||||
checkVert(vertex);
|
||||
Set<Object> marks = markers.computeIfAbsent(vertex, v -> new HashSet<>());
|
||||
Set<Object> marks = markers.computeIfAbsent(vertex, v -> getDefaultSet());
|
||||
marks.add(mark);
|
||||
}
|
||||
|
||||
@@ -392,12 +420,12 @@ public abstract class Graph<V> implements Iterable<V> {
|
||||
* If there aren't vertices with that mark then it is returned an empty set.<br>
|
||||
*
|
||||
* @param mark the mark
|
||||
* @return all the vertices that are marked with that specific mark
|
||||
* @return a set of all the vertices that are marked with that specific mark
|
||||
* @throws NullPointerException if the mark is null
|
||||
*/
|
||||
public final Collection<V> getMarkedWith(Object mark) throws NullPointerException {
|
||||
public final Set<V> getMarkedWith(Object mark) throws NullPointerException {
|
||||
check(mark);
|
||||
Collection<V> vertices = new ArrayList<>(markers.size());
|
||||
Set<V> vertices = getDefaultSet();
|
||||
markers.forEach((v, set) -> {
|
||||
if (set.contains(mark))
|
||||
vertices.add(v);
|
||||
@@ -408,16 +436,16 @@ public abstract class Graph<V> implements Iterable<V> {
|
||||
/**
|
||||
* Get all the marker of this vertex.<br>
|
||||
* If the vertex doesn't have any mark, then it will return an empty set.<br>
|
||||
* Note: modifying the returned collection affect the marker of the vertex.
|
||||
* Note: modifying the returned Set affect the marker of the vertex.
|
||||
*
|
||||
* @param vertex the vertex
|
||||
* @return all the mark to the vertex or an empty collection if none
|
||||
* @return a set of all the mark to the vertex or an empty Set if none
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is not contained in the graph
|
||||
*/
|
||||
public final Collection<Object> getMarks(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
public final Set<Object> getMarks(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
checkVert(vertex);
|
||||
return markers.getOrDefault(vertex, new HashSet<>());
|
||||
return markers.getOrDefault(vertex, getDefaultSet());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -428,7 +456,7 @@ public abstract class Graph<V> implements Iterable<V> {
|
||||
*/
|
||||
public final void unMarkAll(Object mark) throws NullPointerException {
|
||||
check(mark);
|
||||
Collection<V> toRemove = new ArrayList<>(markers.size());
|
||||
Set<V> toRemove = getDefaultSet();
|
||||
markers.forEach((v, set) -> {
|
||||
set.remove(mark);
|
||||
if (set.size() == 0)
|
||||
@@ -448,23 +476,23 @@ public abstract class Graph<V> implements Iterable<V> {
|
||||
|
||||
/**
|
||||
* Get all the vertices in the graph.<br>
|
||||
* If the graph doesn't contains vertices, it'll return an empty collection.<br>
|
||||
* If the graph doesn't contains vertices, it'll return an empty Set.<br>
|
||||
*
|
||||
* @return an array that include all the vertices
|
||||
* @return a set that include all the vertices
|
||||
*/
|
||||
public Collection<V> vertices() {
|
||||
Collection<V> collection = new ArrayList<>();
|
||||
forEach(collection::add);
|
||||
return collection;
|
||||
public Set<V> vertices() {
|
||||
Set<V> vertices = getDefaultSet();
|
||||
forEach(vertices::add);
|
||||
return vertices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the edges in the graph.<br>
|
||||
* If the graph doesn't contains edges, it'll return an empty collection.<br>
|
||||
* If the graph doesn't contains edges, it'll return an empty Set.<br>
|
||||
*
|
||||
* @return a collection that include all the edges
|
||||
* @return a Set that include all the edges
|
||||
*/
|
||||
public abstract Collection<Edge<V>> edges();
|
||||
public abstract Set<Edge<V>> edges();
|
||||
|
||||
/**
|
||||
* Tells the degree of a vertex.<br>
|
||||
@@ -523,40 +551,44 @@ public abstract class Graph<V> implements Iterable<V> {
|
||||
public final Graph<V> subGraph(V source, int depth) throws NullPointerException, IllegalArgumentException {
|
||||
checkVert(source);
|
||||
Graph<V> sub = getNewInstance();
|
||||
Set<V> vertices = new HashSet<>(this.size() + 1, 1);
|
||||
Set<V> vertices = getDefaultSet();
|
||||
new BFS<V>().setMaxDepth(Math.max(depth, 0)).visit(this, source, vertices::add);
|
||||
|
||||
sub.addAll(vertices);
|
||||
for (V src : vertices)
|
||||
for (V dest : getChildren(src))
|
||||
if (sub.contains(dest))
|
||||
sub.addEdge(new Edge<>(src, dest, this.getWeight(src, dest)));
|
||||
sub.addEdge(src, dest, this.getWeight(src, dest));
|
||||
return sub;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a sub-graph of the current one with only the vertex marked with the selected markers.<br>
|
||||
* Get a sub-graph of the current one with only the vertex marked with the selected markers. (OR set operation)<br>
|
||||
* Each vertex will have all his markers and his edges, but only the ones with the destination marked with the same marker.<br>
|
||||
* If the marker is not specified or is null then the returning graph will have all the vertices that are not marked by any marker.<br>
|
||||
* If the graph doesn't contain any vertex with that marker then an empty graph is returned.
|
||||
* If in the list of markers there is a null marker it will be skipped.<br>
|
||||
* If the graph doesn't contain any vertex with any marker passed then an empty graph is returned.
|
||||
*
|
||||
* @param marker one or more markers
|
||||
* @return a sub-graph of the current graph
|
||||
*/
|
||||
public final Graph<V> subGraph(Object... marker) {
|
||||
final Graph<V> sub = getNewInstance();
|
||||
final Set<V> allVertices = new HashSet<>();
|
||||
final Set<Object> allMarkers = new HashSet<>();
|
||||
final boolean isEmpty = (marker == null || marker.length == 0);
|
||||
final Set<V> allVertices = getDefaultSet();
|
||||
final Set<Object> allMarkers = getDefaultSet();
|
||||
|
||||
if (!isEmpty) {
|
||||
Collections.addAll(allMarkers, marker);
|
||||
if (marker != null && marker.length > 0)
|
||||
for(int i=0; i<marker.length; i++)
|
||||
if(marker[i] != null)
|
||||
allMarkers.add(marker[i]);
|
||||
|
||||
if(allMarkers.size() > 0)
|
||||
markers.forEach((v, set) -> {
|
||||
if (!Collections.disjoint(allMarkers, set))
|
||||
allVertices.add(v);
|
||||
});
|
||||
} else {
|
||||
Collection<V> toAdd = vertices();
|
||||
else {
|
||||
Set<V> toAdd = vertices();
|
||||
toAdd.removeAll(markers.keySet());
|
||||
allVertices.addAll(toAdd);
|
||||
}
|
||||
@@ -606,7 +638,6 @@ public abstract class Graph<V> implements Iterable<V> {
|
||||
return dijkstra.getLastDistance();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if the object passed is not null.
|
||||
* If it's null then throws eventual exception
|
||||
@@ -4,10 +4,8 @@ import berack96.lib.graph.visit.VisitSCC;
|
||||
import berack96.lib.graph.visit.VisitTopological;
|
||||
import berack96.lib.graph.visit.impl.Tarjan;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* This is a more specific interface for an implementation of a Undirected Graph.<br>
|
||||
@@ -70,17 +68,17 @@ public abstract class GraphDirected<V> extends Graph<V> {
|
||||
/**
|
||||
* Retrieve all the edges of a particular vertex.<br>
|
||||
* Note: the edges that are returned are the one that have this vertex as destination and another as source.<br>
|
||||
* Note2: depending on the implementation, modifying the returned collection<br>
|
||||
* Note2: depending on the implementation, modifying the returned Set<br>
|
||||
* could affect the graph behavior and the changes could be reflected to the graph.
|
||||
*
|
||||
* @param vertex a vertex of the graph
|
||||
* @return a collection of edges
|
||||
* @return a Set of edges
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is not contained in the graph
|
||||
*/
|
||||
public Collection<Edge<V>> getEdgesIn(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
Collection<V> ancestors = getAncestors(vertex);
|
||||
Collection<Edge<V>> edgesIn = new HashSet<>(ancestors.size());
|
||||
public Set<Edge<V>> getEdgesIn(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
Set<V> ancestors = getAncestors(vertex);
|
||||
Set<Edge<V>> edgesIn = getDefaultSet();
|
||||
|
||||
for (V ancestor : ancestors) {
|
||||
int weight = getWeight(ancestor, vertex);
|
||||
@@ -93,17 +91,17 @@ public abstract class GraphDirected<V> extends Graph<V> {
|
||||
/**
|
||||
* Retrieve all the edges that goes OUT of a particular vertex.<br>
|
||||
* Note: the edges that are returned are the one that have this vertex as source and another one as destination.<br>
|
||||
* Note2: depending on the implementation, modifying the returned collection<br>
|
||||
* Note2: depending on the implementation, modifying the returned Set<br>
|
||||
* could affect the graph behavior and the changes could be reflected to the graph.
|
||||
*
|
||||
* @param vertex a vertex of the graph
|
||||
* @return a collection of edges
|
||||
* @return a Set of edges
|
||||
* @throws NullPointerException if the vertex is null
|
||||
* @throws IllegalArgumentException if the vertex is not contained in the graph
|
||||
*/
|
||||
public Collection<Edge<V>> getEdgesOut(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
Collection<V> children = getChildren(vertex);
|
||||
Collection<Edge<V>> edgesOut = new HashSet<>(children.size());
|
||||
public Set<Edge<V>> getEdgesOut(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
Set<V> children = getChildren(vertex);
|
||||
Set<Edge<V>> edgesOut = getDefaultSet();
|
||||
|
||||
for (V child : children) {
|
||||
int weight = getWeight(vertex, child);
|
||||
@@ -178,26 +176,26 @@ public abstract class GraphDirected<V> extends Graph<V> {
|
||||
* The strongly connected components or disconnected components of an arbitrary directed graph
|
||||
* form a partition into subgraphs that are themselves strongly connected.
|
||||
*
|
||||
* @return a collection containing the strongly connected components
|
||||
* @return a Set containing the strongly connected components
|
||||
*/
|
||||
public final Collection<Collection<V>> stronglyConnectedComponents() {
|
||||
public final Set<Set<V>> stronglyConnectedComponents() {
|
||||
VisitSCC<V> visit = new Tarjan<>();
|
||||
visit.visit(this, null, null);
|
||||
return visit.getSCC();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Edge<V>> edgesOf(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
Collection<Edge<V>> edges = getEdgesIn(vertex);
|
||||
public Set<Edge<V>> edgesOf(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
Set<Edge<V>> edges = getEdgesIn(vertex);
|
||||
edges.addAll(getEdgesOut(vertex));
|
||||
return edges;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Edge<V>> edges() {
|
||||
Collection<Edge<V>> collection = new ArrayList<>();
|
||||
forEach(v -> collection.addAll(getEdgesIn(v)));
|
||||
return collection;
|
||||
public Set<Edge<V>> edges() {
|
||||
Set<Edge<V>> set = getDefaultSet();
|
||||
forEach(v -> set.addAll(getEdgesIn(v)));
|
||||
return set;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -3,8 +3,7 @@ package berack96.lib.graph;
|
||||
import berack96.lib.graph.visit.VisitMST;
|
||||
import berack96.lib.graph.visit.impl.Prim;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* This is a more specific interface for an implementation of a Directed Graph.<br>
|
||||
@@ -19,27 +18,27 @@ public abstract class GraphUndirected<V> extends Graph<V> {
|
||||
/**
|
||||
* The connected components of an arbitrary undirected graph form a partition into subgraphs that are themselves connected.
|
||||
*
|
||||
* @return a collection containing the strongly connected components
|
||||
* @return a Set containing the strongly connected components
|
||||
*/
|
||||
public Collection<Collection<V>> connectedComponents() {
|
||||
public Set<Set<V>> connectedComponents() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* minimum spanning forest or minimum spamming tree of the graph
|
||||
*
|
||||
* @return A collection of edges representing the M.S.F.
|
||||
* @return A Set of edges representing the M.S.F.
|
||||
*/
|
||||
public Collection<Edge<V>> minimumSpanningForest() {
|
||||
public Set<Edge<V>> minimumSpanningForest() {
|
||||
VisitMST<V> visit = new Prim<>();
|
||||
visit.visit(this, iterator().next(), null);
|
||||
return visit.getMST();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Edge<V>> edgesOf(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
public Set<Edge<V>> edgesOf(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
checkVert(vertex);
|
||||
Collection<Edge<V>> edges = new LinkedList<>();
|
||||
Set<Edge<V>> edges = getDefaultSet();
|
||||
getChildren(vertex).forEach(v -> edges.add(new Edge<>(vertex, v, getWeight(vertex, v))));
|
||||
return edges;
|
||||
}
|
||||
18
src/main/java/berack96/lib/graph/ObjectsComparator.java
Normal file
18
src/main/java/berack96/lib/graph/ObjectsComparator.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package berack96.lib.graph;
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* Compare two arbitrary objects.<br>
|
||||
* It uses the method hashCode that every object has to compare two objects.<br>
|
||||
* This is a simple use
|
||||
*/
|
||||
public class ObjectsComparator implements Comparator<Object> {
|
||||
static public final ObjectsComparator instance = new ObjectsComparator();
|
||||
|
||||
private ObjectsComparator(){};
|
||||
|
||||
@Override
|
||||
public int compare(Object o1, Object o2) {
|
||||
return o1.hashCode() - o2.hashCode();
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,8 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
*/
|
||||
public class ListGraph<V> extends GraphDirected<V> {
|
||||
|
||||
final private Map<V, List<Adj>> adj = new Hashtable<>();
|
||||
// in case of thread safety use -> Collections.synchronizedSortedMap(TreeMap)
|
||||
final private Map<V, List<Adj>> adj = getDefaultMap();
|
||||
|
||||
@Override
|
||||
public Iterator<V> iterator() {
|
||||
@@ -73,18 +74,18 @@ public class ListGraph<V> extends GraphDirected<V> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
public Set<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
checkVert(vertex);
|
||||
Collection<V> children = new HashSet<>();
|
||||
Set<V> children = getDefaultSet();
|
||||
for (Adj adj : adj.get(vertex))
|
||||
children.add(adj.vertex);
|
||||
return children;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
public Set<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
checkVert(vertex);
|
||||
Collection<V> ancestors = new HashSet<>();
|
||||
Set<V> ancestors = getDefaultSet();
|
||||
adj.forEach((v, list) -> {
|
||||
if (getAdj(list, vertex) != null)
|
||||
ancestors.add(v);
|
||||
@@ -7,11 +7,12 @@ import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* Graph that uses HashMap for vertices and edges<br>
|
||||
* Graph that uses TreeMap for vertices and edges<br>
|
||||
* More specifically it utilizes a Map containing all the vertices mapped to all their edges<br>
|
||||
* Technically this version of the graph combine the fast adding/removing of the edges of the Matrix implementation,
|
||||
* with the low memory and fast adding/removing of vertices of the Linked List implementation.<br>
|
||||
* This happen if the HashMap is not reallocated. So in the end each operation of adding or removing has O(n)
|
||||
* "Fast" in this case means O(log(n)) since it is a tree, so not tecnically as fast as O(1),
|
||||
* but better than O(n).<br>
|
||||
*
|
||||
* @param <V> the vertices
|
||||
* @author Berack96
|
||||
@@ -23,7 +24,7 @@ public class MapGraph<V> extends GraphDirected<V> {
|
||||
* The first vertex is the vertex where start the edge, the second one is where the edge goes<br>
|
||||
* If an edge exist, then it's weight is returned
|
||||
*/
|
||||
private final Map<V, Map<V, Integer>> edges = new HashMap<>();
|
||||
private final Map<V, Map<V, Integer>> edges = getDefaultMap();
|
||||
|
||||
@Override
|
||||
public Iterator<V> iterator() {
|
||||
@@ -39,7 +40,7 @@ public class MapGraph<V> extends GraphDirected<V> {
|
||||
@Override
|
||||
public void add(V vertex) {
|
||||
check(vertex);
|
||||
edges.computeIfAbsent(vertex, v -> new HashMap<>());
|
||||
edges.computeIfAbsent(vertex, v -> new TreeMap<>());
|
||||
edges.forEach((v, adj) -> adj.remove(vertex));
|
||||
edges.get(vertex).clear();
|
||||
}
|
||||
@@ -79,15 +80,15 @@ public class MapGraph<V> extends GraphDirected<V> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
public Set<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
checkVert(vertex);
|
||||
return new HashSet<>(edges.get(vertex).keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
public Set<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
checkVert(vertex);
|
||||
Collection<V> ancestors = new HashSet<>();
|
||||
Set<V> ancestors = getDefaultSet();
|
||||
edges.forEach((v, adj) -> {
|
||||
if (adj.containsKey(vertex))
|
||||
ancestors.add(v);
|
||||
@@ -13,7 +13,7 @@ import java.util.*;
|
||||
*/
|
||||
public class MatrixGraph<V> extends GraphDirected<V> {
|
||||
|
||||
private final Map<V, Integer> map = new HashMap<>();
|
||||
private final Map<V, Integer> map = getDefaultMap();
|
||||
private int[][] matrix = new int[0][0];
|
||||
|
||||
@Override
|
||||
@@ -80,10 +80,10 @@ public class MatrixGraph<V> extends GraphDirected<V> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
public Set<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
checkVert(vertex);
|
||||
int x = map.get(vertex);
|
||||
Collection<V> children = new HashSet<>();
|
||||
Set<V> children = getDefaultSet();
|
||||
Map<Integer, V> invert = getInverted();
|
||||
|
||||
for (int i = 0; i < matrix.length; i++)
|
||||
@@ -93,10 +93,10 @@ public class MatrixGraph<V> extends GraphDirected<V> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
public Set<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
checkVert(vertex);
|
||||
int x = map.get(vertex);
|
||||
Collection<V> ancestors = new HashSet<>();
|
||||
Set<V> ancestors = getDefaultSet();
|
||||
Map<Integer, V> invert = getInverted();
|
||||
|
||||
for (int i = 0; i < matrix.length; i++)
|
||||
@@ -189,7 +189,7 @@ public class MatrixGraph<V> extends GraphDirected<V> {
|
||||
}
|
||||
|
||||
private Map<Integer, V> getInverted() {
|
||||
Map<Integer, V> invert = new HashMap<>(map.size() + 1, 1);
|
||||
Map<Integer, V> invert = getDefaultMap();
|
||||
map.forEach((v, i) -> invert.put(i, v));
|
||||
return invert;
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import java.util.*;
|
||||
|
||||
public class MatrixUndGraph<V> extends GraphUndirected<V> {
|
||||
|
||||
Map<V, Integer> map = new HashMap<>();
|
||||
Map<V, Integer> map = getDefaultMap();
|
||||
private int[][] matrix = new int[0][0];
|
||||
|
||||
@Override
|
||||
@@ -73,33 +73,33 @@ public class MatrixUndGraph<V> extends GraphUndirected<V> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
public Set<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
checkVert(vertex);
|
||||
Collection<V> collection = new HashSet<>();
|
||||
Map<Integer, V> inverted = getInverted();
|
||||
V[] inverted = getInverted();
|
||||
Set<V> set = getDefaultSet();
|
||||
int x = map.get(vertex);
|
||||
for (int i = 0; i < matrix.length; i++)
|
||||
if (i < x && matrix[x][i] != 0)
|
||||
collection.add(inverted.get(i));
|
||||
set.add(inverted[i]);
|
||||
else if (i > x && matrix[i][x] != 0)
|
||||
collection.add(inverted.get(i));
|
||||
return collection;
|
||||
set.add(inverted[i]);
|
||||
return set;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
public Set<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException {
|
||||
return getChildren(vertex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Edge<V>> edges() {
|
||||
Map<Integer, V> inverted = getInverted();
|
||||
Collection<Edge<V>> edges = new LinkedList<>();
|
||||
public Set<Edge<V>> edges() {
|
||||
V[] inverted = getInverted();
|
||||
Set<Edge<V>> edges = getDefaultSet();
|
||||
|
||||
for (int i = 0; i < matrix.length; i++)
|
||||
for (int j = 0; j < matrix[i].length; j++)
|
||||
if (matrix[i][j] != NO_EDGE)
|
||||
edges.add(new Edge<>(inverted.get(i), inverted.get(j), matrix[i][j]));
|
||||
edges.add(new Edge<>(inverted[i], inverted[j], matrix[i][j]));
|
||||
return edges;
|
||||
}
|
||||
|
||||
@@ -168,9 +168,10 @@ public class MatrixUndGraph<V> extends GraphUndirected<V> {
|
||||
return newMatrix;
|
||||
}
|
||||
|
||||
private Map<Integer, V> getInverted() {
|
||||
Map<Integer, V> invert = new HashMap<>(map.size() + 1, 1);
|
||||
map.forEach((v, i) -> invert.put(i, v));
|
||||
@SuppressWarnings("unchecked")
|
||||
private V[] getInverted() {
|
||||
V[] invert = (V[]) new Object[map.size()];
|
||||
map.forEach((v, i) -> invert[i] = v);
|
||||
return invert;
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ public class GraphSaveStructure<V> {
|
||||
* @throws FileNotFoundException in the case the file is not found
|
||||
* @throws NullPointerException in the case the graph is null
|
||||
*/
|
||||
public final void load(@SuppressWarnings("ConstantConditions") Graph<V> graph, String fileName, Class<V> classV) throws FileNotFoundException, NullPointerException {
|
||||
public final void load(Graph<V> graph, String fileName, Class<V> classV) throws FileNotFoundException, NullPointerException {
|
||||
//this way i use this class for the load
|
||||
InstanceCreator<GraphSaveStructure<V>> creator = type -> this;
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(this.getClass(), creator).create();
|
||||
@@ -1,19 +1,20 @@
|
||||
package berack96.lib.graph.struct;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Function;
|
||||
|
||||
import berack96.lib.graph.Graph;
|
||||
|
||||
/**
|
||||
* Simple implementation of the {@link UnionFind} interface with priority to the find function.
|
||||
*
|
||||
* @param <X> the elements to search and merge
|
||||
*/
|
||||
public class QuickFind<X> implements UnionFind<X> {
|
||||
Map<X, Collection<X>> struct = new HashMap<>();
|
||||
Map<X, Collection<X>> struct = Graph.getDefaultMap();
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
@@ -22,7 +23,7 @@ public class QuickFind<X> implements UnionFind<X> {
|
||||
|
||||
@Override
|
||||
public void makeSetAll(Collection<X> elements) throws NullPointerException {
|
||||
Map<X, Collection<X>> temp = new HashMap<>(elements.size() + 1, 1);
|
||||
Map<X, Collection<X>> temp = Graph.getDefaultMap();
|
||||
for (X elem : elements)
|
||||
temp.computeIfAbsent(elem, new AddElement());
|
||||
struct.putAll(temp);
|
||||
@@ -65,10 +66,10 @@ public class QuickFind<X> implements UnionFind<X> {
|
||||
/**
|
||||
* Stupid class for implementing the adding of a new element
|
||||
*/
|
||||
private class AddElement implements Function<X, Collection<X>> {
|
||||
private class AddElement implements Function<X, Set<X>> {
|
||||
@Override
|
||||
public Collection<X> apply(X x) {
|
||||
Collection<X> coll = new HashSet<>();
|
||||
public Set<X> apply(X x) {
|
||||
Set<X> coll = Graph.getDefaultSet();
|
||||
coll.add(x);
|
||||
return coll;
|
||||
}
|
||||
@@ -74,13 +74,13 @@ public class GraphInfo<V> extends JPanel {
|
||||
components.add(vNumber);
|
||||
components.add(new JLabel("Edge Number: "));
|
||||
components.add(eNumber);
|
||||
components.add(new JLabel("Is Cyclic: "));
|
||||
//components.add(new JLabel("Is Cyclic: "));
|
||||
//components.add(gCyclic);
|
||||
|
||||
JPanel panelInfo = new JPanel();
|
||||
panelInfo.setOpaque(false);
|
||||
panelInfo.setBorder(BorderFactory.createLineBorder(Color.RED));
|
||||
panelInfo.setLayout(new GridLayout(components.size()/2, 2, 2*2, 2*2));
|
||||
panelInfo.setLayout(new GridLayout(components.size()/2, 2));
|
||||
components.forEach(panelInfo::add);
|
||||
components.clear();
|
||||
|
||||
@@ -123,7 +123,7 @@ public class GraphInfo<V> extends JPanel {
|
||||
|
||||
JPanel panelVertex = new JPanel();
|
||||
panelVertex.setOpaque(false);
|
||||
panelVertex.setLayout(new GridLayout(components.size()/2, 2, 2*2, 2*2));
|
||||
panelVertex.setLayout(new GridLayout(components.size()/2, 2));
|
||||
components.forEach(panelVertex::add);
|
||||
components.clear();
|
||||
|
||||
@@ -163,7 +163,7 @@ public class GraphInfo<V> extends JPanel {
|
||||
JPanel panelSave = new JPanel();
|
||||
panelSave.setOpaque(false);
|
||||
panelSave.setBorder(BorderFactory.createLineBorder(Color.RED));
|
||||
panelSave.setLayout(new GridLayout(components.size()/2, 2, 2*2, 2*2));
|
||||
panelSave.setLayout(new GridLayout(components.size()/2, 2));
|
||||
components.forEach(panelSave::add);
|
||||
components.clear();
|
||||
|
||||
@@ -177,7 +177,6 @@ public class GraphInfo<V> extends JPanel {
|
||||
this.add(panelVertex);
|
||||
this.add(panelErrors);
|
||||
this.add(panelSave);
|
||||
/*this.add(panelSave);*/
|
||||
|
||||
modVertex.doClick();
|
||||
|
||||
@@ -2,7 +2,7 @@ package berack96.lib.graph.visit;
|
||||
|
||||
import berack96.lib.graph.Edge;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @param <V>
|
||||
@@ -10,10 +10,11 @@ import java.util.Collection;
|
||||
public interface VisitMST<V> extends VisitStrategy<V> {
|
||||
|
||||
/**
|
||||
* Return the latest calculated MST.
|
||||
* Return the latest calculated MST.<br>
|
||||
* https://en.wikipedia.org/wiki/Minimum_spanning_tree
|
||||
*
|
||||
* @return the latest MST
|
||||
* @throws NullPointerException if there is no last calculated MST
|
||||
*/
|
||||
Collection<Edge<V>> getMST();
|
||||
Set<Edge<V>> getMST();
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package berack96.lib.graph.visit;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Interface that is helpful for implements visit that needs to retrieve the SCC
|
||||
@@ -16,5 +16,5 @@ public interface VisitSCC<V> extends VisitStrategy<V> {
|
||||
* @return the latest SCC
|
||||
* @throws NullPointerException if there is no last calculated SCC
|
||||
*/
|
||||
Collection<Collection<V>> getSCC();
|
||||
Set<Set<V>> getSCC();
|
||||
}
|
||||
@@ -32,8 +32,8 @@ public class Dijkstra<V> implements VisitDistance<V> {
|
||||
public VisitInfo<V> visit(Graph<V> graph, V source, Consumer<V> visit) throws NullPointerException, IllegalArgumentException {
|
||||
VisitInfo<V> info = new VisitInfo<>(source);
|
||||
Queue<QueueEntry> queue = new PriorityQueue<>();
|
||||
Map<V, Integer> dist = new HashMap<>();
|
||||
Map<V, V> prev = new HashMap<>();
|
||||
Map<V, Integer> dist = Graph.getDefaultMap();
|
||||
Map<V, V> prev = Graph.getDefaultMap();
|
||||
|
||||
this.source = source;
|
||||
dist.put(source, 0); // Initialization
|
||||
@@ -63,7 +63,7 @@ public class Dijkstra<V> implements VisitDistance<V> {
|
||||
}
|
||||
|
||||
/* Cleaning up the results */
|
||||
distance = new HashMap<>();
|
||||
distance = Graph.getDefaultMap();
|
||||
for (V vertex : prev.keySet()) {
|
||||
List<Edge<V>> path = new LinkedList<>();
|
||||
V child = vertex;
|
||||
@@ -17,10 +17,10 @@ import java.util.function.Consumer;
|
||||
* @param <V> The vertex of the graph
|
||||
*/
|
||||
public class Kruskal<V> implements VisitMST<V> {
|
||||
private Collection<Edge<V>> mst;
|
||||
private Set<Edge<V>> mst;
|
||||
|
||||
@Override
|
||||
public Collection<Edge<V>> getMST() {
|
||||
public Set<Edge<V>> getMST() {
|
||||
return mst;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public class Kruskal<V> implements VisitMST<V> {
|
||||
List<Edge<V>> edges = new ArrayList<>(graph.edges());
|
||||
Collections.sort(edges);
|
||||
|
||||
mst = new HashSet<>(graph.size(), 1);
|
||||
mst = Graph.getDefaultSet();
|
||||
Iterator<Edge<V>> iter = edges.iterator();
|
||||
while (iter.hasNext() && sets.size() > 1) {
|
||||
Edge<V> edge = iter.next();
|
||||
@@ -5,8 +5,7 @@ import berack96.lib.graph.Graph;
|
||||
import berack96.lib.graph.GraphUndirected;
|
||||
import berack96.lib.graph.visit.VisitMST;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
@@ -17,17 +16,17 @@ import java.util.function.Consumer;
|
||||
*/
|
||||
public class Prim<V> implements VisitMST<V> {
|
||||
|
||||
private Collection<Edge<V>> mst;
|
||||
private Set<Edge<V>> mst;
|
||||
|
||||
@Override
|
||||
public Collection<Edge<V>> getMST() {
|
||||
public Set<Edge<V>> getMST() {
|
||||
return mst;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VisitInfo<V> visit(Graph<V> graph, V source, Consumer<V> visit) throws NullPointerException, UnsupportedOperationException {
|
||||
mst = new LinkedList<>();
|
||||
Collection<V> vertices = graph.vertices();
|
||||
mst = Graph.getDefaultSet();
|
||||
Set<V> vertices = graph.vertices();
|
||||
|
||||
if (source == null)
|
||||
source = vertices.iterator().next();
|
||||
@@ -15,7 +15,7 @@ import java.util.function.Consumer;
|
||||
*/
|
||||
public class Tarjan<V> implements VisitSCC<V>, VisitTopological<V> {
|
||||
|
||||
private Collection<Collection<V>> SCC = null;
|
||||
private Set<Set<V>> SCC = null;
|
||||
private List<V> topologicalSort = null;
|
||||
|
||||
private Map<V, Integer> indices = null;
|
||||
@@ -24,7 +24,7 @@ public class Tarjan<V> implements VisitSCC<V>, VisitTopological<V> {
|
||||
private VisitInfo<V> info = null;
|
||||
|
||||
@Override
|
||||
public Collection<Collection<V>> getSCC() {
|
||||
public Set<Set<V>> getSCC() {
|
||||
return SCC;
|
||||
}
|
||||
|
||||
@@ -44,12 +44,12 @@ public class Tarjan<V> implements VisitSCC<V>, VisitTopological<V> {
|
||||
*/
|
||||
@Override
|
||||
public VisitInfo<V> visit(Graph<V> graph, V source, Consumer<V> visit) throws NullPointerException, IllegalArgumentException {
|
||||
SCC = new HashSet<>();
|
||||
SCC = Graph.getDefaultSet();
|
||||
topologicalSort = new ArrayList<>(graph.size());
|
||||
info = null;
|
||||
|
||||
indices = new HashMap<>();
|
||||
lowLink = new HashMap<>();
|
||||
indices = Graph.getDefaultMap();
|
||||
lowLink = Graph.getDefaultMap();
|
||||
stack = new Stack<>();
|
||||
int index = 0;
|
||||
|
||||
@@ -89,7 +89,7 @@ public class Tarjan<V> implements VisitSCC<V>, VisitTopological<V> {
|
||||
|
||||
// If v is a root node, pop the stack and generate an SCC
|
||||
if (lowLink.get(vertex).equals(indices.get(vertex))) {
|
||||
Set<V> newComponent = new HashSet<>();
|
||||
Set<V> newComponent = Graph.getDefaultSet();
|
||||
V temp;
|
||||
do {
|
||||
temp = stack.pop();
|
||||
@@ -23,6 +23,7 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Stream;
|
||||
@@ -1543,11 +1544,27 @@ public class TestGraph {
|
||||
new Edge<>("5", "4", 5),
|
||||
new Edge<>("6", "2", 2));
|
||||
|
||||
sub = graph.subGraph("even", "even");
|
||||
shouldContain(sub.vertices(), "2", "4", "6");
|
||||
shouldContain(sub.edges(),
|
||||
new Edge<>("4", "6", 6),
|
||||
new Edge<>("6", "2", 2));
|
||||
|
||||
sub = graph.subGraph("even", null, "even");
|
||||
shouldContain(sub.vertices(), "2", "4", "6");
|
||||
shouldContain(sub.edges(),
|
||||
new Edge<>("4", "6", 6),
|
||||
new Edge<>("6", "2", 2));
|
||||
|
||||
sub = graph.subGraph((Object) null);
|
||||
shouldContain(sub.vertices(), "7", "8");
|
||||
shouldContain(sub.edges(), new Edge<>("8", "7", 9));
|
||||
|
||||
sub = graph.subGraph();
|
||||
shouldContain(sub.vertices(), "7", "8");
|
||||
shouldContain(sub.edges(), new Edge<>("8", "7", 9));
|
||||
|
||||
sub = graph.subGraph(null);
|
||||
sub = graph.subGraph((Object[]) null);
|
||||
shouldContain(sub.vertices(), "7", "8");
|
||||
shouldContain(sub.edges(), new Edge<>("8", "7", 9));
|
||||
}
|
||||
@@ -1672,7 +1689,7 @@ public class TestGraph {
|
||||
//TODO tests for GraphUndirected save/load
|
||||
@ParameterizedTest
|
||||
@MethodSource("getGraphsDir")
|
||||
public void saveLoadDir(final GraphDirected<String> graph) {
|
||||
public void saveLoadDir(final GraphDirected<String> graph) throws URISyntaxException {
|
||||
/*
|
||||
* This graph should be like this
|
||||
*
|
||||
@@ -1682,8 +1699,7 @@ public class TestGraph {
|
||||
* v v
|
||||
* 3 <- 5 -> 4 8
|
||||
*/
|
||||
|
||||
String fileName = "test/resources/test.json";
|
||||
String fileName = ClassLoader.getSystemResource("").getPath() + "resources/test.json";
|
||||
Set<String> vertices = Set.of("1", "2", "3", "4", "5", "6", "7", "8");
|
||||
Set<Edge<String>> edges = new HashSet<>();
|
||||
Map<String, Set<Object>> marks = new HashMap<>();
|
||||
1
src/test/java/resources/test.json
Normal file
1
src/test/java/resources/test.json
Normal file
@@ -0,0 +1 @@
|
||||
{"vertices":["\"6\"","\"5\"","\"4\"","\"3\"","\"2\"","\"1\"","\"8\"","\"7\""],"edges":[{"src":"\"4\"","dest":"\"6\"","weight":6},{"src":"\"2\"","dest":"\"5\"","weight":4},{"src":"\"5\"","dest":"\"4\"","weight":5},{"src":"\"5\"","dest":"\"3\"","weight":2},{"src":"\"1\"","dest":"\"3\"","weight":1},{"src":"\"6\"","dest":"\"2\"","weight":2},{"src":"\"1\"","dest":"\"2\"","weight":1},{"src":"\"8\"","dest":"\"7\"","weight":9}]}
|
||||
@@ -1,54 +0,0 @@
|
||||
{
|
||||
"vertices": [
|
||||
"\"6\"",
|
||||
"\"5\"",
|
||||
"\"4\"",
|
||||
"\"3\"",
|
||||
"\"2\"",
|
||||
"\"1\"",
|
||||
"\"8\"",
|
||||
"\"7\""
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"src": "\"4\"",
|
||||
"dest": "\"6\"",
|
||||
"weight": 6
|
||||
},
|
||||
{
|
||||
"src": "\"2\"",
|
||||
"dest": "\"5\"",
|
||||
"weight": 4
|
||||
},
|
||||
{
|
||||
"src": "\"5\"",
|
||||
"dest": "\"4\"",
|
||||
"weight": 5
|
||||
},
|
||||
{
|
||||
"src": "\"5\"",
|
||||
"dest": "\"3\"",
|
||||
"weight": 2
|
||||
},
|
||||
{
|
||||
"src": "\"1\"",
|
||||
"dest": "\"3\"",
|
||||
"weight": 1
|
||||
},
|
||||
{
|
||||
"src": "\"6\"",
|
||||
"dest": "\"2\"",
|
||||
"weight": 2
|
||||
},
|
||||
{
|
||||
"src": "\"1\"",
|
||||
"dest": "\"2\"",
|
||||
"weight": 1
|
||||
},
|
||||
{
|
||||
"src": "\"8\"",
|
||||
"dest": "\"7\"",
|
||||
"weight": 9
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user