Refactoring

- changed to maven
- modified hashMap and HashSet to Tree
- modified files path
This commit is contained in:
2022-08-30 11:12:50 +02:00
parent 9bda59dc7b
commit 07305b82f8
52 changed files with 1547 additions and 1486 deletions

1
.gitignore vendored
View File

@@ -125,6 +125,7 @@ local.properties
# mine # mine
target/
.idea/* .idea/*
.project .project
*.iml *.iml

View File

@@ -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
View 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>

View File

@@ -18,18 +18,46 @@ import java.util.function.Consumer;
*/ */
public abstract class Graph<V> implements Iterable<V> { public abstract class Graph<V> implements Iterable<V> {
//------------------- STAIC -----------------
public static final int NO_EDGE = 0; 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 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 PARAM_NULL = "The parameter must not be null";
public final static String VERTEX_NOT_CONTAINED = "The vertex must be contained in the graph"; 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 * @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 * @param vertices a collection of the vertices to add
* @throws NullPointerException if the set is null * @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); check(vertices);
for (V vertex : vertices) for (V vertex : vertices)
addIfAbsent(vertex); 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. * After this method's call the graph will have only vertices, and no edge.
*/ */
public void removeAllEdge() { public void removeAllEdge() {
Collection<V> vertices = vertices(); Set<V> vertices = vertices();
removeAll(); removeAll();
addAll(vertices); addAll(vertices);
} }
@@ -292,11 +320,11 @@ public abstract class Graph<V> implements Iterable<V> {
* Retrieve all the edges of a particular vertex.<br> * Retrieve all the edges of a particular vertex.<br>
* *
* @param vertex a vertex of 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 NullPointerException if the vertex is null
* @throws IllegalArgumentException if the vertex is not contained in the graph * @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> * 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. * where V1 is the source of that edge.
* *
* @param vertex the source vertex * @param vertex the source vertex
* @return an array of vertices * @return a set of vertices
* @throws NullPointerException if the vertex is null * @throws NullPointerException if the vertex is null
* @throws IllegalArgumentException if the vertex is not contained in the graph * @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> * Get all the vertices that have the vertex passed as their child.<br>
* Basically is the opposite of {@link #getChildren(Object)} * Basically is the opposite of {@link #getChildren(Object)}
* *
* @param vertex a vertex of the graph * @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 NullPointerException if one of the parameters is null
* @throws IllegalArgumentException if one of the vertex is not contained in the graph * @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> * 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> * 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() { public final Set<Object> marks() {
Collection<Object> ret = new HashSet<>(); Set<Object> ret = getDefaultSet();
markers.forEach((v, set) -> ret.addAll(set)); markers.forEach((v, set) -> ret.addAll(set));
return ret; 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 { public final void mark(V vertex, Object mark) throws NullPointerException, IllegalArgumentException {
check(mark); check(mark);
checkVert(vertex); checkVert(vertex);
Set<Object> marks = markers.computeIfAbsent(vertex, v -> new HashSet<>()); Set<Object> marks = markers.computeIfAbsent(vertex, v -> getDefaultSet());
marks.add(mark); 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> * If there aren't vertices with that mark then it is returned an empty set.<br>
* *
* @param mark the mark * @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 * @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); check(mark);
Collection<V> vertices = new ArrayList<>(markers.size()); Set<V> vertices = getDefaultSet();
markers.forEach((v, set) -> { markers.forEach((v, set) -> {
if (set.contains(mark)) if (set.contains(mark))
vertices.add(v); vertices.add(v);
@@ -408,16 +436,16 @@ public abstract class Graph<V> implements Iterable<V> {
/** /**
* Get all the marker of this vertex.<br> * Get all the marker of this vertex.<br>
* If the vertex doesn't have any mark, then it will return an empty set.<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 * @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 NullPointerException if the vertex is null
* @throws IllegalArgumentException if the vertex is not contained in the graph * @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); 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 { public final void unMarkAll(Object mark) throws NullPointerException {
check(mark); check(mark);
Collection<V> toRemove = new ArrayList<>(markers.size()); Set<V> toRemove = getDefaultSet();
markers.forEach((v, set) -> { markers.forEach((v, set) -> {
set.remove(mark); set.remove(mark);
if (set.size() == 0) if (set.size() == 0)
@@ -448,23 +476,23 @@ public abstract class Graph<V> implements Iterable<V> {
/** /**
* Get all the vertices in the graph.<br> * 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() { public Set<V> vertices() {
Collection<V> collection = new ArrayList<>(); Set<V> vertices = getDefaultSet();
forEach(collection::add); forEach(vertices::add);
return collection; return vertices;
} }
/** /**
* Get all the edges in the graph.<br> * 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> * 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 { public final Graph<V> subGraph(V source, int depth) throws NullPointerException, IllegalArgumentException {
checkVert(source); checkVert(source);
Graph<V> sub = getNewInstance(); 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); new BFS<V>().setMaxDepth(Math.max(depth, 0)).visit(this, source, vertices::add);
sub.addAll(vertices); sub.addAll(vertices);
for (V src : vertices) for (V src : vertices)
for (V dest : getChildren(src)) for (V dest : getChildren(src))
if (sub.contains(dest)) if (sub.contains(dest))
sub.addEdge(new Edge<>(src, dest, this.getWeight(src, dest))); sub.addEdge(src, dest, this.getWeight(src, dest));
return sub; 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> * 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 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 * @param marker one or more markers
* @return a sub-graph of the current graph * @return a sub-graph of the current graph
*/ */
public final Graph<V> subGraph(Object... marker) { public final Graph<V> subGraph(Object... marker) {
final Graph<V> sub = getNewInstance(); final Graph<V> sub = getNewInstance();
final Set<V> allVertices = new HashSet<>(); final Set<V> allVertices = getDefaultSet();
final Set<Object> allMarkers = new HashSet<>(); final Set<Object> allMarkers = getDefaultSet();
final boolean isEmpty = (marker == null || marker.length == 0);
if (!isEmpty) { if (marker != null && marker.length > 0)
Collections.addAll(allMarkers, marker); for(int i=0; i<marker.length; i++)
if(marker[i] != null)
allMarkers.add(marker[i]);
if(allMarkers.size() > 0)
markers.forEach((v, set) -> { markers.forEach((v, set) -> {
if (!Collections.disjoint(allMarkers, set)) if (!Collections.disjoint(allMarkers, set))
allVertices.add(v); allVertices.add(v);
}); });
} else { else {
Collection<V> toAdd = vertices(); Set<V> toAdd = vertices();
toAdd.removeAll(markers.keySet()); toAdd.removeAll(markers.keySet());
allVertices.addAll(toAdd); allVertices.addAll(toAdd);
} }
@@ -606,7 +638,6 @@ public abstract class Graph<V> implements Iterable<V> {
return dijkstra.getLastDistance(); return dijkstra.getLastDistance();
} }
/** /**
* Check if the object passed is not null. * Check if the object passed is not null.
* If it's null then throws eventual exception * If it's null then throws eventual exception

View File

@@ -4,10 +4,8 @@ import berack96.lib.graph.visit.VisitSCC;
import berack96.lib.graph.visit.VisitTopological; import berack96.lib.graph.visit.VisitTopological;
import berack96.lib.graph.visit.impl.Tarjan; 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.List;
import java.util.Set;
/** /**
* This is a more specific interface for an implementation of a Undirected Graph.<br> * 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> * 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> * 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. * could affect the graph behavior and the changes could be reflected to the graph.
* *
* @param vertex a vertex of 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 NullPointerException if the vertex is null
* @throws IllegalArgumentException if the vertex is not contained in the graph * @throws IllegalArgumentException if the vertex is not contained in the graph
*/ */
public Collection<Edge<V>> getEdgesIn(V vertex) throws NullPointerException, IllegalArgumentException { public Set<Edge<V>> getEdgesIn(V vertex) throws NullPointerException, IllegalArgumentException {
Collection<V> ancestors = getAncestors(vertex); Set<V> ancestors = getAncestors(vertex);
Collection<Edge<V>> edgesIn = new HashSet<>(ancestors.size()); Set<Edge<V>> edgesIn = getDefaultSet();
for (V ancestor : ancestors) { for (V ancestor : ancestors) {
int weight = getWeight(ancestor, vertex); 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> * 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> * 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. * could affect the graph behavior and the changes could be reflected to the graph.
* *
* @param vertex a vertex of 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 NullPointerException if the vertex is null
* @throws IllegalArgumentException if the vertex is not contained in the graph * @throws IllegalArgumentException if the vertex is not contained in the graph
*/ */
public Collection<Edge<V>> getEdgesOut(V vertex) throws NullPointerException, IllegalArgumentException { public Set<Edge<V>> getEdgesOut(V vertex) throws NullPointerException, IllegalArgumentException {
Collection<V> children = getChildren(vertex); Set<V> children = getChildren(vertex);
Collection<Edge<V>> edgesOut = new HashSet<>(children.size()); Set<Edge<V>> edgesOut = getDefaultSet();
for (V child : children) { for (V child : children) {
int weight = getWeight(vertex, child); 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 * The strongly connected components or disconnected components of an arbitrary directed graph
* form a partition into subgraphs that are themselves strongly connected. * 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<>(); VisitSCC<V> visit = new Tarjan<>();
visit.visit(this, null, null); visit.visit(this, null, null);
return visit.getSCC(); return visit.getSCC();
} }
@Override @Override
public Collection<Edge<V>> edgesOf(V vertex) throws NullPointerException, IllegalArgumentException { public Set<Edge<V>> edgesOf(V vertex) throws NullPointerException, IllegalArgumentException {
Collection<Edge<V>> edges = getEdgesIn(vertex); Set<Edge<V>> edges = getEdgesIn(vertex);
edges.addAll(getEdgesOut(vertex)); edges.addAll(getEdgesOut(vertex));
return edges; return edges;
} }
@Override @Override
public Collection<Edge<V>> edges() { public Set<Edge<V>> edges() {
Collection<Edge<V>> collection = new ArrayList<>(); Set<Edge<V>> set = getDefaultSet();
forEach(v -> collection.addAll(getEdgesIn(v))); forEach(v -> set.addAll(getEdgesIn(v)));
return collection; return set;
} }
@Override @Override

View File

@@ -3,8 +3,7 @@ package berack96.lib.graph;
import berack96.lib.graph.visit.VisitMST; import berack96.lib.graph.visit.VisitMST;
import berack96.lib.graph.visit.impl.Prim; import berack96.lib.graph.visit.impl.Prim;
import java.util.Collection; import java.util.Set;
import java.util.LinkedList;
/** /**
* This is a more specific interface for an implementation of a Directed Graph.<br> * 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. * 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; return null;
} }
/** /**
* minimum spanning forest or minimum spamming tree of the graph * 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<>(); VisitMST<V> visit = new Prim<>();
visit.visit(this, iterator().next(), null); visit.visit(this, iterator().next(), null);
return visit.getMST(); return visit.getMST();
} }
@Override @Override
public Collection<Edge<V>> edgesOf(V vertex) throws NullPointerException, IllegalArgumentException { public Set<Edge<V>> edgesOf(V vertex) throws NullPointerException, IllegalArgumentException {
checkVert(vertex); 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)))); getChildren(vertex).forEach(v -> edges.add(new Edge<>(vertex, v, getWeight(vertex, v))));
return edges; return edges;
} }

View 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();
}
}

View File

@@ -14,7 +14,8 @@ import java.util.concurrent.atomic.AtomicInteger;
*/ */
public class ListGraph<V> extends GraphDirected<V> { 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 @Override
public Iterator<V> iterator() { public Iterator<V> iterator() {
@@ -73,18 +74,18 @@ public class ListGraph<V> extends GraphDirected<V> {
} }
@Override @Override
public Collection<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException { public Set<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException {
checkVert(vertex); checkVert(vertex);
Collection<V> children = new HashSet<>(); Set<V> children = getDefaultSet();
for (Adj adj : adj.get(vertex)) for (Adj adj : adj.get(vertex))
children.add(adj.vertex); children.add(adj.vertex);
return children; return children;
} }
@Override @Override
public Collection<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException { public Set<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException {
checkVert(vertex); checkVert(vertex);
Collection<V> ancestors = new HashSet<>(); Set<V> ancestors = getDefaultSet();
adj.forEach((v, list) -> { adj.forEach((v, list) -> {
if (getAdj(list, vertex) != null) if (getAdj(list, vertex) != null)
ancestors.add(v); ancestors.add(v);

View File

@@ -7,11 +7,12 @@ import java.util.*;
import java.util.concurrent.atomic.AtomicInteger; 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> * 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, * 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> * 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 * @param <V> the vertices
* @author Berack96 * @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> * 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 * 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 @Override
public Iterator<V> iterator() { public Iterator<V> iterator() {
@@ -39,7 +40,7 @@ public class MapGraph<V> extends GraphDirected<V> {
@Override @Override
public void add(V vertex) { public void add(V vertex) {
check(vertex); check(vertex);
edges.computeIfAbsent(vertex, v -> new HashMap<>()); edges.computeIfAbsent(vertex, v -> new TreeMap<>());
edges.forEach((v, adj) -> adj.remove(vertex)); edges.forEach((v, adj) -> adj.remove(vertex));
edges.get(vertex).clear(); edges.get(vertex).clear();
} }
@@ -79,15 +80,15 @@ public class MapGraph<V> extends GraphDirected<V> {
} }
@Override @Override
public Collection<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException { public Set<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException {
checkVert(vertex); checkVert(vertex);
return new HashSet<>(edges.get(vertex).keySet()); return new HashSet<>(edges.get(vertex).keySet());
} }
@Override @Override
public Collection<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException { public Set<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException {
checkVert(vertex); checkVert(vertex);
Collection<V> ancestors = new HashSet<>(); Set<V> ancestors = getDefaultSet();
edges.forEach((v, adj) -> { edges.forEach((v, adj) -> {
if (adj.containsKey(vertex)) if (adj.containsKey(vertex))
ancestors.add(v); ancestors.add(v);

View File

@@ -13,7 +13,7 @@ import java.util.*;
*/ */
public class MatrixGraph<V> extends GraphDirected<V> { 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]; private int[][] matrix = new int[0][0];
@Override @Override
@@ -80,10 +80,10 @@ public class MatrixGraph<V> extends GraphDirected<V> {
} }
@Override @Override
public Collection<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException { public Set<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException {
checkVert(vertex); checkVert(vertex);
int x = map.get(vertex); int x = map.get(vertex);
Collection<V> children = new HashSet<>(); Set<V> children = getDefaultSet();
Map<Integer, V> invert = getInverted(); Map<Integer, V> invert = getInverted();
for (int i = 0; i < matrix.length; i++) for (int i = 0; i < matrix.length; i++)
@@ -93,10 +93,10 @@ public class MatrixGraph<V> extends GraphDirected<V> {
} }
@Override @Override
public Collection<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException { public Set<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException {
checkVert(vertex); checkVert(vertex);
int x = map.get(vertex); int x = map.get(vertex);
Collection<V> ancestors = new HashSet<>(); Set<V> ancestors = getDefaultSet();
Map<Integer, V> invert = getInverted(); Map<Integer, V> invert = getInverted();
for (int i = 0; i < matrix.length; i++) for (int i = 0; i < matrix.length; i++)
@@ -189,7 +189,7 @@ public class MatrixGraph<V> extends GraphDirected<V> {
} }
private Map<Integer, V> getInverted() { 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)); map.forEach((v, i) -> invert.put(i, v));
return invert; return invert;
} }

View File

@@ -8,7 +8,7 @@ import java.util.*;
public class MatrixUndGraph<V> extends GraphUndirected<V> { 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]; private int[][] matrix = new int[0][0];
@Override @Override
@@ -73,33 +73,33 @@ public class MatrixUndGraph<V> extends GraphUndirected<V> {
} }
@Override @Override
public Collection<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException { public Set<V> getChildren(V vertex) throws NullPointerException, IllegalArgumentException {
checkVert(vertex); checkVert(vertex);
Collection<V> collection = new HashSet<>(); V[] inverted = getInverted();
Map<Integer, V> inverted = getInverted(); Set<V> set = getDefaultSet();
int x = map.get(vertex); int x = map.get(vertex);
for (int i = 0; i < matrix.length; i++) for (int i = 0; i < matrix.length; i++)
if (i < x && matrix[x][i] != 0) if (i < x && matrix[x][i] != 0)
collection.add(inverted.get(i)); set.add(inverted[i]);
else if (i > x && matrix[i][x] != 0) else if (i > x && matrix[i][x] != 0)
collection.add(inverted.get(i)); set.add(inverted[i]);
return collection; return set;
} }
@Override @Override
public Collection<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException { public Set<V> getAncestors(V vertex) throws NullPointerException, IllegalArgumentException {
return getChildren(vertex); return getChildren(vertex);
} }
@Override @Override
public Collection<Edge<V>> edges() { public Set<Edge<V>> edges() {
Map<Integer, V> inverted = getInverted(); V[] inverted = getInverted();
Collection<Edge<V>> edges = new LinkedList<>(); Set<Edge<V>> edges = getDefaultSet();
for (int i = 0; i < matrix.length; i++) for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[i].length; j++) for (int j = 0; j < matrix[i].length; j++)
if (matrix[i][j] != NO_EDGE) 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; return edges;
} }
@@ -168,9 +168,10 @@ public class MatrixUndGraph<V> extends GraphUndirected<V> {
return newMatrix; return newMatrix;
} }
private Map<Integer, V> getInverted() { @SuppressWarnings("unchecked")
Map<Integer, V> invert = new HashMap<>(map.size() + 1, 1); private V[] getInverted() {
map.forEach((v, i) -> invert.put(i, v)); V[] invert = (V[]) new Object[map.size()];
map.forEach((v, i) -> invert[i] = v);
return invert; return invert;
} }
} }

View File

@@ -31,7 +31,7 @@ public class GraphSaveStructure<V> {
* @throws FileNotFoundException in the case the file is not found * @throws FileNotFoundException in the case the file is not found
* @throws NullPointerException in the case the graph is null * @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 //this way i use this class for the load
InstanceCreator<GraphSaveStructure<V>> creator = type -> this; InstanceCreator<GraphSaveStructure<V>> creator = type -> this;
Gson gson = new GsonBuilder().registerTypeAdapter(this.getClass(), creator).create(); Gson gson = new GsonBuilder().registerTypeAdapter(this.getClass(), creator).create();

View File

@@ -1,19 +1,20 @@
package berack96.lib.graph.struct; package berack96.lib.graph.struct;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function; import java.util.function.Function;
import berack96.lib.graph.Graph;
/** /**
* Simple implementation of the {@link UnionFind} interface with priority to the find function. * Simple implementation of the {@link UnionFind} interface with priority to the find function.
* *
* @param <X> the elements to search and merge * @param <X> the elements to search and merge
*/ */
public class QuickFind<X> implements UnionFind<X> { public class QuickFind<X> implements UnionFind<X> {
Map<X, Collection<X>> struct = new HashMap<>(); Map<X, Collection<X>> struct = Graph.getDefaultMap();
@Override @Override
public int size() { public int size() {
@@ -22,7 +23,7 @@ public class QuickFind<X> implements UnionFind<X> {
@Override @Override
public void makeSetAll(Collection<X> elements) throws NullPointerException { 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) for (X elem : elements)
temp.computeIfAbsent(elem, new AddElement()); temp.computeIfAbsent(elem, new AddElement());
struct.putAll(temp); struct.putAll(temp);
@@ -65,10 +66,10 @@ public class QuickFind<X> implements UnionFind<X> {
/** /**
* Stupid class for implementing the adding of a new element * 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 @Override
public Collection<X> apply(X x) { public Set<X> apply(X x) {
Collection<X> coll = new HashSet<>(); Set<X> coll = Graph.getDefaultSet();
coll.add(x); coll.add(x);
return coll; return coll;
} }

View File

@@ -74,13 +74,13 @@ public class GraphInfo<V> extends JPanel {
components.add(vNumber); components.add(vNumber);
components.add(new JLabel("Edge Number: ")); components.add(new JLabel("Edge Number: "));
components.add(eNumber); components.add(eNumber);
components.add(new JLabel("Is Cyclic: ")); //components.add(new JLabel("Is Cyclic: "));
//components.add(gCyclic); //components.add(gCyclic);
JPanel panelInfo = new JPanel(); JPanel panelInfo = new JPanel();
panelInfo.setOpaque(false); panelInfo.setOpaque(false);
panelInfo.setBorder(BorderFactory.createLineBorder(Color.RED)); 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.forEach(panelInfo::add);
components.clear(); components.clear();
@@ -123,7 +123,7 @@ public class GraphInfo<V> extends JPanel {
JPanel panelVertex = new JPanel(); JPanel panelVertex = new JPanel();
panelVertex.setOpaque(false); 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.forEach(panelVertex::add);
components.clear(); components.clear();
@@ -163,7 +163,7 @@ public class GraphInfo<V> extends JPanel {
JPanel panelSave = new JPanel(); JPanel panelSave = new JPanel();
panelSave.setOpaque(false); panelSave.setOpaque(false);
panelSave.setBorder(BorderFactory.createLineBorder(Color.RED)); 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.forEach(panelSave::add);
components.clear(); components.clear();
@@ -177,7 +177,6 @@ public class GraphInfo<V> extends JPanel {
this.add(panelVertex); this.add(panelVertex);
this.add(panelErrors); this.add(panelErrors);
this.add(panelSave); this.add(panelSave);
/*this.add(panelSave);*/
modVertex.doClick(); modVertex.doClick();

View File

@@ -2,7 +2,7 @@ package berack96.lib.graph.visit;
import berack96.lib.graph.Edge; import berack96.lib.graph.Edge;
import java.util.Collection; import java.util.Set;
/** /**
* @param <V> * @param <V>
@@ -10,10 +10,11 @@ import java.util.Collection;
public interface VisitMST<V> extends VisitStrategy<V> { 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 * @return the latest MST
* @throws NullPointerException if there is no last calculated MST * @throws NullPointerException if there is no last calculated MST
*/ */
Collection<Edge<V>> getMST(); Set<Edge<V>> getMST();
} }

View File

@@ -1,6 +1,6 @@
package berack96.lib.graph.visit; 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 * 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 * @return the latest SCC
* @throws NullPointerException if there is no last calculated SCC * @throws NullPointerException if there is no last calculated SCC
*/ */
Collection<Collection<V>> getSCC(); Set<Set<V>> getSCC();
} }

View File

@@ -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 { public VisitInfo<V> visit(Graph<V> graph, V source, Consumer<V> visit) throws NullPointerException, IllegalArgumentException {
VisitInfo<V> info = new VisitInfo<>(source); VisitInfo<V> info = new VisitInfo<>(source);
Queue<QueueEntry> queue = new PriorityQueue<>(); Queue<QueueEntry> queue = new PriorityQueue<>();
Map<V, Integer> dist = new HashMap<>(); Map<V, Integer> dist = Graph.getDefaultMap();
Map<V, V> prev = new HashMap<>(); Map<V, V> prev = Graph.getDefaultMap();
this.source = source; this.source = source;
dist.put(source, 0); // Initialization dist.put(source, 0); // Initialization
@@ -63,7 +63,7 @@ public class Dijkstra<V> implements VisitDistance<V> {
} }
/* Cleaning up the results */ /* Cleaning up the results */
distance = new HashMap<>(); distance = Graph.getDefaultMap();
for (V vertex : prev.keySet()) { for (V vertex : prev.keySet()) {
List<Edge<V>> path = new LinkedList<>(); List<Edge<V>> path = new LinkedList<>();
V child = vertex; V child = vertex;

View File

@@ -17,10 +17,10 @@ import java.util.function.Consumer;
* @param <V> The vertex of the graph * @param <V> The vertex of the graph
*/ */
public class Kruskal<V> implements VisitMST<V> { public class Kruskal<V> implements VisitMST<V> {
private Collection<Edge<V>> mst; private Set<Edge<V>> mst;
@Override @Override
public Collection<Edge<V>> getMST() { public Set<Edge<V>> getMST() {
return mst; return mst;
} }
@@ -32,7 +32,7 @@ public class Kruskal<V> implements VisitMST<V> {
List<Edge<V>> edges = new ArrayList<>(graph.edges()); List<Edge<V>> edges = new ArrayList<>(graph.edges());
Collections.sort(edges); Collections.sort(edges);
mst = new HashSet<>(graph.size(), 1); mst = Graph.getDefaultSet();
Iterator<Edge<V>> iter = edges.iterator(); Iterator<Edge<V>> iter = edges.iterator();
while (iter.hasNext() && sets.size() > 1) { while (iter.hasNext() && sets.size() > 1) {
Edge<V> edge = iter.next(); Edge<V> edge = iter.next();

View File

@@ -5,8 +5,7 @@ import berack96.lib.graph.Graph;
import berack96.lib.graph.GraphUndirected; import berack96.lib.graph.GraphUndirected;
import berack96.lib.graph.visit.VisitMST; import berack96.lib.graph.visit.VisitMST;
import java.util.Collection; import java.util.Set;
import java.util.LinkedList;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
@@ -17,17 +16,17 @@ import java.util.function.Consumer;
*/ */
public class Prim<V> implements VisitMST<V> { public class Prim<V> implements VisitMST<V> {
private Collection<Edge<V>> mst; private Set<Edge<V>> mst;
@Override @Override
public Collection<Edge<V>> getMST() { public Set<Edge<V>> getMST() {
return mst; return mst;
} }
@Override @Override
public VisitInfo<V> visit(Graph<V> graph, V source, Consumer<V> visit) throws NullPointerException, UnsupportedOperationException { public VisitInfo<V> visit(Graph<V> graph, V source, Consumer<V> visit) throws NullPointerException, UnsupportedOperationException {
mst = new LinkedList<>(); mst = Graph.getDefaultSet();
Collection<V> vertices = graph.vertices(); Set<V> vertices = graph.vertices();
if (source == null) if (source == null)
source = vertices.iterator().next(); source = vertices.iterator().next();

View File

@@ -15,7 +15,7 @@ import java.util.function.Consumer;
*/ */
public class Tarjan<V> implements VisitSCC<V>, VisitTopological<V> { 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 List<V> topologicalSort = null;
private Map<V, Integer> indices = 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; private VisitInfo<V> info = null;
@Override @Override
public Collection<Collection<V>> getSCC() { public Set<Set<V>> getSCC() {
return SCC; return SCC;
} }
@@ -44,12 +44,12 @@ public class Tarjan<V> implements VisitSCC<V>, VisitTopological<V> {
*/ */
@Override @Override
public VisitInfo<V> visit(Graph<V> graph, V source, Consumer<V> visit) throws NullPointerException, IllegalArgumentException { 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()); topologicalSort = new ArrayList<>(graph.size());
info = null; info = null;
indices = new HashMap<>(); indices = Graph.getDefaultMap();
lowLink = new HashMap<>(); lowLink = Graph.getDefaultMap();
stack = new Stack<>(); stack = new Stack<>();
int index = 0; 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 v is a root node, pop the stack and generate an SCC
if (lowLink.get(vertex).equals(indices.get(vertex))) { if (lowLink.get(vertex).equals(indices.get(vertex))) {
Set<V> newComponent = new HashSet<>(); Set<V> newComponent = Graph.getDefaultSet();
V temp; V temp;
do { do {
temp = stack.pop(); temp = stack.pop();

View File

@@ -23,6 +23,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -1543,11 +1544,27 @@ public class TestGraph {
new Edge<>("5", "4", 5), new Edge<>("5", "4", 5),
new Edge<>("6", "2", 2)); 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(); sub = graph.subGraph();
shouldContain(sub.vertices(), "7", "8"); shouldContain(sub.vertices(), "7", "8");
shouldContain(sub.edges(), new Edge<>("8", "7", 9)); shouldContain(sub.edges(), new Edge<>("8", "7", 9));
sub = graph.subGraph(null); sub = graph.subGraph((Object[]) null);
shouldContain(sub.vertices(), "7", "8"); shouldContain(sub.vertices(), "7", "8");
shouldContain(sub.edges(), new Edge<>("8", "7", 9)); shouldContain(sub.edges(), new Edge<>("8", "7", 9));
} }
@@ -1672,7 +1689,7 @@ public class TestGraph {
//TODO tests for GraphUndirected save/load //TODO tests for GraphUndirected save/load
@ParameterizedTest @ParameterizedTest
@MethodSource("getGraphsDir") @MethodSource("getGraphsDir")
public void saveLoadDir(final GraphDirected<String> graph) { public void saveLoadDir(final GraphDirected<String> graph) throws URISyntaxException {
/* /*
* This graph should be like this * This graph should be like this
* *
@@ -1682,8 +1699,7 @@ public class TestGraph {
* v v * v v
* 3 <- 5 -> 4 8 * 3 <- 5 -> 4 8
*/ */
String fileName = ClassLoader.getSystemResource("").getPath() + "resources/test.json";
String fileName = "test/resources/test.json";
Set<String> vertices = Set.of("1", "2", "3", "4", "5", "6", "7", "8"); Set<String> vertices = Set.of("1", "2", "3", "4", "5", "6", "7", "8");
Set<Edge<String>> edges = new HashSet<>(); Set<Edge<String>> edges = new HashSet<>();
Map<String, Set<Object>> marks = new HashMap<>(); Map<String, Set<Object>> marks = new HashMap<>();

View 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}]}

View File

@@ -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
}
]
}