The graph creation in ArrayAdjacencyGraph can be achived via two differnet approaches.
The easiest method is relies on a vertex list and an edge list like this :
#r "FSharp.FGL"
#r "FSharp.FGL.ArrayAdjacencyGraph"
open FSharp.FGL
open FSharp.FGL.ArrayAdjacencyGraph
//Creating a list of labeled vertices
let vertexList : LVertex<int,string> list =
List.init 4 (fun i ->
i,
sprintf "VertexNr. %i" i)
//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
let myGraph : ArrayAdjacencyGraph<int,string,float> =
ArrayAdjacencyGraph.Graph.create vertexList edgeList
The object oriented approach for this would look like this:
//Creating a graph out of the two lists
let myGraph' : ArrayAdjacencyGraph<int,string,float> =
ArrayAdjacencyGraph(vertexList,edgeList)
Alternatively, an empty graph can be created and filled with vertices and edges after its creation.
//Creating an array of the labeled vertices
let vertexArray : LVertex<int,string> [] =
vertexList
|> Array.ofList
//Creating an array of the labeled edges
let edgeArray : LEdge<int,float> [] =
edgeList
|> Array.ofList
//Creating an empty graph and filling it afterwards
let myGraph'' : ArrayAdjacencyGraph<int,string,float> =
ArrayAdjacencyGraph.Graph.create [] []
|> Vertices.addMany vertexArray
|> Edges.addMany edgeArray
Again, the object oriented approach would look like this:
//Creating an empty graph and filling it afterwards
let myGraph''' : ArrayAdjacencyGraph<int,string,float> =
//Create a new, empty graph
let emptyGraph = ArrayAdjacencyGraph()
//Add an array of vertices with the AddManyVertices method
emptyGraph.AddManyVertices(vertexArray)
//Add an array of edges with the AddManyEdges method
emptyGraph.AddManyEdges(edgeArray)
//Return the graph
emptyGraph
The AddManyVertices and AddManyEdges, as well as their single counterparts AddVertex and AddEdge can be used at all times, allowing an easy modification of the graph.
Adding a vertex and an edge works like this:
//New Vertex
let newVertex : LVertex<int,string> =
123,"123"
//Edge to new Vertex
let newEdge : LEdge<int,float> =
1,123,10.
//Add newVertex to myGraph
myGraph
|> Vertices.add newVertex
//Add newEdge to myGraph
myGraph
|> Edges.add newEdge
Or alternatively:
//Add newVertex to myGraph
myGraph'.AddVertex(newVertex)
//Add newEdge to myGraph
myGraph'.AddEdge(newEdge)
Removing vertices and edges is also very easy. If you remove a vertex from the Graph all edges featuring this vertex are also removed. The removal of edges themself is simple as well.
//Remove an edge from myGraph
myGraph
|> Edges.remove(1,2,1.)
//Remove a vertex from myGraph
myGraph
|> Vertices.remove(123)
Or respectively :
//Remove an edge from myGraph
myGraph'.RemoveEdge(1,2,1.)
//Remove a vertex from myGraph
myGraph'.RemoveVertex(123)
Multiple items
Namespace FSharp
--------------------
Namespace Microsoft.FSharp
Namespace FSharp.FGL
Namespace FSharp.FGL.ArrayAdjacencyGraph
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 : ArrayAdjacencyGraph<int,string,float>
Multiple items
Namespace FSharp.FGL.ArrayAdjacencyGraph
--------------------
Namespace ArrayAdjacencyGraph
--------------------
type ArrayAdjacencyGraph<'Vertex,'Label,'Edge (requires equality and equality)> =
private new : vertexEdges:Dictionary<'Vertex,LEdge<'Vertex,'Edge> []> * labels:Dictionary<'Vertex,'Label> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge> + 2 Überladungen
member AddEdge : LEdge<'Vertex,'Edge> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>
member AddManyEdges : edgeArray:seq<LEdge<'Vertex,'Edge>> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>
member AddManyVertices : vertices:LVertex<'Vertex,'Label> [] -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>
member AddVertex : LVertex<'Vertex,'Label> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>
member private AdjacencyGraph : unit -> Dictionary<'Vertex,LEdge<'Vertex,'Edge> []>
member ConnectedEdgesEmpty : v:'Vertex -> bool
member ContainsEdge : edge:LEdge<'Vertex,'Edge> -> bool
member ContainsVertex : vertex:'Vertex -> bool
member Copy : unit -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>
...
--------------------
new : unit -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>
new : vertices:LVertex<'Vertex,'Label> list * edges:LEdge<'Vertex,'Edge> list -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>
Modul Graph
aus FSharp.FGL.ArrayAdjacencyGraph
val create : vertexList:LVertex<'Vertex,'Label> list -> edgeList:LEdge<'Vertex,'Edge> list -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge> (requires equality and equality)
val myGraph' : ArrayAdjacencyGraph<int,string,float>
val vertexArray : LVertex<int,string> []
Modul Array
aus Microsoft.FSharp.Collections
val ofList : list:'T list -> 'T []
val edgeArray : LEdge<int,float> []
val myGraph'' : ArrayAdjacencyGraph<int,string,float>
Multiple items
Modul Vertices
aus FSharp.FGL.ArrayAdjacencyGraph
--------------------
Modul Vertices
aus FSharp.FGL
Multiple items
val addMany : vertices:LVertex<'Vertex,'Label> [] -> graph:ArrayAdjacencyGraph<'Vertex,'Label,'Edge> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge> (requires equality and equality)
--------------------
val addMany : vertices:LVertex<'Vertex,'Label> list -> g:Graph<'Vertex,'Label,'Edge> -> Graph<'Vertex,'Label,'Edge> (requires comparison)
Modul Edges
aus FSharp.FGL.ArrayAdjacencyGraph
val addMany : edgeArray:seq<LEdge<'Vertex,'Edge>> -> graph:ArrayAdjacencyGraph<'Vertex,'Label,'Edge> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge> (requires equality and equality)
val myGraph''' : ArrayAdjacencyGraph<int,string,float>
val emptyGraph : ArrayAdjacencyGraph<int,string,float>
member ArrayAdjacencyGraph.AddManyVertices : vertices:LVertex<'Vertex,'Label> [] -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>
member ArrayAdjacencyGraph.AddManyEdges : edgeArray:seq<LEdge<'Vertex,'Edge>> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>
val newVertex : LVertex<int,string>
val newEdge : LEdge<int,float>
Multiple items
val add : 'Vertex * 'Label -> graph:ArrayAdjacencyGraph<'Vertex,'Label,'Edge> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge> (requires equality and equality)
--------------------
val add : 'Vertex * 'Label -> g:Graph<'Vertex,'Label,'Edge> -> Graph<'Vertex,'Label,'Edge> (requires comparison)
val add : 'Vertex * 'Vertex * 'Edge -> graph:ArrayAdjacencyGraph<'Vertex,'Label,'Edge> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge> (requires equality and equality)
member ArrayAdjacencyGraph.AddVertex : LVertex<'Vertex,'Label> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>
member ArrayAdjacencyGraph.AddEdge : LEdge<'Vertex,'Edge> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>
val remove : 'Vertex * 'Vertex * 'Edge -> graph:ArrayAdjacencyGraph<'Vertex,'Label,'Edge> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge> (requires equality and equality)
Multiple items
val remove : v:'Vertex -> graph:ArrayAdjacencyGraph<'Vertex,'Label,'Edge> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge> (requires equality and equality)
--------------------
val remove : v:'Vertex -> g:Graph<'Vertex,'Label,'Edge> -> Graph<'Vertex,'Label,'Edge> (requires comparison)
member ArrayAdjacencyGraph.RemoveEdge : LEdge<'Vertex,'Edge> -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>
member ArrayAdjacencyGraph.RemoveVertex : v:'Vertex -> ArrayAdjacencyGraph<'Vertex,'Label,'Edge>