Build a small example graph as an adjacency list, adjacency matrix, or both, comparing memory usage and lookup cost between representations.
You are a data structures instructor who treats a graph as a map of cities connected by roads, not as an abstract set of nodes and edges, because a student who can picture cities and roads can reason about a graph's shape long before they can read formal graph notation. My representation is [REPRESENTATION:select:adjacency list,adjacency matrix,both compared side by side], my graph type is [GRAPH_TYPE:select:undirected and unweighted,directed,weighted], and my code example should be in [LANGUAGE:select:Python,JavaScript,Java,C++]. Build a small example graph with five or six nodes and roughly six to eight connections, described first as cities and roads in plain language, one or two sentences naming which cities connect to which, matching [GRAPH_TYPE], a road with no arrow if undirected, a one-way road if directed, and a distance or cost labeled on each road if weighted. Then represent that exact graph in code using [REPRESENTATION] in [LANGUAGE], with comments in plain language showing what each part of the structure holds. If I chose both compared side by side, build the same graph both ways so the two representations sit next to each other rather than being described in two disconnected examples. Explain what checking whether two specific cities are directly connected costs in each representation you built, for an adjacency matrix, checking one cell in the grid, close to instant regardless of the graph's size, for an adjacency list, scanning through that one city's own short list of connections, proportional to how many roads that specific city has rather than the whole map. Then explain what the representation costs to store as the graph grows, an adjacency matrix using a cell for every possible pair of cities whether a road exists there or not, growing fast as more cities are added, an adjacency list only storing the roads that actually exist, so a sparse map with few roads per city stays compact while a matrix does not. Close by naming one realistic situation where a sparse, mostly-unconnected graph would make adjacency list the clear choice, and one where a dense, heavily-connected graph would make adjacency matrix the clear choice, tied to the actual node and edge counts involved rather than a generic rule of thumb. If I ask how to actually visit every city reachable from a starting point using this exact graph, point me toward a dedicated algorithm tool for tracing breadth-first or depth-first search step by step instead of walking through a full traversal here, since this tool is built for understanding how the graph itself is stored, not for tracing a search algorithm running on top of it.
Use this prompt anywhere
10,000+ expert prompts for ChatGPT, Claude, Gemini, and wherever you use AI.
Get Early AccessA graph is a map of cities connected by roads, and most confusion about graphs comes from skipping straight to the abstract node-and-edge vocabulary before that picture ever gets built. Once a student can name which cities connect to which, the two ways to actually store that map in code stop being arbitrary syntax to memorize.
This tool builds a small five or six city example graph matching your chosen [GRAPH_TYPE], undirected, directed, or weighted, then represents it in code using [REPRESENTATION], an adjacency list, an adjacency matrix, or both side by side, in your chosen [LANGUAGE]. It explains what checking a connection costs in each form, close to instant for a matrix, proportional to one city's own road count for a list, and what storing the map costs as cities get added, where sparse favors a list and dense favors a matrix, tied to the actual example built.
Run it in the Dock Editor to build a reference of representations studied, pair with the data structure explainer for the structures that came before a graph, then move to the algorithm explainer with practice problem for a traversal traced step by step on top of the graph built here.
In the Dock Editor, or in ChatGPT, Claude, or Gemini, set [REPRESENTATION] to adjacency list, adjacency matrix, or both compared side by side.
Set [GRAPH_TYPE] to undirected and unweighted, directed, or weighted, matching the kind of connections your example graph should have.
Set [LANGUAGE] to Python, JavaScript, Java, or C++ for the commented code example.
Get a small five or six city graph described in plain language first, then represented in code exactly matching your chosen representation.
See what checking a specific connection costs and what storing the whole graph costs, tied to the exact example built rather than stated as an abstract rule.
Get the two standard ways to represent a graph built from the same small example, so the choice between them is a real comparison instead of two disconnected definitions.
Decide whether your actual data, a sparse social network or a dense small routing map, fits an adjacency list or an adjacency matrix better before writing any code.
Review adjacency list and adjacency matrix side by side on the identical example graph, since exams often ask for exactly this comparison.
Get the graph itself represented correctly first, then move to a dedicated algorithm tool for tracing the traversal that runs on top of it.
An adjacency list stores, for each node, only the connections that actually exist, keeping a sparse graph compact. An adjacency matrix stores a cell for every possible pair of nodes whether a connection exists there or not, which grows fast as more nodes are added but makes checking one specific connection close to instant.
An adjacency list, since a sparse graph has few connections per node relative to the total number of nodes, and a list only stores the connections that actually exist instead of a cell for every possible pair, keeping memory use proportional to actual connections rather than to the square of the node count.
An adjacency matrix, since a dense graph has many connections per node, and checking whether two specific nodes connect becomes close to instant, one cell in the grid, rather than scanning through a long list of that node's connections one at a time to find the answer.
No, it stops at building and representing the graph itself. Once the graph exists in either form, a dedicated algorithm tool built specifically for tracing breadth-first or depth-first search step by step covers the actual traversal, since that is a separate skill from choosing a representation.
Yes. Set [GRAPH_TYPE] to weighted, and the example graph gets built with a distance or cost labeled on each connection, represented in code as part of either the adjacency list or adjacency matrix depending on which one you chose, with each stored weight explained in a plain comment.
A directed graph's connections only go one way, so the code representation only records that direction rather than assuming the reverse connection exists automatically. This tool builds the example graph and its code to match, with one-way roads described as such before any code appears.
Discover more prompts that could help with your workflow.
Build a small working program that calls a real public API, with the request, response, and API key setup explained step by step.
Explain a core functional programming idea, such as pure functions or immutability, with a broken code example and its fixed version shown side by side.
Explain a built-in math function, square root, power, absolute value, or rounding, covering math and syntax, then generate practice calls to predict before revealing results.
10,000+ expert-curated prompts for ChatGPT, Claude, Gemini, and wherever you use AI. Our extension helps any prompt deliver better results.