|
| 1 | +import { Graph } from '../AdjacencyList'; |
| 2 | +import { AdjacencyMatrix } from '../AdjacencyMatrix'; |
| 3 | +import { FloydWarshall } from '../shortest-path/floyd-warshall/FloydWarshall'; |
| 4 | + |
| 5 | +export type GreedySpannerParams = { |
| 6 | + device: GPUDevice; |
| 7 | + graph: Graph; |
| 8 | + maxDistortion: number; |
| 9 | +}; |
| 10 | + |
| 11 | +export class GreedySpanner { |
| 12 | + graph: Graph; |
| 13 | + maxDistortion: number; |
| 14 | + #device: GPUDevice; |
| 15 | + #floydWarshall: FloydWarshall; |
| 16 | + |
| 17 | + constructor({ device, graph, maxDistortion }: GreedySpannerParams) { |
| 18 | + this.graph = graph; |
| 19 | + this.maxDistortion = maxDistortion; |
| 20 | + this.#device = device; |
| 21 | + |
| 22 | + this.#floydWarshall = new FloydWarshall({ |
| 23 | + device, |
| 24 | + graph: this.graph, |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + async compute() { |
| 29 | + await this.#floydWarshall.init(); |
| 30 | + console.time('Floyd Warshall'); |
| 31 | + await this.#floydWarshall.compute(true); |
| 32 | + console.timeEnd('Floyd Warshall'); |
| 33 | + |
| 34 | + const spanner = new Graph(); |
| 35 | + this.graph.nodes.forEach((node) => { |
| 36 | + spanner.addNode(node); |
| 37 | + }); |
| 38 | + |
| 39 | + console.log(this.maxDistortion); |
| 40 | + const distanceMatrix = new AdjacencyMatrix( |
| 41 | + this.#floydWarshall.distanceMatrix.size, |
| 42 | + Float32Array |
| 43 | + ); |
| 44 | + |
| 45 | + for (let x = 0; x < distanceMatrix.size; x++) { |
| 46 | + for (let y = 0; y < distanceMatrix.size; y++) { |
| 47 | + distanceMatrix.set(x, y, x === y ? 0 : Infinity); |
| 48 | + } |
| 49 | + } |
| 50 | + const sortedEdges = Array.from(this.graph.edges) |
| 51 | + .map(([_, edge]) => edge) |
| 52 | + .sort((a, b) => a.weight - b.weight); |
| 53 | + |
| 54 | + let skipped = 0; |
| 55 | + for (const edge of sortedEdges) { |
| 56 | + console.log( |
| 57 | + edge.weight.toFixed(3) === |
| 58 | + this.#floydWarshall.distanceMatrix.get(edge.start, edge.end).toFixed(3) |
| 59 | + ); |
| 60 | + if ( |
| 61 | + distanceMatrix.get(edge.start, edge.end) > |
| 62 | + this.maxDistortion * this.#floydWarshall.distanceMatrix.get(edge.start, edge.end) |
| 63 | + ) { |
| 64 | + spanner.addEdge(edge); |
| 65 | + |
| 66 | + for (let x = 0; x < distanceMatrix.size; x++) { |
| 67 | + for (let y = 0; y < distanceMatrix.size; y++) { |
| 68 | + const weight = Math.min( |
| 69 | + distanceMatrix.get(x, y), |
| 70 | + distanceMatrix.get(x, edge.start) + edge.weight + distanceMatrix.get(edge.end, y), |
| 71 | + distanceMatrix.get(x, edge.end) + edge.weight + distanceMatrix.get(edge.start, y) |
| 72 | + ); |
| 73 | + distanceMatrix.set(x, y, weight); |
| 74 | + } |
| 75 | + } |
| 76 | + } else { |
| 77 | + skipped++; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + console.log({ skipped }); |
| 82 | + console.log({ skippedPercentage: (skipped / sortedEdges.length) * 100 }); |
| 83 | + return spanner; |
| 84 | + } |
| 85 | +} |
0 commit comments