数据结构-图及其遍历
生活随笔
收集整理的這篇文章主要介紹了
数据结构-图及其遍历
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
圖-鄰接矩陣
#include<iostream> #define MAX_VERTS 20 using namespace std; //鄰接矩陣 浪費(fèi)空間class Vertex { //頂點(diǎn) public:Vertex(char lab) { Label = lab; } private:char Label; };class Graph { //圖 public:Graph();~Graph();void addVertex(char lab);void addEdge(int start, int end);void printMatrix(); private:Vertex* vertexList[MAX_VERTS];int nVerts;int adjMat[MAX_VERTS][MAX_VERTS]; }; 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]; //vertexList[i]內(nèi)為指針} }int main() {Graph g;g.addVertex('A'); //0g.addVertex('B'); //1g.addVertex('C'); //2g.addVertex('D'); //3g.addVertex('E'); //4g.addEdge(0, 1); //A-Bg.addEdge(0, 3); //A-Dg.addEdge(1, 0); //B-A g.addEdge(1, 4); //B-E 和 E-B會(huì)不會(huì)重復(fù)g.addEdge(2, 4); //C-Eg.addEdge(3, 0); //D-Ag.addEdge(3, 4); //D-Eg.addEdge(4, 1); //E-Bg.addEdge(4, 2); //E-Cg.addEdge(4, 3); //E-Dg.printMatrix();system("pause");return 0; }?
圖-鄰接表
#include<iostream> #include<list> using namespace std; class Vertex {};template<class T> class Graph { private:T** VertexList; //存頂點(diǎn)的數(shù)組list<int>* HeadNodes; int n; //頂點(diǎn)個(gè)數(shù)int nVerts; //計(jì)數(shù)頂點(diǎn)數(shù) public:Graph(const int vertices) :n(vertices){VertexList = new T*[n]; //n個(gè)數(shù)組HeadNodes = new list<int>[n]; //n個(gè)鏈表分別存在n個(gè)數(shù)組里nVerts = 0;}~Graph(){delete[] VertexList;delete[] HeadNodes;}void addVertext(T* v);void addEdge(int start, int end);void printVertice();void printAdjList();};template<class T> void Graph<T>::addVertext(T* v) {VertexList[nVerts++] = v; }template<class T> void Graph<T>::addEdge(int start,int end) {HeadNodes[start].push_back(end); }template<class T> void Graph<T>::printVertice() {for (int i = 0; i < nVerts; i++){cout << *VertexList[i] << " ";}cout << endl; } template<class T> void Graph<T>::printAdjList() {for (int i = 0; i < nVerts; i++){cout << i << "->";for (list<int> ::iterator iter = HeadNodes[i].begin(); iter != HeadNodes[i].end();++iter){cout << *iter << "->";}cout << "end" << endl;;}}int main() {Graph<char> g(5);char a = 'A';char b = 'B';char c = 'C';char d = 'D';char e = 'E';g.addVertext(&a);g.addVertext(&b);g.addVertext(&c);g.addVertext(&d);g.addVertext(&e);g.printVertice();g.addEdge(0, 1); //A-Bg.addEdge(0, 3); //A-Dg.addEdge(1, 0); //B-A g.addEdge(1, 4); //B-E 和 E-B會(huì)不會(huì)重復(fù)g.addEdge(2, 4); //C-Eg.addEdge(3, 0); //D-Ag.addEdge(3, 4); //D-Eg.addEdge(4, 1); //E-Bg.addEdge(4, 2); //E-Cg.addEdge(4, 3); //E-Dg.printAdjList();system("pause");return 0; }圖的搜索
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
圖-DFS深度優(yōu)先搜索
只有有下一個(gè)結(jié)點(diǎn)就一直往下走,知道沒有再往回走
?
圖-BFS廣度優(yōu)先搜索
一層一層搜索
#include<iostream> #include<stack> #include<queue> #define MAX_VERTS 20 using namespace std; //鄰接矩陣 浪費(fèi)空間class Vertex { //頂點(diǎn) public:Vertex(char lab) { Label = lab; 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 getAdjUnvisitedVertex(int v); }; void Graph::DFS() {stack<int> gStack; //保存頂點(diǎn)下標(biāo)vertexList[0]->wasVisited = true;showVertex(0);gStack.push(0);int v;while (gStack.size() > 0){v = getAdjUnvisitedVertex(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 = getAdjUnvisitedVertex(vert1);while (vert2 != -1){vertexList[vert2]->wasVisited = true;showVertex(vert2);gQueue.push(vert2);vert2 = getAdjUnvisitedVertex(vert1);}}cout << endl;for (int i = 0; i < nVerts; i++){vertexList[i]->wasVisited = false;} } int Graph::getAdjUnvisitedVertex(int v) {for (int i = 0; i < nVerts; i++){if ((adjMat[v][i] == 1) && (vertexList[i]->wasVisited == false))return i;}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]; //vertexList[i]內(nèi)為指針} }int main() {Graph g;g.addVertex('A'); //0g.addVertex('B'); //1g.addVertex('C'); //2g.addVertex('D'); //3g.addVertex('E'); //4g.addEdge(0, 1); //A-Bg.addEdge(0, 3); //A-Dg.addEdge(1, 0); //B-A g.addEdge(1, 4); //B-E 和 E-B會(huì)不會(huì)重復(fù)g.addEdge(2, 4); //C-Eg.addEdge(3, 0); //D-Ag.addEdge(3, 4); //D-Eg.addEdge(4, 1); //E-Bg.addEdge(4, 2); //E-Cg.addEdge(4, 3); //E-Dg.printMatrix();cout << "深度優(yōu)先搜索的結(jié)果:";g.DFS();cout << "廣度優(yōu)先搜索的結(jié)果:";g.BFS();system("pause");return 0; }?運(yùn)行結(jié)果:
?
?
總結(jié)
以上是生活随笔為你收集整理的数据结构-图及其遍历的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STL17-函数对象
- 下一篇: 图像分类 数据准备(将文件夹中所有图片路