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

Lab2 Algo

parent 08a78cfd
No related branches found
No related tags found
No related merge requests found
import it.uniupo.graphLib.DirectedGraph;
import it.uniupo.graphLib.GraphInterface;
import it.uniupo.graphLib.UndirectedGraph;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import static org.junit.jupiter.api.Assertions.*;
public class TestGraph {
@Test
void testCreate() {
GraphInterface grafo = new DirectedGraph(3);
BFS bfsTest = new BFS(grafo);
assertNotNull(bfsTest);
}
@Test
void testInvalidSorgente() {
GraphInterface grafo = new DirectedGraph("1");
BFS bfsTest = new BFS(grafo);
assertThrows(IllegalArgumentException.class, () -> bfsTest.getNodesInOrderOfVisit(3));
}
@Test
@Timeout(value = 500)
void testScoperti() {
GraphInterface grafo = new DirectedGraph("3;0 1;1 2;2 0");
BFS bfsTest = new BFS(grafo);
assertTrue(bfsTest.getNodesInOrderOfVisit(0) != null);
}
@Test
void testNumeroNodiVisitati() {
//Un solo nodo
GraphInterface grafo = new UndirectedGraph("1");
BFS bfsTest = new BFS(grafo);
assertEquals(1, bfsTest.getNodesInOrderOfVisit(0).size());
//Due nodi ed un arco
grafo = new UndirectedGraph("2; 0 1");
bfsTest = new BFS(grafo);
assertEquals(2, bfsTest.getNodesInOrderOfVisit(0).size());
//Quattro nodi e quattro archi
grafo = new UndirectedGraph("4;0 2;0 1;2 3;1 3");
bfsTest = new BFS(grafo);
assertEquals(4, bfsTest.getNodesInOrderOfVisit(2).size());
}
@Test
void testBFSOrder() {
GraphInterface grafo = new UndirectedGraph("4;0 2;0 1;2 3;1 3");
BFS bfsTest = new BFS(grafo);
assertTrue(bfsTest.getNodesInOrderOfVisit(2).get(2) == 0 || bfsTest.getNodesInOrderOfVisit(2).get(2) == 3);
}
@Test
public void testInitNumeroNodiVisitati() {
GraphInterface grafo = new UndirectedGraph("4;0 2;0 1;2 3;1 3");
BFS bfsTest = new BFS(grafo); //<<- creato una volta sola
int numeroNodi = bfsTest.getNodesInOrderOfVisit(0).size();//<<-prima chiamata del metodo
assertEquals(4, numeroNodi);
numeroNodi = bfsTest.getNodesInOrderOfVisit(2).size(); //<<-seconda chiamata, stesso oggetto, parametro diverso
assertEquals(4, numeroNodi);
numeroNodi = bfsTest.getNodesInOrderOfVisit(3).size(); //<<-terza chiamata, stesso oggetto, parametro diverso
assertEquals(4, numeroNodi);
}
}
<?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$/TestGraph" 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://$USER_HOME$/Downloads/graphLib.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$USER_HOME$/Downloads/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>
<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
File added
File added
File added
import it.uniupo.graphLib.GraphInterface;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class BFS {
private GraphInterface myGraph;
public BFS(GraphInterface g) {
//System.out.println(g);
this.myGraph = g;
}
public ArrayList<Integer> getNodesInOrderOfVisit(int sorgente) {
if (this.myGraph.getOrder() <= sorgente) {
throw new IllegalArgumentException("Dimensione della Sorgente errata");
}
ArrayList<Integer> s = new ArrayList<>();
ArrayList<Integer> valueToReturn = new ArrayList<>();
s.add(sorgente);
valueToReturn.add(sorgente);
Queue<Integer> q = new LinkedList<>();
q.add(sorgente);
while (!q.isEmpty()) {
int u = q.remove();
List<Integer> neighbors = (List<Integer>) this.myGraph.getNeighbors(u);
for (Integer neighbor : neighbors) {
if (!s.contains(neighbor)) {
s.add(neighbor);
q.add(neighbor);
valueToReturn.add(neighbor);
}
}
}
return valueToReturn;
}
}
import it.uniupo.graphLib.Graph;
import it.uniupo.graphLib.UndirectedGraph;
public class Main {
public static void main(String[] args) {
Graph mioGrafo = new UndirectedGraph("3; 0 1; 1 2; 0 2;"); //Num Nodi + Archi
BFS prova = new BFS(mioGrafo);
System.out.println(prova.getNodesInOrderOfVisit(0));
}
}
\ 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