Skip to content
Snippets Groups Projects
Commit 3d3c24ef authored by Gianluca Mastrolonardo's avatar Gianluca Mastrolonardo
Browse files

da finire es ma funzionante

parent 0603a067
No related branches found
No related tags found
No related merge requests found
### 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
<?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$/Lab5.iml" filepath="$PROJECT_DIR$/Lab5.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.GraphInterface;
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;
public class TestLab {
@Test
void testIsConnected() {
UndirectedGraph gOneNode = new UndirectedGraph("1;");
BFS bfsTest = new BFS(gOneNode);
Assertions.assertTrue(bfsTest.isConnected());
UndirectedGraph gConnected = new UndirectedGraph("3; 0 1; 1 2; 2 0");
BFS bfsTest2 = new BFS(gConnected);
Assertions.assertTrue(bfsTest2.isConnected());
UndirectedGraph gUnonnected = new UndirectedGraph("5; 0 1; 0 2");
BFS bfsTest3 = new BFS(gUnonnected);
Assertions.assertFalse(bfsTest3.isConnected());
}
@Test
void testConnectedComponents() {
UndirectedGraph gOneNode = new UndirectedGraph("1");
BFS bfsTest = new BFS(gOneNode);
int[] cc1 = bfsTest.connectedComponents();
Assertions.assertEquals(cc1[0], 0);
UndirectedGraph gConnected = new UndirectedGraph("2; 0 1;");
BFS bfsTest2 = new BFS(gConnected);
int[] cc2 = bfsTest2.connectedComponents();
Assertions.assertEquals(cc2[0], cc2[1]);
UndirectedGraph gUnconnected = new UndirectedGraph("2;");
BFS bfsTest3 = new BFS(gUnconnected);
int[] cc3 = bfsTest3.connectedComponents();
Assertions.assertNotEquals(cc3[0], cc3[1]);
UndirectedGraph gBig = new UndirectedGraph("6; 0 4; 0 1; 2 5;");
BFS bfsTestBig = new BFS(gBig);
int[] ccBig = bfsTestBig.connectedComponents();
Assertions.assertEquals(ccBig[0], ccBig[1]);
Assertions.assertEquals(ccBig[0], ccBig[4]);
Assertions.assertEquals(ccBig[2], ccBig[5]);
Assertions.assertNotEquals(ccBig[3], ccBig[1]);
}
@Test
void testTopologicalOrder() {
DirectedGraph gOneNode = new DirectedGraph("1");
DFS dfsTest = new DFS(gOneNode);
Assertions.assertEquals(dfsTest.topologicalOrder().getFirst(), 0);
DirectedGraph gTwoNodes = new DirectedGraph("2; 0 1;");
DFS dfsTest2 = new DFS(gTwoNodes);
Assertions.assertTrue(dfsTest2.topologicalOrder().indexOf(0) < dfsTest2.topologicalOrder().indexOf(1));
DirectedGraph gThreeNodes = new DirectedGraph("3; 2 0;");
DFS dfsTest3 = new DFS(gThreeNodes);
System.out.println(dfsTest3.topologicalOrder());
ArrayList<Integer> topologicOrder = dfsTest3.topologicalOrder();
//DA FINIRE MA FUNZIONA
}
}
import it.uniupo.graphLib.*;
import java.awt.List;
import java.util.*;
public class BFS {
private GraphInterface myGraph;
private boolean[] founded;
public BFS(GraphInterface g) {
//System.out.println(g);
this.myGraph = g;
this.founded = new boolean[this.myGraph.getOrder()];
}
public ArrayList<Integer> getNodesInOrderOfVisit(int sorgente) {
if (this.myGraph.getOrder() <= sorgente) {
throw new IllegalArgumentException("Sorgente errata");
}
ArrayList<Integer> returnList = new ArrayList<>();
returnList.add(sorgente);
Queue<Integer> q = new LinkedList<>();
founded[sorgente] = true;
q.add(sorgente);
while (!q.isEmpty()) {
int u = q.remove();
Iterable<Integer> neighbors = this.myGraph.getNeighbors(u);
for (int v : neighbors) {
if (!founded[v]) {
founded[v] = true;
q.add(v);
returnList.add(v);
}
}
}
return returnList;
}
public boolean isConnected() {
if (myGraph == null) {
return true;
}
ArrayList<Integer> nodes = getNodesInOrderOfVisit(0);
return nodes.size() == myGraph.getOrder();
}
public int[] connectedComponents() {
int[] connectedComponentsArr = new int[myGraph.getOrder()];
Arrays.fill(connectedComponentsArr, -1);
Arrays.fill(founded, false);
int cnt = 0;
for (int node = 0; node < myGraph.getOrder(); node++) {
if (!founded[node]) {
ArrayList<Integer> nodes = getNodesInOrderOfVisit(node);
for (int n : nodes) {
connectedComponentsArr[n] = cnt;
}
cnt++;
}
}
return connectedComponentsArr;
}
}
import it.uniupo.graphLib.GraphInterface;
import it.uniupo.graphLib.UndirectedGraph;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
public class DFS {
private GraphInterface myGraph;
private boolean[] founded;
private GraphInterface treeDFS;
private ArrayList<Integer> postVisitOrder = new ArrayList<>();
public DFS(GraphInterface g) {
this.myGraph = g;
this.founded = new boolean[this.myGraph.getOrder()];
this.treeDFS = this.myGraph.create();
}
private void visitaDFS(int sorg) {
if (sorg >= this.myGraph.getOrder() || sorg < 0) {
throw new IllegalArgumentException("Sorgente non presenten nel grafo");
}
founded[sorg] = true;
for (int node : this.myGraph.getNeighbors(sorg)) {
if (!founded[node]) {
treeDFS.addEdge(sorg, node);
visitaDFS(node);
}
}
postVisitOrder.add(sorg);
}
public GraphInterface getTree(int sorg) {
visitaDFS(sorg);
return this.treeDFS;
}
public GraphInterface getTreeRicoprente(int sorg) throws NotAllNodesReachedException {
visitaDFS(sorg);
if (treeDFS.getOrder() != myGraph.getOrder() - 1) {
throw new NotAllNodesReachedException("Esistono due alberi");
}
return this.treeDFS;
}
public GraphInterface getForest() {
for (int node = 0; node < this.myGraph.getOrder(); node++) {
if (!this.founded[node]) {
getTree(node);
}
}
return this.treeDFS;
}
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 class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
public class NotAllNodesReachedException extends Exception {
public NotAllNodesReachedException() {
super();
}
public NotAllNodesReachedException(String messaggio) {
super(messaggio);
}
}
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