Graphs

Nodes and edges modeling networks, dependencies, and paths.

What is a graph?

A graph is a set of vertices (nodes) joined by edges (links). Unlike an array’s straight row or a tree’s single parent rule, any vertex can connect to any other — cities on a map, accounts that follow each other, packages that depend on libraries. The shape is the relationships.

Vertex
A node — a person, city, task, or cell you care about
Edge
A link between two vertices — friendship, road, or “must run before”
Directed / Undirected
Arrows point one way, or a link works both ways
Degree
How many edges touch a vertex (in-degree, out-degree, or both)
Path
A walk from one vertex to another along edges
Cycle
A path that returns to the vertex where it started

See it as nodes and edges

Picture five friends. An edge means “they know each other.” Draw the links one at a time, then walk the graph — each hop lights the next person.

Nothing sits in a fixed slot. The structure is the connections. Once edges exist, you can ask “who is next to A?” or “can I reach E from A?”

Five vertices · undirected edges
Focusdraw edges

Edges appear first. Then a visit pulse walks A → B → E → D → C.

Types of graphs

Interviews usually classify graphs three ways: do edges have a direction, do they carry a weight, and is the graph dense enough that a matrix beats a list for storage.

Directed edges are one-way streets. Weighted edges carry a cost. Dense graphs have many edges relative to vertices — sparse ones have few.

How graphs are classified
FocusGraph

Direction, weight, and density decide how you store and walk the graph.

How it is stored in memory

Two common layouts. An adjacency list keeps a neighbor array per vertex — great when edges are scarce. An adjacency matrix is an n×n grid; cell i,j is 1 if an edge exists — great when you need O(1) “is there an edge?” checks.

Tap Next to add the same undirected edges to both views and watch them stay in sync.

List vs matrix · same three edges
StatusPress Next to add A—B

Both structures describe the same undirected triangle.

Step 1 of 4

Operations

Everyday moves on a small undirected graph. Watch vertices and edges light up. For copy-paste TypeScript, open the Functions tab.

Add vertex & edge

Register a new vertex with an empty neighbor list, then connect it. Undirected edges update both ends.

Grow the graph
StatusA—B ready

Add C, then link B—C both ways.

Neighbors

Ask who sits one hop from a vertex. In a list that is one array read; the demo pulses every neighbor of the focus node.

Who is next to A?
Neighborsof A → B, C, D

A’s list is [B, C, D]. E is two hops away.

BFS walk

Use a queue. Visit everything one hop away before anything two hops away — ring by ring from the start.

Breadth-first from A
Queue walkA · then B, C, D · then E

Level 0: A. Level 1: B, C, D. Level 2: E.

DFS walk

Use a stack (or recursion). Dive deep along one path before backtracking to try the next branch.

Depth-first from A
Stack walkA → B → E → D · then C

Dive A→B→E, backtrack through D, then take the leftover branch to C.