HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, graph traversal (also known as graph search) refers to the process of visiting (checking and/or updating) each vertex in a
graph Graph may refer to: Mathematics *Graph (discrete mathematics), a structure made of vertices and edges **Graph theory, the study of such graphs and their properties *Graph (topology), a topological space resembling a graph in the sense of discre ...
. Such traversals are classified by the order in which the vertices are visited.
Tree traversal In computer science, tree traversal (also known as tree search and walking the tree) is a form of graph traversal and refers to the process of visiting (e.g. retrieving, updating, or deleting) each node in a tree data structure, exactly once. S ...
is a special case of graph traversal.


Redundancy

Unlike tree traversal, graph traversal may require that some vertices be visited more than once, since it is not necessarily known before transitioning to a vertex that it has already been explored. As graphs become more
dense Density (volumetric mass density or specific mass) is the substance's mass per unit of volume. The symbol most often used for density is ''ρ'' (the lower case Greek letter rho), although the Latin letter ''D'' can also be used. Mathematically ...
, this redundancy becomes more prevalent, causing computation time to increase; as graphs become more sparse, the opposite holds true. Thus, it is usually necessary to remember which vertices have already been explored by the algorithm, so that vertices are revisited as infrequently as possible (or in the worst case, to prevent the traversal from continuing indefinitely). This may be accomplished by associating each vertex of the graph with a "color" or "visitation" state during the traversal, which is then checked and updated as the algorithm visits each vertex. If the vertex has already been visited, it is ignored and the path is pursued no further; otherwise, the algorithm checks/updates the vertex and continues down its current path. Several special cases of graphs imply the visitation of other vertices in their structure, and thus do not require that visitation be explicitly recorded during the traversal. An important example of this is a tree: during a traversal it may be assumed that all "ancestor" vertices of the current vertex (and others depending on the algorithm) have already been visited. Both the
depth-first Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible alon ...
and breadth-first graph searches are adaptations of tree-based algorithms, distinguished primarily by the lack of a structurally determined "root" vertex and the addition of a data structure to record the traversal's visitation state.


Graph traversal algorithms

Note. — If each vertex in a graph is to be traversed by a tree-based algorithm (such as DFS or BFS), then the algorithm must be called at least once for each connected component of the graph. This is easily accomplished by iterating through all the vertices of the graph, performing the algorithm on each vertex that is still unvisited when examined.


Depth-first search

A depth-first search (DFS) is an algorithm for traversing a finite graph. DFS visits the child vertices before visiting the sibling vertices; that is, it traverses the depth of any particular path before exploring its breadth. A stack (often the program's
call stack In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or ma ...
via
recursion Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics ...
) is generally used when implementing the algorithm. The algorithm begins with a chosen "root" vertex; it then iteratively transitions from the current vertex to an adjacent, unvisited vertex, until it can no longer find an unexplored vertex to transition to from its current location. The algorithm then backtracks along previously visited vertices, until it finds a vertex connected to yet more uncharted territory. It will then proceed down the new path as it had before, backtracking as it encounters dead-ends, and ending only when the algorithm has backtracked past the original "root" vertex from the very first step. DFS is the basis for many graph-related algorithms, including topological sorts and
planarity testing In graph theory, the planarity testing problem is the algorithmic problem of testing whether a given graph is a planar graph (that is, whether it can be drawn in the plane without edge intersections). This is a well-studied problem in computer sc ...
.


Pseudocode

* ''Input'': A graph ''G'' and a vertex ''v'' of ''G''. * ''Output'': A labeling of the edges in the connected component of ''v'' as discovery edges and back edges. procedure DFS(''G'', ''v'') is label ''v'' as explored for all edges ''e'' in ''G''.incidentEdges(''v'') do if edge ''e'' is unexplored then ''w'' ← ''G''.adjacentVertex(''v'', ''e'') if vertex ''w'' is unexplored then label ''e'' as a discovered edge recursively call DFS(''G'', ''w'') else label ''e'' as a back edge


Breadth-first search

A breadth-first search (BFS) is another technique for traversing a finite graph. BFS visits the sibling vertices before visiting the child vertices, and a
queue __NOTOC__ Queue () may refer to: * Queue area, or queue, a line or area where people wait for goods or services Arts, entertainment, and media *''ACM Queue'', a computer magazine * ''The Queue'' (Sorokin novel), a 1983 novel by Russian author ...
is used in the search process. This algorithm is often used to find the shortest path from one vertex to another.


Pseudocode

* ''Input'': A graph ''G'' and a vertex ''v'' of ''G''. * ''Output'': The closest vertex to ''v'' satisfying some conditions, or null if no such vertex exists. procedure BFS(''G'', ''v'') is create a queue ''Q'' enqueue ''v'' onto ''Q'' mark ''v'' while ''Q'' is not empty do ''w'' ← ''Q''.dequeue() if ''w'' is what we are looking for then return ''w'' for all edges ''e'' in ''G''.adjacentEdges(''w'') do ''x'' ← ''G''.adjacentVertex(''w'', ''e'') if ''x'' is not marked then mark ''x'' enqueue ''x'' onto ''Q'' return null


