Skip to content
Snippets Groups Projects
Commit 4f596e42 authored by Gianluca Mastrolonardo's avatar Gianluca Mastrolonardo
Browse files

Add Lab6 and Lab5 need fix

parent 1355e66c
No related branches found
No related tags found
No related merge requests found
......@@ -75,5 +75,10 @@ class TestLab {
DirectedGraph cyclicGraph = new DirectedGraph("3; 0 1; 1 2; 2 0");
DFS dfsCyclicTest = new DFS(cyclicGraph);
Assertions.assertThrows(IllegalArgumentException.class, () -> dfsCyclicTest.topologicalOrderDag());
//Da sistemare, bisogna usare ordine fine visita invece che array di padri (per grafi orientati)
DirectedGraph cyclicGraph2 = new DirectedGraph("2; 0 1; 1 0;");
DFS dfsCyclicTest2 = new DFS(cyclicGraph2);
Assertions.assertThrows(IllegalArgumentException.class, () -> dfsCyclicTest2.topologicalOrderDag());
}
}
### 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
<?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$/Lab6.iml" filepath="$PROJECT_DIR$/Lab6.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"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="47e80429-8a37-4045-98c6-d9f72531884e" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Test/TestVoli.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Voli.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../Lab5/Test/TestLab.java" beforeDir="false" afterPath="$PROJECT_DIR$/../Lab5/Test/TestLab.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="FormatOnSaveOptions">
<option name="myRunOnSave" value="true" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../../.." />
</component>
<component name="ProjectColorInfo">{
&quot;associatedIndex&quot;: 8
}</component>
<component name="ProjectId" id="2fligKtvqSt5UTpaCzrCyG5VubV" />
<component name="ProjectLevelVcsManager">
<ConfirmationsSetting value="2" id="Add" />
</component>
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"ASKED_ADD_EXTERNAL_FILES": "true",
"Downloaded.Files.Path.Enabled": "false",
"Repository.Attach.Annotations": "false",
"Repository.Attach.JavaDocs": "false",
"Repository.Attach.Sources": "false",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "main",
"last_opened_file_path": "/home/20050114/Documents/appunti/Algoritmi_2/Laboratorio/Lab6/Test",
"project.structure.last.edited": "Modules",
"project.structure.proportion": "0.0",
"project.structure.side.proportion": "0.2",
"settings.editor.selected.configurable": "actions.on.save"
}
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/Test" />
</key>
</component>
<component name="RunManager" selected="JUnit.TestVoli">
<configuration name="TestVoli" 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="TEST_OBJECT" value="class" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="TestVoli.testScali" 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="testScali" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="TestVoli.testTempo" 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="testTempo" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.TestVoli" />
<item itemvalue="JUnit.TestVoli.testTempo" />
<item itemvalue="JUnit.TestVoli.testScali" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="47e80429-8a37-4045-98c6-d9f72531884e" name="Changes" comment="" />
<created>1714382175723</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1714382175723</updated>
</task>
<servers />
</component>
<component name="VcsManagerConfiguration">
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
</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.DirectedGraph;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestVoli {
@Test
public void testTempo() {
DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1");
Voli testVoli = new Voli(graph);
int tempo = testVoli.tempo(0, 2);
assertEquals(6, tempo);
graph = new DirectedGraph("3;0 1 3");
testVoli = new Voli(graph);
tempo = testVoli.tempo(0, 2);
assertEquals(-1, tempo);
}
@Test
public void testTempoExc1() {
DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1");
Voli testVoli = new Voli(graph);
Assertions.assertThrows(java.lang.IllegalArgumentException.class, () -> {
testVoli.tempo(-1, 2);
});
}
@Test
public void testTempoExc2() {
DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1");
Voli testVoli = new Voli(graph);
Assertions.assertThrows(java.lang.IllegalArgumentException.class, () -> {
testVoli.tempo(0, 3);
});
}
@Test
public void testScali() {
DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1");
Voli testVoli = new Voli(graph);
int scali = testVoli.scali(0, 2);
assertEquals(0, scali);
graph = new DirectedGraph("3;0 1 3");
testVoli = new Voli(graph);
scali = testVoli.scali(0, 2);
assertEquals(-1, scali);
}
@Test
public void testScaliExc() {
DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1");
Voli testVoli = new Voli(graph);
Assertions.assertThrows(java.lang.IllegalArgumentException.class, () -> {
testVoli.scali(-1, 2);
});
}
@Test
public void testTempoMinimo() {
DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1");
Voli testVoli = new Voli(graph);
int tempoMinimo = testVoli.tempoMinimo(0, 2);
assertEquals(4, tempoMinimo);
graph = new DirectedGraph("3;0 1 3");
testVoli = new Voli(graph);
tempoMinimo = testVoli.tempoMinimo(0, 2);
assertEquals(-1, tempoMinimo);
}
@Test
public void testTempoMinimoExc() {
DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1");
Voli testVoli = new Voli(graph);
Assertions.assertThrows(java.lang.IllegalArgumentException.class, () -> {
testVoli.tempoMinimo(-1, 2);
});
}
/*
@Test
public void testPercorsoTempoMinimo() {
DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1");
Voli testVoli = new Voli(graph);
ArrayList<Edge> percTempMin = testVoli.trattaVeloce(0, 2);
assertEquals(2, percTempMin.size());
assertEquals(percTempMin.get(0), new Edge(0, 1));
assertEquals(percTempMin.get(1), new Edge(1, 2));
graph = new DirectedGraph("3;0 1 3");
testVoli = new Voli(graph);
percTempMin = testVoli.trattaVeloce(0, 2);
assertTrue(percTempMin == null);
percTempMin = testVoli.trattaVeloce(0, 1);
assertEquals(1, percTempMin.size());
assertEquals(percTempMin.get(0), new Edge(0, 1));
}
@Test
public void testPercorsoScaliMinimo() {
DirectedGraph graph = new DirectedGraph("3;0 1 3;0 2 6;1 2 1");
Voli testVoli = new Voli(graph);
ArrayList<Integer> percScaliMin = testVoli.elencoScali(0, 2);
assertEquals(0, percScaliMin.size());
graph = new DirectedGraph("3;0 1 3");
testVoli = new Voli(graph);
percScaliMin = testVoli.elencoScali(0, 2);
assertTrue(percScaliMin == null);
graph = new DirectedGraph("1");
testVoli = new Voli(graph);
percScaliMin = testVoli.elencoScali(0, 0);
assertEquals(0, percScaliMin.size());
graph = new DirectedGraph("4; 2 1 1; 1 0 4; 2 3 2; 1 3 3;3 0 2");
testVoli = new Voli(graph);
percScaliMin = testVoli.elencoScali(2, 0);
assertEquals(1, percScaliMin.size());
assertEquals(1, percScaliMin.get(0).intValue());
}
*/
}
\ No newline at end of file
import it.uniupo.graphLib.DirectedGraph;
public class Main {
public static void main(String[] args) {
DirectedGraph graph = new DirectedGraph("3; 0 1; ");
//Voli v = new Voli("3");
}
}
\ No newline at end of file
import it.uniupo.algoTools.MinHeap;
import it.uniupo.graphLib.DirectedGraph;
import it.uniupo.graphLib.Edge;
import it.uniupo.graphLib.GraphInterface;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class Voli {
private DirectedGraph myCollegamenti;
private boolean[] founded;
public Voli(DirectedGraph collegamenti) {
this.myCollegamenti = collegamenti;
this.founded = new boolean[myCollegamenti.getOrder()];
}
private int[] dijkstra(int sorg) {
int[] distance = new int[myCollegamenti.getOrder()];
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 (!founded[nodeW]) {
founded[nodeW] = true;
distance[nodeW] = distance[nodeU] + heapReturnedEdge.getWeight();
for (Edge e : myCollegamenti.getOutEdges(nodeW)) {
heap.add(e, distance[nodeW] + e.getWeight());
}
}
}
return distance;
}
private int[] bfs(int sorg) {
if (sorg >= this.myCollegamenti.getOrder()) {
throw new IllegalArgumentException("Sorgente errata");
}
founded[sorg] = true;
int[] bfsDistance = new int[myCollegamenti.getOrder()];
Arrays.fill(bfsDistance, -1);
bfsDistance[sorg] = 0;
Queue<Integer> q = new LinkedList<>();
q.add(sorg);
while (!q.isEmpty()) {
int u = q.remove();
for (int v : myCollegamenti.getNeighbors(u)) {
if (!founded[v]) {
founded[v] = true;
q.add(v);
bfsDistance[v] = bfsDistance[u] + 1;
}
}
}
return bfsDistance;
}
private int[] bfsTime(int sorg) {
if (sorg >= this.myCollegamenti.getOrder()) {
throw new IllegalArgumentException("Sorgente errata");
}
founded[sorg] = true;
int[] bfsDistanceWeigth = new int[myCollegamenti.getOrder()];
Arrays.fill(bfsDistanceWeigth, -1);
bfsDistanceWeigth[sorg] = 0;
Queue<Integer> q = new LinkedList<>();
q.add(sorg);
while (!q.isEmpty()) {
int u = q.remove();
for (Edge uv : myCollegamenti.getOutEdges(u)) {
int v = uv.getHead();
if (!founded[v]) {
founded[v] = true;
q.add(v);
bfsDistanceWeigth[v] = bfsDistanceWeigth[u] + uv.getWeight();
}
}
}
return bfsDistanceWeigth;
}
public int tempoMinimo(int partenza, int destinazione) {
if (partenza >= myCollegamenti.getOrder() || destinazione >= myCollegamenti.getOrder() || partenza <= -1 || destinazione <= -1) {
throw new IllegalArgumentException("Aereoporto di partenza o di destinazione non esite");
}
Arrays.fill(founded, false);
int[] d = dijkstra(partenza);
return d[destinazione];
}
public int scali(int partenza, int destinazione) {
if (partenza >= myCollegamenti.getOrder() || destinazione >= myCollegamenti.getOrder() || partenza <= -1 || destinazione <= -1) {
throw new IllegalArgumentException("Aereoporto di partenza o di destinazione non esite");
}
int[] res = bfs(partenza);
if (partenza == destinazione) return 0;
if (res[destinazione] != -1) return --res[destinazione];
else return res[destinazione];
}
public int tempo(int partenza, int destinazione) {
if (partenza >= myCollegamenti.getOrder() || destinazione >= myCollegamenti.getOrder() || partenza <= -1 || destinazione <= -1) {
throw new IllegalArgumentException("Aereoporto di partenza o di destinazione non esite");
}
Arrays.fill(founded, false);
return bfsTime(partenza)[destinazione];
}
}
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