{"id":25045142,"url":"https://github.com/petruki/dijkstra-algorithm","last_synced_at":"2025-03-31T00:25:35.187Z","repository":{"id":102027867,"uuid":"350955554","full_name":"petruki/dijkstra-algorithm","owner":"petruki","description":"My implementation of Dijkstra algorithm","archived":false,"fork":false,"pushed_at":"2024-09-03T00:43:10.000Z","size":755,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-06T05:34:58.608Z","etag":null,"topics":["dijkstra-algorithm","java","java-streams"],"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/petruki.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-24T05:23:22.000Z","updated_at":"2024-09-03T00:41:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"450f1ce0-059c-4c39-b532-5f331fd81cbf","html_url":"https://github.com/petruki/dijkstra-algorithm","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petruki%2Fdijkstra-algorithm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petruki%2Fdijkstra-algorithm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petruki%2Fdijkstra-algorithm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petruki%2Fdijkstra-algorithm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petruki","download_url":"https://codeload.github.com/petruki/dijkstra-algorithm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246399096,"owners_count":20770856,"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":["dijkstra-algorithm","java","java-streams"],"created_at":"2025-02-06T05:32:39.465Z","updated_at":"2025-03-31T00:25:35.154Z","avatar_url":"https://github.com/petruki.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"### About\n\nFamous algrithm by Edsger W. Dijkstra.\n\nAlgorithm well explained here:\n[Graph Data Structure 4. Dijkstra’s Shortest Path Algorithm](https://www.youtube.com/watch?v=pVfj6mxhdMw)\n\nThis implementation offers a wide view of how the algorithm works.\u003cbr\u003e\nI have added a few features that can make more complex scenarios such as unidirectional vertices and density matrix for pathfinder simulation.\n\n### Features\n\n- Pre-calculate Dijkstra table based on the origin node.\n- Vertices can be set as single or bidirectional for more complex scenarios.\n- Nodes can bet set to ignored.\n- Density matrix for pathfinder simulations.\n\t- Import plaintext or images\n- Display result table.\n- Display summary with visited nodes and total cost.\n- Real-time screen capture, why not?\n\n#### Density Matrix Simulator App\n\n- Create and run Density Matrix simulations using the Desktop UI.\n\n### Running\n\n\u003e Scenario\n\n![Scenario 1](https://raw.githubusercontent.com/petruki/dijkstra-algorithm/master/docs/scenario1.jpg)\n\n\u003e Code\n\n```java\nimport static com.github.petruki.model.Vertex.get;\n\nList\u003cVertex\u003e vertices = Arrays.asList(\n\t/*\n\t * B to A is not allowed\n\t * only A -\u003e B\n\t */\n\tget(\"A\", \"B\", 2, false),\n\tget(\"A\", \"D\", 3),\n\tget(\"D\", \"B\", 1),\n\tget(\"D\", \"E\", 1),\n\tget(\"E\", \"B\", 2),\n\tget(\"E\", \"C\", 2),\n\tget(\"B\", \"H\", 3),\n\tget(\"C\", \"J\", 7),\n\tget(\"C\", \"G\", 5),\n\tget(\"G\", \"H\", 3),\n\tget(\"G\", \"J\", 2),\n\t\n\t/*\n\t * J to H is not allowed\n\t * only H -\u003e J\n\t */\n\tget(\"H\", \"J\", 3, false)\n);\n\nDijkstra dijkstra = new Dijkstra(vertices);\ndijkstra.generateTable(\"A\");\nDijkstraResult result = dijkstra.getShortestPath(\"A\");\n\nresult.pritnResult(true);\n```\n\n\u003e Displays\n\n```\nJ -\u003e A: [J, G, H, B, D, A] - cost: 12.0\nvertex=A, prev=D, distance=12\nvertex=B, prev=H, distance=8\nvertex=C, prev=J, distance=7\nvertex=D, prev=B, distance=9\nvertex=E, prev=C, distance=9\nvertex=G, prev=J, distance=2\nvertex=H, prev=G, distance=5\nvertex=J, prev= , distance=0\n```\n\n### Running: Generated Density Matrix\n\nA density matrix consists of a given matrix that contains 'n' nodes connected to all surrounding neighbors.\nThe cost for both horizontal and vertical vertices can be different from diagonal vertices.\n\nTo make the pathfinder more interesting, it is possible to exclude nodes from the matrix.\n\n```java\n// Generates a 10x10 Density Matrix with 1un. as horizontal/vertical cost and ignored diagonal trip\nDensityMatrix densityMatrix = DijkstraUtils.generateDensityMatrix(10, 10, 1f, -1f);\ndensityMatrix.setStartNode(\"0\");\ndensityMatrix.setEndNode(\"97\");\ndensityMatrix.setIgnored(Arrays.asList(\n\t\t\"96\", \"87\", \"88\", \"86\", \"77\", \"67\", \"69\",\n\t\t\"57\", \"45\", \"35\", \"25\", \"14\", \"4\", \"44\",\n\t\t\"54\", \"24\", \"53\", \"62\", \"52\", \"61\", \"71\",\n\t\t\"40\", \"31\", \"30\", \"32\", \"22\", \"11\"\n\t\t));\n\n// Build Dijkstra table\nDijkstra dijkstra = new Dijkstra(densityMatrix.getVertices());\ndijkstra.generateTable(densityMatrix);\n\n// Find shortest path\nDijkstraResult result = dijkstra.getShortestPath(densityMatrix.getEndNode());\n\n// Print results\nresult.printResult(false);\nDijkstraUtils.printResultDensityMatrix(result, densityMatrix, true);\n```\n\n##### **Output**\n\n- [NodeId] = shortest path found\n- X = ignored NodeIds \n\n```\n0 -\u003e 97: [0, 1, 2, 12, 13, 23, 33, 43, 42, 41, 51, 50, 60, 70, 80, 81, 82, 72, 73, 63, 64, 65, 55, 56, 46, 47, 48, 58, 68, 78, 79, 89, 99, 98, 97] - cost: 34.0\n   [0]   [1]   [2]     3     +     5     6     7     8     9\n    10     +  [12]  [13]     +    15    16    17    18    19\n    20    21     +  [23]     +     +    26    27    28    29\n     +     +     +  [33]    34     +    36    37    38    39\n     +  [41]  [42]  [43]     +     +  [46]  [47]  [48]    49\n  [50]  [51]     +     +     +  [55]  [56]     +  [58]    59\n  [60]     +     +  [63]  [64]  [65]    66     +  [68]     +\n  [70]     +  [72]  [73]    74    75    76     +  [78]  [79]\n  [80]  [81]  [82]    83    84    85     +     +     +  [89]\n    90    91    92    93    94    95     +  [97]  [98]  [99]\n```\n\nThe last parameter of 'DijkstraUtils.printResultDensityMatrix' can print the matrix in a prettier way, which is interesting for maze simulations.\n\n```\n[O][O][O]     +               \n     +[O][O]  +               \n        +[O]  +  +            \n  +  +  +[O]     +            \n  +[O][O][O]  +  +[O][O][O]   \n[O][O]  +  +  +[O][O]  +[O]   \n[O]  +  +[O][O][O]     +[O]  +\n[O]  +[O][O]           +[O][O]\n[O][O][O]           +  +  +[O]\n                    +[O][O][O]\n```\n\n### Running: Input Density Matrix\n\nIt is also possible to give a plain text input that will be converted internally to the Density Matrix.\n\nThe following example shows a maze type matrix, where the characters define its strucuture:\n\n- 'x': ignored/wall\n- ' ': travelable nodes\n- 's': starting node\n- 'e': ending node\n\n```java\nString[] maze = {\n\t\"+++++++++++++++\",\n\t\"+s+       + +e+\",\n\t\"+ +++++ +++ + +\",\n\t\"+ + +       + +\",\n\t\"+ +   +++ + + +\",\n\t\"+ + + +   + + +\",\n\t\"+   + +   + + +\",\n\t\"+++++ +   + + +\",\n\t\"+     +   +   +\",\n\t\"+++++++++++++++\"\n};\n```\n\nTo code below shows how to use this strucuture as input for the Density Matrix.\n\n```java\n// Generates a Density Matrix based on a plain text input\nDensityMatrix densityMatrix = DijkstraUtils.generateDensityMatrix(maze, 1f, -1f);\n\n// Build Dijkstra table\nDijkstra dijkstra = new Dijkstra(densityMatrix.getVertices());\ndijkstra.generateTable(densityMatrix);\n\n// Find shortest path\nDijkstraResult result = dijkstra.getShortestPath(densityMatrix.getEndNode());\n\n// Print results\nDijkstraUtils.printResultDensityMatrix(result, densityMatrix, true);\n\n```\n\n##### **Output**\n\n```\n  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +\n  +[O]  +                       +     +[O]  +\n  +[O]  +  +  +  +  +     +  +  +     +[O]  +\n  +[O]  +     +[O][O][O][O][O][O][O]  +[O]  +\n  +[O]  +[O][O][O]  +  +  +     +[O]  +[O]  +\n  +[O]  +[O]  +     +           +[O]  +[O]  +\n  +[O][O][O]  +     +           +[O]  +[O]  +\n  +  +  +  +  +     +           +[O]  +[O]  +\n  +                 +           +[O][O][O]  +\n  +  +  +  +  +  +  +  +  +  +  +  +  +  +  +\n\n```\n\n\n#### **Density Matrix App**\n\nCreate simulations based on Density Matrix scenarios or open the sampe included in this project located at: savedwork/demo1.dmf\n\n![Density Matrix Sample](https://raw.githubusercontent.com/petruki/dijkstra-algorithm/master/docs/density_matrix_app.jpg)\n\n#### **Density Matrix App: Importing images**\n\nTry using this simple built-in image processor that can import data from external image sources.\n\n\u003e Image Sample\n\n![Image Sample](https://raw.githubusercontent.com/petruki/dijkstra-algorithm/master/sample/maze.png)\n\n\u003e Output\n\n![Density Matrix Sample](https://raw.githubusercontent.com/petruki/dijkstra-algorithm/master/docs/density_matrix_app_image_import.jpg)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetruki%2Fdijkstra-algorithm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetruki%2Fdijkstra-algorithm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetruki%2Fdijkstra-algorithm/lists"}