0 people like it.
Like the snippet!
Transitive Reduction of a graph
Implements a simple algorithm to compute the transitive reduction of a graph. The transitive reduction of a graph is the minimal set of edges with the same transitive closure
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:
|
open System
open System.Collections
open System.Collections.Generic
/// Adjacency list representation of a graph, mapping each vertex to its successors
type Graph<'a when 'a : comparison> = Map<'a, 'a[]>
/// A standard rose tree
type Tree<'a when 'a : comparison> =
{ root : 'a ; sub : Tree<'a>[] }
member this.ToSeq() =
seq { yield this.root
for t in this.sub do
yield! t }
interface IEnumerable<'a> with
member this.GetEnumerator() : IEnumerator<'a> =
this.ToSeq().GetEnumerator()
member this.GetEnumerator() : IEnumerator =
(this.ToSeq() :> IEnumerable).GetEnumerator()
/// Depth first search from the given root
let dfs (g: Graph<'a>) (root:'a) =
let rec go (n:'a) =
match g.TryFind n with
| None -> failwithf "node %A not found in graph" n
| Some nn -> { root = n ; sub = Array.map go nn}
go root
/// The transitive reduction of a graph is the minimal set of edges with the same transitive closure
let transitiveReduction(g:Graph<'a>) : Graph<'a> =
let dfss = Map.map (fun _ -> Seq.collect (dfs g) >> Set.ofSeq) g
let reachable n = Set.add n (dfss.[n])
g |> Map.map(fun n ss ->
ss
|> Array.mapi (fun i s ->
if Seq.exists (fun i -> reachable(ss.[i]).Contains s) (Seq.append [0..i-1] [i+1..ss.Length-1])
then None
else Some s
)
|> Array.choose id)
let g1 = Map.ofSeq <|
[ 0, [|1;2;3|]
1, [|2|]
2, [|3|]
3, [| |]
]
let g1' = transitiveReduction g1
|
namespace System
namespace System.Collections
namespace System.Collections.Generic
type Graph<'a (requires comparison)> = Map<'a,'a []>
Full name: Script.Graph<_>
Adjacency list representation of a graph, mapping each vertex to its successors
Multiple items
module Map
from Microsoft.FSharp.Collections
--------------------
type Map<'Key,'Value (requires comparison)> =
interface IEnumerable
interface IComparable
interface IEnumerable<KeyValuePair<'Key,'Value>>
interface ICollection<KeyValuePair<'Key,'Value>>
interface IDictionary<'Key,'Value>
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
member Add : key:'Key * value:'Value -> Map<'Key,'Value>
member ContainsKey : key:'Key -> bool
override Equals : obj -> bool
member Remove : key:'Key -> Map<'Key,'Value>
...
Full name: Microsoft.FSharp.Collections.Map<_,_>
--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
type Tree<'a (requires comparison)> =
{root: 'a;
sub: Tree<'a> [];}
interface IEnumerable<'a>
member ToSeq : unit -> seq<'a>
Full name: Script.Tree<_>
A standard rose tree
Tree.root: 'a
Tree.sub: Tree<'a> []
val this : Tree<'a> (requires comparison)
member Tree.ToSeq : unit -> seq<'a>
Full name: Script.Tree`1.ToSeq
Multiple items
val seq : sequence:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Core.Operators.seq
--------------------
type seq<'T> = IEnumerable<'T>
Full name: Microsoft.FSharp.Collections.seq<_>
val t : Tree<'a> (requires comparison)
Multiple items
type IEnumerable =
member GetEnumerator : unit -> IEnumerator
Full name: System.Collections.IEnumerable
--------------------
type IEnumerable<'T> =
member GetEnumerator : unit -> IEnumerator<'T>
Full name: System.Collections.Generic.IEnumerable<_>
override Tree.GetEnumerator : unit -> IEnumerator<'a>
Full name: Script.Tree`1.GetEnumerator
Multiple items
type IEnumerator =
member Current : obj
member MoveNext : unit -> bool
member Reset : unit -> unit
Full name: System.Collections.IEnumerator
--------------------
type IEnumerator<'T> =
member Current : 'T
Full name: System.Collections.Generic.IEnumerator<_>
member Tree.ToSeq : unit -> seq<'a>
override Tree.GetEnumerator : unit -> IEnumerator
Full name: Script.Tree`1.GetEnumerator
val dfs : g:Graph<'a> -> root:'a -> Tree<'a> (requires comparison)
Full name: Script.dfs
Depth first search from the given root
val g : Graph<'a> (requires comparison)
val root : 'a (requires comparison)
val go : ('a -> Tree<'a>) (requires comparison)
val n : 'a (requires comparison)
member Map.TryFind : key:'Key -> 'Value option
union case Option.None: Option<'T>
val failwithf : format:Printf.StringFormat<'T,'Result> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.failwithf
union case Option.Some: Value: 'T -> Option<'T>
val nn : 'a [] (requires comparison)
type Array =
member Clone : unit -> obj
member CopyTo : array:Array * index:int -> unit + 1 overload
member GetEnumerator : unit -> IEnumerator
member GetLength : dimension:int -> int
member GetLongLength : dimension:int -> int64
member GetLowerBound : dimension:int -> int
member GetUpperBound : dimension:int -> int
member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
member Initialize : unit -> unit
member IsFixedSize : bool
...
Full name: System.Array
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []
Full name: Microsoft.FSharp.Collections.Array.map
val transitiveReduction : g:Graph<'a> -> Graph<'a> (requires comparison)
Full name: Script.transitiveReduction
The transitive reduction of a graph is the minimal set of edges with the same transitive closure
val dfss : Map<'a,Set<'a>> (requires comparison)
val map : mapping:('Key -> 'T -> 'U) -> table:Map<'Key,'T> -> Map<'Key,'U> (requires comparison)
Full name: Microsoft.FSharp.Collections.Map.map
module Seq
from Microsoft.FSharp.Collections
val collect : mapping:('T -> #seq<'U>) -> source:seq<'T> -> seq<'U>
Full name: Microsoft.FSharp.Collections.Seq.collect
Multiple items
module Set
from Microsoft.FSharp.Collections
--------------------
type Set<'T (requires comparison)> =
interface IComparable
interface IEnumerable
interface IEnumerable<'T>
interface ICollection<'T>
new : elements:seq<'T> -> Set<'T>
member Add : value:'T -> Set<'T>
member Contains : value:'T -> bool
override Equals : obj -> bool
member IsProperSubsetOf : otherSet:Set<'T> -> bool
member IsProperSupersetOf : otherSet:Set<'T> -> bool
...
Full name: Microsoft.FSharp.Collections.Set<_>
--------------------
new : elements:seq<'T> -> Set<'T>
val ofSeq : elements:seq<'T> -> Set<'T> (requires comparison)
Full name: Microsoft.FSharp.Collections.Set.ofSeq
val reachable : ('a -> Set<'a>) (requires comparison)
val add : value:'T -> set:Set<'T> -> Set<'T> (requires comparison)
Full name: Microsoft.FSharp.Collections.Set.add
val ss : 'a [] (requires comparison)
val mapi : mapping:(int -> 'T -> 'U) -> array:'T [] -> 'U []
Full name: Microsoft.FSharp.Collections.Array.mapi
val i : int
val s : 'a (requires comparison)
val exists : predicate:('T -> bool) -> source:seq<'T> -> bool
Full name: Microsoft.FSharp.Collections.Seq.exists
val append : source1:seq<'T> -> source2:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.append
property Array.Length: int
val choose : chooser:('T -> 'U option) -> array:'T [] -> 'U []
Full name: Microsoft.FSharp.Collections.Array.choose
val id : x:'T -> 'T
Full name: Microsoft.FSharp.Core.Operators.id
val g1 : Map<int,int []>
Full name: Script.g1
val ofSeq : elements:seq<'Key * 'T> -> Map<'Key,'T> (requires comparison)
Full name: Microsoft.FSharp.Collections.Map.ofSeq
val g1' : Graph<int>
Full name: Script.g1'
More information