https://github.com/username0x0a/swiftydijkstra
Simple objective implementation of Dijkstra algorithm (shortest path search) written in Swift
https://github.com/username0x0a/swiftydijkstra
dijkstra dijkstra-algorithm dijkstra-swift shortest-path-problem shortest-paths swift
Last synced: 8 months ago
JSON representation
Simple objective implementation of Dijkstra algorithm (shortest path search) written in Swift
- Host: GitHub
- URL: https://github.com/username0x0a/swiftydijkstra
- Owner: username0x0a
- License: mit
- Created: 2017-02-12T18:20:36.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2020-04-25T22:34:29.000Z (over 5 years ago)
- Last Synced: 2025-04-20T10:39:59.291Z (8 months ago)
- Topics: dijkstra, dijkstra-algorithm, dijkstra-swift, shortest-path-problem, shortest-paths, swift
- Language: Swift
- Size: 40 KB
- Stars: 4
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SwiftyDijkstra
Example _Dijkstra_ algorithm (shortest path problem) implementation written in Swift.
## Usage

```swift
import Foundation
import SwiftyDijkstra
// Define the nodes
let Brno = Node("Brno")
let Praha = Node("Praha")
let Ostrava = Node("Ostrava")
let Pardubice = Node("Pardubice")
let Kolin = Node("Kolin")
let Plzen = Node("Plzen")
// Define the graph edges
var edges = [
Edge(from: Ostrava, to: Brno, cost: 180),
Edge(from: Praha, to: Ostrava, cost: 420),
Edge(from: Pardubice, to: Brno, cost: 110),
Edge(from: Pardubice, to: Praha, cost: 90),
Edge(from: Pardubice, to: Plzen, cost: 140),
Edge(from: Pardubice, to: Ostrava, cost: 290),
Edge(from: Plzen, to: Praha, cost: 70),
]
// Initialize the graph itself
var G = Graph(edges: edges)
// Look for shortest paths and print'em
print(G.shortestPath(from: Praha, to: Ostrava))
// outputs [Praha, Pardubice, Ostrava]
print(G.shortestPath(from: Ostrava, to: Plzen))
// outputs [Ostrava, Pardubice, Plzen]
print(G.shortestPath(from: Ostrava, to: Ostrava))
// outputs [Ostrava]
print(G.shortestPath(from: Brno, to: Kolin))
// outputs []
```
## License
Dijkstra example is released under the MIT license. See the LICENSE file for details.