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

da finire

parent b2601ba9
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,8 @@ class TestAllacciamentoIdrico {
AllacciamentoIdrico allId;
@Test
public void testCreate() {
public
void testCreate() {
UndirectedGraph mappa = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1");
int[][] costoScavo = {{0, 6, -1, 8}, {6, 0, 7, -1}, {-1, 7, 0, 10}, {8, -1, 10, 0}};
int costoTubo = 3;
......@@ -17,7 +18,8 @@ class TestAllacciamentoIdrico {
}
@Test
public void test01() {
public
void test01() {
UndirectedGraph mappa = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1");
int[][] costoScavo = {{0, 6, -1, 8}, {6, 0, 7, -1}, {-1, 7, 0, 10}, {8, -1, 10, 0}};
int costoTubo = 3;
......@@ -28,7 +30,7 @@ class TestAllacciamentoIdrico {
Assertions.assertTrue(!allId.progettoComune().hasEdge(2, 3));
Assertions.assertTrue(allId.progettoProprietari().hasEdge(2, 3));
Assertions.assertTrue(!allId.progettoProprietari().hasEdge(0, 3));
//Assertions.assertEquals(2, allId.speseExtraComune());
Assertions.assertEquals(2, allId.speseExtraComune());
// Assertions.assertEquals(3, allId.speseExtraProprietario(3));
}
......
import it.uniupo.graphLib.Edge;
import it.uniupo.graphLib.UndirectedGraph;
public class AllacciamentoIdrico {
public
class AllacciamentoIdrico {
private UndirectedGraph mappa;
private final int[][] costoScavo;
private final int costoTubo;
private final int puntoAllacciamento;
private final UndirectedGraph mappaCostiScavi;
public AllacciamentoIdrico(UndirectedGraph mappa, int[][] costoScavo, int costoTubo, int puntoAllacciamento) {
public
AllacciamentoIdrico(UndirectedGraph mappa, int[][] costoScavo, int costoTubo, int puntoAllacciamento) {
this.mappa = mappa;
this.costoScavo = costoScavo;
this.costoTubo = costoTubo;
this.puntoAllacciamento = puntoAllacciamento;
mappaCostiScavi = (UndirectedGraph) mappa.create();
for (int i = 0; i < costoScavo.length; ++i) {
for (int j = 0; j < costoScavo[i].length; ++j) {
if (i != j && costoScavo[i][j] != -1) {
if (!mappaCostiScavi.hasEdge(i, j)) {
mappaCostiScavi.addEdge(i, j, costoScavo[i][j]);
}
}
}
}
}
//Ai proprietari conviene Dijkstra, perché hanno il cammino minimo
......@@ -21,19 +35,21 @@ public class AllacciamentoIdrico {
return dijkstra.getDijkstraTree(puntoAllacciamento);
}
public
UndirectedGraph progettoComune() {
//Uso l'MST ma non usi pesi normali ma uso il costo dello scavo
UndirectedGraph mappaCostiScavi = (UndirectedGraph) mappa.create();
for (int i = 0; i < costoScavo.length; ++i) {
for (int j = 0; j < costoScavo[i].length; ++j) {
if (i != j && costoScavo[i][j] != -1) {
if (!mappaCostiScavi.hasEdge(i, j)) {
mappaCostiScavi.addEdge(i, j, costoScavo[i][j]);
}
}
}
}
Prim prim = new Prim(mappaCostiScavi);
return prim.getMST();
}
public
int speseExtraComune() {
Dijkstra dijkstra = new Dijkstra(mappaCostiScavi);
Prim prim = new Prim(mappaCostiScavi);
System.out.println("Comune: " + dijkstra.getDijkstraSize(puntoAllacciamento));
System.out.println("Propietari: " + prim.getMSTSize());
return dijkstra.getDijkstraSize(puntoAllacciamento) - prim.getMSTSize();
}
}
......@@ -9,19 +9,22 @@ import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class Dijkstra {
public
class Dijkstra {
private UndirectedGraph myGraph;
private boolean[] founded;
public Dijkstra(UndirectedGraph graph) {
public
Dijkstra(UndirectedGraph graph) {
this.myGraph = graph;
this.founded = new boolean[this.myGraph.getOrder()];
}
public UndirectedGraph getDijkstraTree(int sorg) {
UndirectedGraph dijkstraTree = (UndirectedGraph) myGraph.create();
private
int dijkstra(int sorg, UndirectedGraph dijkstraTree) {
int size = 0;
int[] distance = new int[myGraph.getOrder()];
Arrays.fill(distance, -1);
......@@ -40,14 +43,30 @@ public class Dijkstra {
if (!founded[nodeW]) {
founded[nodeW] = true;
distance[nodeW] = distance[nodeU] + heapReturnedEdge.getWeight();
size += heapReturnedEdge.getWeight();
dijkstraTree.addEdge(heapReturnedEdge);
for (Edge e : myGraph.getOutEdges(nodeW)) {
heap.add(e, distance[nodeW] + e.getWeight());
}
}
}
return size;
}
public
UndirectedGraph getDijkstraTree(int sorg) {
Arrays.fill(founded, false);
UndirectedGraph dijkstraTree = (UndirectedGraph) myGraph.create();
dijkstra(sorg, dijkstraTree);
return dijkstraTree;
}
public
int getDijkstraSize(int sorg) {
Arrays.fill(founded, false);
UndirectedGraph dijkstraTree = (UndirectedGraph) myGraph.create();
return dijkstra(sorg, dijkstraTree);
}
}
\ No newline at end of file
......@@ -5,17 +5,20 @@ import it.uniupo.graphLib.UndirectedGraph;
import java.util.Arrays;
public class Prim {
public
class Prim {
private UndirectedGraph myGraph;
private boolean[] founded;
public Prim(UndirectedGraph g) {
public
Prim(UndirectedGraph g) {
this.myGraph = g;
this.founded = new boolean[myGraph.getOrder()];
}
private int prim(int sorg, UndirectedGraph MST) {
private
int prim(int sorg, UndirectedGraph MST) {
int cost = 0;
founded[sorg] = true;
MinHeap<Edge, Integer> heap = new MinHeap<>();
......@@ -39,10 +42,18 @@ public class Prim {
return cost;
}
public UndirectedGraph getMST() {
public
UndirectedGraph getMST() {
Arrays.fill(founded, false);
UndirectedGraph MST = (UndirectedGraph) myGraph.create();
prim(0, MST);
return MST;
}
public
int getMSTSize() {
Arrays.fill(founded, false);
UndirectedGraph MST = (UndirectedGraph) myGraph.create();
return prim(0, MST);
}
}
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