K-Core Decomposition

Glossary

Directed

Directed trait. The algorithm is well-defined on a directed graph.

Directed

Directed trait. The algorithm ignores the direction of the graph.

Directed

Directed trait. The algorithm does not run on a directed graph.

Undirected

Undirected trait. The algorithm is well-defined on an undirected graph.

Undirected

Undirected trait. The algorithm ignores the undirectedness of the graph.

Heterogeneous nodes

Heterogeneous nodes fully supported. The algorithm has the ability to distinguish between nodes of different types.

Heterogeneous nodes

Heterogeneous nodes allowed. The algorithm treats all selected nodes similarly regardless of their label.

Heterogeneous relationships

Heterogeneous relationships fully supported. The algorithm has the ability to distinguish between relationships of different types.

Heterogeneous relationships

Heterogeneous relationships allowed. The algorithm treats all selected relationships similarly regardless of their type.

Weighted relationships

Weighted trait. The algorithm supports a relationship property to be used as weight, specified via the relationshipWeightProperty configuration parameter.

Weighted relationships

Weighted trait. The algorithm treats each relationship as equally important, discarding the value of any relationship weight.

Introduction

The K-core decomposition constitutes a process of separates the nodes in a graph into groups based on the degree sequence and topology of the graph.

The term i-core refers to a maximal subgraph of the original graph such that each node in this subgraph has degree at least i. The maximality ensures that it is not possible to find another subgraph with more nodes where this degree property holds.

The nodes in the subgraph denoted by i-core also belong to the subgraph denoted by j-core for any j<i. The converse however is not true. Each node u is associated with a core value which denotes the largest value i such that u belongs to the i-core. The largest core value is called the degeneracy of the graph.

Standard algorithms for K-Core Decomposition iteratively remove the node of lowest degree until the graph becomes empty. When a node is removed from the graph, all of its relationships are removed, and the degree of its neighbors is reduced by one. With this approach, the different core groups are discovered one-by-one.

The Neo4j GDS Library offers a parallel implementation based on two recent approaches for the problem:

K-core Decomposition can have applications in several fields ranging from social network analysis to bioinformatics. Some of the possible use-cases are presented here.

Syntax

This section covers the syntax used to execute the K-Core Decomposition algorithm in each of its execution modes. We are describing the named graph variant of the syntax. To learn more about general syntax variants, see Syntax overview.

K-Core Decomposition syntax per mode
Run .K-Core Decomposition in stream mode on a named graph.
CALL gds.kcore.stream(
  graphName: String,
  configuration: Map
) YIELD
  nodeId: Integer,
  coreValue: Float
Table 1. Parameters
Name Type Default Optional Description

graphName

String

n/a

no

The name of a graph stored in the catalog.

configuration

Map

{}

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 2. Configuration
Name Type Default Optional Description

nodeLabels

List of String

['*']

yes

Filter the named graph using the given node labels. Nodes with any of the given labels will be included.

relationshipTypes

List of String

['*']

yes

Filter the named graph using the given relationship types. Relationships with any of the given types will be included.

concurrency

Integer

4

yes

The number of concurrent threads used for running the algorithm.

jobId

String

Generated internally

yes

An ID that can be provided to more easily track the algorithm’s progress.

logProgress

Boolean

true

yes

If disabled the progress percentage will not be logged.

Table 3. Results
Name Type Description

nodeId

Integer

Node ID.

coreValue

Float

Core value.

Run K-Core Decomposition in stats mode on a named graph.
CALL gds.kcore.stats(
  graphName: String,
  configuration: Map
) YIELD
  degeneracy: Integer,
  preProcessingMillis: Integer,
  computeMillis: Integer,
  postProcessingMillis: Integer,
  configuration: Map
Table 4. Parameters
Name Type Default Optional Description

graphName

String

n/a

no

The name of a graph stored in the catalog.

configuration

Map

{}

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 5. Configuration
Name Type Default Optional Description

nodeLabels

List of String

['*']

yes

Filter the named graph using the given node labels. Nodes with any of the given labels will be included.

relationshipTypes

List of String

['*']

yes

Filter the named graph using the given relationship types. Relationships with any of the given types will be included.

concurrency

Integer

4

yes

The number of concurrent threads used for running the algorithm.

jobId

String

Generated internally

yes

An ID that can be provided to more easily track the algorithm’s progress.

logProgress

Boolean

true

yes

If disabled the progress percentage will not be logged.

Table 6. Results
Name Type Description

degeneracy

Integer

the maximum core value in the graph.

preProcessingMillis

Integer

Milliseconds for preprocessing the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing the statistics.

configuration

Map

Configuration used for running the algorithm.

Run K-Core Decomposition in mutate mode on a named graph.
CALL gds.kcore.mutate(
  graphName: String,
  configuration: Map
) YIELD
  degeneracy: Integer,
  preProcessingMillis: Integer,
  computeMillis: Integer,
  postProcessingMillis: Integer,
  mutateMillis: Integer,
  nodePropertiesWritten: Integer,
  configuration: Map
Table 7. Parameters
Name Type Default Optional Description

graphName

String

n/a

no

The name of a graph stored in the catalog.

configuration

Map

{}

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 8. Configuration
Name Type Default Optional Description

mutateProperty

String

n/a

no

The node property in the GDS graph to which the core value is written.

nodeLabels

List of String

['*']

yes

Filter the named graph using the given node labels.

relationshipTypes

List of String

['*']

yes

Filter the named graph using the given relationship types.

concurrency

Integer

4

yes

The number of concurrent threads used for running the algorithm.

jobId

String

Generated internally

yes

An ID that can be provided to more easily track the algorithm’s progress.

Table 9. Results
Name Type Description

degeneracy

Integer

the maximum core value in the graph.

preProcessingMillis

Integer

Milliseconds for preprocessing the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing the statistics.

mutateMillis

Integer

Milliseconds for adding properties to the projected graph.

nodePropertiesWritten

Integer

Number of properties added to the projected graph.

configuration

Map

Configuration used for running the algorithm.

Run K-Core decomposition in write mode on a named graph.
CALL gds.kcore.write(
  graphName: String,
  configuration: Map
) YIELD
  degeneracy: Integer,
  preProcessingMillis: Integer,
  computeMillis: Integer,
  postProcessingMillis: Integer,
  writeMillis: Integer,
  nodePropertiesWritten: Integer,
  configuration: Map
Table 10. Parameters
Name Type Default Optional Description

graphName

String

n/a

no

The name of a graph stored in the catalog.

configuration

Map

{}

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 11. Configuration
Name Type Default Optional Description

nodeLabels

List of String

['*']

yes

Filter the named graph using the given node labels. Nodes with any of the given labels will be included.

relationshipTypes

List of String

['*']

yes

Filter the named graph using the given relationship types. Relationships with any of the given types will be included.

concurrency

Integer

4

yes

The number of concurrent threads used for running the algorithm.

jobId

String

Generated internally

yes

An ID that can be provided to more easily track the algorithm’s progress.

logProgress

Boolean

true

yes

If disabled the progress percentage will not be logged.

writeConcurrency

Integer

value of 'concurrency'

yes

The number of concurrent threads used for writing the result to Neo4j.

writeProperty

String

n/a

no

The node property in the Neo4j database to which the core value is written.

Table 12. Results
Name Type Description

degeneracy

Integer

the maximum core value in the graph.

preProcessingMillis

Integer

Milliseconds for preprocessing the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing the statistics.

writeMillis

Integer

Milliseconds for writing result data back.

nodePropertiesWritten

Integer

Number of properties written to Neo4j.

configuration

Map

The configuration used for running the algorithm.

Examples

All the examples below should be run in an empty database.

The examples use Cypher projections as the norm. Native projections will be deprecated in a future release.

In this section we will show examples of running the K-Core Decomposition algorithm on a concrete graph. The intention is to illustrate what the results look like and to provide a guide in how to make use of the algorithm in a real setting. We will do this on a small social network graph of a handful nodes connected in a particular pattern. The example graph looks like this:

Visualization of the example graph
The following Cypher statement will create the example graph in the Neo4j database:
CREATE
  (alice:User {name: 'Alice'}),
  (bridget:User {name: 'Bridget'}),
  (charles:User {name: 'Charles'}),
  (doug:User {name: 'Doug'}),
  (eli:User {name: 'Eli'}),
  (filip:User {name: 'Filip'}),
  (greg:User {name: 'Greg'}),
  (harry:User {name: 'Harry'}),
  (ian:User {name: 'Ian'}),
  (james:User {name: 'James'}),

  (alice)-[:FRIEND]->(bridget),
  (bridget)-[:FRIEND]->(charles),
  (charles)-[:FRIEND]->(doug),
  (charles)-[:FRIEND]->(harry),
  (doug)-[:FRIEND]->(eli),
  (doug)-[:FRIEND]->(filip),
  (doug)-[:FRIEND]->(greg),
  (eli)-[:FRIEND]->(filip),
  (eli)-[:FRIEND]->(greg),
  (filip)-[:FRIEND]->(greg),
  (greg)-[:FRIEND]->(harry),
  (ian)-[:FRIEND]->(james)

With the graph in Neo4j we can now project it into the graph catalog to prepare it for algorithm execution. We do this using a Cypher projection targeting the User nodes and the FRIEND relationships.

