Algorithm Learning-Graph breadth-first traversal (BFS) (c + +)

Source: Internet
Author: User

Breadth-First traversal

Breadth-first traversal is a graph traversal method that is often seen and common, with the exception of BFS and DFS, which is the depth-first traversal method. I'll write it in my next blog post.

Traversal process

I believe every person who looks at this blog will be able to read and understand the adjacency list storage diagram.
Do not understand the people. Please learn the storage method first. In my previous blog.
Portal: Diagram Representation method

Then we have a figure such as the following:

Node 1->3->null
Node 2->null
Node 3->2->4->null
Node 4->1->2->null

So we already know what the map is.

If we start the traversal from Node 1.
The node 1 is grayed out first, then added to the queue, and then all the points in the node 1 are grayed out and added to the queue.

Output and eject the first element 节点1 of the team and turn the color of node 1 to black.

The adjacent nodes of the first element of the team are then added to the queue. Then continue to output and eject the first element of the team and so on. Until the queue is empty.

Code implementation

Here is the code implementation I wrote. Simpler.

I have to write part of the gaze.

////Main.cpp//BFS////Created by Alps on 15/3/30.//Copyright (c) 2015 Chen. All rights reserved.//#include <iostream>#include <queue>#ifndef Vertex#define VERTEX int#endif#ifndef Numvertex#define NUMVERTEX 4#endif#define WHITE 0#define GRAY 1#define BLACK 2using namespace STD;structnode{intValintWeight    Node* Next; NodeintVintW): Val (v), weight (W), Next (NULL) {}};typedefnode* VList;structtableentery{VList Header; Vertex color;};typedefTableentery table[numvertex+1];voidInittableentry (Vertex start, Table T) {//init the GraphVertex Outdegree =0; VList temp = NULL; for(inti =1; I <= Numvertex; i++) {scanf("%d", &outdegree);//input the out degree of vertexT[i].header = NULL; T[i].color = white; for(intj =0; J < Outdegree; J + +) {temp = (VList)malloc(sizeof(structnode));scanf("%d%d", &temp->val, &temp->weight);            Temp->next = T[i].header;        T[i].header = temp; }} t[start].color = GRAY;//init The start vertex color to gray}voidBFS (Vertex start, Table T) { queue<Vertex>Q;    Q.push (start); VList temp = NULL; while(! Q.empty ()) {//if queue is not empty and then the BFS isn't overtemp = T[q.front ()].header;//find The front of the queue         while(temp) {//if the front vertex has next vertex            if(T[temp->val].color = = white) {Q.push (temp->val);//push The white vertex to the queueT[temp->val].color = GRAY;//change the color to gray} temp = temp->next; }printf("%d", Q.front ());//output the vertexT[q.front ()].color = BLACK;//then Change ColorQ.pop (); }}intMainintargcConst Char* argv[]) {Table T; Inittableentry (1, T); BFS (1, T);return 0;}

The above code is the BFS. There are actually many other implementations.

All can.

Algorithm Learning-Graph breadth-first traversal (BFS) (c + +)

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.