Created
December 31, 2016 03:05
-
-
Save paralax/70086ad46077e5d09ec60b7d369b4f20 to your computer and use it in GitHub Desktop.
implementation of basic algebra for graphs
This file contains hidden or 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
| // https://blogs.ncl.ac.uk/andreymokhov/an-algebra-of-graphs/ | |
| type Vertex = string | |
| type Graph(vertices:Set<Vertex>, edges:Set<(Vertex * Vertex)>) = | |
| member this.vertices = vertices | |
| member this.edges = edges | |
| member this.empty() = new Graph(Set.empty, Set.empty) | |
| member this.overlay(g:Graph) = new Graph(Set.union this.vertices g.vertices, Set.union this.edges g.edges) | |
| member this.connect(g:Graph) = | |
| let E = Set.union this.edges g.edges | |
| let EE = Set.map (fun x -> Set.map (fun y -> (x,y)) this.vertices) g.vertices | |
| |> Seq.concat | |
| |> Set.ofSeq | |
| new Graph(Set.union this.vertices g.vertices, Set.union E EE) | |
| static member (+) (g:Graph, this:Graph) = this.overlay g | |
| static member (*) (g:Graph, this:Graph) = this.connect g | |
| let g1 = new Graph(set ["a"; "b"; "c"], set [("a", "b"); ("a", "c")]) | |
| let g2 = new Graph(set ["c";"d";"e"], set [("c","d"); ("d","e")]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment