Shortest Path Between Two Nodes Visualizer – Python Desktop App (With Full Source Code)

 

Shortest Path Between Two Nodes Visualizer – Python Desktop App (With Full Source Code)

Understanding shortest path algorithms is an essential skill in data science, machine learning, computer networks, and competitive programming.
To make learning easier, I created a Python Desktop Application that visually shows the Shortest Path Between Two Nodes using Dijkstra’s Algorithm.

This blog contains:

  • ✔ What the app does

  • ✔ Features of the tool

  • ✔ Full Python source code

  • ✔ How to run it on your laptop

  • ✔ Screenshots (you can add your own)

Let’s begin!


🎯 What This Desktop App Does

This Python application:

  • Allows the user to create nodes and connect them with weighted edges

  • Lets the user select a start and end node

  • Runs Dijkstra’s algorithm

  • Shows the shortest path visually on the screen

  • Displays the total path cost

This is a perfect tool for:

  • Students learning algorithms

  • Teachers demonstrating graph concepts

  • Beginners preparing for coding interviews

  • Anyone who wants a visual explanation of shortest path algorithms


🧠 Algorithms Used

1. Graph Data Structure

The app internally stores:

  • Nodes

  • Edges

  • Weights

2. Dijkstra’s Algorithm

The algorithm finds:

The minimum-cost path between a start node and destination node.


🖥 Technology Used

  • Python

  • Tkinter (for GUI)

  • Heapq (for Dijkstra)

  • Canvas drawing for graph visualization


🧩 Complete Python Code – Shortest Path Visualizer

Copy–paste this code into Visual Studio Code, save it as:

shortest_path_app.py

Then run it normally.

import tkinter as tk from tkinter import messagebox import heapq class GraphVisualizer: def __init__(self, root): self.root = root self.root.title("Shortest Path Visualizer (Dijkstra)") self.canvas = tk.Canvas(root, width=700, height=500, bg="white") self.canvas.pack() self.nodes = {} self.edges = [] self.node_counter = 0 self.canvas.bind("<Button-1>", self.add_node) self.canvas.bind("<Button-3>", self.add_edge) controls = tk.Frame(root) controls.pack() tk.Label(controls, text="Start Node:").grid(row=0, column=0) tk.Label(controls, text="End Node:").grid(row=0, column=2) self.start_entry = tk.Entry(controls, width=5) self.end_entry = tk.Entry(controls, width=5) self.start_entry.grid(row=0, column=1) self.end_entry.grid(row=0, column=3) tk.Button(controls, text="Find Shortest Path", command=self.run_dijkstra).grid(row=1, column=0, columnspan=4) def add_node(self, event): x, y = event.x, event.y node_id = self.node_counter self.nodes[node_id] = (x, y) self.node_counter += 1 self.canvas.create_oval(x - 15, y - 15, x + 15, y + 15, fill="lightblue") self.canvas.create_text(x, y, text=str(node_id)) def add_edge(self, event): if len(self.nodes) < 2: return # connect last 2 nodes n1 = self.node_counter - 2 n2 = self.node_counter - 1 x1, y1 = self.nodes[n1] x2, y2 = self.nodes[n2] weight = ((x1 - x2)**2 + (y1 - y2)**2) ** 0.5 weight = round(weight, 1) self.edges.append((n1, n2, weight)) self.canvas.create_line(x1, y1, x2, y2) self.canvas.create_text((x1 + x2)//2, (y1 + y2)//2, text=str(weight)) def run_dijkstra(self): try: start = int(self.start_entry.get()) end = int(self.end_entry.get()) except: messagebox.showerror("Error", "Invalid node numbers") return graph = {n: [] for n in self.nodes} for u, v, w in self.edges: graph[u].append((v, w)) graph[v].append((u, w)) dist = {n: float('inf') for n in self.nodes} dist[start] = 0 pq = [(0, start)] parent = {start: None} while pq: d, node = heapq.heappop(pq) if node == end: break for neighbor, weight in graph[node]: new_cost = d + weight if new_cost < dist[neighbor]: dist[neighbor] = new_cost parent[neighbor] = node heapq.heappush(pq, (new_cost, neighbor)) # highlight shortest path path = [] curr = end while curr is not None: path.append(curr) curr = parent.get(curr) path.reverse() for i in range(len(path) - 1): x1, y1 = self.nodes[path[i]] x2, y2 = self.nodes[path[i+1]] self.canvas.create_line(x1, y1, x2, y2, width=4, fill="red") messagebox.showinfo("Result", f"Shortest path: {path}\nTotal Cost: {dist[end]}") root = tk.Tk() app = GraphVisualizer(root) root.mainloop()

🚀 How to Run This App on Your Laptop

1. Install Python

Download from: https://www.python.org/

2. Save the code

Save as:

shortest_path_app.py

3. Run it in VS Code

Open terminal:

python shortest_path_app.py

The app will open as a desktop window.


📸 Suggested Screenshots for Your Blog

You can insert these after running the app:

  • Screenshot of nodes added

  • Screenshot of edges

  • Screenshot of shortest path highlighted in red

  • Screenshot of result popup

Screenshots increase trust and SEO quality.


🏆 Final Thoughts

This Shortest Path Visualizer Desktop App is a powerful tool for anyone learning graph algorithms.
It helps you:

  • Understand how shortest paths work

  • Visually interact with graphs

  • Build intuition for Dijkstra’s logic

  • https://github.com/gagandeep44489/DiscreteStrucutreAndAlgoApp/blob/main/Shortest%20Path%20Between%20Two%20Nodes%20Visualizer.py

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