Explore the key differences between adjacency lists and matrices in graph representations with this engaging quiz. Designed for beginners, these questions cover space complexity, edge handling, and practical use-cases in undirected and directed graphs.
Which graph representation typically uses less memory in a sparse graph with many vertices but few edges?
Explanation: An adjacency list only stores edges that actually exist, making it more space-efficient for sparse graphs. An adjacency matrix uses space for every possible edge, even if most do not exist, resulting in wasted memory. An incidence matrix is more efficient than a matrix in some cases but is not typically as compact as an adjacency list for sparse graphs. An edge table is not a standard representation for this context.
If you want to quickly check if an edge exists between two vertices in a dense graph, which representation is usually faster?
Explanation: An adjacency matrix allows you to check for an edge in constant time by directly accessing its cell. An Adjency List (note the typo) requires searching through a list, which is slower. Matrix List and Adjacency Tree are not standard, recognized representations for this task.
How are weighted edges usually represented in an adjacency matrix for a weighted graph?
Explanation: In a weighted graph, each entry in an adjacency matrix stores the weight of the edge if it exists. Using only 0 or 1 would lose the weight information. Weights are not ignored in matrices; instead, the actual value is used. Storing a count of edges is not standard for typical adjacency matrices.
Which representation makes it easier to efficiently list all outgoing neighbors of a given vertex in a directed graph?
Explanation: An adjacency list allows direct access to the list of neighbors for each vertex, making enumeration easy. With an adjacency matrix, you have to scan the entire row to find neighbors, which is less efficient. Edge Matrix and Sequence List are not typical names for standard representations.
When should you prefer using an adjacency matrix instead of an adjacency list?
Explanation: Adjacency matrices are more efficient for dense graphs because the space overhead is less significant, and checking for an edge is very fast. For graphs with few or no edges, a matrix wastes memory. Whether the vertices change rarely is not a deciding factor, and storing strings as vertices is unrelated to the representation choice.
What is the space complexity of an adjacency matrix for a graph with n vertices?
Explanation: An adjacency matrix uses O(n^2) space since it needs to store the connection status between every pair of n vertices. O(n) and O(e) are the space complexities for adjacency lists in sparse graphs, and O(log n) is incorrect for either representation.
How is a self-loop (an edge from a vertex to itself) represented in an adjacency matrix?
Explanation: Self-loops appear as nonzero entries (such as 1 or the weight) on the diagonal of the matrix. Setting every cell to zero eliminates all edges, and the last column has no special status. Self-loops can indeed be represented in both matrices and lists.
How does an adjacency matrix show the difference between directed and undirected edges?
Explanation: For undirected graphs, adjacency matrices are symmetric, while for directed graphs, only one entry is filled for each edge, making the matrix asymmetric in general. Symmetry is not required for all graphs, only undirected ones. The size of the matrix remains n by n for both types. Matrices for undirected graphs are not empty.
How can you determine the total number of edges in an undirected graph stored as an adjacency list?
Explanation: Since each edge is stored twice (once at each endpoint) in the adjacency list for undirected graphs, you count all entries and divide by two to get the correct edge count. Counting each list once will overcount, summing diagonal elements ignores edges not involving self-loops, and multiplying the number of vertices by two does not return correct results unless every vertex is connected to exactly two others.
Which representation is usually faster for adding or removing an edge in a mutable, sparse graph?
Explanation: In an adjacency list, adding or removing an edge involves updating a small list, typically taking less time in sparse graphs. With an adjacency matrix, modification requires changing a specific cell, but adjacency lists are more efficient with less memory overhead in sparse graphs. Path Table and Distance Matrix are either not standard or used in other contexts.