<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.algo.informatik.tu-darmstadt.de/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Flomm</id>
	<title>Algowiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.algo.informatik.tu-darmstadt.de/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Flomm"/>
	<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/Special:Contributions/Flomm"/>
	<updated>2026-05-10T20:21:07Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.38.4</generator>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Category:Minimal_Spanning_Tree&amp;diff=1800</id>
		<title>Category:Minimal Spanning Tree</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Category:Minimal_Spanning_Tree&amp;diff=1800"/>
		<updated>2014-11-07T15:49:38Z</updated>

		<summary type="html">&lt;p&gt;Flomm: /* Output */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
[[File:min_span_tree.jpg|400px]]&lt;br /&gt;
&lt;br /&gt;
The red colored edges are the minimal spanning tree of this graph.&lt;br /&gt;
&lt;br /&gt;
== Input ==&lt;br /&gt;
&lt;br /&gt;
* A undirected connected graph &amp;lt;math&amp;gt;G=(V,E)&amp;lt;/math&amp;gt;&lt;br /&gt;
* A weight &amp;lt;math&amp;gt;w(u,v)&amp;lt;/math&amp;gt; for each edge &amp;lt;math&amp;gt;(u,v) \in E&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Output ==&lt;br /&gt;
&lt;br /&gt;
A acylic subset &amp;lt;math&amp;gt;T \subseteq E&amp;lt;/math&amp;gt; in which &amp;lt;math&amp;gt;w(T) = \sum_{(u,v) \in T} w(u,v)&amp;lt;/math&amp;gt; is minimal.&lt;br /&gt;
&lt;br /&gt;
== Generic algorihm ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Generic-MST(G, w)&lt;br /&gt;
 1 ''A'' &amp;amp;larr; &amp;amp;empty;&lt;br /&gt;
 2 '''while''' ''A'' is not a spanning tree&lt;br /&gt;
 3     '''do''' determine a edge ''(u,v)''&lt;br /&gt;
 4              ''A'' &amp;amp;larr; ''A'' &amp;amp;cup; ''{(u,v)}''&lt;br /&gt;
 5 '''return''' ''A''&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Known algorithms ==&lt;br /&gt;
