图的遍历方法及代码
一般来讲,经典的遍历方法是深度优先搜索遍历和广度优先搜索遍历算法,下面给出其具体实现的源程序。
//图ª? 的Ì? 广? 度¨¨优?先¨¨搜? 说¦Ì遍À¨¦历¤¨²
typedef struct
{
int Data [MAXNUM ];
int head ;
int tail ;
}Sequeue ;
void Queueinit (Sequeue *Q )
{
Q ->head =0;
Q ->tail =0;
}
int Queuepush (Sequeue *Q , int ch )
{
if ((Q ->tail + 1)%MAXNUM == Q ->head )
return 0;
Q ->Data [Q ->tail ]=ch ;
Q ->tail =(Q ->tail +1)%MAXNUM ;
return 1;
}
int Queuepop (Sequeue *Q , int *ch )
{
if (Q ->head ==Q ->tail )
return 0;
*ch =Q ->Data [Q ->head ];
Q ->head =(Q ->head +1)%MAXNUM ;
return 1;
}
int QueueIsempty (Sequeue *Q )
{
if (Q ->head == Q ->tail )
return 1;
else
return 0;
}
//广? 度¨¨优?先¨¨遍À¨¦历¤¨²函¡¥数ºy
void BFSM (Maxtrixgraph *G , int k )
{
//start from vertex k
int i , j ;
Sequeue Q ;
Queueinit (&Q );
G ->Istrave [k ]=1;
cout "Vertex [k ];
Queuepush (&Q , k );
while (! QueueIsempty (&Q ))
{
Queuepop (&Q ,&i );
for (j =0;j Vertex_num; j ++)
if (G ->Edges [i ][j ]!=MAXVALUE && !G ->Istrave [j ]) {
cout "Vertex [j ];
G ->Istrave [j ]=1;
Queuepush (&Q , j );
}
}
}
//广? 度¨¨优?先¨¨搜? 索¡Â算? 法¤¡§的Ì? 测a 试º? 程¨¬序¨°
void BFSTraverse (Maxtrixgraph *G )
{
for (int i =0;i Vertex_num; i ++)
G ->Istrave [i ]=0;
cout Vertex_num; i ++)
{
if (! G ->Istrave [i ])
BFSM (G , i );
}
cout
}
//图ª? 的Ì? 深¦? 度¨¨优?先¨¨搜? 索¡Â遍À¨¦历¤¨²算? 法¤¡§
void DFSM (Maxtrixgraph *G , int i )
{
//start from vertex i
G ->Istrave [i ]=1;
cout "Vertex [i ];
for (int j =0;j Vertex_num; j ++)
{
if (G ->Edges [i ][j ]!=MAXVALUE && !G ->Istrave [j ])
DFSM (G , j );
}
}
//图ª? 的Ì? 深¦? 度¨¨优?先¨¨搜? 索¡Â遍À¨¦历¤¨²的Ì? 测a 试º? 程¨¬序¨° void DFSTraverse (Maxtrixgraph *G )
{
for (int i =0;i Vertex_num; i ++)
G ->Istrave [i ]=0;
cout Vertex_num; i ++)
{
if (!G ->Istrave [i ])
DFSM (G , i );
}
cout
}
然后结合图的邻接矩阵创建方法,给出测试全过程的代码: #include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std ;
#define MAXNUM 26
#define MAXVALUE 32767
//矩? 阵¨领¢¨¬接¨图ª? 的Ì? 定¡§义°?
typedef struct
{
char Vertex [MAXNUM ]; //vertex
int Edges [MAXNUM ][MAXNUM ]; //edge
int Istrave [MAXNUM ]; //flag
int Vertex_num; //number of vertex int Edges_num; //number of edges
int Glaphtype ; //0:no direction; 1:direction }Maxtrixgraph ;
//创ä¡ä建¡§矩? 阵¨领¢¨¬接¨矩? 阵¨
void Creatmatrixgraph (Maxtrixgraph *G )
{
int i , j , k , weight ;
char start , end ;
cout Vertex_num; i ++)
{
cout
cin >>G ->Vertex [i ];
}
cout Edges_num; k ++)
{
cout
cin >>start >>end >>weight ;
i =0;
j =0;
while (G ->Vertex [i ] != start )
{
i ++;
}
while (G ->Vertex [j ] != end )
{
j ++;
}
G ->Edges [i ][j ] = weight ;
if (G ->Glaphtype == 0)
G ->Edges [j ][i ] = weight ;
}
}
//输º? 出? 邻¢¨²接¨矩? 阵¨
void Outmatrix (Maxtrixgraph *G )
{
int i , j ;
cout
for (j =0;j Vertex_num; j ++)
cout Vertex [j ];
cout Vertex_num; i ++)
{
cout Vertex [i ];
for (j =0;j Vertex_num; j ++)
{
if (G ->Edges [i ][j ] == MAXVALUE )
cout
else
cout Edges [i ][j ];
}
cout
}
}
主函数调用为:
#include "stdafx.h"
#include "example32_graph.h"
#include
#include
int main ()
{
Maxtrixgraph G ;
int i , j ;
char select ;
do
{
cout
cin >>G . Glaphtype ;
cout >G . Vertex_num>>G . Edges_num;
for (i =0;i
for (j =0;j
G . Edges [i ][j ]=MAXVALUE ;
Creatmatrixgraph (&G );
cout
DFSTraverse (&G );
BFSTraverse (&G );
cout >select ;
} while (select !='N' && select !='n' );
getch ();
return 0;
}
实验结果示例: