Skip to content
Snippets Groups Projects
Commit 792422e5 authored by Gianluca's avatar Gianluca
Browse files

ok

parent 58f73030
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$/BFS.iml" filepath="$PROJECT_DIR$/BFS.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.GraphInterface;
import it.uniupo.graphLib.UndirectedGraph;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import java.util.ArrayList;
public
class TestBFS {
@Test
void testCostructor() {
UndirectedGraph g = new UndirectedGraph("1");
BFS bfsTest = new BFS(g);
Assertions.assertNotNull(bfsTest);
}
@Test
void testNodesInOrderVisit() {
{
UndirectedGraph g = new UndirectedGraph("4; 0 1; 1 2; 0 3");
BFS bfsTest = new BFS(g);
ArrayList<Integer> nodes = bfsTest.getNodesInOrderOfVisit(0);
Assertions.assertTrue((nodes.indexOf(1) == 1 && nodes.indexOf(2) == 3) || (nodes.indexOf(1) == 3 && nodes.indexOf(2) == 1));
}
{
UndirectedGraph g = new UndirectedGraph("4; 0 1; 1 2; 0 3");
BFS bfsTest = new BFS(g);
ArrayList<Integer> nodes = bfsTest.getNodesInOrderOfVisit(1);
Assertions.assertTrue((nodes.indexOf(1) == 2 && nodes.indexOf(2) == 0) || (nodes.indexOf(1) == 0 && nodes.indexOf(2) == 2));
}
}
@Test
void testDistance() {
{
UndirectedGraph g = new UndirectedGraph("4; 0 1; 1 2; 0 3");
BFS bfsTest = new BFS(g);
int source = 0;
int[] distance = bfsTest.getDistanza(source);
Assertions.assertEquals(distance[source], 0);
Assertions.assertEquals(distance[3], 1);
Assertions.assertEquals(distance[2], 2);
}
{
UndirectedGraph g = new UndirectedGraph("6; 0 1; 0 2; 0 3; 0 4; 0 5;");
BFS bfsTest = new BFS(g);
int source = 0;
int[] distance = bfsTest.getDistanza(source);
for (int i = 1; i < g.getOrder(); ++i) Assertions.assertEquals(distance[i], 1);
}
{
UndirectedGraph g = new UndirectedGraph("6; 0 1; 1 2; 2 3; 3 4; 4 5; 0 5");
BFS bfsTest = new BFS(g);
int source = 0;
int[] distance = bfsTest.getDistanza(source);
Assertions.assertEquals(distance[5], 1);
Assertions.assertEquals(distance[4], 2);
}
}
@Test
void testDistanceNode() {
{
UndirectedGraph g = new UndirectedGraph("6; 0 1; 1 2; 2 3; 3 4;");
BFS bfsTest = new BFS(g);
int source = 0;
int destination = 4;
int distance = bfsTest.getDistanza(source, destination);
Assertions.assertEquals(distance, 4);
}
{
UndirectedGraph g = new UndirectedGraph("6; 0 1; 1 2; 2 3; 3 4;");
BFS bfsTest = new BFS(g);
int source = 0;
int destination = 5;
int distance = bfsTest.getDistanza(source, destination);
Assertions.assertEquals(distance, -1);
}
}
@Test
void testBFSTree() {
{
UndirectedGraph g = new UndirectedGraph("3; 0 1; 1 2; 2 1; 0 2;");
BFS bfsTest = new BFS(g);
int source = 0;
GraphInterface bfSTree = bfsTest.bfsTree(source);
Assertions.assertFalse(bfSTree.hasEdge(1, 2));
Assertions.assertTrue(bfSTree.hasEdge(0, 1) && bfSTree.hasEdge(0, 2));
Assertions.assertEquals(bfSTree.getEdgeNum(), g.getOrder() - 1);
}
{
UndirectedGraph g = new UndirectedGraph("6; 0 1; 1 2; 2 3; 2 4; 0 5; 5 2;");
BFS bfsTest = new BFS(g);
int source = 0;
GraphInterface bfSTree = bfsTest.bfsTree(source);
Assertions.assertFalse(bfSTree.hasEdge(5, 2));
Assertions.assertEquals(bfSTree.getEdgeNum(), g.getOrder() - 1);
}
}
@Test
void testMinPath() {
{
UndirectedGraph g = new UndirectedGraph("3; 0 1; 1 2; 0 2;");
BFS bfsTest = new BFS(g);
int source = 0;
int dest = 2;
ArrayList<Integer> minPath = bfsTest.camminoMinimo(source, dest);
System.out.println(minPath);
Assertions.assertTrue(minPath.size() == 2 && minPath.getFirst() == 0 && minPath.getLast() == 2);
}
}
}
import it.uniupo.graphLib.Edge;
import it.uniupo.graphLib.Graph;
import it.uniupo.graphLib.GraphInterface;
import java.util.*;
public
class BFS {
private final
GraphInterface myGraph;
private final int orderMyGraph;
private boolean[] founded;
public
BFS(GraphInterface g) {
myGraph = g;
orderMyGraph = myGraph.getOrder();
founded = new boolean[orderMyGraph];
}
private
boolean isValidNode(int node) {
return (node >= 0 && node < orderMyGraph);
}
private
ArrayList<Integer> implementationBFS(int sorg) {
Arrays.fill(founded, false);
Queue<Integer> queue = new LinkedList<>();
ArrayList<Integer> visitOrder = new ArrayList<>();
founded[sorg] = true;
queue.add(sorg);
visitOrder.add(sorg);
while (!queue.isEmpty()) {
int extractedNode = queue.remove();
for (Integer neighbor : myGraph.getNeighbors(extractedNode)) {
if (!founded[neighbor]) {
founded[neighbor] = true;
queue.add(neighbor);
visitOrder.add(neighbor);
}
}
}
return visitOrder;
}
private
int[] implementationBFSDistance(int sorg) {
int[] distance = new int[orderMyGraph];
Arrays.fill(distance, -1);
Arrays.fill(founded, false);
Queue<Integer> queue = new LinkedList<>();
founded[sorg] = true;
distance[sorg] = 0;
queue.add(sorg);
while (!queue.isEmpty()) {
int extractedNode = queue.remove();
for (Integer neighbor : myGraph.getNeighbors(extractedNode)) {
if (!founded[neighbor]) {
founded[neighbor] = true;
distance[neighbor] = distance[extractedNode] + 1;
queue.add(neighbor);
}
}
}
return distance;
}
private
GraphInterface implementationBFSTree(int sorg) {
GraphInterface treeBFS = myGraph.create();
Arrays.fill(founded, false);
Queue<Integer> queue = new LinkedList<>();
founded[sorg] = true;
queue.add(sorg);
while (!queue.isEmpty()) {
int extractedNode = queue.remove();
for (Integer neighbor : myGraph.getNeighbors(extractedNode)) {
if (!founded[neighbor]) {
founded[neighbor] = true;
treeBFS.addEdge(extractedNode, neighbor);
queue.add(neighbor);
}
}
}
return treeBFS;
}
private
int[] implementationBFSFathers(int sorg) {
int[] fathers = new int[orderMyGraph];
Arrays.fill(founded, false);
Arrays.fill(fathers, -1);
Queue<Integer> queue = new LinkedList<>();
founded[sorg] = true;
fathers[sorg] = sorg;
queue.add(sorg);
while (!queue.isEmpty()) {
int extractedNode = queue.remove();
for (Integer neighbor : myGraph.getNeighbors(extractedNode)) {
if (!founded[neighbor]) {
founded[neighbor] = true;
queue.add(neighbor);
fathers[neighbor] = extractedNode;
}
}
}
return fathers;
}
public
ArrayList<Integer>
getNodesInOrderOfVisit(int source) {
if (!isValidNode(source)) throw new IllegalArgumentException("Errore");
return implementationBFS(source);
}
public
int[] getDistanza(int sorgente) {
if (!isValidNode(sorgente)) throw new IllegalArgumentException("Errore");
return implementationBFSDistance(sorgente);
}
public
int getDistanza(int sorgente, int nodo) {
if (!isValidNode(sorgente) && !isValidNode(nodo)) {
throw new IllegalArgumentException("Sorgente o nodo non valide");
}
return implementationBFSDistance(sorgente)[nodo];
}
public
GraphInterface bfsTree(int sorgente) {
if (!isValidNode(sorgente)) throw new IllegalArgumentException("Errore");
return implementationBFSTree(sorgente);
}
public
ArrayList<Integer> camminoMinimo(int sorgente, int dest) {
if (!isValidNode(sorgente) && !isValidNode(dest)) {
throw new IllegalArgumentException("Sorgente o nodo non valide");
}
int[] fathers = implementationBFSFathers(sorgente);
if (fathers[dest] == -1) return null;
int node = dest;
ArrayList<Integer> minPath = new ArrayList<>();
while (node != sorgente) {
minPath.add(node);
node = fathers[node];
}
minPath.add(sorgente);
Collections.reverse(minPath);
return minPath;
}
}
No preview for this file type
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