图-邻接矩阵
#include<iostream>
#define MAX_VERTS 20
using namespace std;
//邻接矩阵 浪费空间class Vertex { //顶点
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]内为指针}
}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会不会重复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; //存顶点的数组list<int>* HeadNodes; int n; //顶点个数int nVerts; //计数顶点数
public:Graph(const int vertices) :n(vertices){VertexList = new T*[n]; //n个数组HeadNodes = new list<int>[n]; //n个链表分别存在n个数组里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会不会重复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深度优先搜索
只有有下一个结点就一直往下走,知道没有再往回走
图-BFS广度优先搜索
一层一层搜索
#include<iostream>
#include<stack>
#include<queue>
#define MAX_VERTS 20
using namespace std;
//邻接矩阵 浪费空间class Vertex { //顶点
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; //保存顶点下标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]内为指针}
}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会不会重复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 << "深度优先搜索的结果:";g.DFS();cout << "广度优先搜索的结果:";g.BFS();system("pause");return 0;
}
运行结果: