\begin{algorithm}
\caption{BFS-Visit-Iterative($G, v$)}
\begin{algorithmic}
\State $Q \gets \emptyset$
\State Markiere $v$ als aktiv
\State \Call{Enqueue}{$Q, v$}
\While{$Q \neq \emptyset$}
\State $w \gets$ \Call{Dequeue}{$Q$}
\State Markiere $w$ als besucht
\For{each $(w, x) \in E$}
\If{$x$ nicht aktiv und $x$ noch nicht besucht}
\State Markiere $x$ als aktiv
\State \Call{Enqueue}{$Q, x$}
\EndIf
\EndFor
\EndWhile
\end{algorithmic}
\end{algorithm}
This implementation uses a Queue (FIFO) which is very similar to our implementation from last semester.
DFS (Iterativ)
\begin{algorithm}
\caption{DFS-Visit-Iterative($G, v$)}
\begin{algorithmic}
\State $S \gets \emptyset$
\State \Call{Push}{$S, v$}
\While{$S \neq \emptyset$}
\State $w \gets$ \Call{Pop}{$S$}
\If{$w$ noch nicht besucht}
\State Markiere $w$ als besucht
\For{each $(w, x) \in E$ in reverse order}
\If{$x$ noch nicht besucht}
\State \Call{Push}{$S, x$}
\EndIf
\EndFor
\EndIf
\EndWhile
\end{algorithmic}
\end{algorithm}
This represents a major difference to our previously recursive implementation. We use a Stack (LIFO) to “simulate” the recursive descent into the tree’s branches.