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 {
return alberoBFSdallaSorgente;
}
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;
}
private DirectedGraph bfsTreeDirected(int sorgente) {
if (this.myGraph.getOrder() <= sorgente) {
throw new IllegalArgumentException("Dimensione della Sorgente errata");
}
boolean[] founded = new boolean[this.myGraph.getOrder()];
Queue<Integer> q = new LinkedList<>();
DirectedGraph alberoBFSdallaSorgente = new DirectedGraph(this.myGraph.getOrder());
//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);
}
founded[sorgente] = true;
q.add(sorgente);
//Se sono nella foglia sbagliata
if (((Collection<?>) bfsAlbero.getNeighbors(node)).size() == 1) {
returnArray.remove(node);
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);
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.
//Proviamo con la ricorsione
//Implementazione sbagliata, ma l'idea credo sia giusta
GraphInterface bfsAlbero = bfsTree(sorgente);
DirectedGraph bfsAlbero = bfsTreeDirected(sorgente);
boolean[] founded = new boolean[this.myGraph.getOrder()];
ArrayList<Integer> returnArray = new ArrayList<>();
founded[sorgente] = true;
cammminoMinimoImpl(bfsAlbero, sorgente, nodo, founded, returnArray);
if (!returnArray.isEmpty()) {
returnArray.addFirst(sorgente);
}
System.out.println(bfsAlbero);
cammminoMinimoImpl(bfsAlbero, sorgente, destinazione, returnArray);
return returnArray;
}
......
......@@ -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
BFS mioBFS = new BFS(mioGrafo);
int source = 0;
int destination = 3;
int destination = 1;
System.out.println(">>> Sorgente delle Operazione: " + source);
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