Skip to content

Commit ccf80a6

Browse files
authored
Merge branch '2024_sem2' into 2024_sem2
2 parents 4545255 + f806f5a commit ccf80a6

File tree

18 files changed

+795
-158
lines changed

18 files changed

+795
-158
lines changed

src/algorithms/controllers/AStar.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default {
2121
};
2222
},
2323

24-
run(chunker, {edgeValueMatrix, coordsMatrix, startNode, endNodes, heuristicFn}) {
24+
run(chunker, {edgeValueMatrix, coordsMatrix, startNode, endNodes, heuristicFn, moveNode}) {
2525
// String Variables used in displaying algo
2626
const algNameStr = 'aStar';
2727
const dashStr = '-';
@@ -130,6 +130,7 @@ export default {
130130
(vis, edgeArray, coordsArray) => {
131131
vis.graph.directed(false);
132132
vis.graph.weighted(true);
133+
vis.graph.moveNodeFn(moveNode);
133134
vis.graph.set(edgeArray, Array.from({ length: numVertices }, (v, k) => (k + 1)),coordsArray);
134135
},
135136
[E, coords]

src/algorithms/controllers/BFS.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
};
1919
},
2020

21-
run(chunker, { edgeValueMatrix, coordsMatrix, endNodes, startNode}) {
21+
run(chunker, { edgeValueMatrix, coordsMatrix, endNodes, startNode, moveNode}) {
2222

2323
//Defining queue
2424
function Queue() {
@@ -70,6 +70,7 @@ export default {
7070
(vis, edgeArray, coordsArray) => {
7171
vis.graph.directed(false);
7272
vis.graph.weighted(false);
73+
vis.graph.moveNodeFn(moveNode);
7374
vis.graph.set(edgeArray, Array.from({ length: edgeValueMatrix.length }, (v, k) => (k + 1)), coordsArray);
7475

7576
},

src/algorithms/controllers/DFS.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default {
2121
};
2222
},
2323

24-
run(chunker, { edgeValueMatrix, coordsMatrix, endNodes, startNode}) {
24+
run(chunker, { edgeValueMatrix, coordsMatrix, endNodes, startNode, moveNode}) {
2525

2626
const E = [...edgeValueMatrix];
2727
const coords = [...coordsMatrix];
@@ -50,6 +50,7 @@ export default {
5050
(vis, edgeArray, coordsArray) => {
5151
vis.graph.directed(false);
5252
vis.graph.weighted(false);
53+
vis.graph.moveNodeFn(moveNode);
5354
vis.graph.set(edgeArray, Array.from({ length: edgeValueMatrix.length }, (v, k) => (k + 1)), coordsArray);
5455

5556
},

src/algorithms/controllers/DFSrec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default {
2525
};
2626
},
2727

28-
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes}) {
28+
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes, moveNode}) {
2929
// String Variables used in displaying algo
3030
const algNameStr = 'DFSrec';
3131
const nStr = 'n';
@@ -241,6 +241,7 @@ export default {
241241
(vis, edgeArray, coordsArray) => {
242242
vis.graph.directed(false);
243243
vis.graph.weighted(false);
244+
vis.graph.moveNodeFn(moveNode);
244245
vis.graph.set(edgeArray, Array.from({ length: numVertices }, (v, k) => (k + 1)),coordsArray);
245246
},
246247
[E, coords], 0

src/algorithms/controllers/dijkstra.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default {
1717
};
1818
},
1919

20-
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes}) {
20+
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes, moveNode}) {
2121
// String Variables used in displaying algo
2222
const algNameStr = 'dijkstra';
2323
const dashStr = '-';
@@ -115,6 +115,7 @@ export default {
115115
(vis, edgeArray, coordsArray) => {
116116
vis.graph.directed(false);
117117
vis.graph.weighted(true);
118+
vis.graph.moveNodeFn(moveNode);
118119
vis.graph.set(edgeArray, Array.from({ length: numVertices }, (v, k) => (k + 1)),coordsArray);
119120
},
120121
[E, coords]

src/algorithms/controllers/kruskal.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
};
1919
},
2020

21-
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes}) {
21+
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes, moveNode}) {
2222
// String Variables used in displaying algo
2323
const algNameStr = 'kruskal';
2424
const etcStr = '...';
@@ -114,6 +114,7 @@ export default {
114114
(vis, edgeArray, coordsArray) => {
115115
vis.graph.directed(false);
116116
vis.graph.weighted(true);
117+
vis.graph.moveNodeFn(moveNode);
117118
vis.graph.set(edgeArray, Array.from({ length: numVertices }, (v, k) => (k + 1)),coordsArray);
118119
},
119120
[E, coords]

src/algorithms/controllers/prim.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
};
1919
},
2020

