Skip to content

Commit 1c18d0e

Browse files
2 parents 6797386 + 324cafe commit 1c18d0e

File tree

3 files changed

+133
-25
lines changed

3 files changed

+133
-25
lines changed

algo/src/main/java/org/neo4j/gds/similarity/nodesim/TopKGraph.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,23 @@
1919
*/
2020
package org.neo4j.gds.similarity.nodesim;
2121

22+
import org.neo4j.gds.RelationshipType;
23+
import org.neo4j.gds.api.DefaultValue;
2224
import org.neo4j.gds.api.Graph;
2325
import org.neo4j.gds.api.GraphAdapter;
26+
import org.neo4j.gds.api.PropertyState;
27+
import org.neo4j.gds.api.nodeproperties.ValueType;
2428
import org.neo4j.gds.api.properties.relationships.RelationshipConsumer;
2529
import org.neo4j.gds.api.properties.relationships.RelationshipWithPropertyConsumer;
30+
import org.neo4j.gds.api.schema.Direction;
31+
import org.neo4j.gds.api.schema.GraphSchema;
32+
import org.neo4j.gds.api.schema.ImmutableMutableGraphSchema;
33+
import org.neo4j.gds.api.schema.ImmutableRelationshipPropertySchema;
34+
import org.neo4j.gds.api.schema.MutableRelationshipSchema;
35+
import org.neo4j.gds.api.schema.MutableRelationshipSchemaEntry;
36+
import org.neo4j.gds.core.Aggregation;
37+
38+
import java.util.Map;
2639

