{"id":23291263,"url":"https://github.com/tomasjohansson/adapters-shortest-paths","last_synced_at":"2025-07-01T05:36:51.423Z","repository":{"id":32211660,"uuid":"109717592","full_name":"TomasJohansson/adapters-shortest-paths","owner":"TomasJohansson","description":"Adapters for Java implementations finding the shortest paths between two locations. This is useful for travel routing  when you want to minimize the total time or total distance.","archived":false,"fork":false,"pushed_at":"2023-06-14T22:29:09.000Z","size":952,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-06T17:52:11.542Z","etag":null,"topics":["k-shortest-paths","kshortestpaths","shortest-path-algorithm","shortest-paths-algorithm","shortest-route","shortest-routes","travel-routes","travel-routing","yen","yen-algorithm","yenalgorithm"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TomasJohansson.png","metadata":{"files":{"readme":"docs/README.md","changelog":null,"contributing":null,"funding":null,"license":"License.txt","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":"2017-11-06T16:03:52.000Z","updated_at":"2022-02-11T12:34:01.000Z","dependencies_parsed_at":"2025-04-06T17:45:56.240Z","dependency_job_id":"d42dce09-a784-4538-b23f-24e7443366fb","html_url":"https://github.com/TomasJohansson/adapters-shortest-paths","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/TomasJohansson/adapters-shortest-paths","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasJohansson%2Fadapters-shortest-paths","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasJohansson%2Fadapters-shortest-paths/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasJohansson%2Fadapters-shortest-paths/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasJohansson%2Fadapters-shortest-paths/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TomasJohansson","download_url":"https://codeload.github.com/TomasJohansson/adapters-shortest-paths/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasJohansson%2Fadapters-shortest-paths/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262905204,"owners_count":23382524,"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":["k-shortest-paths","kshortestpaths","shortest-path-algorithm","shortest-paths-algorithm","shortest-route","shortest-routes","travel-routes","travel-routing","yen","yen-algorithm","yenalgorithm"],"created_at":"2024-12-20T05:16:27.778Z","updated_at":"2025-07-01T05:36:51.404Z","avatar_url":"https://github.com/TomasJohansson.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# License Notice\nNotice that the \"core\" library with the API and general code is released with MIT License.\nHowever, the libraries which are implementing adapters are licensed in the same way as the adapted libraries.\nCurrently there are **six** such adapter libraries, and **if you intend to use one or more of them you must check their licenses**:\n* [adapters-shortest-paths-impl-yanqi](https://github.com/TomasJohansson/adapters-shortest-paths/tree/master/adapters-shortest-paths-impl-yanqi)\n* [adapters-shortest-paths-impl-bsmock](https://github.com/TomasJohansson/adapters-shortest-paths/tree/master/adapters-shortest-paths-impl-bsmock)\n* [adapters-shortest-paths-impl-jgrapht](https://github.com/TomasJohansson/adapters-shortest-paths/tree/master/adapters-shortest-paths-impl-jgrapht)\n* [adapters-shortest-paths-impl-mulavito](https://github.com/TomasJohansson/adapters-shortest-paths/tree/master/adapters-shortest-paths-impl-mulavito)\n* [adapters-shortest-paths-impl-reneargento](https://github.com/TomasJohansson/adapters-shortest-paths/tree/master/adapters-shortest-paths-impl-reneargento)\n* [adapters-shortest-paths-impl-jython_networkx](https://github.com/TomasJohansson/adapters-shortest-paths/tree/master/adapters-shortest-paths-impl-jython_networkx)\n\nNote that only **two** (yanqi and bsmock) of the above six implementation libraries are available from OSSRH (\"Maven Central\").   \n\n## Prerequisites if you are trying to compile the source code from this git repository\n* [Git](https://git-scm.com) (or [Git BASH for Windows](https://gitforwindows.org))\n* [Maven](http://maven.apache.org) (e.g. version 3.6.3)\n* Java 9. Define an environment variable \"JDK9\" pointing to the path where you have installed (or extracted tar.gz file) [JDK 9](http://jdk.java.net/archive/) (which supports compiling to Java 6 , and this environment variable JDK9 is used in the file pom.xml in the root directory)\n* See also the [prerequisites for the Jython implementation](https://github.com/TomasJohansson/adapters-shortest-paths/tree/master/adapters-shortest-paths-impl-jython_networkx)\n\nIf the above conditions are fulfilled and you have access to an internet connection then it should hopefully work to use this library as below:\n```shell\ngit clone https://github.com/TomasJohansson/adapters-shortest-paths\nmvn test\n```\nNote that the two sample projects are not included as modules (from the root level file pom.xml).  \nThey can be run separately like this:\n\n```shell\ncd adapters-shortest-paths-example-project\nmvn test\n\ncd adapters-shortest-paths-example-project-jpa-entities   \nmvn test\n```\n\n## Information about why only some libraries are deployed to OSSRH (\"Maven Central\")\n\nCurrently only **two** (yanqi and bsmock) of the above six implementation libraries are available from OSSRH (\"Maven Central\").\u003cbr\u003e\nThe reasons are related to licenses and performance.\n\nIf you would decide which project to use then you would likely want to use an implementation producing correct results and as fast as possible.\u003cbr\u003e\nThe test cases indicate that all six implementations produce the same results, which means that correctness is not a relevant factor when deciding which library to use.\n\nIf we for a second would ignore the licensing issue, then why not simply only deploy the fastest implementation?\u003cbr\u003e\nWell, since the project is an adapter it would look weird if someone finds it through a website where you can search for maven projects, and then find this \"adapter\" project with only one implementation.\u003cbr\u003e\nTherefore I consider two libraries as a minimum that should be deployed.\n\nThen the two fastest should be deployed?\u003cbr\u003e\nWell, yes that would be natural if you ignore the licensing issue.\u003cbr\u003e\nThe test cases with big graphs indicate that the \"reneargento\" implementation is the fastest, and then the second fastest is \"yanqi\".\u003cbr\u003e\nThe reason for not having deployed \"reneargento\" is that the license is **probably** less permissive and I do not want to get into trouble.\u003cbr\u003e\nMore information about the license for that adaptee project can be found here:\u003cbr\u003e\nhttps://github.com/TomasJohansson/algorithms-sedgewick-wayne\n\nThe permissive Apache License is used for both of the two adaptee projects (forks) used by the adapters \"yanqi\" and \"bsmock\".\n\n## Adapters for Java implementations of Graph algorithms useful for finding the shortest paths ins travel routing.\n\nThe purpose of this project is to provide Adapters for Java implementations finding the shortest path**s** (and note the plural of the word 'path**s**').\u003cbr\u003e\nThis is useful for travel routing  when you want to minimize the total time or total distance.\u003cbr\u003e\nThe project might also be useful for other situations, but travel routing is the main kind of application I have in mind.\u003cbr\u003e\nRegarding graph theory applicable for finding the shortest paths in travel routing, see more information about that further down in a separate section at this page.\n  \n**Currently there are six implemented Adapters**, i.e. six different implementations can be used.\nSince the Client code is using the same Target interface (see the [Adapter Design Pattern](https://en.wikipedia.org/wiki/Adapter_pattern)) it is possible to **reuse the same test code for the different implementations**.\nTherefore you can assert their results against each other, which could help finding bugs. If one implementation would produce a different result than the others, then it is likely a bug that should be reported and hopefully fixed. However, note that the tested graph need to be constructed in such a way that there must not be more than one path (among the first shortest paths you use test assertions for) with the same total weight. If multiple paths have the same total weight then it is not obvious which should be sorted first, and then it would not be surprising if different implementations produce different results.\n\nWhen you run such tests with the same test data for different implementations then you can also easily **compare the performance for the different implementations**.       \n\n### Example of how to use this shortest paths adapter library:\n\nThe Java code example below uses the following graph with four vertices (A,B,C,D) and five edges with weights.\u003cbr\u003e(A to B (5) , A to C (6) , B to C (7)  , B to D (8) , C to D (9) ).\u003cbr\u003e![alt text](images/shortest_paths_getting_started_example.gif \"Logo Title Text 1\")\u003cbr\u003e\nThere are three possible paths from A to D , with the total weight within parenthesis : \n* A to B to D (total cost: 13 = 5 + 8)\n* A to C to D (total cost: 15 = 6 + 9)\n* A to B to C to D (total cost: 21 = 5 + 7 + 9)\n\nFor example, the vertices might represent cities, and the edges might represent roads with distances as weights.\n\nThe Java code below can be used for finding the shortest paths (sorted with the shortest first) from A to D :\u003cbr\u003e\n(and similar code as below can be found in the example project's file [ExampleMain.java](https://github.com/TomasJohansson/adapters-shortest-paths/blob/master/adapters-shortest-paths-example-project/src/main/java/shortest_paths_getting_started_example/ExampleMain.java))\n\n```java\nimport static com.programmerare.shortestpaths.core.impl.EdgeImpl.createEdge;\nimport static com.programmerare.shortestpaths.core.impl.GraphImpl.createGraph;\nimport static com.programmerare.shortestpaths.core.impl.VertexImpl.createVertex;\nimport static com.programmerare.shortestpaths.core.impl.WeightImpl.createWeight;\nimport java.util.Arrays;\nimport java.util.List;\nimport com.programmerare.shortestpaths.adapter.bsmock.PathFinderFactoryBsmock;\nimport com.programmerare.shortestpaths.adapter.jgrapht.PathFinderFactoryJgrapht;\nimport com.programmerare.shortestpaths.adapter.mulavito.PathFinderFactoryMulavito;\nimport com.programmerare.shortestpaths.adapter.reneargento.PathFinderFactoryReneArgento;\nimport com.programmerare.shortestpaths.adapter.yanqi.PathFinderFactoryYanQi;\nimport com.programmerare.shortestpaths.adapter.jython_networkx.PathFinderFactoryJythonNetworkx;// requires Jython and the Python pip package networkx \nimport com.programmerare.shortestpaths.core.api.Edge;\nimport com.programmerare.shortestpaths.core.api.Graph;\nimport com.programmerare.shortestpaths.core.api.Path;\nimport com.programmerare.shortestpaths.core.api.PathFinder;\nimport com.programmerare.shortestpaths.core.api.PathFinderFactory;\nimport com.programmerare.shortestpaths.core.api.Vertex;\nimport com.programmerare.shortestpaths.core.api.Weight;\nimport com.programmerare.shortestpaths.core.validation.GraphEdgesValidationDesired;\n...\n\n\tVertex a = createVertex(\"A\");\n\tVertex b = createVertex(\"B\");\n\tVertex c = createVertex(\"C\");\n\tVertex d = createVertex(\"D\");\n\n\tList\u003cEdge\u003e edges = Arrays.asList(\n\t\tcreateEdge(a, b, createWeight(5)),\n\t\tcreateEdge(a, c, createWeight(6)),\n\t\tcreateEdge(b, c, createWeight(7)),\n\t\tcreateEdge(b, d, createWeight(8)),\n\t\tcreateEdge(c, d, createWeight(9))\n\t);\n\t\n\tGraph graph = createGraph(edges, GraphEdgesValidationDesired.YES); \n\n\t// the two first below implementations \"YanQi\" and \"Bsmock\" are available from OSSRH (\"Maven Central\")\n\tPathFinderFactory pathFinderFactory = new PathFinderFactoryYanQi(); // available from \"Maven Central\"\n\t// or: pathFinderFactory = new PathFinderFactoryBsmock(); // available from \"Maven Central\"\n\t// or: pathFinderFactory = new PathFinderFactoryJgrapht();  // currently NOT available from \"Maven Central\" !\n\t// or: pathFinderFactory = new PathFinderFactoryReneArgento(); // currently NOT available from \"Maven Central\" !\n\t// or: pathFinderFactory = new PathFinderFactoryMulavito(); // currently NOT available from \"Maven Central\" !\n\t// or: pathFinderFactory = new PathFinderFactoryJythonNetworkx(); // NOT available from \"Maven Central\" ! and it requires Jython and the Python pip package networkx\n\t// (currently there are six implementations)\n\n\tPathFinder pathFinder = pathFinderFactory.createPathFinder(graph);\n\tList\u003cPath\u003e shortestPaths = pathFinder.findShortestPaths(a, d, 10); // last parameter is max number to return but in this case there are only 3 possible paths\n\tfor (Path path : shortestPaths) {\n\t\tWeight totalWeightForPath = path.getTotalWeightForPath();\n\t\tSystem.out.println(totalWeightForPath);\n\t\tList\u003cEdge\u003e pathEdges = path.getEdgesForPath();\n\t\tfor (Edge edge : pathEdges) {\n\t\t\tVertex startVertex = edge.getStartVertex();\n\t\t\tVertex endVertex = edge.getEndVertex();\n\t\t\tWeight edgeWeight = edge.getEdgeWeight();\t\t\t\t\t\n\t\t\tSystem.out.println(startVertex);\n\t\t\tSystem.out.println(endVertex);\n\t\t\tSystem.out.println(edgeWeight);\n\t\t}\t\t\t\n\t}\n}\n```\nAssuming you are using Maven, to be able to use the above code, you can use the following configuration in your \"pom.xml\" file :\n```xml\n\u003crepositories\u003e\n\t...\n\t\u003crepository\u003e\n\t\t\u003c!-- \n\t\tadd this jitpack.io repository as below if you want to use some of the libraries not currently \n\t\tavailable from \"Maven Central\" i.e. if you want to use some other library than \"YanQi\" and \"Bsmock\" \n\t\twhich are the only two currently implementation libraries available att OSSRH (\"Maven Central\")\n\t\t--\u003e\t\n\t\t\u003cid\u003ejitpack.io\u003c/id\u003e\n\t\t\u003curl\u003ehttps://jitpack.io\u003c/url\u003e\n\t\u003c/repository\u003e\n\u003c/repositories\u003e\n\t\n\u003cdependencies\u003e\n    ...\n\t\u003c!--\n\t\tYou can use the below dependency if you want to use some implementation library not \n\t\tavailable at OSSRH (\"Maven Central\") i.e. if you want to use the above repository jitpack.io  \n\t\tNote that if you use this dependency below \n\t\t(e.g. because you want to use the \"reneargento\" implementation since it seems to be the fastest)\n\t\tthen you are probably restricted to use it only in a context not violating the license GPLv3.\n\t\tFurther information about the licensing issue:\n\t\thttps://github.com/TomasJohansson/adapters-shortest-paths/tree/master/adapters-shortest-paths-impl-reneargento\n\t--\u003e\n\t\u003cdependency\u003e\n\t\t\u003cgroupId\u003ecom.github.TomasJohansson\u003c/groupId\u003e\n\t\t\u003cartifactId\u003eadapters-shortest-paths\u003c/artifactId\u003e\n\t\t\u003cversion\u003efdb3ad991e7157667dcee3efe3200106fe9cd584\u003c/version\u003e \u003c!--https://github.com/TomasJohansson/adapters-shortest-paths/commits/master  --\u003e\n\t\u003c/dependency\u003e\n\t\u003c!--\n\t    An ALTERNATIVE to the above dependency (and then you do neither have to add jitpack.io as above\n\t    is to use one or both of the below dependencies which \n\t    are the ONLY TWO implementation libraries CURRENTLY deployed to OSSRH (\"Maven Central\")    \n    --\u003e\n    \u003cdependency\u003e\n        \u003c!-- Apache Software License, Version 2.0 --\u003e\n        \u003cgroupId\u003ecom.programmerare.shortest-paths\u003c/groupId\u003e\n        \u003cartifactId\u003eadapters-shortest-paths-impl-yanqi\u003c/artifactId\u003e\n        \u003cversion\u003e1.0.0\u003c/version\u003e\n    \u003c/dependency\u003e      \t\n    \n    \u003cdependency\u003e\n        \u003c!-- Apache Software License, Version 2.0 --\u003e\n        \u003cgroupId\u003ecom.programmerare.shortest-paths\u003c/groupId\u003e\n        \u003cartifactId\u003eadapters-shortest-paths-impl-bsmock\u003c/artifactId\u003e\t\t\n        \u003cversion\u003e1.0.0\u003c/version\u003e\n    \u003c/dependency\u003e\n    \n    \u003c!-- the \"core\" library are also deployed to OSSRH but it should be automatically retrieved when some of the above two dependencies are used --\u003e      \t\t\t          \n\u003c/dependencies\u003e\n```\n\n### Gradle or SBT dependencies\nIf you are not using Maven but instead for example Gradle or SBT, then you can find the dependencies at \nmvnrepository.com for [core](https://mvnrepository.com/artifact/com.programmerare.shortest-paths/adapters-shortest-paths-core) and \n[yanqi](https://mvnrepository.com/artifact/com.programmerare.shortest-paths/adapters-shortest-paths-impl-yanqi)\nand [bsmock](https://mvnrepository.com/artifact/com.programmerare.shortest-paths/adapters-shortest-paths-impl-bsmock). \n\n\n### Java version\nIf you are using Java 8+, then you should be able to use all Adapters, but if you use Java 6 or Java 7 then you are more limited regarding which of the Adapters to use.\n\nJava **6** is currently used for compiling the the core library itself, including the Adapter implementations.\n\nHowever, two of the Adaptee libraries are compiled for Java **8**.\n( [JGraphT](https://github.com/jgrapht/jgrapht/blob/master/pom.xml) and [the fork of \"reneargento/algorithms-sedgewick-wayne\"](https://github.com/TomasJohansson/algorithms-sedgewick-wayne) )  \n\nOne of the Adapt**ee** libraries are compiled for Java **7**.\n( [MuLaViTo](https://github.com/TomasJohansson/MuLaViTo-fork/blob/45f161312b5e9ec50e2d430d17e8d4a395ce82be/pom.xml) (i.e. the fork of \u003chttps://sourceforge.net/p/mulavito/\u003e)\n\nThe following two Adapt**ee** libraries can be compiled for Java **6** :  \n[The fork of \"bsmock/k-shortest-paths\"](https://github.com/TomasJohansson/k-shortest-paths) and [The fork of \"yan-qi/k-shortest-paths-java-version\"](https://github.com/TomasJohansson/k-shortest-paths-java-version)  \n\nThe adapter library \"adapters-shortest-paths-impl-jython_networkx\" can be compiled with Java 6 but \nit has a difference kind of dependency than the others. See below in the next section. \n\n### Some comments about the six adaptee libraries currently being used\n\nThere are currently Adapter implementations for the following six libraries:\n* \u003chttps://github.com/jgrapht/jgrapht\u003e\n* \u003chttps://github.com/yan-qi/k-shortest-paths-java-version\u003e\n* \u003chttps://github.com/bsmock/k-shortest-paths\u003e\n* \u003chttps://github.com/reneargento/algorithms-sedgewick-wayne\u003e\n* \u003chttps://github.com/TomasJohansson/MuLaViTo-fork\u003e (fork based on \u003chttps://sourceforge.net/p/mulavito/\u003e)\n* [Jython](https://www.jython.org/) and the Python pip package [networkx](https://networkx.github.io/)\n\nRegarding the versions/\"releases\" of the above libraries:\n\n* Regarding jgrapht, the [version 1.4.0](https://github.com/jgrapht/jgrapht/releases/tag/1.4.0) is currently used (but **not** released to OSSRH as mentioned further up in this webpage)\n* Regarding the [\"yan-ki\"](https://github.com/yan-qi/k-shortest-paths-java-version) implementation, there seems to be no official releases. Also, I could not find a way of reusing the library without modification since it seems to [require input from a file](https://github.com/yan-qi/k-shortest-paths-java-version/issues/4) which would mean I could not have used it as intended, e.g. programmatically creating a big graph for comparison against other implementations. This was one of the reasons why I instead use a [forked version](https://github.com/TomasJohansson/k-shortest-paths-java-version/commits/programmatic-graph-creation-without-using-inputfile). Another reason for creating and using a fork was the limitation that the input vertices needs to be integer in a sequence, while the other libraries support general strings. I fixed this with a mapper class in which maps back and forth from more general input strings.          \n* Regarding the [\"bsmock\"](https://github.com/bsmock/k-shortest-paths) implementation, it was not even a maven project. Therefore I [forked](https://github.com/TomasJohansson/k-shortest-paths/commits/adding-maven-structure-and-junit-test) it and created a maven project of it. I have created a [pull request with my changes](https://github.com/bsmock/k-shortest-paths/pull/2).\n* Regarding \"reneargento\", it was neither a maven project, which was the reason for forking it. It also included a jar file, but the fork is instead using maven and jitpack for defining the dependency in the pom file. Please [read about the license for that dependency](https://github.com/TomasJohansson/algorithms-sedgewick-wayne).\n* Regarding \"mulavito\", it was neither a maven project, which was one of the reason for forking it. It also included unnecessary (for the purpose of just wanting to use the shortest path algorithm) many third-party libraries which have been removed from a branch of the fork.  \n* Regarding \"Jython/Networkx\" it is different from the others.  Instead of using a dependency in Maven pom file for an external Java library, it uses a Python script within the Adapter library. It will only work if you have installed Jython and the pip package networkx (and you must have some jython environment variable defining the jython home directory). This is tested with Windows 10 and Jython 2.7.2 which is currently (october 2020) the latest Jython version. To make the code in this implementation find your Jython home directory with your file jython.jar, then you must have defined an environment variable containing the string \"jython\" (**not** case sensitive) for example \"jython.home\" or \"jython_home\" or simply \"jython\". In that Jython home directory there should be a subdirectory \"Lib/site-packages\" and there you should have installed 'networkx' with this command: \"jython -m pip install networkx\".  \n\t\n#### Some concepts in graph theory:\n\n(see the next section further down below regarding how these concepts are relevant when you want to find the shortest paths in travel routing)\n\n\"[Vertex](https://en.wikipedia.org/wiki/Vertex_(graph_theory))\" = A point or a node in a so called 'Graph'. 'Point' or 'Node' are alternative words sometimes used instead of 'Vertex' . (the plural form of the word 'vertex' is 'vertices')\n \n\"[Edge](https://en.wikipedia.org/wiki/Edge_(graph_theory))\" = Connection between two vertices in a 'Graph'. 'Arc' or 'Line' are alternative words sometimes used instead of 'Edge'. There can also be a direction and/or a weight associated with an edge.\n\n\"[Graph](https://en.wikipedia.org/wiki/Graph_(discrete_mathematics))\" = Collection of edges (and thus of course the collection also includes vertices since there are normally two vertices in an edge, unless the edge is a loop with the same vertex in both ends of the edge).\n \n\"[Weight](https://en.wikipedia.org/wiki/Glossary_of_graph_theory_terms#weight)\" = Some kind of 'cost' associated with an edge. It can be thought of as the 'cost' of going from the vertex in one end to the vertex in the other end of the edge. Time or distance are examples of weights/costs. When trying to find the shortest path, allowing negative weights tend to make the problem more complicated. The naive approach is that you can simply adjust all weights with a constant big enough to make all weights positive, but then [please look at a counter-example proving it is not that easy](https://www.quora.com/Can-you-add-a-large-constant-to-a-weighted-graph-with-negative-edges-so-that-all-the-weights-become-positive-then-run-Dijkstras-algorithm-starting-at-node-S-and-return-to-the-shortest-path-found-to-node-T-in-order-to-still-get-the-shortest-path). \n\n\"[Path](https://en.wikipedia.org/wiki/Path_(graph_theory))\" = Sequence of one or more edges leading from some start vertex to some end vertex. For example, if you have one edge from vertex A to vertex B, and one edge from vertex B to vertex C, then you would have one possible path from A to C by combining those two edges (A to B, and B to C) as a path.\n\n\"[Direction](https://en.wikipedia.org/wiki/Directed_graph)\" of an edge means that one of the two vertices for the edge is the \"start vertex\" and the other is the \"end vertex\".  \nWhen the graph is illustrated in a picture you will normally see an arrow illustrating the direction.\n\n\"[Loop](https://en.wikipedia.org/wiki/Loop_(graph_theory))\" = Edge connecting a vertex with itself, i.e. the same vertex in both ends of the edge.\n\n\"[Cycle](https://en.wikipedia.org/wiki/Cycle_(graph_theory))\" = Path where you reach the same vertex again i.e. more than once within the path.\n\n\"Vertex-[disjoint](https://en.wikipedia.org/wiki/Glossary_of_graph_theory_terms#disjoint)\" paths means that the paths do not have any vertex in common. Vertex-independent is another word for 'Vertex-disjoint'.\n\n\"Edge-disjoint\" paths means that the paths do not have any edge in common. Edge-independent is another word for 'Edge-disjoint'.  \n\n\"[Multi-edge](https://en.wikipedia.org/wiki/Multiple_edges)\" means that there are more than one edge connecting the same two vertices (in the same direction if the edges in the graph are directed). 'Parallel edges' or 'Multiple edges' are alternative phrases for 'Multi-edge'. \n\n### Some words about how graph theory relates to finding the shortest paths when implementing travel routing\n\n\"Vertex\": You can think of a 'vertex' as a specific geographic location/point which is either an end of the road (dead end/return path) or a place where you have an option about where to go next when you reach that point e.g. at a cross-road.\n\n\"Edge\": You can think of an 'edge' as a road section between to geographic points (\"Vertex\" above).\n\n\"Graph\": You can think of a 'graph' as the road network.\n\n\"Weight\": You can think of an 'edge weight' as the time or distance for traveling through a road section ('edge' above) . When searching for the shortest paths (i.e. paths with minimal total weights) from one location to another, you may want to minimize the total travel time (although distance can also be an interesting kind of weight). Note that the weight does not necessarily have to be  equal for both directions. For example, if you have defined a graph with walking times between different points when the road section is not flat, then the weight (time) will be larger when you walk steep up compared to walking steep down. The algorithm does **not** need to be able to handle negative weights since time and distance are concepts with positive values.     \n\n\"Path\": Think of different alternative paths (i.e. paths with the same start vertex and end vertex) as alternative routes. If you have used a GPS (or have made some search with google map regarding traveling between two points) then you have probably seen that you sometimes will see a couple of suggested alternative routes/paths. \n\n\"Direction\": Often, but not always (but almost always regarding walking) the roads are bidirectional, i.e. you can walk or go by car in both directions. However, as you know, sometimes you can go by car in only one direction.\n\n\"Loops\" and \"cycles\": Of course, you never want to waste time with loops in travel routing, going back to the same place again during the trip. You only want to find meaningful alternative paths, and those should **not** include any loops nor cycles. This is relevant to consider in graph theory. For example, there is an algorithm named \"Eppstein\" which can find the shortest paths but it does not exclude cycles from the result paths, and therefore is not an appropriate algorithm for travel routing.          \n\n\"Vertex-disjoint\" and \"Edge-disjoint\": It is okay if the alternative routes/paths are **partially** the same, i.e. passing trough the same points or road sections. In other words, when an algorithm finds alternative routes/paths, they do **not** have to be disjoint.   \n\n\"Multi-edge\": The algorithm does **not** need to suport multi-edged paths. It would not make sense to choose some other strategy than simply choosing the edge with the smallest weight (time or distance) between two points.\nOf course, if you consider rush hours, it is indeed very common with different travel times between two points, but to handle those situations you can define alternative graphs, i.e. edges with different weights for different times of the day. \n  \n\nOne interesting algorithm for \"[k shortest paths](https://en.wikipedia.org/wiki/K_shortest_path_routing)\" is [Yen's algorithm](https://en.wikipedia.org/wiki/Yen%27s_algorithm).\nAs mentioned above, Eppstein is not as interesting since it can include cycles.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasjohansson%2Fadapters-shortest-paths","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomasjohansson%2Fadapters-shortest-paths","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasjohansson%2Fadapters-shortest-paths/lists"}