Basic functionality

In FSharp.FGL, the basic functions are separated into the Directed and the Undirected namespaces. Both undirected and directed graph functions use the same input type, but the information is read differently, depending on wether you call a function from the undirected or the directed namespace. Therefore it is advised to decide which kind of graph you work with beforehand. In this quick tutorial we will work with directed Graphs.

#r "FSharp.FGL.dll"

open FSharp.FGL
open FSharp.FGL.Directed

Creating a Graph

The Graph can be created by the "Graph.empty" function. The Graph can then be filled by the "Vertices.add"/"Vertices.addMany" and the "Edges.add"/"Edges.addMany" functions. Keep in mind that FSharp.FGL does not support unlabeled edges and unlabeled vertices

//Creating a list of labeled vertices
let vertexList : LVertex<int,string> list = List.init 4 (fun i -> 
        i + 1,
        sprintf "VertexNr. %i" (i + 1))

//Creating a list of labeled edges
let edgeList : LEdge<int,float> list = [(1,2,1.);(2,1,1.);(1,3,0.5);(3,4,0.8);(4,3,0.8)]

//Creating a graph out of the two lists can be done in different ways
let myGraph : Graph<int,string,float> = 
    Graph.create vertexList edgeList

let myGraph' : Graph<int,string,float> =
    Graph.empty
    |> Vertices.addMany vertexList
    |> Edges.addMany edgeList

The Graph.create function is located in the Directed and in the Undirected namespaces. Therefore calling it while having one of those opened will result in building the according graph type. Adding and Removing single vertices or edges can be done with the following functions:

let myChangedGraph : Graph<int,string,float> =
    myGraph
    |> Vertices.add (5,"VertexNr. 5")
    |> Edges.add (5,1,0.7)
    |> Edges.remove (2,1)

Iteration

coming soon

Multiple items
Namespace FSharp

--------------------
Namespace Microsoft.FSharp
Namespace FSharp.FGL
Namespace FSharp.FGL.Directed
val vertexList : LVertex<int,string> list
type LVertex<'Vertex,'Label> = 'Vertex * 'Label
Multiple items
val int : value:'T -> int (requires member op_Explicit)

--------------------
[<Struct>]
type int = int32

--------------------
type int<'Measure> =
  int
Multiple items
val string : value:'T -> string

--------------------
type string = System.String
type 'T list = List<'T>
Multiple items
Modul List

aus Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
    interface IReadOnlyList<'T>
    interface IReadOnlyCollection<'T>
    interface IEnumerable
    interface IEnumerable<'T>
    member GetReverseIndex : rank:int * offset:int -> int
    member GetSlice : startIndex:int option * endIndex:int option -> 'T list
    static member Cons : head:'T * tail:'T list -> 'T list
    member Head : 'T
    member IsEmpty : bool
    member Item : index:int -> 'T with get
    ...
val init : length:int -> initializer:(int -> 'T) -> 'T list
val i : int
val sprintf : format:Printf.StringFormat<'T> -> 'T
val edgeList : LEdge<int,float> list
type LEdge<'Vertex,'Edge> = 'Vertex * 'Vertex * 'Edge
Multiple items
val float : value:'T -> float (requires member op_Explicit)

--------------------
[<Struct>]
type float = System.Double

--------------------
type float<'Measure> =
  float
val myGraph : Graph<int,string,float>
Multiple items
Modul Graph

aus FSharp.FGL.Directed

--------------------
Modul Graph

aus FSharp.FGL

--------------------
type Graph<'Vertex,'Label,'Edge (requires comparison)> = Map<'Vertex,MContext<'Vertex,'Label,'Edge>>
val create : vertices:LVertex<'Vertex,'Label> list -> edges:LEdge<'Vertex,'Edge> list -> Graph<'Vertex,'Label,'Edge> (requires comparison)
val myGraph' : Graph<int,string,float>
val empty : Graph<'Vertex,'Label,'Edge> (requires comparison)
Multiple items
Modul Vertices

aus FSharp.FGL.Directed

--------------------
Modul Vertices

aus FSharp.FGL
val addMany : vertices:LVertex<'Vertex,'Label> list -> g:Graph<'Vertex,'Label,'Edge> -> Graph<'Vertex,'Label,'Edge> (requires comparison)
Modul Edges

aus FSharp.FGL.Directed
val addMany : edges:LEdge<'Vertex,'Edge> list -> g:Graph<'Vertex,'Label,'Edge> -> Graph<'Vertex,'Label,'Edge> (requires comparison)
val myChangedGraph : Graph<int,string,float>
val add : 'Vertex * 'Label -> g:Graph<'Vertex,'Label,'Edge> -> Graph<'Vertex,'Label,'Edge> (requires comparison)
val add : 'Vertex * 'Vertex * 'Edge -> g:Graph<'Vertex,'Label,'Edge> -> Graph<'Vertex,'Label,'Edge> (requires comparison)
val remove : 'Vertex * 'Vertex -> g:Graph<'Vertex,'Label,'Edge> -> Graph<'Vertex,'Label,'Edge> (requires comparison)