-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
370 lines (335 loc) · 7.46 KB
/
main.cpp
File metadata and controls
370 lines (335 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* main.cpp
*
* Created on: Apr 24, 2020
* Author: AdrianVazquez
*/
/*
*
MIT License
Copyright (c) [2020] [Adrian Vazquez]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <iostream>
#include <time.h>
#include <limits.h>
#include <string.h>
using namespace std;
const int PQ_SIZE = 500;
double prob(void){
double result = rand();
result= result/RAND_MAX;
return result ;
}
struct edgenode {
int y;
int weight;
struct edgenode *next;
};
typedef struct {
edgenode **edges;
int *degree;
int nvertices;
int nedges;
bool directed;
}AdjancencyList;
typedef struct {
int key;
int value;
}queue_elem;
typedef struct{
queue_elem q[PQ_SIZE];
int n;
}queue;
void insert_edge(AdjancencyList *g ,int x, int y, int weight,bool directed){
edgenode *p;
p = new edgenode;
p ->weight = weight;
p ->y =y;
p ->next = g->edges[x];
g->edges[x] = p;
g->degree[x] ++;
if (directed==false){
insert_edge(g,y,x,weight,true);
}
else g->nedges++;
}
class p_queue{
public:
p_queue(){
q = new queue;
}
void insert(int x, int k);
void make(int *s , int *k, int n);
void q_print(void);
int extract_min(void);
void heapsort(int *s,int *k,int n);
int size(void);
~p_queue(){
delete q;
}
private:
int parent(int n);
void bubble_up(int p);
void swap(int a , int b);
void bubble_down( int p);
int young_child(int n);
queue *q;
};
class graph{
public:
graph(int n, bool directed){
List= new AdjancencyList;
int i;
List->nvertices = n;
List->nedges = 0;
List->directed = directed;
List->degree = new int[n];
List->edges = new edgenode*[n];
for (i=0; i<n; i++)
List->degree[i] = 0;
for (i=0; i<n; i++)
List->edges[i] = NULL;
distance = new int[n];
prev = new int[n];
path = new int[n];
pathSize = 0;
cout<<"Class Constructed , empty matrix, size :"<<n<<endl;
hops = 0;
};
void init(int n = 0, bool directed = false);
void print_graph(void);
int *getPath(int u, int w );
int getSize(int w);
void MonteCarlo(double density, int initialCost, int Range);
int *ShortestPath(int start);
~graph(){
release();
cout<<"Class Destructed"<<endl;
}
private:
AdjancencyList *List;
int *distance;
int *path;
int pathSize;
int hops;
int *prev;
void find_path(int start, int end);
void putIn(int p);
int genCost(int initialCost,int Range);
void release(void){
delete [] List->degree;
delete [] List->edges;
delete List;
delete [] distance;
delete [] prev ;
delete [] path;
}
};
int graph::genCost(int initialCost,int Range){
double aux = rand();
aux= aux/RAND_MAX *(Range -initialCost);
int result = initialCost+static_cast<int>(aux);
return result;
}
void graph::MonteCarlo(double density, int initialCost, int Range){
int weight=0;
for(int i=0; i< graph::List->nvertices;++i)
for(int j = i+1;j<graph::List->nvertices;++j)
if ((prob()<density)){
weight = genCost(initialCost, Range);
insert_edge(List, i, j,weight ,false);
}
}
void graph::print_graph( void ){
edgenode *p;
for(int i=0; i< graph::List->nvertices;++i){
cout<<i<<" :";
p=graph::List->edges[i];
while(p != NULL){
cout<<"->"<<p->y;
p = p->next;
}
cout<<endl;
}
}
int *graph::ShortestPath(int start){
int i;
edgenode *p;
int v;
int w;
int weight;
p_queue Q; // xxx Dynamic allocation of size
distance= new int[List->nvertices];
for (i=0; i <List->nvertices;i++){
distance[i] = INT_MAX;
prev[i] = -1;
}
distance[start] =0;
v = start;
Q.insert(v,distance[v]);
p = List->edges[v];
while(Q.size()!=0){
v = Q.extract_min();
p = List->edges[v];
while(p!=NULL){
w = p->y;
weight = p->weight;
if (distance[w] > (distance[v]+ weight)){
distance[w] = distance[v]+ weight;
Q.insert(w,distance[w]);
prev[w] =v;
}
p = p ->next;
}
}
for(i = 0; i < List->nvertices;i ++)
if(distance[i] ==INT_MAX)
distance[i] =-1;
return distance;
}
void graph::find_path(int start, int end){
if ((start == end)||(end == -1)){
putIn(start);
cout<<endl;
}
else{
find_path(start,prev[end]);
putIn(end);
}
}
void graph::putIn(int p){
hops++;
path[hops] = p;
}
int p_queue::parent(int n){
if (n==1)
return -1;
else
return n/2;
}
void p_queue::insert(int x, int k){
if (q->n >PQ_SIZE)
cout<<"Warning, priority queue overflow "<<endl;
else {
q->n ++;
q->q[q->n].value =x;
q->q[q->n].key =k;
bubble_up(q->n);
}
}
void p_queue::bubble_up(int p){
if (parent(p) == -1)
return;
if(q->q[parent(p)].key > q->q[p].key){
swap(p,parent(p));
bubble_up(parent(p));
}
}
void p_queue::swap(int a , int b){
queue_elem aux = p_queue::q->q[a];
p_queue::q->q[a] = p_queue::q->q[b];
p_queue::q->q[b] = aux;
}
void p_queue::make(int *s , int *k ,int n){
q->n = 0;
for(int i =0; i<n; i++){
insert(s[i],k[i]);
}
}
void p_queue::q_print(void){
for(int i =1; i<=p_queue::q->n; i++)
cout<<" "<<p_queue::q->q[i].value;
cout<<endl;
}
int p_queue::extract_min(void){
int min = -1; /* Minumun value*/
if (q->n <=0)
cout<<"Warning: empty priority queue"<<endl;
else {
min = q->q[1].value;
q->q[1] = q->q[q->n];
q->n = q->n -1;
bubble_down(1);
}
return min;
}
void p_queue::bubble_down(int p){
int c;
int min_index;
c = young_child(p);
min_index = p;
for(int i = 0;i<=1;i++)
if (((c+i)<= q->n))
if ((q->q[min_index].key > q->q[c+i].key))
min_index= c+i;
if (min_index!=p){
swap(p, min_index);
bubble_down(min_index);
}
}
void p_queue::heapsort(int *s,int *k,int n){
make(s,k,n);
for(int i=0;i<n;i++)
s[i] = extract_min();
}
int p_queue::young_child(int n){
return 2*n;
}
int p_queue::size(void){
return q->n;
}
int *graph::getPath(int u, int w){
hops =0;
find_path(u,w);
return path;
}
int graph::getSize(int w){
return distance[w];
}
int main() {
srand(time(0)); /* initialize some variables*/
int n = 50; /* Graph size*/
int *a;
double density =0.4;
int range =9;
a = new int[n];
graph g(n,false); /* Initialize the graph */
g.MonteCarlo(density,1,range); /* Fill graph,( precision, start range, range) */
g.print_graph(); /* Print graph in Adjacency List mode*/
a=g.ShortestPath(0); /* Shortest path starting in 0*/
cout<<"Cost array: "; /* Print cost array */
for (int i = 0; i <n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
/* Calculate average */
int len = 0;
int p = 0;
int count = 0;
for(int i = 0; i < n ; i++){
p = g.getSize(i);
if ( p>0){ /* Only count with are connected*/
len = p +len ;
count ++;
}
}
double promedio = static_cast<double>(len)/static_cast<double>(count);
cout<<"The average Cost is: "<<promedio<<endl;
delete [] a;
return 0;
}