A graph is a collection of nodes (vertices) connected by edges. Graphs model relationships: social networks, maps, dependencies, and more.| Term | Definition |
|---|
| Vertex (Node) | A point in the graph |
| Edge | A connection between two vertices |
| Neighbor | A vertex directly connected by an edge |
| Degree | Number of edges connected to a vertex |
| Path | A sequence of vertices connected by edges |
| Cycle | A path that starts and ends at the same vertex |
| Type | Description | Example |
|---|
| Tree | Connected, acyclic graph | File system, org chart |
| DAG | Directed acyclic graph | Task dependencies |
| Bipartite | Vertices split into two sets | Matching problems |
| Complete | Every vertex connected to all others | Tournament brackets |
Many problems are graphs in disguise:- 2D grids: Each cell is a vertex, adjacent cells are neighbors
- Word transformations: Each word is a vertex, edges connect words that differ by one letter
- State machines: Each state is a vertex, transitions are edges
- Social networks: People are vertices, friendships are edges
When you see a graph problem, ask:- Directed or undirected? Can you go both ways?
- Weighted or unweighted? Do edges have costs?
- Can there be cycles? Or is it a DAG/tree?
- Is it connected? Or multiple components?