<li><ahref="/files/algorithms/graphs/iterative-and-iterators.tar.gz"target="_blank"rel="noopener noreferrer">Source code used later on.</a></li>
</ul>
<p>As we have talked on the seminar, iterative approach to implementing DFS is not very intuitive and is a very easy way how to create an incorrect implementation.</p>
<p>On the other hand, we have seen iterative implementation in the exercises and I have also prepared two from which one was similar to recursive implementation without colors from exercises and the other one used features of high-level languages.</p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="different-implementations">Different implementations<ahref="#different-implementations"class="hash-link"aria-label="Direct link to Different implementations"title="Direct link to Different implementations"></a></h2>
<h3class="anchor anchorWithStickyNavbar_LWe7"id="recursive-dfs-implementation-from-exercises-without-colors">Recursive DFS implementation from exercises without colors<ahref="#recursive-dfs-implementation-from-exercises-without-colors"class="hash-link"aria-label="Direct link to Recursive DFS implementation from exercises without colors"title="Direct link to Recursive DFS implementation from exercises without colors"></a></h3>
<p>This implementation is correct, does the DFS traversal as it should, however it has one “smallish” downside and that is the time complexity. The usage of set raises the time complexity, of course it is implementation dependant. However in case of either RB-tree or hash-table implementation, we get look-up in time <spanclass="katex"><spanclass="katex-mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mimathvariant="script">O</mi><mostretchy="false">(</mo><mi>n</mi><mostretchy="false">)</mo></mrow><annotationencoding="application/x-tex">\mathcal{O}(n)</annotation></semantics></math></span><spanclass="katex-html"aria-hidden="true"><spanclass="base"><spanclass="strut"style="height:1em;vertical-align:-0.25em"></span><spanclass="mord mathcal"style="margin-right:0.02778em">O</span><spanclass="mopen">(</span><spanclass="mord mathnormal">n</span><spanclass="mclose">)</span></span></span></span> for hash-table in worst-case or <spanclass="katex"><spanclass="katex-mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mimathvariant="script">O</mi><mostretchy="false">(</mo><mi>log</mi><mo></mo><mi>n</mi><mostretchy="false">)</mo></mrow><annotationencoding="application/x-tex">\mathcal{O}(\log n)</annotation></semantics></math></span><spanclass="katex-html"aria-hidden="true"><spanclass="base"><spanclass="strut"style="height:1em;vertical-align:-0.25em"></span><spanclass="mord mathcal"style="margin-right:0.02778em">O</span><spanclass="mopen">(</span><spanclass="mop">lo<spanstyle="margin-right:0.01389em">g</span></span><spanclass="mspace"style="margin-right:0.1667em"></span><spanclass="mord mathnormal">n</span><spanclass="mclose">)</span></span></span></span> for the other in the worst-case. Both are not ideal compared to checking color on vertex.</p>
<h3class="anchor anchorWithStickyNavbar_LWe7"id="iterative-dfs-from-the-exercises">Iterative DFS from the exercises<ahref="#iterative-dfs-from-the-exercises"class="hash-link"aria-label="Direct link to Iterative DFS from the exercises"title="Direct link to Iterative DFS from the exercises"></a></h3>
<p>As we can see, there is some ordering in which we search through the successors. Time complexity is OK, stack holds at most all vertices (they must be on the current path).</p>
<h3class="anchor anchorWithStickyNavbar_LWe7"id="my-iterative-with-path-in-stack">My iterative with path in stack<ahref="#my-iterative-with-path-in-stack"class="hash-link"aria-label="Direct link to My iterative with path in stack"title="Direct link to My iterative with path in stack"></a></h3>
<p>This approach is similar to the iterative solution from the exercises, but it does not keep the index of the next successor, therefore it always iterates through all of them, which raises the time complexity.</p>
<h3class="anchor anchorWithStickyNavbar_LWe7"id="my-iterative-solution-with-iterators">My iterative solution with iterators<ahref="#my-iterative-solution-with-iterators"class="hash-link"aria-label="Direct link to My iterative solution with iterators"title="Direct link to My iterative solution with iterators"></a></h3>
<p>On the other hand, we do not actually have to depend on the representation of the graph. In this case, we just <em>somehow</em> obtain the iterator (which yields all of the succesors) and keep it in the stack.</p>
<p>( The way we manipulate with the iterators is closest to the C# implementation. Apart from the <code>Iterator</code> thing :) In case you tried to implement it in C++, you would more than likely need to change the check, since you would get first successor right at the beginning )</p>
<p>So here we don't keep indices, but the iterators. We can also check existence of other successors easily: by the iterator moving after the last successor.</p>
<p>Closer explanation of the <em>iterator shenanigans</em> follows. In the beginning, either <code>start</code> or when pushing new vertex, we are pushing an iterator that points <em>just before</em> the first successor. When populating <code>lastVertex</code> and <code>successors</code> in the <code>while</code>-loop, we take the element from the top of the stack. <code>MoveNext</code> returns <code>true</code> if there is an element, i.e. successor in this case. If it returns <code>false</code> we have nothing to do and we pop the vertex from the stack (also set finishing time and color). If we have successor we check if it has been already visited or not. If has not, we set discovery time and color accordingly, also we add it to stack.</p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="implementation">Implementation<ahref="#implementation"class="hash-link"aria-label="Direct link to Implementation"title="Direct link to Implementation"></a></h2>
<p>In case you want to play around with the code. At the beginning there is a link to the C# implementation that can be used. It has a basic representation of graph and includes BFS/DFS implementation in classes.</p>
<p>In <code>Program.cs</code> you can also find a method that returns graph we used on the seminar.</p></div><footerclass="theme-doc-footer docusaurus-mt-lg"><divclass="theme-doc-footer-tags-row row margin-bottom--sm"><divclass="col"><b>Tags:</b><ulclass="tags_jXut padding--none margin-left--sm"><liclass="tag_QGVx"><aclass="tag_zVej tagRegular_sFm0"href="/algorithms/tags/csharp/">csharp</a></li><liclass="tag_QGVx"><aclass="tag_zVej tagRegular_sFm0"href="/algorithms/tags/graphs/">graphs</a></li><liclass="tag_QGVx"><aclass="tag_zVej tagRegular_sFm0"href="/algorithms/tags/iterators/">iterators</a></li><liclass="tag_QGVx"><aclass="tag_zVej tagRegular_sFm0"href="/algorithms/tags/iterative/">iterative</a></li></ul></div></div><divclass="theme-doc-footer-edit-meta-row row"><divclass="col"><ahref="https://github.com/mfocko/blog/tree/main/algorithms/10-graphs/2021-05-18-iterative-and-iterators.md"target="_blank"rel="noopener noreferrer"class="theme-edit-this-page"><svgfill="currentColor"height="20"width="20"viewBox="0 0 40 40"class="iconEdit_Z9Sw"aria-hidden="true"><g><pathd="m34.5 11.7l-3 3.1-6.3-6.3 3.1-3q0.5-0.5 1.2-0.5t1.1 0.5l3.9 3.9q0.5 0.4 0.5 1.1t-0.5 1.2z m-29.5 17.1l18.4-18.5 6.3 6.3-18.4 18.4h-6.3v-6.2z"></path></g></svg>Edit this page</a></div><divclass="col lastUpdated_vwxv"><spanclass="theme-last-updated">Last updated<!----> on <b><timedatetime="2021-05-18T00:00:00.000Z">May 18, 2021</time></b></span></div></div></footer></article><navclass="pagination-nav docusaurus-mt-lg"aria-label="Docs pages"><aclass="pagination-nav__link pagination-nav__link--prev"href="/algorithms/category/graphs/"><divclass="pagination-nav__sublabel">Previous</div><divclass="pagination-nav__label">Graphs</div></a><aclass="pagination-nav__link pagination-nav__link--next"href="/algorithms/graphs/bfs-tree/"><divclass="pagination-nav__sublabel">Next</div><divclass="pagination-nav__label">Distance boundaries from BFS tree on undirected graphs</div></a></nav></div></div><divclass="col col--3"><divclass="tableOfContents_bqdL thin-scrollbar theme-doc-toc-desktop"><ulclass="table-of-contents table-of-contents__left-border"><li><ahref="#introduction"class="table-of-contents__link toc-highlight">Introduction</a></li><li><ahref="#different-implementations"class="table-of-contents__link toc-highlight">Different implementations</a><ul><li><ahref="#recursive-dfs-implementation-from-exercises-without-colors"class="table-of-contents__link toc-highlight">Recursive DFS implementation from exercises without colors</a></li><li><ahref="#iterative-dfs-from-the-exercises"class="table-of-contents__link toc-highlight">Iterative DFS from the exercises</a></li><li><ahref="#my-iterative-with-path-in-stack"class="table-of-contents__link toc-highlight">My iterative with path in stack</a></li><li><ahref="#my-iterative-solution-with-iterators"class="table-of-contents__link toc-highlight">My iterative solution with iterators</a></li></ul></li><li><ahref="#implementation"class="table-of-contents__link toc-highlight">Implementation</a></li></ul></div></div></div></div></main></div></div></div><footerclass="footer footer--dark"><divclass="container container-fluid"><divclass="row footer__links"><divclass="col footer__col"><divclass="footer__title">Git</div><ulclass="footer__items clean-list"><liclass="footer__item"><ahref="https://github.com/mfocko"target="_blank"rel="noopener noreferrer"class="footer__link-item">GitHub<svgwidth="13.5"height="13.5"aria-hidden="true"viewBox="0 0 24 24"class="iconExternalLink_nPIU"><pathfill="currentColor"d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><liclass="footer__item"><ahref="https://gitlab.com/mfocko"target="_blank"rel="noopener noreferrer"class="footer__link-item">GitLab<svgwidth="13.5"height="13.5"aria-hidden="true"viewBox="0 0 24 24"class="iconExternalLink_nPIU"><pathfill="currentColor"d="M2113v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.0354-6.9777.07