目录
目录README.md

SwiftyDijkstra

Example Dijkstra algorithm (shortest path problem) implementation written in Swift.

Usage

Example usage

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.

关于
70.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号