&lt;br /&gt;
* [[Kruskal]]&lt;br /&gt;
* [[Prim]]&lt;/div&gt;</summary>
		<author><name>Flomm</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Category:Minimal_Spanning_Tree&amp;diff=1799</id>
		<title>Category:Minimal Spanning Tree</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Category:Minimal_Spanning_Tree&amp;diff=1799"/>
		<updated>2014-11-07T15:47:44Z</updated>

		<summary type="html">&lt;p&gt;Flomm: Created page with &amp;quot;== Introduction ==  400px  The red colored edges are the minimal spanning tree of this graph.  == Input ==  * A undirected connected graph &amp;lt;math&amp;gt;G=(...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
[[File:min_span_tree.jpg|400px]]&lt;br /&gt;
&lt;br /&gt;
The red colored edges are the minimal spanning tree of this graph.&lt;br /&gt;
&lt;br /&gt;
== Input ==&lt;br /&gt;
&lt;br /&gt;
* A undirected connected graph &amp;lt;math&amp;gt;G=(V,E)&amp;lt;/math&amp;gt;&lt;br /&gt;
* A weight &amp;lt;math&amp;gt;w(u,v)&amp;lt;/math&amp;gt; for each edge &amp;lt;math&amp;gt;(u,v) \in E&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Output ==&lt;br /&gt;
&lt;br /&gt;
A acylic subset &amp;lt;math&amp;gt;T \subset E&amp;lt;/math&amp;gt; in which &amp;lt;math&amp;gt;w(T) = \sum_{(u,v) \in T} w(u,v)&amp;lt;/math&amp;gt; is minimal.&lt;br /&gt;
&lt;br /&gt;
== Generic algorihm ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 Generic-MST(G, w)&lt;br /&gt;
 1 ''A'' &amp;amp;larr; &amp;amp;empty;&lt;br /&gt;
 2 '''while''' ''A'' is not a spanning tree&lt;br /&gt;
 3     '''do''' determine a edge ''(u,v)''&lt;br /&gt;
 4              ''A'' &amp;amp;larr; ''A'' &amp;amp;cup; ''{(u,v)}''&lt;br /&gt;
 5 '''return''' ''A''&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Known algorithms ==&lt;br /&gt;
&lt;br /&gt;
* [[Kruskal]]&lt;br /&gt;
* [[Prim]]&lt;/div&gt;</summary>
		<author><name>Flomm</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=File:Min_span_tree.jpg&amp;diff=1798</id>
		<title>File:Min span tree.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=File:Min_span_tree.jpg&amp;diff=1798"/>
		<updated>2014-11-07T15:36:04Z</updated>

		<summary type="html">&lt;p&gt;Flomm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Flomm</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Strongly_connected_components&amp;diff=1766</id>
		<title>Strongly connected components</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Strongly_connected_components&amp;diff=1766"/>
		<updated>2014-11-04T15:23:01Z</updated>

		<summary type="html">&lt;p&gt;Flomm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Input ==&lt;br /&gt;
&lt;br /&gt;
A [[Basic graph definitions|directed graph]] &amp;lt;math&amp;gt;G=(V,A)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Output ==&lt;br /&gt;
&lt;br /&gt;
A set of sets of nodes. Each set of nodes contains exactly the nodes of one [[Basic graph definitions#Connectedness|SCC]]. The correspondence between the [[Basic graph definitions#Connectedness|SCC]] and these sets of nodes is one-to-one.&lt;br /&gt;
&lt;br /&gt;
== Known algorithms ==&lt;br /&gt;
&lt;br /&gt;
# [[Kosaraju]]&lt;/div&gt;</summary>
		<author><name>Flomm</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Depth-first_search&amp;diff=1655</id>
		<title>Depth-first search</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Depth-first_search&amp;diff=1655"/>
		<updated>2014-10-30T14:13:28Z</updated>

		<summary type="html">&lt;p&gt;Flomm: Added stack implementation pseudocode&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Algorithms]]&lt;br /&gt;
[[Category:Search Algorithms]]&lt;br /&gt;
[[Category:Tree Algorithms]]&lt;br /&gt;
[[Category:Graph Traversal]]&lt;br /&gt;
&lt;br /&gt;
== General information ==&lt;br /&gt;
&lt;br /&gt;
'''Algorithmic problem:''' [[Graph traversal]]&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' loop&lt;br /&gt;
&lt;br /&gt;
== Abstract view ==&lt;br /&gt;
&lt;br /&gt;
'''Definitions:'''&lt;br /&gt;
# For each node, an arbitrary but fixed ordering of the outgoing arcs is assumed. An arc &amp;lt;math&amp;gt;(v,w)&amp;lt;/math&amp;gt; preceding an arc &amp;lt;math&amp;gt;(v,w')&amp;lt;/math&amp;gt; in this ordering is called '''lexicographically smaller''' than &amp;lt;math&amp;gt;(v,w)&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Let &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;p'&amp;lt;/math&amp;gt; be two paths that start from the same node &amp;lt;math&amp;gt;v\in V&amp;lt;/math&amp;gt;, but may or may not have the same endnode. Let &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; be the last common node such that the subpaths of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;p'&amp;lt;/math&amp;gt; from &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; up to &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; are identical (possibly &amp;lt;math&amp;gt;v=w&amp;lt;/math&amp;gt;). If the next arc of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; is lexicographically smaller than the next arc of &amp;lt;math&amp;gt;p'&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; is said to be '''lexicograpically smaller''' than &amp;lt;math&amp;gt;p'&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Note that the lexicographically smallest path from &amp;lt;math&amp;gt;v\in V&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;w\in V&amp;lt;/math&amp;gt; is well defined and unique. With respect to a starting node &amp;lt;math&amp;gt;s\in V&amp;lt;/math&amp;gt;, a node &amp;lt;math&amp;gt;v\in V&amp;lt;/math&amp;gt; is '''lexicographically smaller''' than &amp;lt;math&amp;gt;w\in V&amp;lt;/math&amp;gt; if the lexicographically smallest path from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is lexicographically smaller than the lexicographically smallest path from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt;.&lt;br /&gt;
# In all of the above cases, the reverse relation is called '''lexicographically larger'''.&lt;br /&gt;
# A node &amp;lt;math&amp;gt;v\in V&amp;lt;/math&amp;gt; is '''lexicographically smaller''' (resp., '''lexicograpically larger''') than a path &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; if the lexicographically smallest path from the start node of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is lexicographically smaller (resp., larger) than &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt;. (Note the asymmetry: In both cases, the lexicographically ''smallest'' path to &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is used.)&lt;br /&gt;
# In all cases, we also say '''precedes''' and '''succeeds''', respectively, instead of &amp;quot;is lexicograpically smaller/larger&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
'''Additional output:'''&lt;br /&gt;
# Each node has two Boolean labels with semantics, &amp;quot;is seen&amp;quot; and &amp;quot;is finished&amp;quot;.&lt;br /&gt;
# An arborescence &amp;lt;math&amp;gt;A=(V',A')&amp;lt;/math&amp;gt; rooted at &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;V'\subseteq V&amp;lt;/math&amp;gt; is the set of all nodes reachable from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; (including &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt;). For each node &amp;lt;math&amp;gt;v\in V'&amp;lt;/math&amp;gt;, the path in &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; is the lexicographically smallest &amp;lt;math&amp;gt;(s,v)&amp;lt;/math&amp;gt;-path in &amp;lt;math&amp;gt;G&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Specific characteristic:'''&lt;br /&gt;
The nodes may be returned either in lexicographic order or (alternatively or simultaneously) in an order that fulfills the following property:&lt;br /&gt;
Let &amp;lt;math&amp;gt;v,w\in V&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is seen before &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt;. If there is a path from &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; is finished prior to &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Auxiliary data:'''&lt;br /&gt;
# A stack &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; whose elements are nodes in &amp;lt;math&amp;gt;V&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Each node  has a '''current arc''' &amp;lt;math&amp;gt;a_v\in V&amp;lt;/math&amp;gt;, which is either void or an outgoing arc &amp;lt;math&amp;gt;a_v=(v,w)&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Invariant:''' &lt;br /&gt;
Before and after each iteration:&lt;br /&gt;
# &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; forms a path &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; from the start node &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; to some other node, that is, the order of the nodes on &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; is the order in which they appear in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; (start node &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; at the bottom of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;).&lt;br /&gt;
# For each node not yet seen, the current arc is the first arc (or void if the node has no outgoing arcs).&lt;br /&gt;
# For each node &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt;:&lt;br /&gt;
## If there are arcs &amp;lt;math&amp;gt;(v,w)\in A&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; is not yet seen, the current arc equals or precedes the first such arc.&lt;br /&gt;
## The subpath of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; from the start node &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is the lexicographically first &amp;lt;math&amp;gt;(s,v)&amp;lt;/math&amp;gt;-path.&lt;br /&gt;
# The nodes on &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; are seen but not finished. Let &amp;lt;math&amp;gt;p+a&amp;lt;/math&amp;gt; denote the concatenation of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; with the current arc &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; of the last node of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt;. The nodes that are lexicographically smaller than &amp;lt;math&amp;gt;p+a&amp;lt;/math&amp;gt; are seen and finished, and the nodes that lexicographically succeed &amp;lt;math&amp;gt;p+a&amp;lt;/math&amp;gt; are neither seen nor finished. (Note that nothing is said about the head of &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' Either one node is finished or the current arc of one node is moved forward.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' &amp;lt;math&amp;gt;S=\emptyset&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Induction basis ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' No node is finished. The start node &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; is seen, no other node is seen. The start node is the only element of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. The current arc of each node is its first outgoing arc. If the nodes are to be returned in lexicographic order, the start node &amp;lt;math&amp;gt;&amp;lt;/math&amp;gt; is, initially, the only member of the output sequence; otherwise, the initial output sequence is empty. Arborescence &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; is initialized so as to contain &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; and nothing else.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Obvious.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Obvious.&lt;br /&gt;
&lt;br /&gt;
== Induction step ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:'''&lt;br /&gt;
# Let &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; be the last node of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; (=the top element of &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;).&lt;br /&gt;
# While the current arc of &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is not void and while the head of the current arc is labeled as seen: Move the current arc one step forward.&lt;br /&gt;
# If the current arc of &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; is not void, say, &amp;lt;math&amp;gt;a_v=(v,w)&amp;lt;/math&amp;gt;:&lt;br /&gt;
## Insert &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;(v,w)&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;.&lt;br /&gt;
## Push &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;.&lt;br /&gt;
## Label &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; as seen.&lt;br /&gt;
## If the output order is the lexicographic one: Append &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; to the output sequence.&lt;br /&gt;
# Otherwise:&lt;br /&gt;
## Remove &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; from &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;&lt;br /&gt;
## Label &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; as finished.&lt;br /&gt;
## If the output order is '''not''' the lexicographic one: Put &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; in the output sequence.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Obvious.&lt;br /&gt;
&lt;br /&gt;
'''Proof:'''&lt;br /&gt;
The loop ''variant'' is obviously fulfilled.&lt;br /&gt;
&lt;br /&gt;
The first point of the ''invariant'' is obviously fulfilled, too. The second point follows from the fact that the current arc of a node is initialized to be the node's very first outgoing arc and only changed after the node is labeled as ''seen''. Point 3.1 follows from the fact that the current arc never skips an arc that points to an unseen node.&lt;br /&gt;
&lt;br /&gt;
For point 3.2, let &amp;lt;math&amp;gt;w\neq v&amp;lt;/math&amp;gt; be the last node on &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;p'&amp;lt;/math&amp;gt; such that both paths are identical from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; (possibly, &amp;lt;math&amp;gt;w=s&amp;lt;/math&amp;gt;). Further, let &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;u'&amp;lt;/math&amp;gt; be the immediate successors of &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;p'&amp;lt;/math&amp;gt;, respectively. Then &amp;lt;math&amp;gt;u'&amp;lt;/math&amp;gt; has been seen before &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt;. Note that &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; has not been seen earlier than &amp;lt;math&amp;gt;u&amp;lt;/math&amp;gt; (in fact, later, unless &amp;lt;math&amp;gt;v=u&amp;lt;/math&amp;gt;). In summary, &amp;lt;math&amp;gt;u'&amp;lt;/math&amp;gt; has been seen before &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;. Since there is a path from &amp;lt;math&amp;gt;u'&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;, the correctness proof [[#Correctness|below]] will prove point 3.2.&lt;br /&gt;
&lt;br /&gt;
When a node is pushed on &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, it is neither seen nor finished immediately before that iteration and then labeled as seen in that iteration. The node is finished when it leaves &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Both facts together give the first sentence of point 4. The other statements of point 4 follow from the observation that the concatenation of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; with the current arc of the endnode of &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; increases lexicographically in each iteration.&lt;br /&gt;
&lt;br /&gt;
== Correctness ==&lt;br /&gt;
&lt;br /&gt;
It is easy to see that each operation of the algorithm is well defined. Due to the variant, the loop terminates after a finite number of steps. Immediately before the last iteration, &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; consists of the start node &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; only, and the current arc of &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; is void. Therefore, ''all'' nodes reachable from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; except for &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; itself are lexicographically smaller than &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; at that moment. Due to point 4 of the invariant, all of these nodes are finished. In the last iteration, &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; is finished as well.&lt;br /&gt;
&lt;br /&gt;
So, it remains to show that the specific characteristic is fulfilled in both cases.&lt;br /&gt;
Due to point 3.2 of the invariant, a node is seen for the first time via its lexicographically smallest path from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt;. Since the current path increases lexicographically in each iteration, the nodes are labeled as seen in lexicographic order. So consider the second, the non-lexicographic case.&lt;br /&gt;
Let &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; be seen before &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; and assume there is a path from &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt;. We have to show that &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; is finished prior to &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;. Let &amp;lt;math&amp;gt;p'&amp;lt;/math&amp;gt; denote the lexicographically smallest &amp;lt;math&amp;gt;(v,w)&amp;lt;/math&amp;gt;-path. There is a stage in which this path is a subpath of the current path &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt;. Clearly, &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; cannot be removed from &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt; before &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt;. This proves the claim.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
&lt;br /&gt;
'''Statement:''' The asymptotic complexity is in &amp;lt;math&amp;gt;\Theta(|V|+|A|)&amp;lt;/math&amp;gt; in the worst case.&lt;br /&gt;
&lt;br /&gt;
'''Proof:'''&lt;br /&gt;
For every node reachable from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; (including &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt;), the algorithm processes each of its outgoing arcs exactly one. And from each of these nodes, the algorithm goes backwards exactly once. Obviously, each of these steps requires a constant number of operations.&lt;br /&gt;
&lt;br /&gt;
== Remark ==&lt;br /&gt;
&lt;br /&gt;
Alternatively, DFS could be implemented as a recursive procedure. However, this excludes the option to implement DFS as an iterator, which means to turn the loop inside-out (cf. remarks on [[Graph traversal|graph traversal]]).&lt;br /&gt;
&lt;br /&gt;
== Pseudocode recursive implementation == &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== DFS(''G'') ====&lt;br /&gt;
:'''for''' each vertex ''u'' &amp;amp;isin; ''V'' [''G''] &lt;br /&gt;
::'''do''' color[''u''] &amp;amp;larr; WHITE&lt;br /&gt;
::: ''&amp;amp;pi;''[''u''] &amp;amp;larr; NIL&lt;br /&gt;
:''time'' &amp;amp;larr; 0&lt;br /&gt;
:: '''do if''' ''color''[''u''] == WHITE&lt;br /&gt;
::: '''then''' DFS-VISiT(''u'')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====DFS-VISIT(''u'')====&lt;br /&gt;
: ''color''[''u''] &amp;amp;larr; GRAY   &lt;br /&gt;
: ''time'' &amp;amp;larr; ''time'' + 1&lt;br /&gt;
: ''d''[''u''] &amp;amp;larr; ''time''&lt;br /&gt;
: '''for''' each ''v'' &amp;amp;isin; ''Adj''[''u'']&lt;br /&gt;
:: '''do if''' ''color''[''v''] = WHITE&lt;br /&gt;
::: ''' then ''' &amp;amp;pi; [''v''] &amp;amp;larr; ''u''&lt;br /&gt;
:::: DFS-VISIT(''v'')&lt;br /&gt;
:''color''[''u''] &amp;amp;larr; BLACK&lt;br /&gt;
:''f''[''u''] &amp;amp;larr; ''time'' &amp;amp;larr; ''time'' + 1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Pseudocode stack implementation ==&lt;br /&gt;
&lt;br /&gt;
====DFS(''s'')====&lt;br /&gt;
: ''S = new Stack()''&lt;br /&gt;
: ''s.IsSeen = true''&lt;br /&gt;
: ''S.push(s)''&lt;br /&gt;
: '''while''' ''S'' &amp;amp;ne; &amp;amp;empty;&lt;br /&gt;
:: ''n'' = ''S.peek()''&lt;br /&gt;
:: ''a'' = ''(v, w)'' = ''n.nextArc()''&lt;br /&gt;
:: '''if''' ''a'' == ''null''&lt;br /&gt;
::: ''n.IsFinised'' = ''true''&lt;br /&gt;
::: ''S.pop()''&lt;br /&gt;
:: '''else if''' ''w.IsSeen'' == ''false''&lt;br /&gt;
::: ''w.IsSeen'' = ''true''&lt;br /&gt;
::: ''S.push(w)''&lt;/div&gt;</summary>
		<author><name>Flomm</name></author>
	</entry>
	<entry>
		<id>https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Breadth-first_search&amp;diff=1629</id>
		<title>Breadth-first search</title>
		<link rel="alternate" type="text/html" href="https://wiki.algo.informatik.tu-darmstadt.de/index.php?title=Breadth-first_search&amp;diff=1629"/>
		<updated>2014-10-29T13:23:36Z</updated>

		<summary type="html">&lt;p&gt;Flomm: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Algorithms]]&lt;br /&gt;
[[Category:Search Algorithms]]&lt;br /&gt;
[[Category:Tree Algorithms]]&lt;br /&gt;
[[Category:Graph Traversal]]&lt;br /&gt;
&lt;br /&gt;
== General information ==&lt;br /&gt;
&lt;br /&gt;
'''Algorithmic problem:''' [[Graph traversal]]&lt;br /&gt;
&lt;br /&gt;
'''Type of algorithm:''' loop&lt;br /&gt;
&lt;br /&gt;
== Abstract view ==&lt;br /&gt;
&lt;br /&gt;
'''Definition:'''&lt;br /&gt;
On this page, the '''distance''' of a node &amp;lt;math&amp;gt;v\in V&amp;lt;/math&amp;gt; is the minimal number of arcs on a path from the start node &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Specific characteristic:'''&lt;br /&gt;
The nodes are finished in the order of increasing distance (which is not unique, in general).&lt;br /&gt;
&lt;br /&gt;
'''Auxiliary data:'''&lt;br /&gt;
A [[FIFO queue]] &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; whose elements are nodes in &amp;lt;math&amp;gt;V&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Invariant:''' &lt;br /&gt;
Before and after each iteration:&lt;br /&gt;
# There is a '''current distance''' &amp;lt;math&amp;gt;k\in\mathbb{N}&amp;lt;/math&amp;gt;.&lt;br /&gt;
# All nodes with distance at most &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; are already seen.&lt;br /&gt;
# Let &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; denote the current size of &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;. There is &amp;lt;math&amp;gt;\ell\in\{1,\ldots,n\}&amp;lt;/math&amp;gt; such that the first &amp;lt;math&amp;gt;\ell&amp;lt;/math&amp;gt; elements of &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; have distance &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; and the last &amp;lt;math&amp;gt;n-\ell&amp;lt;/math&amp;gt; elements have distance &amp;lt;math&amp;gt;k+1&amp;lt;/math&amp;gt; (in particular, ''all'' elements have distance &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; if, and only if, it is &amp;lt;math&amp;gt;\ell=n&amp;lt;/math&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
'''Variant:''' One node is removed from &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Break condition:''' &amp;lt;math&amp;gt;Q=\emptyset&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Induction basis ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:''' The start node is seen, no other node is seen. The start node is the only element of &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;. Both the output sequence and the arborescence &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt; contain the start node &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt; and nothing else.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Obvious.&lt;br /&gt;
&lt;br /&gt;
'''Proof:''' Obvious.&lt;br /&gt;
&lt;br /&gt;
== Induction step ==&lt;br /&gt;
&lt;br /&gt;
'''Abstract view:'''&lt;br /&gt;
# Extract the first element &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; from &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;.&lt;br /&gt;
# Append &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; to the output sequence.&lt;br /&gt;
# For each outgoing arc &amp;lt;math&amp;gt;(v,w)&amp;lt;/math&amp;gt; of &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; is not yet seen:&lt;br /&gt;
## Insert &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; and &amp;lt;math&amp;gt;(v,w)&amp;lt;/math&amp;gt; in &amp;lt;math&amp;gt;A&amp;lt;/math&amp;gt;.&lt;br /&gt;
## Label &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; as seen.&lt;br /&gt;
## Append &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; to &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''Implementation:''' Obvious.&lt;br /&gt;
&lt;br /&gt;
'''Proof:'''&lt;br /&gt;
The variant is obviously fulfilled. The first point of the invariant is only a notation, so nothing is to show for that.&lt;br /&gt;
&lt;br /&gt;
Let &amp;lt;math&amp;gt;w\in V&amp;lt;/math&amp;gt; not yet seen before the current iteration, and let &amp;lt;math&amp;gt;(v,w)\in A&amp;lt;/math&amp;gt;. The induction hypothesis (second point of the invariant) implies that the distance of &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; is greater than &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;. However, &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; has distance &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;, so the distance of &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; cannot be greater than &amp;lt;math&amp;gt;k+1&amp;lt;/math&amp;gt;. Im summary, this proves the third point of the invariant.&lt;br /&gt;
&lt;br /&gt;
The critical iterations for the second invariant are those where &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; increases. These are exactly the iterations in which &amp;lt;math&amp;gt;\ell=1&amp;lt;/math&amp;gt; immediately before (and, consequently, &amp;lt;math&amp;gt;\ell=n&amp;lt;/math&amp;gt; immediately after) that iteration. All nodes with old distance &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; have been seen and no such node is in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; anymore after the current iteration. Therefore, all nodes &amp;lt;math&amp;gt;w&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;(v,w)\in A&amp;lt;/math&amp;gt; have been seen as well. Clearly, this includes all nodes with old distance &amp;lt;math&amp;gt;k+1&amp;lt;/math&amp;gt;, in other words, the nodes with new distance &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Correctness ==&lt;br /&gt;
&lt;br /&gt;
It is easy to see that each operation of the algorithm is well defined. Due to the variant, the loop terminates after a finite number of steps. Based on a straightforward induction on the iterations, the third invariant ensures that the nodes leave &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; in ascending order of their distances.&lt;br /&gt;
&lt;br /&gt;
== Complexity ==&lt;br /&gt;
&lt;br /&gt;
'''Statement:''' The asymptotic complexity is in &amp;lt;math&amp;gt;\Theta(|V|+|A|)&amp;lt;/math&amp;gt; in the worst case.&lt;br /&gt;
&lt;br /&gt;
'''Proof:'''&lt;br /&gt;
The complexity of each iteration is linear in the number of arcs leaving the current node &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;. Therefore, the complexity of the entire loop is in &amp;lt;math&amp;gt;\Theta(|A|)&amp;lt;/math&amp;gt; in the (worst) case when all nodes are reachable from &amp;lt;math&amp;gt;s&amp;lt;/math&amp;gt;.  The complexity of the initialization is dominated by the initialization of the seen labels of all nodes, which is clearly in &amp;lt;math&amp;gt;\Theta(|V|)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Remark ==&lt;br /&gt;
&lt;br /&gt;
The nodes in the queue form kind of a &amp;quot;frontier line&amp;quot;, which separates the nodes that already left &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; from the nodes that have not yet entered &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; so far.&lt;br /&gt;
More specifically, each path from some seen node to some unseen node contains at least one node that is currently stored in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
To see that, we apply a straightforward induction on the iterations. Let &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; be a path from some seen node outside &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; to some node that is not yet seen immediately after the current iteration. If &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; does not contain &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;, the claim follows from the induction hypothesis. So assume &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; contains &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt;. Let &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; be the last node on &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; that is already seen immediately after the current iteration. If &amp;lt;math&amp;gt;x=v&amp;lt;/math&amp;gt;, the claim follows from the fact that all unseen nodes &amp;lt;math&amp;gt;w\in V&amp;lt;/math&amp;gt; with &amp;lt;math&amp;gt;(v,w)\in A&amp;lt;/math&amp;gt; were put in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt;, so the immediate successor of &amp;lt;math&amp;gt;v&amp;lt;/math&amp;gt; on &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; is in &amp;lt;math&amp;gt;Q&amp;lt;/math&amp;gt; now. Otherwise, the claim follows from the induction hypothesis again.&lt;br /&gt;
&lt;br /&gt;
== Pseudocode == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 BFS(''G,s'')&lt;br /&gt;
  1  '''for''' each vertex ''u'' &amp;amp;isin; ''G.V'' - {''s''}&lt;br /&gt;
  2       ''u.color'' = WHITE&lt;br /&gt;
  3       ''u.d'' = &amp;amp;infin;&lt;br /&gt;
  4       ''u.''&amp;amp;pi; = NIL&lt;br /&gt;
  5  ''s.color'' = GRAY&lt;br /&gt;
  6  ''s.d'' = 0&lt;br /&gt;
  7  ''s.&amp;amp;pi;'' = NIL&lt;br /&gt;
  8  ''Q'' = &amp;amp;Oslash;&lt;br /&gt;
  9  ENQUE(''Q, s'')&lt;br /&gt;
 10  '''while''' ''Q'' &amp;amp;ne; &amp;amp;Oslash;&lt;br /&gt;
 11      ''u'' = DEQUEUE(''Q'')&lt;br /&gt;
 12      '''for''' each ''v'' &amp;amp;isin; ''G.Adj''[''u'']&lt;br /&gt;
 13              '''if''' ''v.color'' == WHITE&lt;br /&gt;
 14                       ''v.color'' = GRAY&lt;br /&gt;
 15                       ''v.d'' = ''u.d'' + 1&lt;br /&gt;
 16                       ''v''.&amp;amp;pi; = ''u''&lt;br /&gt;
 17                       ENQUEUE(''Q, v'')&lt;br /&gt;
 18      ''u.color'' = BLACK&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>Flomm</name></author>
	</entry>
</feed>