04. Graphs, Dijkstra, and Path Animation - Part 1/2
In this lesson, we transform the map from a simple graphical representation into a real travel network:
- buildings become nodes in a graph,
- pedestrian paths become weighted edges, and
- the browser automatically calculates the shortest route between two points using Dijkstra’s algorithm.
Introduction
Section titled “Introduction”Use Case Diagram
Section titled “Use Case Diagram”Diagram created with Mermaid
Project file structure
Section titled “Project file structure”At the end of this lesson, the project folder will contain these files in its root directory:
Directoryinteractive campus map project/
- dijkstra.js JavaScript module containing Dijkstra’s algorithm for calculating the shortest path[cite: 2]
- grafo.json Graph data structure containing the nodes and weighted path edges[cite: 2]
- index.html Main HTML page containing the map and selection controls[cite: 2]
- locali.json List of buildings, used to dynamically populate the selection menus[cite: 2]
- mappa.svg Vector map graphic loaded into the page[cite: 2]
- script.js Main logic that loads files with
fetch(), handles events, and draws the path in the DOM[cite: 2] - style.css Stylesheet for the page layout, building highlights, and path animation[cite: 2]
Sequence Diagram
Section titled “Sequence Diagram”Diagram created with Mermaid
Component Diagram
Section titled “Component Diagram”Diagram created with Mermaid
Create the lezione-4 Working Branch
Section titled “Create the lezione-4 Working Branch”Before integrating the graph structure and Dijkstra’s algorithm for calculating shortest paths, create a dedicated branch to isolate this lesson’s changes:
- Open the Source Control panel in VS Code (
Ctrl + Shift + G). - Click the current branch name in the lower-left corner (or at the top of the panel) and select
Create new branch.... - Enter the new branch name:
lezione-4, then press Enter.
Add a Road Junction Layer to mappa.svg
Section titled “Add a Road Junction Layer to mappa.svg”To make routes follow the roads and avoid cutting across lawns “as the crow flies,” we need to define points (junctions) along the pedestrian paths, at:
-
path intersections:
Section titled “path intersections:”
-
orthogonal branches from paths to buildings:
Section titled “orthogonal branches from paths to buildings:”
Meaningful IDs for paths
Section titled “Meaningful IDs for paths”To make it easier to position junctions, add meaningful id values to the paths in mappa.svg:
<!-- LAYER 2: PEDESTRIAN PATHS --> <g id="layer-percorsi"> <rect x="50" y="270" width="700" height="60" fill="#d1d5db" rx="4" /> <rect id="percorso-orizzontale" x="50" y="270" width="700" height="60" fill="#d1d5db" rx="4" />
<rect x="180" y="80" width="50" height="440" fill="#d1d5db" rx="4" /> <rect id="percorso-est" x="570" y="80" width="50" height="440" fill="#d1d5db" rx="4" />
<rect x="180" y="80" width="50" height="440" fill="#d1d5db" rx="4" /> <rect id="percorso-ovest" x="180" y="80" width="50" height="440" fill="#d1d5db" rx="4" /> </g>The junction layer
Section titled “The junction layer”Now add the junction layer at the end of mappa.svg, positioning the first access points orthogonal to the buildings and the main intersections:
<!-- EXISTING LAYERS -->
<!-- NEW ROAD AND ACCESS JUNCTION LAYER --> <g id="layer-snodi"> <!-- Access junctions orthogonal to the buildings --> <circle id="snodo-uffici" cx="205" cy="150" r="4" fill="#dc2626" /> <circle id="snodo-aule" cx="205" cy="430" r="4" fill="#dc2626" /> <circle id="snodo-mensa" cx="310" cy="300" r="4" fill="#dc2626" />
<!-- Junctions at the main road intersections --> <circle id="snodo-incrocio-ovest" cx="205" cy="300" r="4" fill="#0284c7" /> <circle id="snodo-incrocio-est" cx="595" cy="300" r="4" fill="#0284c7" /> </g>
</svg>Junction visibility on the map
Section titled “Junction visibility on the map”To make positioning and visual calibration easier, add this rule to style.css:
/* In style.css: comment out or uncomment display to show/hide junctions */#layer-snodi {/* display: none; */}Define the Graph with Junctions (grafo.json)
Section titled “Define the Graph with Junctions (grafo.json)”Now that we have positioned the road junctions in mappa.svg, we need to update grafo.json. Connections will no longer link buildings directly; they will pass through junction nodes.
Here is a valid grafo.json snippet modeling the route from the offices to the intersections via junctions:
{ "edificio-uffici": { "snodo-uffici": 30 }, "snodo-uffici": { "edificio-uffici": 30, "snodo-incrocio-ovest": 100 }, "snodo-incrocio-ovest": { "snodo-uffici": 100, "snodo-aule": 100, "snodo-mensa": 100 }}How is grafo.json structured?
Section titled “How is grafo.json structured?”This file represents the campus road and connection graph using a nested JSON object (an adjacency map or table):
-
Top-level keys (nodes): Each top-level key (such as
"edificio-uffici"or"snodo-incrocio-ovest") represents a reference point on the map, whether a building or a strategic intersection. -
Nested objects (edges and weights): Each node contains another object listing its directly reachable neighbors and their numerical weights (the distance in meters or travel cost) required to move from one point to another.
-
Bidirectionality: Notice that connections are mirrored (e.g. from
edificio-ufficitosnodo-ufficiand back), reflecting the undirected graph structure in which roads can be traveled in either direction.
Final Commit and Sync (Git / GitHub)
Section titled “Final Commit and Sync (Git / GitHub)”- Check the files: Open the Source Control panel in VS Code and make sure all modified and created files (
grafo.json,mappa.svg,locali.json,style.css,script.js, andindex.html) are in the staging area. - Create the commit: Enter the commit message
"Completed Lesson 4: Graphs, Dijkstra, and Path Animation Part 1/2"and confirm. - Sync: Use Publish Branch or Sync Changes to push everything to GitHub.
Answered: 0 / 0 (0%)
STICS © 2026 by Carlo Scocco is licensed under CC BY-NC 4.0