Graph breadth first traverses BFS (Adjacent matrix representation)

Source: Internet
Author: User
1. Preface

ReviewAlgorithmChapter 4: path in the figure. First, the algorithm breadth first traversal is completed, and the queue structure is used.

2. References


3. The relevant blog has completed the depth-first traversal of the graph in the previous blog, and uses the adjacent matrix representation.

Depth-first traversal of an image (using the adjacent matrix)



# Include <iostream> <br/> # include <malloc. h> <br/> # include <queue> <br/> # include <stdlib. h> <br/> using namespace STD; </P> <p> # define maxnum 100 // defines the maximum number of vertices in the adjacent area. <br/> int visited [maxnum]; // use the visited array to identify whether the vertex has been accessed. 0 indicates that it has not been accessed, and 1 indicates that it has been accessed. <br/> queue <int> q; <br/> // The structure of the graph's Adjacent matrix <br/> typedef struct <br/> {<br/> char V [maxnum]; // vertex information of the graph <br/> int e [maxnum] [maxnum]; // vertex information of the graph <br/> int vnum; // Number of vertices <br/> int Enum; // Number of edges <br/>} Graph; <br/> // function declaration <br/> void creategraph (graph * g); // create a graph G <br/> void BFS (graph * g ); // traverse graph G by breadth first </P> <p> void BFS (graph * g, int I) <br/>{< br/> int K, J; <br/> cout <"vertex" <I <"accessed" <Endl; <br/> visited [I] = 1; // mark vertex I accessed </P> <p> q. push (I); <br/> while (! Q. empty () <br/>{< br/> K = Q. front (); <br/> q. pop (); <br/> // cout <q. size (); <br/> for (j = 1; j <= G-> vnum; j ++) <br/>{< br/> If (G-> E [k] [J]! = 0 & visited [J] = 0) <br/> {<br/> cout <"vertex" <j <"accessed" <Endl; <br/> visited [J] = 1; <br/> q. push (j); <br/>}</P> <p> void BFS (graph * g) <br/>{< br/> int I; <br/> // initialize the visited array, indicates that at first all vertices have not been accessed <br/> for (I = 1; I <= G-> vnum; I ++) <br/> visited [I] = 0; <br/> // breadth-first search <br/> for (I = 1; I <= G-> vnum; I ++) <br/>{< br/> If (visited [I] = 0) // If This vertex is accessed, then we traverse the breadth first from vertex I <br/> BFS (G, I ); <br/>}< br/> void creategraph (graph * g) // create graph G <br/>{< br/> cout <"creating undirected graph... "<Endl; <br/> cout <" Enter the number of vertices vnum: "; <br/> CIN> G-> vnum; <br/> cout <"Enter the number of edges Enum:"; <br/> CIN> G-> Enum; <br/> int I, J; </P> <p> // initial drawing G <br/> for (I = 1; I <= G-> vnum; I ++) <br/> for (j = 1; j <= G-> vnum; j ++) <br/> G-> E [I] [J] = 0; </P> <p> // condition of the input edge <br/> cout <"Enter the header and end of the edge" <Endl; <br/> for (int K = 1; k <= G-> Enum; k ++) <br/>{< br/> CIN> I> J; <br/> G-> E [I] [J] = 1; <br/> G-> E [J] [I] = 1; <br/>}</P> <p> int main () <br/>{< br/> graph * g; <br/> G = (graph *) malloc (sizeof (graph); <br/> creategraph (g); <br/> BFS (g ); <br/> system ("pause"); <br/> return 0; <br/>}

Test input:


Creating undirected graph...

Enter the number of vertices vnum: 6

Enter the number of edges Enum: 5

Enter the edge header and end

1 2

1 4

1 5

1 6

2 3

Output result:

Vertex 1 has been accessed

Vertex 2 has been accessed

Vertex 4 has been accessed

Vertex 5 has been accessed

Vertex 6 has been accessed

Vertex 3 has been accessed

Press any key to continue...


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: 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.