💻
Algorithm
  • README
  • Array
    • At Most To Equal
    • Count Inversions In An Array
    • Interleaving Placement
    • Kadane
    • Left To Right State Transition
    • Permutation
    • Quick Select
    • Sliding Window
    • Two Pointers
  • Binary Tree
    • Avl Tree
    • Binary Search Tree
    • Serialization And Deserialization
    • Traversal
  • Company
    • Facebook
  • Cpp
    • Array
    • Memset 3 F
    • Overflow
  • Data Structure
    • Binary Indexed Tree
    • Segment Tree And Binary Index Tree
    • Segment Tree
    • Stack
    • Trie
    • Union Find
  • Dynamic Programming
    • Knapsack
      • 0 1 Knapsack
      • Bounded Knapsack
      • Unbounded Knapsack
    • Bitmask Dp
    • Dp On Subsets
    • Dp On Tree
    • Dp With Sorting
    • Selective State Dp
    • Travelling Salesperson
  • Graph
    • Minimum Spanning Tree
      • Kruskal
      • Prim
    • Shortest Path
      • Bellman Ford
      • Dijkstra
      • Floyd Warshall
      • Johnson
      • Shortest Path Faster Algorithm
    • Bi Directional Breadth First Search
    • Bipartite
    • Breadth First Search
    • Component Coloring
    • Component Count
    • Depth First Search
    • Eulerian Path
    • Maximum Bipartite Matching
    • Tarjan
    • Topological Sort
    • Tree Diameter
    • Tree Ring Order Traversal
  • Greedy
    • Greedy Scheduling
    • Regret Greedy
  • Math
    • Catalan Number
    • Combinatorics
    • Factorial
    • Factorization
    • Fast Pow
    • Gcd
    • Geometry
    • Get Digits
    • Lcm
    • Median Minimizes Sum Of Absolute Deviations
    • Mode
    • Modular Multiplicative Inverse
    • Palindrome
    • Prime Number
    • Round Up
    • Sieve Of Eratosthenes
    • Stars And Bars
    • Sum Of Sequence
  • Miscellaneous
    • Bin Packing
    • Floyds Tortoise And Hare
    • Hungarian
    • Palindrome
  • Sort
    • Bubble Sort
    • Cycle Sort
    • Heap Sort
    • Merge Sort
    • Quick Sort
    • Sorting
  • Stl
    • Cpp Stl
    • Istringstream
    • Lower Bound Upper Bound
    • Priority Queue
  • String
    • Kmp
    • Manacher
    • Rabin Karp
    • String Processing
    • Z
  • Backtracking
  • Binary Answer
  • Binary Lifting
  • Binary Search
  • Bit Manipulation
  • Date
  • Difference Array
  • Discretization
  • Divide And Conquer
  • Gray Code
  • Great Problems For Practice
  • Interval Scheduling Maximization
  • Io Optimization
  • K Subset Partitioning
  • Line Sweep
  • Longest Common Subsequence
  • Longest Increasing Subsequence
  • Meet In The Middle
  • Minmax
  • Mono Deque
  • Monotonic Stack
  • Offline Query
  • P And Np
  • Prefix State Map
  • Prefix Sum
  • Random
  • Reservoir Sampling
  • Reverse Polish Notation
  • Sqrt Decomposition
Powered by GitBook
On this page
  • Comparison of Kruskal's Algorithm and Prim's Algorithm
  • Problems

Was this helpful?

  1. Graph

Minimum Spanning Tree

PreviousGraphNextKruskal

Last updated 1 year ago

Was this helpful?

A minimum-spanning-tree is a tree (without cycles) connecting all the vertices and with the smallest cost.

Comparison of Kruskal's Algorithm and Prim's Algorithm

Prim's (O(ElogV)) is more performant on dense graph. Kruskal's (O(ElogE)) is more performant on sparse graph.

Aspect
Prim's Algorithm
Kruskal's Algorithm

Algorithm Type

Greedy

Greedy

Edge Selection

Chooses the minimum-weight edge connected to the current MST.

Sorts all edges by weight and adds them to the MST if they don't form a cycle.

Data Structures Used

Priority queue or min-heap (to efficiently select the minimum-weight edge).

Disjoint-set data structure (to check for cycles).

Complexity

O(V^2) using an adjacency matrix, O(E + V log V) using a binary heap for dense graphs.

O(E log E) to sort the edges, O(E log V) to process all edges and find MST using disjoint-set.

Suitability

More efficient for dense graphs (E is close to V^2).

Suitable for sparse graphs (E is much less than V^2).

Applications

Network design, clustering, road planning, maze generation, etc.

Network design, cluster analysis, image segmentation, etc.

Cycle Handling

Doesn't directly deal with cycles; relies on the connectedness of the graph.

Detects and avoids cycles explicitly.

Output

Yields a single MST rooted at a selected vertex.

Yields a forest of MSTs initially; they combine into a single MST as more edges are added.

Problems

  • Dense Graph. Prim is better.

1584. Min Cost to Connect All Points (Medium)
1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree (Hard)