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

da finire Lab

parent 429f378a
No related branches found
No related tags found
No related merge requests found
Showing
with 272 additions and 17 deletions
...@@ -4,10 +4,7 @@ ...@@ -4,10 +4,7 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="47e80429-8a37-4045-98c6-d9f72531884e" name="Changes" comment=""> <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>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
......
...@@ -25,11 +25,10 @@ class TestAllacciamentoIdrico { ...@@ -25,11 +25,10 @@ class TestAllacciamentoIdrico {
int costoTubo = 3; int costoTubo = 3;
int puntoAll = 1; int puntoAll = 1;
allId = new AllacciamentoIdrico(mappa, costoScavo, costoTubo, puntoAll); allId = new AllacciamentoIdrico(mappa, costoScavo, costoTubo, puntoAll);
System.out.println(allId.progettoComune());
Assertions.assertTrue(allId.progettoComune().hasEdge(0, 3)); 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(2, 3));
Assertions.assertTrue(!allId.progettoProprietari().hasEdge(0, 3)); Assertions.assertFalse(allId.progettoProprietari().hasEdge(0, 3));
Assertions.assertEquals(2, allId.speseExtraComune()); Assertions.assertEquals(2, allId.speseExtraComune());
// Assertions.assertEquals(3, allId.speseExtraProprietario(3)); // Assertions.assertEquals(3, allId.speseExtraProprietario(3));
} }
......
...@@ -3,7 +3,7 @@ import it.uniupo.graphLib.UndirectedGraph; ...@@ -3,7 +3,7 @@ import it.uniupo.graphLib.UndirectedGraph;
public public
class AllacciamentoIdrico { class AllacciamentoIdrico {
private UndirectedGraph mappa; private final UndirectedGraph mappa;
private final int[][] costoScavo; private final int[][] costoScavo;
private final int costoTubo; private final int costoTubo;
private final int puntoAllacciamento; private final int puntoAllacciamento;
...@@ -45,11 +45,10 @@ class AllacciamentoIdrico { ...@@ -45,11 +45,10 @@ class AllacciamentoIdrico {
public public
int speseExtraComune() { 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); Prim prim = new Prim(mappaCostiScavi);
System.out.println("Comune: " + dijkstra.getDijkstraSize(puntoAllacciamento)); return dijkstra.getDijkstraSize(puntoAllacciamento, costoScavo) - prim.getMSTSize();
System.out.println("Propietari: " + prim.getMSTSize());
return dijkstra.getDijkstraSize(puntoAllacciamento) - prim.getMSTSize();
} }
} }
...@@ -62,10 +62,23 @@ class Dijkstra { ...@@ -62,10 +62,23 @@ class Dijkstra {
} }
public public
int getDijkstraSize(int sorg) { int getDijkstraSize(int sorg, int[][] costoScavi) {
Arrays.fill(founded, false); Arrays.fill(founded, false);
UndirectedGraph dijkstraTree = (UndirectedGraph) myGraph.create(); 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;
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<project version="4"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <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> </modules>
</component> </component>
</project> </project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" /> <mapping directory="$PROJECT_DIR$/../../../.." vcs="Git" />
</component> </component>
</project> </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="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
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());
}
}
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);
}
}
### 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$/MSTInterface_PrimKruskal.iml" filepath="$PROJECT_DIR$/MSTInterface_PrimKruskal.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;
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());
}
}
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