第七十二课 图的存储结构(上)

 

 

 

顶点和数据元素相关联:

每个顶点关联着一个字符串,这个就是顶点的数据,这个数据关联着工程意义。

添加MatrixGraph.h文件:

  1 #ifndef MATRIXGRAPH_H
  2 #define MATRIXGRAPH_H
  3 
  4 #include "Graph.h"
  5 #include "Exception.h"
  6 #include "DynamicArray.h"
  7 
  8 namespace DTLib
  9 {
 10 // V是顶点类型,E是边的类型
 11 template < int N, typename V, typename E >
 12 class MatrixGraph : public Graph<V, E>
 13 {
 14 protected:
 15     V* m_vertexes[N];
 16     E* m_edges[N][N];
 17     int m_eCount;
 18 public:
 19     MatrixGraph()
 20     {
 21         for(int i = 0; i < vCount(); i++)
 22         {
 23             m_vertexes[i] = NULL;
 24 
 25             for( int j = 0; j < vCount(); j++)
 26             {
 27                 m_edges[i][j] = NULL;
 28             }
 29         }
 30 
 31         m_eCount = 0;
 32     }
 33 
 34     V getVertex(int i)
 35     {
 36         V ret;
 37 
 38         if( !getVertex(i, ret) )
 39         {
 40             THROW_EXCEPTION(InvalidParameterException, "Index i is invalid...");
 41         }
 42 
 43         return ret;
 44     }
 45 
 46     bool getVertex(int i, V& value)
 47     {
 48         bool ret = ((0 <= i) && (i < vCount()));
 49 
 50         if( ret )
 51         {
 52             if( m_vertexes[i] != NULL )
 53             {
 54                 value = *(m_vertexes[i]);
 55             }
 56             else
 57             {
 58                 THROW_EXCEPTION(InvalidOperationException, "No value assigned to this vertex...");
 59             }
 60         }
 61 
 62         return ret;
 63     }
 64     bool setVertex(int i, const V& value)
 65     {
 66         bool ret = ((0 <= i) && (i < vCount()));
 67 
 68         if( ret )
 69         {
 70             V* data = m_vertexes[i];
 71 
 72             if( data == NULL )
 73             {
 74                 data = new V();
 75             }
 76 
 77             if( data != NULL )
 78             {
 79                 *data = value;
 80 
 81                 m_vertexes[i] = data;  //引入data为了异常安全
 82             }
 83             else
 84             {
 85                 THROW_EXCEPTION(NoEnoughMemoryException, "No memory to store new vertex value...");
 86             }
 87         }
 88 
 89         return ret;
 90     }
 91 
 92     SharedPointer< Array<int> > getAdjacent(int i) //获取i的邻接顶点
 93     {
 94         DynamicArray<int>* ret = NULL;
 95 
 96         if( (0 <= i) && (i < vCount()) )
 97         {
 98             int n = 0;
 99 
100             for(int j = 0; j < vCount(); j++)
101             {
102                 if( m_edges[i][j] != NULL )
103                 {
104                     n++;
105                 }
106             }
107 
108             ret = new DynamicArray<int>(n);
109 
110             if( ret != NULL )
111             {
112                 for(int j=0,k=0; j<vCount(); j++)
113                 {
114                     if( m_edges[i][j] != NULL )
115                     {
116                         ret->set(k++, j);
117                     }
118                 }
119             }
120             else
121             {
122                 THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create new ret object...");
123             }
124         }
125         else
126         {
127             THROW_EXCEPTION(InvalidParameterException, "Index i is invalid...");
128         }
129 
130         return ret;
131     }
132 
133     E getEdge(int i, int j)
134     {
135         E ret;
136 
137         if( !getEdge(i, j, ret) )
138         {
139             THROW_EXCEPTION(InvalidParameterException, "Index <i,j> is invalid...");
140         }
141 
142         return ret;
143     }
144 
145     bool getEdge(int i, int j, E& value)
146     {
147         bool ret = ((0 <= i) && (i < vCount()) &&
148                     (0 <= j) && (j < vCount()) );
149 
150         if( ret )
151         {
152             if( m_edges[i][j] != NULL )
153             {
154                 value = *(m_edges[i][j]);
155             }
156             else
157             {
158                 THROW_EXCEPTION(InvalidOperationException, "No value assigned to this edge...");
159             }
160         }
161 
162         return ret;
163     }
164 
165     bool setEdge(int i, int j, const E& value)
166     {
167         bool ret = ((0 <= i) && (i < vCount()) &&
168                     (0 <= j) && (j < vCount()) );
169 
170         if( ret )
171         {
172             E* ne = m_edges[i][j];
173 
174             if( ne == NULL )
175             {
176                 //i和j之间没有边,现在要设置一条边
177                 ne = new E();
178 
179                 if( ne != NULL )
180                 {
181                     *ne = value;
182 
183                     m_edges[i][j] = ne;
184 
185                     m_eCount++;
186                 }
187                 else
188                 {
189                     THROW_EXCEPTION(NoEnoughMemoryException, "No memory to store new edge value...");
190                 }
191             }
192             else
193             {
194                 *ne = value;
195             }
196         }
197 
198         return ret;
199     }
200 
201     bool removeEdge(int i, int j)
202     {
203         bool ret = ((0 <= i) && (i < vCount()) &&
204                     (0 <= j) && (j < vCount()) );
205 
206         if( ret )
207         {
208             E* toDel = m_edges[i][j];
209 
210             m_edges[i][j] = NULL;
211 
212             if( toDel != NULL )
213             {
214                 m_eCount--;
215 
216                 delete toDel;
217             }
218         }
219 
220         return ret;
221     }
222 
223     int vCount()
224     {
225         return N;
226     }
227 
228     int eCount()
229     {
230         return m_eCount;
231     }
232 
233     int OD(int i)
234     {
235         int ret = 0;
236 
237         if( (0 <= i) && (i < vCount()) )
238         {
239             for(int j = 0; j < vCount(); j++)
240             {
241                 if( m_edges[i][j] != NULL )
242                 {
243                     ret++;
244                 }
245             }
246         }
247         else
248         {
249             THROW_EXCEPTION(InvalidParameterException, "Index i is invalid...");
250         }
251 
252         return ret;
253     }
254 
255     int ID(int i)
256     {
257         int ret = 0;
258 
259         if( (0 <= i) && (i < vCount()) )
260         {
261             for(int j = 0; j < vCount(); j++)
262             {
263                 if( m_edges[j][i] != NULL )
264                 {
265                     ret++;
266                 }
267             }
268         }
269         else
270         {
271             THROW_EXCEPTION(InvalidParameterException, "Index i is invalid...");
272         }
273 
274         return ret;
275     }
276 
277     ~MatrixGraph()
278     {
279         for(int i = 0; i < vCount(); i++)
280         {
281             for( int j = 0; j < vCount(); j++)
282             {
283                 delete m_edges[i][j];
284             }
285 
286             delete m_vertexes[i];
287         }
288     }
289 };
290 
291 }
292 
293 #endif // MATRIXGRAPH_H

