Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,15 @@ propvalue
ro
GenAI

WCC
WSL
Kruskal's
MSF
MST
MSTs
Prim's
SPpath
SSpath
WCC
WSL

undirected
preprocessing
Expand Down
3 changes: 3 additions & 0 deletions algorithms/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ This overview summarizes the available algorithms and links to their individual
- **[SSpath](./sspath.md)**
Enumerates all paths from a single source node to other nodes, based on constraints like edge filters and depth.

- **[MSF](./msf.md)**
Computes the Minimum Spanning Forest of a graph, finding the minimum spanning tree for each connected component.

For path expressions like `shortestPath()` used directly in Cypher queries, refer to the [Cypher Path Functions section](../cypher/functions.md#path-functions).

## Centrality Measures
Expand Down
177 changes: 177 additions & 0 deletions algorithms/msf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
---
title: "MSF"
description: "Minimum Spanning Forest Algorithm"
parent: "Algorithms"
nav_order: 9
---

# Minimum Spanning Forest (MSF)

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.

## What is a Minimum Spanning Forest?

- For a **connected graph**, the MSF is a single minimum spanning tree (MST) that connects all nodes with the minimum total edge weight
- For a **disconnected graph**, the MSF consists of multiple MSTs, one for each connected component
- 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

## Use Cases

- **Network Design**: Minimize cable/pipeline costs when connecting multiple locations
- **Clustering**: Identify natural groupings in data by analyzing the forest structure
- **Image Segmentation**: Group similar pixels using edge weights as similarity measures
- **Road Networks**: Optimize road construction to connect all cities with minimum cost

## Syntax

```cypher
CALL algo.MSF(
config: MAP
) YIELD src, dest, weight, relationshipType
```

### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `config` | MAP | Configuration map containing algorithm parameters |

#### Configuration Options

| Option | Type | Required | Default | Description |
|--------|------|----------|---------|-------------|
| `sourceNodes` | List of Nodes | No | All nodes | Starting nodes for the algorithm. If not provided, all nodes in the graph are considered |
| `relationshipTypes` | List of Strings | No | All types | Relationship types to traverse. If not provided, all relationship types are considered |
| `relationshipWeightProperty` | String | No | `null` | Property name containing edge weights. If not specified, all edges have weight 1.0 |
| `defaultValue` | Float | No | `1.0` | Default weight for edges that don't have the weight property |

### Returns

| Field | Type | Description |
|-------|------|-------------|
| `src` | Node | Source node of the edge in the spanning forest |
| `dest` | Node | Destination node of the edge in the spanning forest |
| `weight` | Float | Weight of the edge |
| `relationshipType` | String | Type of the relationship |

## Examples

### Example 1: Basic MSF with Unweighted Graph

Find the minimum spanning forest treating all edges equally:

```cypher
CALL algo.MSF({}) YIELD src, dest, weight, relationshipType
RETURN src.name AS source, dest.name AS destination, weight, relationshipType
```

### Example 2: MSF with Weighted Edges

Consider a graph representing cities connected by roads with distances:

```cypher
// Create a weighted graph
CREATE (a:City {name: 'A'}), (b:City {name: 'B'}), (c:City {name: 'C'}),
(d:City {name: 'D'}), (e:City {name: 'E'})
CREATE (a)-[:ROAD {distance: 2}]->(b),
(a)-[:ROAD {distance: 3}]->(c),
(b)-[:ROAD {distance: 1}]->(c),
(b)-[:ROAD {distance: 4}]->(d),
(c)-[:ROAD {distance: 5}]->(d),
(d)-[:ROAD {distance: 6}]->(e)

// Find minimum spanning forest using distance weights
CALL algo.MSF({
relationshipWeightProperty: 'distance'
}) YIELD src, dest, weight
RETURN src.name AS from, dest.name AS to, weight AS distance
ORDER BY weight
```

**Result:**
```text
from | to | distance
-----|----|---------
B | C | 1.0
A | B | 2.0
A | C | 3.0
Copy link

Copilot AI Nov 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example result is incorrect. According to Kruskal's/Prim's algorithm for MSF, once nodes A, B, and C are connected (via edges B-C with weight 1.0 and A-B with weight 2.0), the edge A-C with weight 3.0 would create a cycle and should not be included in the MSF. The correct result should only include edges with weights 1.0, 2.0, 4.0, and 6.0.

Suggested change
A | C | 3.0

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

B | D | 4.0
D | E | 6.0
```

### Example 3: MSF on Specific Relationship Types

Find the spanning forest considering only specific relationship types:

```cypher
CALL algo.MSF({
relationshipTypes: ['ROAD', 'HIGHWAY'],
relationshipWeightProperty: 'distance'
}) YIELD src, dest, weight, relationshipType
RETURN src.name AS from, dest.name AS to, weight, relationshipType
```

### Example 4: MSF Starting from Specific Nodes

Compute the spanning forest starting from a subset of nodes:

```cypher
MATCH (start:City) WHERE start.name IN ['A', 'B']
WITH collect(start) AS startNodes
CALL algo.MSF({
sourceNodes: startNodes,
relationshipWeightProperty: 'distance'
}) YIELD src, dest, weight
RETURN src.name AS from, dest.name AS to, weight
```

### Example 5: Disconnected Graph

For a graph with multiple components, MSF returns multiple trees:

```cypher
// Create two disconnected components
CREATE (a:Node {name: 'A'})-[:CONNECTED {weight: 1}]->(b:Node {name: 'B'}),
(b)-[:CONNECTED {weight: 2}]->(c:Node {name: 'C'}),
(x:Node {name: 'X'})-[:CONNECTED {weight: 3}]->(y:Node {name: 'Y'})

// Find MSF
CALL algo.MSF({
relationshipWeightProperty: 'weight'
}) YIELD src, dest, weight
RETURN src.name AS from, dest.name AS to, weight
```

**Result:** Two separate trees (A-B-C and X-Y)

## Algorithm Details

FalkorDB's MSF implementation uses an efficient matrix-based approach optimized for graph databases:

1. **Connected Components**: First identifies all connected components in the graph
2. **MST per Component**: Computes a minimum spanning tree for each component using a variant of Kruskal's or Prim's algorithm
3. **Edge Selection**: Selects edges in order of increasing weight, avoiding cycles

### Performance Characteristics

- **Time Complexity**: O(E log V) where E is the number of edges and V is the number of vertices
- **Space Complexity**: O(V + E)
- **Optimized**: Uses sparse matrix representation for efficient computation

## Best Practices

1. **Weight Properties**: Ensure weight properties are numeric (integers or floats)
2. **Missing Weights**: Use `defaultValue` to handle edges without weight properties
3. **Large Graphs**: For large graphs (100K+ nodes), consider filtering by `sourceNodes` or `relationshipTypes`
4. **Directed vs Undirected**: The algorithm treats relationships as undirected for spanning forest purposes

## Related Algorithms

- **[WCC (Weakly Connected Components)](./wcc.md)**: Identify connected components before running MSF
- **[BFS](./bfs.md)**: Traverse the resulting spanning forest
- **[SPpath](./sppath.md)**: Find shortest paths using the spanning forest structure

## See Also

- [Cypher Procedures](../cypher/procedures.md)
- [Graph Algorithms Overview](./index.md)
6 changes: 5 additions & 1 deletion cypher/procedures.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ GRAPH.QUERY social "CALL db.labels() YIELD label"
| db.idx.fulltext.createNodeIndex | `label`, `property` [, `property` ...] | none | Builds a full-text searchable index on a label and the 1 or more specified properties. |
| db.idx.fulltext.drop | `label` | none | Deletes the full-text index associated with the given label. |
| db.idx.fulltext.queryNodes | `label`, `string` | `node`, `score` | Retrieve all nodes that contain the specified string in the full-text indexes on the given label. |
| db.idx.fulltext.queryRelationships | `relationshipType`, `string` | `relationship`, `score` | Retrieve all relationships that contain the specified string in the full-text indexes on the given relationship type. See [Full-Text Indexing](/cypher/indexing#full-text-indexing) for details. |
Copy link

Copilot AI Nov 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line numbering appears inconsistent. Line 35 follows line 34 directly, but the surrounding context shows lines 32-34 already exist. This creates a numbering gap and could indicate misaligned line numbers in the diff.

Copilot uses AI. Check for mistakes.
| db.idx.vector.queryNodes | `label`, `attribute`, `k`, `query` | `node`, `score` | Retrieve up to k nodes with vectors most similar to the query vector using the specified label and attribute. See [Vector Indexing](/cypher/indexing#vector-indexing) for details. |
| db.idx.vector.queryRelationships | `relationshipType`, `attribute`, `k`, `query` | `relationship`, `score` | Retrieve up to k relationships with vectors most similar to the query vector using the specified relationship type and attribute. See [Vector Indexing](/cypher/indexing#vector-indexing) for details. |
| algo.pageRank | `label`, `relationship-type` | `node`, `score` | Runs the pagerank algorithm over nodes of given label, considering only edges of given relationship type. |
| [algo.BFS](#BFS) | `source-node`, `max-level`, `relationship-type` | `nodes`, `edges` | Performs BFS to find all nodes connected to the source. A `max level` of 0 indicates unlimited and a non-NULL `relationship-type` defines the relationship type that may be traversed. |
| algo.BFS | `source-node`, `max-level`, `relationship-type` | `nodes`, `edges` | Performs BFS to find all nodes connected to the source. A `max level` of 0 indicates unlimited and a non-NULL `relationship-type` defines the relationship type that may be traversed. See [BFS Algorithm](/algorithms/bfs) for details. |
| algo.MSF | `config` | `src`, `dest`, `weight`, `relationshipType` | Computes the Minimum Spanning Forest of the graph. See [MSF Algorithm](/algorithms/msf) for details. |
| dbms.procedures() | none | `name`, `mode` | List all procedures in the DBMS, yields for every procedure its name and mode (read/write). |
Loading