Dijkstra’s Algorithm Simulator Desktop App in Python (Tkinter GUI)

 

Dijkstra’s Algorithm Simulator Desktop App in Python (Tkinter GUI)

If you are learning algorithms, preparing for coding interviews, or teaching data structures, a visual and interactive tool can make a big difference. In this blog, we will build a Dijkstra’s Algorithm Desktop Application in Python using Tkinter, the built-in GUI library.

This app allows you to add graph edges, choose a start node, and run Dijkstra's Algorithm with a step-by-step explanation of how the shortest paths are calculated.

Let’s explore how it works and how you can use it in your projects, classrooms, or personal learning.


What Is Dijkstra’s Algorithm?

Dijkstra’s Algorithm is a famous shortest-path algorithm used to find the minimum distance from a starting node to all other nodes in a weighted graph.

It is widely used in:

  • Google Maps & GPS route finding

  • Network routing protocols

  • AI and game development pathfinding

  • Logistics & supply chain optimization

This algorithm works only on graphs with non-negative weights.


🧑‍💻 Why a Desktop App?

While many students read about Dijkstra’s Algorithm, very few actually visualize how it runs step-by-step.
This desktop app helps you:

  • Add edges dynamically

  • Visualize the algorithm’s steps clearly

  • Understand how distance updates happen

  • Avoid manually tracing graph paths

  • Experiment with custom graphs instantly

Since this is a Tkinter desktop GUI, you can run it on any system without installing extra frameworks.


🖥️ Features of the Dijkstra Desktop App

This Python Tkinter application includes:

✔ Add edges through input

You can add graph connections using the format:

A B 4 B C 2 A C 7

✔ See the full list of graph edges

All added edges appear in a scrollable area.

✔ Select the start node

Just type the starting point (like A, B, C).

✔ Run Dijkstra Algorithm

The app displays:

  • Each node visited

  • Each neighbor checked

  • Whether the distance was updated

  • Current shortest path decisions

✔ Final shortest distances

At the bottom, the app shows the shortest distance of each node from the starting point.


🧾 Full Python Code (Tkinter GUI)

You can copy and run this code directly in Python:

import tkinter as tk from tkinter import messagebox, scrolledtext import heapq def dijkstra(graph, start): distances = {node: float("inf") for node in graph} distances[start] = 0 pq = [(0, start)] steps = [] while pq: current_distance, current_node = heapq.heappop(pq) if current_distance > distances[current_node]: continue steps.append(f"Visiting: {current_node} (distance = {current_distance})") for neighbor, weight in graph[current_node].items(): steps.append(f" Checking {neighbor} with weight {weight}") distance = current_distance + weight if distance < distances[neighbor]: steps.append(f" Updated {neighbor}: {distances[neighbor]}{distance}") distances[neighbor] = distance heapq.heappush(pq, (distance, neighbor)) else: steps.append(f" No update needed") return distances, steps class DijkstraApp: def __init__(self, root): self.root = root root.title("Dijkstra Algorithm Simulator") root.geometry("700x600") tk.Label(root, text="Enter edges (Format: A B 4)").pack() self.edge_entry = tk.Entry(root, width=40) self.edge_entry.pack() self.add_button = tk.Button(root, text="Add Edge", command=self.add_edge) self.add_button.pack() tk.Label(root, text="Graph Edges:").pack() self.edge_list_box = scrolledtext.ScrolledText(root, width=50, height=7) self.edge_list_box.pack() tk.Label(root, text="Start Node:").pack() self.start_entry = tk.Entry(root, width=10) self.start_entry.pack() tk.Button(root, text="Run Dijkstra", command=self.run_dijkstra).pack() tk.Label(root, text="Steps:").pack() self.steps_box = scrolledtext.ScrolledText(root, width=80, height=15) self.steps_box.pack() tk.Label(root, text="Shortest Distances:").pack() self.result_box = scrolledtext.ScrolledText(root, width=40, height=7) self.result_box.pack() self.graph = {} 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_list_box.insert(tk.END, f"{u} -- {v} (weight {w})\n") self.edge_entry.delete(0, tk.END) except: messagebox.showerror("Error", "Enter edge in correct format: A B 5") def run_dijkstra(self): start = self.start_entry.get().strip() if start not in self.graph: messagebox.showerror("Error", "Start node not found in graph!") return distances, steps = dijkstra(self.graph, start) 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") for node, dist in distances.items(): self.result_box.insert(tk.END, f"{node}: {dist}\n") if __name__ == "__main__": root = tk.Tk() app = DijkstraApp(root) root.mainloop()

🚀 How to Run This Desktop App

Follow these steps:

  1. Install Python (if not installed).

  2. Create a file named:

    dijkstra_gui.py
  3. Copy the above code into the file.

  4. Run the script:

    python dijkstra_gui.py

Your desktop app will open instantly!


🎯 Want to Make It an EXE App?

If you want this to run as a Windows .exe application, you can convert it using:

pyinstaller --noconsole --onefile dijkstra_gui.py

I can also generate a full tutorial for EXE conversion — just ask!


🏁 Final Thoughts

This Dijkstra Desktop Simulator is a powerful educational tool for students, teachers, and developers. Whether you're learning algorithms or preparing coding tutorials, this visual app helps you understand shortest-path calculations easily.

If you want more apps like:

  • BFS/DFS Visualizer

  • Minimum Spanning Tree Simulator

  • A* Pathfinding Visualizer

  • Graph Drawing GUI

Just comment and I’ll generate them!

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