The breadth-first Traversal Algorithm for undirected Connected Graphs

Source: Internet
Author: User

Image breadth firstAlgorithmIt is the basic graph algorithm and also the basis of the minimum spanning tree and single-source shortest path algorithm. Recently, I implemented the breadth-first traversal algorithm, hoping to have a deeper understanding or better understanding of the algorithm. I saw a video about Yu Dan's visit to youmi online the other day. I agree with Yu Dan's point of view. A person's knowledge is very important. The so-called knowledge, from one aspect, is to be personally experienced. This is especially true for algorithms, a discipline that emphasizes both mathematics and programming.

Any algorithm needs to understand the context of the algorithm. You need to ask yourself some questions about the name of the algorithm and solve the problems, what are the important definitions or conditions of these problems. In fact, there is no omnipotent method, but there are indeed algorithms that can solve specific problems under certain condition sets. Here, the breadth-first traversal algorithm targets undirected connected graphs. One is undirected, and the other is connected. I will give you a undirected connected graph and A Start Node. What should I do if I want you to do a breadth-first traversal?

 /*  ** The adjacent matrix is used to represent an undirected connected graph.  */  # Include <Iostream> # Include <Queue> Using   Namespace STD;  # Define Len 10 # Define Infinite 100 # Define Nil-1 Bool  M [Len] [Len];  Enum  Color {white, gray, black}; color [Len];  Int  D [Len];  Int  P [Len];  //  The breadth-first traversal algorithm is implemented using a queue + coloring tag.  Void BFS (Int  S ){  Int  I;  For (I = 0 ; I <Len; ++ I) {color [I] = White; d [I] = Infinite; P [I] = Nil;} color [s] = Gray; d [s] = 0  ; P [s] = Nil; queue <Int > Q; q. Push (s );  While (! Q. Empty ()){  Int U = Q. Front (); cout <U < Endl; q. Pop ();  //  Access all vertices next to u      Int  J;  For (J = 0 ; J <Len; ++ J ){ If (M [u] [J] = True  ){  If (Color [J] = White) {color [J] = Gray; d [J] = D [u] + 1  ; P [J] = U; q. Push (j) ;}} color [u] = Black ;}}  Int  Main (){  Int  I, J; For (I = 0 ; I <Len; ++ I ){  For (J = 0 ; J <Len; ++ J) {M [I] [J] = False  ;} M [  0 ] [ 2 ] = True ; M [ 2 ] [ 0 ] = True ; M [  1 ] [ 7 ] = True ; M [ 7 ] [ 1 ] = True  ; M [  2 ] [ 7 ] = True ; M [ 7 ] [ 2 ] = True  ; M [ 2 ] [ 4 ] = True ; M [ 4 ] [ 2 ] = True  ; M [  7 ] [ 3 ] = True ; M [ 3 ] [ 7 ] = True  ; M [  3 ] [ 4 ] = True ; M [ 4 ] [ 3 ] = True  ; M [  4 ] [ 5 ] = True ; M [ 5 ] [ 4 ] = True  ; M [  5 ] [8 ] = True ; M [ 8 ] [ 5 ] = True  ; M [  8 ] [ 6 ] = True ; M [ 6 ] [ 8 ] = True  ; M [  8 ] [ 9 ] = True ; M [ 9 ] [ 8 ] = True  ; BFS (  0  );  Return   0  ;} 

 

The aboveProgramIs the breadth-first traversal of the following undirected connected graph:

The output result after compilation and running is:

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.