diff --git a/Algoritmi_2/Laboratorio/Lab5/.gitignore b/Algoritmi_2/Laboratorio/Lab5/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..f68d1099657e34d4e7a68aadc730b3ecad84667d --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab5/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/Algoritmi_2/Laboratorio/Lab5/.idea/.gitignore b/Algoritmi_2/Laboratorio/Lab5/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..26d33521af10bcc7fd8cea344038eaaeb78d0ef5 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab5/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Algoritmi_2/Laboratorio/Lab5/.idea/misc.xml b/Algoritmi_2/Laboratorio/Lab5/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..69ace3f6affaf674f40a55887f3d6f3564afd626 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab5/.idea/misc.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK"> + <output url="file://$PROJECT_DIR$/out" /> + </component> +</project> \ No newline at end of file diff --git a/Algoritmi_2/Laboratorio/Lab5/.idea/modules.xml b/Algoritmi_2/Laboratorio/Lab5/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..4b70421b0daab8efd1ee60e96008c8120bf50ffb --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab5/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/Lab5.iml" filepath="$PROJECT_DIR$/Lab5.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/Algoritmi_2/Laboratorio/Lab5/.idea/vcs.xml b/Algoritmi_2/Laboratorio/Lab5/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..c2365ab11f9ba6b763735c8fd976420234bb3521 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab5/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="$PROJECT_DIR$/../../.." vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/Algoritmi_2/Laboratorio/Lab5/Lab5.iml b/Algoritmi_2/Laboratorio/Lab5/Lab5.iml new file mode 100644 index 0000000000000000000000000000000000000000..e40bd7b2f05a540a9fe10150d63c4a2d3c9cc131 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab5/Lab5.iml @@ -0,0 +1,39 @@ +<?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$/Test" 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://$MODULE_DIR$/../graphLib.jar!/" /> + </CLASSES> + <JAVADOC> + <root url="jar://$MODULE_DIR$/../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> + </component> +</module> \ No newline at end of file diff --git a/Algoritmi_2/Laboratorio/Lab5/Test/TestLab.java b/Algoritmi_2/Laboratorio/Lab5/Test/TestLab.java new file mode 100644 index 0000000000000000000000000000000000000000..44caa32580074985d5f48994a224b2a258246749 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab5/Test/TestLab.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.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; + +public class TestLab { + @Test + void testIsConnected() { + UndirectedGraph gOneNode = new UndirectedGraph("1;"); + BFS bfsTest = new BFS(gOneNode); + Assertions.assertTrue(bfsTest.isConnected()); + + UndirectedGraph gConnected = new UndirectedGraph("3; 0 1; 1 2; 2 0"); + BFS bfsTest2 = new BFS(gConnected); + Assertions.assertTrue(bfsTest2.isConnected()); + + UndirectedGraph gUnonnected = new UndirectedGraph("5; 0 1; 0 2"); + BFS bfsTest3 = new BFS(gUnonnected); + Assertions.assertFalse(bfsTest3.isConnected()); + + } + + @Test + void testConnectedComponents() { + UndirectedGraph gOneNode = new UndirectedGraph("1"); + BFS bfsTest = new BFS(gOneNode); + int[] cc1 = bfsTest.connectedComponents(); + Assertions.assertEquals(cc1[0], 0); + + UndirectedGraph gConnected = new UndirectedGraph("2; 0 1;"); + BFS bfsTest2 = new BFS(gConnected); + int[] cc2 = bfsTest2.connectedComponents(); + Assertions.assertEquals(cc2[0], cc2[1]); + + UndirectedGraph gUnconnected = new UndirectedGraph("2;"); + BFS bfsTest3 = new BFS(gUnconnected); + int[] cc3 = bfsTest3.connectedComponents(); + Assertions.assertNotEquals(cc3[0], cc3[1]); + UndirectedGraph gBig = new UndirectedGraph("6; 0 4; 0 1; 2 5;"); + BFS bfsTestBig = new BFS(gBig); + int[] ccBig = bfsTestBig.connectedComponents(); + Assertions.assertEquals(ccBig[0], ccBig[1]); + Assertions.assertEquals(ccBig[0], ccBig[4]); + Assertions.assertEquals(ccBig[2], ccBig[5]); + Assertions.assertNotEquals(ccBig[3], ccBig[1]); + } + + @Test + void testTopologicalOrder() { + DirectedGraph gOneNode = new DirectedGraph("1"); + DFS dfsTest = new DFS(gOneNode); + Assertions.assertEquals(dfsTest.topologicalOrder().getFirst(), 0); + + + DirectedGraph gTwoNodes = new DirectedGraph("2; 0 1;"); + DFS dfsTest2 = new DFS(gTwoNodes); + Assertions.assertTrue(dfsTest2.topologicalOrder().indexOf(0) < dfsTest2.topologicalOrder().indexOf(1)); + + DirectedGraph gThreeNodes = new DirectedGraph("3; 2 0;"); + DFS dfsTest3 = new DFS(gThreeNodes); + System.out.println(dfsTest3.topologicalOrder()); + ArrayList<Integer> topologicOrder = dfsTest3.topologicalOrder(); + //DA FINIRE MA FUNZIONA + } +} diff --git a/Algoritmi_2/Laboratorio/Lab5/src/BFS.java b/Algoritmi_2/Laboratorio/Lab5/src/BFS.java new file mode 100644 index 0000000000000000000000000000000000000000..476da9d8c0b843b60b51fbee8638a99a61d4d42a --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab5/src/BFS.java @@ -0,0 +1,66 @@ +import it.uniupo.graphLib.*; + +import java.awt.List; +import java.util.*; + +public class BFS { + + private GraphInterface myGraph; + private boolean[] founded; + + + public BFS(GraphInterface g) { + //System.out.println(g); + this.myGraph = g; + this.founded = new boolean[this.myGraph.getOrder()]; + } + + public ArrayList<Integer> getNodesInOrderOfVisit(int sorgente) { + if (this.myGraph.getOrder() <= sorgente) { + throw new IllegalArgumentException("Sorgente errata"); + } + ArrayList<Integer> returnList = new ArrayList<>(); + returnList.add(sorgente); + Queue<Integer> q = new LinkedList<>(); + founded[sorgente] = true; + q.add(sorgente); + while (!q.isEmpty()) { + int u = q.remove(); + Iterable<Integer> neighbors = this.myGraph.getNeighbors(u); + for (int v : neighbors) { + if (!founded[v]) { + founded[v] = true; + q.add(v); + returnList.add(v); + } + } + } + return returnList; + } + + public boolean isConnected() { + if (myGraph == null) { + return true; + } + ArrayList<Integer> nodes = getNodesInOrderOfVisit(0); + return nodes.size() == myGraph.getOrder(); + } + + public int[] connectedComponents() { + int[] connectedComponentsArr = new int[myGraph.getOrder()]; + Arrays.fill(connectedComponentsArr, -1); + Arrays.fill(founded, false); + + int cnt = 0; + for (int node = 0; node < myGraph.getOrder(); node++) { + if (!founded[node]) { + ArrayList<Integer> nodes = getNodesInOrderOfVisit(node); + for (int n : nodes) { + connectedComponentsArr[n] = cnt; + } + cnt++; + } + } + return connectedComponentsArr; + } +} diff --git a/Algoritmi_2/Laboratorio/Lab5/src/DFS.java b/Algoritmi_2/Laboratorio/Lab5/src/DFS.java new file mode 100644 index 0000000000000000000000000000000000000000..6e1db65ea45e67da9c494e0ec1ace6cdc3b58dac --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab5/src/DFS.java @@ -0,0 +1,76 @@ +import it.uniupo.graphLib.GraphInterface; +import it.uniupo.graphLib.UndirectedGraph; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +public class DFS { + + private GraphInterface myGraph; + private boolean[] founded; + private GraphInterface treeDFS; + + private ArrayList<Integer> postVisitOrder = new ArrayList<>(); + + public DFS(GraphInterface g) { + this.myGraph = g; + this.founded = new boolean[this.myGraph.getOrder()]; + this.treeDFS = this.myGraph.create(); + } + + private void visitaDFS(int sorg) { + if (sorg >= this.myGraph.getOrder() || sorg < 0) { + throw new IllegalArgumentException("Sorgente non presenten nel grafo"); + } + founded[sorg] = true; + for (int node : this.myGraph.getNeighbors(sorg)) { + if (!founded[node]) { + treeDFS.addEdge(sorg, node); + visitaDFS(node); + } + } + postVisitOrder.add(sorg); + } + + public GraphInterface getTree(int sorg) { + visitaDFS(sorg); + return this.treeDFS; + } + + public GraphInterface getTreeRicoprente(int sorg) throws NotAllNodesReachedException { + visitaDFS(sorg); + if (treeDFS.getOrder() != myGraph.getOrder() - 1) { + throw new NotAllNodesReachedException("Esistono due alberi"); + } + return this.treeDFS; + } + + public GraphInterface getForest() { + for (int node = 0; node < this.myGraph.getOrder(); node++) { + if (!this.founded[node]) { + getTree(node); + } + } + return this.treeDFS; + } + + public ArrayList<Integer> topologicalOrder() { + postVisitOrder.clear(); + Arrays.fill(founded, false); + if (myGraph instanceof UndirectedGraph) { + throw new IllegalArgumentException("Graph need to be Directed!"); + } + for (int node = 0; node < this.myGraph.getOrder(); node++) { + if (!founded[node]) { + visitaDFS(node); + } + } + ArrayList<Integer> topologicOrder = postVisitOrder; + Collections.reverse(topologicOrder); + return topologicOrder; + } + + +} diff --git a/Algoritmi_2/Laboratorio/Lab5/src/Main.java b/Algoritmi_2/Laboratorio/Lab5/src/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..3e59c38fbd57497a72e78859efacc67ac75869da --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab5/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/Algoritmi_2/Laboratorio/Lab5/src/NotAllNodesReachedException.java b/Algoritmi_2/Laboratorio/Lab5/src/NotAllNodesReachedException.java new file mode 100644 index 0000000000000000000000000000000000000000..447b97515815389cb252fc8155f94c409c52bdcb --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab5/src/NotAllNodesReachedException.java @@ -0,0 +1,12 @@ +public class NotAllNodesReachedException extends Exception { + + + public NotAllNodesReachedException() { + super(); + } + + public NotAllNodesReachedException(String messaggio) { + super(messaggio); + } + +}