{"id":13858889,"url":"https://github.com/easbar/fast_paths","last_synced_at":"2025-04-13T04:16:27.023Z","repository":{"id":44668934,"uuid":"192286022","full_name":"easbar/fast_paths","owner":"easbar","description":"Fast shortest path calculations for Rust ","archived":false,"fork":false,"pushed_at":"2024-05-07T09:57:10.000Z","size":1386,"stargazers_count":279,"open_issues_count":4,"forks_count":24,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-13T04:16:09.437Z","etag":null,"topics":["contraction-hierarchies","dijkstra-algorithm","road-network","routing-algorithm","shortest-paths","traffic-simulation"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/easbar.png","metadata":{"files":{"readme":"README.md","changelog":"changelog","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-17T06:15:39.000Z","updated_at":"2025-04-09T22:12:13.000Z","dependencies_parsed_at":"2024-10-30T16:03:06.551Z","dependency_job_id":"8ed19b0f-7c62-480e-b050-915bc92b8f46","html_url":"https://github.com/easbar/fast_paths","commit_stats":{"total_commits":47,"total_committers":5,"mean_commits":9.4,"dds":"0.21276595744680848","last_synced_commit":"4e663e5044afa1cd465bb6f571c7b0035a927f3d"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easbar%2Ffast_paths","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easbar%2Ffast_paths/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easbar%2Ffast_paths/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/easbar%2Ffast_paths/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/easbar","download_url":"https://codeload.github.com/easbar/fast_paths/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248661716,"owners_count":21141451,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["contraction-hierarchies","dijkstra-algorithm","road-network","routing-algorithm","shortest-paths","traffic-simulation"],"created_at":"2024-08-05T03:02:24.441Z","updated_at":"2025-04-13T04:16:26.999Z","avatar_url":"https://github.com/easbar.png","language":"Rust","readme":"# Fast Paths\n\nThe most famous algorithms used to calculate shortest paths are probably Dijkstra's algorithm and A*. However, shortest path calculation can be done much faster by preprocessing the graph.\n\n*Fast Paths* uses *Contraction Hierarchies*, one of the best known speed-up techniques for shortest path calculation. It is especially suited to calculate shortest paths in road networks, but can be used for any directed graph with positive, non-zero edge weights.\n\n### Installation\n\nIn `Cargo.toml`\n\n```toml\n[dependencies]\nfast_paths = \"1.0.0\"\n\n```\n### Basic usage\n\n```rust\n// begin with an empty graph\nlet mut input_graph = InputGraph::new();\n\n// add an edge between nodes with ID 0 and 6, the weight of the edge is 12.\n// Note that the node IDs should be consecutive, if your graph has N nodes use 0...N-1 as node IDs,\n// otherwise performance will degrade.\ninput_graph.add_edge(0, 6, 12);\n// ... add many more edges here\n\n// freeze the graph before using it (you cannot add more edges afterwards, unless you call thaw() first)\ninput_graph.freeze();\n\n// prepare the graph for fast shortest path calculations. note that you have to do this again if you want to change the\n// graph topology or any of the edge weights\nlet fast_graph = fast_paths::prepare(\u0026input_graph);\n\n// calculate the shortest path between nodes with ID 8 and 6 \nlet shortest_path = fast_paths::calc_path(\u0026fast_graph, 8, 6);\n\nmatch shortest_path {\n    Some(p) =\u003e {\n        // the weight of the shortest path\n        let weight = p.get_weight();\n        \n        // all nodes of the shortest path (including source and target)\n        let nodes = p.get_nodes();\n    },\n    None =\u003e {\n        // no path has been found (nodes are not connected in this graph)\n    }\n}\n\n\n```\n\n### Batch-wise shortest path calculation\n\nFor batch-wise calculation of shortest paths the method described above is inefficient. You should keep the `PathCalculator` object to execute multiple queries instead:\n\n```rust\n// ... see above\n// create a path calculator (note: not thread-safe, use a separate object per thread)\nlet mut path_calculator = fast_paths::create_calculator(\u0026fast_graph);\nlet shortest_path = path_calculator.calc_path(\u0026fast_graph, 8, 6);\n```\n\n### Calculating paths between multiple sources and targets\n\nWe can also efficiently calculate the shortest path when we want to consider multiple sources or targets:\n\n```rust\n// ... see above\n// we want to either start at node 2 or 3 both of which carry a different initial weight\nlet sources = vec![(3, 5), (2, 7)];\n// ... and go to either node 6 or 8 which also both carry a cost upon arrival\nlet targets = vec![(6, 2), (8, 10)];\n// calculate the path with minimum cost that connects any of the sources with any of the targets while taking into \n// account the initial weights of each source and node\nlet shortest_path = path_calculator.calc_path_multiple_sources_and_targets(\u0026fast_graph, sources, targets);\n```\n\n### Serializing the prepared graph\n\n`FastGraph` implements standard [Serde](https://serde.rs/) serialization.\n\nTo be able to use the graph in a 32bit WebAssembly environment, it needs to be transformed to a 32bit representation when preparing it on a 64bit system. This can be achieved with the following two methods, but it will only work for graphs that do not exceed the 32bit limit, i.e. the number of nodes and edges and all weights must be below 2^32.\n\n```rust\nuse fast_paths::{deserialize_32, serialize_32, FastGraph};\n\n#[derive(Serialize, Deserialize)]\nstruct YourData {\n    #[serde(serialize_with = \"serialize_32\", deserialize_with = \"deserialize_32\")]\n    graph: FastGraph,\n    // the rest of your struct\n}\n```\n\n### Preparing the graph after changes\n\nThe graph preparation can be done much faster using a fixed node ordering, which is just a permutation of node ids. This can be done like this:\n\n```rust\nlet fast_graph = fast_paths::prepare(\u0026input_graph);\nlet node_ordering = fast_graph.get_node_ordering();\n\nlet another_fast_graph = fast_paths::prepare_with_order(\u0026another_input_graph, \u0026node_ordering);\n```\n\nFor this to work `another_input_graph` must have the same number of nodes as `input_graph`, otherwise `prepare_with_order` will return an error. Also performance will only be acceptable if `input_graph` and `another_input_graph` are similar to each other, say you only changed a few edge weights. \n \n### Benchmarks\n\n*FastPaths* was run on a single core on a consumer-grade laptop using the road networks provided for the [DIMACS implementation challenge graphs](http://www.diag.uniroma1.it/~challenge9/download.shtml). The following graphs were used for the benchmark:\n\n|area|number of nodes|number of edges|\n|-|-|-|\n|New York|264.347|730.100|\n|California\u0026Nevada|1.890.816|4.630.444|\n|USA|23.947.348|57.708.624|\n\n|graph|metric|preparation time|average query time|out edges|in edges|\n|-|-|-|-|-|-|\n|NY city|distance|9 s|55 μs|747.555|747.559|\n|CAL\u0026NV|distance|36 s|103 μs|4.147.109|4.147.183|\n|USA|distance|10.6 min|630 μs|52.617.216|52.617.642|\n|NY city|time|6 s|26 μs|706.053|706.084|\n|CAL\u0026NV|time|24 s|60 μs|3.975.276|3.975.627|\n|USA|time|5.5 min|305 μs|49.277.058|49.283.162|\n\nThe shortest path calculation time was averaged over 100k random routing queries. The benchmarks were run on a Macbook Pro M1 Max using Rust 1.74.1.\nThe code for running these benchmarks can be found on the `benchmarks` branch.\n\nThere are also some benchmarks using smaller maps included in the test suite. You can run them like this:\n```shell\nexport RUST_TEST_THREADS=1; cargo test --release -- --ignored --nocapture\n```\n\n### Graph limitations \n\n- loop-edges (from node A to node A) will be ignored, because since we are only considering positive non-zero edge-weights they cannot be part of a shortest path \n- in case the graph has duplicate edges (multiple edges from node A to node B) only the edge with the lowest weight will be considered\n\n### Special Thanks\n\nThanks to [Dustin Carlino](http://github.com/dabreegster) from [A/B Street](http://github.com/dabreegster/abstreet)!\n\n\n#### License\n\nThis project is licensed under either of\n\n * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or\n   http://www.apache.org/licenses/LICENSE-2.0)\n * MIT license ([LICENSE-MIT](LICENSE-MIT) or\n   http://opensource.org/licenses/MIT)\n\nat your option.\n\n#### Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in fast_paths by you, as defined in the Apache-2.0 license, shall be\ndual licensed as above, without any additional terms or conditions.\n","funding_links":[],"categories":["Rust"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feasbar%2Ffast_paths","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feasbar%2Ffast_paths","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feasbar%2Ffast_paths/lists"}