Draft
Graph Basics
Model relationships with nodes, edges, and adjacency so search algorithms can move one local step at a time.
Hook problem
Imagine a building where rooms are connected by doors. You do not need the shape of every room yet. You need a model that answers one small question: from this room, which rooms can I reach in one step?
A graph model keeps the direct-connection facts and throws away floorplan details that do not matter yet.
First naive idea
One direct way to help visitors is to write route cards: “Lobby -> Hall -> Lab” or “Cafe -> Hall -> Lab.” For a few trips, that feels clear.
Base advice card
Base advice card
Base advice card
Route cards describe full trips, not reusable local facts.
Where it breaks
Now add one new door from Cafe to Lab. The building fact changed once, but several advice cards need review. Some advice remains valid, some is stale, and the new direct advice is missing.
Still valid.
Still reaches Lab, but now needs review.
Advice should become Cafe -> Lab.
New direct advice is missing.
Graph fact changed; advice cards now need review.
Route cards are human-maintained advice, not an algorithm. The pain is that full routes mix the stable structure with decisions about how to travel.
The core invention
A graph stores local relationship facts. A node represents one object, an edge represents one direct relationship, and adjacency records the immediate neighbors of a node.
A graph is a modeling choice, not an inevitable copy of the building.
Interactive visual demo
Neighbor inspector
Select a room to see only the one-step neighbors an algorithm can inspect next.
Selected: Hall
Formal version
A graph is often written as G = (V, E). V is the set of vertices or nodes, and E is the set of edges. In this page, Lobby is a node in V, and Lobby-Hall is an edge in E.
Notation records the same direct facts as the picture.
Edges can be undirected or directed. An undirected stored edge like ["lobby", "hall"] creates two neighbor facts: Lobby lists Hall, and Hall lists Lobby.
Implementation sketch
Storing complete route advice is brittle:
const routeAdvice = [["cafe", "hall", "lab"]];
An adjacency list stores local facts instead:
const adjacency = {
lobby: ["hall", "stairs"],
hall: ["lobby", "lab", "cafe"],
lab: ["hall"],
stairs: ["lobby", "cafe"],
cafe: ["hall", "stairs"]
};
[["cafe","hall","lab"], ...]cafe: ["hall", "stairs"]One stored undirected edge creates two neighbor facts.
Representation invariant
The representation is correct when every direct door appears according to the chosen convention. For an undirected graph, every door appears in both rooms’ adjacency rows.
Hall connects directly to Lobby, Lab, and Cafe.
Complexity and tradeoffs
An adjacency list is compact when each node has only a few neighbors. An adjacency matrix is a table lookup: choose a row, choose a column, and read whether a direct edge exists.
Representation comparison
Result: Choose an operation to compare the two representations.
Adjacency list
Adjacency matrix
| Lobby | Hall | Lab | Stairs | Cafe | |
|---|---|---|---|---|---|
| Lobby | 0 | 1 | 0 | 1 | 0 |
| Hall | 1 | 0 | 1 | 0 | 1 |
| Lab | 0 | 1 | 0 | 0 | 0 |
| Stairs | 1 | 0 | 0 | 0 | 1 |
| Cafe | 0 | 1 | 0 | 1 | 0 |
| Lobby | Hall | Lab | Stairs | Cafe | |
|---|---|---|---|---|---|
| Lobby | 0 | 1 | 0 | 1 | 0 |
| Hall | 1 | 0 | 1 | 0 | 1 |
| Lab | 0 | 1 | 0 | 0 | 0 |
| Stairs | 1 | 0 | 0 | 0 | 1 |
| Cafe | 0 | 1 | 0 | 1 | 0 |
Cell (Lab, Cafe) is 0 because there is no direct door.
Directed and weighted variants
A directed edge changes only one row. A weighted edge adds a cost label, but it does not change whether two nodes are adjacent.
Direction decides which row gets the neighbor.
Weight changes cost, not whether a room is adjacent.
Common confusions
An edge is one direct connection. A path chains edges. A neighbor is one edge away. A reachable node may need several edges.
Hall-Lab is one edge; Lobby -> Hall -> Lab is a path.
Lab is reachable from Lobby, but not adjacent to Lobby.
Lab -> Hall does not automatically allow Hall -> Lab.
Lobby-Hall cost 2 and Lobby-Stairs cost 5 are both one neighbor step.
The same fixture keeps the vocabulary grounded.
Connections in the graph
BFS repeats the graph-basics question: from the current node, which adjacent neighbors can be visited next? Dijkstra later keeps nodes, edges, and adjacency, then adds edge costs to choose the cheapest next frontier.
Exercises
- Which rooms are one-step neighbors of Lobby?
- If Cafe -> Lab is a one-way edge, which adjacency row changes?
- What is the matrix cell
(Lab, Cafe)in the base fixture? - Is
Lobby -> Hall -> Labone edge or a path?
Graph connections : Graph Basics