Skip to content

Commit 8764313

Browse files
committed
Add K-Core Decomposition community detection
1 parent c9a9cdc commit 8764313

8 files changed

+135
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Community Detection K-Core Decomposition Estimate
2+
3+
CALL gds.kcore.write.estimate(
4+
$dependencies_projection + '-without-empty', {
5+
writeProperty: $dependencies_projection_write_property
6+
})
7+
YIELD requiredMemory
8+
,nodeCount
9+
,relationshipCount
10+
,bytesMin
11+
,bytesMax
12+
,heapPercentageMin
13+
,heapPercentageMax
14+
,treeView
15+
,mapView
16+
RETURN requiredMemory
17+
,nodeCount
18+
,relationshipCount
19+
,bytesMin
20+
,bytesMax
21+
,heapPercentageMin
22+
,heapPercentageMax
23+
,treeView
24+
,mapView
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Community Detection K-Core Decomposition Statistics
2+
3+
CALL gds.kcore.stats(
4+
$dependencies_projection + '-without-empty', {
5+
})
6+
YIELD degeneracy, preProcessingMillis, computeMillis, postProcessingMillis
7+
RETURN degeneracy, preProcessingMillis, computeMillis, postProcessingMillis
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Community Detection K-Core Decomposition Mutate
2+
3+
CALL gds.kcore.mutate(
4+
$dependencies_projection + '-without-empty', {
5+
mutateProperty: $dependencies_projection_write_property
6+
})
7+
YIELD degeneracy, nodePropertiesWritten, preProcessingMillis, computeMillis, postProcessingMillis, mutateMillis
8+
RETURN degeneracy, nodePropertiesWritten, preProcessingMillis, computeMillis, postProcessingMillis, mutateMillis
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Community Detection K-Core Decomposition Stream
2+
3+
CALL gds.kcore.stream(
4+
$dependencies_projection + '-without-empty', {
5+
})
6+
YIELD nodeId, coreValue
7+
WITH gds.util.asNode(nodeId) AS member
8+
,coreValue
9+
WITH member
10+
,coalesce(member.fqn, member.fileName, member.name) AS memberName
11+
,coreValue
12+
WITH count(DISTINCT member) AS memberCount
13+
,collect(DISTINCT memberName) AS memberNames
14+
,coreValue
15+
RETURN memberCount
16+
,coreValue
17+
,memberNames
18+
ORDER BY memberCount DESC, coreValue ASC
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Community Detection K-Core Decomposition write node property communitykCoreDecompositionValue
2+
3+
CALL gds.kcore.write(
4+
$dependencies_projection + '-without-empty', {
5+
writeProperty: $dependencies_projection_write_property
6+
})
7+
YIELD degeneracy
8+
,preProcessingMillis
9+
,computeMillis
10+
,writeMillis
11+
,postProcessingMillis
12+
,nodePropertiesWritten
13+
RETURN degeneracy
14+
,preProcessingMillis
15+
,computeMillis
16+
,writeMillis
17+
,postProcessingMillis
18+
,nodePropertiesWritten
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Community Detection Label Propagation Label Delete
2+
3+
CALL db.labels() YIELD label
4+
WHERE label STARTS WITH $dependencies_projection_node + $dependencies_projection_write_label
5+
WITH collect(label) AS selectedLabels
6+
MATCH (member)
7+
WHERE $dependencies_projection_node IN labels(member)
8+
AND member[$dependencies_projection_write_property] IS NOT NULL
9+
WITH collect(member) AS members, selectedLabels
10+
CALL apoc.create.removeLabels(members, selectedLabels) YIELD node
11+
RETURN COUNT(node) AS nodesCount;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Write a property from the projection into the Graph. Variables: dependencies_projection, dependencies_projection_write_property
2+
3+
MATCH (member)
4+
WHERE member[$dependencies_projection_write_property] IS NOT NULL
5+
AND $dependencies_projection_node IN LABELS(member)
6+
WITH collect(member) AS members
7+
,count(DISTINCT member) AS memberCount
8+
,$dependencies_projection_node + $dependencies_projection_write_label + toString(member[$dependencies_projection_write_property]) AS labelName
9+
WHERE memberCount > 1
10+
UNWIND members AS member
11+
CALL apoc.create.addLabels(member, [labelName]) YIELD node
12+
RETURN count(node) AS nodesCount

scripts/reports/CommunityCsv.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,40 @@ detectCommunitiesWithWeaklyConnectedComponents() {
165165
execute_cypher "${COMMUNITY_DETECTION_CYPHER_DIR}/Community_Detection_3f_WeaklyConnectedComponents_Label.cypher" "${@}"
166166
}
167167

