diff --git a/Algoritmi_2/Laboratorio/Lab2/TestGraph/TestGraph.java b/Algoritmi_2/Laboratorio/Lab2/TestGraph/TestGraph.java new file mode 100644 index 0000000000000000000000000000000000000000..6aa6324c1d3881dc960d57ca07b583c9f1c79ac4 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab2/TestGraph/TestGraph.java @@ -0,0 +1,69 @@ +import it.uniupo.graphLib.DirectedGraph; +import it.uniupo.graphLib.GraphInterface; +import it.uniupo.graphLib.UndirectedGraph; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import static org.junit.jupiter.api.Assertions.*; + +public class TestGraph { + @Test + void testCreate() { + GraphInterface grafo = new DirectedGraph(3); + BFS bfsTest = new BFS(grafo); + assertNotNull(bfsTest); + } + + @Test + void testInvalidSorgente() { + GraphInterface grafo = new DirectedGraph("1"); + BFS bfsTest = new BFS(grafo); + assertThrows(IllegalArgumentException.class, () -> bfsTest.getNodesInOrderOfVisit(3)); + } + + @Test + @Timeout(value = 500) + void testScoperti() { + GraphInterface grafo = new DirectedGraph("3;0 1;1 2;2 0"); + BFS bfsTest = new BFS(grafo); + assertTrue(bfsTest.getNodesInOrderOfVisit(0) != null); + } + + @Test + void testNumeroNodiVisitati() { + //Un solo nodo + GraphInterface grafo = new UndirectedGraph("1"); + BFS bfsTest = new BFS(grafo); + assertEquals(1, bfsTest.getNodesInOrderOfVisit(0).size()); + + //Due nodi ed un arco + grafo = new UndirectedGraph("2; 0 1"); + bfsTest = new BFS(grafo); + assertEquals(2, bfsTest.getNodesInOrderOfVisit(0).size()); + + //Quattro nodi e quattro archi + grafo = new UndirectedGraph("4;0 2;0 1;2 3;1 3"); + bfsTest = new BFS(grafo); + assertEquals(4, bfsTest.getNodesInOrderOfVisit(2).size()); + + } + + @Test + void testBFSOrder() { + GraphInterface grafo = new UndirectedGraph("4;0 2;0 1;2 3;1 3"); + BFS bfsTest = new BFS(grafo); + assertTrue(bfsTest.getNodesInOrderOfVisit(2).get(2) == 0 || bfsTest.getNodesInOrderOfVisit(2).get(2) == 3); + } + + @Test + public void testInitNumeroNodiVisitati() { + GraphInterface grafo = new UndirectedGraph("4;0 2;0 1;2 3;1 3"); + BFS bfsTest = new BFS(grafo); //<<- creato una volta sola + int numeroNodi = bfsTest.getNodesInOrderOfVisit(0).size();//<<-prima chiamata del metodo + assertEquals(4, numeroNodi); + numeroNodi = bfsTest.getNodesInOrderOfVisit(2).size(); //<<-seconda chiamata, stesso oggetto, parametro diverso + assertEquals(4, numeroNodi); + numeroNodi = bfsTest.getNodesInOrderOfVisit(3).size(); //<<-terza chiamata, stesso oggetto, parametro diverso + assertEquals(4, numeroNodi); + } +} diff --git a/Algoritmi_2/Laboratorio/Lab2/algo2_2.iml b/Algoritmi_2/Laboratorio/Lab2/algo2_2.iml new file mode 100644 index 0000000000000000000000000000000000000000..9ce1010f9f3c33e417125a676e5038466e5b6114 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab2/algo2_2.iml @@ -0,0 +1,55 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/TestGraph" isTestSource="true" /> + <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + <orderEntry type="module-library"> + <library> + <CLASSES> + <root url="jar://$USER_HOME$/Downloads/graphLib.jar!/" /> + </CLASSES> + <JAVADOC> + <root url="jar://$USER_HOME$/Downloads/graphLib.jar!/doc" /> + </JAVADOC> + <SOURCES /> + </library> + </orderEntry> + <orderEntry type="module-library" scope="TEST"> + <library name="JUnit5.8.1"> + <CLASSES> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" /> + </CLASSES> + <JAVADOC /> + <SOURCES /> + </library> + </orderEntry> + <orderEntry type="module-library" scope="TEST"> + <library name="JUnit5.8.1"> + <CLASSES> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" /> + <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" /> + </CLASSES> + <JAVADOC /> + <SOURCES /> + </library> + </orderEntry> + </component> +</module> \ No newline at end of file diff --git a/Algoritmi_2/Laboratorio/Lab2/out/production/algo2_2/BFS.class b/Algoritmi_2/Laboratorio/Lab2/out/production/algo2_2/BFS.class new file mode 100644 index 0000000000000000000000000000000000000000..c11dfe84ffe56572c3978073fb4e50863fe675a3 Binary files /dev/null and b/Algoritmi_2/Laboratorio/Lab2/out/production/algo2_2/BFS.class differ diff --git a/Algoritmi_2/Laboratorio/Lab2/out/production/algo2_2/Main.class b/Algoritmi_2/Laboratorio/Lab2/out/production/algo2_2/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..b5b51aed06a353d0c36a5840ef299ac7d1a85e64 Binary files /dev/null and b/Algoritmi_2/Laboratorio/Lab2/out/production/algo2_2/Main.class differ diff --git a/Algoritmi_2/Laboratorio/Lab2/out/test/algo2_2/TestGraph.class b/Algoritmi_2/Laboratorio/Lab2/out/test/algo2_2/TestGraph.class new file mode 100644 index 0000000000000000000000000000000000000000..acd573a97439e977f7f82b1ade68f2f8de7a9471 Binary files /dev/null and b/Algoritmi_2/Laboratorio/Lab2/out/test/algo2_2/TestGraph.class differ diff --git a/Algoritmi_2/Laboratorio/Lab2/src/BFS.java b/Algoritmi_2/Laboratorio/Lab2/src/BFS.java new file mode 100644 index 0000000000000000000000000000000000000000..a040151790ffec0e4686a9353306761168aba5db --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab2/src/BFS.java @@ -0,0 +1,40 @@ +import it.uniupo.graphLib.GraphInterface; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class BFS { + + private GraphInterface myGraph; + + public BFS(GraphInterface g) { + //System.out.println(g); + this.myGraph = g; + } + + public ArrayList<Integer> getNodesInOrderOfVisit(int sorgente) { + if (this.myGraph.getOrder() <= sorgente) { + throw new IllegalArgumentException("Dimensione della Sorgente errata"); + } + ArrayList<Integer> s = new ArrayList<>(); + ArrayList<Integer> valueToReturn = new ArrayList<>(); + s.add(sorgente); + valueToReturn.add(sorgente); + Queue<Integer> q = new LinkedList<>(); + q.add(sorgente); + while (!q.isEmpty()) { + int u = q.remove(); + List<Integer> neighbors = (List<Integer>) this.myGraph.getNeighbors(u); + for (Integer neighbor : neighbors) { + if (!s.contains(neighbor)) { + s.add(neighbor); + q.add(neighbor); + valueToReturn.add(neighbor); + } + } + } + return valueToReturn; + } +} diff --git a/Algoritmi_2/Laboratorio/Lab2/src/Main.java b/Algoritmi_2/Laboratorio/Lab2/src/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..b454cf6512a7146885d9199b12291ad511696c51 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab2/src/Main.java @@ -0,0 +1,10 @@ +import it.uniupo.graphLib.Graph; +import it.uniupo.graphLib.UndirectedGraph; + +public class Main { + public static void main(String[] args) { + Graph mioGrafo = new UndirectedGraph("3; 0 1; 1 2; 0 2;"); //Num Nodi + Archi + BFS prova = new BFS(mioGrafo); + System.out.println(prova.getNodesInOrderOfVisit(0)); + } +} \ No newline at end of file