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

fixed

parent c25d402c
No related branches found
No related tags found
No related merge requests found
......@@ -106,7 +106,6 @@ class BFSAndDFSApp {
throw new IllegalArgumentException("Impossibile usare hasDirCycle su un grafo non orientato");
}
for (int i = 0; i < myGraph.getOrder(); i++) {
System.out.println("pup!");
fathers = new int[myGraph.getOrder()];
Arrays.fill(fathers, -1);
scoperti = new boolean[myGraph.getOrder()];
......
......@@ -65,4 +65,15 @@ class TestLab {
DFS dfsTest3 = new DFS(gThreeNodes);
Assertions.assertTrue(dfsTest3.topologicalOrder().indexOf(2) < dfsTest3.topologicalOrder().indexOf(0));
}
@Test
void testTopologicalOrderDag() {
DirectedGraph dag = new DirectedGraph("4; 0 1; 0 2; 1 2; 1 3");
DFS dfsTest = new DFS(dag);
Assertions.assertTrue(dfsTest.topologicalOrderDag().indexOf(0) < dfsTest.topologicalOrder().indexOf(3));
DirectedGraph cyclicGraph = new DirectedGraph("3; 0 1; 1 2; 2 0");
DFS dfsCyclicTest = new DFS(cyclicGraph);
Assertions.assertThrows(IllegalArgumentException.class, () -> dfsCyclicTest.topologicalOrderDag());
}
}
......@@ -6,21 +6,25 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
public class DFS {
public
class DFS {
private GraphInterface myGraph;
private boolean[] founded;
private GraphInterface treeDFS;
private int[] fathers;
private ArrayList<Integer> postVisitOrder = new ArrayList<>();
public DFS(GraphInterface g) {
public
DFS(GraphInterface g) {
this.myGraph = g;
this.founded = new boolean[this.myGraph.getOrder()];
this.treeDFS = this.myGraph.create();
}
private void visitaDFS(int sorg) {
private
void visitaDFS(int sorg) {
if (sorg >= this.myGraph.getOrder() || sorg < 0) {
throw new IllegalArgumentException("Sorgente non presenten nel grafo");
}
......@@ -34,12 +38,14 @@ public class DFS {
postVisitOrder.add(sorg);
}
public GraphInterface getTree(int sorg) {
public
GraphInterface getTree(int sorg) {
visitaDFS(sorg);
return this.treeDFS;
}
public GraphInterface getTreeRicoprente(int sorg) throws NotAllNodesReachedException {
public
GraphInterface getTreeRicoprente(int sorg) throws NotAllNodesReachedException {
visitaDFS(sorg);
if (treeDFS.getOrder() != myGraph.getOrder() - 1) {
throw new NotAllNodesReachedException("Esistono due alberi");
......@@ -47,7 +53,8 @@ public class DFS {
return this.treeDFS;
}
public GraphInterface getForest() {
public
GraphInterface getForest() {
for (int node = 0; node < this.myGraph.getOrder(); node++) {
if (!this.founded[node]) {
getTree(node);
......@@ -56,7 +63,63 @@ public class DFS {
return this.treeDFS;
}
public ArrayList<Integer> topologicalOrder() {
private
boolean hasDirCycleImpl(int sorg) {
founded[sorg] = true;
for (Integer node : myGraph.getNeighbors(sorg)) {
if (!founded[node]) {
fathers[node] = sorg;
if (hasDirCycleImpl(node)) {
return true;
}
} else if (fathers[sorg] != -1 && 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()];
Arrays.fill(fathers, -1);
founded = new boolean[myGraph.getOrder()];
Arrays.fill(founded, false);
if (hasDirCycleImpl(i)) {
return true;
}
}
return false;
}
public
ArrayList<Integer> topologicalOrder() {
postVisitOrder.clear();
Arrays.fill(founded, false);
if (myGraph instanceof UndirectedGraph) {
throw new IllegalArgumentException("Graph need to be Directed!");
}
for (int node = 0; node < this.myGraph.getOrder(); node++) {
if (!founded[node]) {
visitaDFS(node);
}
}
ArrayList<Integer> topologicOrder = postVisitOrder;
Collections.reverse(topologicOrder);
return topologicOrder;
}
public
ArrayList<Integer> topologicalOrderDag() {
if (hasDirCycle()) {
throw new IllegalArgumentException("Il grafo non è un DAG");
}
postVisitOrder.clear();
Arrays.fill(founded, false);
if (myGraph instanceof UndirectedGraph) {
......
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