{"id":18568007,"url":"https://github.com/willybrauner/dijkstra-algorithm","last_synced_at":"2025-11-04T11:02:50.736Z","repository":{"id":41251263,"uuid":"507321093","full_name":"willybrauner/dijkstra-algorithm","owner":"willybrauner","description":"Dijkstra's algorithm implementation for any data structure","archived":false,"fork":false,"pushed_at":"2022-06-29T20:46:22.000Z","size":6211,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-11-04T11:02:36.380Z","etag":null,"topics":["algorithm","dijkstra","dijkstra-algorithm"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/willybrauner.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-25T13:40:38.000Z","updated_at":"2023-09-17T21:12:16.000Z","dependencies_parsed_at":"2022-09-20T23:03:23.111Z","dependency_job_id":null,"html_url":"https://github.com/willybrauner/dijkstra-algorithm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/willybrauner/dijkstra-algorithm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willybrauner%2Fdijkstra-algorithm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willybrauner%2Fdijkstra-algorithm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willybrauner%2Fdijkstra-algorithm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willybrauner%2Fdijkstra-algorithm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willybrauner","download_url":"https://codeload.github.com/willybrauner/dijkstra-algorithm/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willybrauner%2Fdijkstra-algorithm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":282624451,"owners_count":26699981,"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-11-04T02:00:05.887Z","response_time":62,"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","dijkstra","dijkstra-algorithm"],"created_at":"2024-11-06T22:28:24.867Z","updated_at":"2025-11-04T11:02:50.720Z","avatar_url":"https://github.com/willybrauner.png","language":"TypeScript","readme":"# Dijkstra's algorithm\n\nThis repos contains an implementation of [dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) that can be used with any data structure.\n\n\u003cimg alt=\"demo\" src=\"dijkstra.gif\" /\u003e\n\n\u003e Dijkstra's algorithm is a variant of the [BFS algorithm](https://en.wikipedia.org/wiki/Breadth-first_search) that finds the shortest path between two nodes in a graph.\n\n## Motivation\n\nI discovered the Dijkstra's algorithm when I was working on [advent-of-code 2021 day 15](https://adventofcode.com/2021/day/15).\nEach example of the algorithm implementation was very specific to the data structure of the current use case, so I tried to write a generic and flexible implementation with the help of [Paul Brauner](https://github.com/polux).\n\n[Dijkstra's function](./src/dijkstra.ts) can accept different data structure as input:\n(example of inputs used in [unit tests](./src/dijkstra.test.ts))\n\n1. On this 2D matrix (from advent-of-code example), each vertex is a vec2 coordinate `(y, x)` when the cost of the path is number of the cooridnate.\n\n```ts\nconst matrix = [\n  [1, 1, 6, 3, 7, 5, 1, 7, 4, 2],\n  [1, 3, 8, 1, 3, 7, 3, 6, 7, 2],\n  [2, 1, 3, 6, 5, 1, 1, 3, 2, 8],\n  [3, 6, 9, 4, 9, 3, 1, 5, 6, 9],\n  [7, 4, 6, 3, 4, 1, 7, 1, 1, 1],\n  [1, 3, 1, 9, 1, 2, 8, 1, 3, 7],\n  [1, 3, 5, 9, 9, 1, 2, 4, 2, 1],\n  [3, 1, 2, 5, 4, 2, 1, 6, 3, 9],\n  [1, 2, 9, 3, 1, 3, 8, 5, 2, 1],\n  [2, 3, 1, 1, 9, 4, 4, 5, 8, 1],\n]\n```\n\n2. This graph can be used too when object keys are verticies and values are neighbors associated to there own cost.\n\n```ts\nconst graph = {\n  start: { A: 5, B: 2 },\n  A: { start: 1, C: 4, D: 2 },\n  B: { A: 8, D: 7 },\n  C: { D: 6, finish: 3 },\n  D: { finish: 1 },\n  finish: {},\n}\n```\n\n## Usage\n\nIn order to accept different data structures, `dijkstra` recieves some functions as params:\n\n- `getNeighbors: (v: GVertex) =\u003e GVertex[]` returns array of vertices that are neighbors of `v`\n- `getCostBetweenVertices: (a: GVertex, b: GVertex) =\u003e number` returns cost of edge between thwo vertices\n- `source: GVertex` vertex from which algorithm starts\n- `isTarget: (vertex: GVertex) =\u003e boolean` returns `true` if vertex is the target\n- `queue = priorityQueueMinHeap\u003cGVertex\u003e()` queue that is used to store vertices in order of their distance from source\n\nexemple of usage:\n\n```ts\nconst getNeighbors = (vertex) =\u003e Object.keys(graph[vertex])\nconst distanceBetweenTwoVertices = (v1, v2) =\u003e graph[v1][v2]\nconst isTarget = (vertex) =\u003e vertex === \"finish\"\n\nconst shortestPathCost: number = dijkstra\u003cstring\u003e(\n  getNeighbors,\n  distanceBetweenTwoVertices,\n  \"start\",\n  isTarget\n)\n```\n\n`dijkstra` returns the shortest path cost from source to target\n\n## Priority Queue\n\nThis algorithm is using a [priority queue](https://en.wikipedia.org/wiki/Priority_queue) to store vertices in order of their distance from source. The last one is a big part of Dijkstra implementation if we want to use it with big data structures.\n\nTwo types of priority queue have been tested in this algorithm:\n\n- [priority Queue min](./src/priorityQueueMin.ts) (`O(n)`complexity)\n- [priority Queue min with binary heap](./src/priorityQueueMinHeap.ts) (`O(log n)`complexity)\n\nThe second one will be more efficient because binary heap doesn't require us to iterate over the list.\n\n## Requirement\n\n- node js `\u003e= 16`\n- npm `\u003e= 8`\n\n## test it\n\n- Clone the repos\n\n```shell\n$ git clone git@github.com:willybrauner/dijkstra-algorithm.git\n```\n\n- Install dépendencies\n\n```shell\n$ npm i\n```\n\n- Start tests once\n\n```shell\n$ npm run test\n```\n\n- Start tests in watch mode\n\n```shell\n$ npm run test:watch\n```\n\n## Credits\n\n© Willy Brauner \u0026 Paul Brauner\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillybrauner%2Fdijkstra-algorithm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillybrauner%2Fdijkstra-algorithm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillybrauner%2Fdijkstra-algorithm/lists"}