Here’s a clear comparison of the basic differences among Kruskal's, Prim's, Dijkstra's,
Floyd-Warshall, and Bellman-Ford algorithms:
1. Purpose:
Algorithm Purpose
Kruskal's Finds a Minimum Spanning Tree (MST) in an undirected graph.
Prim's Finds a Minimum Spanning Tree (MST) in an undirected graph.
Dijkstra's Finds the shortest path from a source node to all other nodes in a
weighted graph (non-negative weights).
Floyd-Warsh Finds the shortest paths between all pairs of nodes in a weighted
all graph (handles negative weights, but no negative cycles).
Bellman-For Finds the shortest path from a source node to all other nodes in a
d weighted graph (handles negative weights, detects negative cycles).
2. Graph Type:
Algorithm Graph Type
Kruskal's Undirected, weighted graph.
Prim's Undirected, weighted graph.
Dijkstra's Directed or undirected, weighted graph (non-negative weights).
Floyd-Warshall Directed or undirected, weighted graph (handles negative weights).
Bellman-Ford Directed or undirected, weighted graph (handles negative weights).
3. Approach:
Algorithm Approach
Kruskal's Greedy algorithm. Adds edges in increasing order of weight, avoiding
cycles.
Prim's Greedy algorithm. Grows the MST by adding the smallest edge
connected to the current tree.
Dijkstra's Greedy algorithm. Relaxes edges from the source node to find the
shortest path.
Floyd-Warsha Dynamic programming. Computes shortest paths between all pairs
ll of nodes.
Bellman-Ford Relaxes edges repeatedly to find the shortest path, even with
negative weights.
5. Edge Weight Handling:
Algorithm Edge Weight Handling
Kruskal's Works with positive and negative weights.
Prim's Works with positive and negative weights.
Dijkstra's Works only with non-negative weights.
Floyd-Warshall Works with positive and negative weights (no negative cycles).
Bellman-Ford Works with positive and negative weights (detects negative
cycles).
6. Output:
Algorithm Output
Kruskal's A set of edges forming the Minimum Spanning Tree (MST).
Prim's A set of edges forming the Minimum Spanning Tree (MST).
Dijkstra's Shortest path distances from the source node to all other nodes.
Floyd-Warsha Shortest path distances between all pairs of nodes.
ll
Bellman-Ford Shortest path distances from the source node to all other nodes (or
detects negative cycles).
Calculations
Time complexity is about the number of operations an algorithm performs as a
function of the input size. It gives us a way to analyze how the computational effort
(number of steps or operations) grows when the input size increases.
What Are "Operations"?
● An operation can be any basic step the algorithm performs, such as:
○ Comparisons (e.g., comparing two numbers)
○ Arithmetic operations (e.g., addition, multiplication)
○ Memory accesses (e.g., reading or writing to an array)
○ Loop iterations
○ Function calls
● The exact nature of an "operation" depends on the algorithm and the problem
being solved
Time Complexity vs. Actual Runtime
● Time Complexity: Describes the number of operations as a function of input size
● Actual Runtime: Depends on:
○ Hardware (e.g., CPU speed, memory)
○ Programming language (e.g., C++ is faster than Python)
○ Implementation details (e.g., optimizations)