Choose the best scenario where using an adjacency list is more efficient than an adjacency matrix.
Think about memory usage and how many edges exist compared to vertices.
An adjacency list uses less memory for sparse graphs because it stores only existing edges. An adjacency matrix always uses memory proportional to the square of vertices, which is inefficient for sparse graphs.
What is the printed adjacency matrix after adding edges (0->1), (1->2), and (2->0) in a graph with 3 vertices?
int[,] matrix = new int[3,3]; matrix[0,1] = 1; matrix[1,2] = 1; matrix[2,0] = 1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Console.Write(matrix[i,j] + " "); } Console.WriteLine(); }
Check which indices are set to 1 in the matrix.
The matrix rows represent source vertices and columns represent destination vertices. Only the specified edges are set to 1.
What is the printed adjacency list after adding edges (0->1), (0->2), (1->2), and (2->0) in a graph with 3 vertices?
List<int>[] adjList = new List<int>[3]; for (int i = 0; i < 3; i++) adjList[i] = new List<int>(); adjList[0].Add(1); adjList[0].Add(2); adjList[1].Add(2); adjList[2].Add(0); for (int i = 0; i < 3; i++) { Console.Write(i + ": "); foreach (int v in adjList[i]) { Console.Write(v + " -> "); } Console.WriteLine("null"); }
Look at the order edges were added to each list.
The adjacency list stores neighbors in the order they were added. Vertex 0 has edges to 1 and 2, vertex 1 to 2, and vertex 2 to 0.
For a graph with 10,000 vertices and 20,000 edges, which data structure uses less memory?
Consider how many edges exist compared to the total possible edges.
An adjacency matrix for 10,000 vertices requires 100 million entries, which is large. An adjacency list stores only 20,000 edges, much less memory.
You need to frequently check if an edge exists between two vertices in a graph with 500 vertices and 100,000 edges. Which data structure is best?
Think about the time complexity of checking if an edge exists.
An adjacency matrix allows O(1) time to check if an edge exists by direct indexing. Adjacency lists require searching neighbors, which can be slower.
