|
| 1 | +--- |
| 2 | +title: "Community Detection using Label Propagation (CDLP)" |
| 3 | +description: "Community Detection using Label Propagation (CDLP)" |
| 4 | +parent: "Algorithms" |
| 5 | +--- |
| 6 | + |
| 7 | +# Community Detection using Label Propagation (CDLP) |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +The Community Detection using Label Propagation (CDLP) algorithm identifies communities in networks by propagating labels through the graph structure. |
| 12 | +Each node starts with a unique label, and through iterative propagation, nodes adopt the most frequent label among their neighbors, naturally forming communities where densely connected nodes share the same label. |
| 13 | + |
| 14 | +CDLP serves as a powerful algorithm in scenarios such as: |
| 15 | +- Social network community detection |
| 16 | +- Biological network module identification |
| 17 | +- Web page clustering and topic detection |
| 18 | +- Market segmentation analysis |
| 19 | +- Fraud detection networks |
| 20 | + |
| 21 | +## Algorithm Details |
| 22 | + |
| 23 | +CDLP initializes by assigning each node a unique label (typically its node ID). |
| 24 | +The algorithm then iteratively updates each node's label to the most frequent label among its neighbors. |
| 25 | +During each iteration, nodes are processed in random order to avoid deterministic bias. |
| 26 | +The algorithm continues until labels stabilize (no changes occur) or a maximum number of iterations is reached. |
| 27 | +The final labels represent community assignments, where nodes sharing the same label belong to the same community. |
| 28 | + |
| 29 | +The algorithm's strength lies in its ability to discover communities without requiring prior knowledge of the number of communities or their sizes. |
| 30 | +It runs in near-linear time and mimics epidemic contagion by spreading labels through the network. |
| 31 | + |
| 32 | +### Performance |
| 33 | + |
| 34 | +CDLP operates with a time complexity of **O(m + n)** per iteration, where: |
| 35 | +- **n** represents the total number of nodes |
| 36 | +- **m** represents the total number of edges |
| 37 | + |
| 38 | +The algorithm typically converges within a few iterations, making it highly efficient for large-scale networks. |
| 39 | + |
| 40 | +## Syntax |
| 41 | + |
| 42 | +```cypher |
| 43 | +CALL algo.labelPropagation([config]) |
| 44 | +``` |
| 45 | + |
| 46 | +### Parameters |
| 47 | + |
| 48 | +The procedure accepts an optional configuration `Map` with the following parameters: |
| 49 | + |
| 50 | +| Name | Type | Default | Description | |
| 51 | +|---------------------|---------|------------------------|----------------------------------------------------------------------------------| |
| 52 | +| `nodeLabels` | Array | All labels | Array of node labels to filter which nodes are included in the computation | |
| 53 | +| `relationshipTypes` | Array | All relationship types | Array of relationship types to define which edges are traversed | |
| 54 | +| `maxIterations` | Integer | 10 | Maximum number of iterations to run the algorithm | |
| 55 | + |
| 56 | +### Return Values |
| 57 | +The procedure returns a stream of records with the following fields: |
| 58 | + |
| 59 | +| Name | Type | Description | |
| 60 | +|---------------|---------|---------------------------------------------------------------------| |
| 61 | +| `node` | Node | The node entity included in the community | |
| 62 | +| `communityId` | Integer | Identifier of the community the node belongs to | |
| 63 | + |
| 64 | +## Examples |
| 65 | + |
| 66 | +Let's take this Social Network as an example: |
| 67 | + |
| 68 | +``` |
| 69 | + (Alice)---(Bob)---(Charlie) (Kate) |
| 70 | + | | | |
| 71 | + (Diana) | (Eve)---(Frank) |
| 72 | + | | | | |
| 73 | + (Grace)--(Henry) (Iris)--(Jack) |
| 74 | +``` |
| 75 | + |
| 76 | +There are 3 different communities that should emerge from this network: |
| 77 | +- Alice, Bob, Charlie, Diana, Grace, Henry |
| 78 | +- Eve, Frank, Iris, Jack |
| 79 | +- Any isolated nodes |
| 80 | + |
| 81 | +### Create the Graph |
| 82 | + |
| 83 | +```cypher |
| 84 | +CREATE |
| 85 | + (alice:Person {name: 'Alice'}), |
| 86 | + (bob:Person {name: 'Bob'}), |
| 87 | + (charlie:Person {name: 'Charlie'}), |
| 88 | + (diana:Person {name: 'Diana'}), |
| 89 | + (eve:Person {name: 'Eve'}), |
| 90 | + (frank:Person {name: 'Frank'}), |
| 91 | + (grace:Person {name: 'Grace'}), |
| 92 | + (henry:Person {name: 'Henry'}), |
| 93 | + (iris:Person {name: 'Iris'}), |
| 94 | + (jack:Person {name: 'Jack'}), |
| 95 | + (kate:Person {name: 'Kate'}), |
| 96 | +
|
| 97 | + (alice)-[:KNOWS]->(bob), |
| 98 | + (bob)-[:KNOWS]->(charlie), |
| 99 | + (alice)-[:KNOWS]->(diana), |
| 100 | + (bob)-[:KNOWS]->(henry), |
| 101 | + (diana)-[:KNOWS]->(grace), |
| 102 | + (grace)-[:KNOWS]->(henry), |
| 103 | + (charlie)-[:KNOWS]->(eve), |
| 104 | + (eve)-[:KNOWS]->(frank), |
| 105 | + (eve)-[:KNOWS]->(iris), |
| 106 | + (frank)-[:KNOWS]->(jack), |
| 107 | + (iris)-[:KNOWS]->(jack) |
| 108 | +``` |
| 109 | + |
| 110 | +### Example: Detect all communities in the network |
| 111 | + |
| 112 | +```cypher |
| 113 | +CALL algo.labelPropagation() YIELD node, communityId RETURN node.name AS name, communityId ORDER BY communityId, name |
| 114 | +``` |
| 115 | + |
| 116 | +#### Expected Results |
| 117 | +| name | communityId | |
| 118 | +|------------|-------------| |
| 119 | +| `Alice` | 0 | |
| 120 | +| `Bob` | 0 | |
| 121 | +| `Charlie` | 0 | |
| 122 | +| `Diana` | 0 | |
| 123 | +| `Grace` | 0 | |
| 124 | +| `Henry` | 0 | |
| 125 | +| `Eve` | 2 | |
| 126 | +| `Frank` | 2 | |
| 127 | +| `Iris` | 2 | |
| 128 | +| `Jack` | 2 | |
| 129 | +| `Kate` | 10 | |
| 130 | + |
| 131 | +### Example: Detect communities with limited iterations |
| 132 | + |
| 133 | +```cypher |
| 134 | +CALL algo.labelPropagation({maxIterations: 5}) YIELD node, communityId |
| 135 | +``` |
| 136 | + |
| 137 | +### Example: Focus on specific node types |
| 138 | + |
| 139 | +```cypher |
| 140 | +CALL algo.labelPropagation({nodeLabels: ['Person']}) YIELD node, communityId |
| 141 | +``` |
| 142 | + |
| 143 | +### Example: Use only certain relationship types |
| 144 | + |
| 145 | +```cypher |
| 146 | +CALL algo.labelPropagation({relationshipTypes: ['KNOWS', 'FRIENDS_WITH']}) YIELD node, communityId |
| 147 | +``` |
| 148 | + |
| 149 | +### Example: Combine node and relationship filtering |
| 150 | + |
| 151 | +```cypher |
| 152 | +CALL algo.labelPropagation({ |
| 153 | + nodeLabels: ['Person'], |
| 154 | + relationshipTypes: ['KNOWS'] |
| 155 | +}) YIELD node, communityId |
| 156 | +``` |
| 157 | + |
| 158 | +### Example: Group communities together |
| 159 | + |
| 160 | +```cypher |
| 161 | +CALL algo.labelPropagation() YIELD node, communityId |
| 162 | +RETURN collect(node.name) AS community_members, communityId, count(*) AS community_size |
| 163 | +ORDER BY community_size DESC |
| 164 | +``` |
| 165 | + |
| 166 | +#### Expected Results |
| 167 | +| community_members | communityId | community_size | |
| 168 | +|----------------------------------------------------------|-------------|----------------| |
| 169 | +| `["Alice", "Bob", "Charlie", "Diana", "Grace", "Henry"]` | 0 | 6 | |
| 170 | +| `["Eve", "Frank", "Iris", "Jack"]` | 2 | 4 | |
| 171 | +| `["Kate"]` | 10 | 1 | |
| 172 | + |
| 173 | +### Example: Find the largest communities |
| 174 | + |
| 175 | +```cypher |
| 176 | +CALL algo.labelPropagation() YIELD node, communityId |
| 177 | +RETURN communityId, collect(node) AS nodes, count(*) AS size |
| 178 | +ORDER BY size DESC |
| 179 | +LIMIT 1 |
| 180 | +``` |
| 181 | + |
0 commit comments