2740
public class TopKGraph extends GraphAdapter {
2841

@@ -39,6 +52,45 @@ public int degree(long nodeId) {
3952
return topKList != null ? topKList.size() : 0;
4053
}
4154

55+
@Override
56+
public boolean hasRelationshipProperty() {
57+
return true;
58+
}
59+
60+
@Override
61+
public GraphSchema schema() {
62+
var type = RelationshipType.of("SIMILAR");
63+
var relationshipSchema = new MutableRelationshipSchema(
64+
Map.of(
65+
type,
66+
new MutableRelationshipSchemaEntry(
67+
type,
68+
Direction.DIRECTED,
69+
Map.of(
70+
"similarity",
71+
ImmutableRelationshipPropertySchema.of(
72+
"similarity",
73+
ValueType.DOUBLE,
74+
DefaultValue.DEFAULT,
75+
PropertyState.TRANSIENT,
76+
Aggregation.NONE
77+
)
78+
)
79+
)
80+
)
81+
);
82+
83+
return ImmutableMutableGraphSchema.builder()
84+
.from(graph.schema())
85+
.relationshipSchema(relationshipSchema)
86+
.build();
87+
}
88+
89+
@Override
90+
public boolean isMultiGraph() {
91+
return super.isMultiGraph();
92+
}
93+
4294
@Override
4395
public long relationshipCount() {
4496
return topKMap.similarityPairCount();

algo/src/main/java/org/neo4j/gds/spanningtree/SpanningGraph.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,24 @@
1919
*/
2020
package org.neo4j.gds.spanningtree;
2121

22+
import org.neo4j.gds.RelationshipType;
23+
import org.neo4j.gds.api.DefaultValue;
2224
import org.neo4j.gds.api.Graph;
2325
import org.neo4j.gds.api.GraphAdapter;
26+
import org.neo4j.gds.api.PropertyState;
27+
import org.neo4j.gds.api.nodeproperties.ValueType;
2428
import org.neo4j.gds.api.properties.relationships.RelationshipConsumer;
2529
import org.neo4j.gds.api.properties.relationships.RelationshipWithPropertyConsumer;
30+
import org.neo4j.gds.api.schema.Direction;
31+
import org.neo4j.gds.api.schema.GraphSchema;
32+
import org.neo4j.gds.api.schema.ImmutableMutableGraphSchema;
33+
import org.neo4j.gds.api.schema.ImmutableRelationshipPropertySchema;
34+
import org.neo4j.gds.api.schema.MutableRelationshipSchema;
35+
import org.neo4j.gds.api.schema.MutableRelationshipSchemaEntry;
36+
import org.neo4j.gds.core.Aggregation;
2637

2738
import java.util.Arrays;
39+
import java.util.Map;
2840

2941
public class SpanningGraph extends GraphAdapter {
3042

@@ -44,6 +56,50 @@ public int degree(long nodeId) {
4456
}
4557
}
4658

59+
@Override
60+
public long relationshipCount() {
61+
// not counting root -> root as a rel
62+
return spanningTree.effectiveNodeCount() - 1;
63+
}
64+
65+
@Override
66+
public boolean hasRelationshipProperty() {
67+
return true;
68+
}
69+
70+
@Override
71+
public GraphSchema schema() {
72+
var relationshipSchema = new MutableRelationshipSchema(
73+
Map.of(
74+
RelationshipType.ALL_RELATIONSHIPS,
75+
new MutableRelationshipSchemaEntry(
76+
RelationshipType.ALL_RELATIONSHIPS,
77+
Direction.DIRECTED,
78+
Map.of(
79+
"cost",
80+
ImmutableRelationshipPropertySchema.of(
81+
"cost",
82+
ValueType.DOUBLE,
83+
DefaultValue.DEFAULT,
84+
PropertyState.TRANSIENT,
85+
Aggregation.NONE
86+
)
87+
)
88+
)
89+
)
90+
);
91+
92+
return ImmutableMutableGraphSchema.builder()
93+
.from(graph.schema())
94+
.relationshipSchema(relationshipSchema)
95+
.build();
96+
}
97+
98+
@Override
99+
public boolean isMultiGraph() {
100+
return false;
101+
}
102+
47103
@Override
48104
public void forEachRelationship(long nodeId, RelationshipConsumer consumer) {
49105
forEachRelationship(

algo/src/test/java/org/neo4j/gds/similarity/nodesim/NodeSimilarityTest.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -462,15 +462,15 @@ void shouldComputeNegativeTopKForSupportedDirections(Orientation orientation, in
462462

463463
assertGraphEquals(
464464
orientation == REVERSE
465-
? fromGdl(" (i1:Item)-[:LIKES {w: 0.50000D}]->(i3:Item)" +
466-
", (i2:Item)-[:LIKES {w: 0.50000D}]->(i3)" +
467-
", (i3)-[:LIKES {w: 0.500000D}]->(i1)" +
465+
? fromGdl(" (i1:Item)-[:SIMILAR {w: 0.50000D}]->(i3:Item)" +
466+
", (i2:Item)-[:SIMILAR {w: 0.50000D}]->(i3)" +
467+
", (i3)-[:SIMILAR {w: 0.500000D}]->(i1)" +
468468
", (i4:Item)" +
469469
", (:Person), (:Person), (:Person), (:Person)")
470-
: fromGdl(" (a:Person)-[:LIKES {w: 0.333333D}]->(c:Person)" +
471-
", (b:Person)-[:LIKES {w: 0.00000D}]->(c)" +
472-
", (c)-[:LIKES {w: 0.000000D}]->(b)" +
473-
", (d:Person)-[:LIKES {w: 0.333333D}]->(c)" +
470+
: fromGdl(" (a:Person)-[:SIMILAR {w: 0.333333D}]->(c:Person)" +
471+
", (b:Person)-[:SIMILAR {w: 0.00000D}]->(c)" +
472+
", (c)-[:SIMILAR {w: 0.000000D}]->(b)" +
473+
", (d:Person)-[:SIMILAR {w: 0.333333D}]->(c)" +
474474
", (:Item), (:Item), (:Item), (:Item)"),
475475
similarityGraph
476476
);
@@ -636,26 +636,26 @@ void shouldComputeSimilarityGraphInAllSupportedDirections(Orientation orientatio
636636
orientation == REVERSE
637637
? fromGdl(" (:Person), (:Person), (:Person), (:Person)" +
638638
", (:Item)" +
639-
", (i1:Item)-[:LIKES {property: 1.000000D}]->(i2:Item)" +
640-
", (i1)-[:LIKES {property: 0.500000D}]->(i3:Item)" +
641-
", (i2)-[:LIKES {property: 0.500000D}]->(i3)" +
639+
", (i1:Item)-[:SIMILAR {property: 1.000000D}]->(i2:Item)" +
640+
", (i1)-[:SIMILAR {property: 0.500000D}]->(i3:Item)" +
641+
", (i2)-[:SIMILAR {property: 0.500000D}]->(i3)" +
642642
// Add results in reverse direction because topK
643-
", (i2)-[:LIKES {property: 1.000000D}]->(i1)" +
644-
", (i3)-[:LIKES {property: 0.500000D}]->(i1)" +
645-
", (i3)-[:LIKES {property: 0.500000D}]->(i2)")
646-
: fromGdl(" (p1:Person)-[:LIKES {property: 0.666667D}]->(p2:Person)" +
647-
", (p1)-[:LIKES {property: 0.333333D}]->(p3:Person)" +
648-
", (p1)-[:LIKES {property: 1.000000D}]->(p4:Person)" +
649-
", (p2)-[:LIKES {property: 0.000000D}]->(p3)" +
650-
", (p2)-[:LIKES {property: 0.666667D}]->(p4)" +
651-
", (p3)-[:LIKES {property: 0.333333D}]->(p4)" +
643+
", (i2)-[:SIMILAR {property: 1.000000D}]->(i1)" +
644+
", (i3)-[:SIMILAR {property: 0.500000D}]->(i1)" +
645+
", (i3)-[:SIMILAR {property: 0.500000D}]->(i2)")
646+
: fromGdl(" (p1:Person)-[:SIMILAR {property: 0.666667D}]->(p2:Person)" +
647+
", (p1)-[:SIMILAR {property: 0.333333D}]->(p3:Person)" +
648+
", (p1)-[:SIMILAR {property: 1.000000D}]->(p4:Person)" +
649+
", (p2)-[:SIMILAR {property: 0.000000D}]->(p3)" +
650+
", (p2)-[:SIMILAR {property: 0.666667D}]->(p4)" +
651+
", (p3)-[:SIMILAR {property: 0.333333D}]->(p4)" +
652652
// Add results in reverse direction because topK
653-
" (p2)-[:LIKES {property: 0.666667D}]->(p1)" +
654-
", (p3)-[:LIKES {property: 0.333333D}]->(p1)" +
655-
", (p4)-[:LIKES {property: 1.000000D}]->(p1)" +
656-
", (p3)-[:LIKES {property: 0.000000D}]->(p2)" +
657-
", (p4)-[:LIKES {property: 0.666667D}]->(p2)" +
658-
", (p4)-[:LIKES {property: 0.333333D}]->(p3)" +
653+
" (p2)-[:SIMILAR {property: 0.666667D}]->(p1)" +
654+
", (p3)-[:SIMILAR {property: 0.333333D}]->(p1)" +
655+
", (p4)-[:SIMILAR {property: 1.000000D}]->(p1)" +
656+
", (p3)-[:SIMILAR {property: 0.000000D}]->(p2)" +
657+
", (p4)-[:SIMILAR {property: 0.666667D}]->(p2)" +
658+
", (p4)-[:SIMILAR {property: 0.333333D}]->(p3)" +
659659
", (:Item), (:Item), (:Item), (:Item)"),
660660
resultGraph
661661
);

0 commit comments

Comments
 (0)