Worksheets related to Applied Discrete Structures
Image: ubuntu2004
Implementation of the Breadth-First Search algorithm
Applied Discrete Structures by Alan Doerr & Kenneth Levasseur is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
b We implement the breadth-first search algorithm from Section 9.3 of Applied Discrete Structures in this notebook. We use external lists to keep track of whether a vertex has been found (array name: found) and where it was found from (array name: frm, since from is a reserved word).
We start with a random graph. The default is a random graph with 100 vertices and a probablity of any edge being present being 0.05.
We will search for sink=95 starting at source=0.
Initialize the "found" and "from" lists:
The depth sets will be collected in a dictionary, the th depth set being .
Compute the depth sets. Instead of stopping when the sink vertex is found, we continue until we get an empty depth set. This will allow us to find paths to every reachable vertex.
At this point, the value of is the depth at which we had an empty set and so the maximal depth is one less than this value.
We can use the function to find other vertices.
Not every vertex can be reached: