diff --git a/Algoritmi_2/Laboratorio/Lab6/.idea/workspace.xml b/Algoritmi_2/Laboratorio/Lab6/.idea/workspace.xml index 4357394aeedd13b8bfe26e99b264bed54aa5abaa..0bf0fac3633bc6fd0bbcbafb1909a8443d2e77c6 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 446e7a473bef7aa5dac086d40f2bb5dc4ebf3645..15d2699480966e09a6bcd6353967ddc6b2ea4a65 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 ba36df309442da9b1cdcc935db5f7a6b5ec8991e..34a6ae37f21fc7616f21a1bff10b82e3aed508e9 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 2c5b158431e8b791a69f3674ed9e477e167e1208..66281bc88c6c219e6da4d50cf4f8e09a3f31f5f1 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 ff8ca85d503083e6ba84bb088ed3d39b49e20f95..09650dbed19d43b9b3529bb6fb6abd0d840c9de7 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 c2365ab11f9ba6b763735c8fd976420234bb3521..4fce1d86b49521afe1cee4ed1c13b6396ebbc6f3 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 0000000000000000000000000000000000000000..c75e903a60cf6808ca8cf172a5a4018f8e898c44 --- /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 0000000000000000000000000000000000000000..971349d1c7b7ae0458a36105964048b6aa016629 --- /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 0000000000000000000000000000000000000000..138a3d11a488e96912094fb55d342d2d6d2ff985 --- /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 0000000000000000000000000000000000000000..f68d1099657e34d4e7a68aadc730b3ecad84667d --- /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 0000000000000000000000000000000000000000..26d33521af10bcc7fd8cea344038eaaeb78d0ef5 --- /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 0000000000000000000000000000000000000000..69ace3f6affaf674f40a55887f3d6f3564afd626 --- /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 0000000000000000000000000000000000000000..afef18ca9192493c83a332f80dee0733f9164687 --- /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 0000000000000000000000000000000000000000..4fce1d86b49521afe1cee4ed1c13b6396ebbc6f3 --- /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 0000000000000000000000000000000000000000..9aa92e91148f2596fbd6a72b38da2b1073d984c6 --- /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 0000000000000000000000000000000000000000..7e15e13fe59c93834baf2098a94edee47d5f3a4d --- /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 0000000000000000000000000000000000000000..3a8629142a9937b89b200fe8124e9ca2a1a321a9 --- /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 0000000000000000000000000000000000000000..641f0503335f6e42d50805ab7b107629e1fcab46 --- /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 0000000000000000000000000000000000000000..3e59c38fbd57497a72e78859efacc67ac75869da --- /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 0000000000000000000000000000000000000000..eafed1bb6dbd69e890120eb77e0f02ae9e616c27 --- /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 Binary files /dev/null and b/Algoritmi_2/Teoria/Clustering.xopp differ