Built in 2019
Challenges
Graph Challenges
Challenges to understand graphs developed for the Artificial Intelligence club.
Made with
I was running the Artificial Intelligence club at my university, and I had a problem. Many students had never seen a data structure. Explaining graphs to them with words alone was frustrating.
Graphs sound abstract until you see one work. Networks, maps, social connections and game AI all use them. Reading about breadth first search is one thing. Watching it find a path through a maze is another.
So I built two interactive challenges for the club. Both run in the browser with pure JavaScript and HTML5 Canvas, a browser element for drawing. There are no frameworks and no dependencies, only the code and a visual playground.
Two ways to see graphs
The project has two separate challenges. Each one teaches a different side of graph algorithms.
The first challenge draws a graph as a network of connected nodes. You can drag the nodes around the canvas. The system finds the shortest path between any two nodes with a recursive search. When you pick a start and an end, the path lights up in red.
The second challenge is a maze pathfinder. You draw walls on a grid to create obstacles. The algorithm then finds a path from the start to the goal and marks each step. This shows how search algorithms work in a physical space.
Both challenges share the same drawing system. One shows the abstract structure of a graph. The other shows a practical use.
Cities in a matrix
The first challenge stores the graph in an adjacency matrix. That is a table where each row and each column is a node. A 1 means two nodes are connected. A 0 means they are not.
let cities = [
[0,1,0,0,0,0,0,0,0,0,1,0,0],
[1,0,1,0,0,0,0,0,0,0,0,1,0],
[0,1,0,1,0,0,0,0,0,0,0,0,0],
// ... more rows
];
The layout places the nodes in a circle using trigonometry. Each node sits at an angle calculated from its index. This keeps the screen clean and makes the connections easy to follow.
You can drag any node to a new place. The edges redraw on their own, so the picture stays accurate as the layout changes. This helps students see that a graph’s structure is separate from how it is drawn.
Trying every path
To find the shortest path between two nodes, the first challenge uses recursive depth first search with backtracking. Depth first search follows one route as far as it can before trying another. Backtracking means going back a step when a route leads nowhere.
A graph can have many paths between two points. The algorithm tries each neighbor, explores every option and keeps the shortest route it finds. If a neighbor leads nowhere, it goes back and tries another one.
The search marks a node as visited before it explores it, so it never walks in circles. When the recursive call returns, it unmarks the node. That lets the algorithm find all possible paths without getting stuck in loops.
When the search ends, the system colors the edges of the shortest path red. Students see exactly which connections form the best route.
A maze is a graph
The second challenge treats a 2D grid as a graph. Each cell is a node. Two cells next to each other share an edge unless a wall blocks them.
The maze stores its data differently. Instead of an adjacency matrix, it uses a 2D array where each value is a cell type:
0is an empty floor1is a wall2is the start position3is the end position
You can click and drag to draw or erase walls. The screen responds right away, so students can try many maze layouts.
Waves from the start
The maze solver uses wave propagation, a version of breadth first search. Breadth first search checks every cell at one distance before moving one step further.
It starts at the initial position and gives each neighboring cell a distance. Then it takes those neighbors and gives a distance to their unvisited neighbors. The search spreads outward like ripples in a pond. Each wave moves one step further from the start. When a wave reaches the goal, the algorithm stops.
To rebuild the path, it works backward from the goal. Each step follows smaller and smaller distance values back to the start. The final path shows up in pink on the grid.
This method always finds the shortest path in an unweighted graph. Every cell at distance 5 gets visited before any cell at distance 6. So the first path to reach the goal must be the shortest.
Shapes that know clicks
Both challenges draw on HTML5 Canvas. A shared utility provides basic shapes like rectangles and circles. Each shape knows how to draw itself and how to tell if a mouse click hit it.
The circle shape has this test:
contains(x, y) {
return this.x - this.radius <= x &&
this.x + this.radius >= x &&
this.y + this.radius >= y &&
this.y - this.radius <= y;
}
This simple bounding box check is what makes dragging nodes possible. A bounding box is the square around the circle.
Drawing happens in layers. First the edges are drawn between connected nodes. Then the nodes are drawn on top. This order stops edges from covering the node labels.
Click, drag, release
Mouse events drive everything. When you press the mouse button, the system checks if the click hit a node. If it did, it saves that node as the current selection.
When the mouse moves and there is a selection, the system updates the node’s position and redraws everything. When you release the button, it clears the selection.
The maze challenge uses the same pattern in a different way. A click toggles a wall instead of selecting a node. Dragging toggles every cell along the way. That makes it easy to draw long walls or clear large areas.
What students took away
The challenges did their job. Students could see the algorithms work instead of only reading about them. They could change the inputs and watch the outputs change.
The graph challenge showed that structure matters more than looks. Dragging nodes did not change which nodes connected to which. The adjacency matrix stayed the same.
The maze challenge made search algorithms easy to picture. Students watched the wave spread out and then saw the path traced back. That gave them a mental model they could use elsewhere.
Simple code on purpose
I kept the code simple on purpose. There were no build tools and no transpilation, which is converting code into another form before it runs. It is plain JavaScript that runs directly in the browser.
That helped the students. They could read the source code and change it. They could see exactly how everything worked without digging through a framework.
The Canvas API is simple too. Draw a rectangle. Draw a circle. Draw a line. Working at that low level made me think carefully about what I was drawing and why.
Seeing beats reading
These challenges taught me that interaction changes how people learn. A static diagram explains an idea. An interactive tool lets students explore it.
Limits helped too. Using only plain JavaScript meant I had to understand every line of code. Nothing was hidden inside a library function.
Graphs are networks, and search is exploration. Watching both move on a screen made abstract ideas concrete.
Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build