Chapter 39: Breadth-First Search Challenges

Challenge 1

For the following undirected graph, list the maximum number of items ever in the queue. Assume that the starting vertex is A.

思路: 動手畫畫看

同時在queue中的最多點數是3。

Challenge 2

In this chapter, you went over an iterative implementation of breadth-first search. Now write a recursive implementation.

  • 思路: Iterateion和Recursive要可以互換,可以用Recursive就可以改寫成Iterateion,可以Iterateion就可以改寫成Recursive

Challenge 3

Add a method to Graph to detect if a graph is disconnected. An example of a disconnected graph is shown below:

To help you solve this challenge, a property allVertices was added to the Graph protocol: var allVertices: [Vertex] { get } This property is already implemented by AdjacencyMatrix and AdjacencyList.

思路: disconnected表示有兩個點找不到任何路徑可以相連。

Last updated