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

fatto es hiking

parent 70f36d83
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>
</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()));
}
}
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);
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ 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