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:
55:
56:
57:
58:
59:
60:
|
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[]>
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 dfsF (g: Graph<'a>) k (root:'a) =
match g.TryFind root with
| None -> {root = root ; sub = [||]}
| Some nn -> { root = root ; sub = Array.map k nn}
let rec fix f x = f (fix f) x
let dfs g root = fix (dfsF g) 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> =
// dfs for every node
let dfss = g |> Map.map (fun n _ -> dfs g n |> Set.ofSeq)
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
[<EntryPoint>]
let main argv =
printfn "%A" g1'
0 // return an integer exit code
|
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<_>
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 dfsF : g:Graph<'a> -> k:('a -> Tree<'a>) -> root:'a -> Tree<'a> (requires comparison)
Full name: Script.dfsF
Depth first search from the given root
val g : Graph<'a> (requires comparison)
val k : ('a -> Tree<'a>) (requires comparison)
val root : 'a (requires comparison)
member Map.TryFind : key:'Key -> 'Value option
union case Option.None: Option<'T>
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 fix : f:(('a -> 'b) -> 'a -> 'b) -> x:'a -> 'b
Full name: Script.fix
val f : (('a -> 'b) -> 'a -> 'b)
val x : 'a
val dfs : g:Graph<'a> -> root:'a -> Tree<'a> (requires comparison)
Full name: Script.dfs
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
val n : 'a (requires comparison)
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)
module Seq
from Microsoft.FSharp.Collections
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'
Multiple items
type EntryPointAttribute =
inherit Attribute
new : unit -> EntryPointAttribute
Full name: Microsoft.FSharp.Core.EntryPointAttribute
--------------------
new : unit -> EntryPointAttribute
val main : argv:string [] -> int
Full name: Script.main
val argv : string []
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
More information