{"size": 421, "ext": "js", "max_stars_repo_path": "src/crackingTheCodeInterview/treesAndGraphs/kruskal.js", "max_stars_repo_name": "marco-fp/node-playground", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/crackingTheCodeInterview/treesAndGraphs/kruskal.js", "max_issues_repo_name": "marco-fp/node-playground", "max_issues_repo_issues_event_min_datetime": "2020-09-04T14:39:56.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-07T20:34:43.000Z", "max_forks_repo_path": "src/crackingTheCodeInterview/treesAndGraphs/kruskal.js", "max_forks_repo_name": "marco-fp/node-playground", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 3, "max_forks_count": null, "avg_line_length": 23.3888888889, "max_line_length": 60, "alphanum_fraction": 0.5724465558}
function kruskal(graph) {
    const sortedEdges = [...graph.edges].sort(
        (a, b) => a.weight - b.weight
    );

    const disjoinedSet = new DisjoinedSet(graph.nodes);
    const mst = new Graph();

    sortedEdges.forEach(edge => {
        if (!disjoinedSet.inSameSet(edge.start, edge.end)) {
            disjoinedSet.union(edge.start, edge.end);
            mst.addEdge(edge);
        }
    })

    return mst;
}
