数据结构基础(20) --图的存储结构

来源:互联网 发布:mac屏幕涂层脱落怎么办 编辑:程序博客网 时间:2024/05/20 22:36

图的结构定义

    图是由一个顶点集 V 和一个弧集 E构成的数据结构。

     Graph = (V , E )

   其中,E = {<v,w>| v,w∈V 且 P(v,w)} <v,w>表示从 v 到 w 的一条弧,并称 v 为弧尾,w 为弧头。谓词 P(v,w) 定义了弧 <v,w>的意义或信息。

   由顶点集和边集构成的图称作无向图。

   如果”弧”是有方向的,则称由顶点集和弧集构成的图为有向图。

 

邻接矩阵

 

  定义:矩阵的元素为

 有向图的邻接矩阵为非对称矩阵, 而无向图的邻接矩阵为对称矩阵;

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //无向图的邻接矩阵  
  2. const int MAX_VERTS = 20;  
  3. //顶点  
  4. template <typename Type>  
  5. class Vertex  
  6. {  
  7. public:  
  8.     Vertex(const Type &_node = Type())  
  9.         : node(_node) {}  
  10.   
  11. private:  
  12.     Type node;  
  13. };  
  14. //图  
  15. template <typename Type>  
  16. class Graph  
  17. {  
  18. public:  
  19.     Graph();  
  20.     ~Graph();  
  21.   
  22.     void addVertex(const Type &vertex);  
  23.     void addEdge(int start, int end);  
  24.     void printMatrix();  
  25.   
  26. private:  
  27.     Vertex<Type>* vertexList[MAX_VERTS];  
  28.     int nVerts;  
  29.     int adjMatrix[MAX_VERTS][MAX_VERTS];  
  30. };  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. template <typename Type>  
  2. Graph<Type>::Graph():nVerts(0)  
  3. {  
  4.     for (int i = 0; i < MAX_VERTS; ++i)  
  5.         for (int j = 0; j < MAX_VERTS; ++j)  
  6.             adjMatrix[i][j] = 0;  
  7. }  
  8. template <typename Type>  
  9. Graph<Type>::~Graph()  
  10. {  
  11.     for (int i = 0; i < nVerts; ++i)  
  12.         delete vertexList[i];  
  13. }  
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. template <typename Type>  
  2. void Graph<Type>::addVertex(const Type &vertex)  
  3. {  
  4.     vertexList[nVerts ++] = new Vertex<Type>(vertex);  
  5. }  
  6. template <typename Type>  
  7. void Graph<Type>::addEdge(int start, int end)  
  8. {  
  9.     //无向图  
  10.     adjMatrix[start][end] = 1;  
  11.     adjMatrix[end][start] = 1;  
  12. }  
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. template <typename Type>  
  2. void Graph<Type>::printMatrix()  
  3. {  
  4.     for (int i = 0; i < nVerts; ++i)  
  5.     {  
  6.         for (int j = 0; j < nVerts; ++j)  
  7.             cout << adjMatrix[i][j] << ' ';  
  8.         cout << endl;  
  9.     }  
  10. }  


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //测试代码  
  2. int main()  
  3. {  
  4.     Graph<char> g;  
  5.     g.addVertex('A');   //0  
  6.     g.addVertex('B');   //1  
  7.     g.addVertex('C');   //2  
  8.     g.addVertex('D');   //3  
  9.     g.addVertex('E');   //4  
  10.   
  11.     g.addEdge(0, 1);    //A-B  
  12.     g.addEdge(0, 3);    //A-D  
  13.     g.addEdge(1, 0);    //B-A  
  14.     g.addEdge(1, 4);    //B-E  
  15.     g.addEdge(2, 4);    //C-E  
  16.     g.addEdge(3, 0);    //D-A  
  17.     g.addEdge(3, 4);    //D-E  
  18.     g.addEdge(4, 1);    //E-B  
  19.     g.addEdge(4, 2);    //E-C  
  20.     g.addEdge(4, 3);    //E-D  
  21.   
  22.     g.printMatrix();  
  23.   
  24.     return 0;  
  25. }  

邻接表


注意:在有向图的邻接表中不易找到指向该顶点的弧。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //无向图的邻接表  
  2. template <typename Type>  
  3. class Graph  
  4. {  
  5. public:  
  6.     Graph(int _size = 10);  
  7.     ~Graph();  
  8.   
  9.     void addVertex(const Type &vertex);  
  10.     void addEdge(int start, int end);  
  11.     void printVertex();  
  12.     void printAdjList();  
  13.   
  14. private:  
  15.     Type *vertexList;  
  16.     list<int> *headNode;  
  17.     int size;  
  18.     int nVertex;  
  19. };  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. template <typename Type>  
  2. Graph<Type>::Graph(int _size):size(_size), nVertex(0)  
  3. {  
  4.     vertexList = new Type[size];  
  5.     headNode = new list<int>[size];  
  6. }  
  7. template <typename Type>  
  8. Graph<Type>::~Graph()  
  9. {  
  10.     delete []vertexList;  
  11.     delete []headNode;  
  12. }  
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. template <typename Type>  
  2. void Graph<Type>::addVertex(const Type &vertex)  
  3. {  
  4.     vertexList[nVertex ++] = vertex;  
  5. }  
  6. template <typename Type>  
  7. void Graph<Type>::addEdge(int start, int end)  
  8. {  
  9.     headNode[start].push_back(end);  
  10. }  
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. template <typename Type>  
  2. void Graph<Type>::printVertex()  
  3. {  
  4.     cout << vertexList[0];  
  5.     for (int i = 1; i < nVertex; ++i)  
  6.         cout << ' ' << vertexList[i];  
  7.     cout << endl;  
  8. }  
  9. template <typename Type>  
  10. void Graph<Type>::printAdjList()  
  11. {  
  12.     for (int i = 0; i < nVertex; ++i)  
  13.     {  
  14.         cout << i;  
  15.         for (list<int>::iterator iter = headNode[i].begin();  
  16.                 iter != headNode[i].end();  
  17.                 ++iter)  
  18.             cout << " -> " << *iter;  
  19.         cout << endl;  
  20.     }  
  21. }  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //测试代码  
  2. int main()  
  3. {  
  4.     Graph<char> g;  
  5.     g.addVertex('A');   //0  
  6.     g.addVertex('B');   //1  
  7.     g.addVertex('C');   //2  
  8.     g.addVertex('D');   //3  
  9.     g.addVertex('E');   //4  
  10.     g.printVertex();  
  11.   
  12.     g.addEdge(0, 1);    //A-B  
  13.     g.addEdge(0, 3);    //A-D  
  14.     g.addEdge(1, 0);    //B-A  
  15.     g.addEdge(1, 4);    //B-E  
  16.     g.addEdge(2, 4);    //C-E  
  17.     g.addEdge(3, 0);    //D-A  
  18.     g.addEdge(3, 4);    //D-E  
  19.     g.addEdge(4, 1);    //E-B  
  20.     g.addEdge(4, 2);    //E-C  
  21.     g.addEdge(4, 3);    //E-D  
  22.   
  23.     g.printAdjList();  
  24.   
  25.     return 0;  


原文地址:http://blog.csdn.net/zjf280441589/article/details/42710859

0 0
原创粉丝点击