21-
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes}) {
21+
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes, moveNode}) {
2222
// String Variables used in displaying algo
2323
const algNameStr = 'prim';
2424
const dashStr = '-';
@@ -132,6 +132,7 @@ export default {
132132
(vis, edgeArray, coordsArray) => {
133133
vis.graph.directed(false);
134134
vis.graph.weighted(true);
135+
vis.graph.moveNodeFn(moveNode);
135136
vis.graph.set(edgeArray, Array.from({ length: numVertices }, (v, k) => (k + 1)),coordsArray);
136137
},
137138
[E, coords]

src/algorithms/controllers/sorts.c

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
// #define QUICKSORT // some version of quicksort
88
// #define MERGE_TD // top-down merge sort
99
// #define MERGE_BUP // bottom-up merge sort
10+
#define MERGE_NAT // natural merge sort
1011
// #define MSD_RADIX // radix exchange sort
11-
#define MERGE_TD_LA // top-down merge sort for lists imp as arrays
12+
// #define MERGE_TD_LA // top-down merge sort for lists imp as arrays
1213
// XXX should do top-down merge sort for lists imp with pointers - main code
1314
// should be identical - just need to write init code for list and change
1415
// some macro definitions
@@ -59,6 +60,10 @@ List mergesort_td_la(int L, int len);
5960
void mergesort_bup(int A[], int size);
6061
#endif // MERGE_BUP
6162

63+
#ifdef MERGE_NAT
64+
void mergesort_nat(int A[], int size);
65+
#endif // MERGE_NAT
66+
6267
#ifdef MSD_RADIX
6368
int radix_partition(int *A, int left, int right, int mask);
6469
void msd_radix_sort(int A[], int left, int right, int mask);
@@ -102,6 +107,9 @@ main() {
102107
#ifdef MERGE_BUP
103108
mergesort_bup(A, Size-1);
104109
#endif // MERGE_BUP
110+
#ifdef MERGE_NAT
111+
mergesort_nat(A, Size-1);
112+
#endif // MERGE_NAT
105113
for (i=1; i < Size; i++) printf("%d ", A[i]); printf("\n");
106114
}
107115

@@ -323,7 +331,7 @@ mergesort_td(int A[], int left, int right) {
323331
List
324332
mergesort_td_la(int L, int len) {
325333
int i, mid;
326-
List Lmid, R, M, E;
334+
List Lmid, R, M, Mlast;
327335

328336
if (len > 1) {
329337
// determine Lmid, the mid point of L
@@ -362,23 +370,23 @@ mergesort_td_la(int L, int len) {
362370
R = tail(R);
363371
}
364372
// scan through adding elements to the end of M
365-
E = M;
373+
Mlast = M;
366374
while (L != Null && R != Null) {
367375
if (head(L) <= head(R)) {
368-
tail(E) = L;
369-
E = L;
376+
tail(Mlast) = L;
377+
Mlast = L;
370378
L = tail(L);
371379
} else {
372-
tail(E) = R;
373-
E = R;
380+
tail(Mlast) = R;
381+
Mlast = R;
374382
R = tail(R);
375383
}
376384
}
377385
// add any elements not scanned to the end of M
378386
if (L == Null)
379-
tail(E) = R;
387+
tail(Mlast) = R;
380388
else
381-
tail(E) = L;
389+
tail(Mlast) = L;
382390
printf("Merged: ");
383391
for (Lmid = M; Lmid != Null; Lmid = tail(Lmid))
384392
printf(" %d", head(Lmid));
@@ -397,7 +405,7 @@ mergesort_td_la(int L, int len) {
397405
#endif // MERGE_TD_LA
398406

399407
#ifdef MERGE_BUP
400-
// XXX could reduce duplication with MERGE_TD
408+
// XXX could reduce duplication with MERGE_TD and MERGE_NAT
401409
int B[Size];
402410

403411
int
@@ -460,3 +468,77 @@ printf("Merging %d %d %d - %d\n", left, mid, right, runlength);
460468
}
461469

462470
#endif // MERGE_BUP
471+
472+
#ifdef MERGE_NAT
473+
// XXX could reduce duplication with MERGE_TD and MERGE_BUP
474+
int B[Size];
475+
476+
int
477+
minimum(int i, int j) {
478+
if (i <= j)
479+
return i;
480+
else
481+
return j;
482+
}
483+
484+
// Sort array A[1]..A[size] in ascending order
485+
void
486+
mergesort_nat(int A[], int size) {
487+
int runcount, left, mid, right;
488+
int ap1, ap1max, ap2, ap2max, bp;
489+
490+
do {
491+
runcount = 0;
492+
left = 1;
493+
do {
494+
// find the first run, A[left..mid]
495+
mid = left;
496+
while (mid < size && A[mid] <= A[mid+1])
497+
mid++;
498+
// find the second run, A[mid+1..right]
499+
right = mid+1;
500+
while (right < size && A[right] <= A[right+1])
501+
right++;
502+
if (mid < size) {
503+
// merge A[left..mid] and A[mid+1..right], with the result in A
504+
printf("Merging %d %d %d \n", left, mid, right);
505+
ap1 = left;
506+
ap1max = mid;
507+
ap2 = mid+1;
508+
ap2max = right;
509+
bp = left;
510+
while (ap1 <= ap1max && ap2 <= ap2max)
511+
if (A[ap1] < A[ap2]) {
512+
B[bp] = A[ap1];
513+
ap1 = ap1+1;
514+
bp = bp+1;
515+
} else {
516+
B[bp] = A[ap2];
517+
ap2 = ap2+1;
518+
bp = bp+1;
519+
}
520+
while (ap1 <= ap1max) {
521+
B[bp] = A[ap1];
522+
ap1 = ap1+1;
523+
bp = bp+1;
524+
}
525+
while (ap2 <= ap2max) {
526+
B[bp] = A[ap2];
527+
ap2 = ap2+1;
528+
bp = bp+1;
529+
}
530+
for (bp = left; bp <= right; bp++)
531+
A[bp] = B[bp];
532+
}
533+
runcount++;
534+
left = right + 1;
535+
} while (left < size);
536+
} while (runcount > 1);
537+
// if (left < right-1) { // for testing/debugging
538+
// int i1;
539+
// printf("Ret from ms(%d, %d): ", left, right);
540+
// for (i1=1; i1 < Size; i1++) printf("%d ", A[i1]); printf("\n");
541+
// }
542+
}
543+
544+
#endif // MERGE_NAT

src/algorithms/controllers/transitiveClosure.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export default {
1919
initVisualisers() {
2020
return {
2121
graph: {
22-
instance: new GraphTracer('key', null, 'Transitive Closure'),
22+
// instance: new GraphTracer('key', null, 'Transitive Closure'),
23+
instance: new GraphTracer('key', null, 'Graph view'),
2324
order: 0
2425
},
2526
// create a separate component for displaying the matrix as a 2D array
@@ -42,22 +43,25 @@ export default {
4243
return out;
4344
},
4445

45-
run(chunker, { matrix, size }) {
46+
run(chunker, { edgeValueMatrix, coordsMatrix, startNode, endNodes, moveNode}) {
4647
// eslint-disable-next-line no-unused-expressions
48+
const matrix = edgeValueMatrix;
49+
const size = matrix.length;
4750
const numOfNodes = size;
4851
const nodes = new Array(numOfNodes);
4952
let prevI = 0;
5053
let prevJ = 0;
5154
let prevK = 0;
52-
chunker.add(1, (g) => {
55+
chunker.add(1, (g, edgeArray, coordsArray) => {
5356
// show kth tag when step back
5457
setKthVisible(true);
5558
g.array.set([...matrix], 'tc');
56-
g.graph.set([...matrix], Array.from({ length: matrix.length }, (v, k) => (k + 1)));
57-
g.graph.layoutCircle();
59+
g.graph.set(edgeArray, Array.from({ length: matrix.length }, (v, k) => (k + 1)),coordsArray);
60+
// g.graph.layoutCircle();
5861
// initialise the matrix in the 'Matrix' component
5962
g.graph.setIstc();
60-
}, [this.graph], [this.array]);
63+
g.graph.moveNodeFn(moveNode);
64+
}, [[...edgeValueMatrix], [...coordsMatrix]]);
6165

6266
for (let i = 0; i < numOfNodes; i++) {
6367
nodes[i] = this.copyArr([...matrix]);

src/algorithms/instructions/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ on the arrow in its middle, the right and bottom panels can be enlarged or shrun
7979
by dragging the "..." up temporarily`,
8080
`Under the ${KEY_PLAY} button, toggle between sample graphs (eg Graph 1) and random graphs, or`,
8181
'edit text for X-Y node coordinates (this can change the graph size) and edges/weights (weights are ignored for unweighted graph algorithms), or',
82-
'enter X-Y node coordinates and edges/weights in tables.',
82+
'enter X-Y node coordinates and edges/weights in tables below, or',
83+
'change X-Y node coordinates by selecting a node with the mouse and dragging it.',
8384
`The graph size can also be explicitly increased/decreased - this generates a new random graph.`,
8485
`Edge weights (for weighted graph algorithms) can be toggled between Euclidean, Manhattan and as defined explicitly in the input.`,
8586
]},

0 commit comments

Comments
 (0)