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

to finish

parent a2d15ce1
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,7 @@ import java.util.Arrays; ...@@ -9,7 +9,7 @@ import java.util.Arrays;
public class TestGraph { public class TestGraph {
@Test @Test
void hasUndirectedCycleTest(){ void hasUndirectedCycleTest() {
UndirectedGraph myGraph = new UndirectedGraph("4; 1 0; 0 2; 2 3; 3 0"); UndirectedGraph myGraph = new UndirectedGraph("4; 1 0; 0 2; 2 3; 3 0");
BFSAndDFSApp testApp = new BFSAndDFSApp(myGraph); BFSAndDFSApp testApp = new BFSAndDFSApp(myGraph);
Assertions.assertTrue(testApp.hasUndirectedCycle()); Assertions.assertTrue(testApp.hasUndirectedCycle());
...@@ -22,12 +22,10 @@ public class TestGraph { ...@@ -22,12 +22,10 @@ public class TestGraph {
} }
@Test @Test
void orderPostVisitTest(){ void orderPostVisitTest() {
UndirectedGraph myGraph = new UndirectedGraph("4; 2 0; 1 0; 3 2; 3 1"); UndirectedGraph myGraph = new UndirectedGraph("4; 2 0; 1 0; 3 2; 3 1");
BFSAndDFSApp testApp = new BFSAndDFSApp(myGraph); BFSAndDFSApp testApp = new BFSAndDFSApp(myGraph);
ArrayList<Integer> output = testApp.getNodesInOrderPostVisit(0); ArrayList<Integer> output = testApp.getNodesInOrderPostVisit(0);
System.out.println(output);
ArrayList<Integer> expected1 = new ArrayList<>(); ArrayList<Integer> expected1 = new ArrayList<>();
...@@ -37,12 +35,34 @@ public class TestGraph { ...@@ -37,12 +35,34 @@ public class TestGraph {
expected1.add(0); expected1.add(0);
ArrayList<Integer> expected2 = new ArrayList<>(); ArrayList<Integer> expected2 = new ArrayList<>();
expected1.add(2); expected2.add(2);
expected1.add(3); expected2.add(3);
expected1.add(1); expected2.add(1);
expected1.add(0); expected2.add(0);
Assertions.assertTrue(output.equals(expected1) || output.equals(expected2));
}
@Test
void hasDirCycleTest() {
DirectedGraph g = new DirectedGraph("1");
BFSAndDFSApp testApp = new BFSAndDFSApp(g);
Assertions.assertFalse(testApp.hasDirCycle());
g = new DirectedGraph("2; 0 1");
testApp = new BFSAndDFSApp(g);
Assertions.assertFalse(testApp.hasDirCycle());
g = new DirectedGraph("3; 1 0; 1 2; 0 2");
testApp = new BFSAndDFSApp(g);
Assertions.assertFalse(testApp.hasDirCycle());
g = new DirectedGraph("3; 0 2; 2 1; 1 0");
testApp = new BFSAndDFSApp(g);
Assertions.assertTrue(testApp.hasDirCycle());
Assertions.assertTrue( output.equals(expected1) || output.equals(expected2)); g = new DirectedGraph("5; 0 4; 4 1; 4 2; 3 4; 2 3");
testApp = new BFSAndDFSApp(g);
Assertions.assertTrue(testApp.hasDirCycle());
} }
} }
import it.uniupo.graphLib.GraphInterface; import it.uniupo.graphLib.GraphInterface;
import it.uniupo.graphLib.UndirectedGraph;
import java.util.Arrays; import java.util.Arrays;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -12,7 +13,7 @@ public class BFSAndDFSApp { ...@@ -12,7 +13,7 @@ public class BFSAndDFSApp {
private int[] fathers; private int[] fathers;
GraphInterface treeDFS; GraphInterface treeDFS;
public BFSAndDFSApp(GraphInterface g){ public BFSAndDFSApp(GraphInterface g) {
myGraph = g; myGraph = g;
BFS bfs = new BFS(g); BFS bfs = new BFS(g);
DFS dfs = new DFS(g); DFS dfs = new DFS(g);
...@@ -32,8 +33,7 @@ public class BFSAndDFSApp { ...@@ -32,8 +33,7 @@ public class BFSAndDFSApp {
fathers[node] = node; fathers[node] = node;
treeDFS.addEdge(sorg, node); treeDFS.addEdge(sorg, node);
return visitaDFS(node); return visitaDFS(node);
} } else if (node != fathers[node]) {
else if (node != fathers[node]){
return true; return true;
} }
} }
...@@ -42,7 +42,7 @@ public class BFSAndDFSApp { ...@@ -42,7 +42,7 @@ public class BFSAndDFSApp {
public boolean hasUndirectedCycle() { public boolean hasUndirectedCycle() {
for (int nodo = 0; nodo < this.myGraph.getOrder(); nodo++) { for (int nodo = 0; nodo < this.myGraph.getOrder(); nodo++) {
if(visitaDFS(nodo)){ if (visitaDFS(nodo)) {
return true; return true;
} }
} }
...@@ -62,9 +62,36 @@ public class BFSAndDFSApp { ...@@ -62,9 +62,36 @@ public class BFSAndDFSApp {
orderPostVisit.add(sorg); orderPostVisit.add(sorg);
} }
public ArrayList<Integer> getNodesInOrderPostVisit(int sorg){ public ArrayList<Integer> getNodesInOrderPostVisit(int sorg) {
ArrayList<Integer> orderPostVisit = new ArrayList<>(); ArrayList<Integer> orderPostVisit = new ArrayList<>();
visitaDFSPostVisit(sorg, orderPostVisit); visitaDFSPostVisit(sorg, orderPostVisit);
return orderPostVisit; return orderPostVisit;
} }
private boolean hasDirCycleImpl(int sorg) {
scoperti[sorg] = true;
for (Integer node : myGraph.getNeighbors(sorg)) {
if (!scoperti[node]) {
fathers[node] = sorg;
return hasDirCycleImpl(node);
} else if (node != fathers[sorg]) {
return true;
}
}
return false;
}
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()];
scoperti = new boolean[myGraph.getOrder()];
if (hasDirCycleImpl(i)) {
return true;
}
}
return false;
}
} }
\ No newline at end of file
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