Socket
Book a DemoInstallSign in
Socket

kruskal-mst

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kruskal-mst

Implementation of Kruskal's algorithm for finding a minimum spanning forest of an unidirected edge-weighted graph.

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
2.8K
-24.36%
Maintainers
1
Weekly downloads
 
Created
Source

npm version Build Status Coverage Status Dependencies Status

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

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:

[
  { 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

import { kruskal, Edge } from 'kruskal-mst';

const edges: Edge<string>[] = [
    { 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);

Keywords

Kruskal

FAQs

Package last updated on 15 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.