168+
169+
# Community Detection using the K-Core Decomposition Algorithm
170+
#
171+
# Required Parameters:
172+
# - dependencies_projection=...
173+
# Name prefix for the in-memory projection name for dependencies. Example: "package"
174+
# - dependencies_projection_node=...
175+
# Label of the nodes that will be used for the projection. Example: "Package"
176+
# - dependencies_projection_weight_property=...
177+
# Name of the node property that contains the dependency weight. Example: "weight"
178+
detectCommunitiesWithKCoreDecomposition() {
179+
local COMMUNITY_DETECTION_CYPHER_DIR="${CYPHER_DIR}/Community_Detection"
180+
local PROJECTION_CYPHER_DIR="${CYPHER_DIR}/Dependencies_Projection"
181+
local writePropertyName="dependencies_projection_write_property=communityKCoreDecompositionValue"
182+
local writeLabelName="dependencies_projection_write_label=KCoreDecomposition"
183+
184+
# Statistics
185+
execute_cypher "${COMMUNITY_DETECTION_CYPHER_DIR}/Community_Detection_5a_K_Core_Decomposition_Estimate.cypher" "${@}" "${writePropertyName}"
186+
execute_cypher "${COMMUNITY_DETECTION_CYPHER_DIR}/Community_Detection_5b_K_Core_Decomposition_Statistics.cypher" "${@}"
187+
188+
# Run the algorithm and write the result into the in-memory projection ("mutate")
189+
execute_cypher "${COMMUNITY_DETECTION_CYPHER_DIR}/Community_Detection_5c_K_Core_Decomposition_Mutate.cypher" "${@}" "${writePropertyName}"
190+
191+
# Stream to CSV
192+
local nodeLabel
193+
nodeLabel=$( extractQueryParameter "dependencies_projection_node" "${@}")
194+
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_8_Stream_Mutated.cypher" "${@}" "${writePropertyName}" > "${FULL_REPORT_DIRECTORY}/${nodeLabel}_Communities_K_Core_Decomposition.csv"
195+
196+
# Update Graph (node properties and labels) using the already mutated property projection
197+
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_9_Write_Mutated.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
198+
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_10_Delete_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
199+
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_11_Add_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
200+
}
201+
168202
# ---------------------------------------------------------------
169203

170204
# Artifact Query Parameters
@@ -180,6 +214,7 @@ time detectCommunitiesWithLeiden "${ARTIFACT_PROJECTION}" "${ARTIFACT_NODE}" "${
180214
time detectCommunitiesWithLouvain "${ARTIFACT_PROJECTION}" "${ARTIFACT_NODE}" "${ARTIFACT_WEIGHT}"
181215
time detectCommunitiesWithWeaklyConnectedComponents "${ARTIFACT_PROJECTION}" "${ARTIFACT_NODE}" "${ARTIFACT_WEIGHT}"
182216
time detectCommunitiesWithLabelPropagation "${ARTIFACT_PROJECTION}" "${ARTIFACT_NODE}" "${ARTIFACT_WEIGHT}" "${ARTIFACT_GAMMA}"
217+
time detectCommunitiesWithKCoreDecomposition "${ARTIFACT_PROJECTION}" "${ARTIFACT_NODE}" "${ARTIFACT_WEIGHT}" "${ARTIFACT_GAMMA}"
183218

184219
# ---------------------------------------------------------------
185220

@@ -196,6 +231,7 @@ time detectCommunitiesWithLeiden "${PACKAGE_PROJECTION}" "${PACKAGE_NODE}" "${PA
196231
time detectCommunitiesWithLouvain "${PACKAGE_PROJECTION}" "${PACKAGE_NODE}" "${PACKAGE_WEIGHT}"
197232
time detectCommunitiesWithWeaklyConnectedComponents "${PACKAGE_PROJECTION}" "${PACKAGE_NODE}" "${PACKAGE_WEIGHT}"
198233
time detectCommunitiesWithLabelPropagation "${PACKAGE_PROJECTION}" "${PACKAGE_NODE}" "${PACKAGE_WEIGHT}" "${PACKAGE_GAMMA}"
234+
time detectCommunitiesWithKCoreDecomposition "${PACKAGE_PROJECTION}" "${PACKAGE_NODE}" "${PACKAGE_WEIGHT}" "${PACKAGE_GAMMA}"
199235

200236
# Package Community Detection - Special CSV Queries after update
201237
execute_cypher "${CYPHER_DIR}/Community_Detection/Compare_Community_Detection_Results.cypher" > "${FULL_REPORT_DIRECTORY}/Compare_Community_Detection_Results.csv"
@@ -216,6 +252,7 @@ time detectCommunitiesWithLeiden "${TYPE_PROJECTION}" "${TYPE_NODE}" "${TYPE_WEI
216252
time detectCommunitiesWithLouvain "${TYPE_PROJECTION}" "${TYPE_NODE}" "${TYPE_WEIGHT}"
217253
time detectCommunitiesWithWeaklyConnectedComponents "${TYPE_PROJECTION}" "${TYPE_NODE}" "${TYPE_WEIGHT}"
218254
time detectCommunitiesWithLabelPropagation "${TYPE_PROJECTION}" "${TYPE_NODE}" "${TYPE_WEIGHT}" "${TYPE_GAMMA}"
255+
time detectCommunitiesWithKCoreDecomposition "${TYPE_PROJECTION}" "${TYPE_NODE}" "${TYPE_WEIGHT}" "${TYPE_GAMMA}"
219256

220257
# Type Community Detection - Special CSV Queries after update
221258
execute_cypher "${CYPHER_DIR}/Community_Detection/Which_type_community_spans_several_artifacts_and_how_are_the_types_distributed.cypher" > "${FULL_REPORT_DIRECTORY}/Type_Communities_Leiden_That_Span_Multiple_Artifacts.csv"

0 commit comments

Comments
 (0)