Algorithm research: Breadth-first traversal of graphs

Source: Internet
Author: User

The traversal of the graph is similar to the traversal of the tree, and we want to go through the rest of the graph from one vertex in the graph, and make each vertex accessible only once, a process called graph traversal (Traverse graph).

There are generally two kinds of traversal methods of graphs, the first one is the "depth first search" that we talked about earlier, and there are also called depth first searches, referred to as DFS (Depth). The second is the breadth-first traversal (breadth first search), also known as breadth-priority searching, referred to as BFS. We have covered in more detail the breadth-first search strategy in queue and breadth first search, and no longer dwell on this. If the depth of the graph takes precedence over the first-order traversal of a tree, then the breadth-first traversal of the graph is analogous to the sequence traversal of the tree, we have slightly changed the first picture of the graph 7-5-3 into the second picture, so that the sense of hierarchy is stronger, the breadth-first traversal requires the operation of the queue, and can refer to the sequential storage structure of the queue.

The following only gives an algorithm code for the breadth-first traversal of graphs when the adjacency matrix and adjacency table are stored. Do not give the overall test run of the code, in fact, only need to write a function to create a diagram can be the overall test, you can refer to the "Adjacency matrix Creation Diagram" and "adjacency Table creation diagram"

First, if we are using the adjacency matrix, the code is as follows:

The 
 typedef char VERTEXTYPE/* Vertex type should be user-defined/typedef int EDGETYPE;/* The weight value type on the edge should be defined by user/#define MAXSIZE 9/* Storage space Initial #define MAXEDGE #define MAXVEX 9 typedef struct {Vertextype Vexs[maxvex];/* Vertex table */Edgetype Arc[m axvex][maxvex];/* adjacency matrix can be regarded as side table/int numvertexes, numedges;
    
/* The current number of vertices and the number of edges/} mgraph;
    /* adjacency Matrix BREADTH traversal algorithm */void Bfstraverse (Mgraph G) {int I, J;
    Queue Q;
    for (i = 0; i < g.numvertexes i++) visited[i] = false; Initqueue (&Q)/* Initialize an auxiliary queue * * for (i = 0; i < g.numvertexes i++)//* For each vertex loop */{if (!visit  ED[I] * * * * * * If not visited on the processing/{Visited[i] = true;/* Set the current vertex access * * cout << g.vexs[i] << ' '; /* Print vertices, can also be other operations/EnQueue (&q, I);/* Put this vertex into queue/while (!  Queueempty (Q)) * * If the current queue is not empty/{dequeue (&q, &i);/* The team to the element out of the queue, assign value to I/for
                (j = 0; J < G.numvertexes; J + +)
{                    /* To determine if other vertices exist with the current vertex and have not been visited */if (g.arc[i][j] = = 1 &&!visited[j])  {Visited[j] = true;/* will find this vertex marked as visited/cout << G.vexs[j] << ';
            /* Print vertex/EnQueue (&q, j);//* will find this vertex into queue/}} }
        }
    }
}

The traversal result is: A B F C G I E D H (diagram structure shown above)

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.