Home > database >  Data structure, the directed graph adjacency list BFS breadth-first traversal
Data structure, the directed graph adjacency list BFS breadth-first traversal

Time:10-04

Everyone a great god, void BFS () as the writing is wrong, I carry out is adcefb
Is not the same as the title, I hope you to see
#include
using namespace std;
# define mvnum 10
# define Null 0
# define maxsize 10
# define the error 0
# define ok 1
Typedef char vertextype;
Typedef int arctype;
Typedef struct ArcNode
{
Int AdjVex;
Struct ArcNode * nextarc;
} ArcNode;
Typedef struct
{
Vertextype data;
ArcNode * firstarc;
} Vnode AdjList [mvnum];
Typedef struct
{
AdjList are drawn.
Int vexnum arcnum;

} ALGraph;

//__________________________________________________Queue
Typedef int the status;
Typedef int QelemType;
Typedef struct sqQueue
{
Int the front and rear;
QelemType * base;
} sqQueue;

The status InitQueue (sqQueue & amp; Q)
{
Q.b ase=new QelemType [maxsize];
if(! Q.b ase) return the error;
Q.f ront=0;
Q.r ear=0;
Return ok;
}
The status the EnQueue (sqQueue & amp; Q, QelemType e)
{
If ((Q.r ear + 1) % maxsize==Q.f ront)
Return the error;
Q.b ase [Q.r ear]=e;
Q.r ear=(Q.r ear + 1) % maxsize;
Return ok;
}
The status to DeQueue (sqQueue & amp; Q, QelemType & amp; E)
{
If (Q.f ront==Q.r ear) return the error;
E=Q.b ase [Q.f ront];
Q.f ront=(Q.f ront + 1) % maxsize;
Return ok;
}
Bool QueueEmpty (sqQueue Q)
{
If (Q.r ear==Q.f ront) return true;
The else return false;
}
//____________________________________________Queue


Int LocateVex (ALGraph G, vertextype p)
{
Int xiabiao;
for(int i=0; i{
If (G.v ertices [I] data=https://bbs.csdn.net/topics/=p)
{
Xiabiao=I;
}
}
Return xiabiao;
}
Void CreateGraph (ALGraph & amp; G)
{
Int I, k, j;
Cin> G.v exnum> G.a rcnum;
for(i=0; i{
Cin> G.v ertices [I]. Data;
G.v ertices [I] firstarc=Null;
}
For (k=0; k{
Vertextype v1, v2,
Cin> V1 & gt;> V2.
I=LocateVex (G, v1);
J=LocateVex (G, v2);
ArcNode * p1=new ArcNode;
P1 - & gt; AdjVex=j;
P1 - & gt; Nextarc=G.v ertices [I] firstarc;
G.v ertices [I] firstarc=p1;
ArcNode * p2=new ArcNode;
The p2 - & gt; AdjVex=I;
The p2 - & gt; Nextarc=G.v ertices [j] firstarc;
G.v ertices [j] firstarc=p2;
}
}
Bool visited [mvnum];
Int FirstAdjVex (ALGraph G, int u)
{
ArcNode * p;
P=G.v ertices [u] firstarc;
If (p) return p - & gt; AdjVex;
The else return - 1;
}
Int NextAdjVex (ALGraph G, int u, int w)
{
ArcNode * p;
P=G.v ertices [u] firstarc;
While (p - & gt; AdjVex!=w)
P=p - & gt; Nextarc;
If (p) return p - & gt; AdjVex;
The else return - 1;
}
Void BFS (ALGraph G, int v)
{
Int w, u;
SqQueue Q;
ArcNode * p;
coutP=G.v ertices [v] firstarc;
InitQueue (Q);
The EnQueue (Q, v);
while(! QueueEmpty (Q))
{
DeQueue (Q, u);
For (w=FirstAdjVex (G, u); W>=0; W=NextAdjVex (G, u, w))
While (p!=Null)
{
W=p - & gt; AdjVex;
if(! Visited [w]) BFS (G, w);
P=p - & gt; Nextarc;
}
The EnQueue (Q, w);
}
}
Void BFSTraverse (ALGraph G)
{
Int v.
For (n=0; VVisited [v]=false;
For (n=0; Vif(! Visited [v]) BFS (G, v);
}

Int main ()
{
ALGraph G;
CreateGraph (G);
BFSTraverse (G);
return 0;
}

CodePudding user response:

FirstAdjVex and NextAdjVex function, I don't speak much
  • Related