|
| 1 | +--- |
| 2 | +title: "MSF" |
| 3 | +description: "Minimum Spanning Forest Algorithm" |
| 4 | +parent: "Algorithms" |
| 5 | +nav_order: 9 |
| 6 | +--- |
| 7 | + |
| 8 | +# Minimum Spanning Forest (MSF) |
| 9 | + |
| 10 | +The Minimum Spanning Forest algorithm computes the minimum spanning forest of a graph. A minimum spanning forest is a collection of minimum spanning trees, one for each connected component in the graph. |
| 11 | + |
| 12 | +## What is a Minimum Spanning Forest? |
| 13 | + |
| 14 | +- For a **connected graph**, the MSF is a single minimum spanning tree (MST) that connects all nodes with the minimum total edge weight |
| 15 | +- For a **disconnected graph**, the MSF consists of multiple MSTs, one for each connected component |
| 16 | +- The forest contains no cycles and has exactly `N - C` edges, where `N` is the number of nodes and `C` is the number of connected components |
| 17 | + |
| 18 | +## Use Cases |
| 19 | + |
| 20 | +- **Network Design**: Minimize cable/pipeline costs when connecting multiple locations |
| 21 | +- **Clustering**: Identify natural groupings in data by analyzing the forest structure |
| 22 | +- **Image Segmentation**: Group similar pixels using edge weights as similarity measures |
| 23 | +- **Road Networks**: Optimize road construction to connect all cities with minimum cost |
| 24 | + |
| 25 | +## Syntax |
| 26 | + |
| 27 | +```cypher |
| 28 | +CALL algo.MSF( |
| 29 | + config: MAP |
| 30 | +) YIELD src, dest, weight, relationshipType |
| 31 | +``` |
| 32 | + |
| 33 | +### Parameters |
| 34 | + |
| 35 | +| Parameter | Type | Description | |
| 36 | +|-----------|------|-------------| |
| 37 | +| `config` | MAP | Configuration map containing algorithm parameters | |
| 38 | + |
| 39 | +#### Configuration Options |
| 40 | + |
| 41 | +| Option | Type | Required | Default | Description | |
| 42 | +|--------|------|----------|---------|-------------| |
| 43 | +| `sourceNodes` | List of Nodes | No | All nodes | Starting nodes for the algorithm. If not provided, all nodes in the graph are considered | |
| 44 | +| `relationshipTypes` | List of Strings | No | All types | Relationship types to traverse. If not provided, all relationship types are considered | |
| 45 | +| `relationshipWeightProperty` | String | No | `null` | Property name containing edge weights. If not specified, all edges have weight 1.0 | |
| 46 | +| `defaultValue` | Float | No | `1.0` | Default weight for edges that don't have the weight property | |
| 47 | + |
| 48 | +### Returns |
| 49 | + |
| 50 | +| Field | Type | Description | |
| 51 | +|-------|------|-------------| |
| 52 | +| `src` | Node | Source node of the edge in the spanning forest | |
| 53 | +| `dest` | Node | Destination node of the edge in the spanning forest | |
| 54 | +| `weight` | Float | Weight of the edge | |
| 55 | +| `relationshipType` | String | Type of the relationship | |
| 56 | + |
| 57 | +## Examples |
| 58 | + |
| 59 | +### Example 1: Basic MSF with Unweighted Graph |
| 60 | + |
| 61 | +Find the minimum spanning forest treating all edges equally: |
| 62 | + |
| 63 | +```cypher |
| 64 | +CALL algo.MSF({}) YIELD src, dest, weight, relationshipType |
| 65 | +RETURN src.name AS source, dest.name AS destination, weight, relationshipType |
| 66 | +``` |
| 67 | + |
| 68 | +### Example 2: MSF with Weighted Edges |
| 69 | + |
| 70 | +Consider a graph representing cities connected by roads with distances: |
| 71 | + |
| 72 | +```cypher |
| 73 | +// Create a weighted graph |
| 74 | +CREATE (a:City {name: 'A'}), (b:City {name: 'B'}), (c:City {name: 'C'}), |
| 75 | + (d:City {name: 'D'}), (e:City {name: 'E'}) |
| 76 | +CREATE (a)-[:ROAD {distance: 2}]->(b), |
| 77 | + (a)-[:ROAD {distance: 3}]->(c), |
| 78 | + (b)-[:ROAD {distance: 1}]->(c), |
| 79 | + (b)-[:ROAD {distance: 4}]->(d), |
| 80 | + (c)-[:ROAD {distance: 5}]->(d), |
| 81 | + (d)-[:ROAD {distance: 6}]->(e) |
| 82 | +
|
| 83 | +// Find minimum spanning forest using distance weights |
| 84 | +CALL algo.MSF({ |
| 85 | + relationshipWeightProperty: 'distance' |
| 86 | +}) YIELD src, dest, weight |
| 87 | +RETURN src.name AS from, dest.name AS to, weight AS distance |
| 88 | +ORDER BY weight |
| 89 | +``` |
| 90 | + |
| 91 | +**Result:** |
| 92 | +```text |
| 93 | +from | to | distance |
| 94 | +-----|----|--------- |
| 95 | +B | C | 1.0 |
| 96 | +A | B | 2.0 |
| 97 | +A | C | 3.0 |
| 98 | +B | D | 4.0 |
| 99 | +D | E | 6.0 |
| 100 | +``` |
| 101 | + |
| 102 | +### Example 3: MSF on Specific Relationship Types |
| 103 | + |
| 104 | +Find the spanning forest considering only specific relationship types: |
| 105 | + |
| 106 | +```cypher |
| 107 | +CALL algo.MSF({ |
| 108 | + relationshipTypes: ['ROAD', 'HIGHWAY'], |
| 109 | + relationshipWeightProperty: 'distance' |
| 110 | +}) YIELD src, dest, weight, relationshipType |
| 111 | +RETURN src.name AS from, dest.name AS to, weight, relationshipType |
| 112 | +``` |
| 113 | + |
| 114 | +### Example 4: MSF Starting from Specific Nodes |
| 115 | + |
| 116 | +Compute the spanning forest starting from a subset of nodes: |
| 117 | + |
| 118 | +```cypher |
| 119 | +MATCH (start:City) WHERE start.name IN ['A', 'B'] |
| 120 | +WITH collect(start) AS startNodes |
| 121 | +CALL algo.MSF({ |
| 122 | + sourceNodes: startNodes, |
| 123 | + relationshipWeightProperty: 'distance' |
| 124 | +}) YIELD src, dest, weight |
| 125 | +RETURN src.name AS from, dest.name AS to, weight |
| 126 | +``` |
| 127 | + |
| 128 | +### Example 5: Disconnected Graph |
| 129 | + |
| 130 | +For a graph with multiple components, MSF returns multiple trees: |
| 131 | + |
| 132 | +```cypher |
| 133 | +// Create two disconnected components |
| 134 | +CREATE (a:Node {name: 'A'})-[:CONNECTED {weight: 1}]->(b:Node {name: 'B'}), |
| 135 | + (b)-[:CONNECTED {weight: 2}]->(c:Node {name: 'C'}), |
| 136 | + (x:Node {name: 'X'})-[:CONNECTED {weight: 3}]->(y:Node {name: 'Y'}) |
| 137 | +
|
| 138 | +// Find MSF |
| 139 | +CALL algo.MSF({ |
| 140 | + relationshipWeightProperty: 'weight' |
| 141 | +}) YIELD src, dest, weight |
| 142 | +RETURN src.name AS from, dest.name AS to, weight |
| 143 | +``` |
| 144 | + |
| 145 | +**Result:** Two separate trees (A-B-C and X-Y) |
| 146 | + |
| 147 | +## Algorithm Details |
| 148 | + |
| 149 | +FalkorDB's MSF implementation uses an efficient matrix-based approach optimized for graph databases: |
| 150 | + |
| 151 | +1. **Connected Components**: First identifies all connected components in the graph |
| 152 | +2. **MST per Component**: Computes a minimum spanning tree for each component using a variant of Kruskal's or Prim's algorithm |
| 153 | +3. **Edge Selection**: Selects edges in order of increasing weight, avoiding cycles |
| 154 | + |
| 155 | +### Performance Characteristics |
| 156 | + |
| 157 | +- **Time Complexity**: O(E log V) where E is the number of edges and V is the number of vertices |
| 158 | +- **Space Complexity**: O(V + E) |
| 159 | +- **Optimized**: Uses sparse matrix representation for efficient computation |
| 160 | + |
| 161 | +## Best Practices |
| 162 | + |
| 163 | +1. **Weight Properties**: Ensure weight properties are numeric (integers or floats) |
| 164 | +2. **Missing Weights**: Use `defaultValue` to handle edges without weight properties |
| 165 | +3. **Large Graphs**: For large graphs (100K+ nodes), consider filtering by `sourceNodes` or `relationshipTypes` |
| 166 | +4. **Directed vs Undirected**: The algorithm treats relationships as undirected for spanning forest purposes |
| 167 | + |
| 168 | +## Related Algorithms |
| 169 | + |
| 170 | +- **[WCC (Weakly Connected Components)](./wcc.md)**: Identify connected components before running MSF |
| 171 | +- **[BFS](./bfs.md)**: Traverse the resulting spanning forest |
| 172 | +- **[SPpath](./sppath.md)**: Find shortest paths using the spanning forest structure |
| 173 | + |
| 174 | +## See Also |
| 175 | + |
| 176 | +- [Cypher Procedures](../cypher/procedures.md) |
| 177 | +- [Graph Algorithms Overview](./index.md) |
0 commit comments