1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4 #include <ctime>
5
6 using namespace std;
7 using std::vector;
8
9 struct ArcBox
10 {
11 int headvex,tailvex;
12 ArcBox *hlink,*tlink;
13 float weight;
14 };
15
16
17 template <class TElemType>
18 class Graph
19 {
20 public:
21
22 vector<ArcBox*> back;//全局变量,希望它不要总被初始化,能递增的存储p
23 void CreateOlgraph();
24 void PrintOlgraph();
25 void DFSOlgraph(int v,bool *visited,bool *finished); //表示顺向深度优先搜索
26 void Graph<TElemType>::Backtraverse();
27 void DFSOlgraph(int u,int v);
28 void findcircle();
29
30
31
32 private:
33
34 /* struct ArcBox
35 {
36 int headvex,tailvex;
37 ArcBox *hlink,*tlink;
38 float weight;
39 };*/
40
41
42 template <class TElemType>
43 struct Vertex
44 {
45 TElemType data;
46 ArcBox *firstin,*firstout;
47 };
48
49 struct Olgraph
50 {
51 int vexnum,arcnum;
52 Vertex<TElemType> *vex;
53 };
54 Olgraph olgraph; //有向图的十字链表存储结构
55
56
57 };
58
59 template <class TElemType>
60 void Graph<TElemType>::CreateOlgraph() // 创建十字链表
61 {
62 ArcBox *p,*q;;
63 //输入k
64 int a,b,k,i;
65 cout << "请输入k的值" << endl;
66 cin >> k;
67
68 a = k*k;//顶点数
69 b = 2*k*(k-1);//边数
70 olgraph.vexnum = a;//将输入的顶点数赋给图的顶点数
71 olgraph.arcnum = b;//将输入的边数赋给图的边数
72 olgraph.vex = (Vertex<TElemType> *)malloc(olgraph.vexnum * sizeof(Vertex<TElemType>));
73
74 for(i = 0;i < olgraph.vexnum;i++)
75 {
76 olgraph.vex[i].data = i+1;//给每个顶点赋值
77 olgraph.vex[i].firstin = olgraph.vex[i].firstout = NULL;
78
79 }
80
81 for(i = 0; i < olgraph.vexnum; i++)
82 {
83 if((olgraph.vex[i].data)% k != 0 )
84 {
85 p = (ArcBox *)malloc(sizeof(ArcBox));
86 q = (ArcBox *)malloc(sizeof(ArcBox));
87
88 p ->tailvex = i;
89 p ->tlink = olgraph.vex[i].firstout;olgraph.vex[i].firstout = p;
90 p->headvex = i+1;
91 p->hlink = olgraph.vex[i+1].firstin;olgraph.vex[i+1].firstin = p;
92
93 q ->tailvex = i+1;
94 q ->tlink = olgraph.vex[i+1].firstout;olgraph.vex[i+1].firstout = q;
95 q ->headvex = i;
96 q ->hlink = olgraph.vex[i].firstin;olgraph.vex[i].firstin = q;
97
98 p->weight = 10;
99 q->weight = 10;
100
101 }
102 }
103
104 for(i = 0; i < olgraph.vexnum - k; i++)
105 {
106 p = (ArcBox *)malloc(sizeof(ArcBox));
107 q = (ArcBox *)malloc(sizeof(ArcBox));
108
109 p ->tailvex = i;
110 p ->tlink = olgraph.vex[i].firstout;olgraph.vex[i].firstout = p;
111 p->headvex = i+k;
112 p->hlink = olgraph.vex[i+k].firstin;olgraph.vex[i+k].firstin = p;
113
114 q ->tailvex = i+k;
115 q ->tlink = olgraph.vex[i+k].firstout;olgraph.vex[i+k].firstout = q;
116 q ->headvex = i;
117 q ->hlink = olgraph.vex[i].firstin;olgraph.vex[i].firstin = q;
118
119 p->weight = 10;
120 q->weight = 10;
121
122 }
123
124
12
204
205 }
206
207 //end create
208
209
210 template <class TElemType>
211 void Graph<TElemType>::PrintOlgraph() // 输出十字链表
212 {
213 int k;
214 ArcBox *p;
215 cout << " The Vertex Outdegree = > " << endl;
216 for(k = 0; k <olgraph.vexnum; k ++)
217 {
218 p = olgraph.vex[k].firstout;
219 cout << " Vertex" << k+1 << ": ";
220 while(p != NULL)
221 {
222
223 cout << "<"<<(p->tailvex)+1 << "," << (p->headvex)+1<< ">" << " ";
224 p = p-> tlink;
225
226 }
227 cout << endl;
228
229 }
230
231 cout << endl; cout << endl; cout << endl;
232
233 cout << " The Vertex Indegree = > " << endl;
234 for(k = 0; k <olgraph.vexnum; k ++)
235 {
236 p = olgraph.vex[k].firstin;
237 cout << " Vertex" << k+1 << ": ";
238 while(p != NULL)
239 {
240
241 cout << "<"<<(p->tailvex)+1 << "," << (p->headvex)+1<< ">" << " ";
242 p = p-> hlink;
243
244 }
245 cout << endl;
246
247 }
248
249 }
250
251
252
253 int main()
254 {
255 double duration;
256 clock_t t_start, t_end;
257
258 Graph<int> gph;
259
260 gph.CreateOlgraph();
261 gph.PrintOlgraph();
262
263
264 t_start=clock();
265
266 //gph.findcircle();
267
268 t_end = clock();
269
270 duration = (double)(t_end - t_start)/(double)CLOCKS_PER_SEC;
271
272 cout<< "Cost time: "<<duration <<"s"<<endl;
273 //gph.findvector();
274
275 return 0;
276
277 }