MemotivaLeetCode Patterns Flashcards: Graphs, BFS, DFS, Topological Sort, Union Find

How do you represent a graph for coding interview problems?

LeetCode Patterns Flashcards: Graphs, BFS, DFS, Topological Sort, Union Find

Аудио-карточка · 0:29

Nortren·

How do you represent a graph for coding interview problems?

0:29

The two main representations are adjacency lists and adjacency matrices. An adjacency list stores a list of neighbors for each node, typically as a hash map from node to list of neighbors. It uses O of V plus E space and is efficient for sparse graphs, which is most interview problems. An adjacency matrix uses a two-dimensional array where entry at row i column j indicates an edge from i to j, using O of V squared space. Many problems provide edges as a list of pairs, which you convert to an adjacency list before processing.
neetcode.io