Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development: Remove atlas dependency on jgrapht and apfloat #9342

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import jakarta.validation.constraints.NotNull;

import org.jgrapht.alg.util.UnionFind;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

Expand All @@ -24,6 +23,7 @@
import de.tum.cit.aet.artemis.atlas.domain.competency.RelationType;
import de.tum.cit.aet.artemis.atlas.dto.NgxLearningPathDTO;
import de.tum.cit.aet.artemis.atlas.repository.CompetencyRelationRepository;
import de.tum.cit.aet.artemis.atlas.service.util.UnionFind;
import de.tum.cit.aet.artemis.core.domain.User;
import de.tum.cit.aet.artemis.exercise.domain.Exercise;
import de.tum.cit.aet.artemis.lecture.domain.LectureUnit;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package de.tum.cit.aet.artemis.atlas.service.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class UnionFind<T> {

private final Map<T, T> parentMap = new LinkedHashMap();

private final Map<T, Integer> rankMap = new HashMap();

private int count;

public UnionFind(Collection<T> elements) {
for (var element : elements) {
parentMap.put(element, element);
rankMap.put(element, 0);
}
count = elements.size();
}

/**
* Adds an element to the UnionFind data structure.
*
* @param element the element
*/
public void addElement(T element) {
if (parentMap.containsKey(element)) {
return;
}
parentMap.put(element, element);
rankMap.put(element, 0);
count++;
}

/**
* Finds the representative element of the set that the given element is in.
*
* @param element the element
* @return the representative element of the set
*/
public T find(T element) {
if (!this.parentMap.containsKey(element)) {
throw new IllegalArgumentException("element is not contained in this UnionFind data structure: " + element);
}
else {
T current = element;

while (true) {
T root = this.parentMap.get(current);
if (root.equals(current)) {
root = current;

T parent;
for (current = element; !current.equals(root); current = parent) {
parent = this.parentMap.get(current);
this.parentMap.put(current, root);
}

return root;
}

current = root;
}
}
}

/**
* Unions two sets that are represented by the given elements.
*
* @param element1 the first element
* @param element2 the second element
*/
public void union(T element1, T element2) {
if (!this.parentMap.containsKey(element1)) {
throw new IllegalArgumentException("element1 is not contained in this UnionFind data structure: " + element1);
}
else if (!this.parentMap.containsKey(element2)) {
throw new IllegalArgumentException("element2 is not contained in this UnionFind data structure: " + element2);
}
T parent1 = this.find(element1);
T parent2 = this.find(element2);
if (!parent1.equals(parent2)) {
int rank1 = this.rankMap.get(parent1);
int rank2 = this.rankMap.get(parent2);
if (rank1 > rank2) {
this.parentMap.put(parent2, parent1);
}
else if (rank1 < rank2) {
this.parentMap.put(parent1, parent2);
}
else {
this.parentMap.put(parent2, parent1);
this.rankMap.put(parent1, rank1 + 1);
}

this.count--;
}
}

/**
* Checks if two elements are in the same set.
*
* @param element1 the first element
* @param element2 the second element
* @return true if the elements are in the same set, false otherwise
*/
public boolean inSameSet(T element1, T element2) {
return this.find(element1).equals(this.find(element2));
}

/**
* Returns the number of sets.
*
* @return the number of sets
*/
public int numberOfSets() {
return this.count;
}

/**
* Returns the number of elements in the union find data structure.
*
* @return the number of elements
*/
public int size() {
return this.parentMap.size();
}
}
83 changes: 83 additions & 0 deletions src/test/java/de/tum/cit/aet/artemis/UnionFindTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package de.tum.cit.aet.artemis;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import de.tum.cit.aet.artemis.atlas.service.util.UnionFind;

class UnionFindTest {

private UnionFind<Integer> unionFind;

@BeforeEach
void setUp() {
List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5);
unionFind = new UnionFind<>(elements);
}

@Test
void testInitialization() {
assertThat(unionFind.size()).isEqualTo(5);
assertThat(unionFind.numberOfSets()).isEqualTo(5);
}

@Test
void testAddElement() {
unionFind.addElement(6);
assertThat(unionFind.size()).isEqualTo(6);
assertThat(unionFind.numberOfSets()).isEqualTo(6);
}

@Test
void testFind() {
assertThat(unionFind.find(1)).isEqualTo(1);
assertThat(unionFind.find(2)).isEqualTo(2);
}

@Test
void testUnion() {
unionFind.union(1, 2);
assertThat(unionFind.numberOfSets()).isEqualTo(4);
assertThat(unionFind.inSameSet(1, 2)).isTrue();
}

@Test
void testInSameSet() {
unionFind.union(1, 2);
assertThat(unionFind.inSameSet(1, 2)).isTrue();
assertThat(unionFind.inSameSet(1, 3)).isFalse();
}

@Test
void testNumberOfSets() {
unionFind.union(1, 2);
unionFind.union(3, 4);
assertThat(unionFind.numberOfSets()).isEqualTo(3);
}

@Test
void testSize() {
assertThat(unionFind.size()).isEqualTo(5);
unionFind.addElement(6);
assertThat(unionFind.size()).isEqualTo(6);
}

@Test
void testComplexSequence() {
unionFind.union(1, 2);
unionFind.union(3, 4);
unionFind.union(1, 3);
assertThat(unionFind.numberOfSets()).isEqualTo(2);
assertThat(unionFind.inSameSet(1, 2)).isTrue();
assertThat(unionFind.inSameSet(1, 3)).isTrue();
assertThat(unionFind.inSameSet(1, 4)).isTrue();
assertThat(unionFind.inSameSet(2, 3)).isTrue();
assertThat(unionFind.inSameSet(2, 4)).isTrue();
assertThat(unionFind.inSameSet(3, 4)).isTrue();
}
}
Loading