trekhleb/javascript-algorithms · error · Error

Kruskal's algorithms works only for undirected graphs

Error message

Kruskal's algorithms works only for undirected graphs

What it means

Error "Kruskal's algorithms works only for undirected graphs" thrown in trekhleb/javascript-algorithms.

Source

Thrown at src/algorithms/graph/kruskal/kruskal.js:13

export default function kruskal(graph) {
  // It should fire error if graph is directed since the algorithm works only
  // for undirected graphs.
  if (graph.isDirected) {
    throw new Error('Kruskal\'s algorithms works only for undirected graphs');
  }

View on GitHub (pinned to 85293e3e2b)

Solutions

  1. Create the graph as undirected with new Graph() (the default) instead of new Graph(true).
  2. Convert the directed graph to an undirected one before running Kruskal's algorithm.
  3. For directed graphs use a directed-MST algorithm such as Edmonds' algorithm.

When it happens

Trigger: kruskal is called on a graph where graph.isDirected is true; the minimum spanning tree algorithm only works for undirected graphs.

Common situations: Passing a directed Graph instance to Kruskal's minimum spanning tree algorithm, which is defined only for undirected weighted graphs.


AI-assisted analysis of trekhleb/javascript-algorithms@85293e3e2b (2026-08-24). Data as JSON: /api/errors/02d62ae5f1949165. Report an issue: GitHub.