<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wikimd.org/index.php?action=history&amp;feed=atom&amp;title=Depth-first_search</id>
	<title>Depth-first search - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wikimd.org/index.php?action=history&amp;feed=atom&amp;title=Depth-first_search"/>
	<link rel="alternate" type="text/html" href="https://wikimd.org/index.php?title=Depth-first_search&amp;action=history"/>
	<updated>2026-04-25T03:48:04Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wikimd.org/index.php?title=Depth-first_search&amp;diff=5890665&amp;oldid=prev</id>
		<title>Prab: CSV import</title>
		<link rel="alternate" type="text/html" href="https://wikimd.org/index.php?title=Depth-first_search&amp;diff=5890665&amp;oldid=prev"/>
		<updated>2024-06-05T22:37:32Z</updated>

		<summary type="html">&lt;p&gt;CSV import&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[File:Depth-first-tree.svg|thumb|Depth-first-tree]] [[file:Depth-First-Search.gif|right|thumb|Depth-First-Search]] [[file:graph.traversal.example.svg|right|thumb|graph.traversal.example]] [[file:Tree_edges.svg|thumb|Tree_edges]] [[file:If-then-else-control-flow-graph.svg|thumb|If-then-else-control-flow-graph]] [[file:Graph.traversal.example.svg|thumb|Graph.traversal.example]] [[file:MAZE_30x20_DFS.ogv|thumb|MAZE_30x20_DFS.ogv]] &amp;#039;&amp;#039;&amp;#039;Depth-first search&amp;#039;&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;DFS&amp;#039;&amp;#039;&amp;#039;) is an algorithm for traversing or searching [[tree (data structure)|tree]] or [[graph (discrete mathematics)|graph]] data structures. The algorithm starts at the [[root (graph theory)|root]] node (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.&lt;br /&gt;
&lt;br /&gt;
==Algorithm==&lt;br /&gt;
The basic idea of DFS is to start from the root (or any arbitrary node) and mark the node and move to an adjacent unmarked node and continue this process until there is no unmarked adjacent node. Then backtrack and check for other unmarked nodes and traverse them. Finally, print the nodes in the path.&lt;br /&gt;
&lt;br /&gt;
===Pseudocode===&lt;br /&gt;
The pseudocode for DFS is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Recursive DFS:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;pseudocode&amp;quot;&amp;gt;&lt;br /&gt;
procedure DFS(G, v) is&lt;br /&gt;
  label v as discovered&lt;br /&gt;
  for all directed edges from v to w that are in G.adjacentEdges(v) do&lt;br /&gt;
    if vertex w is not labeled as discovered then&lt;br /&gt;
      recursively call DFS(G, w)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Iterative DFS:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;pseudocode&amp;quot;&amp;gt;&lt;br /&gt;
procedure DFS-iterative(G, v) is&lt;br /&gt;
  let S be a stack&lt;br /&gt;
  S.push(v)&lt;br /&gt;
  while S is not empty do&lt;br /&gt;
    v = S.pop()&lt;br /&gt;
    if v is not labeled as discovered then&lt;br /&gt;
      label v as discovered&lt;br /&gt;
      for all edges from v to w in G.adjacentEdges(v) do&lt;br /&gt;
        S.push(w)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Applications==&lt;br /&gt;
DFS is used in various applications, including:&lt;br /&gt;
* [[Topological sorting]]&lt;br /&gt;
* [[Finding connected components]]&lt;br /&gt;
* [[Pathfinding]]&lt;br /&gt;
* [[Solving puzzles]] with only one solution, such as mazes&lt;br /&gt;
&lt;br /&gt;
==Properties==&lt;br /&gt;
* DFS can be implemented using [[recursion]] or using a [[stack (abstract data type)|stack]].&lt;br /&gt;
* The time complexity of DFS is O(V + E), where V is the number of vertices and E is the number of edges.&lt;br /&gt;
* The space complexity of DFS is O(V) in the worst case.&lt;br /&gt;
&lt;br /&gt;
==Related Algorithms==&lt;br /&gt;
* [[Breadth-first search]]&lt;br /&gt;
* [[Dijkstra&amp;#039;s algorithm]]&lt;br /&gt;
* [[A* search algorithm]]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
* [[Graph theory]]&lt;br /&gt;
* [[Tree traversal]]&lt;br /&gt;
* [[Algorithm]]&lt;br /&gt;
&lt;br /&gt;
==Related Pages==&lt;br /&gt;
* [[Graph (discrete mathematics)]]&lt;br /&gt;
* [[Tree (data structure)]]&lt;br /&gt;
* [[Breadth-first search]]&lt;br /&gt;
* [[Topological sorting]]&lt;br /&gt;
* [[Pathfinding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Graph algorithms]]&lt;br /&gt;
[[Category:Search algorithms]]&lt;br /&gt;
[[Category:Computer science]]&lt;br /&gt;
&lt;br /&gt;
{{Graph algorithms}}&lt;br /&gt;
{{Algorithm-stub}}&lt;/div&gt;</summary>
		<author><name>Prab</name></author>
	</entry>
</feed>