The breadth-first traversal of the graph without graphs and its Java implementation

Source: Internet
Author: User

Breadth-First traversal (Breadth-first traverse,bfts), called breadth-first search, is a traversal strategy for connected graphs. It is called the breadth-first traversal because his mind begins with a vertex V0, radiating the first to traverse the wider area around it.

Algorithm description

Given figure g= (v,e). V is a collection of nodes, and E is a collection of edges.

Set an Access flag bit vflag (i) indicates the access of node I, if Vflag (i) =0 indicates that node i is not accessed

Vflag (i) =1 indicates that node I has been visited.

L Initialize the vflag=0 of all nodes.

L a node V0 (the node can be arbitrary), and access this vertex.

From V0, visit each of the V0 adjacency points (subscript from small to large) w1,w2,...,wk; then, from W1,w2,..., Wk, you access the adjacent neighboring points that are not visited.

L Repeat up until all the vertices have been accessed.

For example,

Fig1. 5-node non-direction graph

Let's say we start with the V3 node and load the V3 into the traversal result queue and the cache queue. The V3 access flag position 1. Remove the cache queue first and discard it. Here is the V3 node. Find the nodes that are not visited that are adjacent to the V3 node. There are V1 and V5 nodes, and we will load the V1 and V5 into the cache queue and the traversal queue respectively by the subscript size. From this. The cache queue is [1,5]. The traversal queue is [3,1,5]. Now that the cache queue is not empty, we continue to repeat the previous steps. The cache queue first V1 is removed and discarded. Find the nodes that are not visited with V1, and have v2,v4. At this point the cache queue is [5,2,4] and the traversal queue is [3,1,5,2,4]. At this point, the traversal has completed. The cache queue has not been left empty and continues to repeat the above steps. That is, remove and discard the cache queue first V5, find the node that is adjacent to V5, not. V3 is adjacent but V3 has been accessed. At this point the cache queue is [2,4] and the traversal queue is [3,1,5,2,4]. Similar to losing v2,v4 until the cache queue is left empty. Actually save the program run time can set the traversal end condition to the Access flag queue all non 0.

Java implementation code for BFT:

1%============================================ 2% file name        :           bfst3 version number:        1.0.0 4           isabelle< [Email protected]> 5% :          6 Modified:    Sat Oct. 8 1:20:01 2017 7% Creation Time:    Sat Oct. 8 1:20:01 2017 8 --------------------------------------------------------------------------                9 Blog:http://  Www.cnblogs.com/isabellezhouCopyright (Dist)11%======================================== == = Publicclass{}

The breadth-first traversal of the graph without graphs and its Java implementation

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.