C++实现图的搜索(DFS和BFS)

  1 #include<iostream>
  2 #include<stack>
  3 
  4 #include<queue>
  5 #define Max 20
  6 using namespace std;
  7 
  8 class Vertex
  9 {
 10 public:
 11     Vertex(char lab)
 12     {
 13         Label=lab;
 14     wasVisited=false;
 15     }
 16 public:
 17     bool wasVisited;
 18     char Label;
 19 };
 20 
 21 
 22 class Graph
 23 {
 24 
 25 public:
 26     Graph();//构造函数
 27     ~Graph();//析构函数
 28     void addVertex(char lab);//增加一个节点
 29     void addEdge(int start,int end);//增加一条边,起点到终点
 30     void printMatrix();//打印出矩阵
 31     void showVertex(int v);
 32     void DFS();
 33     void BFS();
 34 private:
 35     Vertex* vertexList[Max];   //存放每个节点的指针的数组
 36     int nVerts;//实际数量
 37     int adjMat[Max][Max];//矩阵
 38     int getAdjUnvisitedVertex(int v);//获得其相邻的节点 在邻接矩阵里找其最近的
 39 };
 40 
 41 void Graph::DFS()
 42 {
 43     stack<int>gStack;
 44     vertexList[0]->wasVisited=true;
 45     showVertex(0);
 46     gStack.push(0);
 47     int v;
 48     while(gStack.size()>0)
 49     {
 50         v=getAdjUnvisitedVertex(gStack.top());
 51         if(v==-1)
 52         {
 53             cout<<"出:"<<gStack.top()<<endl;//查看出栈情况
 54             gStack.pop();
 55         }
 56         else
 57         {
 58             vertexList[v]->wasVisited=true;
 59             showVertex(v);
 60             gStack.push(v);
 61         }
 62 
 63     }
 64 
 65     for(int j=0;j<nVerts;j++)        //重新置为未访问
 66         vertexList[j]->wasVisited=false;
 67 
 68 
 69 }
 70 void Graph::BFS()
 71 {
 72     queue<int> gQueue;
 73     vertexList[0]->wasVisited=true;
 74     showVertex(0);
 75     gQueue.push(0);
 76     int vert1,vert2;
 77     while(gQueue.size()>0)
 78     {
 79         vert1=gQueue.front();
 80         gQueue.pop();
 81         vert2=getAdjUnvisitedVertex(vert1);
 82         while(vert2!=-1)
 83         {
 84             vertexList[vert2]->wasVisited=true;
 85             showVertex(vert2);
 86             gQueue.push(vert2);
 87             vert2=getAdjUnvisitedVertex(vert1);
 88         }
 89     }
 90     cout<<endl;
 91     for(int j=0;j<nVerts;j++)        //重新置为未访问
 92         vertexList[j]->wasVisited=false;
 93 
 94 
 95 }
 96 
 97 int Graph::getAdjUnvisitedVertex(int v)//得到其相邻节点
 98 {
 99     for(int j=0;j<nVerts;j++)
100     {
101         if((adjMat[v][j]==1)&&(vertexList[j]->wasVisited==false))//找其第一个相邻(邻接)的且没有被访问过的
102             return j;
103     }
104 
105     return -1;
106 
107 }
108 void Graph::showVertex(int v)   //展示该下标对应的节点
109 {
110     cout<<vertexList[v]->Label<<" ";
111 }
112 Graph::Graph()
113 {
114 
115     nVerts=0;
116     for(int i=0;i<Max;i++)
117         for(int j=0;j<Max;j++)
118             adjMat[i][j]=0;
119 
120 }
121 void Graph::addVertex(char lab)
122 {
123 
124     vertexList[nVerts++]=new Vertex(lab);//
125 
126 }
127 void Graph::addEdge(int start,int end)
128 {
129     adjMat[start][end]=adjMat[end][start]=1;
130 
131 }
132 
133 void Graph::printMatrix()
134 {
135 
136     for(int i=0;i<nVerts;i++)
137     {
138         for(int j=0;j<nVerts;j++)
139         {
140             cout<<adjMat[i][j]<<" ";
141         }
142         cout<<endl;
143     }
144 }
145 
146 Graph::~Graph()
147 {
148 
149     for(int i=0;i<nVerts;i++)
150     {
151         delete vertexList[i];
152     }
153 }
154 int main()
155 {
156 
157     Graph g;
158     g.addVertex('A');//0
159     g.addVertex('B');//1
160     g.addVertex('C');//2
161     g.addVertex('D');//3
162     g.addVertex('E');//4
163     g.addEdge(0,1);//A-B
164     g.addEdge(1,4);//B-E
165     g.addEdge(2,4);//C-E
166 
167     g.addEdge(0,3);//A-D
168     g.addEdge(3,0);
169     g.addEdge(3,4);
170 
171     g.printMatrix();
172 
173 
174     cout<<"DFS搜索"<<endl;
175     g.DFS();
176     cout<<endl;
177     cout<<"BFS搜索"<<endl;
178     g.BFS();
179     return 0;
180 }

猜你喜欢

转载自www.cnblogs.com/libin123/p/10420216.html