-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathGraphDijkstra.ts
More file actions
28 lines (28 loc) · 1.11 KB
/
GraphDijkstra.ts
File metadata and controls
28 lines (28 loc) · 1.11 KB
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
/** Copyright https://github.com/microwind */
const V = 6, INF = Number.MAX_SAFE_INTEGER;
function minDistance(dist: number[], visited: boolean[]): number {
let min = INF, minIndex = -1;
for (let v = 0; v < V; v++) {
if (!visited[v] && dist[v] <= min) { min = dist[v]; minIndex = v; }
}
return minIndex;
}
function dijkstra(graph: number[][], src: number): void {
const dist: number[] = new Array(V).fill(INF);
const visited: boolean[] = new Array(V).fill(false);
dist[src] = 0;
for (let count = 0; count < V - 1; count++) {
const u = minDistance(dist, visited);
visited[u] = true;
for (let v = 0; v < V; v++) {
if (!visited[v] && graph[u][v] !== 0 && dist[u] !== INF && dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
console.log("Vertex Distance from Source");
for (let i = 0; i < V; i++) console.log(`${i} ${dist[i]}`);
}
const graph = [[0,4,0,0,0,0],[4,0,8,0,0,0],[0,8,0,7,0,4],[0,0,7,0,9,14],[0,0,0,9,0,10],[0,0,4,14,10,0]];
dijkstra(graph, 0);
export { dijkstra, minDistance };