<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>In-place algorithm &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/in-place-algorithm/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Computer Algorithms: Graph Depth-First Search</title>
		<link>/2012/09/17/computer-algorithms-graph-depth-first-search/</link>
		<comments>/2012/09/17/computer-algorithms-graph-depth-first-search/#comments</comments>
		<pubDate>Mon, 17 Sep 2012 10:52:59 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[Breadth-first search]]></category>
		<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[Connectivity]]></category>
		<category><![CDATA[Depth-first search]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[graph-walk algorithm]]></category>
		<category><![CDATA[In-place algorithm]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[search algorithms]]></category>
		<category><![CDATA[specific algorithms]]></category>
		<category><![CDATA[two main algorithms]]></category>
		<category><![CDATA[USD]]></category>

		<guid isPermaLink="false">/?p=3340</guid>
		<description><![CDATA[Introduction Along with breadth-first search, depth-first search is one of the two main methods to walk through a graph. This approach though is different. Breadth-first search (BFS) looks pretty much like starting from a vertex and expanding the searching process level by level. This means that first we get some information of all the successors &#8230; <a href="/2012/09/17/computer-algorithms-graph-depth-first-search/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Graph Depth-First Search</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/09/24/computer-algorithms-graph-best-first-search/" rel="bookmark" title="Computer Algorithms: Graph Best-First Search">Computer Algorithms: Graph Best-First Search </a></li>
<li><a href="/2012/09/10/computer-algorithms-graph-breadth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Breadth First Search">Computer Algorithms: Graph Breadth First Search </a></li>
<li><a href="/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Dijkstra Shortest Path in a Graph">Computer Algorithms: Dijkstra Shortest Path in a Graph </a></li>
<li><a href="/2012/10/08/computer-algorithms-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Graph">Computer Algorithms: Shortest Path in a Graph </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Along with <a title="Computer Algorithms: Graph Breadth First Search" href="/2012/09/10/computer-algorithms-graph-breadth-first-search/">breadth-first search</a>, depth-first search is one of the two main methods to walk through a graph. This approach though is different. Breadth-first search (BFS) looks pretty much like starting from a vertex and expanding the searching process level by level. This means that first we get some information of all the successors of the given node and then we go further with the next level. In other words BFS is like a wave. Depth-first search is based on a different approach, which can be very useful in some specific algorithms.</p>
<figure id="attachment_3348" style="width: 621px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/09/1.-DFS-vs.-BFS.png"><img class="size-full wp-image-3348" title="DFS vs. BFS" src="/wp-content/uploads/2012/09/1.-DFS-vs.-BFS.png" alt="DFS vs. BFS" width="621" height="351" srcset="/wp-content/uploads/2012/09/1.-DFS-vs.-BFS.png 621w, /wp-content/uploads/2012/09/1.-DFS-vs.-BFS-300x169.png 300w" sizes="(max-width: 621px) 100vw, 621px" /></a><figcaption class="wp-caption-text">Depth-first and breadth-first search are the two main ways to explore a graph!</figcaption></figure>
<p>Both methods can be useful in solving different tasks.<span id="more-3340"></span></p>
<h2>Overview</h2>
<p>Depth-first search is an algorithm that by given starting and target node, finds a path between them. We can use DFS also to walk through all the vertices of a graph, in case the graph is connected.</p>
<figure id="attachment_3350" style="width: 621px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/09/2.-DFS-explained.png"><img class="size-full wp-image-3350" title="DFS explained" src="/wp-content/uploads/2012/09/2.-DFS-explained.png" alt="DFS explained" width="621" height="351" srcset="/wp-content/uploads/2012/09/2.-DFS-explained.png 621w, /wp-content/uploads/2012/09/2.-DFS-explained-300x169.png 300w" sizes="(max-width: 621px) 100vw, 621px" /></a><figcaption class="wp-caption-text">The algorithm frist goes in depth and then backtracks to all unvisited successors!</figcaption></figure>
<p>The whole idea of this algorithm is to go as far as possible from the given starting node searching for the target. In case we get to a node that has no successors, we get back (typically this is done recursively) and we continue with the last vertex that isn’t visited yet.</p>
<p>So basically we have 3 steps:</p>
<ol>
<li>Pick up a vertex that isn&#8217;t visited yet and mark it visited;</li>
<li>Go to its first non-visited successor and mark it visited;</li>
<li>If all the successors of the vertex are already visited or it doesn&#8217;t have successors &#8211; go back to its parent;</li>
</ol>
<h2>Code</h2>
<p>The following <a href="/category/php/" title="PHP on Stoimen.com">PHP</a> code implements the depth-first search. The key point is the recursion in the method depthFirst.</p>
<pre lang="PHP">
class Graph 
{
    protected $_len = 0;
    protected $_g = array();
    protected $_visited = array();
    
    public function __construct()
    {
        $this->_g = array(
            array(0, 1, 1, 0, 0, 0),
            array(1, 0, 0, 1, 0, 0),
            array(1, 0, 0, 1, 1, 1),
            array(0, 1, 1, 0, 1, 0),
            array(0, 0, 1, 1, 0, 1),
            array(0, 0, 1, 0, 1, 0),
        );
        
        $this->_len = count($this->_g);
        
        $this->_initVisited();
    }
    
    protected function _initVisited()
    {
        for ($i = 0; $i < $this->_len; $i++) {
            $this->_visited[$i] = 0;
        }
    }
    
    public function depthFirst($vertex)
    {
        $this->_visited[$vertex] = 1;
    
        echo $vertex . "\n";
        
        for ($i = 0; $i < $this->_len; $i++) {
            if ($this->_g[$vertex][$i] == 1 && !$this->_visited[$i]) {
                $this->depthFirst($i);
            }
        }
    }
}

$g = new Graph();
// 2 0 1 3 4 5
$g->depthFirst(2);
</pre>
<h2>Complexity</h2>
<p>By using an adjacency matrix we need n<sup>2</sup> space for a graph with <strong>n</strong> vertices. We also use an additional array to mark visited vertices, which requires additional space of <strong>n</strong>! Thus the space complexity is O(n<sup>2</sup>).</p>
<p>When it comes to time complexity since we have a recursion and we try visiting all the vertices on each step, the worst-case time is yet again O(n<sup>2</sup>)!</p>
<h2>Application</h2>
<p>This graph-walk algorithm can be very useful when solving some specific tasks like finding the shortest/longest paths in a graph. Although <a href="/2012/09/10/computer-algorithms-graph-breadth-first-search/" title="Computer Algorithms: Graph Breadth First Search">BFS</a> and DFS aren&#8217;t the only methods of walking through a graph, they are considered the two main algorithms of that kind. This is important in order to solve graph-based problems.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/09/24/computer-algorithms-graph-best-first-search/" rel="bookmark" title="Computer Algorithms: Graph Best-First Search">Computer Algorithms: Graph Best-First Search </a></li>
<li><a href="/2012/09/10/computer-algorithms-graph-breadth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Breadth First Search">Computer Algorithms: Graph Breadth First Search </a></li>
<li><a href="/2012/10/15/computer-algorithms-dijkstra-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Dijkstra Shortest Path in a Graph">Computer Algorithms: Dijkstra Shortest Path in a Graph </a></li>
<li><a href="/2012/10/08/computer-algorithms-shortest-path-in-a-graph/" rel="bookmark" title="Computer Algorithms: Shortest Path in a Graph">Computer Algorithms: Shortest Path in a Graph </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/09/17/computer-algorithms-graph-depth-first-search/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Friday Algorithms: Input Data and Complexity</title>
		<link>/2010/09/03/friday-algorithms-input-data-and-complexity/</link>
		<comments>/2010/09/03/friday-algorithms-input-data-and-complexity/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 09:32:47 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[algorithm complexity]]></category>
		<category><![CDATA[Computational complexity theory]]></category>
		<category><![CDATA[Context of computational complexity]]></category>
		<category><![CDATA[In-place algorithm]]></category>
		<category><![CDATA[input data]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Sorting algorithms]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Theoretical computer science]]></category>

		<guid isPermaLink="false">/?p=1955</guid>
		<description><![CDATA[Size of .. As we all know most of the cases a program execution time depends most from the input data. As I wrote in my post the degree of the input data estimates the algorithm complexity. Of course 3*n² is a bit faster than 5*n², but in general both functions are similar and have &#8230; <a href="/2010/09/03/friday-algorithms-input-data-and-complexity/" class="more-link">Continue reading <span class="screen-reader-text">Friday Algorithms: Input Data and Complexity</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/16/friday-algorithms-a-data-structure-javascript-stack/" rel="bookmark" title="Friday Algorithms: A Data Structure: JavaScript Stack">Friday Algorithms: A Data Structure: JavaScript Stack </a></li>
<li><a href="/2012/01/30/computer-algorithms-data-compression-with-relative-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Relative Encoding">Computer Algorithms: Data Compression with Relative Encoding </a></li>
<li><a href="/2012/02/06/computer-algorithms-data-compression-with-prefix-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Prefix Encoding">Computer Algorithms: Data Compression with Prefix Encoding </a></li>
<li><a href="/2012/02/27/computer-algorithms-shell-sort/" rel="bookmark" title="Computer Algorithms: Shell Sort">Computer Algorithms: Shell Sort </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Size of ..</h2>
<p>As we all know most of the cases a program execution time depends most from the input data. As I wrote in my <a title="Algorithm complexity and estimation" href="/2010/08/29/beginning-algorithm-complexity-and-estimation/" target="_blank">post</a> the degree of the input data estimates the algorithm complexity.</p>
<p>Of course 3*n² is a bit faster than 5*n², but in general both functions are similar and have the same complexity. In this case we tend to say that the size of the input data is n.</p>
<figure id="attachment_1961" style="width: 430px" class="wp-caption alignnone"><a href="/wp-content/uploads/2010/09/input-data.jpg"><img class="size-full wp-image-1961 " title="input-data" src="/wp-content/uploads/2010/09/input-data.jpg" alt="Size of the input data" width="430" height="137" srcset="/wp-content/uploads/2010/09/input-data.jpg 430w, /wp-content/uploads/2010/09/input-data-300x95.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a><figcaption class="wp-caption-text">The size of the input data affects the complexity of the algorithm</figcaption></figure>
<p>It is easy to bind this to arrays or other simple data structures. For example when sorting an array with n elements, the size of the input data is n, but sometimes there is not such an obvious relation between an algorithm and the size of the input data.</p>
<p>For instance when we&#8217;ve to search a path into a graph, than maybe the best way to describe the input data is by summing both the number of vertices and the number of edges.</p>
<h2>Conclusion</h2>
<figure id="attachment_1963" style="width: 430px" class="wp-caption alignnone"><a href="/wp-content/uploads/2010/09/think.jpg"><img class="size-full wp-image-1963 " title="Think, think, think" src="/wp-content/uploads/2010/09/think.jpg" alt="Think, think, think" width="430" height="286" srcset="/wp-content/uploads/2010/09/think.jpg 430w, /wp-content/uploads/2010/09/think-300x199.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a><figcaption class="wp-caption-text">Think, think, think</figcaption></figure>
<p>However this is important when dealing with algorithms, because a mistake in the estimation of the input data size will result in wrong algorithm estimation at all</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/16/friday-algorithms-a-data-structure-javascript-stack/" rel="bookmark" title="Friday Algorithms: A Data Structure: JavaScript Stack">Friday Algorithms: A Data Structure: JavaScript Stack </a></li>
<li><a href="/2012/01/30/computer-algorithms-data-compression-with-relative-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Relative Encoding">Computer Algorithms: Data Compression with Relative Encoding </a></li>
<li><a href="/2012/02/06/computer-algorithms-data-compression-with-prefix-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Prefix Encoding">Computer Algorithms: Data Compression with Prefix Encoding </a></li>
<li><a href="/2012/02/27/computer-algorithms-shell-sort/" rel="bookmark" title="Computer Algorithms: Shell Sort">Computer Algorithms: Shell Sort </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/09/03/friday-algorithms-input-data-and-complexity/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
