Minimum Spanning Tree Desktop App — Kruskal & Prim (Python / Tkinter)

Minimum Spanning Tree (MST) Desktop App in Python (Kruskal & Prim Algorithms)

Understanding graph algorithms becomes much easier when you can visualize them. One of the most important concepts in graph theory is the Minimum Spanning Tree (MST). It forms the backbone of network design, clustering, data compression, and many optimization problems. In this blog, we explore a Python Tkinter Desktop App that allows you to generate MSTs using both Kruskal’s Algorithm and Prim’s Algorithm.

This desktop tool is perfect for students, teachers, and interview candidates who want to understand MSTs deeply and interactively.


What Is a Minimum Spanning Tree (MST)?

A Minimum Spanning Tree of a weighted graph is a subset of edges that:

  1. Connects all the vertices

  2. Has no cycles

  3. Has the minimum total weight

MSTs are used everywhere:

  • Network design (LAN, WAN, telecom)

  • Power grid optimization

  • Transportation planning

  • Image segmentation

  • Clustering algorithms

  • Minimum cost infrastructure design

To compute the MST, there are two famous algorithms:

  • Kruskal’s Algorithm – edge-based

  • Prim’s Algorithm – node-based

This desktop app supports both.


Why Build a Desktop GUI App for MST?

Most tutorials show code or mathematical steps, but very few show how MST algorithms actually behave step-by-step.
A GUI application helps learners:

  • Add edges manually

  • Visualize edge sorting (Kruskal)

  • Watch incremental tree building

  • Compare Kruskal and Prim approaches

  • See how cycles are avoided

  • Understand priority queues and Union-Find

  • Get a practical feel of MST generation

The Tkinter library makes it possible to build this with zero external dependencies.


Features of the MST Desktop App

This simulation app includes the following features:

1. Add Weighted Edges

Users can enter edges using simple input:

A B 3 A C 5 B D 2

Edges automatically appear in the graph list.

2. Choose Algorithm

You can choose between:

  • Kruskal’s MST

  • Prim’s MST

3. Step-by-Step Visualization

The app shows:

  • Which edges are being considered

  • Whether the edge is added to the MST

  • Cycle detection (for Kruskal)

  • Priority queue updates (for Prim)

4. View the Final MST

After completing the algorithm:

  • All MST edges are displayed

  • Total Minimum Weight is shown

  • Every processing step is logged

5. Fully GUI-Based

No command-line typing required.
Easy to understand for beginners.


Full Python Code for MST Desktop App (Kruskal + Prim)

Below is the complete ready-to-run Tkinter desktop application:

import tkinter as tk from tkinter import scrolledtext, messagebox import heapq # ------------------ KRUSKAL ALGORITHM ------------------ # class UnionFind: def __init__(self, nodes): self.parent = {n: n for n in nodes} def find(self, node): if self.parent[node] != node: self.parent[node] = self.find(self.parent[node]) return self.parent[node] def union(self, a, b): rootA = self.find(a) rootB = self.find(b) if rootA != rootB: self.parent[rootB] = rootA return True return False def kruskal(graph): edges = [] for u in graph: for v, w in graph[u].items(): if (v, u, w) not in edges: edges.append((u, v, w)) edges.sort(key=lambda x: x[2]) steps = [] mst = [] nodes = set(graph.keys()) uf = UnionFind(nodes) for u, v, w in edges: steps.append(f"Checking edge {u}-{v} (weight {w})") if uf.union(u, v): mst.append((u, v, w)) steps.append(f" Added to MST") else: steps.append(f" Forms cycle → Skipped") return mst, steps # ------------------ PRIM ALGORITHM ------------------ # def prim(graph, start): visited = set() pq = [] mst = [] steps = [] for v, w in graph[start].items(): heapq.heappush(pq, (w, start, v)) visited.add(start) steps.append(f"Start Prim at {start}") while pq: w, u, v = heapq.heappop(pq) steps.append(f"Checking edge {u}-{v} (weight {w})") if v not in visited: visited.add(v) mst.append((u, v, w)) steps.append(f" Added to MST") for nxt, wt in graph[v].items(): if nxt not in visited: heapq.heappush(pq, (wt, v, nxt)) return mst, steps # ------------------ GUI APP ------------------ # class MSTApp: def __init__(self, root): self.root = root root.title("MST Algorithms: Kruskal & Prim") root.geometry("750x650") self.graph = {} tk.Label(root, text="Enter Edge (Format: A B 5):").pack() self.edge_entry = tk.Entry(root, width=40) self.edge_entry.pack() tk.Button(root, text="Add Edge", command=self.add_edge).pack() tk.Label(root, text="Graph Edges:").pack() self.edge_box = scrolledtext.ScrolledText(root, width=50, height=7) self.edge_box.pack() tk.Label(root, text="Start Node for Prim:").pack() self.start_entry = tk.Entry(root, width=10) self.start_entry.pack() tk.Button(root, text="Run Kruskal MST", command=self.run_kruskal).pack() tk.Button(root, text="Run Prim MST", command=self.run_prim).pack() tk.Label(root, text="Steps:").pack() self.steps_box = scrolledtext.ScrolledText(root, width=80, height=20) self.steps_box.pack() tk.Label(root, text="MST Result:").pack() self.result_box = scrolledtext.ScrolledText(root, width=40, height=6) self.result_box.pack() def add_edge(self): text = self.edge_entry.get().strip() try: u, v, w = text.split() w = int(w) if u not in self.graph: self.graph[u] = {} if v not in self.graph: self.graph[v] = {} self.graph[u][v] = w self.graph[v][u] = w self.edge_box.insert(tk.END, f"{u} -- {v} (weight {w})\n") self.edge_entry.delete(0, tk.END) except: messagebox.showerror("Error", "Enter edge in the format: A B 5") def run_kruskal(self): mst, steps = kruskal(self.graph) self.display_results(mst, steps) def run_prim(self): start = self.start_entry.get().strip() if start not in self.graph: messagebox.showerror("Error", "Start node not found!") return mst, steps = prim(self.graph, start) self.display_results(mst, steps) def display_results(self, mst, steps): self.steps_box.delete(1.0, tk.END) self.result_box.delete(1.0, tk.END) for s in steps: self.steps_box.insert(tk.END, s + "\n") total_weight = 0 for u, v, w in mst: self.result_box.insert(tk.END, f"{u}-{v} (weight {w})\n") total_weight += w self.result_box.insert(tk.END, f"\nTotal Weight: {total_weight}\n") if __name__ == "__main__": root = tk.Tk() app = MSTApp(root) root.mainloop()

How to Use This App

  1. Install Python on your system.

  2. Create a file named mst_app.py.

  3. Paste the full code into the file.

  4. Run it using the command:

python mst_app.py

The desktop window will appear, and you can start adding edges and running MST algorithms immediately.


Final Thoughts

This MST Desktop App is a powerful learning tool for anyone exploring graph algorithms. By visually watching Kruskal and Prim in action, you gain a deeper understanding of how MSTs are formed. The step-by-step breakdown makes it perfect for:

  • Classroom demonstrations

  • Coding interview preparation

  • Student learning projects

  • Teaching assistants and educators

  • Graph theory enthusiasts

If you want blog articles for BFS/DFS Visualization, A* Pathfinding, Dijkstra, or Graph Drawing apps, I can write those as well—just ask!

Comments

Popular posts from this blog

NAND / NOR Logic Simulator: A Python Desktop App for Understanding Universal Logic Gates

Subset Sum Problem Visualizer Using Python (Dynamic Programming GUI Tool)

String Matching Algorithm Trainer (KMP & Rabin-Karp) – Python Desktop App