Figure (3) - traversal and connectivity of graphs

Similar to tree traversal, for a given graph, visiting all nodes in the graph along some edges or arcs so that each node is visited only once, this process is called graph traversal.
There are usually two ways to traverse a graph: depth-first traversal and breadth-first traversal. Both methods work for both undirected and directed graphs.

1. Depth-first traversal
Depth-first traversal of a graph is based on depth-first search.
Depth-first search starts from a certain vertex v in the graph, after visiting vertex v, and then performs depth-first search from any adjacent vertex w of v that has not been visited, until all paths in the graph are connected to vertex v. vertices have been visited.
Depth-first search can be implemented with a recursive algorithm.

The access order of a depth-first traversal of a graph is similar to that of a preorder traversal of a tree. If the graph to be traversed is a connected graph, all vertices in the graph can be accessed through a single depth-first search. If it is a disconnected graph, multiple depth-first searches are required.
After the depth-first search is completed, the resulting graph, whose n-1 edges link all n vertices, is called a depth-first search spanning tree.

2. Breadth-first traversal

Breadth-first traversal of a graph is based on breadth-first search.
Breadth-first search starts from a vertex v in the graph, and then visits the unvisited vertices w1, w2, w3...wk of v after visiting vertex v, and then visits w1, w2, w3... All unvisited adjacent vertices of .wk. Then start from these visited vertices, and then visit all their unvisited adjacent vertices, and so on, until all the vertices in the graph that are connected by a path to the vertex v have been visited.
Breadth-first search is a hierarchical search process, similar to the hierarchical traversal of a tree. Like depth-first traversal, if the graph to be traversed is a connected graph, all vertices in the graph can be accessed through a single breadth-first search. If it is a non-connected graph, multiple breadth-first searches are required.
Breadth-first search is not a recursive process, nor is its algorithm recursive. In order to achieve layer-by-layer access, a queue is used in the algorithm to record the vertices of the previous layer and this layer that have just been visited, so as to facilitate access to the next layer.
Breadth-first spanning tree is obtained after breadth-first search.

The time complexity of the depth-first traversal algorithm of the graph is similar to that of the breadth-first traversal algorithm:
For the adjacency matrix representation, to scan n vertices in the matrix, a double-layer loop is performed, so the time complexity is O(n 2).
For the adjacency list representation, let the number of edges of the graph be e, to scan an array with n components and e edges represented by a singly linked list, so the time complexity is O(e+ n) .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324770234&siteId=291194637