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

Lab6 finito

parent 0ddc99b9
No related branches found
No related tags found
No related merge requests found
...@@ -5,12 +5,11 @@ ...@@ -5,12 +5,11 @@
</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 afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Test/TestVoli.java" beforeDir="false" afterPath="$PROJECT_DIR$/Test/TestVoli.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/Voli.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Voli.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Test/TestVoli.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/../../Teoria/Grafi_Introduzione.pdf" beforeDir="false" afterPath="$PROJECT_DIR$/../../Teoria/Grafi_Introduzione.pdf" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Voli.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/../../Teoria/Grafi_Introduzione.xopp" beforeDir="false" afterPath="$PROJECT_DIR$/../../Teoria/Grafi_Introduzione.xopp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../Lab5/Test/TestLab.java" beforeDir="false" afterPath="$PROJECT_DIR$/../Lab5/Test/TestLab.java" afterDir="false" />
</list> </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" />
...@@ -45,13 +44,21 @@ ...@@ -45,13 +44,21 @@
"keyToString": { "keyToString": {
"ASKED_ADD_EXTERNAL_FILES": "true", "ASKED_ADD_EXTERNAL_FILES": "true",
"Downloaded.Files.Path.Enabled": "false", "Downloaded.Files.Path.Enabled": "false",
"JUnit.TestVoli.daEliminare.executor": "Run",
"JUnit.TestVoli.executor": "Run",
"JUnit.TestVoli.merda.executor": "Run",
"JUnit.TestVoli.testElencoScaliMinimo.executor": "Run",
"JUnit.TestVoli.testPercorsoTempoMinimo.executor": "Run",
"JUnit.TestVoli.testScali.executor": "Run",
"JUnit.TestVoli.testTempoMinimo.executor": "Run",
"Repository.Attach.Annotations": "false", "Repository.Attach.Annotations": "false",
"Repository.Attach.JavaDocs": "false", "Repository.Attach.JavaDocs": "false",
"Repository.Attach.Sources": "false", "Repository.Attach.Sources": "false",
"RunOnceActivity.OpenProjectViewOnStart": "true", "RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "main", "git-widget-placeholder": "main",
"last_opened_file_path": "/home/20050114/Documents/appunti/Algoritmi_2/Laboratorio/Lab6/Test", "kotlin-language-version-configured": "true",
"last_opened_file_path": "/home/gianluca/Documents/appunti/Algoritmi_2/Laboratorio/Lab6",
"project.structure.last.edited": "Modules", "project.structure.last.edited": "Modules",
"project.structure.proportion": "0.0", "project.structure.proportion": "0.0",
"project.structure.side.proportion": "0.2", "project.structure.side.proportion": "0.2",
...@@ -73,21 +80,41 @@ ...@@ -73,21 +80,41 @@
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="TestVoli.testScali" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="TestVoli.daEliminare" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab6" /> <module name="Lab6" />
<option name="PACKAGE_NAME" value="" /> <option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="TestVoli" /> <option name="MAIN_CLASS_NAME" value="TestVoli" />
<option name="METHOD_NAME" value="testScali" /> <option name="METHOD_NAME" value="daEliminare" />
<option name="TEST_OBJECT" value="method" /> <option name="TEST_OBJECT" value="method" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="TestVoli.testTempo" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="TestVoli.merda" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab6" /> <module name="Lab6" />
<option name="PACKAGE_NAME" value="" /> <option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="TestVoli" /> <option name="MAIN_CLASS_NAME" value="TestVoli" />
<option name="METHOD_NAME" value="testTempo" /> <option name="METHOD_NAME" value="merda" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="TestVoli.testElencoScaliMinimo" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab6" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="TestVoli" />
<option name="METHOD_NAME" value="testElencoScaliMinimo" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="TestVoli.testPercorsoTempoMinimo" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab6" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="TestVoli" />
<option name="METHOD_NAME" value="testPercorsoTempoMinimo" />
<option name="TEST_OBJECT" value="method" /> <option name="TEST_OBJECT" value="method" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
...@@ -96,8 +123,10 @@ ...@@ -96,8 +123,10 @@
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="JUnit.TestVoli" /> <item itemvalue="JUnit.TestVoli" />
<item itemvalue="JUnit.TestVoli.testTempo" /> <item itemvalue="JUnit.TestVoli.testElencoScaliMinimo" />
<item itemvalue="JUnit.TestVoli.testScali" /> <item itemvalue="JUnit.TestVoli.merda" />
<item itemvalue="JUnit.TestVoli.testPercorsoTempoMinimo" />
<item itemvalue="JUnit.TestVoli.daEliminare" />
</list> </list>
</recent_temporary> </recent_temporary>
</component> </component>
......
import it.uniupo.graphLib.DirectedGraph; import it.uniupo.graphLib.DirectedGraph;
import it.uniupo.graphLib.Edge;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestVoli { public class TestVoli {
...@@ -49,6 +52,8 @@ public class TestVoli { ...@@ -49,6 +52,8 @@ public class TestVoli {
scali = testVoli.scali(0, 2); scali = testVoli.scali(0, 2);
assertEquals(-1, scali); assertEquals(-1, scali);
testVoli = new Voli(new DirectedGraph("5;0 1 1;1 2 1;2 3 1;0 4 15;4 3 2"));
assertEquals(1, testVoli.scali(0, 3));
} }
@Test @Test
...@@ -83,7 +88,6 @@ public class TestVoli { ...@@ -83,7 +88,6 @@ public class TestVoli {
}); });
} }
/*
@Test @Test
public void testPercorsoTempoMinimo() { public void testPercorsoTempoMinimo() {
DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1"); DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1");
...@@ -96,16 +100,15 @@ public class TestVoli { ...@@ -96,16 +100,15 @@ public class TestVoli {
graph = new DirectedGraph("3;0 1 3"); graph = new DirectedGraph("3;0 1 3");
testVoli = new Voli(graph); testVoli = new Voli(graph);
percTempMin = testVoli.trattaVeloce(0, 2); percTempMin = testVoli.trattaVeloce(0, 2);
assertTrue(percTempMin == null); Assertions.assertNull(percTempMin);
percTempMin = testVoli.trattaVeloce(0, 1); percTempMin = testVoli.trattaVeloce(0, 1);
assertEquals(1, percTempMin.size()); assertEquals(1, percTempMin.size());
assertEquals(percTempMin.get(0), new Edge(0, 1)); assertEquals(percTempMin.getFirst(), new Edge(0, 1));
} }
@Test @Test
public void testPercorsoScaliMinimo() { public void testElencoScaliMinimo() {
DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1"); DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1");
Voli testVoli = new Voli(graph); Voli testVoli = new Voli(graph);
ArrayList<Integer> percScaliMin = testVoli.elencoScali(0, 2); ArrayList<Integer> percScaliMin = testVoli.elencoScali(0, 2);
...@@ -114,7 +117,7 @@ public class TestVoli { ...@@ -114,7 +117,7 @@ public class TestVoli {
graph = new DirectedGraph("3;0 1 3"); graph = new DirectedGraph("3;0 1 3");
testVoli = new Voli(graph); testVoli = new Voli(graph);
percScaliMin = testVoli.elencoScali(0, 2); percScaliMin = testVoli.elencoScali(0, 2);
assertTrue(percScaliMin == null); Assertions.assertNull(percScaliMin);
graph = new DirectedGraph("1"); graph = new DirectedGraph("1");
testVoli = new Voli(graph); testVoli = new Voli(graph);
...@@ -125,7 +128,7 @@ public class TestVoli { ...@@ -125,7 +128,7 @@ public class TestVoli {
testVoli = new Voli(graph); testVoli = new Voli(graph);
percScaliMin = testVoli.elencoScali(2, 0); percScaliMin = testVoli.elencoScali(2, 0);
assertEquals(1, percScaliMin.size()); assertEquals(1, percScaliMin.size());
assertEquals(1, percScaliMin.get(0).intValue()); assertEquals(1, percScaliMin.getFirst().intValue());
} }
*/
} }
\ No newline at end of file
...@@ -3,6 +3,7 @@ import it.uniupo.graphLib.DirectedGraph; ...@@ -3,6 +3,7 @@ import it.uniupo.graphLib.DirectedGraph;
import it.uniupo.graphLib.Edge; import it.uniupo.graphLib.Edge;
import it.uniupo.graphLib.GraphInterface; import it.uniupo.graphLib.GraphInterface;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
...@@ -45,6 +46,41 @@ public class Voli { ...@@ -45,6 +46,41 @@ public class Voli {
return distance; return distance;
} }
private boolean dijkstraTrattaVeloce(int sorg, int dest, ArrayList<Edge> returnArray) {
int[] distance = new int[myCollegamenti.getOrder()];
//ArrayList<Edge> returnArray = new ArrayList<>();
Arrays.fill(distance, -1);
founded[sorg] = true;
distance[sorg] = 0;
MinHeap<Edge, Integer> heap = new MinHeap<Edge, Integer>();
for (Edge e : myCollegamenti.getOutEdges(sorg)) {
heap.add(e, distance[sorg] + e.getWeight());
}
while (!heap.isEmpty()) {
Edge heapReturnedEdge = heap.extractMin(); //arco (u,w), u alla prima iterazione è uguale a sorg
int nodeU = heapReturnedEdge.getTail();
int nodeW = heapReturnedEdge.getHead();
if (!returnArray.isEmpty() && nodeU == returnArray.getLast().getTail()) {
returnArray.removeLast();
}
if (!founded[nodeW]) {
returnArray.add(heapReturnedEdge);
if (nodeW == dest) {
return true;
}
founded[nodeW] = true;
distance[nodeW] = distance[nodeU] + heapReturnedEdge.getWeight();
for (Edge e : myCollegamenti.getOutEdges(nodeW)) {
heap.add(e, distance[nodeW] + e.getWeight());
}
}
}
return false;
}
private int[] bfs(int sorg) { private int[] bfs(int sorg) {
if (sorg >= this.myCollegamenti.getOrder()) { if (sorg >= this.myCollegamenti.getOrder()) {
throw new IllegalArgumentException("Sorgente errata"); throw new IllegalArgumentException("Sorgente errata");
...@@ -92,31 +128,77 @@ public class Voli { ...@@ -92,31 +128,77 @@ public class Voli {
return bfsDistanceWeigth; return bfsDistanceWeigth;
} }
public int tempoMinimo(int partenza, int destinazione) { private ArrayList<Integer> bfsMinScali(int sorg, int dest) {
if (partenza >= myCollegamenti.getOrder() || destinazione >= myCollegamenti.getOrder() || partenza <= -1 || destinazione <= -1) { if (sorg >= this.myCollegamenti.getOrder()) {
throw new IllegalArgumentException("Aereoporto di partenza o di destinazione non esite"); throw new IllegalArgumentException("Sorgente errata");
} }
ArrayList<Integer> returnArray = new ArrayList<>();
founded[sorg] = true;
Queue<Integer> q = new LinkedList<>();
q.add(sorg);
while (!q.isEmpty()) {
int u = q.remove();
if (u != sorg) {
returnArray.add(u);
}
boolean noNewNodes = true;
for (int v : myCollegamenti.getNeighbors(u)) {
if (!founded[v]) {
founded[v] = true;
noNewNodes = false;
q.add(v);
//returnArray.add(v);
}
if (v == dest) {
return returnArray;
}
}
if (!returnArray.isEmpty() && noNewNodes) {
returnArray.removeLast();
}
}
return null;
}
private void checkAirport(int partenza, int destinazione) {
if (partenza < 0 || destinazione < 0 || partenza >= myCollegamenti.getOrder() || destinazione >= myCollegamenti.getOrder()) {
throw new IllegalArgumentException("Aeroporto di partenza o di destinazione non esite");
}
}
public int tempoMinimo(int partenza, int destinazione) {
checkAirport(partenza, destinazione);
Arrays.fill(founded, false); Arrays.fill(founded, false);
int[] d = dijkstra(partenza); int[] d = dijkstra(partenza);
return d[destinazione]; return d[destinazione];
} }
public int scali(int partenza, int destinazione) { public int scali(int partenza, int destinazione) {
if (partenza >= myCollegamenti.getOrder() || destinazione >= myCollegamenti.getOrder() || partenza <= -1 || destinazione <= -1) { checkAirport(partenza, destinazione);
throw new IllegalArgumentException("Aereoporto di partenza o di destinazione non esite");
}
int[] res = bfs(partenza);
if (partenza == destinazione) return 0; if (partenza == destinazione) return 0;
int[] res = bfs(partenza);
if (res[destinazione] != -1) return --res[destinazione]; if (res[destinazione] != -1) return --res[destinazione];
else return res[destinazione]; else return res[destinazione];
} }
public int tempo(int partenza, int destinazione) { public int tempo(int partenza, int destinazione) {
if (partenza >= myCollegamenti.getOrder() || destinazione >= myCollegamenti.getOrder() || partenza <= -1 || destinazione <= -1) { checkAirport(partenza, destinazione);
throw new IllegalArgumentException("Aereoporto di partenza o di destinazione non esite");
}
Arrays.fill(founded, false); Arrays.fill(founded, false);
return bfsTime(partenza)[destinazione]; return bfsTime(partenza)[destinazione];
} }
public ArrayList<Edge> trattaVeloce(int partenza, int destinazione) {
checkAirport(partenza, destinazione);
Arrays.fill(founded, false);
ArrayList<Edge> resultArray = new ArrayList<>();
return dijkstraTrattaVeloce(partenza, destinazione, resultArray) ? resultArray : null;
}
public ArrayList<Integer> elencoScali(int partenza, int destinazione) {
checkAirport(partenza, destinazione);
if (partenza == destinazione) return new ArrayList<>(); //Serve solo per ritornare un ArrayList vuota
Arrays.fill(founded, false);
return bfsMinScali(partenza, destinazione);
}
} }
No preview for this file type
No preview for this file type
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