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

ancora niente...

parent 5fc36539
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
...@@ -134,48 +134,73 @@ public class BFS { ...@@ -134,48 +134,73 @@ public class BFS {
return alberoBFSdallaSorgente; return alberoBFSdallaSorgente;
} }
private void cammminoMinimoImpl(GraphInterface bfsAlbero, int sorgente, int nodo, boolean[] founded, ArrayList<Integer> returnArray) { private DirectedGraph bfsTreeDirected(int sorgente) {
for (Integer node : bfsAlbero.getNeighbors(sorgente)) { if (this.myGraph.getOrder() <= sorgente) {
//Se c'è destinazione tra i vicini throw new IllegalArgumentException("Dimensione della Sorgente errata");
if (node == nodo) { }
returnArray.add(node); boolean[] founded = new boolean[this.myGraph.getOrder()];
return; Queue<Integer> q = new LinkedList<>();
} DirectedGraph alberoBFSdallaSorgente = new DirectedGraph(this.myGraph.getOrder());
//Se non c'è espoloro i nodi non ancora scoperti founded[sorgente] = true;
if (!founded[node]) { q.add(sorgente);
founded[node] = true;
returnArray.add(node);
cammminoMinimoImpl(bfsAlbero, node, nodo, founded, returnArray);
} else {
returnArray.remove(node);
}
//Se sono nella foglia sbagliata while (!q.isEmpty()) {
if (((Collection<?>) bfsAlbero.getNeighbors(node)).size() == 1) { int u = q.remove();
returnArray.remove(node); Iterable<Integer> neighbors = this.myGraph.getNeighbors(u);
for (Integer v : neighbors) {
if (!founded[v]) {
founded[v] = true;
q.add(v);
alberoBFSdallaSorgente.addEdge(u, v);
}
} }
}
return alberoBFSdallaSorgente;
}
// nodo non esiste private void cammminoMinimoImpl(DirectedGraph bfsAlbero, int sorg, int dest, ArrayList<Integer> path) {
//Se ho trovato il nodo
if (sorg == dest) {
return;
} }
//Se sono nella foglia sbagliata
if (((Collection<?>) bfsAlbero.getNeighbors(sorg)).isEmpty()) {
//Pulisco il percorso corrente perché errato
path.clear();
}
//Allora devo ancora esplorare
for (Integer node : bfsAlbero.getNeighbors(sorg)) {
path.add(node);
//System.out.println(">>> Nodo: " + node);
cammminoMinimoImpl(bfsAlbero, node, dest, path);
if (node == dest) {
break;
}
}
} }
public ArrayList<Integer> camminoMinimo(int sorgente, int nodo) {
public ArrayList<Integer> camminoMinimo(int sorgente, int destinazione) {
//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.
//Proviamo con la ricorsione //Proviamo con la ricorsione
//Implementazione sbagliata, ma l'idea credo sia giusta //Implementazione sbagliata, ma l'idea credo sia giusta
GraphInterface bfsAlbero = bfsTree(sorgente); DirectedGraph bfsAlbero = bfsTreeDirected(sorgente);
boolean[] founded = new boolean[this.myGraph.getOrder()]; boolean[] founded = new boolean[this.myGraph.getOrder()];
ArrayList<Integer> returnArray = new ArrayList<>(); ArrayList<Integer> returnArray = new ArrayList<>();
founded[sorgente] = true; System.out.println(bfsAlbero);
cammminoMinimoImpl(bfsAlbero, sorgente, nodo, founded, returnArray);
if (!returnArray.isEmpty()) { cammminoMinimoImpl(bfsAlbero, sorgente, destinazione, returnArray);
returnArray.addFirst(sorgente);
}
return returnArray; return returnArray;
} }
......
...@@ -8,7 +8,7 @@ public class Main { ...@@ -8,7 +8,7 @@ public class Main {
Graph mioGrafo = new UndirectedGraph("7; 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); BFS mioBFS = new BFS(mioGrafo);
int source = 0; int source = 0;
int destination = 3; int destination = 1;
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