Why the tree resulting from Kruskal is different from Dijkstra?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In computer science, algorithms discussing graph structures are a crucial component of efficient network analysis, optimization, and navigation. Two such algorithms are Kruskal's and Dijkstra's, each with distinct purposes and operational mechanisms. While both work on graphs, they give different results due to their differing objectives. This piece delves into why the tree resulting from Kruskal's algorithm is different from Dijkstra's, utilizing technical explanations, examples, and a comprehensive summary table.
Kruskal's Algorithm
Kruskal's algorithm is designed to find the Minimum Spanning Tree (MST) of a given, connected, and undirected graph. A spanning tree is a subset of the graph that connects all vertices with the minimum possible total edge weight.
The Process:
- Sort the edges of the graph in non-decreasing order based on their weights.
- Initialize a forest, where each vertex is a separate tree.
- Iterate through the sorted edges, adding an edge to the growing spanning tree if it doesn't form a cycle (using a union-find data structure).
Example: Consider a graph with vertices and edges with given weights. After sorting, the algorithm picks the smallest weight edges while avoiding cycles, forming the MST inclusive of all vertices at the minimal weight.
Dijkstra's Algorithm
Dijkstra's algorithm, in contrast, computes the shortest paths from a single source vertex to all other vertices in a weighted graph, which may be directed.
The Process:
- Initialize the distances, setting the source vertex distance to zero and all others to infinity.
- Use a priority queue to continually extract the vertex with the smallest known distance.
- Update the distances to neighboring vertices if a shorter path is found.
Example: For the same graph of , starting at vertex A, Dijkstra would adjust distances as it identifies shorter routes to vertices by examining available paths.
Key Differences and Resulting Trees
The results of these algorithms are disparate due to differing aims: spanning trees versus shortest paths.
Spanning Trees (Kruskal): • Purpose: Connects all vertices. • Structure: No cycles, minimal total edge weight. • Use-cases: Network design, circuit design.
Shortest Path Trees (Dijkstra): • Purpose: Shortest path from source to all nodes. • Structure: Paths may form a tree or not (possible with paths converging/intersecting beyond the source). • Use-cases: Routing, navigation systems.
Visual Example:
Consider a graph with weights:
4 2 1
• Kruskal's MST: A-B, B-D, C-D (Total Weight = 4) • Dijkstra (from A): A-B-D, A-C (routes based on individual path costs)
Related reading
- Why there is no stdcopy_if algorithm?
- Why use binary search if there's ternary search?
- Why use Dijkstra's Algorithm if Breadth First Search BFS can do the same thing faster?
- Why use Dijkstra's algorithm instead of Best Cheapest First Search?
- Why there is no ConcurrentHashSet against ConcurrentHashMap
- Why updating shallow copy dictionary doesn't update original dictionary?
- Why use stdranges algorithms over regular algorithms?
- Why use two different algorithm for sorting arrays?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.