trekhleb/javascript-algorithms · error · Error
Prim's algorithms works only for undirected graphs
Error message
Prim's algorithms works only for undirected graphs
What it means
Error "Prim's algorithms works only for undirected graphs" thrown in trekhleb/javascript-algorithms.
Source
Thrown at src/algorithms/graph/prim/prim.js:12
export default function prim(graph) {
// It should fire error if graph is directed since the algorithm works only
// for undirected graphs.
if (graph.isDirected) {
throw new Error('Prim\'s algorithms works only for undirected graphs');
}View on GitHub (pinned to 85293e3e2b)
Solutions
- Instantiate the graph as undirected with new Graph() before calling prim.
- Convert directed edges to undirected edges prior to building the minimum spanning tree.
- For directed graphs use an arborescence algorithm such as Edmonds' instead of Prim's.
When it happens
Trigger: prim is called on a graph where graph.isDirected is true; the minimum spanning tree algorithm only works for undirected graphs.
Common situations: Running Prim's minimum spanning tree algorithm on a directed graph; Prim's algorithm assumes undirected weighted edges.
AI-assisted analysis of trekhleb/javascript-algorithms@85293e3e2b (2026-08-24).
Data as JSON: /api/errors/ca92bb347c81ac04.
Report an issue: GitHub.