Skip to content

Commit 8b5cbd2

Browse files
committed
feat: prims algorithm
1 parent 66e1e54 commit 8b5cbd2

19 files changed

+160
-62
lines changed

.jest.config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"clearMocks": true,
33
"moduleNameMapper": {
44
"@code/(.*)": [
5-
"<rootDir>/src/day3/$1"
5+
"<rootDir>/src/day5/$1"
66
]
77
},
88
"preset": "ts-jest"

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Literal Improvement Gaining Master and Tutelage on Algorithms
1010
Let's Intelligently Generate Multiple Algorithm Training Assessments // permdaddy
1111

1212
### Sugma Nuts
13-
Studious Users Get Major Abilities. New Useful Training for Students
13+
Studious Users Get Major Abilities. New Useful Training for Students
1414

1515
### Ligma Fart
1616
Learn Intermediate Groundbreaking Massive Algorithms. Free Algorithm Research & Training System

package.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"dependencies": {
77
"@types/jest": "^28.1.3",
88
"jest": "^28.1.1",
9+
"prettier": "^2.7.1",
910
"ts-jest": "^28.0.5",
1011
"ts-node": "^10.8.1",
1112
"tsconfig-paths": "^4.0.0",
@@ -15,16 +16,17 @@
1516
"scripts": {
1617
"test": "jest InsertionSort MergeSort Queue Stack QuickSort DijkstraList",
1718
"clear": "./scripts/clear",
19+
"prettier": "prettier --write ./src",
1820
"generate": "./scripts/generate",
19-
"day": "echo /home/mpaulson/personal/kata/src/day3"
21+
"day": "echo /home/mpaulson/personal/kata/src/day5"
2022
},
2123
"kata_stats": {
2224
"ArrayList": 0,
23-
"DijkstraList": 2,
24-
"InsertionSort": 1,
25-
"MergeSort": 1,
26-
"Queue": 1,
27-
"Stack": 1,
28-
"QuickSort": 1
25+
"DijkstraList": 4,
26+
"InsertionSort": 3,
27+
"MergeSort": 3,
28+
"Queue": 3,
29+
"Stack": 3,
30+
"QuickSort": 3
2931
}
3032
}

prettier.config.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
tabWidth: 4,
3+
printWidth: 80,
4+
proseWrap: "never",
5+
trailingComma: "all",
6+
singleQuote: false,
7+
semi: true,
8+
};
9+
10+

src/__tests__/ArrayList.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import ArrayList from "@code/ArrayList";
22
import { test_list } from "./ListTest";
33

