The 4 Graph Input Formats You Need to Recognize
Graph problems rarely say "here is a graph". They describe it in one of four formats, and spotting the format quickly tells you how to build your traversal.
Below is each format with the same idea drawn as a picture.
1) Edge List
You receive a list of pairs, where each pair names two vertices joined by an edge. The problem statement says whether an edge [a, b] goes one way or both ways. Build an adjacency list from it before traversing.
2) Adjacency List
The most convenient format. Each vertex maps straight to the vertices it connects to, so you can loop over neighbors right away. In Python this is usually a dictionary of lists.
3) Adjacency Matrix
An n by n grid of 0s and 1s, where row i and column j say whether vertex i has an edge to vertex j. Checking a single edge is instant, but listing neighbors means scanning a whole row.
4) Grid
Each cell of a 2D grid is a vertex, and the problem describes which neighboring cells you may move to. Classic examples count islands of land cells or spread something through the grid one step at a time.