forked from logicchains/LPATHBench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.fs
More file actions
50 lines (44 loc) · 1.64 KB
/
fs.fs
File metadata and controls
50 lines (44 loc) · 1.64 KB
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
module LongestPath
open System
open System.IO
type Route =
struct
val Dest : int
val Cost : int
new (dest : int, cost : int) = { Dest = dest; Cost = cost}
end
type Node = Route array
let readPlaces () =
let lines =
use stream = File.OpenRead(@"agraph")
use reader = new StreamReader(stream)
(reader.ReadToEnd()).Split ([|"\n"|], System.StringSplitOptions.None)
let numNodes = lines.[0] |> int
let nodes = Array.create numNodes [||]
lines.[1..]
|> Array.map(fun l -> l.Split ([|" "|], System.StringSplitOptions.None))
|> Array.filter (fun l -> l.Length > 2)
|> Array.fold(fun (acc : Node[]) v ->
let node, neighbour, cost = (int v.[0], int v.[1], int v.[2])
acc.[node] <- Array.append [| Route(neighbour, cost) |] acc.[node]
acc) nodes
let rec getLongestPath (nodes : Node array) nodeId (visited : bool array) =
visited.[nodeId] <- true;
let max = ref 0
nodes.[nodeId] |> Array.iter (fun neighbour->
if (not visited.[neighbour.Dest])
then
let dist = neighbour.Cost + getLongestPath nodes neighbour.Dest visited
if dist > !max then max := dist)
visited.[nodeId] <- false;
!max
[<EntryPoint>]
let main argv =
let nodes = readPlaces()
let visited = Array.zeroCreate nodes.Length
let sw = System.Diagnostics.Stopwatch.StartNew()
let len = getLongestPath nodes 0 visited
sw.Stop()
let duration = sw.ElapsedMilliseconds
printf "%d LANGUAGE FSharp %d\n" len duration
0