How to achieve breadth-first traversal (BFS) and breadth-bfs

Source: Internet
Author: User

How to achieve breadth-first traversal (BFS) and breadth-bfs

BFS process:

1. Access vertex V and Mark V as accessed

Ii. vertex V entry queue

3. If the queue is not empty, execute the command; otherwise, the algorithm ends.

4. Obtain the right vertex u in the output queue. If the vertex is not accessed, access the vertex and mark it as accessed.

5. Find the first adjacent node w of u.

6. If w does not exist, go to step 3; otherwise, run it cyclically.

A. If w has not been accessed, add w to the queue first.

B. Find the next adjacent node of vertex u, which is marked as w, and go to Step 6.


The BFS access sequence is as follows:

A BEDC


The change process of the queue is as follows:

A

B

BE

ED

D

C

The Code is as follows:

# Include <iostream> using namespace std; # define VertexSize 10int visit [VertexSize]; // ==============================================================#define QueueSize 30 typedef struct {int Seq [QueueSize]; int front; int rear; int count;} RQueue; RQueue Q; void Initiate_Queue (RQueue * Q) {Q-> front = 0; Q-> rear = 0; q-> count = 0;} void AppendQueue (RQueue * Q, int data) {if (Q-> count> = QueueSize) {cout <"overflow" <endl; return;} Q-> Seq [Q-> rear] = data; Q-> r Ear = (Q-> rear + 1) % QueueSize; Q-> count ++;} int QueueNotEmpty (RQueue * Q) {if (Q-> count! = 0) return 1; else return 0;} int DeleteQueue (RQueue * Q) {if (Q-> count <= 0) {cout <"empty" <endl; return NULL;} int d; d = Q-> Seq [Q-> front]; Q-> front = (Q-> front + 1) % QueueSize; q-> count --; return d ;} // ========================================================== typedef struct {int weight [VertexSize] [VertexSize];} graph; void Initiate_Graph (Graph * g, int n) {int I, j; for (I = 0; I <n; I ++) visit [I] = 0; for (j = 0; j <n; j ++) {if (I = j) g-> weight [I] [j] = 0; else g-> wei Ght [I] [j] = 0x7fff;} void InsertEdge (Graph * g, int v, int w, int weight, int n) {if (v <0 | v> = n | w <0 | w> = n) {cout <"overflow! ======== "<Endl;} g-> weight [v] [w] = weight;} void dfs (Graph * g, int u, int n) {cout <u <"; visit [u] = 1; int I; for (I = 0; I <n; I ++) {if (g-> weight [u] [I]> 0 & g-> weight [u] [I] <0x7fff &&! Visit [I]) {visit [I] = 1; dfs (g, I, n) ;}} void bfs (Graph * g, int u, int n) {Initiate_Queue (& Q); int j; cout <u <""; visit [u] = 1; AppendQueue (& Q, u ); while (QueueNotEmpty (& Q) {int x = DeleteQueue (& Q); for (j = 0; j <n; j ++) {if (g-> weight [x] [j]> 0 & g-> weight [x] [j] <0x7fff &&! Visit [j]) {cout <j <"; visit [j] = 1; AppendQueue (& Q, j) ;}}} void main () {Graph g; int n, edge; cout <"Enter the number of vertices in the Graph:" <endl; cin> n; cout <"Enter the number of edges in the graph" <endl; cin> edge; Initiate_Graph (& g, n); int I, p1, p2, weight; cout <"Enter vertex-weight:" <endl; for (I = 0; I <edge; I ++) {cin> p1> p2> weight; InsertEdge (& g, p1, p2, weight, n) ;}cout <"depth-first traversal: "<endl; dfs (& g, 0, n); cout <endl; for (I = 0; I <n; I ++) visit [I] = 0; cout <"breadth-first traversal:" <endl; bfs (& g, 0, n); cout <endl; system ("pause ");}



C #) Implementation of deep and breadth-first search algorithms for Graphs

C #) Deep Priority Search for images
PublicvoidDFSTraverse () // depth-first Traversal
{
InitVisited (); // set all the visited flag to false
DFS (items [0]); // traverse from the first Vertex
}
PrivatevoidDFS (Vertex <T> v) // uses recursion for depth-first Traversal
{
V. visited = true; // set the access flag to true.
Console. Write (v. data + ""); // access
Nodenode = v. firstEdge;
While (node! = Null) // All adjacent contacts accessing this Vertex
{// If the adjacent vertex is not accessed, recursively access its edge
If (! Node. adjvex. visited)
{
DFS (node. adjvex); // Recursion
}
Node = node. next; // access the next adjacent point
}
}
PrivatevoidInitVisited () // initialize the visited flag
{
Foreach (Vertex <T> vinitems)
{
V. visited = false; // set all to false.
}
}

C #) Search for the image breadth first
PublicvoidBFSTraverse () // breadth-first Traversal
{
InitVisited (); // set all the visited flag to false
BFS (items [0]); // traverse from the first Vertex
}
PrivatevoidBFS (Vertex <T> v) // use the queue for breadth-first Traversal
{// Create a queue
Queue <Vertex <T> queue = newQueue <Vertex <T> ();
Console. Write (v. data + ""); // access
V. visited = true; // set the access flag
Queue. Enqueue (v); // enter the team
While (queue. Count> 0) // loop as long as the team is not empty
{
Vertex <T> w = queue. Dequeue ();
Nodenode = w. firstEdge;
While (node! = Null) // All adjacent contacts accessing this Vertex
{// If the adjacent vertex is not accessed, recursively access its edge
If (! Node. adjvex. visited)
{
Console. Write (node. adjvex. data + ""); // access
Node. adjvex. visited = true; // set the access flag
Queue. Enqueue (node. adjvex); // enter the team
}
Node = node. next; // access the next adjacent point
}
}
}... Remaining full text>

The difference between depth-first traversal and breadth-first Traversal

Depth-first traversal and breadth-first traversal are graph Traversal Algorithms (do not understand the chapter on Data Structure traversal ).
The depth first traverses from a vertex, first accesses this vertex, then finds the first unaccessed neighbor node that just accesses this node, and then uses this neighbor node as the vertex, continue to find its next vertex for access. Repeat this step until all nodes are accessed.
The breadth first traverses a vertex, first accesses this vertex, and then finds all unaccessed adjacent points of this node, after the access, access all the nodes in the first adjacent contacts of these nodes. Repeat this method until all nodes are accessed.

The biggest difference between the two methods is that the former keeps accessing the first adjacent point of the vertex and then accesses the second adjacent point of the vertex; the latter accesses all adjacent points of the vertex from the vertex and goes down one layer by one.

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.