4-
test("array-list", function() {
4+
test("array-list", function () {
55
const list = new ArrayList<number>(3);
66
test_list(list);
77
});
8-
9-
10-

src/__tests__/DijkstraList.ts

+35-17
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,48 @@
1-
import dijkstra_list from "@code/DijkstraList"
1+
import dijkstra_list from "@code/DijkstraList";
22

3-
test("dijkstra via adj list", function() {
3+
test("dijkstra via adj list", function () {
44
const list: WeightedAdjacencyList = [];
55

66
// (1) --- (4) ---- (5)
77
// / | | /|
88
// (0) | ------|------- |
99
// \ |/ | |
1010
// (1) --- (4) ---- (5)
11-
list[0] = [{to: 1, weight: 3}, {to: 2, weight: 1}];
12-
list[1] = [{to: 0, weight: 3}, {to: 2, weight: 4}, {to: 4, weight: 1}];
13-
list[2] = [{to: 1, weight: 4}, {to: 3, weight: 7}, {to: 0, weight: 1}];
14-
list[3] = [{to: 2, weight: 7}, {to: 4, weight: 5}, {to: 6, weight: 1}];
15-
list[4] = [{to: 1, weight: 1}, {to: 3, weight: 5}, {to: 5, weight: 2}];
16-
list[5] = [{to: 6, weight: 1}, {to: 4, weight: 2}];
17-
list[6] = [{to: 3, weight: 1}, {to: 5, weight: 1}];
11+
list[0] = [
12+
{ to: 1, weight: 3 },
13+
{ to: 2, weight: 1 },
14+
];
15+
list[1] = [
16+
{ to: 0, weight: 3 },
17+
{ to: 2, weight: 4 },
18+
{ to: 4, weight: 1 },
19+
];
20+
list[2] = [
21+
{ to: 1, weight: 4 },
22+
{ to: 3, weight: 7 },
23+
{ to: 0, weight: 1 },
24+
];
25+
list[3] = [
26+
{ to: 2, weight: 7 },
27+
{ to: 4, weight: 5 },
28+
{ to: 6, weight: 1 },
29+
];
30+
list[4] = [
31+
{ to: 1, weight: 1 },
32+
{ to: 3, weight: 5 },
33+
{ to: 5, weight: 2 },
34+
];
35+
list[5] = [
36+
{ to: 6, weight: 1 },
37+
{ to: 4, weight: 2 },
38+
];
39+
list[6] = [
40+
{ to: 3, weight: 1 },
41+
{ to: 5, weight: 1 },
42+
];
1843

1944
/// waht?
2045
// what..
2146
// what...
22-
expect(dijkstra_list(0, 6, list)).toEqual([
23-
0,
24-
1,
25-
4,
26-
5,
27-
6,
28-
]);
47+
expect(dijkstra_list(0, 6, list)).toEqual([0, 1, 4, 5, 6]);
2948
});
30-

src/__tests__/DoublyLinkedList.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import LinkedList from "@code/DoublyLinkedList";
22
import { test_list } from "./ListTest";
33

4-
test("DoublyLinkedList", function() {
4+
test("DoublyLinkedList", function () {
55
const list = new LinkedList<number>(3);
66
test_list(list);
77
});
8-
9-
10-

src/__tests__/InsertionSort.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import insertion_sort from "@code/InsertionSort";
22

3-
test("insertion-sort", function() {
3+
test("insertion-sort", function () {
44
const arr = [9, 3, 7, 4, 69, 420, 42];
5-
debugger
5+
debugger;
66
// where is my debugger
77
insertion_sort(arr);
88
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
99
});
10-

src/__tests__/ListTest.ts

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ export function test_list(list: List<number>): void {
1313
expect(list.removeAt(0)).toEqual(5);
1414
expect(list.removeAt(0)).toEqual(11);
1515
expect(list.length).toEqual(0);
16-
1716
}

src/__tests__/MergeSort.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import merge_sort from "@code/MergeSort";
22

3-
test("merge-sort", function() {
3+
test("merge-sort", function () {
44
const arr = [9, 3, 7, 4, 69, 420, 42];
55
merge_sort(arr);
66
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
77
});
8-
9-

src/__tests__/PrimsAlgorithm.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import prims from "@code/PrimsAlgorithm";
2+
3+
test("PrimsAlgorithm", function () {
4+
const list: WeightedAdjacencyList = [];
5+
6+
// (1) --- (4) ---- (5)
7+
// / | | /|
8+
// (0) | ------|------- |
9+
// \ |/ | |
10+
// (1) --- (4) ---- (5)
11+
list[0] = [
12+
{ to: 1, weight: 3 },
13+
{ to: 2, weight: 1 },
14+
];
15+
list[1] = [
16+
{ to: 0, weight: 3 },
17+
{ to: 2, weight: 4 },
18+
{ to: 4, weight: 1 },
19+
];
20+
list[2] = [
21+
{ to: 1, weight: 4 },
22+
{ to: 3, weight: 7 },
23+
{ to: 0, weight: 1 },
24+
];
25+
list[3] = [
26+
{ to: 2, weight: 7 },
27+
{ to: 4, weight: 5 },
28+
{ to: 6, weight: 1 },
29+
];
30+
list[4] = [
31+
{ to: 1, weight: 1 },
32+
{ to: 3, weight: 5 },
33+
{ to: 5, weight: 2 },
34+
];
35+
list[5] = [
36+
{ to: 6, weight: 1 },
37+
{ to: 4, weight: 2 },
38+
];
39+
list[6] = [
40+
{ to: 3, weight: 1 },
41+
{ to: 5, weight: 1 },
42+
];
43+
44+
// there is only one right answer for this graph
45+
expect(prims(list)).toEqual([
46+
[
47+
{ to: 2, weight: 1 },
48+
{ to: 1, weight: 3 },
49+
],
50+
[
51+
{ to: 0, weight: 3 },
52+
{ to: 4, weight: 1 },
53+
],
54+
[{ to: 0, weight: 1 }],
55+
[{ to: 6, weight: 1 }],
56+
[
57+
{ to: 1, weight: 1 },
58+
{ to: 5, weight: 2 },
59+
],
60+
[
61+
{ to: 4, weight: 2 },
62+
{ to: 6, weight: 1 },
63+
],
64+
[
65+
{ to: 5, weight: 1 },
66+
{ to: 3, weight: 1 },
67+
],
68+
]);
69+
});

src/__tests__/Queue.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Queue from "@code/Queue";
22

3-
test("queue", function() {
3+
test("queue", function () {
44
const list = new Queue<number>();
55

66
list.enqueue(5);
@@ -11,7 +11,7 @@ test("queue", function() {
1111
expect(list.length).toEqual(2);
1212

1313
// this must be wrong..?
14-
debugger
14+
debugger;
1515

1616
// i hate using debuggers
1717
list.enqueue(11);
@@ -29,5 +29,3 @@ test("queue", function() {
2929
expect(list.peek()).toEqual(69);
3030
expect(list.length).toEqual(1);
3131
});
32-
33-

src/__tests__/QuickSort.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import quick_sort from "@code/QuickSort";
22

3-
test("quick-sort", function() {
3+
test("quick-sort", function () {
44
const arr = [9, 3, 7, 4, 69, 420, 42];
55

66
debugger;
77
quick_sort(arr);
88
expect(arr).toEqual([3, 4, 7, 9, 42, 69, 420]);
99
});
10-
11-
12-

src/__tests__/SinglyLinkedList.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import LinkedList from "@code/SingleLinkedList";
22
import { test_list } from "./ListTest";
33

4-
test("linked-list", function() {
4+
test("linked-list", function () {
55
const list = new LinkedList<number>(3);
66
test_list(list);
77
});
8-
9-

src/__tests__/Stack.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Stack from "@code/Stack";
22

3-
test("stack", function() {
3+
test("stack", function () {
44
const list = new Stack<number>();
55

66
list.push(5);
@@ -25,6 +25,3 @@ test("stack", function() {
2525

2626
//yayaya
2727
});
28-
29-
30-

src/global.d.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ declare interface List<T> {
66
add(item: T): void;
77
}
88

9-
declare type GraphEdge = {to: number, weight: number};
9+
declare type CompleteGraphEdge = { from: number; to: number; weight: number };
10+
declare type GraphEdge = { to: number; weight: number };
1011
declare type WeightedAdjacencyList = GraphEdge[][];
1112
declare type WeightedAdjacencyMatrix = number[][]; // A number means weight
1213

@@ -17,9 +18,9 @@ declare type BinaryNode<T> = {
1718
value: T;
1819
left: BinaryNode<T>;
1920
right: BinaryNode<T>;
20-
}
21+
};
2122

2223
declare type GeneralNode<T> = {
2324
value: T;
24-
children: GeneralNode<T>[]
25-
}
25+
children: GeneralNode<T>[];
26+
};

src/graph.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function add_node_list(
2+
list: WeightedAdjacencyList,
3+
from: number,
4+
to: number,
5+
weight: number,
6+
): void {
7+
let l = list[from];
8+
if (!l) {
9+
l = list[from] = [];
10+
}
11+
12+
l.push({ to, weight });
13+
}

tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"baseUrl": "src",
1313
"paths": {
1414
"@code/*": [
15-
"day3/*"
15+
"day5/*"
1616
]
1717
}
1818
},
@@ -22,4 +22,4 @@
2222
"exclude": [
2323
"node_modules"
2424
]
25-
}
25+
}

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,11 @@ pkg-dir@^4.2.0:
18791879
dependencies:
18801880
find-up "^4.0.0"
18811881

1882+
prettier@^2.7.1:
1883+
version "2.7.1"
1884+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.1.tgz#e235806850d057f97bb08368a4f7d899f7760c64"
1885+
integrity sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==
1886+
18821887
pretty-format@^28.0.0, pretty-format@^28.1.1:
18831888
version "28.1.1"
18841889
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-28.1.1.tgz#f731530394e0f7fcd95aba6b43c50e02d86b95cb"

0 commit comments

Comments
 (0)