Skip to content

Commit fec79e8

Browse files
authoredNov 1, 2024
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.
1 parent ca3d16d commit fec79e8

File tree

1 file changed

+12
-34
lines changed

1 file changed

+12
-34
lines changed
 

‎src/algorithms/graph/kruskal/kruskal.js

+12-34
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,31 @@ import DisjointSet from '../../../data-structures/disjoint-set/DisjointSet';
77
* @return {Graph}
88
*/
99
export default function kruskal(graph) {
10-
// It should fire error if graph is directed since the algorithm works only
11-
// for undirected graphs.
10+
// Check for directed graph
1211
if (graph.isDirected) {
13-
throw new Error('Kruskal\'s algorithms works only for undirected graphs');
12+
throw new Error("Kruskal's algorithm works only for undirected graphs");
1413
}
1514

16-
// Init new graph that will contain minimum spanning tree of original graph.
15+
// Initialize the MST graph
1716
const minimumSpanningTree = new Graph();
1817

19-
// Sort all graph edges in increasing order.
20-
const sortingCallbacks = {
18+
// Sort all edges by weight in ascending order
19+
const sortedEdges = new QuickSort({
2120
/**
2221
* @param {GraphEdge} graphEdgeA
2322
* @param {GraphEdge} graphEdgeB
2423
*/
25-
compareCallback: (graphEdgeA, graphEdgeB) => {
26-
if (graphEdgeA.weight === graphEdgeB.weight) {
27-
return 1;
28-
}
24+
compareCallback: (graphEdgeA, graphEdgeB) => graphEdgeA.weight - graphEdgeB.weight,
25+
}).sort(graph.getAllEdges());
2926

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

35-
// Create disjoint sets for all graph vertices.
36-
const keyCallback = (graphVertex) => graphVertex.getKey();
37-
const disjointSet = new DisjointSet(keyCallback);
38-
39-
graph.getAllVertices().forEach((graphVertex) => {
40-
disjointSet.makeSet(graphVertex);
41-
});
42-
43-
// Go through all edges started from the minimum one and try to add them
44-
// to minimum spanning tree. The criteria of adding the edge would be whether
45-
// it is forms the cycle or not (if it connects two vertices from one disjoint
46-
// set or not).
47-
for (let edgeIndex = 0; edgeIndex < sortedEdges.length; edgeIndex += 1) {
48-
/** @var {GraphEdge} currentEdge */
49-
const currentEdge = sortedEdges[edgeIndex];
50-
51-
// Check if edge forms the cycle. If it does then skip it.
31+
// Add edges to the MST if they don’t form a cycle
32+
for (const currentEdge of sortedEdges) {
5233
if (!disjointSet.inSameSet(currentEdge.startVertex, currentEdge.endVertex)) {
53-
// Unite two subsets into one.
5434
disjointSet.union(currentEdge.startVertex, currentEdge.endVertex);
55-
56-
// Add this edge to spanning tree.
5735
minimumSpanningTree.addEdge(currentEdge);
5836
}
5937
}

0 commit comments

Comments
 (0)