-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherdos-renyi-graph.c
474 lines (403 loc) · 14.3 KB
/
erdos-renyi-graph.c
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// A C Program to demonstrate adjacency list representation of graphs
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "random64.h"
//////
// Voltage List
/////
// a single node in the network
struct VoltNode
{
double voltageNew;
double voltageOld;
double conductance;
};
// structure represents a list of volt nodes
struct VoltList
{
struct VoltNode *head; // pointer to the head
};
struct NodesVoltages
{
int n;
struct VoltList *array;
};
// utility function for new volt nodes
// initializes to default value
struct VoltNode* newVoltNode(double initVoltage, double conductance) {
struct VoltNode *newNode = (struct VoltNode *) malloc(sizeof(struct VoltNode));
newNode->voltageNew = initVoltage;
newNode->voltageOld = initVoltage;
newNode->conductance = conductance;
return newNode;
};
// utility function creates a list of nodes and voltages
struct NodesVoltages* createNodesVoltages(int n) {
struct NodesVoltages *nodesVoltages = (struct NodesVoltages*) malloc(sizeof(struct NodesVoltages));
nodesVoltages->n = n;
// create a VoltList array of pointers to VoltNodes within the container struct
nodesVoltages->array = (struct VoltList*) malloc(n * sizeof(struct VoltList));
//initialize all fields to NULL
int i;
for (i = 0; i < nodesVoltages->n; i++) {
nodesVoltages->array[i].head = NULL;
}
return nodesVoltages;
}
// function to initialize a node
void initNode(struct NodesVoltages* nodesVoltages, int nodeNum, double voltage, double conductance) {
struct VoltNode* voltNode = newVoltNode(voltage, conductance);
// place (pointer to) new node in the array of voltage nodes
nodesVoltages->array[nodeNum].head = voltNode;
}
void initAllNodes(struct NodesVoltages* nodesVoltages, double voltage, double conductance) {
int i;
for (i = 0; i< nodesVoltages->n; i++ ) {
initNode(nodesVoltages, i, voltage, conductance);
}
}
//////
// Adjacency List
/////
// A structure to represent an adjacency list node
struct AdjListNode
{
int val;
struct AdjListNode* next;
};
// A structure to represent an adjacency liat
struct AdjList
{
struct AdjListNode *head; // pointer to head node of list
};
// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
struct AdjList* array;
};
// A utility function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int value)
{
struct AdjListNode* newNode = (struct AdjListNode*) malloc(sizeof(struct AdjListNode));
newNode->val = value;
newNode->next = NULL;
return newNode;
}
// A utility function that creates a graph of V vertices
struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
// Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by making head as NULL
int i;
for (i = 0; i < V; i++) {
graph->array[i].head = NULL;
}
return graph;
}
// Adds an edge to an undirected graph
void addEdgeAdjList(struct Graph* graph, int node_a, int node_b) {
// Add node_b to node_a. The node is added at the begining of the linked list
struct AdjListNode* newNode = newAdjListNode(node_b);
newNode->next = graph->array[node_a].head;
graph->array[node_a].head = newNode;
// add node_a to node_b as well, since this an undirected graph
newNode = newAdjListNode(node_a);
newNode->next = graph->array[node_b].head;
graph->array[node_b].head = newNode;
}
// A utility function to print the adjacenncy list representation of graph
void pretty_printGraph(struct Graph* graph) {
int v;
for (v = 0; v < graph->V; v++)
{
struct AdjListNode* sweeperNode = graph->array[v].head; //sweeperNode starts as head
printf("\n Adjacency list of vertex %d\n head ", v);
while (sweeperNode)
{
printf("-> %d", sweeperNode->val); // val = current node value
sweeperNode = sweeperNode->next; // next = next
}
printf("\n");
}
}
// print the adjacency list in a more matrix-like way
void printMatrix (struct Graph* graph) {
int v;
for (v = 0; v < graph->V; v++) {
int count = 0;
struct AdjListNode *sweeperNode = graph->array[v].head;
printf("\nNode %d: ", v);
while (sweeperNode) {
printf("%d ", sweeperNode->val);
sweeperNode = sweeperNode->next;
count++;
}
printf(" count = %d ", count);
}
}
void printEndMatrix(struct Graph *graph) {
int v = 0;
// print last 30 rows if there are more than 30 nodes
if (graph->V > 30) {
v = graph->V - 30;
}
for( ; v < graph->V; v++) {
int count = 0;
struct AdjListNode *sweeperNode = graph->array[v].head;
printf("\n Node %d: ", v);
while (sweeperNode) {
printf("%d ", sweeperNode->val);
sweeperNode = sweeperNode->next;
count++;
}
printf(" count = %d ", count);
}
}
int countNodes(struct Graph *graph) {
return graph->V;
}
// traverses entire adjacency list in O(n * m) time
int countEdges(struct Graph * graph) {
int v;
int total_edges = 0;
for (v = 0; v < graph->V; v++) {
struct AdjListNode *sweeperNode = graph->array[v].head;
while (sweeperNode) {
total_edges++;
sweeperNode = sweeperNode->next;
}
}
return total_edges;
}
// return 0 if graph does not contain node_a, node_b pair, else return 1
// the efficiency of this depends on the linked-list formulation graph
int isFull(struct Graph* graph, int node_a, int node_b) {
struct AdjListNode *sweeperNode = graph->array[node_a].head;
while (sweeperNode) {
if(sweeperNode->val == node_b) {
return 1; // found a pair match
}
sweeperNode = sweeperNode->next;
}
return 0;
}
void destroyGraph(struct Graph *graph) {
int i;
for (i = 0; i < graph->V; i++) {
free(graph->array[i].head);
}
// finally
free(graph);
}
///
/// Voltage list and Adjacency List interface
///
// function to calculate newVoltage for a node
void setNewVoltage(struct Graph* graph, struct NodesVoltages* nodesVoltages, int nodeNum) {
// add own value to the new voltage sum
double sum = nodesVoltages->array[nodeNum].head->voltageOld;
int count = 1; // start count with 1
// access adjacency list at nodeNum to get neighbors
struct AdjListNode *sweeperNode = graph->array[nodeNum].head;
while (sweeperNode) {
int neighborNum = sweeperNode->val;
sweeperNode = sweeperNode->next;
// use neighbor as the number of the node, access nodesVoltages with it
double neighborVoltage = nodesVoltages->array[neighborNum].head->voltageOld;
count++;
sum += neighborVoltage;
}
// set new voltage using nodeNum
double average = (double) sum/count;
nodesVoltages->array[nodeNum].head->voltageNew = average;
}
void calculateAllNewVoltages(struct Graph* graph, struct NodesVoltages* nodesVoltages) {
int i;
for (i = 0; i< graph->V; i++) {
setNewVoltage(graph, nodesVoltages, i);
}
}
//function to update a node
void updateNodeOldVoltage(struct NodesVoltages* nodesVoltages, int nodeNum) {
nodesVoltages->array[nodeNum].head->voltageOld = nodesVoltages->array[nodeNum].head->voltageNew;
}
void globalNodeUpdate(struct NodesVoltages* nodesVoltages) {
int i;
for (i = 0; i< nodesVoltages->n; i++) {
updateNodeOldVoltage(nodesVoltages, i);
}
}
void printVoltages(struct NodesVoltages* nodesVoltages) {
int nodeNum = 0;
if (nodesVoltages->n > 30) {
nodeNum = nodesVoltages->n - 30;
}
for ( ; nodeNum < nodesVoltages->n; nodeNum++) {
double val = nodesVoltages->array[nodeNum].head->voltageOld;
printf("\nNode %d: Voltage %lf", nodeNum, val);
}
}
void writeVoltages(struct NodesVoltages* nodesVoltages, double convergence, int iterations) {
int nodeNum = 0;
FILE* ff;
char fname[100];
// prepare file to write
printf("What is the name of the output file? \n");
scanf("%s", fname);
ff = fopen(fname,"w+"); // ff is a pointer
if(!ff) {
// 0 is false, so !ff will execute if ff is equal to zero
printf("\n Failed to write to file \n");
exit(1); // exit with error
}
// write convergence and iterations to file
fprintf(ff, "# convergence = %lf, iterations = %d \n", convergence, iterations);
// loop and write to file
for (nodeNum = 0; nodeNum < nodesVoltages->n; nodeNum++) {
double val = nodesVoltages->array[nodeNum].head->voltageOld;
fprintf(ff,"%lf \n", val);
}
printf("Written to file: %s \n", fname);
}
double calculateConvergence(struct Graph* graph, struct NodesVoltages* nodesVoltages,
int source, int sink) {
// check for current in = current out
// voltage near source should equal voltage near sink, because resistance = 1
double source_voltage = nodesVoltages->array[source].head->voltageOld;
// access adjacency list at nodeNum to get neighbors
struct AdjListNode* sweeperNode = graph->array[source].head;
double sum = 0;
while (sweeperNode) {
// neighborNum represents the numbers of the neighbors of the source node
int neighborNum = sweeperNode->val;
sweeperNode = sweeperNode->next;
// use neighbor as the number of the node, access nodesVoltages with it
double neighborVoltage = nodesVoltages->array[neighborNum].head->voltageOld;
sum += source_voltage - neighborVoltage;
}
// take difference in voltage between source and sum of surrounding voltages
source_voltage = sum;
// do this for the sink
double sink_voltage = nodesVoltages->array[sink].head->voltageOld;
// access adjacency list at nodeNum to get neighbors
sweeperNode = graph->array[sink].head;
sum = 0;
while (sweeperNode) {
// neighborNum represents the numbers of the neighbors of the source node
int neighborNum = sweeperNode->val;
sweeperNode = sweeperNode->next;
// use neighbor as the number of the node, access nodesVoltages with it
double neighborVoltage = nodesVoltages->array[neighborNum].head->voltageOld;
sum += neighborVoltage - sink_voltage;
}
sink_voltage = sum;
// take difference in voltage between source and sum of surrounding voltages
return source_voltage - sink_voltage;
}
// Create an adjacency list representation of a network
// This is justified if the number of edges is several orders of magnitude
// less than the number of nodes squares
// Will scale according to the number of edges
int main()
{
clock_t start, finish;
int n;
printf("How many vertices in the graph? \n");
scanf("%d",&n);
int edges;
printf("How many edges? \n");
scanf("%d", &edges);
int seed;
printf("Random seed integer \n");
scanf("%d", &seed);
seed_random64(seed);
// start timer
start = clock();
// make graph with n nodes
struct Graph* graph = createGraph(n);
// fill the graph with edges between nodes
int node_a, node_b;
int edge_count = 0;
while(edge_count < edges) {
node_a = floor (n * random64()); // make new nodes in the range of nodes
node_b = floor (n * random64());
// reject parallel edges (more than one edge between two nodes)
// and loops (edges from a node to itself)
if (!isFull(graph, node_a, node_b) && node_a != node_b ) {
addEdgeAdjList(graph, node_a, node_b);
edge_count++;
}
// else repeat while loop, since edge_count is not incremented
}
finish = clock();
// print the adjacency list representation of the above graph
// printEndMatrix(graph);
// count nodes
int size = 0;
size = countNodes(graph);
printf("Made an adjacency list with %d nodes \n", size);
// count edges
int num_edges = -1; // in case of failure
num_edges = countEdges(graph)/2; // undirected edges
printf("Adjacency list contains %d unique edges \n", num_edges);
// print out time
double time_create = (double) (finish - start) / CLOCKS_PER_SEC;
printf("Time = %f \n", time_create);
/// Do Voltage calculations
// initialize all nodes - n is the number of verticies
struct NodesVoltages* nodesVoltages = createNodesVoltages(n);
initAllNodes(nodesVoltages, 0.5, 1);
//
// Use graph adjacency list and voltage list together
//
// arbitrary sources and sinks written in for testing purposes
int sink = 0;
int source = 1;
nodesVoltages->array[source].head->voltageOld = 1;
nodesVoltages->array[sink].head->voltageOld = 0;
// start timer
start = clock();
// iterate a certain number of times - 100 chosen for testing purposes
int i;
int max_iter, min_iter;
double tol;
printf("How many iterations for voltage calculation? Integer: \n");
scanf("%d", &max_iter);
printf("How many minimum iterations?\n");
scanf("%d", &min_iter);
printf("What is the tolerance for convergence?\n");
scanf("%lf", &tol);
for (i = 0; i < max_iter; i++) {
// calculate using old values
calculateAllNewVoltages(graph, nodesVoltages);
// assign all members of voltage list their new values to old values
globalNodeUpdate(nodesVoltages);
// before the loop, assign the sink to 0 again and the source to 1
nodesVoltages->array[source].head->voltageOld = 1;
nodesVoltages->array[sink].head->voltageOld = 0;
// break if calculations are below tolerance, and above min_iterations
if (calculateConvergence(graph, nodesVoltages, source, sink) < tol
&& calculateConvergence(graph, nodesVoltages, source, sink) > -tol
&& i > min_iter) {
break;
}
}
// end timer
finish = clock();
time_create = (double) (finish - start) / CLOCKS_PER_SEC;
double convergence = calculateConvergence(graph, nodesVoltages, source, sink);
//calculate convergence
printf("Convergence = %lf \n", convergence);
// printVoltages(nodesVoltages);
writeVoltages(nodesVoltages, convergence, i);
printf("Time = %f for %d iterations\n", time_create, i);
// tear down the graph
destroyGraph(graph);
return 0;
}