Introduction to algorithms-breadth-first search and deep-first search; Introduction to deep-first search

Source: Internet
Author: User

Introduction to algorithms-breadth-first search and deep-first search; Introduction to deep-first search

Extended search

Given the graph G = (V, E) and a specific Source Vertex s, the breadth first searches for the edge in G, in order to "Discover" all vertices that can be reached from s, and calculate the distance between s and all these reachable vertices (that is, the minimum number of edges ). The search algorithm can also generate a breadth-first tree with s root and reachable vertices including all s. For any vertex v reachable from s, the path from s to v in the breadth-first tree corresponds to a shortest path from s to v in graph G, that is, the path containing the minimum number of edges. This algorithm is also applicable to Directed Graphs and undirected graphs.

For details about the specific algorithm, see algorithm-12. breadth-first search

Algorithm analysis:

Analyze the running time of the algorithm on the input graph G = (V, E. UseClustering AnalysisTechnology, after initialization, no vertex is set to white. Therefore,The test in the second row ensures that each vertex only enters the queue once at most, and thus only comes out of the queue once at most. It takes O (1) to start and exit the queue. Therefore, the entire time required for queue operations is O (V ). Because the list of adjacent tables is scanned only when each vertex is out of the queue, the list of adjacent tables of each vertex is scanned at most once. Because the length of all the adjacent tables is O (E), it takes O (E) to scan all the adjacent tables). The overhead of the initialization operation is O (V). Therefore, the total running time of the BFS process is O (V + E ). It can be seen that the running time of the breadth-first search is a linear function of the size of the adjacent table of graph G.

Deep Priority Search

As the name "deep-first search" implies, this search algorithm adopts a search policy that searches for a graph as "deep" as possible. In the depth-first search, if the last detected vertex has an edge not detected starting from this point, it will continue to be detected along this edge. After all the edges of vertex v have been explored, the search will go back to those edges that find that vertex v has a starting point.This process continues until all vertices that can be reached from the Source Vertex are found.. If no undiscovered vertex exists, select one of them as the Source Vertex and repeat the above process. The entire process is repeated until all vertices are found.

Algorithm-11. Deep Priority Search

Algorithm analysis:

DFS 1st ~ 3 lines and 5th ~ The cycle time in the 7 rows is O (V), which does not include the time at which the DFS-VISIT is called. Just like when we deal with breadth-first searches, we useClustering Analysis. For each vertex v vertex V, the process DFS-VISIT is called only once, because the process is called only for the white vertex, and the first thing the process does is to set the vertex to Gray. 4th ~ The loop in the seven rows is executed | Adj [v] | times. Because

Therefore, the execution process in the DFS-VISIT 4th ~ The total cost of 7 rows is O (E ). Therefore, the running time of DFS is O (V + E ).

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.