https://github.com/dranidis/kruskal-mst
Kruskal's algorithm for a minimum spanning tree
https://github.com/dranidis/kruskal-mst
graph graphs kruskal-algorithm kruskal-mst minimum-spanning-trees npm-package typescript
Last synced: 5 months ago
JSON representation
Kruskal's algorithm for a minimum spanning tree
- Host: GitHub
- URL: https://github.com/dranidis/kruskal-mst
- Owner: dranidis
- License: mit
- Created: 2021-03-15T20:56:46.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-15T21:23:08.000Z (over 5 years ago)
- Last Synced: 2025-10-25T10:54:36.322Z (8 months ago)
- Topics: graph, graphs, kruskal-algorithm, kruskal-mst, minimum-spanning-trees, npm-package, typescript
- Language: TypeScript
- Homepage:
- Size: 59.6 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://badge.fury.io/js/kruskal-mst)
[](https://travis-ci.com/dranidis/kruskal-mst)
[](https://coveralls.io/github/dranidis/kruskal-mst)
[](https://status.david-dm.org/gh/dranidis/kruskal-mst)
# kruskal-mst
Kruskal's algorithm finds a minimum spanning forest of an undirected edge-weighted graph. If the graph is connected, it finds a minimum spanning tree.
For more information read: https://en.wikipedia.org/wiki/Kruskal%27s_algorithm
## Usage
### Javascript
```javascript
k = require('kruskal-mst');
edges = [
{ from: 'A', to: 'B', weight: 1 },
{ from: 'A', to: 'C', weight: 5 },
{ from: 'A', to: 'E', weight: 7 },
{ from: 'B', to: 'C', weight: 2 },
{ from: 'B', to: 'D', weight: 5 },
{ from: 'C', to: 'F', weight: 8 },
{ from: 'D', to: 'E', weight: 3 },
{ from: 'D', to: 'F', weight: 4 },
{ from: 'E', to: 'F', weight: 5 },
];
mst = k.kruskal(edges);
console.log(mst);
```
#### Output:
```javascript
[
{ from: 'A', to: 'B', weight: 1 },
{ from: 'B', to: 'C', weight: 2 },
{ from: 'D', to: 'E', weight: 3 },
{ from: 'D', to: 'F', weight: 4 },
{ from: 'B', to: 'D', weight: 5 }
]
```
### Typescript
```typescript
import { kruskal, Edge } from 'kruskal-mst';
const edges: Edge[] = [
{ from: 'A', to: 'B', weight: 1 },
{ from: 'A', to: 'C', weight: 5 },
{ from: 'A', to: 'E', weight: 7 },
{ from: 'B', to: 'C', weight: 2 },
{ from: 'B', to: 'D', weight: 5 },
{ from: 'C', to: 'F', weight: 8 },
{ from: 'D', to: 'E', weight: 3 },
{ from: 'D', to: 'F', weight: 4 },
{ from: 'E', to: 'F', weight: 5 },
];
const spanningTree = kruskal(edges);
console.log(spanningTree);
```