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

ok

parent 792422e5
No related branches found
No related tags found
No related merge requests found
Showing
with 434 additions and 0 deletions
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
BFSDFSApplications
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/BFSDFSApplications.iml" filepath="$PROJECT_DIR$/BFSDFSApplications.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../../../.." vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/Test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../../graphLib.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MODULE_DIR$/../../graphLib.jar!/doc" />
</JAVADOC>
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
\ No newline at end of file
import it.uniupo.graphLib.DirectedGraph;
import it.uniupo.graphLib.Graph;
import it.uniupo.graphLib.UndirectedGraph;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public
class TestBFS {
/*
@Test
void testCycle() {
Graph g = new UndirectedGraph("4; 1 0; 0 2; 2 3; 3 0");
Assertions.assertTrue((new BFS(g)).hasUndirectedCycle());
g = new UndirectedGraph("5; 0 1; 1 2; 2 3; 3 4; 4 0");
Assertions.assertTrue((new BFS(g)).hasUndirectedCycle());
}
@Test
void testNoCycle() {
Graph g = new UndirectedGraph("3; 0 1; 0 2");
Assertions.assertFalse((new BFS(g)).hasUndirectedCycle());
}
*/
@Test
void testNodesOrderPostVisit() {
Graph g = new UndirectedGraph("4; 2 0; 1 0; 3 2; 3 1");
ArrayList<Integer> order = (new BFS(g)).getNodesInOrderPostVisit(0);
ArrayList<Integer> a = new ArrayList<>(List.of(1, 3, 2, 0));
ArrayList<Integer> b = new ArrayList<>(List.of(2, 1, 3, 0));
Assertions.assertTrue(order.equals(a) || order.equals(b));
}
@Test
void hasUndirectedCycle() {
Graph g = new UndirectedGraph("4; 1 0; 0 2; 2 3; 3 0");
Assertions.assertTrue((new BFS(g)).hasUndirectedCycle());
g = new UndirectedGraph("4; 1 0; 2 0; 3 0");
Assertions.assertFalse((new BFS(g)).hasUndirectedCycle());
}
/*
@Test
void hasDirectedCycle() {
Graph g = new DirectedGraph("1;");
Assertions.assertFalse((new BFS(g)).hasDirectedCycle());
g = new DirectedGraph("2; 0 1");
Assertions.assertFalse((new BFS(g)).hasDirectedCycle());
g = new DirectedGraph("3; 1 0; 1 2; 0 2");
Assertions.assertFalse((new BFS(g)).hasDirectedCycle());
g = new DirectedGraph("3; 0 2; 2 1; 1 0");
Assertions.assertTrue((new BFS(g)).hasDirectedCycle());
g = new DirectedGraph("5; 0 4; 4 1; 4 2; 3 4; 2 3");
Assertions.assertTrue((new BFS(g)).hasDirectedCycle());
}
@Test
void getDirCycle() {
Graph g = new DirectedGraph("1;");
System.out.println((new BFS(g)).getDirCycle());
}
*/
}
\ No newline at end of file
import it.uniupo.graphLib.DirectedGraph;
import it.uniupo.graphLib.Graph;
import it.uniupo.graphLib.UndirectedGraph;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public
class TestDFS {
@Test
void testCycle() {
Graph g = new UndirectedGraph("4; 1 0; 0 2; 2 3; 3 0");
Assertions.assertTrue((new DFS(g)).hasUndirectedCycle());
g = new UndirectedGraph("5; 0 1; 1 2; 2 3; 3 4; 4 0");
Assertions.assertTrue((new DFS(g)).hasUndirectedCycle());
}
@Test
void testNoCycle() {
Graph g = new UndirectedGraph("3; 0 1; 0 2");
Assertions.assertFalse((new DFS(g)).hasUndirectedCycle());
}
@Test
void testNodesOrderPostVisit() {
Graph g = new UndirectedGraph("4; 2 0; 1 0; 3 2; 3 1");
ArrayList<Integer> order = (new DFS(g)).getNodesInOrderPostVisit(0);
ArrayList<Integer> a = new ArrayList<>(List.of(1, 3, 2, 0));
ArrayList<Integer> b = new ArrayList<>(List.of(2, 1, 3, 0));
Assertions.assertTrue(order.equals(a) || order.equals(b));
}
@Test
void hasUndirectedCycle() {
Graph g = new UndirectedGraph("4; 1 0; 0 2; 2 3; 3 0");
Assertions.assertTrue((new DFS(g)).hasUndirectedCycle());
g = new UndirectedGraph("4; 1 0; 2 0; 3 0");
Assertions.assertFalse((new DFS(g)).hasUndirectedCycle());
}
@Test
void hasDirectedCycle() {
Graph g = new DirectedGraph("1;");
Assertions.assertFalse((new DFS(g)).hasDirectedCycle());
g = new DirectedGraph("2; 0 1");
Assertions.assertFalse((new DFS(g)).hasDirectedCycle());
g = new DirectedGraph("3; 1 0; 1 2; 0 2");
Assertions.assertFalse((new DFS(g)).hasDirectedCycle());
g = new DirectedGraph("3; 0 2; 2 1; 1 0");
Assertions.assertTrue((new DFS(g)).hasDirectedCycle());
g = new DirectedGraph("5; 0 4; 4 1; 4 2; 3 4; 2 3");
Assertions.assertTrue((new DFS(g)).hasDirectedCycle());
}
@Test
void getDirCycle() {
Graph g = new DirectedGraph("6; 0 1; 2 0; 1 2; 2 3; 3 0; 3 4; 4 5");
System.out.println((new DFS(g)).getDirCycle());
}
}
import it.uniupo.graphLib.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public
class BFS {
private final GraphInterface myGraph;
private final int orderMyGraph;
private boolean[] founded;
private int[] fathers;
public
BFS(GraphInterface g) {
myGraph = g;
orderMyGraph = myGraph.getOrder();
founded = new boolean[orderMyGraph];
fathers = new int[orderMyGraph];
}
private
boolean visitBFSCycles(int sorg) {
founded[sorg] = true;
Queue<Integer> queue = new LinkedList<>();
queue.add(sorg);
while (!queue.isEmpty()) {
int node = queue.remove();
for (Integer neighbor : myGraph.getNeighbors(node)) {
if (!founded[neighbor]) {
founded[neighbor] = true;
queue.add(neighbor);
fathers[neighbor] = node;
} else if (neighbor != fathers[node]) {
return true;
}
}
}
return false;
}
private
void inizializeIstanceVars() {
Arrays.fill(founded, false);
Arrays.fill(fathers, -1);
}
/* Restituisce true se il grafo non
* orientato ha un ciclo non banale (comportamento indefinito su grafo orientato):
* potete implementarlo prima solo per un grafo non orientato connesso, poi per il
* caso generale
*/
public
boolean hasUndirectedCycle() {
if (myGraph instanceof DirectedGraph) throw new IllegalArgumentException("Invalid GraphType");
inizializeIstanceVars();
return visitBFSCycles(0);
}
public
ArrayList<Integer> getNodesInOrderPostVisit(int i) {
inizializeIstanceVars();
return null;
}
}
import it.uniupo.graphLib.*;
import java.util.ArrayList;
import java.util.Arrays;
public
class DFS {
private final GraphInterface myGraph;
private final int orderMyGraph;
private boolean[] founded;
private int[] fathers;
private boolean[] terminated;
private int startCycleNode = -1;
private int endCycleNode = -1;
public
DFS(GraphInterface g) {
myGraph = g;
orderMyGraph = myGraph.getOrder();
founded = new boolean[orderMyGraph];
fathers = new int[orderMyGraph];
terminated = new boolean[orderMyGraph];
}
private
void visitDFSDirectedCycleNodes(int source, GraphInterface DFSTree) {
founded[source] = true;
for (Integer neighbor : myGraph.getNeighbors(source)) {
if (!founded[neighbor]) {
founded[neighbor] = true;
fathers[neighbor] = source;
DFSTree.addEdge(source, neighbor);
visitDFSDirectedCycleNodes(neighbor, DFSTree);
} else if (!terminated[neighbor]) {
startCycleNode = neighbor;
endCycleNode = source;
return;
}
}
terminated[source] = true;
}
private
boolean visitDFSDirectedCycle(int source) {
founded[source] = true;
for (Integer neighbor : myGraph.getNeighbors(source)) {
if (!founded[neighbor]) {
founded[neighbor] = true;
if (visitDFSDirectedCycle(neighbor)) {
return true;
}
} else if (!terminated[neighbor]) {
return true;
}
}
terminated[source] = true;
return false;
}
private
boolean visitDFSUndirectedCyles(int source) {
founded[source] = true;
for (Integer neighbor : myGraph.getNeighbors(source)) {
if (!founded[neighbor]) {
fathers[neighbor] = source;
if (visitDFSUndirectedCyles(neighbor)) {
return true;
}
} else if (neighbor != fathers[source]) {
return true;
}
}
return false;
}
private
void visitDFSPostOrder(int source, ArrayList<Integer> postOrder) {
founded[source] = true;
for (Integer neighbor : myGraph.getNeighbors(source)) {
if (!founded[neighbor]) {
founded[neighbor] = true;
visitDFSPostOrder(neighbor, postOrder);
}
}
postOrder.add(source);
}
private
void inizializeIstanceVars() {
Arrays.fill(founded, false);
Arrays.fill(fathers, -1);
Arrays.fill(terminated, false);
}
public
boolean hasUndirectedCycle() {
inizializeIstanceVars();
if (myGraph instanceof DirectedGraph) throw new IllegalArgumentException("Invalid Graph Type");
return visitDFSUndirectedCyles(0);
}
public
ArrayList<Integer> getNodesInOrderPostVisit(int sorgente) {
inizializeIstanceVars();
ArrayList<Integer> postOrder = new ArrayList<>();
visitDFSPostOrder(sorgente, postOrder);
return postOrder;
}
public
boolean hasDirectedCycle() {
inizializeIstanceVars();
if (myGraph instanceof UndirectedGraph) throw new IllegalArgumentException("Invalid Graph Type");
return visitDFSDirectedCycle(0);
}
public
ArrayList<Integer> getDirCycle() {
inizializeIstanceVars();
GraphInterface DFSTree = myGraph.create();
visitDFSDirectedCycleNodes(0, DFSTree);
if (startCycleNode == -1 && endCycleNode == -1) return null;
int temp = endCycleNode;
System.out.println(DFSTree);
ArrayList<Integer> cycleNodes = new ArrayList<>();
while (fathers[temp] == startCycleNode) {
System.out.println(temp);
cycleNodes.add(temp);
temp = fathers[temp];
}
cycleNodes.add(temp);
return cycleNodes;
}
}
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