Skip to content

Commit 7f618dc

Browse files
Fix K-spanning tree docTests
Co-authored-by: Jonatan Jäderberg <jonatan.jaderberg@gmail.com>
1 parent 597be29 commit 7f618dc

File tree

3 files changed

+99
-32
lines changed

3 files changed

+99
-32
lines changed

alpha/alpha-proc/src/test/java/org/neo4j/gds/spanningtree/KSpanningTreeWriteProcTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.neo4j.gds.extension.Neo4jGraph;
3131

3232
import java.util.HashMap;
33+
import java.util.HashSet;
3334

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

@@ -83,16 +84,20 @@ void testMax() {
8384
assertThat(row.getNumber("computeMillis").longValue() >= 0).isTrue();
8485
});
8586

86-
final HashMap<String, Long> communities = new HashMap<>();
87+
final HashMap<String, Integer> communities = new HashMap<>();
88+
final HashSet<Integer> distinctCommunities = new HashSet<>();
8789

8890
runQueryWithRowConsumer("MATCH (n) WHERE n.partition IS NOT NULL RETURN n.name as name, n.partition as p", row -> {
8991
final String name = row.getString("name");
90-
final long p = row.getNumber("p").longValue();
92+
final int p = row.getNumber("p").intValue();
9193
communities.put(name, p);
94+
distinctCommunities.add(p);
95+
9296
});
9397

9498
assertThat(communities).matches(c -> c.get("a").equals(c.get("b")) ^ c.get("c").equals(c.get("d")));
9599
assertThat(communities.get("a")).isNotEqualTo(communities.get("c"));
100+
assertThat(distinctCommunities.size()).isEqualTo(3);
96101
}
97102

98103
@Test
@@ -113,14 +118,15 @@ void testMin() {
113118
});
114119

115120
final HashMap<String, Integer> communities = new HashMap<>();
116-
121+
final HashSet<Integer> distinctCommunities = new HashSet<>();
117122
runQueryWithRowConsumer("MATCH (n) WHERE n.partition IS NOT NULL RETURN n.name as name, n.partition as p", row -> {
118123
final String name = row.getString("name");
119124
final int p = row.getNumber("p").intValue();
120125
communities.put(name, p);
126+
distinctCommunities.add(p);
121127
});
122128

123129
assertThat(communities).matches(c -> c.get("a").equals(c.get("d")) ^ c.get("b").equals(c.get("c")));
124-
assertThat(communities.get("a")).isNotEqualTo(communities.get("b"));
130+
assertThat(distinctCommunities.size()).isEqualTo(3);
125131
}
126132
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.doc;
21+
22+
import org.neo4j.gds.catalog.GraphProjectProc;
23+
import org.neo4j.gds.functions.AsNodeFunc;
24+
import org.neo4j.gds.spanningtree.KSpanningWriteTreeProc;
25+
26+
import java.util.List;
27+
28+
class KSpanningTreeDocTest extends SingleFileDocTestBase {
29+
30+
@Override
31+
protected List<Class<?>> functions() {
32+
return List.of(AsNodeFunc.class);
33+
}
34+
35+
@Override
36+
protected List<Class<?>> procedures() {
37+
return List.of(
38+
KSpanningWriteTreeProc.class,
39+
GraphProjectProc.class
40+
);
41+
}
42+
43+
@Override
44+
protected String adocFile() {
45+
return "pages/alpha-algorithms/k-minimum-weight-spanning-tree.adoc";
46+
}
47+
48+
}

doc/modules/ROOT/pages/alpha-algorithms/k-minimum-weight-spanning-tree.adoc

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ YIELD effectiveNodeCount: Integer,
6262
image::mst.png[]
6363

6464
.The following will create the sample graph depicted in the figure:
65-
[source, cypher, role=noplay]
65+
[source, cypher, role=noplay setup-query]
6666
----
6767
CREATE (a:Place {id: 'A'}),
6868
(b:Place {id: 'B'}),
@@ -81,7 +81,7 @@ CREATE (a:Place {id: 'A'}),
8181
----
8282

