Skip to content
Snippets Groups Projects
Commit a9683a3f authored by Gianluca's avatar Gianluca
Browse files

piango

parent 8ef05cba
No related branches found
No related tags found
No related merge requests found
### 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
# Default ignored files
/shelf/
/workspace.xml
<?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
<?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
<?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
<?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>
<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
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()));
}
@Test
public void test3() {
UndirectedGraph rifugi = new UndirectedGraph("5; 0 1 13; 0 2 16; 0 3 19; 0 4 9; 1 2 7; 1 3 14; 1 4 22; 2 3 12; 2 4 15; 3 4 26");
boolean[][] percorribile = new boolean[5][5];
Arrays.fill(percorribile[0], true);
Arrays.fill(percorribile[1], true);
Arrays.fill(percorribile[2], true);
Arrays.fill(percorribile[3], true);
Arrays.fill(percorribile[4], true);
Hikes hikes;
hikes = new Hikes(rifugi, percorribile);
Assertions.assertEquals(12, hikes.minDistanza(3));
Assertions.assertEquals(13, hikes.minDistanza(2));
percorribile[0][1] = false;
percorribile[1][0] = false;
percorribile[2][3] = false;
percorribile[3][2] = false;
hikes = new Hikes(rifugi, percorribile);
Assertions.assertEquals(14, hikes.minDistanza(3));
Assertions.assertEquals(15, hikes.minDistanza(2));
}
}
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 Hikes {
private UndirectedGraph myGraph;
private final int orderMyGraph;
private final boolean[][] percorribile;
public Hikes(UndirectedGraph rifugi, boolean[][] percorribile) {
myGraph = rifugi;
orderMyGraph = myGraph.getOrder();
this.percorribile = percorribile;
}
private int kruslkalSpacing(int k) {
UnionByRank unionByRank = new UnionByRank(orderMyGraph);
MinHeap<Edge, Integer> minHeap = new MinHeap<>();
for (int i = 0; i < orderMyGraph; i++) {
for (Edge e : myGraph.getOutEdges(i)) {
minHeap.add(e, e.getWeight());
}
}
while (!minHeap.isEmpty() && unionByRank.getNumberOfSets() > k) {
Edge e = minHeap.extractMin();
int headLeader = unionByRank.find(e.getHead());
int tailLeader = unionByRank.find(e.getTail());
if (headLeader != tailLeader) {
unionByRank.union(headLeader, tailLeader);
}
}
//Trovo Spaziamento
int spaziamento = -1;
boolean isValidEdge = false;
while (!minHeap.isEmpty() && !isValidEdge) {
Edge e = minHeap.extractMin();
int headLeader = unionByRank.find(e.getHead());
int tailLeader = unionByRank.find(e.getTail());
if (headLeader != tailLeader) {
isValidEdge = true;
spaziamento = e.getWeight();
}
}
return spaziamento;
}
public int minDistanza(int numGite) {
if (numGite < 2 || numGite > orderMyGraph) throw new IllegalArgumentException("Num Gite errato");
for (int i = 0; i < percorribile.length; i++) {
for (int j = 0; j < percorribile[i].length; j++) {
if (!percorribile[i][j]) {
myGraph.removeEdge(i, j);
}
}
}
return kruslkalSpacing(numGite);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment