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

need fix

parent 0f0702bc
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,8 @@ import it.uniupo.graphLib.UndirectedGraph;
import java.util.Arrays;
import java.util.ArrayList;
public class BFSAndDFSApp {
public
class BFSAndDFSApp {
private GraphInterface myGraph;
private BFS bfs;
private DFS dfs;
......@@ -13,7 +14,8 @@ public class BFSAndDFSApp {
private int[] fathers;
GraphInterface treeDFS;
public BFSAndDFSApp(GraphInterface g) {
public
BFSAndDFSApp(GraphInterface g) {
myGraph = g;
BFS bfs = new BFS(g);
DFS dfs = new DFS(g);
......@@ -23,7 +25,8 @@ public class BFSAndDFSApp {
Arrays.fill(this.fathers, -1);
}
private boolean visitaDFS(int sorg) {
private
boolean visitaDFS(int sorg) {
if (sorg >= this.myGraph.getOrder() || sorg < 0) {
throw new IllegalArgumentException("Sorgente non presenten nel grafo");
}
......@@ -40,7 +43,8 @@ public class BFSAndDFSApp {
return false;
}
public boolean hasUndirectedCycle() {
public
boolean hasUndirectedCycle() {
for (int nodo = 0; nodo < this.myGraph.getOrder(); nodo++) {
if (visitaDFS(nodo)) {
return true;
......@@ -49,7 +53,8 @@ public class BFSAndDFSApp {
return false;
}
private void visitaDFSPostVisit(int sorg, ArrayList<Integer> orderPostVisit) {
private
void visitaDFSPostVisit(int sorg, ArrayList<Integer> orderPostVisit) {
if (sorg >= this.myGraph.getOrder() || sorg < 0) {
throw new IllegalArgumentException("Sorgente non presenten nel grafo");
}
......@@ -62,13 +67,15 @@ public class BFSAndDFSApp {
orderPostVisit.add(sorg);
}
public ArrayList<Integer> getNodesInOrderPostVisit(int sorg) {
public
ArrayList<Integer> getNodesInOrderPostVisit(int sorg) {
ArrayList<Integer> orderPostVisit = new ArrayList<>();
visitaDFSPostVisit(sorg, orderPostVisit);
return orderPostVisit;
}
private boolean hasDirCycleImpl(int sorg) {
private
boolean hasDirCycleImpl(int sorg) {
scoperti[sorg] = true;
for (Integer node : myGraph.getNeighbors(sorg)) {
if (!scoperti[node]) {
......@@ -81,13 +88,16 @@ public class BFSAndDFSApp {
return false;
}
public boolean hasDirCycle() {
public
boolean hasDirCycle() {
if (myGraph instanceof UndirectedGraph) {
throw new IllegalArgumentException("Impossibile usare hasDirCycle su un grafo non orientato");
}
for (int i = 0; i < myGraph.getOrder(); i++) {
fathers = new int[myGraph.getOrder()];
Arrays.fill(fathers, -1);
scoperti = new boolean[myGraph.getOrder()];
Arrays.fill(scoperti, false);
if (hasDirCycleImpl(i)) {
return true;
}
......
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