Skip to content

Instantly share code, notes, and snippets.

@paralax
Created December 31, 2016 03:05
Show Gist options
  • Save paralax/70086ad46077e5d09ec60b7d369b4f20 to your computer and use it in GitHub Desktop.
Save paralax/70086ad46077e5d09ec60b7d369b4f20 to your computer and use it in GitHub Desktop.
implementation of basic algebra for graphs
// 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