In this repo, you can find the implementation for:
- Graph –> adjacency list implementation
- Priority Queues –> Binary Min Heap
- Shortest Paths Algorithms –> See below
- Topological Sorting Algorithms –> Queue and DFS-based implementations
A CityGraph is used, where vertices are cities. Each city has outgoing and incoming edges, represented as roads. The roads have weights, which are the distances.
Directory contains a Binary Min Heap implementation, and a General Binary Min Heap implementation. The difference between the two
is that the former only accepts values (numbers) to be added / extracted, and the latter accepts an object with a
{ key: Number, obj: Object } and priority is kept on the val.
| Algorithm | Time | Space | Description |
|---|---|---|---|
| Bellman-Ford | O(VE) | O(V) | For V vertices, it loops at most all of the edges |
| Dijkstra | O((V + E) log(V)) | O(V) | Binary Min Heap used, so no amortized O(1) extractMin() operation. |
| A-star (A*) | O((V + E) log(V)) | O(V) | Worst case is Dijkstra's |
Notes:
Dijkstra's other time complexities with priority queues: More info
- Unsorted array: O(V2)
- Binary heap: O((E + V) log(V))
- Fibonacci heap: O(E + V log(V))
For A*, the heuristic is the time that it takes to get from the city to the destination, which takes into account the region that is being traversed in the country.
Contains two implementations for a topological sort:
- Queue-based
- DFS-based
Enjoy!!