Graph Fundamentals
Learn the core language of nodes, edges, paths, and cycles. Graphs are the broad container that trees, BFS, DFS, Dijkstra, and MST all live inside.
Interactive visualization
Use the controls to connect the idea to concrete operations before diving into the full write-up.
A graph is the general container
Graphs model relationships. Once you can see nodes and edges clearly, traversal and shortest-path algorithms become natural.
Family
Graphs
Nodes, edges, traversal, connectivity, and shortest paths.
Builds on
Foundational
You can start here without another page first.
Unlocks
12 next topics
Use these follow-ups when you want to apply the current idea in a richer setting.
Learning paths
3
This topic appears in curated progressions so you can keep moving without guessing the next step.
Problem
Many problems are really about relationships rather than individual values: cities connected by roads, users linked by friendships, courses linked by prerequisites, files linked by dependencies.
You need a model that captures things and the connections between them. That model is a graph.
Intuition
A graph has:
- Vertices / nodes: the things
- Edges: the relationships between them
That’s it. Everything else is a refinement:
- Directed vs undirected edges
- Weighted vs unweighted edges
- Cyclic vs acyclic structure
- Connected vs disconnected components
This is why graph thinking is so broad: once a problem becomes “objects + links”, graph algorithms become available.
Core vocabulary
Path
A path is a sequence of edges connecting one node to another.
A -> C -> F
Once you can talk about paths, you can ask the big questions:
- Is a node reachable?
- What is the shortest path?
- Does a path respect dependencies?
Cycle
A cycle is a path that returns to where it started.
A -> B -> E -> C -> A
Cycles matter because they force you to track visited state and often make problems harder.
Connected component
A connected component is a maximal set of nodes where every node can reach every other node.
In an undirected graph, components answer: “how many separate islands are there?”
Degree
The degree of a node is how many edges touch it.
In directed graphs, this splits into:
- in-degree: edges coming in
- out-degree: edges going out
In-degree is the key idea behind Kahn’s algorithm for topological sort.
Representation
Adjacency list
A: [B, C]
B: [A, D, E]
C: [A, E, F]
Best when the graph is sparse. Most interview graphs are sparse, so adjacency lists are the default representation.
Adjacency matrix
A B C D
A [ 0 1 1 0 ]
B [ 1 0 0 1 ]
C [ 1 0 0 0 ]
D [ 0 1 0 0 ]
Best when the graph is dense or when you need constant-time edge existence checks. But it costs space.
Traversal is the universal primitive
Two traversals dominate graph problems:
- BFS explores level by level and gives shortest paths in unweighted graphs
- DFS dives deep, making it ideal for cycle detection, topological ordering, and exhaustive search
Once you understand graphs, BFS and DFS feel like two different ways of asking the same question: how do I systematically explore connectivity?
Trees are special graphs
A tree is a graph with stronger guarantees:
- connected
- acyclic
- exactly edges for nodes
- exactly one simple path between any two nodes
That one sentence is worth remembering:
A tree is a special case of a graph.
This is why so many tree techniques are really graph techniques with fewer edge cases. BFS and DFS still work. Shortest paths become trivial because there is only one path. Visited sets often disappear because the structure itself prevents revisits.
Common graph shapes
DAG (Directed Acyclic Graph)
A DAG is a directed graph with no cycles.
Use it for:
- build systems
- course schedules
- dependency resolution
DAGs unlock topological sort.
Weighted graph
Edges carry costs:
A --5--> B
A --2--> C
Once weights matter, plain BFS is no longer enough. You move to Dijkstra (or Bellman-Ford / Floyd-Warshall depending on the setting).
Complexity
With an adjacency list:
| Operation | Time |
|---|---|
| Store the graph | |
| BFS | |
| DFS | |
| Check one specific edge |
With an adjacency matrix:
| Operation | Time |
|---|---|
| Store the graph | |
| BFS / DFS | |
| Check one specific edge |
Key takeaways
- A graph is the general model for connected things; trees, DAGs, and weighted networks are all more specialized graph shapes
- Paths, cycles, and connected components are the first three structural questions to ask when reading a graph problem
- Adjacency lists are the default representation for sparse graphs and almost always the right interview choice
- BFS and DFS are the two universal graph traversals; most graph algorithms are built on top of them
- A tree is just a connected acyclic graph, which is why graph intuition transfers directly into tree problems
Practice problems
| Problem | Difficulty | Key idea |
|---|---|---|
| Find if Path Exists in Graph | 🟢 Easy | Reachability is the most basic graph question |
| Number of Connected Components in an Undirected Graph | 🟡 Medium | Traverse each unseen component once |
| Is Graph Bipartite? | 🟡 Medium | BFS/DFS coloring on a general graph |
| Course Schedule | 🟡 Medium | DAG reasoning and cycle detection |
| Clone Graph | 🟡 Medium | Traverse while preserving structure |
Relation to other topics
- Tree fundamentals adds stronger guarantees on top of a graph: no cycles, full connectivity, unique simple paths
- BFS and DFS are the two core traversals every graph problem is built from
- Topological sort is what happens when the graph is directed and acyclic
- Dijkstra adds weights to shortest-path reasoning
- Minimum spanning tree and Union-Find are about connectivity and edge selection under graph constraints
What this enables
Once the current idea feels natural, these are the most useful next jumps.
BFS — Breadth-First Search
Explore a graph level by level using a queue. BFS finds the shortest path in unweighted graphs and is fundamental to many graph algorithms.
DFS — Depth-First Search
Dive as deep as possible before backtracking. DFS is essential for detecting cycles, topological sorting, and exploring all paths in a graph.
0-1 BFS
Find shortest paths in graphs whose edge weights are only 0 or 1 using a deque instead of a heap.
Dijkstra's Algorithm
Find the shortest path in weighted graphs with non-negative edges. Dijkstra is BFS with a priority queue — it processes nodes in order of cumulative distance.
Minimum Spanning Tree
Find the subset of edges that connects all vertices with minimum total weight. Kruskal's and Prim's are the two classic approaches.
Topological Sort
Order the nodes of a DAG so that every edge points forward. Essential for dependency resolution, build systems, and scheduling.
Related directions
These topics live nearby conceptually, even if they are not direct prerequisites.
BFS — Breadth-First Search
Explore a graph level by level using a queue. BFS finds the shortest path in unweighted graphs and is fundamental to many graph algorithms.
DFS — Depth-First Search
Dive as deep as possible before backtracking. DFS is essential for detecting cycles, topological sorting, and exploring all paths in a graph.
0-1 BFS
Find shortest paths in graphs whose edge weights are only 0 or 1 using a deque instead of a heap.
Bridges and Articulation Points
Use DFS timestamps and low-link values to find the edges and vertices whose removal disconnects an undirected graph.
More from Graphs
Stay in the same family when you want parallel variations of the same mental model.
BFS — Breadth-First Search
Explore a graph level by level using a queue. BFS finds the shortest path in unweighted graphs and is fundamental to many graph algorithms.
DFS — Depth-First Search
Dive as deep as possible before backtracking. DFS is essential for detecting cycles, topological sorting, and exploring all paths in a graph.
0-1 BFS
Find shortest paths in graphs whose edge weights are only 0 or 1 using a deque instead of a heap.
Dijkstra's Algorithm
Find the shortest path in weighted graphs with non-negative edges. Dijkstra is BFS with a priority queue — it processes nodes in order of cumulative distance.
Paths that include this topic
Follow one of these sequences if you want a guided next step instead of open-ended browsing.
Graph toolkit
Start with graph shape, then layer traversal, connectivity structure, and both single-source and all-pairs path algorithms.
Weighted shortest paths
See the spectrum from plain BFS to deque-based 0-1 BFS to priority-queue shortest paths and dense all-pairs DP.
Graph structure
Go beyond traversal into SCCs, critical edges, degree structure, and other non-shortest-path graph properties.
From the blog
Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.