DFS深度优先搜索,使用堆栈来实现
BFS广度优先搜索,使用队列来实现
下面先看DFS
#include<iostream>
#include<stack>
#define MAX_VERTS 20//最大节点数
using namespace std;
//节点类
class Vertex{
public:
Vertex(char label){
Label = label;
wasVisited = false;
}
public:
bool wasVisited;
char Label;
};
class Graph{
public:
Graph();
~Graph();
void addVertex(char lab);
void addEdge(int start, int end);
void printMatrix();
void showVertex(int v);
void DFS();
private:
Vertex* vertexList[MAX_VERTS];
int nVerts;//顶点个数
int adjMat[MAX_VERTS][MAX_VERTS];
int getAdjUnivisitedVertex(int v);//获取邻接矩阵下一个未被访问的节点
};
void Graph::DFS(){
stack<int> gStack;
vertexList[0]->wasVisited = true;
showVertex(0);
gStack.push(0);
int v;
while (gStack.size() > 0){
v = getAdjUnivisitedVertex(gStack.top());
if (v == -1)
gStack.pop();
else{
vertexList[v]->wasVisited = true;
showVertex(v);
gStack.push(v);
}
}
cout << endl;
for (int j = 0; j < nVerts; j++)
vertexList[j]->wasVisited = false;
}
int Graph::getAdjUnivisitedVertex(int v){
for (int j = 0; j < nVerts; j++){
if ((adjMat[v][j] == 1) && (vertexList[j]->wasVisited == false))
return j;
}
return -1;
}
void Graph::showVertex(int v){
cout << vertexList[v]->Label << " ";
}
Graph::Graph(){
nVerts = 0;
for (int i = 0; i < MAX_VERTS; i++){
for (int j = 0; j < MAX_VERTS; j++){
adjMat[i][j] = 0;
}
}
}
void Graph::addVertex(char lab){
vertexList[nVerts++] = new Vertex(lab);
}
void Graph::addEdge(int start, int end){
adjMat[start][end] = 1;
adjMat[end][start] = 1;
}
void Graph::printMatrix(){
for (int i = 0; i < nVerts; i++){
for (int j = 0; j < nVerts; j++){
cout << adjMat[i][j] << " ";
}
cout << endl;
}
}
Graph::~Graph(){
for (int i = 0; i < nVerts; i++){
delete vertexList[i];
}
}
int main(){
cout << "测试邻接矩阵:" << endl;
Graph g;
g.addVertex('A');//0
g.addVertex('B');//1
g.addVertex('C');//2
g.addVertex('D');//3
g.addVertex('E');//4
g.addEdge(0, 1);
g.addEdge(0, 3);
g.addEdge(1, 0);
g.addEdge(1, 4);
g.addEdge(2, 4);
g.addEdge(3, 0);
g.addEdge(3, 4);
g.addEdge(4, 1);
g.addEdge(4, 2);
g.addEdge(4, 3);
g.printMatrix();
g.DFS();
system("pause");
return 0;
}
下面看BFS
#include<iostream>
#include<stack>
#include<queue>
#define MAX_VERTS 20
using namespace std;
class Vertex{
public:
Vertex(char label){
Label = label;
wasVisited = false;
}
public:
bool wasVisited;
char Label;
};
class Graph{
public:
Graph();
~Graph();
void addVertex(char lab);
void addEdge(int start, int end);
void printMatrix();
void showVertex(int v);
void DFS();
void BFS();
private:
Vertex* vertexList[MAX_VERTS];
int nVerts;//顶点个数
int adjMat[MAX_VERTS][MAX_VERTS];
int getAdjUnivisitedVertex(int v);//获取邻接矩阵下一个未被访问的节点
};
void Graph::DFS(){
stack<int> gStack;
vertexList[0]->wasVisited = true;
showVertex(0);
gStack.push(0);
int v;
while (gStack.size() > 0){
v = getAdjUnivisitedVertex(gStack.top());
if (v == -1)
gStack.pop();
else{
vertexList[v]->wasVisited = true;
showVertex(v);
gStack.push(v);
}
}
cout << endl;
for (int j = 0; j < nVerts; j++)
vertexList[j]->wasVisited = false;
}
void Graph::BFS(){
queue<int> gQueue;
vertexList[0]->wasVisited = true;
showVertex(0);
gQueue.push(0);
int vert1, vert2;
while (gQueue.size()>0){
vert1 = gQueue.front();
gQueue.pop();
vert2 = getAdjUnivisitedVertex(vert1);
while (vert2 != -1){
vertexList[vert2]->wasVisited = true;
showVertex(vert2);
gQueue.push(vert2);
vert2 = getAdjUnivisitedVertex(vert1);
}
}
cout << endl;
for (int j = 0; j < nVerts; j++)
vertexList[j]->wasVisited = false;
}
int Graph::getAdjUnivisitedVertex(int v){
for (int j = 0; j < nVerts; j++){
if ((adjMat[v][j] == 1) && (vertexList[j]->wasVisited == false))
return j;
}
return -1;
}
void Graph::showVertex(int v){
cout << vertexList[v]->Label << " ";
}
Graph::Graph(){
nVerts = 0;
for (int i = 0; i < MAX_VERTS; i++){
for (int j = 0; j < MAX_VERTS; j++){
adjMat[i][j] = 0;
}
}
}
void Graph::addVertex(char lab){
vertexList[nVerts++] = new Vertex(lab);
}
void Graph::addEdge(int start, int end){
adjMat[start][end] = 1;
adjMat[end][start] = 1;
}
void Graph::printMatrix(){
for (int i = 0; i < nVerts; i++){
for (int j = 0; j < nVerts; j++){
cout << adjMat[i][j] << " ";
}
cout << endl;
}
}
Graph::~Graph(){
for (int i = 0; i < nVerts; i++){
delete vertexList[i];
}
}
int main(){
cout << "测试邻接矩阵:" << endl;
Graph g;
g.addVertex('A');//0
g.addVertex('B');//1
g.addVertex('C');//2
g.addVertex('D');//3
g.addVertex('E');//4
g.addEdge(0, 1);
g.addEdge(0, 3);
g.addEdge(1, 0);
g.addEdge(1, 4);
g.addEdge(2, 4);
g.addEdge(3, 0);
g.addEdge(3, 4);
g.addEdge(4, 1);
g.addEdge(4, 2);
g.addEdge(4, 3);
g.printMatrix();
cout << "测试深度优先搜索:" << endl;
g.DFS();
cout << "测试广度优先搜索:" << endl;
g.BFS();
system("pause");
return 0;
}
总结:图的常用两种搜索算法,使用邻接矩阵表示图的边。使用数组来表示邻接矩阵。