<?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>search start &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/search-start/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 Breadth First Search</title>
		<link>/2012/09/10/computer-algorithms-graph-breadth-first-search/</link>
		<comments>/2012/09/10/computer-algorithms-graph-breadth-first-search/#comments</comments>
		<pubDate>Sun, 09 Sep 2012 21:52:49 +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[Connected component]]></category>
		<category><![CDATA[Depth-first search]]></category>
		<category><![CDATA[Dijkstra's algorithm]]></category>
		<category><![CDATA[Graph]]></category>
		<category><![CDATA[graph algorithms]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[search algorithms]]></category>
		<category><![CDATA[search start]]></category>
		<category><![CDATA[search walks]]></category>
		<category><![CDATA[Theoretical computer science]]></category>

		<guid isPermaLink="false">/?p=3338</guid>
		<description><![CDATA[Introduction Since we already know how to represent graphs, we can go further for some very simple approaches of walking through them. Passing by all the vertices of a graph is a fundamental technique for most of the graph algorithms, such as finding shortest/longest paths, etc. First thing to note is that graphs are not &#8230; <a href="/2012/09/10/computer-algorithms-graph-breadth-first-search/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Graph Breadth First Search</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/09/17/computer-algorithms-graph-depth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Depth-First Search">Computer Algorithms: Graph Depth-First Search </a></li>
<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/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>Since we already know <a href="/2012/08/31/computer-algorithms-graphs-and-their-representation/" title="Computer Algorithms: Graphs and their Representation">how to represent graphs</a>, we can go further for some very simple approaches of walking through them. Passing by all the vertices of a graph is a fundamental technique for most of the graph algorithms, such as finding shortest/longest paths, etc.</p>
<p>First thing to note is that graphs are not trees, in most of the cases, so walking through them can&#8217;t start from a root, as we do with trees. What we must do first is to decide from where to start – in other words &#8211; choosing a starting vertex. </p>
<figure id="attachment_3343" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/09/1.-BFS-Choosing-a-Starting-Point.png"><img src="/wp-content/uploads/2012/09/1.-BFS-Choosing-a-Starting-Point.png" alt="BFS Choosing a Starting Point" title="BFS Choosing a Starting Point" width="620" height="399" class="size-full wp-image-3343" srcset="/wp-content/uploads/2012/09/1.-BFS-Choosing-a-Starting-Point.png 620w, /wp-content/uploads/2012/09/1.-BFS-Choosing-a-Starting-Point-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">It&#8217;s clear that depending on the starting point we can get different passes through the graph. Thus choosing a starting point can be very important for our algorithm!</figcaption></figure>
<p>After that we need to know how to proceed. There are two approaches mostly known as “breadth first” and “depth first” search. While depth first search start from a vertex and goes as far as possible, then walks back and passes through vertices that haven’t been visited yet, breath first search is an approach of passing through all the neighbors of the node first, and then go to the next level.<br />
<span id="more-3338"></span></p>
<h2>Overview</h2>
<p>We can thing of breadth first search as a “wave” walk through the graph. In other words we go level by level, as shown on the picture below.</p>
<figure id="attachment_3344" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/09/2.-BFS-Wave.png"><img src="/wp-content/uploads/2012/09/2.-BFS-Wave.png" alt="BFS Wave" title="BFS Wave" width="620" height="399" class="size-full wp-image-3344" srcset="/wp-content/uploads/2012/09/2.-BFS-Wave.png 620w, /wp-content/uploads/2012/09/2.-BFS-Wave-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">For this very specific graph on the picture we can see how breadth first search walks through the graph level by level!</figcaption></figure>
<p>Initially we mark all vertices as unvisited. A common approach is to create an empty queue where we put the vertices level by level, starting with the initial vertex.</p>
<figure id="attachment_3342" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/09/3.-BFS-Using-a-Queue.png"><img src="/wp-content/uploads/2012/09/3.-BFS-Using-a-Queue.png" alt="BFS Using a Queue" title="BFS Using a Queue" width="620" height="399" class="size-full wp-image-3342" srcset="/wp-content/uploads/2012/09/3.-BFS-Using-a-Queue.png 620w, /wp-content/uploads/2012/09/3.-BFS-Using-a-Queue-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Using a queue is a typical approach for breadth first search! However this requires more space!</figcaption></figure>
<h2>Code</h2>
<p>This simple approach is fairly easy to implement. Here’s the <a href="/category/php/" title="PHP on Stoimen.com">PHP</a> implementation in few lines of code.</p>
<pre lang="PHP">
<?php

$g = array(
    0 => array(0, 1, 1, 0, 0, 0),
    1 => array(1, 0, 0, 1, 0, 0),
    2 => array(1, 0, 0, 1, 0, 0),
    3 => array(0, 1, 1, 0, 1, 0),
    4 => array(0, 0, 0, 1, 0, 1),
    5 => array(0, 0, 0, 0, 1, 0),
);

function init(&$visited, &$graph) 
{
    foreach ($graph as $key => $vertex) {
        $visited[$key] = 0;
    }
}

function breadth_first(&$graph, $start, $visited)
{
    // create an empty queue
    $q = array();
    
    // initially enqueue only the starting vertex
    array_push($q, $start);
    $visited[$start] = 1;
    echo $start . "\n";
    
    while (count($q)) {
        $t = array_shift($q);
        
        foreach ($graph[$t] as $key => $vertex) {
            if (!$visited[$key] && $vertex == 1) {
                $visited[$key] = 1;
                array_push($q, $key);
                echo $key . "\t";
            }
        }
        echo "\n";
    }
}

$visited = array();
init($visited, $g);
breadth_first($g, 2, $visited);
</pre>
<h2>Complexity</h2>
<p>The complexity of this algorithm clearly is O(n<sup>2</sup>).</p>
<h2>Application</h2>
<p>As I said breadth first and depth first searches are used in many practical cases, as finding shortest/minimal paths etc. That is why understanding these basic principles of walking through a graph is crucial for other, more complex, graph algorithms.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/09/17/computer-algorithms-graph-depth-first-search/" rel="bookmark" title="Computer Algorithms: Graph Depth-First Search">Computer Algorithms: Graph Depth-First Search </a></li>
<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/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/10/computer-algorithms-graph-breadth-first-search/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
