Graph traversal: Difference between revisions
Jump to navigation
Jump to search
(Created page with "== Input == # A directed graph <math>G=(V,A)</math>. # A '''start node''' <math>s\in V</math>. ''Output:''' A sequence of all nodes of <math>G</m...") |
No edit summary |
||
(16 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Basic definitions == | |||
# [[Basic graph definitions]] | |||
== Input == | == Input == | ||
Line 4: | Line 8: | ||
# A '''start node''' <math>s\in V</math>. | # A '''start node''' <math>s\in V</math>. | ||
== Output == | |||
An [[Sets and sequences#Ordered and sorted sequences|ordered sequence]], whose content is a permutation of all nodes of <math>G</math> that can be reached from <math>s</math> via paths in <math>G</math>. | |||
== Known algorithms == | == Known algorithms == | ||
# [[Depth-first search]] | # [[Depth-first search]] | ||
# [[Breadth-first | # [[Breadth-first search]] | ||
== Remarks == | == Remarks == | ||
# For the purpose of graph traversal, an undirected graph may be viewed as a directed graph: Replace each edge <math>\{v,w\}</math> by two arcs, <math>(v,w)<math> and <math>(w,v)</math>. | # Without loss of generality, the graph is [[Basic graph definitions#Directed and undirected graphs|simple]]. | ||
# It may be reasonable to implement a graph traversal algorithm in the form of an iterator, which returns the nodes | # For the purpose of graph traversal, an undirected graph may be viewed as a directed graph: Replace each edge <math>\{v,w\}</math> by two arcs, <math>(v,w)</math> and <math>(w,v)</math>. | ||
# [[Dijkstra|Dijkstra's algorithm]] may be implemented as a graph traversal that returns the nodes in ascending order of | # It may be reasonable to implement a graph traversal algorithm in the form of an iterator, which returns the processed nodes and/or the traversed arcs step-by-step. In such an implementation, the loop is turned inside-out, so the client code has full control over the loop: may terminate the loop early, suspend the loop and resume its execution later on, insert additional functionality in every iteration, etc. | ||
# [[Dijkstra|Dijkstra's algorithm]] may be implemented as a graph traversal that returns the nodes in the order of finishing. In other words, in ascending order of the node distances (which is not unique, in general). | |||
# The common graph traversal algorithms deliver specific additional output besides the above-mentioned output sequence. In each case, additional output is specified on the page of the respective algorithm. | |||
# '''Early termination:''' Often, graph traversal is used to find a path from some start node <math>s</math> to some other node <math>t</math>. In this case, the traversal may terminate earlier, namely when <math>t</math> is seen (if <math>t</math> is ''not'' reachable from <math>s</math>, the search must be exhaustive in order to prove that no path exists). |
Latest revision as of 19:06, 9 November 2014
Basic definitions
Input
- A directed graph [math]\displaystyle{ G=(V,A) }[/math].
- A start node [math]\displaystyle{ s\in V }[/math].
Output
An ordered sequence, whose content is a permutation of all nodes of [math]\displaystyle{ G }[/math] that can be reached from [math]\displaystyle{ s }[/math] via paths in [math]\displaystyle{ G }[/math].
Known algorithms
Remarks
- Without loss of generality, the graph is simple.
- For the purpose of graph traversal, an undirected graph may be viewed as a directed graph: Replace each edge [math]\displaystyle{ \{v,w\} }[/math] by two arcs, [math]\displaystyle{ (v,w) }[/math] and [math]\displaystyle{ (w,v) }[/math].
- It may be reasonable to implement a graph traversal algorithm in the form of an iterator, which returns the processed nodes and/or the traversed arcs step-by-step. In such an implementation, the loop is turned inside-out, so the client code has full control over the loop: may terminate the loop early, suspend the loop and resume its execution later on, insert additional functionality in every iteration, etc.
- Dijkstra's algorithm may be implemented as a graph traversal that returns the nodes in the order of finishing. In other words, in ascending order of the node distances (which is not unique, in general).
- The common graph traversal algorithms deliver specific additional output besides the above-mentioned output sequence. In each case, additional output is specified on the page of the respective algorithm.
- Early termination: Often, graph traversal is used to find a path from some start node [math]\displaystyle{ s }[/math] to some other node [math]\displaystyle{ t }[/math]. In this case, the traversal may terminate earlier, namely when [math]\displaystyle{ t }[/math] is seen (if [math]\displaystyle{ t }[/math] is not reachable from [math]\displaystyle{ s }[/math], the search must be exhaustive in order to prove that no path exists).