{"record":{"id":"ca92bb347c81ac04","repo":"trekhleb/javascript-algorithms","slug":"prim-s-algorithms-works-only-for-undirected-graphs","errorCode":null,"errorMessage":"Prim's algorithms works only for undirected graphs","messagePattern":"Prim's algorithms works only for undirected graphs","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/algorithms/graph/prim/prim.js","lineNumber":12,"sourceCode":"export default function prim(graph) {\n  // It should fire error if graph is directed since the algorithm works only\n  // for undirected graphs.\n  if (graph.isDirected) {\n    throw new Error('Prim\\'s algorithms works only for undirected graphs');\n  }","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/trekhleb/javascript-algorithms/blob/85293e3e2b88f4d2ce330d956b139cf628aa1e82/src/algorithms/graph/prim/prim.js#L1-L30","documentation":"Error \"Prim's algorithms works only for undirected graphs\" thrown in trekhleb/javascript-algorithms.","triggerScenarios":"prim is called on a graph where graph.isDirected is true; the minimum spanning tree algorithm only works for undirected graphs.","commonSituations":"Running Prim's minimum spanning tree algorithm on a directed graph; Prim's algorithm assumes undirected weighted edges.","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."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"85293e3e2b88f4d2ce330d956b139cf628aa1e82","analyzedAt":"2026-08-24T05:59:10.417Z","schemaVersion":2},"datasetVersion":"2026-08-24T07:17:09.176Z"}