Last active
May 6, 2023 15:45
-
-
Save gaeng2y/c1bb20a2e18c132882b78c75c35b5ad0 to your computer and use it in GitHub Desktop.
Graph protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum EdgeType { | |
case directed | |
case undirected | |
} | |
public protocol Graph { | |
associatedtype Element | |
func createVertex(data: Element) -> Vertex<Element> | |
func addDirectedEdge(from source: Vertex<Element>, | |
to destination: Vertex<Element>, | |
weight: Double?) | |
func addUndirectedEdge(between source: Vertex<Element>, | |
and destination: Vertex<Element>, | |
weight: Double?) | |
func add(_ edge: EdgeType, from source: Vertex<Element>, | |
to destination: Vertex<Element>, | |
weight: Double?) | |
func edges(from source: Vertex<Element>) -> [Edge<Element>] | |
func weight(from source: Vertex<Element>, | |
to destination: Vertex<Element>) -> Double? | |
} | |
public struct Vertex<T> { | |
public let index: Int | |
public let data: T | |
} | |
extension Vertex: Hashable where T: Hashable {} | |
extension Vertex: Equatable where T: Equatable {} | |
public struct Edge<T> { | |
public let source: Vertex<T> | |
public let destination: Vertex<T> | |
public let weight: Double? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment