
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://ricefriedegg.com:80/mediawiki/index.php?action=history&amp;feed=atom&amp;title=BFS</id>
	<title>BFS - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://ricefriedegg.com:80/mediawiki/index.php?action=history&amp;feed=atom&amp;title=BFS"/>
	<link rel="alternate" type="text/html" href="http://ricefriedegg.com:80/mediawiki/index.php?title=BFS&amp;action=history"/>
	<updated>2026-05-26T21:49:39Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>http://ricefriedegg.com:80/mediawiki/index.php?title=BFS&amp;diff=411&amp;oldid=prev</id>
		<title>Rice: /* Analysis */</title>
		<link rel="alternate" type="text/html" href="http://ricefriedegg.com:80/mediawiki/index.php?title=BFS&amp;diff=411&amp;oldid=prev"/>
		<updated>2024-03-20T17:34:49Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Analysis&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 17:34, 20 March 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l37&quot;&gt;Line 37:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 37:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Therefore, the overall runtime complexity would be O(V + E).&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Therefore, the overall runtime complexity would be O(V + E).&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;= Application =&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;BFS is one solution to the [[Shortest Path Problem|shortest path problem]]. Given an &#039;&#039;unweighted&#039;&#039; graph, BFS can find the &#039;&#039;shortest number of jumps&#039;&#039; from one node to every other node (the distance array being kept track of). Notably, BFS does not work for weighted graphs due to each edge not being valued the same.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:Algorithms]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:Algorithms]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key my_wiki:diff:1.41:old-410:rev-411:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Rice</name></author>
	</entry>
	<entry>
		<id>http://ricefriedegg.com:80/mediawiki/index.php?title=BFS&amp;diff=410&amp;oldid=prev</id>
		<title>Rice: Created page with &quot;{{Infobox Algorithm|runtime=O(V+E)|class=Graph Algorithm}}  &#039;&#039;&#039;Breadth-first search (BFS)&#039;&#039;&#039; is a graph traversal algorithm.  = Approach = At the core of BFS is a &#039;&#039;priority queue&#039;&#039; of unvisited nodes&#039;&#039;.&#039;&#039;  # At the start, the source node has a distance of 0, whereas the rest has infinite distance. The source node is pushed into the queue. # The queue is popped as the first node in the queue is visited. # Upon visiting a node, if the node&#039;s distance is infinity (i.e....&quot;</title>
		<link rel="alternate" type="text/html" href="http://ricefriedegg.com:80/mediawiki/index.php?title=BFS&amp;diff=410&amp;oldid=prev"/>
		<updated>2024-03-20T17:24:38Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{Infobox Algorithm|runtime=O(V+E)|class=&lt;a href=&quot;/mediawiki/index.php?title=Graph_Algorithm&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Graph Algorithm (page does not exist)&quot;&gt;Graph Algorithm&lt;/a&gt;}}  &amp;#039;&amp;#039;&amp;#039;Breadth-first search (BFS)&amp;#039;&amp;#039;&amp;#039; is a graph traversal algorithm.  = Approach = At the core of BFS is a &amp;#039;&amp;#039;priority queue&amp;#039;&amp;#039; of unvisited nodes&amp;#039;&amp;#039;.&amp;#039;&amp;#039;  # At the start, the source node has a distance of 0, whereas the rest has infinite distance. The source node is pushed into the queue. # The queue is popped as the first node in the queue is visited. # Upon visiting a node, if the node&amp;#039;s distance is infinity (i.e....&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox Algorithm|runtime=O(V+E)|class=[[Graph Algorithm]]}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Breadth-first search (BFS)&amp;#039;&amp;#039;&amp;#039; is a graph traversal algorithm.&lt;br /&gt;
&lt;br /&gt;
= Approach =&lt;br /&gt;
At the core of BFS is a &amp;#039;&amp;#039;priority queue&amp;#039;&amp;#039; of unvisited nodes&amp;#039;&amp;#039;.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
# At the start, the source node has a distance of 0, whereas the rest has infinite distance. The source node is pushed into the queue.&lt;br /&gt;
# The queue is popped as the first node in the queue is visited.&lt;br /&gt;
# Upon visiting a node, if the node&amp;#039;s distance is infinity (i.e. it hasn&amp;#039;t been visited and isn&amp;#039;t in the queue), BFS adds the node to the queue.&lt;br /&gt;
# The next node is popped and visited until no nodes remain.&lt;br /&gt;
&lt;br /&gt;
= Implementation =&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
BFS(G, s):&lt;br /&gt;
    # Initialize&lt;br /&gt;
    n = len(V)&lt;br /&gt;
    let Q be a FIFO queue&lt;br /&gt;
    let d be distance array of length n&lt;br /&gt;
    for each vertex u in V:&lt;br /&gt;
        d[u] = infty&lt;br /&gt;
    d[s] = 0&lt;br /&gt;
    Q.enqueue(s);&lt;br /&gt;
&lt;br /&gt;
    while Q is not empty:&lt;br /&gt;
        visiting = Q.pop();&lt;br /&gt;
        for each node in adj[visiting]:&lt;br /&gt;
            if d[node] = infty:&lt;br /&gt;
                d[node] = d[visiting] + 1&lt;br /&gt;
                Q.enqueue(node)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Analysis =&lt;br /&gt;
Since the nodes only enter the queue when their distance is infinity, and their distance gets updated as they enter the queue, &amp;#039;&amp;#039;each node only enters the queue once&amp;#039;&amp;#039;. O(V).&lt;br /&gt;
&lt;br /&gt;
Inside each iteration, the adjacency list of each node is looked at, which takes O(E).&lt;br /&gt;
&lt;br /&gt;
Therefore, the overall runtime complexity would be O(V + E).&lt;br /&gt;
[[Category:Algorithms]]&lt;/div&gt;</summary>
		<author><name>Rice</name></author>
	</entry>
</feed>