<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Graph-Theory on IT Comparison</title><link>https://comparison.metacog.co.kr/tags/graph-theory/</link><description>Recent content in Graph-Theory on IT Comparison</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Sun, 02 Aug 2026 23:44:09 +0900</lastBuildDate><atom:link href="https://comparison.metacog.co.kr/tags/graph-theory/index.xml" rel="self" type="application/rss+xml"/><item><title>BFS vs DFS: Traversal Order and Data Structure</title><link>https://comparison.metacog.co.kr/posts/2026-08-02-bfs-vs-dfs-traversal-order-and-data-structure/</link><pubDate>Sun, 02 Aug 2026 23:44:09 +0900</pubDate><guid>https://comparison.metacog.co.kr/posts/2026-08-02-bfs-vs-dfs-traversal-order-and-data-structure/</guid><description>&lt;h2 id="overview"&gt;Overview&lt;/h2&gt;
&lt;p&gt;Both are algorithms for visiting every node in a tree or graph, but they differ in which node they explore next. &lt;strong class="kw"&gt;BFS&lt;/strong&gt; spreads outward level by level using a queue, while &lt;strong class="kw"&gt;DFS&lt;/strong&gt; plunges down one path as far as possible before backtracking, using a stack.&lt;/p&gt;
&lt;h2 id="comparison-diagram"&gt;Comparison Diagram&lt;/h2&gt;
&lt;div class="compare-diagram"&gt;
&lt;svg viewBox="0 0 640 360" xmlns="http://www.w3.org/2000/svg"&gt;&lt;line x1="320" y1="20" x2="320" y2="340" style="stroke:var(--border)" stroke-width="1.5" stroke-dasharray="5,5"/&gt;&lt;text x="160" y="28" text-anchor="middle" style="fill:var(--primary)" font-size="18" font-weight="bold"&gt;BFS&lt;/text&gt;&lt;text x="480" y="28" text-anchor="middle" style="fill:var(--primary)" font-size="18" font-weight="bold"&gt;DFS&lt;/text&gt;&lt;line x1="160" y1="50" x2="100" y2="120" style="stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;line x1="160" y1="50" x2="220" y2="120" style="stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;line x1="100" y1="120" x2="70" y2="190" style="stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;line x1="100" y1="120" x2="130" y2="190" style="stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;line x1="220" y1="120" x2="190" y2="190" style="stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;line x1="220" y1="120" x2="250" y2="190" style="stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;circle cx="160" cy="50" r="16" style="fill:var(--compare-a-soft);stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;text x="160" y="55" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;1&lt;/text&gt;&lt;circle cx="100" cy="120" r="16" style="fill:var(--compare-a-soft);stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;text x="100" y="125" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;2&lt;/text&gt;&lt;circle cx="220" cy="120" r="16" style="fill:var(--compare-a-soft);stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;text x="220" y="125" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;3&lt;/text&gt;&lt;circle cx="70" cy="190" r="16" style="fill:var(--compare-a-soft);stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;text x="70" y="195" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;4&lt;/text&gt;&lt;circle cx="130" cy="190" r="16" style="fill:var(--compare-a-soft);stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;text x="130" y="195" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;5&lt;/text&gt;&lt;circle cx="190" cy="190" r="16" style="fill:var(--compare-a-soft);stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;text x="190" y="195" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;6&lt;/text&gt;&lt;circle cx="250" cy="190" r="16" style="fill:var(--compare-a-soft);stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;text x="250" y="195" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;7&lt;/text&gt;&lt;line x1="480" y1="50" x2="420" y2="120" style="stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;line x1="480" y1="50" x2="540" y2="120" style="stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;line x1="420" y1="120" x2="390" y2="190" style="stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;line x1="420" y1="120" x2="450" y2="190" style="stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;line x1="540" y1="120" x2="510" y2="190" style="stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;line x1="540" y1="120" x2="570" y2="190" style="stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;circle cx="480" cy="50" r="16" style="fill:var(--compare-b-soft);stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;text x="480" y="55" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;1&lt;/text&gt;&lt;circle cx="420" cy="120" r="16" style="fill:var(--compare-b-soft);stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;text x="420" y="125" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;2&lt;/text&gt;&lt;circle cx="540" cy="120" r="16" style="fill:var(--compare-b-soft);stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;text x="540" y="125" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;5&lt;/text&gt;&lt;circle cx="390" cy="190" r="16" style="fill:var(--compare-b-soft);stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;text x="390" y="195" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;3&lt;/text&gt;&lt;circle cx="450" cy="190" r="16" style="fill:var(--compare-b-soft);stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;text x="450" y="195" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;4&lt;/text&gt;&lt;circle cx="510" cy="190" r="16" style="fill:var(--compare-b-soft);stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;text x="510" y="195" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;6&lt;/text&gt;&lt;circle cx="570" cy="190" r="16" style="fill:var(--compare-b-soft);stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;text x="570" y="195" text-anchor="middle" style="fill:var(--content)" font-size="13"&gt;7&lt;/text&gt;&lt;rect x="60" y="260" width="28" height="24" style="fill:var(--compare-a-soft);stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;rect x="92" y="260" width="28" height="24" style="fill:var(--compare-a-soft);stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;rect x="124" y="260" width="28" height="24" style="fill:var(--compare-a-soft);stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;rect x="156" y="260" width="28" height="24" style="fill:var(--compare-a-soft);stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;rect x="188" y="260" width="28" height="24" style="fill:var(--compare-a-soft);stroke:var(--compare-a)" stroke-width="1.5"/&gt;&lt;text x="20" y="277" style="fill:var(--secondary)" font-size="11"&gt;out&lt;/text&gt;&lt;text x="232" y="277" style="fill:var(--secondary)" font-size="11"&gt;in&lt;/text&gt;&lt;text x="140" y="305" text-anchor="middle" style="fill:var(--secondary)" font-size="13"&gt;Queue (FIFO)&lt;/text&gt;&lt;rect x="466" y="228" width="56" height="22" style="fill:var(--compare-b-soft);stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;rect x="466" y="252" width="56" height="22" style="fill:var(--compare-b-soft);stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;rect x="466" y="276" width="56" height="22" style="fill:var(--compare-b-soft);stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;rect x="466" y="300" width="56" height="22" style="fill:var(--compare-b-soft);stroke:var(--compare-b)" stroke-width="1.5"/&gt;&lt;text x="535" y="243" style="fill:var(--secondary)" font-size="11"&gt;push/pop&lt;/text&gt;&lt;text x="494" y="340" text-anchor="middle" style="fill:var(--secondary)" font-size="13"&gt;Stack (LIFO)&lt;/text&gt;&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id="comparison-table"&gt;Comparison Table&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;BFS&lt;/th&gt;
&lt;th&gt;DFS&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Underlying structure&lt;/td&gt;
&lt;td&gt;Queue (FIFO)&lt;/td&gt;
&lt;td&gt;Stack (FILO), often via recursion&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Traversal pattern&lt;/td&gt;
&lt;td&gt;Explores all neighbors at current depth before going deeper&lt;/td&gt;
&lt;td&gt;Follows one branch to its end before backtracking&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Order nodes are visited&lt;/td&gt;
&lt;td&gt;Level by level (breadth-first)&lt;/td&gt;
&lt;td&gt;Branch by branch (depth-first)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory usage&lt;/td&gt;
&lt;td&gt;O(width) — can be large for wide/bushy graphs&lt;/td&gt;
&lt;td&gt;O(depth) — can be large for deep graphs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shortest path guarantee&lt;/td&gt;
&lt;td&gt;Yes, on unweighted graphs (first visit = shortest path)&lt;/td&gt;
&lt;td&gt;No, may find a longer path first&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Implementation style&lt;/td&gt;
&lt;td&gt;Iterative with explicit queue&lt;/td&gt;
&lt;td&gt;Recursive, or iterative with explicit stack&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Risk of infinite loop&lt;/td&gt;
&lt;td&gt;Low with visited-set on cyclic graphs&lt;/td&gt;
&lt;td&gt;Higher on cyclic graphs without visited-set, or infinite depth&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Typical use cases&lt;/td&gt;
&lt;td&gt;Shortest path, level-order processing, web crawling by hops&lt;/td&gt;
&lt;td&gt;Topological sort, cycle detection, maze/backtracking problems&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id="key-differences"&gt;Key Differences&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong class="kw"&gt;BFS&lt;/strong&gt; uses a queue and expands outward level by level, while &lt;strong class="kw"&gt;DFS&lt;/strong&gt; uses a stack (or recursion) and dives deep before backtracking.&lt;/li&gt;
&lt;li&gt;BFS guarantees the &lt;strong class="kw"&gt;shortest path&lt;/strong&gt; on unweighted graphs; DFS does not.&lt;/li&gt;
&lt;li&gt;BFS memory cost scales with graph &lt;strong class="kw"&gt;width&lt;/strong&gt;, while DFS memory cost scales with graph &lt;strong class="kw"&gt;depth&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;DFS naturally supports &lt;strong class="kw"&gt;backtracking&lt;/strong&gt; algorithms like maze solving and topological sort.&lt;/li&gt;
&lt;li&gt;Both require a &lt;strong class="kw"&gt;visited set&lt;/strong&gt; to avoid infinite loops on cyclic graphs.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="when-to-use-each"&gt;When to Use Each&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;BFS&lt;/strong&gt;&lt;/p&gt;</description></item></channel></rss>