Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
27
Task/Dijkstras-algorithm/F-Sharp/dijkstras-algorithm-1.fs
Normal file
27
Task/Dijkstras-algorithm/F-Sharp/dijkstras-algorithm-1.fs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
//Dijkstra's algorithm: Nigel Galloway, August 5th., 2018
|
||||
[<CustomEquality;CustomComparison>]
|
||||
type Dijkstra<'N,'G when 'G:comparison>={toN:'N;cost:Option<'G>;fromN:'N}
|
||||
override g.Equals n =match n with| :? Dijkstra<'N,'G> as n->n.cost=g.cost|_->false
|
||||
override g.GetHashCode() = hash g.cost
|
||||
interface System.IComparable with
|
||||
member n.CompareTo g =
|
||||
match g with
|
||||
| :? Dijkstra<'N,'G> as n when n.cost=None -> (-1)
|
||||
| :? Dijkstra<'N,'G> when n.cost=None -> 1
|
||||
| :? Dijkstra<'N,'G> as g -> compare n.cost g.cost
|
||||
| _-> invalidArg "n" "expecting type Dijkstra<'N,'G>"
|
||||
let inline Dijkstra N G y =
|
||||
let rec fN l f=
|
||||
if List.isEmpty l then f
|
||||
else let n=List.min l
|
||||
if n.cost=None then f else
|
||||
fN(l|>List.choose(fun n'->if n'.toN=n.toN then None else match n.cost,n'.cost,Map.tryFind (n.toN,n'.toN) G with
|
||||
|Some g,None,Some wg ->Some {toN=n'.toN;cost=Some(g+wg);fromN=n.toN}
|
||||
|Some g,Some g',Some wg when g+wg<g'->Some {toN=n'.toN;cost=Some(g+wg);fromN=n.toN}
|
||||
|_ ->Some n'))((n.fromN,n.toN)::f)
|
||||
let r = fN (N|>List.map(fun n->{toN=n;cost=(Map.tryFind(y,n)G);fromN=y})) []
|
||||
(fun n->let rec fN z l=match List.tryFind(fun (_,g)->g=z) r with
|
||||
|Some(n',g') when y=n'->Some(n'::g'::l)
|
||||
|Some(n',g') ->fN n' (g'::l)
|
||||
|_ ->None
|
||||
fN n [])
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
type Node= |A|B|C|D|E|F
|
||||
let G=Map[((A,B),7);((A,C),9);((A,F),14);((B,C),10);((B,D),15);((C,D),11);((C,F),2);((D,E),6);((E,F),9)]
|
||||
let paths=Dijkstra [B;C;D;E;F] G A
|
||||
printfn "%A" (paths E)
|
||||
printfn "%A" (paths F)
|
||||
Loading…
Add table
Add a link
Reference in a new issue