Depth-first search on a tree

98 Views Asked by At

I understand the concept of depth-first search and I know exactly what it does. However, I can't understand this particular algorithm. Can someone give a short example on the basis of this algorithm?

$\text{1. Initilisation: } S:=\{(w,0)\} \text{ and } R := \emptyset$

$\text{2. Choose vertex: choose from $S$ a pair $p=(a,n)$ with a maximal $n$}$

$\text{3. Edit: $S := (S \setminus \{p\}) \space\cup \space \{(b,n+1)\space |\space b \in a.children \}$ and $R:=R\space\cup\space\{ a \}; $ treat $a$}$

$\text{4. Stop?: if $S = \emptyset $ stop else go to Choose vertex}$

1

There are 1 best solutions below

0
On

I found a similar algorithm in my AI course that makes this algorithm clear, you can see the algorithm below. The difference is that this algorithm does not make use of depth $n$.

When we compare this to the algorithm above, we can conclude that at first $S$ is a set that only contains the root $w$ with $0$ as its depth. Then we choose a vertex $a$ with maximal depth $n$ in $S$ and we will remove that vertex from $S$ and add the children of $a$ with depth $(n+1)$ to $S$. After that, $a$ will be added to the set $R$. We repeat this until the set $S$ is empty.

alternative algorithm