Data Structures and Algorithms (DSA27) · Week 14

Graphs and Graph Searches (Lecture 14)

These quizzes are for self-study. Your answers are saved on this device only and are not sent anywhere.

Score: 0 / 22 · Answered: 0

  1. W14-M01 Recall

    An adjacency matrix for a graph with V vertices and E edges uses memory:

  2. W14-M02 Recall

    Breadth-first search keeps the vertices waiting to be expanded in a:

  3. W14-M03 Apply

    An undirected graph has 7 vertices with degrees 2, 3, 2, 2, 3, 3 and 1. How many edges does it have?

  4. W14-M04 Explain

    BFS from s always reaches a vertex first along a path with the fewest edges because:

  5. W14-M05 Apply

    bfs(H, "A") returns:

  6. W14-M06 Apply

    dfs(H, "A") returns:

  7. W14-M07 Apply

    shortest_path_unweighted(H, "D", "C") returns:

  8. W14-M08 Recall

    has_edge(u, v) on an adjacency matrix costs:

  9. W14-M09 Recall

    neighbours(u) on an adjacency matrix costs:

  10. W14-M10 Explain

    A BFS on a MatrixGraph costs O(V2) even when the graph has very few edges, because:

  11. W14-M11 Apply

    An undirected graph with E edges is stored as an adjacency list. How many neighbour entries do the lists hold in total?

  12. W14-M12 Explain

    BFS marks a vertex as visited when it is enqueued, not when it is dequeued, because otherwise:

  13. W14-M13 Explain

    dfs_iterative pushes each vertex's neighbours in reverse order because:

  14. W14-M14 Explain

    The recursive dfs is run on a graph that is one path of 5,000 vertices. In Python, with the default settings, it:

  15. W14-M15 Recall

    A DAG is:

  16. W14-M16 Apply

    topological_sort on the directed graph A → B, B → C, C → A returns:

  17. W14-M17 Explain

    Undirected cycle detection ignores a visited neighbour that is the vertex's parent, because:

  18. W14-M18 Explain

    In the directed diamond A → B, A → C, B → D, C → D, a DFS from A meets D a second time, from C. That is not a cycle, because at that moment D is:

  19. W14-M19 Apply

    A graph has vertices A to I and the undirected edges A–B, B–C, D–E, F–G. How many connected components does it have?

  20. W14-M20 Apply

    topological_sort(P) returns:

  21. W14-M21 Explain

    Which of these can BFS on its own not answer correctly?

  22. W14-M22 Apply

    One BFS over an adjacency list costs:

19 marks

Written questions

5 written questions (19 marks). Write your answer first, then open the model answer and its marking points, and mark yourself honestly: each point is worth about one mark.

Written total: 0 / 19

  1. W14-E1 Explain 4 marks

    Compare the adjacency list and the adjacency matrix: memory, has_edge, neighbours, and the cost of one BFS. Which would you choose for a road map of 100,000 junctions, and which for a graph of 500 people in which almost everyone knows almost everyone and you ask "do these two know each other?" millions of times? Justify with numbers.

  2. W14-E2 Explain 4 marks

    Describe breadth-first search. Explain why it finds shortest paths by number of edges, how shortest_path_unweighted rebuilds the path, and why the answer can be wrong on a road map with different road lengths.

  3. W14-E3 Explain 4 marks

    Recursive DFS and DFS with your own stack: what does the call stack remember for you? Why must dfs_iterative mark a vertex when it is popped and push neighbours in reverse to give the same order as dfs? Why write the iterative version at all in Python?

  4. W14-E4 Explain 4 marks

    Explain cycle detection in an undirected graph and in a directed graph. Why does each rule give wrong answers when used on the other kind of graph? Give a small example of each failure.

  5. W14-E5 Explain 3 marks

    Define a topological order. When does one exist? Describe Kahn's algorithm, how it detects that no order exists, and one real application.