The following statement will project a graph using athe undirected projection and store it in the graph catalog under the name 'graph'.
MATCH (source:User)-[r:FRIEND]->(target:User)
RETURN gds.graph.project(
  'graph',
  source,
  target,
  {},
  { undirectedRelationshipTypes: ['*'] }
)

The graph is projected in the UNDIRECTED orientation as the friendship relationship is associative.

Memory Estimation

First off, we will estimate the cost of running the algorithm using the estimate procedure. This can be done with any execution mode. We will use the write mode in this example. Estimating the algorithm is useful to understand the memory impact that running the algorithm on your graph will have. When you later actually run the algorithm in one of the execution modes the system will perform an estimation. If the estimation shows that there is a very high probability of the execution going over its memory limitations, the execution is prohibited. To read more about this, see Automatic estimation and execution blocking.

For more details on estimate in general, see Memory Estimation.

The following will estimate the memory requirements for running the algorithm:
CALL gds.kcore.write.estimate('graph', { writeProperty: 'coreValue' })
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
Table 13. Results
nodeCount relationshipCount bytesMin bytesMax requiredMemory

10

24

1456

1456

"1456 Bytes"

Stream

In the stream execution mode, the algorithm returns the core value for each node. This allows us to inspect the results directly or post-process them in Cypher without any side effects. For example, we can order the results to find the nodes with the highest core values.

For more details on the stream mode in general, see Stream.

The following will run the algorithm in stream mode:
CALL gds.kcore.stream('graph')
YIELD nodeId, coreValue
RETURN gds.util.asNode(nodeId).name AS name, coreValue
ORDER BY coreValue ASC, name DESC
Table 14. Results
name coreValue

"James"

1

"Ian"

1

"Bridget"

1

"Alice"

1

"Harry"

2

"Charles"

2

"Greg"

3

"Filip"

3

"Eli"

3

"Doug"

3

The algorithm has separated the nodes in the graph in three distinct groups. The first group where all nodes have core value qual to 1 includes James, Ian, Bridget, and Alice. The second group includes Harry and Charles. Here, all the nodes have core value equal to 2. The third group includes Greg, Filip, Eli, and Doug, and all the nodes have core value equal to 3.

As it was explained in introduction, nodes with core value i have degree at least i in the subgraph containing only nodes with core value at least i. For example, although Charles has degree 3, he cannot be part of the 3-core subgraph since one of its neighbors is Bridget from the first group of core value 1. Once Bridget is excluded, Charles is left with a degree of 2, which acts as an upper bound on its core value. One of its two remaining neighbors is Doug who belongs to the 3-core.

Note that as the results show, the nodes in different connected components might be part of the same core group (for example Ian and Alice).

Stats

In the stats execution mode, the algorithm returns a single row containing a summary of the algorithm result. This execution mode does not have any side effects. It can be useful for evaluating algorithm performance by inspecting the computeMillis return item. In the examples below we will omit returning the timings. The full signature of the procedure can be found in the syntax section.

For more details on the stats mode in general, see Stats.

The following will run the algorithm in stats mode:
CALL gds.kcore.stats('graph')
YIELD degeneracy
RETURN degeneracy
Table 15. Results
degeneracy

3

As the results from stream example also confirm, the degeneracy, i.e., the largest core value, is equal to three.

Mutate

The mutate execution mode extends the stats mode with an important side effect: updating the named graph with a new node property containing the core value for that node. The name of the new property is specified using the mandatory configuration parameter mutateProperty. The result is a single summary row, similar to stats, but with some additional metrics. The mutate mode is especially useful when multiple algorithms are used in conjunction.

For more details on the mutate mode in general, see Mutate.

The following will run the algorithm in mutate mode:
CALL gds.kcore.mutate('graph', { mutateProperty: 'coreValue' })
YIELD degeneracy, nodePropertiesWritten
RETURN degeneracy , nodePropertiesWritten
Table 16. Results
degeneracy nodePropertiesWritten

3

10

The returned result is the same as in the stats example. Additionally, the in-memory graph now has a node property coreValue which stores the core value of each node. To find out how to inspect the new schema of the in-memory graph, see Listing graphs in the catalog.

Write

The write execution mode extends the stats mode with an important side effect: writing the core value for each node as a property to the Neo4j database. The name of the new property is specified using the mandatory configuration parameter writeProperty. The result is a single summary row, similar to stats, but with some additional metrics. The write mode enables directly persisting the results to the database.

For more details on the write mode in general, see Write.

The following will run the algorithm in write mode:
CALL gds.kcore.write('graph', { writeProperty: 'coreValue' })
YIELD degeneracy, nodePropertiesWritten
RETURN degeneracy , nodePropertiesWritten
Table 17. Results
degeneracy nodePropertiesWritten

3

10

The returned result is the same as in the stats example. Additionally, each of the seven nodes now has a new property coreValue in the Neo4j database, containing the core value for that node.