Complete the code to create an empty adjacency matrix for a graph with n vertices.
adj_matrix = [[0 for _ in range([1])] for _ in range(n)]
The adjacency matrix is a square matrix of size n x n, so the inner list comprehension must run n times.
Complete the code to add an edge from vertex u to vertex v in the adjacency matrix.
adj_matrix[[1]][v] = 1
To add an edge from u to v, set the matrix cell at row u and column v to 1.
Fix the error in the code to check if there is an edge from vertex x to vertex y.
if adj_matrix[[1]][y] == 1: print("Edge exists")
To check the edge from x to y, use x as the row index and y as the column index.
Fill both blanks to create an adjacency matrix for an undirected graph by adding edges in both directions.
adj_matrix[[1]][v] = 1 adj_matrix[[2]][u] = 1
For undirected graphs, add edges both from u to v and from v to u.
Fill all three blanks to create a dictionary comprehension that maps each vertex to a list of its neighbors using the adjacency matrix.
neighbors = {vertex: [i for i, val in enumerate(adj_matrix[[1]]) if val [2] 1] for vertex in range([3])}The comprehension iterates over each vertex, then over its adjacency row to find neighbors where the value is 1.
