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

upd

parent cc185aa7
No related branches found
No related tags found
No related merge requests found
......@@ -8,17 +8,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$USER_HOME$/Downloads/graphLib.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$USER_HOME$/Downloads/graphLib.jar!/doc" />
</JAVADOC>
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
......@@ -35,5 +24,16 @@
<SOURCES />
</library>
</orderEntry>
<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>
</component>
</module>
\ No newline at end of file
No preview for this file type
No preview for this file type
import it.uniupo.graphLib.GraphInterface;
import it.uniupo.graphLib.UndirectedGraph;
import it.uniupo.graphLib.*;
import java.awt.List;
import java.util.*;
public class BFS {
......@@ -134,36 +134,47 @@ public class BFS {
return alberoBFSdallaSorgente;
}
//Da finire
public ArrayList<Integer> camminoMinimo(int sorgente, int nodo) {
if (this.myGraph.getOrder() <= sorgente) {
throw new IllegalArgumentException("Dimensione della Sorgente errata");
}
if (this.myGraph.getOrder() <= nodo) {
throw new IllegalArgumentException("Dimensione della Destinazione errata");
private void cammminoMinimoImpl(GraphInterface bfsAlbero, int sorgente, int nodo, boolean[] founded, ArrayList<Integer> returnArray) {
for (Integer node : bfsAlbero.getNeighbors(sorgente)) {
//Se c'è destinazione tra i vicini
if (node == nodo) {
returnArray.add(node);
return;
}
//Se non c'è espoloro i nodi non ancora scoperti
if (!founded[node]) {
founded[node] = true;
returnArray.add(node);
cammminoMinimoImpl(bfsAlbero, node, nodo, founded, returnArray);
} else {
returnArray.remove(node);
}
//Se sono nella foglia sbagliata
if (((Collection<?>) bfsAlbero.getNeighbors(node)).size() == 1) {
returnArray.remove(node);
}
// nodo non esiste
}
}
public ArrayList<Integer> camminoMinimo(int sorgente, int nodo) {
//L'idea è quella di usare il BFS Albero, perché aciclico, ed in questo modo trovare il percorso.
//Proviamo con la ricorsione
GraphInterface bfsAlbero = bfsTree(sorgente);
boolean[] founded = new boolean[this.myGraph.getOrder()];
Queue<Integer> q = new LinkedList<>();
ArrayList<Integer> returnArray = new ArrayList<>();
founded[sorgente] = true;
q.add(sorgente);
returnArray.add(sorgente);
while (!q.isEmpty()) {
int u = q.remove();
Iterable<Integer> neighbors = this.myGraph.getNeighbors(u);
for (Integer v : neighbors) {
if (!founded[v]) {
founded[v] = true;
q.add(v);
returnArray.addLast(v);
if (v == nodo) {
return returnArray;
}
}
}
cammminoMinimoImpl(bfsAlbero, sorgente, nodo, founded, returnArray);
if (!returnArray.isEmpty()) {
returnArray.addFirst(sorgente);
}
return null;
return returnArray;
}
}
......@@ -5,17 +5,17 @@ import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Graph mioGrafo = new UndirectedGraph("6; 0 4; 4 3; 1 3; 1 2; 0 2; 0 5; 5 2"); //Num Nodi + Archi
Graph mioGrafo = new UndirectedGraph("7; 0 4; 4 3; 1 3; 1 2; 0 2; 0 5; 5 2"); //Num Nodi + Archi
BFS mioBFS = new BFS(mioGrafo);
int source = 4;
int destination = 1;
int destination = 3;
System.out.println(">>> Sorgente delle Operazione: " + source);
System.out.println(">>> Destinazione delle Operazione: " + destination);
System.out.println(">>> Visita BFS sul Grafo: " + mioBFS.getNodesInOrderOfVisit(source));
System.out.println(">>> Distanza dei nodi dalla sorgente: " + Arrays.toString(mioBFS.getDistance(source)));
System.out.println(">>> Distanza del nodo da " + source + " a " + destination + ": " + mioBFS.getDistance(source, destination));
System.out.println(">>> Albero di Visita con rafice la sorgente: \n" + mioBFS.bfsTree(source));
System.out.println(">>> Albero di Visita con radice la sorgente: \n" + mioBFS.bfsTree(source));
//Da qui in poi da finire
System.out.println(">>> Cammino più corto per andare da sorgente a destinazione: " + mioBFS.camminoMinimo(source, destination));
......
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