8383
.The following will project and store a named graph:
84-
[source, cypher, role=noplay]
84+
[source, cypher, role=noplay graph-project-query]
8585
----
8686
CALL gds.graph.project(
8787
'graph',
@@ -98,46 +98,57 @@ CALL gds.graph.project(
9898
[[algorithms-minimum-weight-spanning-tree-k]]
9999
== K-Spanning tree examples
100100

101+
=== Minimum K-Spanning Tree example
102+
101103
In our sample graph we have 5 nodes.
102-
When we ran MST above, we got a 5-minimum spanning tree returned, that covered all five nodes.
103104
By setting the `k=3`, we define that we want to get returned a 3-minimum spanning tree that covers 3 nodes and has 2 relationships.
104105

105106
.The following will run the k-minimum spanning tree algorithm and write back results:
107+
[role=query-example, no-result=true, group=write-example]
108+
--
106109
[source, cypher, role=noplay]
107110
----
108111
MATCH (n:Place{id: 'D'})
109112
CALL gds.alpha.kSpanningTree.write('graph', {
110113
k: 3,
111114
sourceNode: id(n),
112115
relationshipWeightProperty: 'cost',
113-
writeProperty:'kminst'
116+
writeProperty:'kmin'
114117
})
115118
YIELD preProcessingMillis, computeMillis, writeMillis, effectiveNodeCount
116119
RETURN preProcessingMillis,computeMillis,writeMillis, effectiveNodeCount;
117120
----
121+
--
118122

119-
.Find nodes that belong to our k-spanning tree result:
123+
[role=query-example, group=write-example]
124+
--
125+
.The following will find the nodes that belong to our k-spanning tree result:
120126
[source, cypher, role=noplay]
121127
----
122-
MATCH (n:Place)
123-
WITH n.id AS Place, n.kminst AS Partition, count(*) AS count
124-
WHERE count = 3
125-
RETURN Place, Partition
128+
MATCH (n)
129+
WITH n.kmin AS p, count(n) AS c
130+
WHERE c = 3
131+
MATCH (n)
132+
WHERE n.kmin = p
133+
RETURN n.id As Place, p as Partition
134+
126135
----
127136

128137
.Results
129138
[opts="header",cols="1,1"]
130139
|===
131140
| Place | Partition
132-
| A | 1
133-
| B | 1
134-
| C | 1
135-
| D | 3
136-
| E | 4
141+
| "A" | 1
142+
| "B" | 1
143+
| "C" | 1
137144
|===
138-
145+
--
139146
Nodes A, B, and C are the result 3-minimum spanning tree of our graph.
140147

148+
=== Maximum K-Spanning Tree example
149+
150+
[role=query-example,no-result=true, group=max-example]
151+
--
141152
.The following will run the k-maximum spanning tree algorithm and write back results:
142153
[source, cypher, role=noplay]
143154
----
@@ -146,31 +157,33 @@ CALL gds.alpha.kSpanningTree.write('graph', {
146157
k: 3,
147158
sourceNode: id(n),
148159
relationshipWeightProperty: 'cost',
149-
writeProperty:'kmaxst',
150-
objective: 'maximum',
160+
writeProperty:'kmax',
161+
objective: 'maximum'
151162
})
152163
YIELD preProcessingMillis, computeMillis, writeMillis, effectiveNodeCount
153164
RETURN preProcessingMillis,computeMillis,writeMillis, effectiveNodeCount;
154165
----
166+
--
155167

168+
[role=query-example, group=max-example]
169+
--
156170
.Find nodes that belong to our k-spanning tree result:
157171
[source, cypher, role=noplay]
158172
----
159-
MATCH (n:Place)
160-
WITH n.id AS Place, n.kmaxst AS Partition, count(*) AS count
161-
WHERE count = 3
162-
RETURN Place, Partition
173+
MATCH (n)
174+
WITH n.kmax AS p, count(n) AS c
175+
WHERE c = 3
176+
MATCH (n)
177+
WHERE n.kmax = p
178+
RETURN n.id As Place, p as Partition
163179
----
164-
165180
.Results
166181
[opts="header",cols="1,1"]
167182
|===
168183
| Place | Partition
169-
| A | 0
170-
| B | 1
171-
| C | 3
172-
| D | 3
173-
| E | 3
184+
| "C" | 3
185+
| "D" | 3
186+
| "E" | 3
174187
|===
175-
188+
--
176189
Nodes C, D, and E are the result 3-maximum spanning tree of our graph.

0 commit comments

Comments
 (0)