-
Notifications
You must be signed in to change notification settings - Fork 11
/
Graph.fs
639 lines (511 loc) · 27.1 KB
/
Graph.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
namespace FSharp.FGL.ArrayAdjacencyGraph
open System.Collections.Generic
open FSharp.FGL
module internal Dictionary =
let tryGetValue k (dict:Dictionary<'K,'V>) =
let b,v = dict.TryGetValue(k)
if b then Some v
else None
let getValue k (dict:Dictionary<'K,'V>) =
match (tryGetValue k dict) with
|Some x -> x
|None -> failwith "Error get"
let copyRecursive (innerCopyF : 'V -> 'V) (dict:Dictionary<'K,'V>) =
let newDict = Dictionary<'K,'V>()
for kv in dict do
newDict.Add(kv.Key,innerCopyF kv.Value)
newDict
/// A mutable directed graph data structure efficient for large sparse graph representations
// Adaptation of https://github.com/YaccConstructor/QuickGraph.
type ArrayAdjacencyGraph<'Vertex,'Label,'Edge when 'Vertex : equality and 'Edge : equality> internal (vertexEdges:Dictionary<'Vertex,LEdge<'Vertex,'Edge>[]>, labels:Dictionary<'Vertex,'Label>) =
// Dictionary of the labels belonging to the vertices with (k,v) k-> vertex, v-> label.
let labels : Dictionary<'Vertex,'Label> = labels
// Dictionary of the edges belonging to the vertices with (k,v) k-> vertex, v-> edges.
let vertexOutEdges :Dictionary<'Vertex,LEdge<'Vertex,'Edge>[]> = vertexEdges
new () = ArrayAdjacencyGraph(new Dictionary<'Vertex,LEdge<'Vertex,'Edge>[]>(), new Dictionary<'Vertex,'Label>())
new(vertices : LVertex<'Vertex,'Label> list,edges : LEdge<'Vertex,'Edge> list) =
let vertexArray = vertices |> Array.ofList
let edgeArray = edges |> Array.ofList
let vertexEdges = System.Collections.Generic.Dictionary<'Vertex,LEdge<'Vertex,'Edge>[]>()
let labelDict = System.Collections.Generic.Dictionary<'Vertex,'Label>()
for i=0 to vertexArray.Length-1 do
let (vertex,label) = vertexArray.[i]
let edges =
//Array.filter (fun (s, t, w) -> s=vertex || t=vertex) edgeArray
edgeArray
|> Array.filter (fun (s, t, w) -> s=vertex || t=vertex)
//|> Array.map (fun (s,t,w) ->
// if s=t then [|(s,t,w);(s,t,w)|]
// else [|(s,t,w)|])
//|> Array.concat
labelDict.Add (vertex,label)
vertexEdges.Add (vertex,edges)
ArrayAdjacencyGraph(vertexEdges,labelDict)
///Copys the given graph.
member this.Copy() :ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
let copiedLabels = labels |> Dictionary.copyRecursive id
let newEdges = vertexOutEdges |> Dictionary.copyRecursive Array.copy
ArrayAdjacencyGraph(newEdges,copiedLabels)
member internal this.LabelMap() = labels
member internal this.AdjacencyGraph() = vertexOutEdges
//
//Edges
///Lookup the first edge in the graph that matches the conditions, returning a Some value if it exists and None if not.
member this.TryGetEdge((source:'Vertex),(target:'Vertex)) :LEdge<'Vertex,'Edge> option =
Dictionary.tryGetValue source vertexOutEdges
|> Option.bind (fun edges ->
edges
|> Array.tryFind (fun (s,t,e) -> s = source && t = target)
)
///Return the first edge in the graph that matches the conditions.
member this.GetEdge((source:'Vertex),(target:'Vertex)) :LEdge<'Vertex,'Edge> =
try
vertexOutEdges.Item source
|> Array.find (fun (s,t,e) -> s = source && t = target)
with
| _ -> failwithf "An edge from vertex %O to vertex %O does not exist in the graph" source target
///Lookup all edges in the graph that matches the conditions, returning a Some value if it exists and None if not.
member this.TryGetEdges((source:'Vertex),(target:'Vertex)) :LEdge<'Vertex,'Edge> [] option=
Dictionary.tryGetValue source vertexOutEdges
|> Option.map (fun edges ->
edges
|> Array.filter (fun (s,t,e) -> s = source && t = target)
)
///Return all edges in the graph that matches the conditions
member this.GetEdges((source:'Vertex),(target:'Vertex)) :LEdge<'Vertex,'Edge> [] =
try
vertexOutEdges.Item source
|> Array.filter (fun (s,t,e) -> s = source && t = target)
with
|_ -> failwithf "An edge from vertex %O to vertex %O does not exist in the graph" source target
///Returns all edges of the graph.
member this.GetEdges() :LEdge<'Vertex,'Edge>[]=
let result = Array.zeroCreate vertexOutEdges.Count
let mutable i = 0
for group in vertexOutEdges do
result.[i] <- group.Value
i <- i+1
result
|> Array.concat
|> Array.distinct
///Number of edges in the graph
member this.EdgeCount : int =
let mutable i = 0
for group in vertexOutEdges do
i <- i + group.Value.Length
i / 2
/////Returns true, if there are no edges in the graph, else false.
//member this.IsEdgesEmpty :bool =
// this.EdgeCount = 0
///Returns true, if the edge is found in the graph, else false.
member this.ContainsEdge(edge:LEdge<'Vertex,'Edge>) :bool =
let (source,target,weight) = edge
try
let array = vertexOutEdges.Item source
(Array.contains (edge) array) || (Array.contains (target,source,weight) array)
with
|_ -> false
///Lookup all edges connected to the vertex v in the graph, returning a Some value if a binding exists and None if not.
member this.TryGetConnectedEdges((v:'Vertex)) :LEdge<'Vertex,'Edge> [] option =
Dictionary.tryGetValue v vertexOutEdges
///Lookup all edges connected to the vertex v in the graph, returning an array of connected edges.
member this.GetConnectedEdges((v:'Vertex)) :LEdge<'Vertex,'Edge> [] =
try
vertexOutEdges.Item v
with
| _ -> failwithf "The vertex %O does not exist in the graph." v
///Lookup all edges that target the vertex v in the graph, returning a Some value if a binding exists and None if not.
member this.TryGetInEdges((v:'Vertex)) :LEdge<'Vertex,'Edge> [] option=
Dictionary.tryGetValue v vertexOutEdges
|> Option.map (fun x ->
x
|> Array.filter (fun (s, t, w) -> t=v)
)
///Lookup all edges that target the vertex v in the graph, returning an array of connected edges.
member this.GetInEdges((v:'Vertex)) :LEdge<'Vertex,'Edge> [] =
try
vertexOutEdges.Item v
|> Array.filter (fun (s, t, w) -> t=v)
with
|_ -> failwithf "The vertex %O does not exist in the graph." v
///Lookup all edges that originate from the vertex v in the graph, returning a Some value if a binding exists and None if not.
member this.TryGetOutEdges((v:'Vertex)) :LEdge<'Vertex,'Edge> [] option=
Dictionary.tryGetValue v vertexOutEdges
|> Option.map(fun x ->
x
|> Array.filter (fun (s, t, w) -> s=v)
)
///Lookup all edges that originate from the vertex v in the graph, returning an array of connected edges.
member this.GetOutEdges((v:'Vertex)) :LEdge<'Vertex,'Edge> [] =
try
vertexOutEdges.Item v
|> Array.filter (fun (s, t, w) -> s=v)
with
|_ -> failwithf "The vertex %O does not exist in the graph." v
///Lookup the weight of the edge defined by source and vertex. If it exists, return Some value, else none.
member this.TryGetWeight((source:'Vertex),(target:'Vertex)) :'Edge option=
this.TryGetEdge(source,target)
|> Option.map(fun (s, t, w) ->
w
)
///Return the weight of the edge defined by source and vertex
member this.GetWeight((source:'Vertex),(target:'Vertex)) :'Edge =
try
this.GetEdge (source,target)
|> (fun (s, t, w) -> w)
with
|_ -> failwithf "An edge from vertex %O to vertex %O does not exist in the graph" source target
//Problem, only returns first edge that equals the source and vertex
///Set the weight of the LEdge defined by source and vertex to be equal weight.
member this.SetWeight((source:'Vertex),(target:'Vertex),(weight:'Edge)) =
let sourceIndex =
try
vertexOutEdges.Item source
|> Array.findIndex (fun (s, t, w) -> t=target)
with
|_ -> failwithf "An edge from vertex %O to vertex %O does not exist in the graph" source target
let targetIndex =
try
vertexOutEdges.Item target
|> Array.findIndex (fun (s, t, w) -> s=source)
with
|_ -> failwithf "An edge from vertex %O to vertex %O does not exist in the graph" source target
vertexOutEdges.[source].[sourceIndex] <- (source,target,weight)
vertexOutEdges.[target].[targetIndex] <- (source,target,weight)
this
///Adds a labeled, edge to the graph.
member this.AddEdge((s, t, w): LEdge<'Vertex,'Edge>) =
let edgeArraySource =
try
vertexOutEdges.Item s
with
|_ -> failwithf "The source vertex %O of the edge does not exist in this graph." s
let edgeArrayTarget =
try
vertexOutEdges.Item t
with
|_ -> failwithf "The target vertex %O of the edge does not exist in this graph." t
vertexOutEdges.Item s <- (Array.concat [[|(s,t,w)|];edgeArraySource])
vertexOutEdges.Item t <- (Array.concat [[|(s,t,w)|];edgeArrayTarget])
this
///Adds an array of labeled, edges to the graph.
member this.AddManyEdges edgeArray =
for edge in edgeArray do
this.AddEdge edge |> ignore
this
///Removes an edge from the graph.
member this.RemoveEdge((s, t, w): LEdge<'Vertex,'Edge>) =
if s <> t then
let edgeArraySource =
try
vertexOutEdges.Item s
|> Array.filter (fun x -> x <> (s, t, w) )
with
|_ -> failwithf "The source vertex %O of the edge does not exist in this graph." s
let edgeArrayTarget =
try
vertexOutEdges.Item t
|> Array.filter (fun x -> x <> (s, t, w) )
with
|_ -> failwithf "The target vertex %O of the edge does not exist in this graph." t
vertexOutEdges.Item s <- (edgeArraySource)
vertexOutEdges.Item t <- (edgeArrayTarget)
else
let edgeArraySource =
try
vertexOutEdges.Item s
|> Array.filter (fun x -> x <> (s, t, w) )
with
|_ -> failwithf "The source vertex %O of the edge does not exist in this graph." s
vertexOutEdges.Item s <- (edgeArraySource)
this
///Removes an array of edges from the graph.
member this.RemoveManyEdges (edges:LEdge<'Vertex,'Edge>[]) =
for edge in edges do
this.RemoveEdge edge |> ignore
this
//Vertices
///Returns true, if the graph contains the vertex, else false.
member this.ContainsVertex(vertex:'Vertex) :bool =
vertexOutEdges.ContainsKey(vertex)
/////Returns true, if there are no vertices in the graph, else false.
//member this.IsVerticesEmpty :bool =
// vertexOutEdges.Count = 0
///Returns the number of vertices of the graph.
member this.VertexCount :int =
vertexOutEdges.Count
///Returns the vertices of the graph.
member this.GetVertices() :'Vertex[]=
let result = Array.zeroCreate vertexOutEdges.Count
let mutable i = 0
for group in vertexOutEdges do
result.[i] <- group.Key
i <- i+1
result
///Returns the degree of the vertex v.
member this.Degree(v:'Vertex) :int =
try
vertexOutEdges.Item v
//|> Array.length
|> Array.sumBy (fun (s,t,w) -> if s=t then 2 else 1)
with
|_ -> failwithf "The vertex %O does not exist in the graph." v
///Returns the number of edges that originate from the vertex v.
member this.InDegree((v:'Vertex)) =
//this.TryGetInEdges v
//|> Option.map (Array.length)
this.GetInEdges v
|> Array.length
///Returns the number of edges that target the vertex v.
member this.OutDegree((v:'Vertex)) =
//this.TryGetOutEdges v
//|> Option.map (Array.length)
this.GetOutEdges v
|> Array.length
///Returns the weighted degree of the vertex v.
member this.WeightedDegree(weightingF : 'Edge -> float,v:'Vertex) :float =
try
vertexOutEdges.Item v
|> Array.sumBy (fun (s,t,w) -> if s = t then (weightingF w) * 2. else weightingF w)
//|> weightingF
with
| _ -> failwithf "The vertex %O does not exist in the graph." v
///Returns true, if the vertex v does not have edges connected to it. Otherwise, it returns false.
member this.ConnectedEdgesEmpty(v:'Vertex) :bool =
this.Degree(v) = 0
///Adds a labeled vertex to the graph.
member this.AddVertex ((v, l): LVertex<'Vertex,'Label>)=
vertexOutEdges.Add (v,[||]) |> ignore
labels.Add (v, l) |> ignore
this
///Adds an array of labeled vertices to the graph.
member this.AddManyVertices (vertices:LVertex<'Vertex,'Label>[]) =
for vertex in vertices do
this.AddVertex vertex |> ignore
this
///Removes a vertex from the graph.
member this.RemoveVertex (v:'Vertex) =
vertexOutEdges.Item v
|> this.RemoveManyEdges
|> ignore
vertexOutEdges.Remove (v) |> ignore
labels.Remove (v) |> ignore
this
///Removes an array of vertices from the graph.
member this.RemoveManyVertices (vertices:'Vertex[]) =
for vertex in vertices do
this.RemoveVertex vertex |> ignore
this
///Returns Some vertices if they are predecessors of the vertex, else None.
member this.TryPredecessors(v:'Vertex) :'Vertex[] option =
this.TryGetConnectedEdges v
|> Option.map (fun x ->
x
|> Array.choose (fun (s,t,w) -> if t=v then Some s else None)
|> Array.distinct
)
///Returns the preceding vertices of the vertex.
member this.Predecessors(v:'Vertex) :'Vertex[] =
try
this.GetConnectedEdges v
|> Array.choose (fun (s,t,w) -> if t=v then Some s else None)
|> Array.distinct
with
|_ -> failwithf "The vertex %O does not exist in the graph." v
///Returns Some vertices if they are successors of the vertex, else None.
member this.TrySuccessors(v:'Vertex) :'Vertex[] option =
this.TryGetConnectedEdges v
|> Option.map (fun x ->
x
|> Array.choose (fun (s,t,w) -> if s=v then Some t else None)
|> Array.distinct
)
///Returns the succeeding vertices of the vertex.
member this.Successors(v:'Vertex) :'Vertex[] =
try
this.GetConnectedEdges v
|> Array.choose (fun (s,t,w) -> if s=v then Some t else None)
|> Array.distinct
with
|_ -> failwithf "The vertex %O does not exist in the graph." v
///Returns Some vertices if they are neighbours of the vertex, else None.
member this.TryNeighbours(v:'Vertex) :'Vertex[] option =
this.TryGetConnectedEdges v
|> Option.map (fun x ->
x
|> Array.map (fun (s,t,w) -> if s = v then t else s)
|> Array.distinct
)
///Returns the neighbouring edges of the vertex v.
member this.Neighbours(v:'Vertex) :'Vertex[] =
try
this.GetConnectedEdges v
|> Array.map (fun (s,t,w) -> if s = v then t else s)
|> Array.distinct
with
|_ -> failwithf "The vertex %O does not exist in the graph." v
//Label
///Returns Some label, if a label for the vertex v exists, else none.
member this.TryGetLabel(v:'Vertex) :'Label option =
Dictionary.tryGetValue v labels
///Returns the label for the vertex v.
member this.GetLabel(v:'Vertex) :'Label =
try
labels.Item v
with
| _ -> failwithf "The vertex %O does not exist in the graph." v
///Sets the label for the vertex v.
member this.SetLabel((v:'Vertex),(l:'Label)) =
labels.Item v <- (l)
this
///Returns all labels of the graph.
member this.GetLabels() :'Label []=
let result = Array.zeroCreate labels.Count
let mutable i = 0
for group in labels do
result.[i] <- group.Value
i <- i+1
result
module Graph =
///Create an ArrayAdjacencyGraph based on the given vertex list and edge list.
let create (vertexList : LVertex<'Vertex,'Label> list) (edgeList : LEdge<'Vertex,'Edge> list) : ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
ArrayAdjacencyGraph(vertexList,edgeList)
///Create an ArrayAdjacencyGraph based on the given vertex list and edge list by adding each edge via the AddEdge function.
let createOfEdgelist (vertexList : LVertex<'Vertex,'Label> list) (edgeList : LEdge<'Vertex,'Edge> list) : ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
let graph = ArrayAdjacencyGraph(vertexList,[])
for i in edgeList do
graph.AddEdge i
|> ignore
graph
module Vertices =
//Vertices
///Returns true, if the graph contains the vertex, else false.
let contains (vertex:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :bool =
graph.ContainsVertex vertex
///Returns the number of vertices of the graph.
let count (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :int =
graph.VertexCount
///Returns the vertices of the graph.
let toVertexList (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :'Vertex[]=
graph.GetVertices()
///Returns the degree of the vertex v.
let degree (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :int =
graph.Degree v
///Returns the number of edges that originate from the vertex v.
let inDegree (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :int =
graph.InDegree v
///Returns the number of edges that target the vertex v.
let outDegree (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :int =
graph.OutDegree v
///Returns the weighted degree of the vertex v.
let weightedDegree (weightingF : 'Edge -> float) (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :float =
graph.WeightedDegree (weightingF,v)
///Returns false if the vertex v does not have edges connected to it. Otherwise, it returns true.
let isConnectedToEdges (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :bool =
not (graph.ConnectedEdgesEmpty v)
///Returns a new graph with the given vertex added.
let add ((v, l): LVertex<'Vertex,'Label>) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) : ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
(graph.Copy()).AddVertex(v, l)
///Returns a new graph with the given vertices added.
let addMany (vertices:LVertex<'Vertex,'Label>[]) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) : ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
(graph.Copy()).AddManyVertices(vertices)
///Returns a new graph where the given vertex is removed.
let remove (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) : ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
(graph.Copy()).RemoveVertex(v)
///Returns a new graph where the given vertices are removed.
let removeMany (vertices:'Vertex[]) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) : ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
(graph.Copy()).RemoveManyVertices(vertices)
///Returns Some vertices if they are predecessors of the vertex, else None.
let tryPredecessors (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :'Vertex[] option =
graph.TryPredecessors v
///Returns the preceding vertices of the vertex.
let predecessors (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :'Vertex[] =
graph.Predecessors v
///Returns Some vertices if they are successors of the vertex, else None.
let trySuccessors (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :'Vertex[] option =
graph.TrySuccessors v
///Returns the succeeding vertices of the vertex.
let successors (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :'Vertex[] =
graph.Successors v
///Returns Some vertices if they are neighbours of the vertex, else None.
let tryNeighbours (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :'Vertex[] option =
graph.TryNeighbours v
///Returns the neighbouring edges of the vertex v.
let neighbours (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :'Vertex[] =
graph.Neighbours v
//Label
///Returns Some label, if a label for the vertex v exists, else none.
let tryGetLabel (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :'Label option =
graph.TryGetLabel v
///Returns the label for the vertex v.
let getLabel (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :'Label =
graph.GetLabel v
///Retunrs a new graph where the label for the vertex v is the given label.
let setLabel (v:'Vertex) (l:'Label) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
(graph.Copy()).SetLabel(v,l)
///Returns all labels of the graph.
let getLabelList (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :'Label []=
graph.GetLabels()
module Edges =
//Edges
///Lookup the first edge in the graph that matches the conditions, returning a Some value if it exists and None if not.
let tryGet (source:'Vertex) (target:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :LEdge<'Vertex,'Edge> option =
graph.TryGetEdge(source,target)
///Return the first edge in the graph that matches the conditions.
let get (source:'Vertex) (target:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :LEdge<'Vertex,'Edge> =
graph.GetEdge(source,target)
///Lookup all edges in the graph that matches the conditions, returning a Some value if it exists and None if not.
let tryGetMany (source:'Vertex) (target:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :LEdge<'Vertex,'Edge> [] option=
graph.TryGetEdges(source,target)
///Return all edges in the graph that matches the conditions
let getMany (source:'Vertex) (target:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :LEdge<'Vertex,'Edge> [] =
graph.GetEdges(source,target)
///Returns all edges of the graph.
let toEdgeList (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :LEdge<'Vertex,'Edge>[]=
graph.GetEdges()
///Number of edges in the graph
let count (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) : int =
graph.EdgeCount
///Returns true if the edge is found in the graph, else false.
let contains (edge:LEdge<'Vertex,'Edge>) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :bool =
graph.ContainsEdge edge
///Lookup all edges connected to the vertex v in the graph, returning a Some value if a binding exists and None if not.
let tryGetConnected (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :LEdge<'Vertex,'Edge> [] option =
graph.TryGetConnectedEdges v
///Lookup all edges connected to the vertex v in the graph, returning an array of connected edges.
let getConnected (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :LEdge<'Vertex,'Edge> [] =
graph.GetConnectedEdges v
///Lookup all edges that target the vertex v in the graph, returning a Some value if a binding exists and None if not.
let tryGetInEdges (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :LEdge<'Vertex,'Edge> [] option=
graph.TryGetInEdges v
///Lookup all edges that target the vertex v in the graph, returning an array of connected edges.
let getInEdges (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :LEdge<'Vertex,'Edge> [] =
graph.GetInEdges v
///Lookup all edges that originate from the vertex v in the graph, returning a Some value if a binding exists and None if not.
let tryGetOutEdges (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :LEdge<'Vertex,'Edge> [] option=
graph.TryGetOutEdges v
///Lookup all edges that originate from the vertex v in the graph, returning an array of connected edges.
let getOutEdges (v:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :LEdge<'Vertex,'Edge> [] =
graph.GetOutEdges v
///Lookup the weight of the edge defined by source and vertex. If it exists, return Some value, else none.
let tryGetWeight (source:'Vertex) (target:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :'Edge option=
graph.TryGetWeight(source,target)
///Return the weight of the edge defined by source and vertex
let getWeight (source:'Vertex) (target:'Vertex) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) :'Edge =
graph.GetWeight(source,target)
//Problem, only returns first edge that equals the source and vertex
///Return a new graph where the weight for the given edge was adapted.
let setWeight ((source:'Vertex),(target:'Vertex),(weight:'Edge)) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) : ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
(graph.Copy()).SetWeight(source,target,weight)
///Returns a new graph where the given edge was added.
let add ((s, t, w) : LEdge<'Vertex,'Edge>) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) : ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
(graph.Copy()).AddEdge(s,t,w)
///Returns a new graph where the given edges were added.
let addMany edgeArray (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) : ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
(graph.Copy()).AddManyEdges(edgeArray)
///Returns a new graph where the given edge was removed from the original graph.
let remove ((s, t, w): LEdge<'Vertex,'Edge>) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) : ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
(graph.Copy()).RemoveEdge(s, t, w)
///Returns a new graph where the given edges were removed from the original graph.
let removeMany (edges:LEdge<'Vertex,'Edge>[]) (graph: ArrayAdjacencyGraph<'Vertex,'Label,'Edge>) : ArrayAdjacencyGraph<'Vertex,'Label,'Edge> =
(graph.Copy()).RemoveManyEdges(edges)