Look up an edge
Standard · Trace the algorithm · about 5 min
The same graph is stored as a matrix and as adjacency lists. Two questions are asked: is 2-3 an edge, and is 4-1 an edge?
The matrix reads one cell. The list for u is read from left to right until v is found or the list ends.
Example 1
Input:
{"n":5,"edges":[[0,1],[0,2],[1,2],[1,3],[2,3],[3,4]],"directed":false,"queries":[[2,3],[4,1]],"positions":[[0,1],[1.4,0],[1.4,2],[2.8,1],[4.2,1]]}
Output:
[true,false]
2-3 is an edge; 4-1 is not.
Constraints
- The trace uses the graph shown
Learn the idea first: Adjacency matrix, adjacency list, edge list, a visual lesson.
More Adjacency matrix, adjacency list, edge list problems
- Build an adjacency list Warm-up
- Memory cost Warm-up
- Matrix to list Standard
- Edge list to matrix Standard
- Pick the representation Standard
- Degree from each representation Challenge