Build an adjacency list
Warm-up · Trace the algorithm · about 4 min
The edge list 0-1, 1-2, 0-3, 1-3, 3-4, 2-4 is read from top to bottom. Each undirected edge u-v appends v to list u and u to list v.
Fill in each value before the step plays.
Example 1
Input:
{"n":5,"edges":[[0,1],[1,2],[0,3],[1,3],[3,4],[2,4]],"directed":false,"positions":[[0,0],[1.6,0],[3.2,0],[0.8,1.4],[2.4,1.4]]}
Output:
[[1,3],[0,2,3],[1,4],[0,1,4],[3,2]]
List u ends up with the neighbours of u in reading order.
Constraints
- The trace uses the graph shown
- New entries go at the end of a list
Learn the idea first: Adjacency matrix, adjacency list, edge list, a visual lesson.
More Adjacency matrix, adjacency list, edge list problems
- Memory cost Warm-up
- Matrix to list Standard
- Edge list to matrix Standard
- Look up an edge Standard
- Pick the representation Standard
- Degree from each representation Challenge