{"id":20409712,"url":"https://github.com/sleekpanther/bellman-ford","last_synced_at":"2025-10-12T19:05:29.508Z","repository":{"id":115108603,"uuid":"92628347","full_name":"SleekPanther/bellman-ford","owner":"SleekPanther","description":"Shortest Paths from every vertex to a goal vertex allowing negative-weight edges (when Dijkstra's fails)","archived":false,"fork":false,"pushed_at":"2017-05-30T10:37:43.000Z","size":840,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-12T19:03:16.457Z","etag":null,"topics":["algorithm","algorithms","bellman-ford","bellman-ford-algorithm","cycle","dijkstra","dynamic-programming","edge","graph","graphs","memoization","negative-weight","noah","noah-patullo","pattullo","pattulo","patullo","patulo","shortest-paths"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SleekPanther.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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-05-27T22:52:59.000Z","updated_at":"2025-06-15T18:53:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"13c48190-c3e5-4814-aa55-2e9643c0f535","html_url":"https://github.com/SleekPanther/bellman-ford","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SleekPanther/bellman-ford","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleekPanther%2Fbellman-ford","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleekPanther%2Fbellman-ford/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleekPanther%2Fbellman-ford/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleekPanther%2Fbellman-ford/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SleekPanther","download_url":"https://codeload.github.com/SleekPanther/bellman-ford/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SleekPanther%2Fbellman-ford/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279012644,"owners_count":26085158,"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","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["algorithm","algorithms","bellman-ford","bellman-ford-algorithm","cycle","dijkstra","dynamic-programming","edge","graph","graphs","memoization","negative-weight","noah","noah-patullo","pattullo","pattulo","patullo","patulo","shortest-paths"],"created_at":"2024-11-15T05:43:08.919Z","updated_at":"2025-10-12T19:05:29.470Z","avatar_url":"https://github.com/SleekPanther.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Bellman Ford (Shortest Paths with Negative Weights)\nDynamic programming algorithm to find the shortest paths from every vertex to the goal vertex. Edges can have negative weights, but no negative-weight cycles (otherwise you could keep looping over a negative-weight cycle to get an infinitely negative number \u0026 shortest path would have no meaning). Dijkstra's fails for negative-wiehgt edges but Bellman-Ford can do it.\n\n## Optimal Substructure\n![](images/optimal-substructure.png)  \nThe shortest path between any 2 vertices uses at most **n-1** edges (**n**=number of vertices). Since there cannot be any negative-weight cycles, any more than **n-1** edges would only add to the total path cost so the algorithm stops when **n-1** edges have been considered.  \nBellman-Ford considers the shortest paths in increasing order of **number of edges used** starting from 0 edges (hence infinity for all but the goal node), then shortest paths using 1 edge, up to **n-1** edges.\n\n## Pseudocode\n![](images/pseudocode.png)\n\n## Input Graphs\n### Graph 1\n\u003cimg src=\"images/graph1.png\" width=\"350\"\u003e\n\n### Graph 2\n\u003cimg src=\"images/graph2.png\" width=\"310\"\u003e\n\n## Usage\n- `int[][][] graph` is an **adjacency list** for a weighted, directed graph\n  - `graph[0]` contains all edges **FROM** vertex `0`\n  - Each `graph[0][v]` is an edge as a 2D array containing `[destinationVertex, edgeWeight]`\n  - So for **Graph 1**, `graph[0]` is `{ {1, 6}, {2, 7} },` meaning there are 2 edges **FROM vertex `0`**:\n    1. (0 → 1) weight=6\n    2. (0 → 2) weight=7\n  - **Put an empty array `{}` in a row for a vertex that has no outgoing edges (*see graph 2 for example*)**\n- `String[] vertexNames` convert array index (e.g. 0, 1, 2, ...) to human-readable names  \nThe Dynamic Programming deals with vertices as `int`, but prints the final paths using this conversion array\n- `startingVertex` \u0026 a `goalVertex` as `int`. These indexes correspond to `vertexNames[]` and `graph[][][]`, but **S** and **T** don't have to be the 1st \u0026 last items in the arrays, they don't even have to be called **\"S\"** and **\"T\"**\n\n## Graph 1 Memoization Table\n\u003cimg src=\"images/graph1-memoization-table.png\" width=\"350\"\u003e\n\nFor Bellman-Ford, the last row is the most important. This final row holds cost of the shortest path from any vertex to the goal vertex using using at most **n-1** edges. (A value is **infinity** if it cannot be reached)  \nRetracing the actual path is done using the `successors[]`. Each entry in the memoization table also contains the next vertex in the path (**or `-1` if no path has been found**).  \nFollow the path by looking 1 row above and at the column indicated by the successor. Stop when the `goalVertex` is reached\n\n\n## Code Notes\n- In `findShortestPaths`, **`i` is an edge counter** representing how many edges can currently be included in the shortest path\n- `printTable()` displays **\"inf\"** for **\"infinity\"** because the underlying code uses `Integer.MAX_VALUE` and 10-digit numbers mess up the column alignment when printing\n- `Integer.MAX_VALUE` represents infinity, but that requires some extra code to make sure **overflow** doesn't occur.  \n`if(memoTable[i-1][destinationVertex] != Integer.MAX_VALUE)`  \nThis makes sure that the previous row's value is NOT infinity before adding the `weight` of the new edge, otherwise it would result in a hugely negative number\n- `possiblePathCosts` holds costs of all possible paths coming **FROM** the `sourceVertex` and also the `successor` where that path went. The minimum of all these paths 1 edge away from `sourceVertex` is chosen and the `successor` is also stored\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsleekpanther%2Fbellman-ford","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsleekpanther%2Fbellman-ford","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsleekpanther%2Fbellman-ford/lists"}