Skip to content

Commit 4241353

Browse files
committed
Add test for Dijkstra
1 parent 31b549d commit 4241353

File tree

8 files changed

+660
-24
lines changed

8 files changed

+660
-24
lines changed

src/data-structures/graph/graph.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,40 @@ describe("Graph class test", () => {
9797
test(TEST60);
9898
test(TEST64);
9999
});
100+
101+
test("Test for Dijkstra, using test cases from stanford-algs repo", () => {
102+
// WARNING: https://github.com/beaunus/stanford-algs/issues/53
103+
104+
const TEST_1 = "random_1_4.txt";
105+
const TEST_4 = "random_4_4.txt";
106+
const TEST_5 = "random_5_8.txt";
107+
const test = (file: string) => {
108+
const g = Graph.createListAdjWeighted(
109+
`${__dirname}/test-datasets/SSSP/input_${file}`
110+
);
111+
const ans = getTestAnswerArr(
112+
`${__dirname}/test-datasets/SSSP/output_${file}`
113+
);
114+
const { distances, dequeues } = g.dijkstra("1");
115+
const dist: number[] = new Array();
116+
dist.push(distances.get("7")!);
117+
dist.push(distances.get("37")!);
118+
dist.push(distances.get("59")!);
119+
dist.push(distances.get("82")!);
120+
dist.push(distances.get("99")!);
121+
dist.push(distances.get("115")!);
122+
dist.push(distances.get("133")!);
123+
dist.push(distances.get("165")!);
124+
dist.push(distances.get("188")!);
125+
dist.push(distances.get("197")!);
126+
dist.forEach((d, i) => {
127+
expect(d).toBe(ans[i]);
128+
});
129+
// only work if G is all connected (i.e., only one Component)
130+
expect(dequeues).toBe(g.size);
131+
};
132+
test(TEST_1);
133+
test(TEST_4);
134+
test(TEST_5);
135+
});
100136
});

src/data-structures/graph/graph.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -740,11 +740,12 @@ class Graph<T> {
740740
};
741741

742742
// TODO: USE FIBONACCI HEAP (DECREASE KEY)
743-
// Returns the distance from s to each vertex and their parents
743+
// Returns the distance from s to each vertex, their parents, and the number of dequeues
744744
dijkstra = (s: T) => {
745745
const heap = new MinHeap<T>();
746+
// to count how many dequeues from heap are made
747+
// using decrease key will push and dequeue only one time each node
746748
let dequeues = 0;
747-
// objs to map key to distance and key to parents
748749
const distances: Map<T, number> = new Map();
749750
const parents: Map<T, null | T> = new Map();
750751
let smallest: T;
@@ -755,38 +756,34 @@ class Graph<T> {
755756
}
756757
distances.set(s, 0);
757758
heap.enqueue(s, 0);
758-
// heap.buildHeap(distances)
759759
parents.set(s, null);
760760
let deacrease: boolean | number | BHNode<string> | undefined = false;
761761
// while we have nodes to visite:
762762
while (!heap.isEmpty()) {
763763
smallest = heap.dequeue()!.key;
764764
dequeues++;
765-
if (smallest || distances.get(smallest) !== Infinity) {
766-
const vertexList = this.list.get(smallest)!;
767-
for (let neighbour in vertexList) {
768-
let nextNode = vertexList[neighbour];
769-
// calculate Dijkstra's Greedy Criterium
770-
let d = distances.get(smallest)! + nextNode.weight!;
771-
// compare distance calculated with last distance storaged
772-
if (d < distances.get(nextNode.node)!) {
773-
// updating distances and parents
774-
distances.set(nextNode.node, d);
775-
parents.set(nextNode.node, smallest);
776-
// try to deacrease key
777-
deacrease = heap.decreaseKey(nextNode.node, d);
778-
// if this node is not in heap(wasn't decrease) add to the Heap
779-
if (deacrease === false) {
780-
heap.enqueue(nextNode.node, d);
781-
}
765+
// while heap is not empty: smallest will exist and
766+
// we always will add reachable nodes (i.e. dist < Inf)
767+
const vertexList = this.list.get(smallest)!;
768+
for (let neighbour in vertexList) {
769+
let nextNode = vertexList[neighbour];
770+
// calculate Dijkstra's Greedy Criterium
771+
let d = distances.get(smallest)! + nextNode.weight!;
772+
// compare distance calculated with last distance storaged
773+
if (d < distances.get(nextNode.node)!) {
774+
// updating distances and parents
775+
distances.set(nextNode.node, d);
776+
parents.set(nextNode.node, smallest);
777+
// try to deacrease key
778+
deacrease = heap.decreaseKey(nextNode.node, d);
779+
// if this node is not in heap(wasn't decrease) add to the Heap
780+
if (deacrease === false) {
781+
heap.enqueue(nextNode.node, d);
782782
}
783783
}
784784
}
785785
}
786-
// console.log(
787-
// `dequeues: ${dequeues},size: ${this.size}, h.size: ${heap.size}`
788-
// );
789-
return { distances, parents };
786+
return { distances, parents, dequeues };
790787
};
791788

792789
// Returns the distance from s to each vertex and their parents O(mn)
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
1 8,1
2+
2 80,29
3+
3 118,12
4+
4 200,4 158,14 167,7
5+
5 120,22
6+
6 48,36 126,22
7+
7 137,7 33,33
8+
8 1,1 103,31 117,40
9+
9 92,28 148,35
10+
10 169,39
11+
11 118,18 141,38
12+
12 34,8 48,31 82,39 45,8
13+
13 179,34
14+
14 122,16 44,20 71,38
15+
15 196,27 93,6
16+
16 44,27 33,2 132,34
17+
17 61,2
18+
18 94,20
19+
19 200,14 185,4 92,14 159,8
20+
20 171,26 148,10
21+
21 160,30 135,7
22+
22 38,18 125,42 150,33 195,19
23+
23 40,1
24+
24 33,31 95,27 165,21 172,7
25+
25 42,7
26+
26 196,17 192,43
27+
27 97,13 35,9
28+
28 93,12
29+
29 157,7
30+
30 157,39
31+
31 109,38 41,33
32+
32 193,21 168,9 65,43
33+
33 183,16 7,33 24,31 16,2
34+
34 12,8 188,38 181,34
35+
35 27,9 72,2
36+
36 105,20 173,42
37+
37 92,35
38+
38 22,18 140,26
39+
39 43,41
40+
40 23,1 56,18 46,17
41+
41 121,17 31,33
42+
42 87,43 25,7
43+
43 171,20 39,41
44+
44 16,27 14,20
45+
45 161,10 12,8
46+
46 164,39 40,17
47+
47 109,1
48+
48 12,31 6,36
49+
49 66,40 171,29
50+
50 70,16
51+
51 174,34
52+
52 88,37
53+
53 57,20 80,41
54+
54 170,10 136,4
55+
55 146,11
56+
56 40,18
57+
57 53,20
58+
58 175,32
59+
59 191,23 91,6 102,29
60+
60 131,9 176,2 75,12
61+
61 122,33 138,36 17,2
62+
62 83,37 93,14 79,39
63+
63 82,9
64+
64 119,25 191,22 111,2
65+
65 190,16 32,43
66+
66 49,40
67+
67 89,40
68+
68 154,2 111,44 76,7 139,32
69+
69 114,35 135,35
70+
70 180,17 50,16 189,11
71+
71 14,38
72+
72 35,2 120,24
73+
73 187,5 173,5 122,26
74+
74 184,15 195,6
75+
75 60,12 101,16
76+
76 68,7
77+
77 190,21
78+
78 149,36 124,33 164,37 174,4
79+
79 62,39 161,4 142,42
80+
80 2,29 53,41 108,3
81+
81 169,40
82+
82 63,9 12,39
83+
83 62,37
84+
84 103,14 169,4
85+
85 197,5 156,17
86+
86 131,36 127,5
87+
87 42,43 179,36
88+
88 52,37 176,9
89+
89 67,40 170,29
90+
90 178,11 175,18
91+
91 59,6
92+
92 19,14 37,35 9,28
93+
93 62,14 15,6 28,12 143,3
94+
94 18,20 192,12
95+
95 24,27
96+
96 122,33 99,17
97+
97 27,13
98+
98 171,6
99+
99 96,17
100+
100 186,13 169,40 104,41 128,41 147,44
101+
101 75,16 194,12
102+
102 59,29 199,1
103+
103 8,31 84,14 118,9
104+
104 108,35 100,41
105+
105 36,20 157,21
106+
106 136,37
107+
107 191,28
108+
108 104,35 80,3
109+
109 31,38 47,1
110+
110 128,10
111+
111 68,44 64,2
112+
112 137,17
113+
113 152,2 163,16
114+
114 69,35
115+
115 140,15
116+
116 172,31
117+
117 176,24 8,40
118+
118 11,18 146,20 3,12 103,9
119+
119 130,7 64,25
120+
120 132,37 147,13 5,22 72,24
121+
121 41,17 180,35
122+
122 96,33 14,16 73,26 61,33
123+
123 133,44
124+
124 78,33
125+
125 22,42
126+
126 6,22 182,40 131,38
127+
127 86,5 149,36 133,26 144,44
128+
128 100,41 110,10
129+
129 168,28
130+
130 119,7 141,23
131+
131 126,38 86,36 60,9
132+
132 120,37 16,34
133+
133 123,44 134,29 127,26
134+
134 133,29
135+
135 69,35 21,7 136,40
136+
136 54,4 135,40 106,37
137+
137 7,7 112,17
138+
138 61,36
139+
139 68,32 187,22
140+
140 177,23 115,15 38,26
141+
141 130,23 11,38
142+
142 79,42
143+
143 93,3 184,22
144+
144 127,44
145+
145 156,30
146+
146 55,11 118,20
147+
147 120,13 100,44
148+
148 20,10 9,35 168,34
149+
149 127,36 78,36
150+
150 22,33
151+
151 155,33
152+
152 113,2 173,8
153+
153 158,1
154+
154 68,2
155+
155 191,36 151,33
156+
156 175,43 145,30 85,17
157+
157 30,39 105,21 29,7
158+
158 153,1 4,14
159+
159 19,8
160+
160 167,22 179,13 21,30
161+
161 45,10 79,4
162+
162 185,8 186,8
163+
163 113,16
164+
164 46,39 78,37
165+
165 24,21
166+
166 178,18
167+
167 160,22 4,7
168+
168 148,34 129,28 32,9
169+
169 100,40 84,4 81,40 10,39
170+
170 89,29 54,10
171+
171 49,29 20,26 43,20 98,6
172+
172 24,7 116,31
173+
173 73,5 36,42 152,8
174+
174 78,4 51,34
175+
175 189,4 156,43 58,32 90,18
176+
176 88,9 117,24 60,2
177+
177 140,23
178+
178 182,17 166,18 90,11
179+
179 160,13 13,34 87,36
180+
180 121,35 70,17 198,1
181+
181 34,34
182+
182 178,17 126,40
183+
183 33,16
184+
184 143,22 74,15
185+
185 19,4 162,8
186+
186 100,13 162,8
187+
187 139,22 73,5
188+
188 34,38
189+
189 175,4 70,11
190+
190 77,21 65,16
191+
191 155,36 107,28 64,22 59,23
192+
192 26,43 94,12
193+
193 32,21
194+
194 101,12
195+
195 22,19 74,6
196+
196 26,17 15,27
197+
197 85,5
198+
198 180,1
199+
199 102,1
200+
200 4,4 19,14

0 commit comments

Comments
 (0)