Applications

Breadth-first search can be used to solve many problems in graph theory, for example: * finding all vertices within one connected component; *
Cheney's algorithm Cheney's algorithm, first described in a 1970 Association for Computing Machinery, ACM paper by C.J. Cheney, is a stop and copy method of tracing garbage collection in computer software systems. In this scheme, the Memory management#Dynamic memory ...
; * finding the
shortest path In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized. The problem of finding the shortest path between tw ...
between two vertices; * testing a graph for bipartiteness; *
Cuthill–McKee algorithm In numerical linear algebra, the Cuthill–McKee algorithm (CM), named after Elizabeth Cuthill and James McKee,E. Cuthill and J. McKeethe bandwidth of sparse symmetric matrices''In Proc. 24th Nat. Conf. Association for Computing Machinery, ACM, ...
mesh numbering; * Ford–Fulkerson algorithm for computing the
maximum flow In optimization theory, maximum flow problems involve finding a feasible flow through a flow network that obtains the maximum possible flow rate. The maximum flow problem can be seen as a special case of more complex network flow problems, such ...
in a
flow network In graph theory, a flow network (also known as a transportation network) is a directed graph where each edge has a capacity and each edge receives a flow. The amount of flow on an edge cannot exceed the capacity of the edge. Often in operations re ...
; * serialization/deserialization of a binary tree vs serialization in sorted order (allows the tree to be re-constructed in an efficient manner); *
maze generation algorithm Maze generation algorithms are automated methods for the creation of mazes. Graph theory based methods A maze can be generated by starting with a predetermined arrangement of cells (most commonly a rectangular grid but other arrangements ...
s; *
flood fill Flood fill, also called seed fill, is a flooding algorithm that determines and alters the area connected to a given node in a multi-dimensional array with some matching attribute. It is used in the "bucket" fill tool of paint programs to fill c ...
algorithm for marking contiguous regions of a two dimensional image or n-dimensional array; * analysis of networks and relationships.


Graph exploration

The problem of graph exploration can be seen as a variant of graph traversal. It is an
online problem In computer science, an online algorithm is one that can process its input piece-by-piece in a serial fashion, i.e., in the order that the input is fed to the algorithm, without having the entire input available from the start. In contrast, an o ...
, meaning that the information about the graph is only revealed during the runtime of the algorithm. A common model is as follows: given a connected graph with non-negative edge weights. The algorithm starts at some vertex, and knows all incident outgoing edges and the vertices at the end of these edges—but not more. When a new vertex is visited, then again all incident outgoing edges and the vertices at the end are known. The goal is to visit all ''n'' vertices and return to the starting vertex, but the sum of the weights of the tour should be as small as possible. The problem can also be understood as a specific version of the
travelling salesman problem The travelling salesman problem (also called the travelling salesperson problem or TSP) asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each cit ...
, where the salesman has to discover the graph on the go. For general graphs, the best known algorithms for both undirected and directed graphs is a simple
greedy algorithm A greedy algorithm is any algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage. In many problems, a greedy strategy does not produce an optimal solution, but a greedy heuristic can yield locally ...
: * In the undirected case, the greedy tour is at most -times longer than an optimal tour. The best lower bound known for any deterministic online algorithm is 10/3. ** Unit weight undirected graphs can be explored with a competitive ration of , which is already a tight bound on Tadpole graphs. * In the directed case, the greedy tour is at most ()-times longer than an optimal tour. This matches the lower bound of . An analogous competitive lower bound of ''Ω''(''n'') also holds for randomized algorithms that know the coordinates of each node in a geometric embedding. If instead of visiting all nodes just a single "treasure" node has to be found, the competitive bounds are ''Θ''(''n''2) on unit weight directed graphs, for both deterministic and randomized algorithms.


Universal traversal sequences

A ''universal traversal sequence'' is a sequence of instructions comprising a graph traversal for any
regular graph In graph theory, a regular graph is a graph where each vertex has the same number of neighbors; i.e. every vertex has the same degree or valency. A regular directed graph must also satisfy the stronger condition that the indegree and outdegr ...
with a set number of vertices and for any starting vertex. A probabilistic proof was used by Aleliunas et al. to show that there exists a universal traversal sequence with number of instructions proportional to for any regular graph with ''n'' vertices. The steps specified in the sequence are relative to the current node, not absolute. For example, if the current node is ''v''''j'', and ''v''''j'' has ''d'' neighbors, then the traversal sequence will specify the next node to visit, ''v''''j''+1, as the ''i''th neighbor of ''v''''j'', where 1 ≤ ''i'' ≤ ''d''.


See also

*
External memory graph traversal External memory graph traversal is a type of graph traversal optimized for accessing externally stored memory. Background Graph traversal is a subroutine in most graph algorithms. The goal of a graph traversal algorithm is to visit (and / or proces ...


References

{{reflist Graph algorithms Articles with example pseudocode de:Suchverfahren#Suche in Graphen pl:Przeszukiwanie grafu