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

we made it

parent b6f0e857
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
...@@ -159,27 +159,31 @@ public class BFS { ...@@ -159,27 +159,31 @@ public class BFS {
return alberoBFSdallaSorgente; return alberoBFSdallaSorgente;
} }
private void cammminoMinimoImpl(DirectedGraph bfsAlbero, int sorg, int dest, ArrayList<Integer> path, ArrayList<Boolean> founded) { private void cammminoMinimoImpl(DirectedGraph bfsAlbero, int sorg, int dest, ArrayList<Integer> path, boolean[] founded) {
//Se ho trovato il nodo //Se ho trovato il nodo
if (sorg == dest) { if (sorg == dest) {
founded.add(true); founded[0] = true;
return; return;
} }
//Se sono nella foglia sbagliata //Se sono nella foglia sbagliata
/*
if (((Collection<?>) bfsAlbero.getNeighbors(sorg)).isEmpty()) { if (((Collection<?>) bfsAlbero.getNeighbors(sorg)).isEmpty()) {
//Pulisco il percorso corrente perché errato //Pulisco il percorso corrente perché errato
path.clear(); path.clear();
} }
*/
//Allora devo ancora esplorare //Allora devo ancora esplorare
for (Integer node : bfsAlbero.getNeighbors(sorg)) { for (Integer node : bfsAlbero.getNeighbors(sorg)) {
path.add(node); path.add(node);
System.out.println(">>> Nodo: " + node);
cammminoMinimoImpl(bfsAlbero, node, dest, path, founded); cammminoMinimoImpl(bfsAlbero, node, dest, path, founded);
if (founded.contains(true)) { if (founded[0]) {
break; break;
} else {
path.removeLast();
} }
} }
} }
...@@ -189,16 +193,20 @@ public class BFS { ...@@ -189,16 +193,20 @@ public class BFS {
//L'idea è quella di usare il BFS Albero, perché aciclico, ed in questo modo trovare il percorso. //L'idea è quella di usare il BFS Albero, perché aciclico, ed in questo modo trovare il percorso.
//Provo con la ricorsione //Provo con la ricorsione
//Implementazione sbagliata, ma l'idea credo sia giusta
if (sorgente == destinazione) {
return null;
}
DirectedGraph bfsAlbero = bfsTreeDirected(sorgente); DirectedGraph bfsAlbero = bfsTreeDirected(sorgente);
ArrayList<Integer> returnArray = new ArrayList<>(); ArrayList<Integer> returnArray = new ArrayList<>();
ArrayList<Boolean> founded = new ArrayList<>(); boolean[] founded = new boolean[1];
System.out.println(bfsAlbero);
cammminoMinimoImpl(bfsAlbero, sorgente, destinazione, returnArray, founded); cammminoMinimoImpl(bfsAlbero, sorgente, destinazione, returnArray, founded);
returnArray.addFirst(sorgente); if (!returnArray.isEmpty()) {
returnArray.addFirst(sorgente);
}
return returnArray; return returnArray;
} }
} }
...@@ -5,10 +5,10 @@ import java.util.Arrays; ...@@ -5,10 +5,10 @@ import java.util.Arrays;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Graph mioGrafo = new UndirectedGraph("10; 0 1; 1 2; 1 5; 1 4; 2 4; 2 3; 2 4; 3 9; 5 7; 7 6"); //Num Nodi + Archi Graph mioGrafo = new UndirectedGraph("10; 0 1; 1 2; 2 3; 3 4; 4 5; 5 6; 6 7; 7 8; 8 9; 9 0; 0 2; 2 4; 4 6; 6 8; 8 0;"); //Num Nodi + Archi
BFS mioBFS = new BFS(mioGrafo); BFS mioBFS = new BFS(mioGrafo);
int source = 0; int source = 0;
int destination = 6; int destination = 4;
System.out.println(">>> Sorgente delle Operazione: " + source); System.out.println(">>> Sorgente delle Operazione: " + source);
System.out.println(">>> Destinazione delle Operazione: " + destination); System.out.println(">>> Destinazione delle Operazione: " + 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