Skip to content

Instantly share code, notes, and snippets.

@gaeng2y
Last active May 6, 2023 15:45
Show Gist options
  • Save gaeng2y/c1bb20a2e18c132882b78c75c35b5ad0 to your computer and use it in GitHub Desktop.
Save gaeng2y/c1bb20a2e18c132882b78c75c35b5ad0 to your computer and use it in GitHub Desktop.
Graph protocol
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