From e19635f6e4441bc62d0319540e45ade96c20d941 Mon Sep 17 00:00:00 2001 From: Gianluca <gianlucamastrolonardo10@gmail.com> Date: Wed, 8 May 2024 20:26:22 +0200 Subject: [PATCH] da finire Lab --- .../Laboratorio/Lab6/.idea/workspace.xml | 5 +- .../Es2/Test/TestAllacciamentoIdrico.java | 5 +- .../Lab7/Es2/src/AllacciamentoIdrico.java | 11 ++-- .../Laboratorio/Lab7/Es2/src/Dijkstra.java | 17 ++++- .../{Lab7 => Lab8/Kruskal}/.gitignore | 0 .../{Lab7 => Lab8/Kruskal}/.idea/.gitignore | 0 .../{Lab7 => Lab8/Kruskal}/.idea/misc.xml | 0 .../{Lab7 => Lab8/Kruskal}/.idea/modules.xml | 2 +- .../{Lab7 => Lab8/Kruskal}/.idea/vcs.xml | 2 +- .../Laboratorio/Lab8/Kruskal/Kruskal.iml | 39 +++++++++++ .../Lab8/Kruskal/Test/KruskalTest.java | 30 +++++++++ .../Laboratorio/Lab8/Kruskal/src/Kruskal.java | 57 ++++++++++++++++ .../Lab8/MSTInterface_PrimKruskal/.gitignore | 29 +++++++++ .../MSTInterface_PrimKruskal/.idea/.gitignore | 3 + .../MSTInterface_PrimKruskal/.idea/misc.xml | 6 ++ .../.idea/modules.xml | 8 +++ .../.idea/uiDesigner.xml | 0 .../MSTInterface_PrimKruskal/.idea/vcs.xml | 6 ++ .../MSTInterface_PrimKruskal.iml | 39 +++++++++++ .../Test/TestKruskal.java | 30 +++++++++ .../Test/TestPrim.java | 30 +++++++++ .../MSTInterface_PrimKruskal/src/Kruskal.java | 61 ++++++++++++++++++ .../MSTInterface_PrimKruskal/src/Main.java | 5 ++ .../MSTInterface_PrimKruskal/src/Prim.java | 57 ++++++++++++++++ Algoritmi_2/Teoria/Clustering.xopp | Bin 0 -> 805 bytes 25 files changed, 425 insertions(+), 17 deletions(-) rename Algoritmi_2/Laboratorio/{Lab7 => Lab8/Kruskal}/.gitignore (100%) rename Algoritmi_2/Laboratorio/{Lab7 => Lab8/Kruskal}/.idea/.gitignore (100%) rename Algoritmi_2/Laboratorio/{Lab7 => Lab8/Kruskal}/.idea/misc.xml (100%) rename Algoritmi_2/Laboratorio/{Lab7 => Lab8/Kruskal}/.idea/modules.xml (61%) rename Algoritmi_2/Laboratorio/{Lab7 => Lab8/Kruskal}/.idea/vcs.xml (66%) create mode 100644 Algoritmi_2/Laboratorio/Lab8/Kruskal/Kruskal.iml create mode 100644 Algoritmi_2/Laboratorio/Lab8/Kruskal/Test/KruskalTest.java create mode 100644 Algoritmi_2/Laboratorio/Lab8/Kruskal/src/Kruskal.java create mode 100644 Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.gitignore create mode 100644 Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/.gitignore create mode 100644 Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/misc.xml create mode 100644 Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/modules.xml rename Algoritmi_2/Laboratorio/{Lab7 => Lab8/MSTInterface_PrimKruskal}/.idea/uiDesigner.xml (100%) create mode 100644 Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/vcs.xml create mode 100644 Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/MSTInterface_PrimKruskal.iml create mode 100644 Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/Test/TestKruskal.java create mode 100644 Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/Test/TestPrim.java create mode 100644 Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/src/Kruskal.java create mode 100644 Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/src/Main.java create mode 100644 Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/src/Prim.java create mode 100644 Algoritmi_2/Teoria/Clustering.xopp diff --git a/Algoritmi_2/Laboratorio/Lab6/.idea/workspace.xml b/Algoritmi_2/Laboratorio/Lab6/.idea/workspace.xml index 4357394..0bf0fac 100644 --- a/Algoritmi_2/Laboratorio/Lab6/.idea/workspace.xml +++ b/Algoritmi_2/Laboratorio/Lab6/.idea/workspace.xml @@ -4,10 +4,7 @@ <option name="autoReloadType" value="SELECTIVE" /> </component> <component name="ChangeListManager"> - <list default="true" id="47e80429-8a37-4045-98c6-d9f72531884e" name="Changes" comment=""> - <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> - <change beforePath="$PROJECT_DIR$/Test/TestVoli.java" beforeDir="false" afterPath="$PROJECT_DIR$/Test/TestVoli.java" afterDir="false" /> - </list> + <list default="true" id="47e80429-8a37-4045-98c6-d9f72531884e" name="Changes" comment="" /> <option name="SHOW_DIALOG" value="false" /> <option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> diff --git a/Algoritmi_2/Laboratorio/Lab7/Es2/Test/TestAllacciamentoIdrico.java b/Algoritmi_2/Laboratorio/Lab7/Es2/Test/TestAllacciamentoIdrico.java index 446e7a4..15d2699 100644 --- a/Algoritmi_2/Laboratorio/Lab7/Es2/Test/TestAllacciamentoIdrico.java +++ b/Algoritmi_2/Laboratorio/Lab7/Es2/Test/TestAllacciamentoIdrico.java @@ -25,11 +25,10 @@ class TestAllacciamentoIdrico { int costoTubo = 3; int puntoAll = 1; allId = new AllacciamentoIdrico(mappa, costoScavo, costoTubo, puntoAll); - System.out.println(allId.progettoComune()); Assertions.assertTrue(allId.progettoComune().hasEdge(0, 3)); - Assertions.assertTrue(!allId.progettoComune().hasEdge(2, 3)); + Assertions.assertFalse(allId.progettoComune().hasEdge(2, 3)); Assertions.assertTrue(allId.progettoProprietari().hasEdge(2, 3)); - Assertions.assertTrue(!allId.progettoProprietari().hasEdge(0, 3)); + Assertions.assertFalse(allId.progettoProprietari().hasEdge(0, 3)); Assertions.assertEquals(2, allId.speseExtraComune()); // Assertions.assertEquals(3, allId.speseExtraProprietario(3)); } diff --git a/Algoritmi_2/Laboratorio/Lab7/Es2/src/AllacciamentoIdrico.java b/Algoritmi_2/Laboratorio/Lab7/Es2/src/AllacciamentoIdrico.java index ba36df3..34a6ae3 100644 --- a/Algoritmi_2/Laboratorio/Lab7/Es2/src/AllacciamentoIdrico.java +++ b/Algoritmi_2/Laboratorio/Lab7/Es2/src/AllacciamentoIdrico.java @@ -3,7 +3,7 @@ import it.uniupo.graphLib.UndirectedGraph; public class AllacciamentoIdrico { - private UndirectedGraph mappa; + private final UndirectedGraph mappa; private final int[][] costoScavo; private final int costoTubo; private final int puntoAllacciamento; @@ -45,11 +45,10 @@ class AllacciamentoIdrico { public int speseExtraComune() { - Dijkstra dijkstra = new Dijkstra(mappaCostiScavi); + //Proprietari: percorso più veloce, dopo aggiungo i costi degli scavi + Dijkstra dijkstra = new Dijkstra(mappa); + //Comune: percorso più economico Prim prim = new Prim(mappaCostiScavi); - System.out.println("Comune: " + dijkstra.getDijkstraSize(puntoAllacciamento)); - System.out.println("Propietari: " + prim.getMSTSize()); - - return dijkstra.getDijkstraSize(puntoAllacciamento) - prim.getMSTSize(); + return dijkstra.getDijkstraSize(puntoAllacciamento, costoScavo) - prim.getMSTSize(); } } diff --git a/Algoritmi_2/Laboratorio/Lab7/Es2/src/Dijkstra.java b/Algoritmi_2/Laboratorio/Lab7/Es2/src/Dijkstra.java index 2c5b158..66281bc 100644 --- a/Algoritmi_2/Laboratorio/Lab7/Es2/src/Dijkstra.java +++ b/Algoritmi_2/Laboratorio/Lab7/Es2/src/Dijkstra.java @@ -62,10 +62,23 @@ class Dijkstra { } public - int getDijkstraSize(int sorg) { + int getDijkstraSize(int sorg, int[][] costoScavi) { Arrays.fill(founded, false); UndirectedGraph dijkstraTree = (UndirectedGraph) myGraph.create(); - return dijkstra(sorg, dijkstraTree); + dijkstra(sorg, dijkstraTree); + int size = 0; + for (int i = 0; i < costoScavi.length; ++i) { + for(int j = 0; j < costoScavi[i].length;++j){ + if(i < j) { + if (costoScavi[i][j] != -1 && dijkstraTree.hasEdge(i, j)) { + dijkstraTree.removeEdge(i, j); + dijkstraTree.addEdge(i, j, costoScavi[i][j]); + size += costoScavi[i][j]; + } + } + } + } + return size; } diff --git a/Algoritmi_2/Laboratorio/Lab7/.gitignore b/Algoritmi_2/Laboratorio/Lab8/Kruskal/.gitignore similarity index 100% rename from Algoritmi_2/Laboratorio/Lab7/.gitignore rename to Algoritmi_2/Laboratorio/Lab8/Kruskal/.gitignore diff --git a/Algoritmi_2/Laboratorio/Lab7/.idea/.gitignore b/Algoritmi_2/Laboratorio/Lab8/Kruskal/.idea/.gitignore similarity index 100% rename from Algoritmi_2/Laboratorio/Lab7/.idea/.gitignore rename to Algoritmi_2/Laboratorio/Lab8/Kruskal/.idea/.gitignore diff --git a/Algoritmi_2/Laboratorio/Lab7/.idea/misc.xml b/Algoritmi_2/Laboratorio/Lab8/Kruskal/.idea/misc.xml similarity index 100% rename from Algoritmi_2/Laboratorio/Lab7/.idea/misc.xml rename to Algoritmi_2/Laboratorio/Lab8/Kruskal/.idea/misc.xml diff --git a/Algoritmi_2/Laboratorio/Lab7/.idea/modules.xml b/Algoritmi_2/Laboratorio/Lab8/Kruskal/.idea/modules.xml similarity index 61% rename from Algoritmi_2/Laboratorio/Lab7/.idea/modules.xml rename to Algoritmi_2/Laboratorio/Lab8/Kruskal/.idea/modules.xml index ff8ca85..09650db 100644 --- a/Algoritmi_2/Laboratorio/Lab7/.idea/modules.xml +++ b/Algoritmi_2/Laboratorio/Lab8/Kruskal/.idea/modules.xml @@ -2,7 +2,7 @@ <project version="4"> <component name="ProjectModuleManager"> <modules> - <module fileurl="file://$PROJECT_DIR$/Lab7.iml" filepath="$PROJECT_DIR$/Lab7.iml" /> + <module fileurl="file://$PROJECT_DIR$/Kruskal.iml" filepath="$PROJECT_DIR$/Kruskal.iml" /> </modules> </component> </project> \ No newline at end of file diff --git a/Algoritmi_2/Laboratorio/Lab7/.idea/vcs.xml b/Algoritmi_2/Laboratorio/Lab8/Kruskal/.idea/vcs.xml similarity index 66% rename from Algoritmi_2/Laboratorio/Lab7/.idea/vcs.xml rename to Algoritmi_2/Laboratorio/Lab8/Kruskal/.idea/vcs.xml index c2365ab..4fce1d8 100644 --- a/Algoritmi_2/Laboratorio/Lab7/.idea/vcs.xml +++ b/Algoritmi_2/Laboratorio/Lab8/Kruskal/.idea/vcs.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="VcsDirectoryMappings"> - <mapping directory="$PROJECT_DIR$/../../.." vcs="Git" /> + <mapping directory="$PROJECT_DIR$/../../../.." vcs="Git" /> </component> </project> \ No newline at end of file diff --git a/Algoritmi_2/Laboratorio/Lab8/Kruskal/Kruskal.iml b/Algoritmi_2/Laboratorio/Lab8/Kruskal/Kruskal.iml new file mode 100644 index 0000000..c75e903 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/Kruskal/Kruskal.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="jdk" jdkName="openjdk-21" jdkType="JavaSDK" /> + <orderEntry type="sourceFolder" forTests="false" /> + <orderEntry type="module-library" exported=""> + <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/Lab8/Kruskal/Test/KruskalTest.java b/Algoritmi_2/Laboratorio/Lab8/Kruskal/Test/KruskalTest.java new file mode 100644 index 0000000..971349d --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/Kruskal/Test/KruskalTest.java @@ -0,0 +1,30 @@ +import it.uniupo.graphLib.UndirectedGraph; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class KruskalTest { + @Test + void testNullCostructor() { + UndirectedGraph g = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1"); + Kruskal kruskal = new Kruskal(g); + Assertions.assertNotNull(kruskal); + } + + @Test + void testGetMST() { + UndirectedGraph g = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1"); + Kruskal kruskal = new Kruskal(g); + UndirectedGraph MST = kruskal.getMST(); + Assertions.assertTrue(MST.hasEdge(0, 3)); + Assertions.assertTrue(MST.hasEdge(3, 2)); + Assertions.assertTrue(MST.hasEdge(2, 1)); + Assertions.assertFalse(MST.hasEdge(0, 1)); + } + + @Test + void testGetCost() { + UndirectedGraph g = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1"); + Kruskal kruskal = new Kruskal(g); + Assertions.assertEquals(6, kruskal.getCost()); + } +} diff --git a/Algoritmi_2/Laboratorio/Lab8/Kruskal/src/Kruskal.java b/Algoritmi_2/Laboratorio/Lab8/Kruskal/src/Kruskal.java new file mode 100644 index 0000000..138a3d1 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/Kruskal/src/Kruskal.java @@ -0,0 +1,57 @@ +import it.uniupo.algoTools.MinHeap; +import it.uniupo.algoTools.UnionByRank; +import it.uniupo.algoTools.UnionFind; +import it.uniupo.graphLib.Edge; +import it.uniupo.graphLib.UndirectedGraph; +import it.uniupo.graphLib.*; + + +public class Kruskal { + private final UndirectedGraph myGraph; + + public Kruskal(UndirectedGraph g) { + myGraph = g; + } + + private int kruskal(UndirectedGraph MST) { + UndirectedGraph myGraphCopy = myGraph; + MinHeap<Edge, Integer> minHeap = new MinHeap<>(); + int cost = 0; + + for (int i = 0; i < myGraph.getOrder(); ++i) { + for (Edge e : myGraphCopy.getOutEdges(i)) { + minHeap.add(e, e.getWeight()); + myGraphCopy.removeEdge(e.getTail(), e.getHead()); + } + } + + UnionFind unionFind = new UnionByRank(myGraph.getOrder()); + + while (!minHeap.isEmpty()) { + Edge e = minHeap.extractMin(); + int tail = e.getTail(); + int head = e.getHead(); + int tailLeader = unionFind.find(tail); + int headLeader = unionFind.find(head); + + if (tailLeader != headLeader) { + MST.addEdge(e); + cost += e.getWeight(); + unionFind.union(tailLeader, headLeader); + } + } + return cost; + } + + public UndirectedGraph getMST() { + UndirectedGraph MST = (UndirectedGraph) myGraph.create(); + kruskal(MST); + return MST; + } + + public int getCost() { + UndirectedGraph MST = (UndirectedGraph) myGraph.create(); + return kruskal(MST); + } + +} diff --git a/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.gitignore b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.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/Lab8/MSTInterface_PrimKruskal/.idea/.gitignore b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/misc.xml b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/misc.xml new file mode 100644 index 0000000..69ace3f --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.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/Lab8/MSTInterface_PrimKruskal/.idea/modules.xml b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/modules.xml new file mode 100644 index 0000000..afef18c --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.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$/MSTInterface_PrimKruskal.iml" filepath="$PROJECT_DIR$/MSTInterface_PrimKruskal.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/Algoritmi_2/Laboratorio/Lab7/.idea/uiDesigner.xml b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/uiDesigner.xml similarity index 100% rename from Algoritmi_2/Laboratorio/Lab7/.idea/uiDesigner.xml rename to Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/uiDesigner.xml diff --git a/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/vcs.xml b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.idea/vcs.xml new file mode 100644 index 0000000..4fce1d8 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/.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/Lab8/MSTInterface_PrimKruskal/MSTInterface_PrimKruskal.iml b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/MSTInterface_PrimKruskal.iml new file mode 100644 index 0000000..9aa92e9 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/MSTInterface_PrimKruskal.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/Lab8/MSTInterface_PrimKruskal/Test/TestKruskal.java b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/Test/TestKruskal.java new file mode 100644 index 0000000..7e15e13 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/Test/TestKruskal.java @@ -0,0 +1,30 @@ +import it.uniupo.graphLib.UndirectedGraph; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TestKruskal { + @Test + void testNullCostructor() { + UndirectedGraph g = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1"); + Kruskal kruskal = new Kruskal(g); + Assertions.assertNotNull(kruskal); + } + + @Test + void testGetMST() { + UndirectedGraph g = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1"); + Kruskal kruskal = new Kruskal(g); + UndirectedGraph MST = kruskal.getMST(); + Assertions.assertTrue(MST.hasEdge(0, 3)); + Assertions.assertTrue(MST.hasEdge(3, 2)); + Assertions.assertTrue(MST.hasEdge(2, 1)); + Assertions.assertFalse(MST.hasEdge(0, 1)); + } + + @Test + void testGetCost() { + UndirectedGraph g = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1"); + Kruskal kruskal = new Kruskal(g); + Assertions.assertEquals(6, kruskal.getCost()); + } +} diff --git a/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/Test/TestPrim.java b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/Test/TestPrim.java new file mode 100644 index 0000000..3a86291 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/Test/TestPrim.java @@ -0,0 +1,30 @@ +import it.uniupo.graphLib.UndirectedGraph; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TestPrim { + @Test + void testNullCostructor() { + UndirectedGraph g = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1"); + Prim prim = new Prim(g); + Assertions.assertNotNull(prim); + } + + @Test + void testGetMST() { + UndirectedGraph g = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1"); + Prim prim = new Prim(g); + UndirectedGraph MST = prim.getMST(); + Assertions.assertTrue(MST.hasEdge(0, 3)); + Assertions.assertTrue(MST.hasEdge(3, 2)); + Assertions.assertTrue(MST.hasEdge(2, 1)); + Assertions.assertFalse(MST.hasEdge(0, 1)); + } + + @Test + void testGetCost() { + UndirectedGraph g = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1"); + Prim prim = new Prim(g); + Assertions.assertEquals(6, prim.getCost()); + } +} diff --git a/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/src/Kruskal.java b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/src/Kruskal.java new file mode 100644 index 0000000..641f050 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/src/Kruskal.java @@ -0,0 +1,61 @@ +import it.uniupo.algoTools.MST; +import it.uniupo.algoTools.MinHeap; +import it.uniupo.algoTools.UnionByRank; +import it.uniupo.algoTools.UnionFind; +import it.uniupo.graphLib.Edge; +import it.uniupo.graphLib.UndirectedGraph; + +public class Kruskal implements MST { + private final UndirectedGraph myGraph; + + public Kruskal(UndirectedGraph g) { + myGraph = g; + } + + private int kruskal(UndirectedGraph MST) { + UndirectedGraph myGraphCopy = myGraph; + MinHeap<Edge, Integer> minHeap = new MinHeap<>(); + int cost = 0; + + for (int i = 0; i < myGraph.getOrder(); ++i) { + for (Edge e : myGraphCopy.getOutEdges(i)) { + minHeap.add(e, e.getWeight()); + myGraphCopy.removeEdge(e.getHead(), e.getTail()); + } + } + UnionFind unionFind = new UnionByRank(myGraph.getOrder()); + + while (!minHeap.isEmpty()) { + Edge e = minHeap.extractMin(); + int tail = e.getTail(); + int head = e.getHead(); + int tailLeader = unionFind.find(tail); + int headLeader = unionFind.find(head); + if (tailLeader != headLeader) { + MST.addEdge(e); + cost += e.getWeight(); + unionFind.union(tailLeader, headLeader); + } + } + return cost; + } + + + @Override + public MST create(UndirectedGraph undirectedGraph) { + return new Kruskal(myGraph); + } + + @Override + public UndirectedGraph getMST() { + UndirectedGraph MST = (UndirectedGraph) myGraph.create(); + kruskal(MST); + return MST; + } + + @Override + public int getCost() { + UndirectedGraph MST = (UndirectedGraph) myGraph.create(); + return kruskal(MST); + } +} diff --git a/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/src/Main.java b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/src/Main.java new file mode 100644 index 0000000..3e59c38 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/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/Lab8/MSTInterface_PrimKruskal/src/Prim.java b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/src/Prim.java new file mode 100644 index 0000000..eafed1b --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/MSTInterface_PrimKruskal/src/Prim.java @@ -0,0 +1,57 @@ +import it.uniupo.algoTools.MST; +import it.uniupo.algoTools.MinHeap; +import it.uniupo.graphLib.Edge; +import it.uniupo.graphLib.UndirectedGraph; + +public class Prim implements MST { + private final UndirectedGraph myGraph; + private boolean[] founded; + + public Prim(UndirectedGraph g) { + myGraph = g; + founded = new boolean[myGraph.getOrder()]; + } + + private int prim(int sorg, UndirectedGraph MST) { + founded[sorg] = true; + MinHeap<Edge, Integer> minHeap = new MinHeap<>(); + int cost = 0; + + for (Edge e : myGraph.getOutEdges(sorg)) { + minHeap.add(e, e.getWeight()); + } + + while (!minHeap.isEmpty()) { + Edge e = minHeap.extractMin(); + int v = e.getHead(); + if (!founded[v]) { + founded[v] = true; + MST.addEdge(e); + cost += e.getWeight(); + for (Edge w : myGraph.getOutEdges(v)) { + minHeap.add(w, w.getWeight()); + } + } + } + return cost; + } + + + @Override + public MST create(UndirectedGraph undirectedGraph) { + return new Prim(myGraph); + } + + @Override + public UndirectedGraph getMST() { + UndirectedGraph MST = (UndirectedGraph) myGraph.create(); + prim(0, MST); + return MST; + } + + @Override + public int getCost() { + UndirectedGraph MST = (UndirectedGraph) myGraph.create(); + return prim(0, MST); + } +} diff --git a/Algoritmi_2/Teoria/Clustering.xopp b/Algoritmi_2/Teoria/Clustering.xopp new file mode 100644 index 0000000000000000000000000000000000000000..def74270d8cee256eaf0510897c43f9bf23be037 GIT binary patch literal 805 zcmV+=1KRu_iwFP!000001ASD<j-yBrz1LSD=7`2@2E)j%7Pj%qW^BOb#%7>MNR%Yl z@b!(SYr0k4(nK8cWvr=CzL<Y|mVoFI)a0;QknDsZi3SH%8~|7)3$lXbxA(XC6Sk-d z01=@izz{9S>kk7(5$t4ClDOv}`B`R4jyX8TAbI~47{jP7lL`}m6HSs38OC+<4^5{z z&f0Ji!IJ*$QrExGoc>YfZC<0K%ag}@?k-LH!5p_KR7Nq*PNq8Ph&HeLYSjJ9u7*?* zy}+_OXf|_F=*OB?EGq4S<njt!sX#rs_OvqjkmC^2m#fV!0v_MV@>J@ZYx8WS<L<U9 z24C-3c7KDuo1uykGA1~vg?I<jwH0IE3%LaGXI+RvAF+(gS*5$l5$pTl7+jgzfd#Wp zq-3`A)pJ@KvZcp$g{5nz+Sc>Yb&2y#BzfSCwni<_X8Ymb@E5FObvd9@dJ-6@=&`6O zJF^q%HJYtLJJ@BBu{tu&&cv(bx;m%IlZPk$elqbxwy>tLZsB3k^2$2(p%j`fMO)TJ zXo^BJg#<Pfmm2_s6%IQ~;(IC<Lf6aP7*w3I^<^O}!>BdbjhB}gC=veBM~Ormhs%#$ zFqo@Hv;naG-Jju`t%x&zcA<S!!g-71i%~91x=F=rev=uQzdWh+Zdm%kLA!cGcQ^G; zqBO1aaU5z5lzPbD8x0?hLEuP%vHen?S#SGw88D^fX47zeI^#e5%(~lJQ0&~{*gLl} zych?*t2}R<aC|9H8+x8tma!YK&pR#e2d-2LRQuN+s(;LNZ`L1nnElV(tMhK_aneH` z^j+U|dY~J-u=EQ!>*&jE;6BnO_d#wK4HBHK_T{bnt=Uo_wGsB|$4sNduI*#N-&E2x zvhK`axmO9$M_M^DcCWP5@nRS0zrXunJA#UeLz<?G#hm_+B_m&gG$9^&jI#wP$ijr1 z34+WrBfqmGPcytAr71g+BzY!IN9&jo22qhB*j6!t`+6jQ13?~>L<GT$@_*ew<+UMt jF#1yz)EP;SsUYYR^aAv&?d$wx@cY|$jkk2GVgvvHda;>N literal 0 HcmV?d00001 -- GitLab