测试程序如下:

 1 #include <iostream>
 2 #include "BTreeNode.h"
 3 #include "MatrixGraph.h"
 4 
 5 using namespace std;
 6 using namespace DTLib;
 7 
 8 
 9 int main()
10 {
11     MatrixGraph<3, int, int> g;
12 
13     g.setEdge(0, 1, 1);
14     g.setEdge(1, 0, 2);
15     g.setEdge(1, 2, 3);
16 
17     cout << "vCount : " << g.vCount() << endl;
18     cout << "eCount : " << g.eCount() << endl;
19     cout << "ID(1) : " << g.ID(1) << endl;
20     cout << "OD(1) : " << g.OD(1) << endl;
21     cout << "TD(1) : " << g.TD(1) << endl;
22 
23     cout << "W(0, 1) : " << g.getEdge(0, 1) << endl;
24     cout << "W(1, 0) : " << g.getEdge(1, 0) << endl;
25     cout << "W(1, 2) : " << g.getEdge(1, 2) << endl;
26 
27     SharedPointer< Array<int> > aj = g.getAdjacent(1); //获取1顶点的邻接顶点
28 
29     cout << endl;
30     for(int i = 0; i < aj->length(); i++)
31     {
32         cout << (*aj)[i] << " ";
33     }
34 
35     cout << endl;
36 
37     cout << "delete edge : " << endl;
38     g.removeEdge(0, 1);
39 
40     cout << "eCount : " << g.eCount() << endl;
41 
42     g.setVertex(0, 100);
43 
44     cout << "V(0) : " << g.getVertex(0) << endl;
45 
46     cout << "W(0, 1) : " << g.getEdge(0, 1) << endl;
47     return 0;
48 }

结果如下:

小结:

猜你喜欢

转载自www.cnblogs.com/wanmeishenghuo/p/9704205.html