|
2 | 2 |
|
3 | 3 | A pretty good implementation of Dijkstra's shortest-path algorithm for Deno.
|
4 | 4 |
|
5 |
| -If you ever run into a problem that can be represented as a graph where the solution has something to do with finding the shortest path between nodes, this algorithm is nothing short of magical. It is well worth the time to learn how to use it, even if you don't really understand how the algorithm works. |
| 5 | +If you ever run into a problem that can be represented as a graph where the |
| 6 | +solution has something to do with finding the shortest path between nodes, this |
| 7 | +algorithm is nothing short of magical. It is well worth the time to learn how to |
| 8 | +use it, even if you don't really understand how the algorithm works. |
6 | 9 |
|
7 |
| -This implementation of Dijkstra'a algorithm is able to process large in-memory graphs. It will perform |
8 |
| -reasonably well even when the number of edges is in the millions. The performance is `O(n*log(n))`, where `n` is proportional to the number of nodes plus the number of edges in the graph. |
| 10 | +This implementation of Dijkstra'a algorithm is able to process large in-memory |
| 11 | +graphs. It will perform reasonably well even when the number of edges is in the |
| 12 | +millions. The performance is `O(n*log(n))`, where `n` is proportional to the |
| 13 | +number of nodes plus the number of edges in the graph. |
9 | 14 |
|
10 | 15 | This code was adapted to Typescript from
|
11 | 16 | [A Walkthrough of Dijkstra's Algorithm (In JavaScript!)](https://medium.com/@adriennetjohnson/a-walkthrough-of-dijkstras-algorithm-in-javascript-e94b74192026)
|
12 | 17 | on Medium. This implemetation was originally part of
|
13 | 18 | [BlackholeSuns](https://github.com/j50n/blackholesuns), an open source project
|
14 | 19 | that allowed thousands of No Man's Sky players to navigate the galaxy using
|
15 |
| -mapped black holes. This version is cleaned up a bit and includes a few bug fixes and more tests than the original. See also |
| 20 | +mapped black holes. This version is cleaned up a bit and includes a few bug |
| 21 | +fixes and more tests than the original. See also |
16 | 22 | [Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) on
|
17 | 23 | Wikipedia.
|
18 | 24 |
|
19 | 25 | # Usage Hints
|
20 | 26 |
|
21 |
| -Dijkstra's algorithm actually calculates the shortest paths from the start node to every other node in the graph. In other words, it isn't just calculating one path at a time. This can let you cheaply do things like find the 20 closest nodes from a particular node in the graph, for example. |
| 27 | +Dijkstra's algorithm actually calculates the shortest paths from the start node |
| 28 | +to every other node in the graph. In other words, it isn't just calculating one |
| 29 | +path at a time. This can let you cheaply do things like find the 20 closest |
| 30 | +nodes from a particular node in the graph, for example. |
22 | 31 |
|
23 |
| -You can reverse the direction of the calculation. Calling it "start node" is just a convention. It can also be an end node if you set up the graph correctly. |
| 32 | +You can reverse the direction of the calculation. Calling it "start node" is |
| 33 | +just a convention. It can also be an end node if you set up the graph correctly. |
24 | 34 |
|
25 |
| -This implementation supports cloning the solver and extending its graph dynamically. Graph generation can be expensive. For many cases, most of the graph can be reused, with a only a small portion needing to be dynamic. |
| 35 | +This implementation supports cloning the solver and extending its graph |
| 36 | +dynamically. Graph generation can be expensive. For many cases, most of the |
| 37 | +graph can be reused, with a only a small portion needing to be dynamic. |
26 | 38 |
|
27 | 39 | # Example
|
28 | 40 |
|
29 | 41 | This example recreates the example from the article referenced earlier. The
|
30 | 42 | nodes are mapped to integers from `0` to `n-1`. The names and weights are taken
|
31 |
| -from [A Walkthrough of Dijkstra's Algorithm (In JavaScript!)](https://medium.com/@adriennetjohnson/a-walkthrough-of-dijkstras-algorithm-in-javascript-e94b74192026). |
| 43 | +from |
| 44 | +[A Walkthrough of Dijkstra's Algorithm (In JavaScript!)](https://medium.com/@adriennetjohnson/a-walkthrough-of-dijkstras-algorithm-in-javascript-e94b74192026). |
32 | 45 |
|
33 | 46 | ```ts
|
34 | 47 | const FULLSTACK = 0;
|
|
0 commit comments