Graph and Sorting Algorithm Visualiser
A Python/PyQt5 desktop application that animates graph and sorting algorithms step by step while keeping a graph's drawing, adjacency matrix and adjacency list in sync.
- Period
- Autumn 2025
- Context
- Data Structures course design
- Team
- Individual project
- My role
- Sole developer
- Status
- Completed course project (course grade 91/100)
My contribution
- Designed an MVC structure in which one
GraphModelholds the vertices, edges, adjacency matrix and adjacency list. The graph canvas, the matrix view and the list view all render from it. - Implemented step-by-step visualisation of DFS, BFS, Dijkstra, Prim and Kruskal, plus seven sorting algorithms, for directed or undirected and weighted or unweighted graphs.
- Wrote each algorithm as a Python generator that yields one step at a time, with a
QTimerdriving the animation, so the algorithm code contains no drawing code. - Designed a small graph DSL with
.gvizsave and load, and connected an LLM API that turns a plain-language description into that DSL.
How it works
The program keeps three concerns apart. The model is a single GraphModel that owns the graph data in every representation the views need. The views (a drawing of the graph, an adjacency matrix and an adjacency list) only render what the model holds, so they cannot disagree with each other. The algorithms read from the model and report their progress one step at a time.
Graphs can come from three places: text written in a small DSL, a saved .gviz file, or a plain-language description that an LLM converts into the DSL. All three paths end in the same model, so the rest of the program does not need to know where a graph came from.
A design decision: keeping algorithm steps out of the UI
An animation needs the window to redraw between algorithm steps. If an algorithm ran as an ordinary loop and paused between steps, it would block Qt’s event loop: the window would stop repainting and responding until the algorithm finished. Putting drawing calls inside each algorithm would also tie every algorithm to one particular view.
Instead, each algorithm is a generator. It does its normal work and yields a small record of the step it has just taken. A QTimer asks for the next step on each tick, and the views highlight it. The timer, not the algorithm, decides when the next step happens, so the window stays responsive and the algorithm code stays free of interface code.
The pattern, as a simplified illustration rather than the original source:
from collections import deque
def bfs_steps(graph, start):
visited, queue = {start}, deque([start])
while queue:
u = queue.popleft()
yield ("visit", u)
for v in graph.neighbours(u):
if v not in visited:
visited.add(v)
queue.append(v)
yield ("discover", u, v)
# In the controller: one step per timer tick.
def advance(self):
try:
step = next(self.steps)
except StopIteration:
self.timer.stop()
return
self.views.highlight(step)
The cost of this design is that every algorithm must yield at meaningful points and use a step format the views understand. In return, adding an algorithm does not require touching the views, and adding a view does not require touching the algorithms.
Results & validation
- 21 functional and boundary test cases are recorded in the course report.
- The course design received 91/100.
The test count comes from the original course report (autumn 2025). The tests were not re-run for this website.
Limitations & next steps
- The application is desktop-only. A planned web adaptation will start with BFS, Dijkstra and one sorting algorithm, with step, play, pause, reset and speed controls, and will be labelled as an adaptation of the original program.
- Dijkstra's algorithm is only correct for non-negative edge weights, so the web version will reject negative weights at input. Where several shortest paths tie, more than one visiting order is valid, so results will be compared with the original on fixed sample graphs rather than by exact order alone.
- LLM-assisted generation needs an external API and a network connection. It is one input path alongside the DSL and saved
.gvizfiles, not a requirement for using the tool.
Run / reproduce
Environment
- Python 3.11 with PyQt5 (desktop application)
- An LLM API key only for the plain-language graph generation feature