Skip to content
Snippets Groups Projects
Commit 345655ca authored by Gianluca's avatar Gianluca
Browse files

Male male

parent 0baf1ba1
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$/NumberOfCC.iml" filepath="$PROJECT_DIR$/NumberOfCC.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.UndirectedGraph;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
public
class TestBFS {
@Test
void TestCostructor() {
UndirectedGraph g = new UndirectedGraph("3; 0 1; 1 2; 0 2");
BFS bfsTest = new BFS(g);
Assertions.assertNotNull(bfsTest);
}
@Test
void TestCC() {
UndirectedGraph g = new UndirectedGraph("3;");
BFS bfsTest = new BFS(g);
System.out.println(bfsTest.getCC());
}
}
import it.uniupo.graphLib.GraphInterface;
import it.uniupo.graphLib.GraphUtils;
import javax.sound.sampled.Line;
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 orderGraph;
private boolean[] founded;
public
BFS(GraphInterface g) {
myGraph = g;
orderGraph = myGraph.getOrder();
founded = new boolean[orderGraph];
}
private
ArrayList<Integer> visitBFS(int source) {
ArrayList<Integer> orderVisit = new ArrayList<>();
founded[source] = true;
Queue<Integer> queueBFS = new LinkedList<>();
queueBFS.add(source);
orderVisit.add(source);
while (!queueBFS.isEmpty()) {
int node = queueBFS.remove();
for (Integer neighbor : myGraph.getNeighbors(node)) {
if (!founded[neighbor]) {
queueBFS.add(neighbor);
founded[neighbor] = true;
orderVisit.add(neighbor);
}
}
}
return orderVisit;
}
public
int getCC() {
Arrays.fill(founded, false);
int[] connectedComponentsArr = new int[orderGraph];
Arrays.fill(connectedComponentsArr, -1);
int cnt = 0;
for (int node = 0; node < orderGraph; ++node) {
if (!founded[node]) {
ArrayList<Integer> nodes = visitBFS(node);
for (int n : nodes) {
connectedComponentsArr[n] = cnt;
}
cnt++;
}
}
return cnt;
}
}
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