The breadth-priority traversal algorithm of graphs using queue master for adjacency table directed graph

Source: Internet
Author: User

The source code is as follows:
#include <iostream>using namespace std; #define Max_vertex_num 20typedef int edgedata; typedef char VertexData;   Vertex data field typedef struct Node {//edge table nodes edgedata cost;//edge D weighted int adjvex; adjacency point domain struct node *next;  The next side of the link pointer}edgenode;  typedef struct {//vertex table VertexData vertex;//Vertex data field edgenode *firstedge;//edge-linked header pointer}vertexnode;      typedef struct {//Graph adjacency table Vertexnode Verlist[max_vertex_num]; int vexnum,edgenum;  Vertex number and number of sides}adjgraph; ---------above is the adjacency table data structure of the graph------------------------//typedef struct queuenode* link;struct queuenode{int item; link next;}; Static link head, tail;link new (int item, link next) {link x = NEW queuenode; x->item = Item;x->next = Next;return x ;} void Queueinit (int maxn) {head = NULL;} int Queueempty () {return head = = null;} void Queueput (int item) {if (head = = NULL) {head = (tail = NEW (Item,head)); return;} Tail->next = NEW (item,tail->next); tail = Tail->next;} int Queueget () {int item = Head->item;link T = head->nExt;delete head;head = T;return item;} --------------the data structure of the queue above-----------------------//void Printadjgraph (adjgraph G) {cout<< "The resulting graph is as follows:" < <endl;int i; for (i = 0;i<g.vexnum;i++) {cout<<g.verlist[i].vertex<< "--"; Edgenode *e = G.verlist[i].firstedge;while (e!=null) {cout<<e->adjvex<< "--"; e = e->next;} Cout<<endl;} }//Diagram adjacency table adjgraph createadjgraph (adjgraph G) {int i,j,k,w;cout<< "input vertex count and number of sides" <<endl;cin>>g.vexnum >>G.edgenum;cout<< "input vertex information" <<endl;for (i = 0; i<g.vexnum;i++) {cin>>g.verlist[i].vertex; G.verlist[i].firstedge = NULL; Set the edge table to an empty table}edgedata weight;int head;int tail;cout<< "Enter the front end index of the tail edge table head, and the weight weight, such as (tail head weight)" << Endl for (k=0;k<g.edgenum;k++) {cin>>tail>>head>>weight; Edgenode *p = new Edgenode; P->adjvex = Head;p->cost = Weight;p->next = G.verlist[tail].firstedge;    G.verlist[tail].firstedge = p; One side is tail---->head//Create an image without a directionAdd the following code//p = new edgenode;//P->adjvex = Tail;//p->cost = Weight;//p->next = G.verlist[head].firstedge;//g.ver List[head].firstedge = P;if (k==g.edgenum-1) printadjgraph (G); } return G;  }bool Visited[max_vertex_num]; int Dfn[max_vertex_num]; Vertex first depth number int count = 1;void BFS1 (adjgraph g,int K) {int i; Edgenode *p; Queueinit (+); cout<<g.verlist[k].vertex<<endl;visited[k] = true; Queueput (K); while (! Queueempty ()) {i = Queueget ();p = G.verlist[i].firstedge;while (p) {if (!visited[p->adjvex]) {cout<<g.verlist[ P->adjvex].vertex<<endl;visited[p->adjvex] = true; Queueput (P->adjvex);} p = P->next;}} If the adjacency matrix is replaced by the following program with the while (p) program segment//int J;//while (! Queueempty ()) {//i = Queueget ();//for (j=0;j<g.vexnum;j++)//if (g.edge[i][j]==1 &&!visited[j]) {//cout< &LT;G.VERLIST[J]&LT;&LT;ENDL;//VISITED[J] = True;//queueput (j);//}//}//---------The following is the graph's adjacency matrix data structure-------------////# Define max_vertex_num 20//typedef int qelemtype; typedef int EDGEDATA;//TYPEDEF Char vertexdata;//typedef struct//{//VertexData verlist[max_vertex_num]; Vertex table//Edgedata edge[max_vertex_num][max_vertex_num];                          Adjacency Matrix--can be tested for the relationship between the edges//int vexnum,edgenum; Number of vertices and number of sides//}mtgraph;} Breadth First traverse main program void Bfstraverse (Adjgraph G) {int i; for (i=0;i<g.vexnum;i++) visited[i]=false;for (i=0;i<g.vexnum;i+ +) if (!visited[i]) BFS1 (g,i);} Main () {adjgraph G; g = Createadjgraph (g);cout<< "breadth-first traversal results:" <<endl; Bfstraverse (G); System ("Pause");}
Program Run result diagram


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The breadth-priority traversal algorithm of graphs using queue master for adjacency table directed graph

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.