Graphs Flashcards
Learn
How do you represent a graph with an adjacency list?
Use a mapping where each node points to a list of its neighbors.
What’s an adjacency matrix?
A 2D grid where each cell shows whether an edge exists between nodes.
What’s the difference between a directed and undirected graph?
Directed graphs have one-way edges; undirected graphs have two-way edges.
How do you create a graph using a library like NetworkX?
Use functions to add nodes and edges to a graph object.
When should you use an adjacency list instead of a matrix?
For sparse graphs where memory efficiency is important.
What’s the complexity of adding/removing edges in lists vs matrices?
Both are O(1), but adjacency lists use less memory in sparse graphs.
What are the pros and cons of adjacency lists and matrices?
Lists save space and are good for sparse graphs; matrices allow fast lookups but use more space.
What does an adjacency list with node A pointing to B and C mean?
Node A is connected to nodes B and C.
How do you represent a weighted directed graph?
Use nested mappings where each edge has an associated weight.