假设你已经了解了DFS和BFS
废话不多说 上代码
遍历的图如图所示
该遍历为无向图
若需有向图只需将arc数组修改即可
遍历的图在下方
邻接矩阵写DFS
遍历每一行找到arc数组中有1的
将visited转为true
#include <iostream>
using namespace std;
char vertex[] = {'A', 'B', 'C', 'D', 'E'};
struct VertexNode
{
char data;
};
void AdjMatrix(char arc[][5])
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
arc[i][j] = 0;
}
}
arc[0][1] = arc[0][3] = arc[0][4] = 1;
arc[1][3] = arc[1][0] = 1;
arc[2][3] = 1;
arc[3][0] = arc[3][2] = arc[3][1] = 1;
arc[4][0] = 1;
}
void DFS_matrix(char DFS[][5], int i, bool *visited)
{
visited[i] = true;
for (int j = 0; j < 5; j++)
{
if (!visited[j] && DFS[i][j] == 1)
{
cout << vertex[j] << ",";
DFS_matrix(DFS, j, visited);
}
}
}
void DFS_AdjMatrix(char DFS[][5])
{
bool visited[5] = {0};
for (int i = 0; i < 5; i++)
{
if (!visited[i])
{
cout << vertex[i] << ",";
DFS_matrix(DFS, i, visited);
}
}
}
int main(int argc, char const *argv[])
{
char DFS[5][5] = {0};
AdjMatrix(DFS);
DFS_AdjMatrix(DFS);
system("pause");
return 0;
}
链表写DFS
链表简图
#include <iostream>
using namespace std;
char vertex[] = {'A', 'B', 'C', 'D', 'E'};
typedef struct VertexNode
{
char data;
struct ArcNode * firstArc;
}VertexNode;
typedef struct ArcNode
{
char data;
struct ArcNode *nextarc;
}ArcNode;
ArcNode *insertArcNode(char name)
{
ArcNode *p = new ArcNode;
p->data = name;
p->nextarc = NULL;
return p;
}
VertexNode * AdjList()
{
ArcNode *p=NULL;
VertexNode * List_head = new VertexNode[5];
int c=0;
List_head[c].data= 'A';
p=List_head[c].firstArc=insertArcNode('B');
p=p->nextarc=insertArcNode('D');
p=p->nextarc=insertArcNode('E');
c++;
List_head[c].data='B';
p=List_head[c].firstArc=insertArcNode('A');
p=p->nextarc=insertArcNode('D');
c++;
List_head[c].data='C';
p=List_head[c].firstArc=insertArcNode('D');
c++;
List_head[c].data='D';
p=List_head[c].firstArc=insertArcNode('A');
p=p->nextarc=insertArcNode('B');
p=p->nextarc=insertArcNode('C');
c++;
List_head[c].data='E';
p=List_head[c].firstArc=insertArcNode('A');
return List_head;
}
void DFS_list(VertexNode * Graph,int i,bool *visited)
{
visited[i]=true;
cout<<vertex[i]<<",";
ArcNode *p= Graph[i].firstArc;
while (p!=NULL)
{
int temp = p->data -'A';
if(!visited[temp]) DFS_list(Graph,temp,visited);
p=p->nextarc;
}
}
void DFS_AdjList(VertexNode *Graph)
{
bool visited[5]={0};
for (int i = 0; i < 5; i++)
{
if(!visited[i])
{
DFS_list(Graph,i,visited);
}
}
}
int main(int argc, char const *argv[])
{
VertexNode *Graph = AdjList();
DFS_AdjList(Graph);
system("pause");
return 0;
}
BFS
#include <iostream>
#include <queue>
using namespace std;
char vertex[] = {'A', 'B', 'C', 'D', 'E'};
typedef struct VertexNode
{
char data;
struct ArcNode * firstArc;
}VertexNode;
typedef struct ArcNode
{
char data;
struct ArcNode *nextarc;
}ArcNode;
ArcNode *insertArcNode(char name)
{
ArcNode *p = new ArcNode;
p->data = name;
p->nextarc = NULL;
return p;
}
VertexNode * AdjList()
{
ArcNode *p=NULL;
VertexNode * List_head = new VertexNode[5];
int c=0;
List_head[c].data= 'A';
p=List_head[c].firstArc=insertArcNode('B');
p=p->nextarc=insertArcNode('D');
p=p->nextarc=insertArcNode('E');
c++;
List_head[c].data='B';
p=List_head[c].firstArc=insertArcNode('A');
p=p->nextarc=insertArcNode('D');
c++;
List_head[c].data='C';
p=List_head[c].firstArc=insertArcNode('D');
c++;
List_head[c].data='D';
p=List_head[c].firstArc=insertArcNode('A');
p=p->nextarc=insertArcNode('B');
p=p->nextarc=insertArcNode('C');
c++;
List_head[c].data='E';
p=List_head[c].firstArc=insertArcNode('A');
return List_head;
}
void BFS_list(VertexNode *Graph, int i, bool *visited, queue<char> &Q)
{
cout << Q.front() << ",";
Q.pop();
ArcNode *p = Graph[i].firstArc;
while (p != NULL)
{
/*(p->data - 'A')代表顶点的序号*/
if (*(visited + (p->data - 'A')) == 0) //检测邻接点是否被访问过
{
*(visited + (p->data - 'A')) == true;
Q.push(p->data);
}
p = p->nextarc;
}
}
void BFS_AdjList(VertexNode *Graph)
{
bool visited[5]={0};
queue<char> Q;
for(int i = 0;i<5;i++)
{
if(!visited[i])
{
visited[i]=true;
Q.push(vertex[i]);
BFS_list(Graph,i,visited,Q);
}
}
}
int main(int argc, char const *argv[])
{
VertexNode *Graph = AdjList();
BFS_AdjList(Graph);
system("pause");
return 0;
}