|
| 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.hdbscan; |
| 21 | + |
| 22 | +import org.assertj.core.api.SoftAssertions; |
| 23 | +import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension; |
| 24 | +import org.assertj.core.data.Offset; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 27 | +import org.neo4j.gds.collections.ha.HugeObjectArray; |
| 28 | + |
| 29 | +@ExtendWith(SoftAssertionsExtension.class) |
| 30 | +class ClusterHierarchyTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void shouldWorkWithLineGraph(SoftAssertions assertions) { |
| 34 | + var edges = HugeObjectArray.of( |
| 35 | + new Edge(1, 2, 3.), |
| 36 | + new Edge(0, 1, 5.) |
| 37 | + ); |
| 38 | + |
| 39 | + var clusterHierarchy = ClusterHierarchy.create(3, edges); |
| 40 | + |
| 41 | + // 1. `1` and `2` are joined and create new id = 3 --> first set |
| 42 | + // 2. `0` and `3` are joined and create new id = 4 --> second set |
| 43 | + |
| 44 | + assertions.assertThat(clusterHierarchy.root()).isEqualTo(4L); |
| 45 | + |
| 46 | + assertions.assertThat(clusterHierarchy.left(4)).isEqualTo(0); |
| 47 | + assertions.assertThat(clusterHierarchy.right(4)).isEqualTo(3); |
| 48 | + assertions.assertThat(clusterHierarchy.lambda(4)).isEqualTo(5.); |
| 49 | + assertions.assertThat(clusterHierarchy.size(4)).isEqualTo(3); |
| 50 | + |
| 51 | + assertions.assertThat(clusterHierarchy.left(3)).isEqualTo(1); |
| 52 | + assertions.assertThat(clusterHierarchy.right(3)).isEqualTo(2); |
| 53 | + assertions.assertThat(clusterHierarchy.lambda(3)).isEqualTo(3.); |
| 54 | + assertions.assertThat(clusterHierarchy.size(3)).isEqualTo(2); |
| 55 | + |
| 56 | + assertions.assertThat(clusterHierarchy.size(0)).isEqualTo(1); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void shouldWorkOnMoreComplexTree(SoftAssertions assertions) { |
| 61 | + var edges = HugeObjectArray.of( |
| 62 | + new Edge(2, 0, 0.24862277), |
| 63 | + new Edge(4, 0, 0.24862277), |
| 64 | + new Edge(3, 8, 0.28702033), |
| 65 | + new Edge(10, 8, 0.28702033), |
| 66 | + new Edge(5, 8, 0.31202177), |
| 67 | + new Edge(6, 2, 0.412653), |
| 68 | + new Edge(1, 3, 0.51812741), |
| 69 | + new Edge(9, 2, 0.55225731), |
| 70 | + new Edge(7, 6, 0.65362267), |
| 71 | + new Edge(5, 7, 1.42823558) |
| 72 | + ); |
| 73 | + |
| 74 | + var clusterHierarchy = ClusterHierarchy.create(edges.size() + 1, edges); |
| 75 | + |
| 76 | + |
| 77 | + assertions.assertThat(clusterHierarchy.left(11)).isEqualTo(2); |
| 78 | + assertions.assertThat(clusterHierarchy.right(11)).isEqualTo(0); |
| 79 | + assertions.assertThat(clusterHierarchy.lambda(11)).isCloseTo(0.24862277, Offset.offset(1e-9)); |
| 80 | + assertions.assertThat(clusterHierarchy.size(11)).isEqualTo(2); |
| 81 | + |
| 82 | + assertions.assertThat(clusterHierarchy.left(12)).isEqualTo(4); |
| 83 | + assertions.assertThat(clusterHierarchy.right(12)).isEqualTo(11); |
| 84 | + assertions.assertThat(clusterHierarchy.lambda(12)).isCloseTo(0.24862277, Offset.offset(1e-9)); |
| 85 | + assertions.assertThat(clusterHierarchy.size(12)).isEqualTo(3); |
| 86 | + |
| 87 | + assertions.assertThat(clusterHierarchy.left(13)).isEqualTo(3); |
| 88 | + assertions.assertThat(clusterHierarchy.right(13)).isEqualTo(8); |
| 89 | + assertions.assertThat(clusterHierarchy.lambda(13)).isCloseTo(0.28702033, Offset.offset(1e-9)); |
| 90 | + assertions.assertThat(clusterHierarchy.size(13)).isEqualTo(2); |
| 91 | + |
| 92 | + assertions.assertThat(clusterHierarchy.left(14)).isEqualTo(10); |
| 93 | + assertions.assertThat(clusterHierarchy.right(14)).isEqualTo(13); |
| 94 | + assertions.assertThat(clusterHierarchy.lambda(14)).isCloseTo(0.28702033, Offset.offset(1e-9)); |
| 95 | + assertions.assertThat(clusterHierarchy.size(14)).isEqualTo(3); |
| 96 | + |
| 97 | + assertions.assertThat(clusterHierarchy.left(15)).isEqualTo(5); |
| 98 | + assertions.assertThat(clusterHierarchy.right(15)).isEqualTo(14); |
| 99 | + assertions.assertThat(clusterHierarchy.lambda(15)).isCloseTo(0.31202177, Offset.offset(1e-9)); |
| 100 | + assertions.assertThat(clusterHierarchy.size(15)).isEqualTo(4); |
| 101 | + |
| 102 | + assertions.assertThat(clusterHierarchy.left(16)).isEqualTo(6); |
| 103 | + assertions.assertThat(clusterHierarchy.right(16)).isEqualTo(12); |
| 104 | + assertions.assertThat(clusterHierarchy.lambda(16)).isCloseTo(0.412653, Offset.offset(1e-9)); |
| 105 | + assertions.assertThat(clusterHierarchy.size(16)).isEqualTo(4); |
| 106 | + |
| 107 | + assertions.assertThat(clusterHierarchy.left(17)).isEqualTo(1); |
| 108 | + assertions.assertThat(clusterHierarchy.right(17)).isEqualTo(15); |
| 109 | + assertions.assertThat(clusterHierarchy.lambda(17)).isCloseTo(0.51812741, Offset.offset(1e-9)); |
| 110 | + assertions.assertThat(clusterHierarchy.size(17)).isEqualTo(5); |
| 111 | + |
| 112 | + assertions.assertThat(clusterHierarchy.left(18)).isEqualTo(9); |
| 113 | + assertions.assertThat(clusterHierarchy.right(18)).isEqualTo(16); |
| 114 | + assertions.assertThat(clusterHierarchy.lambda(18)).isCloseTo(0.55225731, Offset.offset(1e-9)); |
| 115 | + assertions.assertThat(clusterHierarchy.size(18)).isEqualTo(5); |
| 116 | + |
| 117 | + assertions.assertThat(clusterHierarchy.left(19)).isEqualTo(7); |
| 118 | + assertions.assertThat(clusterHierarchy.right(19)).isEqualTo(18); |
| 119 | + assertions.assertThat(clusterHierarchy.lambda(19)).isCloseTo(0.65362267, Offset.offset(1e-9)); |
| 120 | + assertions.assertThat(clusterHierarchy.size(19)).isEqualTo(6); |
| 121 | + |
| 122 | + assertions.assertThat(clusterHierarchy.left(20)).isEqualTo(17); |
| 123 | + assertions.assertThat(clusterHierarchy.right(20)).isEqualTo(19); |
| 124 | + assertions.assertThat(clusterHierarchy.lambda(20)).isCloseTo(1.42823558, Offset.offset(1e-9)); |
| 125 | + assertions.assertThat(clusterHierarchy.size(20)).isEqualTo(11); |
| 126 | + } |
| 127 | +} |
0 commit comments