7.5 graph Traversal

Source: Internet
Author: User

7.5 graph Traversal

Graph traversal is similar to tree traversal. A certain vertex accesses the rest of the vertices, and each vertex is accessed only once. This process is called graph traversal. Divided into depth-first traversal and breadth-first Traversal

7.5.1 deep priority Traversal

Depth-first traversal Depth_First_Search), also known as depth-first search, or DFS for short.

Deep priority search is actually a recursive process. It is like traversing a tree in the forward order. A vertex V is used to access this vertex, then, the depth traversal graph is prioritized from the inaccessible neighboring contacts of V until all vertices with the same path as V are accessed. If there are still unaccessed vertices in the graph, select an unaccessed vertex in the graph as the starting point and repeat the above process until all vertices in the graph are accessed.

The Code is as follows:

650) this. width = 650; "title =" QQ20130909175900.jpg "src =" http://www.bkjia.com/uploads/allimg/131228/20134a563-0.jpg "/>

For the structure of the adjacent table, the Code is as follows:

650) this. width = 650; "title =" QQ20130909180148.jpg "src =" http://www.bkjia.com/uploads/allimg/131228/2013491A0-1.jpg "/>

7.5.2 breadth-first Traversal

1. For a vertex V0, first access V0.

2. Access the neighboring nodes that have not been accessed in V0 in sequence.

3. Start from these neighboring contacts and access each of their unvisited neighboring contacts in sequence.

 

This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1293328

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.