-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.c
More file actions
718 lines (622 loc) · 18.7 KB
/
Graph.c
File metadata and controls
718 lines (622 loc) · 18.7 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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h> // For sleep function
#include <limits.h>
#include "Graph.h"
#include "Functions.h"
extern int hasCycle;
int min2(int a, int b)
{
return (a < b) ? a : b;
}
int max2(int a, int b)
{
return (a > b) ? a : b;
}
int arithmetic_eval2(int v1, int v2, char op)
{
switch (op)
{
case '+':
return v1 + v2;
case '-':
return v1 - v2;
case '*':
return v1 * v2;
case '/':
return v1 / v2;
default:
return INT_MIN;
}
}
void AddFormula(Graph *graph, int cell, int c1, int c2, int op_type, Formula *formulaArray)
{
Formula newFormula;
newFormula.op_type = op_type;
newFormula.op_info1 = -1;
newFormula.op_info2 = -1;
if (op_type == 0)
{
newFormula.op_info1 = c1;
}
else
{
newFormula.op_info1 = c1;
newFormula.op_info2 = c2;
}
formulaArray[cell] = newFormula;
}
// Modified to use linked list instead of AVL tree
Cell *Addcell(int cell)
{
Cell *new_cell = (Cell *)malloc(sizeof(Cell));
if (!new_cell)
{
printf("Cannot allocate memory for %d\n", cell);
return NULL;
}
new_cell->cell = cell;
new_cell->next = NULL;
return new_cell;
}
// Create a Range node
Range *AddRange(int startCell, int endCell, int dependentCell)
{
Range *new_range = (Range *)malloc(sizeof(Range));
if (!new_range)
{
printf("Cannot allocate memory for range\n");
return NULL;
}
new_range->startCell = startCell;
new_range->endCell = endCell;
new_range->dependentCell = dependentCell;
new_range->next = NULL;
return new_range;
}
Graph *CreateGraph()
{
Graph *graph = (Graph *)malloc(sizeof(Graph));
if (!graph)
{
printf("Cannot allocate memory for graph\n");
return NULL;
}
graph->adjLists_head = (Cell **)calloc(NUM_CELLS, sizeof(Cell *));
if (!graph->adjLists_head)
{
printf("Cannot allocate memory for adjacency lists\n");
free(graph);
return NULL;
}
// Initialize range list
graph->ranges_head = NULL;
return graph;
}
// Add an edge to the linked list
Cell *Addedge(int cell1, Cell *head)
{
// Fast path if list is empty
if (head == NULL)
{
return Addcell(cell1);
}
// Fast path if cell1 should be at the beginning
if (head->cell == cell1)
{
return head; // Cell already exists, no need to add
}
// Check if the cell already exists in the list
Cell *current = head;
while (current->next != NULL)
{
if (current->next->cell == cell1)
return head; // Cell already exists, no need to add
current = current->next;
}
// Add new cell at the end of the list for better cache locality
Cell *new_cell = Addcell(cell1);
if (new_cell == NULL)
return head;
current->next = new_cell;
return head;
}
// Add a range to the range list
void AddRangeToGraph(Graph *graph, int startCell, int endCell, int dependentCell)
{
Range *new_range = AddRange(startCell, endCell, dependentCell);
if (!new_range)
return;
// Add to the beginning of the list for simplicity
new_range->next = graph->ranges_head;
graph->ranges_head = new_range;
}
// Delete a specific cell from the linked list
Cell *Deletecell(int cell1, Cell *head)
{
if (head == NULL)
return NULL;
// If the head node itself holds the cell to be deleted
if (head->cell == cell1)
{
Cell *temp = head->next;
free(head);
return temp;
}
// Search for the cell to be deleted
Cell *current = head;
Cell *prev = NULL;
while (current != NULL && current->cell != cell1)
{
prev = current;
current = current->next;
}
// If cell was not found
if (current == NULL)
return head;
// Unlink the node from linked list
prev->next = current->next;
free(current);
return head;
}
// Delete range from the range list
void DeleteRangeFromGraph(Graph *graph, int dependentCell)
{
Range *current = graph->ranges_head;
Range *prev = NULL;
while (current != NULL)
{
if (current->dependentCell == dependentCell)
{
// Remove this range
if (prev == NULL)
{
// It's the head node
graph->ranges_head = current->next;
free(current);
current = graph->ranges_head;
}
else
{
// Middle or end node
prev->next = current->next;
free(current);
current = prev->next;
}
}
else
{
prev = current;
current = current->next;
}
}
}
Cell *Deleteedge(Graph *graph, int cell, int COLS, Formula *formulaArray)
{
Formula x = formulaArray[cell];
if (x.op_type >= 1 && x.op_type <= 4)
{
graph->adjLists_head[x.op_info1] = Deletecell(cell, graph->adjLists_head[x.op_info1]);
}
else if (x.op_type >= 5 && x.op_type <= 8)
{
graph->adjLists_head[x.op_info1] = Deletecell(cell, graph->adjLists_head[x.op_info1]);
graph->adjLists_head[x.op_info2] = Deletecell(cell, graph->adjLists_head[x.op_info2]);
}
else if (x.op_type >= 9 && x.op_type <= 13)
{
// For range operations, just delete the range entry
DeleteRangeFromGraph(graph, cell);
}
else if (x.op_type == 14) // SLEEP operation
{
graph->adjLists_head[x.op_info1] = Deletecell(cell, graph->adjLists_head[x.op_info1]);
}
else if (x.op_type == 15) // CONSTANT/CELL operation
{
// Delete edge from the referenced cell (op_info2)
graph->adjLists_head[x.op_info2] = Deletecell(cell, graph->adjLists_head[x.op_info2]);
}
return NULL;
}
Cell *Addedge_formula(Graph *graph, int cell, int COLS, Formula *formulaArray)
{
Formula x = formulaArray[cell];
// For operations 1-4 (single cell operations)
if (x.op_type >= 1 && x.op_type <= 4)
{
graph->adjLists_head[x.op_info1] = Addedge(cell, graph->adjLists_head[x.op_info1]);
}
// For operations 5-8 (two cell operations)
else if (x.op_type >= 5 && x.op_type <= 8)
{
graph->adjLists_head[x.op_info1] = Addedge(cell, graph->adjLists_head[x.op_info1]);
graph->adjLists_head[x.op_info2] = Addedge(cell, graph->adjLists_head[x.op_info2]);
}
// For operations 9-13 (range operations)
else if (x.op_type >= 9 && x.op_type <= 13)
{
int startCell = x.op_info1;
int endCell = x.op_info2;
AddRangeToGraph(graph, startCell, endCell, cell);
}
// For operation 14 (SLEEP)
else if (x.op_type == 14)
{
// If op_info1 is not the cell itself, it's a reference to another cell
if (x.op_info1 != cell)
{
graph->adjLists_head[x.op_info1] = Addedge(cell, graph->adjLists_head[x.op_info1]);
}
// If op_info1 is the cell itself, it's a constant sleep - no edge needed
}
// For operation 15 (CONSTANT/CELL)
else if (x.op_type == 15)
{
// Add edge from the referenced cell (op_info2)
graph->adjLists_head[x.op_info2] = Addedge(cell, graph->adjLists_head[x.op_info2]);
}
return NULL;
}
void getNodesFromList(Cell *head, int *nodes, int *count)
{
Cell *current = head;
while (current != NULL)
{
nodes[(*count)++] = current->cell;
current = current->next;
}
}
// Modified DFS function for topological sort to consider ranges
void dfs(Graph *graph, int cell, int *visited, int *onStack, int *result, int *resultIndex, int *hasCycle, int COLS)
{
// Early exit if cycle already detected
if (*hasCycle)
return;
// Mark the current node as visited and add to recursion stack
visited[cell] = 1;
onStack[cell] = 1;
// Visit all adjacent vertices from direct dependencies
Cell *current = graph->adjLists_head[cell];
while (current != NULL && !(*hasCycle))
{
int dependent = current->cell;
// If not visited, then recursively process it
if (!visited[dependent])
{
dfs(graph, dependent, visited, onStack, result, resultIndex, hasCycle, COLS);
}
// If already in recursion stack, then there's a cycle
else if (onStack[dependent])
{
*hasCycle = 1;
return;
}
current = current->next;
}
// Check if this cell is part of any range and add dependencies
Range *range = graph->ranges_head;
while (range != NULL && !(*hasCycle))
{
int startCell = range->startCell;
int endCell = range->endCell;
int dependent = range->dependentCell;
int startRow = startCell / COLS;
int startCol = startCell % COLS;
int endRow = endCell / COLS;
int endCol = endCell % COLS;
// Optimize by swapping if start > end
if (startRow > endRow)
{
int temp = startRow;
startRow = endRow;
endRow = temp;
}
if (startCol > endCol)
{
int temp = startCol;
startCol = endCol;
endCol = temp;
}
int cellRow = cell / COLS;
int cellCol = cell % COLS;
// Check if the cell is within the range
if (cellRow >= startRow && cellRow <= endRow &&
cellCol >= startCol && cellCol <= endCol)
{
// If dependent is not visited, recursively process it
if (!visited[dependent])
{
dfs(graph, dependent, visited, onStack, result, resultIndex, hasCycle, COLS);
}
// If already in recursion stack, then there's a cycle
else if (onStack[dependent])
{
*hasCycle = 1;
return;
}
}
range = range->next;
}
// Remove from recursion stack and add to result
onStack[cell] = 0;
result[(*resultIndex)++] = cell;
}
// Replace BFS with DFS for topological sort
int *topoSortFromCell(Graph *graph, int startCell, int *size, int *hasCycle, int COLS)
{
*size = 0;
*hasCycle = 0;
// Create arrays for storing result and tracking visited/on-stack
int *result = (int *)malloc(NUM_CELLS * sizeof(int));
int *visited = (int *)calloc(NUM_CELLS, sizeof(int));
int *onStack = (int *)calloc(NUM_CELLS, sizeof(int));
if (!result || !visited || !onStack)
{
// Handle memory allocation failure
if (result)
free(result);
if (visited)
free(visited);
if (onStack)
free(onStack);
*hasCycle = 1; // Use hasCycle to indicate error
return NULL;
}
int resultIndex = 0;
// Perform DFS starting from the startCell
dfs(graph, startCell, visited, onStack, result, &resultIndex, hasCycle, COLS);
free(visited);
free(onStack);
if (*hasCycle)
{
free(result);
return NULL;
}
// Reverse the result (DFS produces reverse topological order)
for (int i = 0; i < resultIndex / 2; i++)
{
int temp = result[i];
result[i] = result[resultIndex - i - 1];
result[resultIndex - i - 1] = temp;
}
*size = resultIndex;
return result;
}
void Recalc(Graph *graph, int C, int *arr, int startCell, Formula *formulaArray)
{
int size;
int *sortedCells = topoSortFromCell(graph, startCell, &size, &hasCycle, C);
if (hasCycle)
{
// printf("Error: Circular dependency detected. Command rejected.\n");
free(sortedCells);
return;
}
// Initialize all affected cells to 0
for (int i = 0; i < size; i++)
{
arr[sortedCells[i]] = 0;
}
// Process cells in topological order
for (int i = 0; i < size; i++)
{
int cell = sortedCells[i];
Formula f = formulaArray[cell];
switch (f.op_type)
{
case 0: // CELL=CONSTANT
arr[cell] = (f.op_info1 == INT_MIN) ? INT_MIN : f.op_info1;
break;
case 1: // CELL=CELL+CONSTANT
case 2: // CELL=CELL-CONSTANT
case 3: // CELL=CELL*CONSTANT
case 4: // CELL=CELL/CONSTANT
{
int v1 = arr[f.op_info1];
int v2 = f.op_info2;
if (v1 == INT_MIN)
{
// printf(" Error: Cell %d has invalid operand (v1 is INT_MIN)\n", f.op_info1);
arr[cell] = INT_MIN;
continue;
}
char op = (f.op_type == 1) ? '+' : (f.op_type == 2) ? '-'
: (f.op_type == 3) ? '*'
: '/';
if (op == '/' && v2 == 0)
{
arr[cell] = INT_MIN;
continue;
}
arr[cell] = arithmetic_eval2(v1, v2, op);
break;
}
case 5: // CELL=CELL+CELL
case 6: // CELL=CELL-CELL
case 7: // CELL=CELL*CELL
case 8: // CELL=CELL/CELL
{
int v1 = arr[f.op_info1];
int v2 = arr[f.op_info2];
if (v1 == INT_MIN || v2 == INT_MIN)
{
arr[cell] = INT_MIN;
continue;
}
if (f.op_type == 8 && v2 == 0)
{
arr[cell] = INT_MIN;
continue;
}
char op = (f.op_type == 5) ? '+' : (f.op_type == 6) ? '-'
: (f.op_type == 7) ? '*'
: '/';
arr[cell] = arithmetic_eval2(v1, v2, op);
break;
}
case 9: // CELL=MIN(RANGE)
case 10: // CELL=MAX(RANGE)
case 11: // CELL=AVG(RANGE)
case 12: // CELL=SUM(RANGE)
case 13: // CELL=STDEV(RANGE)
{
int startCell = f.op_info1;
int endCell = f.op_info2;
int startRow = startCell / C;
int startCol = startCell % C;
int endRow = endCell / C;
int endCol = endCell % C;
// Optimize by swapping if start > end
if (startRow > endRow)
{
int temp = startRow;
startRow = endRow;
endRow = temp;
}
if (startCol > endCol)
{
int temp = startCol;
startCol = endCol;
endCol = temp;
}
int sum = 0, count = 0;
int minVal = INT_MAX, maxVal = INT_MIN;
int hasError = 0;
// First pass: Calculate sum, min, max and check for errors
for (int row = startRow; row <= endRow && !hasError; row++)
{
for (int col = startCol; col <= endCol; col++)
{
int idx = row * C + col;
int val = arr[idx];
if (val == INT_MIN)
{
hasError = 1;
break;
}
sum += val;
count++;
minVal = (val < minVal) ? val : minVal;
maxVal = (val > maxVal) ? val : maxVal;
}
}
if (hasError || count == 0)
{
arr[cell] = INT_MIN;
continue;
}
// Handle different range operations
switch (f.op_type)
{
case 9: // MIN
arr[cell] = minVal;
break;
case 10: // MAX
arr[cell] = maxVal;
break;
case 11: // AVG
arr[cell] = sum / count;
break;
case 12: // SUM
arr[cell] = sum;
break;
case 13: // STDEV
{
int values_count = (endRow - startRow + 1) * (endCol - startCol + 1);
int *values = (int *)malloc(values_count * sizeof(int));
int index = 0;
for (int row = startRow; row <= endRow; row++)
{
for (int col = startCol; col <= endCol; col++)
{
values[index++] = arr[row * C + col];
}
}
// Compute standard deviation using the std function
arr[cell] = std(values, values_count);
free(values);
break;
}
}
break;
}
case 14: // CELL=SLEEP(CONSTANT) or CELL=SLEEP(CELL)
{
int sleep_value = f.op_info2; // Assuming it's a constant
// If op_info1 is not the cell itself, it's a reference to another cell
if (f.op_info1 != cell)
{
sleep_value = arr[f.op_info1];
}
if (sleep_value == INT_MIN)
{
arr[cell] = INT_MIN;
continue;
}
if (sleep_value > 0)
{
// printf("Sleeping for %d seconds...\n", sleep_value);
sleep(sleep_value);
// printf("Awake!\n");
}
arr[cell] = sleep_value;
break;
}
case 15: // CELL=CONSTANT/CELL
{
int v1 = f.op_info1;
int v2 = arr[f.op_info2];
if (v2 == INT_MIN)
{
// printf(" Error: Cell %d has invalid operand (v1 is INT_MIN)\n", f.op_info1);
arr[cell] = INT_MIN;
continue;
}
char op = (f.op_type == 1) ? '+' : (f.op_type == 2) ? '-'
: (f.op_type == 3) ? '*'
: '/';
if (op == '/' && v2 == 0)
{
arr[cell] = INT_MIN;
continue;
}
arr[cell] = arithmetic_eval2(v1, v2, op);
break;
}
default:
// Handle unknown operation type
arr[cell] = INT_MIN;
break;
}
}
free(sortedCells);
}
void FreeGraph(Graph *graph)
{
if (!graph)
return;
// Free cell adjacency lists
for (int i = 0; i < NUM_CELLS; i++)
{
Cell *current = graph->adjLists_head[i];
while (current)
{
Cell *temp = current;
current = current->next;
free(temp);
}
}
// Free range list
Range *current = graph->ranges_head;
while (current)
{
Range *temp = current;
current = current->next;
free(temp);
}
free(graph->adjLists_head);
free(graph);
}