diff --git a/Algoritmi_2/Laboratorio/Lab8/Hiking/.gitignore b/Algoritmi_2/Laboratorio/Lab8/Hiking/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..f68d1099657e34d4e7a68aadc730b3ecad84667d --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/Hiking/.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/Hiking/.idea/.gitignore b/Algoritmi_2/Laboratorio/Lab8/Hiking/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..26d33521af10bcc7fd8cea344038eaaeb78d0ef5 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/Hiking/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Algoritmi_2/Laboratorio/Lab8/Hiking/.idea/misc.xml b/Algoritmi_2/Laboratorio/Lab8/Hiking/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..69ace3f6affaf674f40a55887f3d6f3564afd626 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/Hiking/.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/Hiking/.idea/modules.xml b/Algoritmi_2/Laboratorio/Lab8/Hiking/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..2fb8e2e915873580d8c1156ae8c1fcd1bd9e12ff --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/Hiking/.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$/Hiking.iml" filepath="$PROJECT_DIR$/Hiking.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/Algoritmi_2/Laboratorio/Lab8/Hiking/.idea/vcs.xml b/Algoritmi_2/Laboratorio/Lab8/Hiking/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..4fce1d86b49521afe1cee4ed1c13b6396ebbc6f3 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/Hiking/.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/Hiking/Hiking.iml b/Algoritmi_2/Laboratorio/Lab8/Hiking/Hiking.iml new file mode 100644 index 0000000000000000000000000000000000000000..9aa92e91148f2596fbd6a72b38da2b1073d984c6 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/Hiking/Hiking.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/Hiking/Test/HikingTest.java b/Algoritmi_2/Laboratorio/Lab8/Hiking/Test/HikingTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9f0436c021d16a168c0eb772ea7fdacb9a77d207 --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/Hiking/Test/HikingTest.java @@ -0,0 +1,43 @@ +import it.uniupo.graphLib.UndirectedGraph; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +public class HikingTest { + + @Test + void testMinDistanza() { + UndirectedGraph graph = new UndirectedGraph("5; 0 1 13; 0 2 16; 0 4 9; 3 0 19; 3 1 14; 3 2 12; 3 4 26; 1 4 22; 1 2 7; 4 2 15"); + + boolean[][] map = new boolean[graph.getOrder()][graph.getOrder()]; + for (int i = 0; i < map.length; ++i) { + Arrays.fill(map[i], true); + } + // System.out.println(graph); + Hikes hikesTest = new Hikes(graph, map); + Assertions.assertEquals(12, hikesTest.minDistanza(3)); + Assertions.assertEquals(13, hikesTest.minDistanza(2)); + + Assertions.assertTrue(hikesTest.minDistanza(2) > hikesTest.minDistanza(graph.getOrder())); + } + + @Test + void testMinDistanzaNonPercorribili() { + UndirectedGraph graph = new UndirectedGraph("5; 0 1 13; 0 2 16; 0 4 9; 3 0 19; 3 1 14; 3 2 12; 3 4 26; 1 4 22; 1 2 7; 4 2 15"); + + boolean[][] map = new boolean[graph.getOrder()][graph.getOrder()]; + for (int i = 0; i < map.length; ++i) { + Arrays.fill(map[i], true); + } + map[0][1] = false; + map[3][2] = false; + + + Hikes hikesTest = new Hikes(graph, map); + Assertions.assertEquals(14, hikesTest.minDistanza(3)); + Assertions.assertEquals(15, hikesTest.minDistanza(2)); + + Assertions.assertTrue(hikesTest.minDistanza(2) > hikesTest.minDistanza(graph.getOrder())); + } +} diff --git a/Algoritmi_2/Laboratorio/Lab8/Hiking/src/Hikes.java b/Algoritmi_2/Laboratorio/Lab8/Hiking/src/Hikes.java new file mode 100644 index 0000000000000000000000000000000000000000..a320ba16ac72ceb294adaf7f05b08057090dc34c --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/Hiking/src/Hikes.java @@ -0,0 +1,68 @@ +import it.uniupo.algoTools.MinHeap; +import it.uniupo.algoTools.UnionByRank; +import it.uniupo.algoTools.UnionFind; +import it.uniupo.graphLib.*; + +public class Hikes { + + UndirectedGraph myGraph; + + public Hikes(UndirectedGraph rifugi, boolean[][] percorribile) { + for (int i = 0; i < percorribile.length; ++i) { + for (int j = 0; j < percorribile[i].length; ++j) { + if (!percorribile[i][j] && rifugi.hasEdge(i, j)) { + rifugi.removeEdge(i, j); + } + } + } + myGraph = rifugi; + } + + private int kruskalClustering(int maxCluster) { + UnionFind unionByRank = new UnionByRank(myGraph.getOrder()); + MinHeap<Edge, Integer> minHeap = new MinHeap<>(); + + //UndirectedGraph copyGraph = myGraph; + + for (int i = 0; i < myGraph.getOrder(); ++i) { + for (Edge e : myGraph.getOutEdges(i)) { + minHeap.add(e, e.getWeight()); + // copyGraph.removeEdge(e.getTail(), e.getHead()); + } + } + + while (!minHeap.isEmpty() && unionByRank.getNumberOfSets() > maxCluster) { + Edge edge = minHeap.extractMin(); + int leaderTail = unionByRank.find(edge.getTail()); + int leaderHead = unionByRank.find(edge.getHead()); + if (leaderHead != leaderTail) { + unionByRank.union(leaderHead, leaderTail); + } + } + + //Calcolo lo spaziamento + while (!minHeap.isEmpty()) { + Edge edge = minHeap.extractMin(); + int leaderTail = unionByRank.find(edge.getTail()); + int leaderHead = unionByRank.find(edge.getHead()); + if (leaderHead != leaderTail) { + return edge.getWeight(); + } + } + + //Se numGite è 0 o 1 + return -1; + } + + public int minDistanza(int numGite) { + if (numGite < 0) { + throw new IllegalArgumentException("Il numero delle gite deve essere > 0"); + } + return kruskalClustering(numGite); + + } + + public void printGraph() { + System.out.println(myGraph); + } +} diff --git a/Algoritmi_2/Laboratorio/Lab8/Hiking/src/Main.java b/Algoritmi_2/Laboratorio/Lab8/Hiking/src/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..3e59c38fbd57497a72e78859efacc67ac75869da --- /dev/null +++ b/Algoritmi_2/Laboratorio/Lab8/Hiking/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