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

#415 disjoint sets impl #421

Merged
merged 3 commits into from
Mar 21, 2020
Merged
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
24 changes: 23 additions & 1 deletion src/main/java/org/jpeek/graph/Disjoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package org.jpeek.graph;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.cactoos.Scalar;
Expand All @@ -48,6 +50,26 @@ public Disjoint(final Graph graph) {

@Override
public List<Set<Node>> value() throws Exception {
throw new UnsupportedOperationException(this.graph.toString());
final Set<Node> unvisited = new HashSet<>(this.graph.nodes());
final List<Set<Node>> result = new ArrayList<>(unvisited.size());
while (!unvisited.isEmpty()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HDouss Sorry, I could catch the whole algorithm, but is it possible to use iterator.hasNext() for the unvisited?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fanifieiev it is not really possible, unless to get a new iterator from the set in each iteration. This set is getting shrinked (by more than one element) in each iteration. Even the element at which the iterator is pointing is being removed from the set.

final Node node = unvisited.iterator().next();
final Set<Node> visiting = new HashSet<>();
visiting.add(node);
final Set<Node> current = new HashSet<>();
while (!visiting.isEmpty()) {
final Node visit = visiting.iterator().next();
current.add(visit);
for (final Node connexion:visit.connections()) {
if (!current.contains(connexion)) {
visiting.add(connexion);
}
}
unvisited.remove(visit);
visiting.remove(visit);
}
result.add(current);
}
return result;
}
}
34 changes: 32 additions & 2 deletions src/test/java/org/jpeek/graph/DisjointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Set;
import org.cactoos.list.ListOf;
import org.hamcrest.core.AllOf;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.HasValuesMatching;
Expand All @@ -38,7 +37,6 @@
public final class DisjointTest {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HDouss Could you please add one more test where the nodes in the graph are not connected at all?


@Test
@Disabled
@SuppressWarnings("unchecked")
public void calculatesDisjointSets() throws Exception {
final Node one = new Node.Simple("one");
Expand Down Expand Up @@ -76,4 +74,36 @@ public void calculatesDisjointSets() throws Exception {
).affirm();
}

@Test
@SuppressWarnings("unchecked")
public void calculatesDisjointSetsForUnconnected() throws Exception {
final Node one = new Node.Simple("1");
final Node two = new Node.Simple("2");
final Node three = new Node.Simple("3");
final Node four = new Node.Simple("4");
final Graph graph = new FakeGraph(
new ListOf<>(one, two, three, four)
);
new Assertion<>(
"Must build disjoint sets for unconnected graph",
new Disjoint(graph).value(),
new AllOf<Iterable<Set<Node>>>(
new ListOf<>(
new HasValuesMatching<>(
set -> set.contains(one) && set.size() == 1
),
new HasValuesMatching<>(
set -> set.contains(two) && set.size() == 1
),
new HasValuesMatching<>(
set -> set.contains(three) && set.size() == 1
),
new HasValuesMatching<>(
set -> set.contains(four) && set.size() == 1
)
)
)
).affirm();
}

}
11 changes: 3 additions & 8 deletions src/test/java/org/jpeek/metrics/Lcom4Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,9 @@ public final class Lcom4Test {
* - methodFive does not use 'num' (this is an orphan method, ignored)
* Therefore the number of disjoint sets (LCOM4) should be 1
* since all the methods use the same num field.
* @todo #310:30min LCOM4: Graph algorithm to determine disjoint sets.
* We started to implement disjoint calculus by putting a disabled test.
* Continue to implement the actual calculus of these sets and remove
* the @Disabled annotation on DisjointTest.
* so we can't trace methodFour that uses 'num' indirectly
* Calculating Disjoint sets (also known as Connected Components),
* requires a graph traversal algorithm like depth-first search.
* This one will be tricky to implement in XSLT.
* @todo #415:30min LCOM4: Graph algorithm to determine disjoint sets.
* Disjoint sets calculus is now implemented. We should continue calculating
* LCOM4 metrics (probably you should wait for #413, #403.
* After implementing, remove the @Disabled from the below test too.
*/
@Test
Expand Down