Data structure-Traversal of graphs

Source: Internet
Author: User

the traversal of the graph is means from one vertex, access and only one time access to all remaining vertices in the diagram, not all edges of processing. Is the basis of the problems such as the connectivity of graphs, topological ordering, and path solving.

A very basic graph traversal method has a depth-first search method and a breadth (width)-First search method.

Depth-First search, Depth first Search,DFS

The Depth-first search method is the generalization of the tree's first root traversal, and its basic idea is: from the graph a vertex of G v0 , accesses v0, then selects a vertex vi that is adjacent to V0 and has not been visited , and then chooses a VI adjacent and inaccessible vertices are accessed by VJ and proceed sequentially. If all adjacent vertices of the currently visited vertex have been accessed, retreat back to the last vertex in the sequence of vertices that has been visited, with the vertices wthat have not been visited, and walk forward from W in the same way until all the vertices in the diagram are accessed. Belong to the blind search.

Depth-First search is a classical algorithm in graph theory, using depth-first search algorithm can produce the corresponding topological sort table of target graph, and using topological sort table can solve many related graph theory problems conveniently, such as the maximum path problem and so on.

due to the invention of "depth-first search algorithm", HOPKLOVT and Robert Tayan jointly obtained the highest prize in the field of Computing: Turing Award .

breadth-First search, Breadth First Search,BFS

the breadth-first search of graphs is a generalization of tree-by-level traversal, and is a traversal strategy of connected graphs. Because its idea is to start from a vertex vi , radiating the first to traverse its wider area around, hence the name. The basic idea is to first access the initial point VIand mark it as visited, and then access all of the non-visited adjacency points of vi vi1,vi2,..., VI t, and mark the visited, and then follow VI1 , vi2 the Order of,..., VI T , accesses all the inaccessible adjacency points of each vertex, and is marked as visited, and so on, until all the vertices in the diagram that have a path to the initial point vi are accessed.

Breadth Excellent Search first. Implement The open-closed table is generally used .

BFS is a blind search method that is designed to systematically unfold and examine all the nodes in the diagram to find results. In other words, it does not take into account the possible addresses of the results, and thoroughly searches the entire graph until the results are found. BFS does not use the rule of thumb algorithm.

from the point of view of the algorithm, all child nodes resulting from the expansion of nodes are added to a FIFO queue. In a general implementation, a node whose neighbor node has not been tested is placed in a container called open (such as a queue or linked list), and the tested node is placed in a container called closed . (open-closed table)

One of the most intuitive examples of application is to walk the maze, starting from the starting point, to find the shortest distance to the end, many of the shortest path algorithm is based on the idea of breadth first established.


Data structure-Traversal of graphs

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.