<script>!function(){functiont(t){document.documentElement.setAttribute("data-theme",t)}vare=function(){try{returnnewURLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{returnlocalStorage.getItem("theme")}catch(t){}}();t(null!==e?e:"light")}(),function(){try{constc=newURLSearchParams(window.location.search).entries();for(var[t,e]ofc)if(t.startsWith("docusaurus-data-")){vara=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()</script><divid="__docusaurus"><divrole="region"aria-label="Skip to main content"><aclass="skipToContent_fXgn"href="#__docusaurus_skipToContent_fallback">Skip to main content</a></div><navaria-label="Main"class="navbar navbar--fixed-top"><divclass="navbar__inner"><divclass="navbar__items"><buttonaria-label="Toggle navigation bar"aria-expanded="false"class="navbar__toggle clean-btn"type="button"><svgwidth="30"height="30"viewBox="0 0 30 30"aria-hidden="true"><pathstroke="currentColor"stroke-linecap="round"stroke-miterlimit="10"stroke-width="2"d="M4 7h22M4 15h22M4 23h22"></path></svg></button><aclass="navbar__brand"href="/"><bclass="navbar__title text--truncate">mf</b></a><divclass="navbar__item dropdown dropdown--hoverable"><ahref="#"aria-haspopup="true"aria-expanded="false"role="button"class="navbar__link">Additional FI MU materials</a><ulclass="dropdown__menu"><li><aaria-current="page"class="dropdown__link dropdown__link--active"href="/algorithms/">Algorithms</a></li><li><aclass="dropdown__link"href="/c/">C</a></li><li><aclass="dropdown__link"href="/cpp/">C++</a></li></ul></div><aclass="navbar__item navbar__link"href="/contributions/">Contributions</a><aclass="navbar__item navbar__link"href="/talks/">Talks</a></div><divclass="navbar__items navbar__items--right"><aclass="navbar__item navbar__link"href="/blog/">Blog</a><divclass="toggle_vylO colorModeToggle_DEke"><buttonclass="clean-btn toggleButton_gllP toggleButtonDisabled_aARS"type="button"disabled=""title="Switch between dark and light mode (currently light mode)"aria-label="Switch between dark and light mode (currently light mode)"aria-live="polite"><svgviewBox="0 0 24 24"width="24"height="24"class="lightToggleIcon_pyhR"><pathfill="currentColor"d="M12,9c1.65,0,3,1.35,3,3s-1.35,3-3,3s-3-1.35-3-3S10.35,9,12,9 M12,7c-2.76,0-5,2.24-5,5s2.24,5,5,5s5-2.24,5-5 S14.76,7,12,7L12,7z M2,13l2,0c0.55,0,1-0.45,1-1s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S1.45,13,2,13z M20,13l2,0c0.55,0,1-0.45,1-1 s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S19.45,13,20,13z M11,2v2c0,0.55,0.45,1,1,1s1-0.45,1-1V2c0-0.55-0.45-1-1-1S11,1.45,11,2z M11,20v2c0,0.55,0.45,1,1,1s1-0.45,1-1v-2c0-0.55-0.45-1-1-1C11.45,19,11,19.45,11,20z M5.99,4.58c-0.39-0.39-1.03-0.39-1.41,0 c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0s0.39-1.03,0-1.41L5.99,4.58z M18.36,16.95 c-0.39-0.39-1.03-0.39-1.41,0c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0c0.39-0.39,0.39-1.03,0-1.41 L18.36,16.95z M19.42,5.99c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06c-0.39,0.39-0.39,1.03,0,1.41 s1.03,0.39,1.41,0L19.42,5.99z M7.05,18.36c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06 c-0.39,0.39-0.39,1.03,0,1.41s1.03,0.39,1.41,0L7.05,18.36z"></path></svg><svgviewBox="0 0 24 24"width="24"height="24"class="darkToggleIcon_wfgR"><pathfill="currentColor"d="M9.37,5.51C9.19,6.15,9.1,6.82,9.1,7.5c0,4.08,3.32,7.4,7.4,7.4c0.68,0,1.35-0.09,1.99-0.27C17.45,17.19,14.93,19,12,19 c-3.86,0-7-3.14-7-7C5,9.07,6.81,6.55,9.37,5.51z M12,3c-4.97,0-9,4.03-9,9s4.03,9,9,9s9-4.03,9-9c0-0.46-0.04-0.92-0.1-1.36 c-0.98,1.37-2.58,2.26-4.4,2.26c-2.98,0-5.4-2.42-5.4-5.4c0-1.81,0.89-3.42,2.26-4.4C12.92,3.04,12.46,3,12,3L12,3z"></path></svg></button></div><divclass="navbarSearchContainer_Bca1"><buttontype="button"class="DocSearch DocSearch-Button"aria-label="Search"><spanclass="DocSearch-Button-Container"><svgwidth="20"height="20"class="DocSearch-Search-Icon"viewBox="0 0 20 20"><pathd="M14.38614.386l4.08774.0877-4.0877-4.0877c-2.
<ul>
<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