Skip to content

Commit 2a005e6

Browse files
committed
Add karger min cut test
1 parent a355db0 commit 2a005e6

20 files changed

+797
-11
lines changed

src/data-structures/basics/linkedList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,4 @@ class LinkedList<T> {
163163
};
164164
}
165165

166-
export = LinkedList;
166+
export default LinkedList;

src/data-structures/basics/llNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ class LLNode<T> {
66
}
77
}
88

9-
export = LLNode;
9+
export default LLNode;

src/data-structures/basics/node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ class Node {
66
}
77
}
88

9-
export = Node;
9+
export default Node;

src/data-structures/basics/queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ class Queue {
4141
}
4242
}
4343

44-
export = Queue;
44+
export default Queue;

src/data-structures/basics/stack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ class Stack {
4343
}
4444
}
4545

46-
export = Stack;
46+
export default Stack;

src/data-structures/disjoint-sets/forestNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ class ForestNode<T> {
77
}
88
}
99

10-
export = ForestNode;
10+
export default ForestNode;

src/data-structures/disjoint-sets/forestSet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ class ForestSet<T> {
3838
};
3939
}
4040

41-
export = ForestSet;
41+
export default ForestSet;

src/data-structures/disjoint-sets/listSet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ class ListSet<T> {
6666
};
6767
}
6868

69-
export = ListSet;
69+
export default ListSet;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { getTestAnswer } from "../../helperTests";
2+
import Graph from "./graph";
3+
4+
describe("Graph class test", () => {
5+
//TODO: Add tests for helpers
6+
7+
test("Kargen Min Cut Test", () => {
8+
const TEST_28 = "random_28_125.txt";
9+
const TEST_32 = "random_32_150.txt";
10+
const TEST_36 = "random_36_175.txt";
11+
const TEST_40 = "random_40_200.txt";
12+
13+
const test = (n: number, file: string) => {
14+
let min = Infinity;
15+
for (let i = 0; i < n; i++) {
16+
const g = Graph.createListAdj(
17+
`${__dirname}/test-datasets/kargen-min-cut/input_${file}`
18+
);
19+
min = Math.min(min, g.kargerMinCut());
20+
}
21+
const ans = getTestAnswer(
22+
`${__dirname}/test-datasets/kargen-min-cut/output_${file}`
23+
);
24+
expect(min).toBe(ans);
25+
};
26+
27+
test(600, TEST_28);
28+
test(600, TEST_32);
29+
test(700, TEST_36);
30+
test(700, TEST_40);
31+
});
32+
});

src/data-structures/graph/graph.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import BHNode from "../heaps/bHNode";
77
// import FHNode from "./heaps/fHNode";
88
import ListSet from "../disjoint-sets/listSet";
99
import ForestSet from "../disjoint-sets/forestSet";
10-
import fs from "fs";
10+
import * as fs from "fs";
1111
import Vertex from "./vertex";
1212

1313
// Returns a random number between [min,max)
@@ -65,6 +65,7 @@ class Graph<T> {
6565
return false;
6666
};
6767

68+
// FOR karge min cut
6869
// Returns how many edges are between verteces u and v
6970
hme = (u: T, v: T) => {
7071
if (!this.contains(u)) return false;
@@ -76,6 +77,7 @@ class Graph<T> {
7677
return c;
7778
};
7879

80+
// FOR karge min cut
7981
// Returns the number of edges of this graph
8082
countEdges = () => {
8183
let c = 0;
@@ -87,6 +89,7 @@ class Graph<T> {
8789
return c / 2;
8890
};
8991

92+
// FOR karge min cut
9093
// Returns the key of two neighbours [u,v]
9194
pickRandomEdge = () => {
9295
const keys = [...this.list.keys()];
@@ -98,6 +101,7 @@ class Graph<T> {
98101
return [u, v];
99102
};
100103

104+
// FOR karge min cut
101105
// Merge two verteces into a single one
102106
mergeVerteces = (u: T, v: T) => {
103107
// adds all neighbours of v to u
@@ -1099,4 +1103,4 @@ class Graph<T> {
10991103
};
11001104
}
11011105

1102-
export = Graph;
1106+
export default Graph;

src/data-structures/graph/test-datasets/kargen-min-cut/input_random_28_125.txt

Lines changed: 125 additions & 0 deletions
Large diffs are not rendered by default.

src/data-structures/graph/test-datasets/kargen-min-cut/input_random_32_150.txt

Lines changed: 150 additions & 0 deletions
Large diffs are not rendered by default.

src/data-structures/graph/test-datasets/kargen-min-cut/input_random_36_175.txt

Lines changed: 175 additions & 0 deletions
Large diffs are not rendered by default.

src/data-structures/graph/test-datasets/kargen-min-cut/input_random_40_200.txt

Lines changed: 200 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
31
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
39
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
43
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
61

src/data-structures/graph/vertex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ interface Vertex<T> {
33
weight: number;
44
}
55

6-
export = Vertex;
6+
export default Vertex;

src/helperTests.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import * as fs from "fs";
2+
3+
export const getTestAnswer = (file: string): number => {
4+
const data = fs.readFileSync(file, {
5+
encoding: "utf8",
6+
flag: "r",
7+
});
8+
let line = "";
9+
for (let i = 0; i < data.length; i++) {
10+
line += data[i];
11+
}
12+
return parseInt(line);
13+
};
14+
15+
export const getTestAnswerArr = (file: string): number[] => {
16+
const data = fs.readFileSync(file, {
17+
encoding: "utf8",
18+
flag: "r",
19+
});
20+
let line = "";
21+
for (let i = 0; i < data.length; i++) {
22+
line += data[i];
23+
}
24+
const arr = line.split(",");
25+
return arr.map((x) => +x);
26+
};
27+
28+
export const createArr = (file: string): number[] => {
29+
const data = fs.readFileSync(file, {
30+
encoding: "utf8",
31+
flag: "r",
32+
});
33+
let line = "";
34+
const arr = [];
35+
for (let i = 0; i < data.length; i++) {
36+
if (data[i] !== "\n") {
37+
line += data[i];
38+
} else {
39+
arr.push(parseInt(line));
40+
line = "";
41+
}
42+
}
43+
if (line.trim() !== "") arr.push(parseInt(line));
44+
return arr;
45+
};
46+
47+
export const createHT = (file: string) => {
48+
const data = fs.readFileSync(file, { encoding: "utf8", flag: "r" });
49+
let line = "";
50+
const ht = new Map();
51+
let t;
52+
for (let i = 0; i < data.length; i++) {
53+
if (data[i] !== "\n") {
54+
line += data[i];
55+
} else {
56+
ht.set(parseInt(line), true);
57+
line = "";
58+
}
59+
}
60+
if (line.trim() !== "") ht.set(parseInt(line), true);
61+
return ht;
62+
};
63+
64+
export const createCitiesArr = (file: string, f = true, tp = false) => {
65+
const data = fs.readFileSync(`input_${file}`, {
66+
encoding: "utf8",
67+
flag: "r",
68+
});
69+
let line = "";
70+
let split = [];
71+
const arr = [];
72+
73+
for (let i = 0; i < data.length; i++) {
74+
if (data[i] !== "\n") {
75+
line += data[i];
76+
} else {
77+
const c = { x: 0, y: 0 };
78+
split = line.trim().split(" ");
79+
if (f) {
80+
if (!tp) {
81+
c.x = parseFloat(split[0]);
82+
c.y = parseFloat(split[1]);
83+
} else {
84+
c.x = parseFloat(split[1]);
85+
c.y = parseFloat(split[2]);
86+
}
87+
} else {
88+
c.x = parseInt(split[0]);
89+
c.y = parseInt(split[1]);
90+
}
91+
arr.push(c);
92+
line = "";
93+
}
94+
}
95+
return arr;
96+
};

0 commit comments

Comments
 (0)