/
main.ts
184 lines (184 loc) · 5.14 KB
/
main.ts
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
import { Input, Select, Toggle, colors } from "cliffy";
import { Graph } from "./graph.ts";
import {
bellmanFord,
branchAndBound,
bruteForce,
cycleCanceling,
dijkstra,
doubleTree,
edmondsKarp,
kruskal,
nearestNeighbour,
prim,
subGraphs,
successiveShortestPath,
} from "./algorithms.ts";
import { logWeight } from "./utilities.ts";
export let balanced: boolean;
while (true) {
const directed = await Toggle.prompt("Is the graph directed?");
balanced = await Toggle.prompt("Is the graph balanced?");
const graph = new Graph(
await Input.prompt({
message: "Choose a graph",
suggestions: [
"1",
"2",
"3",
"Fluss",
"Fluss2",
"gross",
"ganzGross",
"ganzGanzGross",
"G_1_2",
"G_1_20",
"G_1_200",
"G_10_20",
"G_10_200",
"G_100_200",
"K_10",
"K_10e",
"K_12",
"K_12e",
"K_15",
"K_15e",
"K_20",
"K_30",
"K_50",
"K_70",
"K_100",
"Kostenminimal1",
"Kostenminimal2",
"Kostenminimal3",
"Kostenminimal4",
"Kostenminimal_gross1",
"Kostenminimal_gross2",
"Kostenminimal_gross3",
"Wege1",
"Wege2",
"Wege3",
],
}),
directed,
balanced
);
while (true) {
const command = await Select.prompt({
message: "What do you want to see?",
options: [
{ name: "Size of subgraphs", value: "subgraphsSize" },
{ name: "Weight of MST via Prim", value: "primWeight" },
{ name: "Weight of MST via Kruskal", value: "kruskalWeight" },
{ name: "Length via Nearest Neighbor", value: "nearestNeighbor" },
{ name: "Length via Double Tree", value: "doubleTree" },
{ name: "Length via Brute Force", value: "bruteForce" },
{ name: "Length via Branch and Bound", value: "branchAndBound" },
{ name: "Shortest Path via Dijkstra", value: "dijkstra" },
{ name: "Shortest Path via Bellman-Ford", value: "bellmanFord" },
{ name: "Max Flow via Edmonds-Karp", value: "edmondsKarp" },
{ name: "Min Cost via Cycle-Canceling", value: "cycleCanceling" },
{
name: "Min Cost via Successive-Shortest-Path",
value: "successiveShortestPath",
},
{ name: "Exit", value: "exit" },
],
});
switch (command) {
case "subgraphsSize":
console.log(
`The graph has ${colors.cyan(
subGraphs(graph).length.toString()
)} subgraphs`
);
break;
case "primWeight":
console.log(
`The graph has an MST with a weight of ${logWeight(prim(graph))}`
);
break;
case "kruskalWeight":
console.log(
`The graph has an MST with a weight of ${logWeight(kruskal(graph))}`
);
break;
case "nearestNeighbor":
console.log(`The length is ${logWeight(nearestNeighbour(graph))}`);
break;
case "doubleTree":
console.log(`The length is ${logWeight(doubleTree(graph))}`);
break;
case "bruteForce":
console.log(`The length is ${logWeight(bruteForce(graph))}`);
break;
case "branchAndBound":
console.log(`The length is ${logWeight(branchAndBound(graph))}`);
break;
case "bellmanFord": {
const startNode = await Input.prompt({
message: "Choose a start node",
default: "0",
});
const endNode = await Input.prompt({
message: "Choose a end node",
default: "0",
});
console.log(
`The shortest paths from ${startNode} to ${endNode} is ${colors.cyan(
bellmanFord(graph, +startNode).nodes[+endNode].toFixed(2)
)}`
);
break;
}
case "dijkstra": {
const startNode = await Input.prompt({
message: "Choose a start node",
default: "0",
});
const endNode = await Input.prompt({
message: "Choose a end node",
default: "0",
});
console.log(
`The shortest paths from ${startNode} to ${endNode} is ${colors.cyan(
dijkstra(graph, +startNode)[+endNode].toFixed(2)
)}`
);
break;
}
case "edmondsKarp": {
const source = await Input.prompt({
message: "Choose a source node",
default: "0",
});
const sink = await Input.prompt({
message: "Choose a sink node",
default: "0",
});
console.log(
`The max flow from ${source} to ${sink} is ${colors.cyan(
edmondsKarp(graph, +source, +sink).maxFlow.toFixed(2)
)}`
);
break;
}
case "cycleCanceling":
case "successiveShortestPath": {
const chosenFunction =
command === "cycleCanceling"
? cycleCanceling
: successiveShortestPath;
console.log(
`The minimal cost is ${colors.cyan(
chosenFunction(graph)!.toFixed(2)
)}`
);
break;
}
}
if (command === "exit") {
break;
}
}
}