|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Neo4j is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | +package org.neo4j.gds.leiden; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.RepeatedTest; |
| 23 | +import org.neo4j.gds.Orientation; |
| 24 | +import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker; |
| 25 | +import org.neo4j.gds.extension.GdlExtension; |
| 26 | +import org.neo4j.gds.extension.GdlGraph; |
| 27 | +import org.neo4j.gds.extension.IdFunction; |
| 28 | +import org.neo4j.gds.extension.Inject; |
| 29 | +import org.neo4j.gds.extension.TestGraph; |
| 30 | + |
| 31 | +import java.util.stream.Collectors; |
| 32 | +import java.util.stream.Stream; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +@GdlExtension |
| 37 | +class LeidenWeightedTest { |
| 38 | + |
| 39 | + @GdlGraph(orientation = Orientation.UNDIRECTED) |
| 40 | + private static final String DB_CYPHER = |
| 41 | + "CREATE" + |
| 42 | + " (nAlice:User)," + |
| 43 | + " (nBridget:User)," + |
| 44 | + " (nCharles:User)," + |
| 45 | + " (nDoug:User)," + |
| 46 | + " (nMark:User)," + |
| 47 | + " (nMichael:User)," + |
| 48 | + |
| 49 | + " (nAlice)-[:LINK {weight: 1}]->(nBridget)," + |
| 50 | + " (nAlice)-[:LINK {weight: 1}]->(nCharles)," + |
| 51 | + " (nCharles)-[:LINK {weight: 1}]->(nBridget)," + |
| 52 | + |
| 53 | + " (nAlice)-[:LINK {weight: 5}]->(nDoug)," + |
| 54 | + |
| 55 | + " (nMark)-[:LINK {weight: 1}]->(nDoug)," + |
| 56 | + " (nMark)-[:LINK {weight: 1}]->(nMichael)," + |
| 57 | + " (nMichael)-[:LINK {weight: 1}]->(nMark)"; |
| 58 | + |
| 59 | + @Inject |
| 60 | + private TestGraph graph; |
| 61 | + |
| 62 | + @Inject |
| 63 | + private IdFunction idFunction; |
| 64 | + |
| 65 | + @RepeatedTest(10) |
| 66 | + void weightedLeiden() { |
| 67 | + var maxLevels = 10; |
| 68 | + Leiden leiden = new Leiden( |
| 69 | + graph, |
| 70 | + maxLevels, |
| 71 | + 1.0 / graph.relationshipCount(), |
| 72 | + 0.01, |
| 73 | + 19L, |
| 74 | + 4, |
| 75 | + ProgressTracker.NULL_TRACKER |
| 76 | + ); |
| 77 | + |
| 78 | + var leidenResult = leiden.compute(); |
| 79 | + |
| 80 | + assertThat(leidenResult.ranLevels()).isLessThanOrEqualTo(maxLevels); |
| 81 | + assertThat(leidenResult.didConverge()).isTrue(); |
| 82 | + |
| 83 | + var communities = leidenResult.communities(); |
| 84 | + |
| 85 | + var communitiesMap = Stream.of("nAlice", "nBridget", "nCharles", "nDoug", "nMark", "nMichael") |
| 86 | + .collect(Collectors.groupingBy(v -> communities.get(idFunction.of(v)))); |
| 87 | + |
| 88 | + assertThat(communitiesMap.values()) |
| 89 | + .satisfiesExactlyInAnyOrder( |
| 90 | + community -> assertThat(community).containsExactlyInAnyOrder("nAlice", "nDoug"), |
| 91 | + community -> assertThat(community).containsExactlyInAnyOrder("nBridget", "nCharles"), |
| 92 | + community -> assertThat(community).containsExactlyInAnyOrder("nMark", "nMichael") |
| 93 | + ); |
| 94 | + |
| 95 | + } |
| 96 | +} |
0 commit comments