Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
| Download
Project: Applied Discrete Structures
Views: 479License: OTHER
Kernel: SageMath (stable)
Implementation of the Depth-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.
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.
In [16]:
In [28]:
[49, 65, 2, 18, 83, 52, 21, 6, 86, 10, 74, 59, 60, 45, 47]
We will search for sink=95 starting at source=0.
In [18]:
Initialize the "found" and "from" lists:
In [42]:
In [43]:
In [44]:
In [45]:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-45-d7271d12febb> in <module>()
----> 1 dfp(Integer(0),Integer(95))
<ipython-input-44-a0191eded801> in dfp(so, sk)
9 return()
10 for v in nbsNew:
---> 11 dfp(v,sk)
... last 1 frames repeated, from the frame below ...
<ipython-input-44-a0191eded801> in dfp(so, sk)
9 return()
10 for v in nbsNew:
---> 11 dfp(v,sk)
RuntimeError: maximum recursion depth exceeded while calling a Python object
In [12]:
In [75]:
[95, 32, 6, 80, 44, 0]
We can use the function to find other vertices.
In [71]:
[30, 44, 0]
Not every vertex can be reached:
In [74]:
31 not found from 0
[]
In [0]:
In [35]:
True
In [0]: