Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: aniruddhaadak80/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: patch-2
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Nov 1, 2024

  1. Update kruskal.js

    1. Error Message: Corrected typo: "Kruskal's algorithms" to "Kruskal's algorithm."
    
    2. Comparison Callback: Simplified by using weight difference directly.
    
    3. Variable Declaration: Replaced @var comments with const for clarity.
    
    4. Algorithm Logic: Maintained the correct implementation of Kruskal's algorithm.
    aniruddhaadak80 authored Nov 1, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    fec79e8 View commit details
Showing with 12 additions and 34 deletions.
  1. +12 −34 src/algorithms/graph/kruskal/kruskal.js
46 changes: 12 additions & 34 deletions src/algorithms/graph/kruskal/kruskal.js
Original file line number Diff line number Diff line change
@@ -7,53 +7,31 @@ import DisjointSet from '../../../data-structures/disjoint-set/DisjointSet';
* @return {Graph}
*/
export default function kruskal(graph) {
// It should fire error if graph is directed since the algorithm works only
// for undirected graphs.
// Check for directed graph
if (graph.isDirected) {
throw new Error('Kruskal\'s algorithms works only for undirected graphs');
throw new Error("Kruskal's algorithm works only for undirected graphs");
}

// Init new graph that will contain minimum spanning tree of original graph.
// Initialize the MST graph
const minimumSpanningTree = new Graph();

// Sort all graph edges in increasing order.
const sortingCallbacks = {
// Sort all edges by weight in ascending order
const sortedEdges = new QuickSort({
/**
* @param {GraphEdge} graphEdgeA
* @param {GraphEdge} graphEdgeB
*/
compareCallback: (graphEdgeA, graphEdgeB) => {
if (graphEdgeA.weight === graphEdgeB.weight) {
return 1;
}
compareCallback: (graphEdgeA, graphEdgeB) => graphEdgeA.weight - graphEdgeB.weight,
}).sort(graph.getAllEdges());

return graphEdgeA.weight <= graphEdgeB.weight ? -1 : 1;
},
};
const sortedEdges = new QuickSort(sortingCallbacks).sort(graph.getAllEdges());
// Initialize disjoint sets for vertices
const disjointSet = new DisjointSet((vertex) => vertex.getKey());
graph.getAllVertices().forEach((vertex) => disjointSet.makeSet(vertex));

// Create disjoint sets for all graph vertices.
const keyCallback = (graphVertex) => graphVertex.getKey();
const disjointSet = new DisjointSet(keyCallback);

graph.getAllVertices().forEach((graphVertex) => {
disjointSet.makeSet(graphVertex);
});

// Go through all edges started from the minimum one and try to add them
// to minimum spanning tree. The criteria of adding the edge would be whether
// it is forms the cycle or not (if it connects two vertices from one disjoint
// set or not).
for (let edgeIndex = 0; edgeIndex < sortedEdges.length; edgeIndex += 1) {
/** @var {GraphEdge} currentEdge */
const currentEdge = sortedEdges[edgeIndex];

// Check if edge forms the cycle. If it does then skip it.
// Add edges to the MST if they don’t form a cycle
for (const currentEdge of sortedEdges) {
if (!disjointSet.inSameSet(currentEdge.startVertex, currentEdge.endVertex)) {
// Unite two subsets into one.
disjointSet.union(currentEdge.startVertex, currentEdge.endVertex);

// Add this edge to spanning tree.
minimumSpanningTree.addEdge(currentEdge);
}
}