Skip to content
Snippets Groups Projects
Commit 355b5d74 authored by Gianluca's avatar Gianluca
Browse files

lab

parent e19635f6
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$/Clustering.iml" filepath="$PROJECT_DIR$/Clustering.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>
<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.Test;
public class ClusteringTest {
@Test
void dada() {
UndirectedGraph g = new UndirectedGraph("8; 0 1 11; 0 7 9; 1 4 1; 1 7 1; 1 2 2; 2 3 3; 3 4 1; 4 5 2; 5 7 8");
Clustering clu = new Clustering(g, 5);
}
@Test
public void test4Clusters() {
UndirectedGraph g = new UndirectedGraph("4; 0 1 2; 1 3 7; 3 2 4; 2 0 1");
Clustering clustering = new Clustering(g, 4);
Assertions.assertFalse(clustering.sameCluster(0, 1));
Assertions.assertFalse(clustering.sameCluster(0, 2));
Assertions.assertFalse(clustering.sameCluster(0, 3));
Assertions.assertFalse(clustering.sameCluster(1, 2));
Assertions.assertFalse(clustering.sameCluster(1, 3));
Assertions.assertFalse(clustering.sameCluster(2, 3));
Assertions.assertEquals(1, clustering.spaziamento());
}
@Test
public void test3Clusters() {
UndirectedGraph g = new UndirectedGraph("4; 0 1 2; 1 3 7; 3 2 4; 2 0 1");
Clustering clustering = new Clustering(g, 3);
Assertions.assertTrue(clustering.sameCluster(0, 2));
Assertions.assertTrue(clustering.sameCluster(2, 0)); //Solo per vedere se la sameCluster è implementata bene
Assertions.assertFalse(clustering.sameCluster(0, 1));
Assertions.assertFalse(clustering.sameCluster(0, 3));
Assertions.assertFalse(clustering.sameCluster(1, 2));
Assertions.assertFalse(clustering.sameCluster(1, 3));
Assertions.assertFalse(clustering.sameCluster(2, 3));
Assertions.assertEquals(2, clustering.spaziamento());
}
@Test
public void test2Clusters() {
UndirectedGraph g = new UndirectedGraph("4; 0 1 2; 1 3 7; 3 2 4; 2 0 1");
Clustering clustering = new Clustering(g, 2);
Assertions.assertTrue(clustering.sameCluster(0, 2) && clustering.sameCluster(0, 1) && clustering.sameCluster(1, 2));
for (int i = 0; i < 3; ++i) {
Assertions.assertFalse(clustering.sameCluster(i, 3));
}
Assertions.assertEquals(4, clustering.spaziamento());
}
@Test
public void test1Clusters() {
UndirectedGraph g = new UndirectedGraph("4; 0 1 2; 1 3 7; 3 2 4; 2 0 1");
Clustering clustering = new Clustering(g, 1);
System.out.println(clustering.getClusters());
System.out.println(clustering.spaziamento());
for (int i = 0; i <= 3; ++i) {
for (int j = 0; j <= 3; ++j) {
if (i != j) {
Assertions.assertTrue(clustering.sameCluster(i, j));
}
}
}
Assertions.assertEquals(-99, clustering.spaziamento()); //Di fatto non esiste spaziamento
}
}
import it.uniupo.algoTools.MinHeap;
import it.uniupo.algoTools.UnionByRank;
import it.uniupo.algoTools.UnionFind;
import it.uniupo.graphLib.Edge;
import it.uniupo.graphLib.UndirectedGraph;
public class Clustering {
private final UndirectedGraph myGraph;
private final int clusterCnt;
public Clustering(UndirectedGraph g, int numCluster) {
myGraph = g;
clusterCnt = numCluster;
}
private int kruskalClustering(UnionFind unionFind) {
MinHeap<Edge, Integer> minHeap = new MinHeap<>();
for (int u = 0; u < myGraph.getOrder(); ++u) {
for (Edge uv : myGraph.getOutEdges(u)) {
minHeap.add(uv, uv.getWeight());
}
}
while (!minHeap.isEmpty() && unionFind.getNumberOfSets() > clusterCnt) {
Edge e = minHeap.extractMin();
int tail = e.getTail();
int head = e.getHead();
int tailLeader = unionFind.find(tail);
int headLeader = unionFind.find(head);
unionFind.union(tailLeader, headLeader);
}
if (minHeap.isEmpty()) return -99; //Se non ci sono più archi
boolean isAValidEdge = false;
int spaziamento = -99; //Se non ci sono più archi validi
while (!minHeap.isEmpty() && !isAValidEdge) {
Edge e = minHeap.extractMin();
int tail = e.getTail();
int head = e.getHead();
int tailLeader = unionFind.find(tail);
int headLeader = unionFind.find(head);
if (tailLeader != headLeader) {
isAValidEdge = true;
spaziamento = e.getWeight();
}
}
return spaziamento;
}
public UnionFind getClusters() {
UnionFind unionFind = new UnionByRank(myGraph.getOrder());
kruskalClustering(unionFind);
return unionFind;
}
public int spaziamento() {
UnionFind unionFind = new UnionByRank(myGraph.getOrder());
return kruskalClustering(unionFind);
}
public boolean sameCluster(int a, int b) {
UnionFind unionFind = new UnionByRank(myGraph.getOrder());
kruskalClustering(unionFind);
return unionFind.find(a) == unionFind.find(b);
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment